dist
4 months ago
src
4 months ago
public-post-preview.js
3 years ago
public-post-preview.min.js
5 years ago
public-post-preview.js
113 lines
| 1 | /* eslint-disable no-var, object-shorthand */ |
| 2 | ( function ( $, ajaxurl, l10n ) { |
| 3 | var DSPublicPostPreview = { |
| 4 | /** |
| 5 | * Initializes the plugin. |
| 6 | * |
| 7 | * @since 2.0.0 |
| 8 | */ |
| 9 | initialize: function () { |
| 10 | var t = this; |
| 11 | |
| 12 | t.checkbox = $( '#public-post-preview' ); |
| 13 | t.link = $( '#public-post-preview-link' ); |
| 14 | t.linkInput = t.link.find( 'input' ); |
| 15 | t.nonce = $( '#public_post_preview_wpnonce' ); |
| 16 | t.status = $( '#public-post-preview-ajax' ); |
| 17 | |
| 18 | t.status.css( 'opacity', 0 ); |
| 19 | |
| 20 | t.checkbox.bind( 'change', function () { |
| 21 | t.change(); |
| 22 | } ); |
| 23 | |
| 24 | t.linkInput.on( 'focus', function () { |
| 25 | $( this ).select(); |
| 26 | } ); |
| 27 | }, |
| 28 | |
| 29 | /** |
| 30 | * Handles a checkbox change. |
| 31 | * |
| 32 | * @since 2.0.0 |
| 33 | */ |
| 34 | change: function () { |
| 35 | var t = this, |
| 36 | checked = t.checkbox.prop( 'checked' ) ? 'true' : 'false'; |
| 37 | |
| 38 | // Disable the checkbox, to prevent double AJAX requests |
| 39 | t.checkbox.prop( 'disabled', 'disabled' ); |
| 40 | |
| 41 | t.request( |
| 42 | { |
| 43 | _ajax_nonce: t.nonce.val(), |
| 44 | checked: checked, |
| 45 | post_ID: $( '#post_ID' ).val(), |
| 46 | }, |
| 47 | function ( response ) { |
| 48 | if ( response.success ) { |
| 49 | if ( 'true' === checked ) { |
| 50 | t.status.text( l10n.enabled ); |
| 51 | t._pulsate( t.status, 'green' ); |
| 52 | } else { |
| 53 | t.status.text( l10n.disabled ); |
| 54 | t._pulsate( t.status, 'red' ); |
| 55 | } |
| 56 | |
| 57 | // Add preview link |
| 58 | if ( response.data && response.data.preview_url ) { |
| 59 | t.linkInput.val( response.data.preview_url ); |
| 60 | } else { |
| 61 | t.linkInput.val( '' ); |
| 62 | } |
| 63 | |
| 64 | // Toggle visibility of the link |
| 65 | t.link.toggle(); |
| 66 | } |
| 67 | |
| 68 | // Enable the checkbox again |
| 69 | t.checkbox.prop( 'disabled', '' ); |
| 70 | } |
| 71 | ); |
| 72 | }, |
| 73 | |
| 74 | /** |
| 75 | * Does the AJAX request. |
| 76 | * |
| 77 | * @since 2.0.0 |
| 78 | * |
| 79 | * @param {Object} data The data to send. |
| 80 | * @param {Object} callback Callback function for a successful request. |
| 81 | */ |
| 82 | request: function ( data, callback ) { |
| 83 | $.ajax( { |
| 84 | type: 'POST', |
| 85 | url: ajaxurl, |
| 86 | data: $.extend( data, { |
| 87 | action: 'public-post-preview', |
| 88 | } ), |
| 89 | success: callback, |
| 90 | } ); |
| 91 | }, |
| 92 | |
| 93 | /** |
| 94 | * Helper for a pulse effect. |
| 95 | * |
| 96 | * @since 2.0.0 |
| 97 | * |
| 98 | * @param {Object} e The element. |
| 99 | * @param {string} color The text color of the element. |
| 100 | */ |
| 101 | _pulsate: function ( e, color ) { |
| 102 | e.css( 'color', color ) |
| 103 | .animate( { opacity: 1 }, 600, 'linear' ) |
| 104 | .animate( { opacity: 0 }, 600, 'linear', function () { |
| 105 | e.empty(); |
| 106 | } ); |
| 107 | }, |
| 108 | }; |
| 109 | |
| 110 | // Document is ready. |
| 111 | $( DSPublicPostPreview.initialize() ); |
| 112 | } )( window.jQuery, window.ajaxurl, window.DSPublicPostPreviewL10n ); |
| 113 |