admin.js
305 lines
| 1 | (function() { |
| 2 | function compressImage(event) { |
| 3 | var element = jQuery(event.target); |
| 4 | var container = element.closest('div.tiny-ajax-container'); |
| 5 | element.attr('disabled', 'disabled'); |
| 6 | container.find('span.spinner').removeClass('hidden'); |
| 7 | container.find('span.dashicons').remove(); |
| 8 | jQuery.ajax({ |
| 9 | url: ajaxurl, |
| 10 | type: 'POST', |
| 11 | data: { |
| 12 | _nonce: tinyCompress.nonce, |
| 13 | action: 'tiny_compress_image_from_library', |
| 14 | id: element.data('id') || element.attr('data-id') |
| 15 | }, |
| 16 | success: function(data) { |
| 17 | container.html(data); |
| 18 | }, |
| 19 | error: function() { |
| 20 | element.removeAttr('disabled'); |
| 21 | container.find('span.spinner').addClass('hidden'); |
| 22 | } |
| 23 | }); |
| 24 | } |
| 25 | |
| 26 | function compressImageSelection() { |
| 27 | jQuery('span.auto-compress').each(function(index, element) { |
| 28 | jQuery(element).siblings('button').click() |
| 29 | }); |
| 30 | } |
| 31 | |
| 32 | function watchCompressingImages() { |
| 33 | if (jQuery('.details-container[data-status="compressing"]').length > 0) { |
| 34 | statusCheckIntervalId = setInterval(checkCompressingImages, 5000); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | function checkCompressingImages() { |
| 39 | jQuery('.details-container[data-status="compressing"]').each(function(index, element) { |
| 40 | element = jQuery(element); |
| 41 | var container = element.closest('div.tiny-ajax-container'); |
| 42 | jQuery.ajax({ |
| 43 | url: ajaxurl, |
| 44 | type: 'POST', |
| 45 | data: { |
| 46 | _nonce: tinyCompress.nonce, |
| 47 | action: 'tiny_get_compression_status', |
| 48 | id: element.attr('data-id') |
| 49 | }, |
| 50 | success: function(data) { |
| 51 | container.html(data); |
| 52 | if (jQuery('.details-container[data-status="compressing"]').length === 0) { |
| 53 | clearInterval(statusCheckIntervalId); |
| 54 | } |
| 55 | }, |
| 56 | error: function() { |
| 57 | element.removeAttr('disabled'); |
| 58 | container.find('span.spinner').addClass('hidden'); |
| 59 | } |
| 60 | }); |
| 61 | }); |
| 62 | } |
| 63 | |
| 64 | function toggleChangeKey(event) { |
| 65 | jQuery('div.tiny-account-status div.update').toggle(); |
| 66 | jQuery('div.tiny-account-status div.status').toggle(); |
| 67 | jQuery('div.tiny-account-status div.upgrade').toggle(); |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | function submitKey(event) { |
| 72 | event.preventDefault(); |
| 73 | jQuery(event.target).attr({disabled: true}).addClass('loading'); |
| 74 | |
| 75 | var action; |
| 76 | var parent = jQuery(event.target).closest('div'); |
| 77 | var key; |
| 78 | var email; |
| 79 | var name; |
| 80 | |
| 81 | if (jQuery(event.target).data('tiny-action') === 'update-key') { |
| 82 | action = 'update'; |
| 83 | key = parent.find('#tinypng_api_key').val(); |
| 84 | } else if (jQuery(event.target).data('tiny-action') === 'create-key') { |
| 85 | action = 'create'; |
| 86 | name = parent.find('#tinypng_api_key_name').val(); |
| 87 | email = parent.find('#tinypng_api_key_email').val(); |
| 88 | } else { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | jQuery.ajax({ |
| 93 | url: ajaxurl, |
| 94 | type: 'POST', |
| 95 | data: { |
| 96 | _nonce: tinyCompress.nonce, |
| 97 | action: 'tiny_settings_' + action + '_api_key', |
| 98 | key: key, |
| 99 | name: name, |
| 100 | email: email |
| 101 | }, |
| 102 | success: function(json) { |
| 103 | var status = jQuery.parseJSON(json); |
| 104 | |
| 105 | if (status.ok) { |
| 106 | var target = jQuery('#tiny-account-status'); |
| 107 | if (target.length) { |
| 108 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 109 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 110 | target.replaceWith(data); |
| 111 | }); |
| 112 | } |
| 113 | jQuery('div.tiny-notice[data-name="setting"]').remove(); |
| 114 | } else { |
| 115 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 116 | parent.addClass('failure'); |
| 117 | parent.find('p.message').text(status.message).show(); |
| 118 | } |
| 119 | }, |
| 120 | error: function() { |
| 121 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 122 | parent.addClass('failure'); |
| 123 | parent.find('p.message').text('Something went wrong, try again soon').show(); |
| 124 | } |
| 125 | }); |
| 126 | |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | function dismissNotice(event) { |
| 131 | var element = jQuery(event.target); |
| 132 | var notice = element.closest('.tiny-notice'); |
| 133 | element.attr('disabled', 'disabled'); |
| 134 | jQuery.ajax({ |
| 135 | url: ajaxurl, |
| 136 | type: 'POST', |
| 137 | dataType: 'json', |
| 138 | data: { |
| 139 | _nonce: tinyCompress.nonce, |
| 140 | action: 'tiny_dismiss_notice', |
| 141 | name: notice.data('name') || notice.attr('data-name') |
| 142 | }, |
| 143 | success: function(data) { |
| 144 | if (data) { |
| 145 | notice.remove(); |
| 146 | } |
| 147 | }, |
| 148 | error: function() { |
| 149 | element.removeAttr('disabled'); |
| 150 | } |
| 151 | }); |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | function updateResizeSettings() { |
| 156 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 157 | jQuery('.tiny-resize-available').show(); |
| 158 | jQuery('.tiny-resize-unavailable').hide(); |
| 159 | } else { |
| 160 | jQuery('.tiny-resize-available').hide(); |
| 161 | jQuery('.tiny-resize-unavailable').show(); |
| 162 | } |
| 163 | |
| 164 | var original_enabled = propOf('#tinypng_resize_original_enabled', 'checked'); |
| 165 | jQuery('#tinypng_resize_original_width, #tinypng_resize_original_height').each(function (i, el) { |
| 166 | el.disabled = !original_enabled; |
| 167 | }); |
| 168 | } |
| 169 | |
| 170 | function updatePreserveSettings() { |
| 171 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 172 | jQuery('.tiny-preserve').show(); |
| 173 | } else { |
| 174 | jQuery('.tiny-preserve').hide(); |
| 175 | jQuery('#tinypng_preserve_data_creation').attr('checked', false); |
| 176 | jQuery('#tinypng_preserve_data_copyright').attr('checked', false); |
| 177 | jQuery('#tinypng_preserve_data_location').attr('checked', false); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | function updateSettings() { |
| 182 | updateResizeSettings(); |
| 183 | updatePreserveSettings(); |
| 184 | } |
| 185 | |
| 186 | var adminpage = ''; |
| 187 | if (typeof window.adminpage !== 'undefined') { |
| 188 | adminpage = window.adminpage; |
| 189 | } |
| 190 | |
| 191 | var statusCheckIntervalId; |
| 192 | |
| 193 | function eventOn(event, eventSelector, callback) { |
| 194 | if (typeof jQuery.fn.on === 'function') { |
| 195 | jQuery(document).on(event, eventSelector, callback); |
| 196 | } else { |
| 197 | jQuery(eventSelector).live(event, callback); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | function propOf(selector, property) { |
| 202 | if (typeof jQuery.fn.prop === 'function') { |
| 203 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 204 | property values into account. */ |
| 205 | return jQuery(selector).prop(property); |
| 206 | } else { |
| 207 | return jQuery(selector).attr(property); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | function setPropOf(selector, property, value) { |
| 212 | if (typeof jQuery.fn.prop === 'function') { |
| 213 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 214 | property values into account. */ |
| 215 | jQuery(selector).prop(property, value); |
| 216 | } else { |
| 217 | jQuery(selector).attr(property, value); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | function changeEnterKeyTarget(selector, button) { |
| 222 | eventOn('keyup keypress', selector, function(event) { |
| 223 | var code = event.keyCode || event.which; |
| 224 | if (code === 13) { |
| 225 | jQuery(button).click(); |
| 226 | return false; |
| 227 | } |
| 228 | }); |
| 229 | } |
| 230 | |
| 231 | switch (adminpage) { |
| 232 | case 'upload-php': |
| 233 | eventOn('click', 'button.tiny-compress', compressImage); |
| 234 | |
| 235 | setPropOf('button.tiny-compress', 'disabled', null); |
| 236 | |
| 237 | compressImageSelection(); |
| 238 | watchCompressingImages(); |
| 239 | |
| 240 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]'); |
| 241 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]'); |
| 242 | break; |
| 243 | case 'post-php': |
| 244 | eventOn('click', 'button.tiny-compress', compressImage); |
| 245 | break; |
| 246 | case 'settings_page_tinify': |
| 247 | changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]'); |
| 248 | changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]'); |
| 249 | |
| 250 | eventOn('click', '[data-tiny-action=create-key]', submitKey); |
| 251 | eventOn('click', '[data-tiny-action=update-key]', submitKey); |
| 252 | eventOn('click', '#change-key', toggleChangeKey); |
| 253 | eventOn('click', '#cancel-change-key', toggleChangeKey); |
| 254 | |
| 255 | var target = jQuery('#tiny-account-status[data-state=pending]'); |
| 256 | if (target.length) { |
| 257 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 258 | target.replaceWith(data); |
| 259 | }); |
| 260 | } |
| 261 | |
| 262 | eventOn('click', 'input[name*=tinypng_sizes], #tinypng_resize_original_enabled', function() { |
| 263 | /* Unfortunately, we need some additional information to display |
| 264 | the correct notice. */ |
| 265 | var totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length; |
| 266 | var compressWr2x = propOf('#tinypng_sizes_wr2x', 'checked'); |
| 267 | if (compressWr2x) { |
| 268 | totalSelectedSizes--; |
| 269 | } |
| 270 | |
| 271 | var image_count_url = ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes; |
| 272 | if (propOf('#tinypng_resize_original_enabled', 'checked') && propOf('#tinypng_sizes_0', 'checked')) { |
| 273 | image_count_url += '&resize_original=true'; |
| 274 | } |
| 275 | if (compressWr2x) { |
| 276 | image_count_url += '&compress_wr2x=true'; |
| 277 | } |
| 278 | jQuery('#tiny-image-sizes-notice').load(image_count_url); |
| 279 | }); |
| 280 | |
| 281 | eventOn('click', '#tinypng_auto_compress_enabled', function() { |
| 282 | updateSettings(); |
| 283 | }); |
| 284 | |
| 285 | jQuery('#tinypng_sizes_0, #tinypng_resize_original_enabled').click(updateSettings); |
| 286 | updateSettings(); |
| 287 | } |
| 288 | |
| 289 | jQuery('.tiny-notice a.tiny-dismiss').click(dismissNotice); |
| 290 | jQuery(function() { |
| 291 | jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice); |
| 292 | }); |
| 293 | |
| 294 | |
| 295 | function onConvertChange(e) { |
| 296 | const newValue = e.target.checked; |
| 297 | if (newValue) { |
| 298 | jQuery('#tinypng_convert_convert_to').removeAttr('disabled'); |
| 299 | } else { |
| 300 | jQuery('#tinypng_convert_convert_to').attr('disabled', true); |
| 301 | } |
| 302 | } |
| 303 | jQuery('#tinypng_conversion_convert').on('change', onConvertChange); |
| 304 | }).call(); |
| 305 |