admin.js
236 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 submitKey(event) { |
| 27 | event.preventDefault() |
| 28 | jQuery(event.target).attr({disabled: true}).addClass('loading') |
| 29 | |
| 30 | var action |
| 31 | var parent = jQuery(event.target).closest('div') |
| 32 | |
| 33 | if (jQuery(event.target).data('tiny-action') == 'update-key') { |
| 34 | action = 'update' |
| 35 | var key = parent.find('#tinypng_api_key').val() |
| 36 | } else if (jQuery(event.target).data('tiny-action') == 'create-key') { |
| 37 | action = 'create' |
| 38 | var name = parent.find('#tinypng_api_key_name').val() |
| 39 | var email = parent.find('#tinypng_api_key_email').val() |
| 40 | } else { |
| 41 | return false |
| 42 | } |
| 43 | |
| 44 | jQuery.ajax({ |
| 45 | url: ajaxurl, |
| 46 | type: 'POST', |
| 47 | data: { |
| 48 | _nonce: tinyCompress.nonce, |
| 49 | action: 'tiny_settings_' + action + '_api_key', |
| 50 | key: key, |
| 51 | name: name, |
| 52 | email: email, |
| 53 | }, |
| 54 | |
| 55 | success: function(json) { |
| 56 | var status = JSON.parse(json) |
| 57 | if (status.ok) { |
| 58 | var target = jQuery('#tiny-account-status') |
| 59 | if (target.length) { |
| 60 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 61 | jQuery(event.target).attr({disabled: false}).removeClass('loading') |
| 62 | target.replaceWith(data) |
| 63 | }) |
| 64 | } |
| 65 | } else { |
| 66 | jQuery(event.target).attr({disabled: false}).removeClass('loading') |
| 67 | parent.addClass('failure') |
| 68 | parent.find('p.message').text(status.message).show() |
| 69 | } |
| 70 | }, |
| 71 | |
| 72 | error: function() { |
| 73 | jQuery(event.target).attr({disabled: false}).removeClass('loading') |
| 74 | parent.addClass('failure') |
| 75 | parent.find('p.message').text('Something went wrong, try again soon').show() |
| 76 | } |
| 77 | }) |
| 78 | |
| 79 | return false |
| 80 | } |
| 81 | |
| 82 | function dismissNotice(event) { |
| 83 | var element = jQuery(event.target) |
| 84 | var notice = element.closest('.tiny-notice') |
| 85 | element.attr('disabled', 'disabled') |
| 86 | jQuery.ajax({ |
| 87 | url: ajaxurl, |
| 88 | type: 'POST', |
| 89 | dataType: 'json', |
| 90 | data: { |
| 91 | _nonce: tinyCompress.nonce, |
| 92 | action: 'tiny_dismiss_notice', |
| 93 | name: notice.data('name') || notice.attr('data-name') |
| 94 | }, |
| 95 | success: function(data) { |
| 96 | if (data) { |
| 97 | notice.remove() |
| 98 | } |
| 99 | }, |
| 100 | error: function() { |
| 101 | element.removeAttr('disabled') |
| 102 | } |
| 103 | }) |
| 104 | return false |
| 105 | } |
| 106 | |
| 107 | function updateResizeSettings() { |
| 108 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 109 | jQuery('.tiny-resize-available').show() |
| 110 | jQuery('.tiny-resize-unavailable').hide() |
| 111 | } else { |
| 112 | jQuery('.tiny-resize-available').hide() |
| 113 | jQuery('.tiny-resize-unavailable').show() |
| 114 | } |
| 115 | |
| 116 | var original_enabled = propOf('#tinypng_resize_original_enabled', 'checked') |
| 117 | jQuery('#tinypng_resize_original_width, #tinypng_resize_original_height').each(function (i, el) { |
| 118 | el.disabled = !original_enabled |
| 119 | }) |
| 120 | } |
| 121 | |
| 122 | function updatePreserveSettings() { |
| 123 | if (propOf('#tinypng_sizes_0', 'checked')) { |
| 124 | jQuery('.tiny-preserve').show() |
| 125 | } else { |
| 126 | jQuery('.tiny-preserve').hide() |
| 127 | jQuery('#tinypng_preserve_data_creation').attr('checked', false) |
| 128 | jQuery('#tinypng_preserve_data_copyright').attr('checked', false) |
| 129 | jQuery('#tinypng_preserve_data_location').attr('checked', false) |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | function updateSettings() { |
| 134 | updateResizeSettings() |
| 135 | updatePreserveSettings() |
| 136 | } |
| 137 | |
| 138 | var adminpage = '' |
| 139 | if (typeof window.adminpage !== 'undefined') { |
| 140 | adminpage = window.adminpage |
| 141 | } |
| 142 | |
| 143 | function eventOn(event, eventSelector, callback) { |
| 144 | if (typeof jQuery.fn.on === 'function') { |
| 145 | jQuery(document).on(event, eventSelector, callback) |
| 146 | } else { |
| 147 | jQuery(eventSelector).live(event, callback) |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | function propOf(selector, property) { |
| 152 | if (typeof jQuery.fn.prop === 'function') { |
| 153 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 154 | property values into account. */ |
| 155 | return jQuery(selector).prop(property) |
| 156 | } else { |
| 157 | return jQuery(selector).attr(property) |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | function setPropOf(selector, property, value) { |
| 162 | if (typeof jQuery.fn.prop === 'function') { |
| 163 | /* Added in 1.6. Before jQuery 1.6, the .attr() method sometimes took |
| 164 | property values into account. */ |
| 165 | jQuery(selector).prop(property, value) |
| 166 | } else { |
| 167 | jQuery(selector).attr(property, value) |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | function changeEnterKeyTarget(selector, button) { |
| 172 | eventOn('keyup keypress', selector, function(event) { |
| 173 | var code = event.keyCode || event.which |
| 174 | if (code == 13) { |
| 175 | jQuery(button).click() |
| 176 | return false |
| 177 | } |
| 178 | }) |
| 179 | } |
| 180 | |
| 181 | switch (adminpage) { |
| 182 | case 'upload-php': |
| 183 | eventOn('click', 'button.tiny-compress', compressImage) |
| 184 | |
| 185 | setPropOf('button.tiny-compress', 'disabled', null) |
| 186 | |
| 187 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action]') |
| 188 | jQuery('<option>').val('tiny_bulk_action').text(tinyCompress.L10nBulkAction).appendTo('select[name=action2]') |
| 189 | break |
| 190 | case 'post-php': |
| 191 | eventOn('click', 'button.tiny-compress', compressImage) |
| 192 | break |
| 193 | case 'options-media-php': |
| 194 | case 'settings_page_media': // Enhanced Media Library plugin |
| 195 | changeEnterKeyTarget('div.tiny-account-status create', '[data-tiny-action=create-key]') |
| 196 | changeEnterKeyTarget('div.tiny-account-status update', '[data-tiny-action=update-key]') |
| 197 | |
| 198 | eventOn('click', '[data-tiny-action=create-key]', submitKey) |
| 199 | eventOn('click', '[data-tiny-action=update-key]', submitKey) |
| 200 | |
| 201 | var target = jQuery('#tiny-account-status[data-state=pending]') |
| 202 | if (target.length) { |
| 203 | jQuery.get(ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_account_status', function(data) { |
| 204 | target.replaceWith(data) |
| 205 | }) |
| 206 | } |
| 207 | |
| 208 | eventOn('click', 'input[name*=tinypng_sizes], #tinypng_resize_original_enabled', function() { |
| 209 | /* Unfortunately, we need some additional information to display |
| 210 | the correct notice. */ |
| 211 | totalSelectedSizes = jQuery('input[name*=tinypng_sizes]:checked').length |
| 212 | compressWr2x = propOf('#tinypng_sizes_wr2x', 'checked') |
| 213 | if (compressWr2x) { |
| 214 | totalSelectedSizes--; |
| 215 | } |
| 216 | |
| 217 | var image_count_url = ajaxurl + (ajaxurl.indexOf( '?' ) > 0 ? '&' : '?') + 'action=tiny_image_sizes_notice&image_sizes_selected=' + totalSelectedSizes |
| 218 | if (propOf('#tinypng_resize_original_enabled', 'checked') && propOf('#tinypng_sizes_0', 'checked')) { |
| 219 | image_count_url += '&resize_original=true' |
| 220 | } |
| 221 | if (compressWr2x) { |
| 222 | image_count_url += '&compress_wr2x=true' |
| 223 | } |
| 224 | jQuery('#tiny-image-sizes-notice').load(image_count_url) |
| 225 | }) |
| 226 | |
| 227 | jQuery('#tinypng_sizes_0, #tinypng_resize_original_enabled').click(updateSettings) |
| 228 | updateSettings() |
| 229 | |
| 230 | } |
| 231 | |
| 232 | jQuery('.tiny-notice a.tiny-dismiss').click(dismissNotice) |
| 233 | jQuery(function() { |
| 234 | jQuery('.tiny-notice.is-dismissible button').unbind('click').click(dismissNotice) |
| 235 | }) |
| 236 | }).call() |