admin.js
287 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 submitKey(event) { |
| 65 | event.preventDefault(); |
| 66 | jQuery(event.target).attr({disabled: true}).addClass('loading'); |
| 67 | |
| 68 | var action; |
| 69 | var parent = jQuery(event.target).closest('div'); |
| 70 | var key; |
| 71 | var email; |
| 72 | var name; |
| 73 | |
| 74 | if (jQuery(event.target).data('tiny-action') === 'update-key') { |
| 75 | action = 'update'; |
| 76 | key = parent.find('#tinypng_api_key').val(); |
| 77 | } else if (jQuery(event.target).data('tiny-action') === 'create-key') { |
| 78 | action = 'create'; |
| 79 | name = parent.find('#tinypng_api_key_name').val(); |
| 80 | email = parent.find('#tinypng_api_key_email').val(); |
| 81 | } else { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | jQuery.ajax({ |
| 86 | url: ajaxurl, |
| 87 | type: 'POST', |
| 88 | data: { |
| 89 | _nonce: tinyCompress.nonce, |
| 90 | action: 'tiny_settings_' + action + '_api_key', |
| 91 | key: key, |
| 92 | name: name, |
| 93 | email: email |
| 94 | }, |
| 95 | success: function(json) { |
| 96 | var status = jQuery.parseJSON(json); |
| 97 | |
| 98 | if (status.ok) { |
| 99 | var target = jQuery('#tiny-account-status'); |
| 100 | if (target.length) { |
| 101 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 102 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 103 | target.replaceWith(data); |
| 104 | }); |
| 105 | } |
| 106 | jQuery('div.tiny-notice[data-name="setting"]').remove(); |
| 107 | } else { |
| 108 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 109 | parent.addClass('failure'); |
| 110 | parent.find('p.message').text(status.message).show(); |
| 111 | } |
| 112 | }, |
| 113 | error: function() { |
| 114 | jQuery(event.target).attr({disabled: false}).removeClass('loading'); |
| 115 | parent.addClass('failure'); |
| 116 | parent.find('p.message').text('Something went wrong, try again soon').show(); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | function dismissNotice(event) { |
| 124 | var element = jQuery(event.target); |
| 125 | var notice = element.closest('.tiny-notice'); |
| 126 | element.attr('disabled', 'disabled'); |
| 127 | jQuery.ajax({ |
| 128 | url: ajaxurl, |
| 129 | type: 'POST', |
| 130 | dataType: 'json', |
| 131 | data: { |
| 132 | _nonce: tinyCompress.nonce, |
| 133 | action: 'tiny_dismiss_notice', |
| 134 | name: notice.data('name') || notice.attr('data-name') |
| 135 | }, |
| 136 | success: function(data) { |
| 137 | if (data) { |
| 138 | notice.remove(); |
| 139 | } |
| 140 | }, |
| 141 | error: function() { |
| 142 | element.removeAttr('disabled'); |
| 143 | } |
| 144 | }); |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | function updateResizeSettings() { |
| 149 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 150 | jQuery('.tiny-resize-available').show(); |
| 151 | jQuery('.tiny-resize-unavailable').hide(); |
| 152 | } else { |
| 153 | jQuery('.tiny-resize-available').hide(); |
| 154 | jQuery('.tiny-resize-unavailable').show(); |
| 155 | } |
| 156 | |
| 157 | var original_enabled = propOf('#tinypng_resize_original_enabled', 'checked'); |
| 158 | jQuery('#tinypng_resize_original_width, #tinypng_resize_original_height').each(function (i, el) { |
| 159 | el.disabled = !original_enabled; |
| 160 | }); |
| 161 | } |
| 162 | |
| 163 | function updatePreserveSettings() { |
| 164 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 165 | jQuery('.tiny-preserve').show(); |
| 166 | } else { |
| 167 | jQuery('.tiny-preserve').hide(); |
| 168 | jQuery('#tinypng_preserve_data_creation').attr('checked', false); |
| 169 | jQuery('#tinypng_preserve_data_copyright').attr('checked', false); |
| 170 | jQuery('#tinypng_preserve_data_location').attr('checked', false); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function updateSettings() { |
| 175 | updateResizeSettings(); |
| 176 | updatePreserveSettings(); |
| 177 | } |
| 178 | |
| 179 | var adminpage = ''; |
| 180 | if (typeof window.adminpage !== 'undefined') { |
| 181 | adminpage = window.adminpage; |
| 182 | } |
| 183 | |
| 184 | var statusCheckIntervalId; |
| 185 | |
| 186 | function eventOn(event, eventSelector, callback) { |
| 187 | if (typeof jQuery.fn.on === 'function') { |
| 188 | jQuery(document).on(event, eventSelector, callback); |
| 189 | } else { |
| 190 | jQuery(eventSelector).live(event, callback); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | function propOf(selector, property) { |
| 195 | if (typeof jQuery.fn.prop === 'function') { |
| 196 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 197 | property values into account. */ |
| 198 | return jQuery(selector).prop(property); |
| 199 | } else { |
| 200 | return jQuery(selector).attr(property); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | function setPropOf(selector, property, value) { |
| 205 | if (typeof jQuery.fn.prop === 'function') { |
| 206 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 207 | property values into account. */ |
| 208 | jQuery(selector).prop(property, value); |
| 209 | } else { |
| 210 | jQuery(selector).attr(property, value); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | function changeEnterKeyTarget(selector, button) { |
| 215 | eventOn('keyup keypress', selector, function(event) { |
| 216 | var code = event.keyCode || event.which; |
| 217 | if (code === 13) { |
| 218 | jQuery(button).click(); |
| 219 | return false; |
| 220 | } |
| 221 | }); |
| 222 | } |
| 223 | |
| 224 | switch (adminpage) { |
| 225 | case 'upload-php': |
| 226 | eventOn('click', 'button.tiny-compress', compressImage); |
| 227 | |
| 228 | setPropOf('button.tiny-compress', 'disabled', null); |
| 229 | |
| 230 | compressImageSelection(); |
| 231 | watchCompressingImages(); |
| 232 | |
| 233 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]'); |
| 234 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]'); |
| 235 | break; |
| 236 | case 'post-php': |
| 237 | eventOn('click', 'button.tiny-compress', compressImage); |
| 238 | break; |
| 239 | case 'options-media-php': |
| 240 | case 'settings_page_tinify': |
| 241 | case 'settings_page_media': // Enhanced Media Library plugin |
| 242 | changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]'); |
| 243 | changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]'); |
| 244 | |
| 245 | eventOn('click', '[data-tiny-action=create-key]', submitKey); |
| 246 | eventOn('click', '[data-tiny-action=update-key]', submitKey); |
| 247 | |
| 248 | var target = jQuery('#tiny-account-status[data-state=pending]'); |
| 249 | if (target.length) { |
| 250 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 251 | target.replaceWith(data); |
| 252 | }); |
| 253 | } |
| 254 | |
| 255 | eventOn('click', 'input[name*=tinypng_sizes], #tinypng_resize_original_enabled', function() { |
| 256 | /* Unfortunately, we need some additional information to display |
| 257 | the correct notice. */ |
| 258 | var totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length; |
| 259 | var compressWr2x = propOf('#tinypng_sizes_wr2x', 'checked'); |
| 260 | if (compressWr2x) { |
| 261 | totalSelectedSizes--; |
| 262 | } |
| 263 | |
| 264 | var image_count_url = ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes; |
| 265 | if (propOf('#tinypng_resize_original_enabled', 'checked') && propOf('#tinypng_sizes_0', 'checked')) { |
| 266 | image_count_url += '&resize_original=true'; |
| 267 | } |
| 268 | if (compressWr2x) { |
| 269 | image_count_url += '&compress_wr2x=true'; |
| 270 | } |
| 271 | jQuery('#tiny-image-sizes-notice').load(image_count_url); |
| 272 | }); |
| 273 | |
| 274 | eventOn('click', '#tinypng_auto_compress_enabled', function() { |
| 275 | updateSettings(); |
| 276 | }); |
| 277 | |
| 278 | jQuery('#tinypng_sizes_0, #tinypng_resize_original_enabled').click(updateSettings); |
| 279 | updateSettings(); |
| 280 | } |
| 281 | |
| 282 | jQuery('.tiny-notice a.tiny-dismiss').click(dismissNotice); |
| 283 | jQuery(function() { |
| 284 | jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice); |
| 285 | }); |
| 286 | }).call(); |
| 287 |