1/* jshint esversion: 6 */ 2 3/* 4 Provide a means to listen for class changes made to an identified JQuery object. This allows for e.g. making decisions 5 when classes are added to or removed from a JQuery object's element 6 7 This can be used via the following where the passed newClass will be the current class value of TARGET: 8 9 $("TARGET").classChange(function(el, newClass) {}) 10 */ 11$.fn.classChange = function (cb) { 12 return $(this).each(function (_, el) { 13 new MutationObserver(function (mutations) { 14 mutations.forEach(function (mutation) { 15 return cb && cb(mutation.target, $(mutation.target).prop(mutation.attributeName)) 16 }); 17 }).observe(el, { 18 attributes: true, 19 attributeFilter: ['class'] // only listen for class attribute changes 20 }); 21 }); 22} 23 24/* 25 Provide the means for a file to be downloaded after a button click is executed. The file download should still occur 26 from a link experience, so we'll establish a temporary link for the sake of downloading the document and remove the link 27 once we fake an interaction with it. Additionally, disable the button while the link interaction is occurring to prevent 28 the notion of immediate double-clicking. The user will be targeted to a blank page where the file is downloading too. 29 This is meant to prevent the attempt to make the same request for resources which can be computationally involved when 30 considering file generation 31 */ 32$.fn.buttonFileDownload = function() { 33 const button = $(this); 34 35 button.click(function() { 36 const url = button.data('file-download-url'); 37 38 if (typeof url === 'string' && url.length > 0) { 39 button.prop('disabled', true); 40 41 $.ajax({ 42 url: url, 43 type: 'GET', 44 xhr: function () { 45 const xhr = new XMLHttpRequest(); 46 xhr.responseType = 'blob'; 47 48 return xhr; 49 } 50 }) 51 .then( 52 function (data, textStatus, jqXHR) { 53 const filenameHeader = jqXHR.getResponseHeader('content-disposition'); 54 55 if (typeof filenameHeader === 'string' && filenameHeader.length > 0) { 56 const filename = /attachment; filename="(.*)"/g.exec(filenameHeader)[1]; 57 58 const documentBody = $('body'); 59 const link = $('<a />'); 60 61 link.attr('download', filename); 62 link.attr('href', window.URL.createObjectURL(data)); 63 64 documentBody.append(link); 65 link[0].click(); 66 link.remove(); 67 } else { 68 alert('Could not prepare file information'); 69 } 70 71 button.prop('disabled', false); 72 }, 73 function () { 74 button.prop('disabled', false); 75 76 alert('Could not complete download'); 77 } 78 ); 79 } 80 }); 81} 82 83/* 84 Provide a lightweight function that is meant to be applicable to buttons that engage in endpoints that enqueue jobs 85 to the queue system. Ensure these buttons are disabled after initial click so that we don't doubly enqueue 86 */ 87$.fn.buttonEnqueueRequest = function ( 88 { 89 requestFailureMessage = 'Could not enqueue', 90 requestDefaultErrorMessage = 'Could not enqueue', 91 requestDefaultSuccessMessage = 'Successfully enqueued', 92 successCallback = (successMessage) => { 93 alert(successMessage); 94 } 95 } = {} 96) { 97 const button = $(this); 98 99 button.click(function() { 100 const url = button.data('enqueue-url'); 101 102 if (typeof url === 'string' && url.length > 0) { 103 button.prop('disabled', true); 104 105 $.ajax({ 106 url: url, 107 type: 'GET' 108 }) 109 .then( 110 function (data, textStatus, jqXHR) { 111 const success = data.hasOwnProperty('success') && data.success; 112 const message = data.hasOwnProperty('message') && typeof data.message === 'string' ? 113 data.message : ''; 114 115 if (success) { 116 successCallback(message.length > 0 ? message : requestDefaultSuccessMessage); 117 } else { 118 alert(message.length ? message : requestDefaultErrorMessage); 119 } 120 121 button.prop('disabled', false); 122 }, 123 function () { 124 button.prop('disabled', false); 125 126 alert(requestFailureMessage); 127 } 128 ); 129 } 130 }); 131}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.