|
| 1 | +/* exported ampPostMetaBox */ |
| 2 | + |
| 3 | +/** |
| 4 | + * AMP Post Meta Box. |
| 5 | + * |
| 6 | + * @todo Rename this to be just the ampEditPostScreen? |
| 7 | + * |
| 8 | + * @since 0.6 |
| 9 | + */ |
| 10 | +var ampPostMetaBox = ( function( $ ) { |
| 11 | + 'use strict'; |
| 12 | + |
| 13 | + var component = { |
| 14 | + |
| 15 | + /** |
| 16 | + * Holds data. |
| 17 | + * |
| 18 | + * @since 0.6 |
| 19 | + */ |
| 20 | + data: { |
| 21 | + previewLink: '', |
| 22 | + disabled: false, |
| 23 | + statusInputName: '', |
| 24 | + l10n: { |
| 25 | + ampPreviewBtnLabel: '' |
| 26 | + } |
| 27 | + }, |
| 28 | + |
| 29 | + /** |
| 30 | + * Toggle animation speed. |
| 31 | + * |
| 32 | + * @since 0.6 |
| 33 | + */ |
| 34 | + toggleSpeed: 200, |
| 35 | + |
| 36 | + /** |
| 37 | + * Core preview button selector. |
| 38 | + * |
| 39 | + * @since 0.6 |
| 40 | + */ |
| 41 | + previewBtnSelector: '#post-preview', |
| 42 | + |
| 43 | + /** |
| 44 | + * AMP preview button selector. |
| 45 | + * |
| 46 | + * @since 0.6 |
| 47 | + */ |
| 48 | + ampPreviewBtnSelector: '#amp-post-preview' |
| 49 | + }; |
| 50 | + |
| 51 | + /** |
| 52 | + * Boot plugin. |
| 53 | + * |
| 54 | + * @since 0.6 |
| 55 | + * @param {Object} data Object data. |
| 56 | + * @return {void} |
| 57 | + */ |
| 58 | + component.boot = function boot( data ) { |
| 59 | + component.data = data; |
| 60 | + $( document ).ready( function() { |
| 61 | + if ( ! component.data.disabled ) { |
| 62 | + component.addPreviewButton(); |
| 63 | + } |
| 64 | + component.listen(); |
| 65 | + } ); |
| 66 | + }; |
| 67 | + |
| 68 | + /** |
| 69 | + * Events listener. |
| 70 | + * |
| 71 | + * @since 0.6 |
| 72 | + * @return {void} |
| 73 | + */ |
| 74 | + component.listen = function listen() { |
| 75 | + $( component.ampPreviewBtnSelector ).on( 'click.amp-post-preview', function( e ) { |
| 76 | + e.preventDefault(); |
| 77 | + component.onAmpPreviewButtonClick(); |
| 78 | + } ); |
| 79 | + |
| 80 | + $( '.edit-amp-status, [href="#amp_status"]' ).click( function( e ) { |
| 81 | + e.preventDefault(); |
| 82 | + component.toggleAmpStatus( $( e.target ) ); |
| 83 | + } ); |
| 84 | + |
| 85 | + $( '#submitpost input[type="submit"]' ).on( 'click', function() { |
| 86 | + $( component.ampPreviewBtnSelector ).addClass( 'disabled' ); |
| 87 | + } ); |
| 88 | + }; |
| 89 | + |
| 90 | + /** |
| 91 | + * Add AMP Preview button. |
| 92 | + * |
| 93 | + * @since 0.6 |
| 94 | + * @return {void} |
| 95 | + */ |
| 96 | + component.addPreviewButton = function addPreviewButton() { |
| 97 | + var previewBtn = $( component.previewBtnSelector ); |
| 98 | + previewBtn |
| 99 | + .clone() |
| 100 | + .insertAfter( previewBtn ) |
| 101 | + .prop( { |
| 102 | + 'href': component.data.previewLink, |
| 103 | + 'id': component.ampPreviewBtnSelector.replace( '#', '' ) |
| 104 | + } ) |
| 105 | + .text( component.data.l10n.ampPreviewBtnLabel ) |
| 106 | + .parent() |
| 107 | + .addClass( 'has-amp-preview' ); |
| 108 | + }; |
| 109 | + |
| 110 | + /** |
| 111 | + * AMP Preview button click handler. |
| 112 | + * |
| 113 | + * We trigger the Core preview link for events propagation purposes. |
| 114 | + * |
| 115 | + * @since 0.6 |
| 116 | + * @return {void} |
| 117 | + */ |
| 118 | + component.onAmpPreviewButtonClick = function onAmpPreviewButtonClick() { |
| 119 | + var $input; |
| 120 | + |
| 121 | + // Flag the AMP preview referer. |
| 122 | + $input = $( '<input>' ) |
| 123 | + .prop( { |
| 124 | + 'type': 'hidden', |
| 125 | + 'name': 'amp-preview', |
| 126 | + 'value': 'do-preview' |
| 127 | + } ) |
| 128 | + .insertAfter( component.ampPreviewBtnSelector ); |
| 129 | + |
| 130 | + // Trigger Core preview button and remove AMP flag. |
| 131 | + $( component.previewBtnSelector ).click(); |
| 132 | + $input.remove(); |
| 133 | + }; |
| 134 | + |
| 135 | + /** |
| 136 | + * Add AMP status toggle. |
| 137 | + * |
| 138 | + * @since 0.6 |
| 139 | + * @param {Object} $target Event target. |
| 140 | + * @return {void} |
| 141 | + */ |
| 142 | + component.toggleAmpStatus = function toggleAmpStatus( $target ) { |
| 143 | + var $container = $( '#amp-status-select' ), |
| 144 | + status = $container.data( 'amp-status' ), |
| 145 | + $checked, |
| 146 | + editAmpStatus = $( '.edit-amp-status' ); |
| 147 | + |
| 148 | + // Don't modify status on cancel button click. |
| 149 | + if ( ! $target.hasClass( 'button-cancel' ) ) { |
| 150 | + status = $( '[name="' + component.data.statusInputName + '"]:checked' ).val(); |
| 151 | + } |
| 152 | + |
| 153 | + $checked = $( '#amp-status-' + status ); |
| 154 | + |
| 155 | + // Toggle elements. |
| 156 | + editAmpStatus.fadeToggle( component.toggleSpeed, function() { |
| 157 | + if ( editAmpStatus.is( ':visible' ) ) { |
| 158 | + editAmpStatus.focus(); |
| 159 | + } |
| 160 | + } ); |
| 161 | + $container.slideToggle( component.toggleSpeed ); |
| 162 | + |
| 163 | + // Update status. |
| 164 | + $container.data( 'amp-status', status ); |
| 165 | + $checked.prop( 'checked', true ); |
| 166 | + $( '.amp-status-text' ).text( $checked.next().text() ); |
| 167 | + }; |
| 168 | + |
| 169 | + return component; |
| 170 | +})( window.jQuery ); |
0 commit comments