| @@ -1,267 +1,195 @@ | ||
| 1 | -jQuery( document ).ready( | |
| 2 | - function( $ ) { | |
| 3 | - var transitionTimeout; | |
| 1 | +jQuery( document ).ready(function( $ ) { | |
| 2 | + var transitionTimeout; | |
| 4 | 3 | |
| 5 | - // languages list table | |
| 6 | - // accessibility to row actions on focus | |
| 7 | - // mainly copy paste of WP code from common.js | |
| 8 | - $( 'table.languages' ).on( | |
| 9 | - { // restricted to languages list table | |
| 10 | - focusin: function() { | |
| 11 | - clearTimeout( transitionTimeout ); | |
| 12 | - focusedRowActions = $( this ).find( '.row-actions' ); | |
| 13 | - // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help. | |
| 14 | - $( '.row-actions' ).not( this ).removeClass( 'visible' ); | |
| 15 | - focusedRowActions.addClass( 'visible' ); | |
| 16 | - }, | |
| 17 | - focusout: function() { | |
| 18 | - // Tabbing between post title and .row-actions links needs a brief pause, otherwise | |
| 19 | - // the .row-actions div gets hidden in transit in some browsers ( ahem, Firefox ). | |
| 20 | - transitionTimeout = setTimeout( | |
| 21 | - function() { | |
| 22 | - focusedRowActions.removeClass( 'visible' ); | |
| 23 | - }, | |
| 24 | - 30 | |
| 25 | - ); | |
| 26 | - } | |
| 27 | - }, | |
| 28 | - 'tr' | |
| 29 | - ); // acts on the whole tr instead of single td as we have actions links in several columns | |
| 4 | + // languages list table | |
| 5 | + // accessibility to row actions on focus | |
| 6 | + // mainly copy paste of WP code from common.js | |
| 7 | + $( 'table.languages' ).on({ // restricted to languages list table | |
| 8 | + focusin: function() { | |
| 9 | + clearTimeout( transitionTimeout ); | |
| 10 | + focusedRowActions = $( this ).find( '.row-actions' ); | |
| 11 | + // transitionTimeout is necessary for Firefox, but Chrome won't remove the CSS class without a little help. | |
| 12 | + $( '.row-actions' ).not( this ).removeClass( 'visible' ); | |
| 13 | + focusedRowActions.addClass( 'visible' ); | |
| 14 | + }, | |
| 15 | + focusout: function() { | |
| 16 | + // Tabbing between post title and .row-actions links needs a brief pause, otherwise | |
| 17 | + // the .row-actions div gets hidden in transit in some browsers ( ahem, Firefox ). | |
| 18 | + transitionTimeout = setTimeout(function() { | |
| 19 | + focusedRowActions.removeClass( 'visible' ); | |
| 20 | + }, 30 ); | |
| 21 | + } | |
| 22 | + }, 'tr' ); // acts on the whole tr instead of single td as we have actions links in several columns | |
| 30 | 23 | |
| 31 | - // Common functions for overriding language and flag dropdown list. | |
| 32 | - var selectmenuRenderItem = function( wrapper, item ) { | |
| 33 | - var li = $( '<li>' ).text( item.label ).prepend( $( item.element ).data( 'flag-html' ) ); | |
| 34 | - li.children( 'img' ).addClass( 'ui-icon' ); | |
| 35 | - return li.appendTo( wrapper ); | |
| 36 | - }; | |
| 37 | - var selectmenuRefreshButtonText = function( selectElement ) { | |
| 38 | - var buttonText = $( selectElement ).selectmenu( 'instance' ).buttonText; | |
| 39 | - buttonText.prepend( $( selectElement ).children( ':selected' ).data( 'flag-html' ) ); | |
| 40 | - buttonText.children( 'img' ).addClass( 'ui-icon' ); | |
| 41 | - }; | |
| 42 | - // Overrides the flag dropdown list with our customized jquery ui selectmenu. | |
| 24 | + // extends selectmenu to add flags in menu items | |
| 25 | + $.widget( "custom.iconselectmenu", $.ui.selectmenu, { | |
| 26 | + _renderItem: function( ul, item ) { | |
| 27 | + var li = $( "<li>", { text: item.label } ); | |
| 28 | + var el = $( item.element ); | |
| 29 | + var url = el.data( "url" ); | |
| 43 | 30 | |
| 44 | - // Inject flag image when jQuery UI selectmenu is created or an item is selected. | |
| 45 | - $( '#flag_list' ).on( | |
| 46 | - 'selectmenucreate selectmenuselect', | |
| 47 | - function(){ | |
| 48 | - selectmenuRefreshButtonText( this ); | |
| 49 | - } | |
| 50 | - ); | |
| 51 | - // Refresh jQuery UI selectmenu when the value is changed programmatically: select another language or edit an existing language. | |
| 52 | - // For putting the focus in the list on the selected item and injecting the right flag on the selected item. | |
| 53 | - $( '#flag_list' ).on( | |
| 54 | - 'selectmenuopen', | |
| 55 | - function(){ | |
| 56 | - $( this ).selectmenu( 'refresh' ).trigger( 'selectmenuselect' ); | |
| 57 | - } | |
| 58 | - ); | |
| 59 | - // Create the jQuery UI selectmenu widget | |
| 60 | - $( '#flag_list' ).selectmenu( { width: '100%' } ); | |
| 61 | - // Overrides each item in the jQuery UI selectmenu list by injecting flag image. | |
| 62 | - $( '#flag_list' ).selectmenu( 'instance' )._renderItem = selectmenuRenderItem; | |
| 31 | + if ( url ) { | |
| 32 | + var w = el.data( "width" ); | |
| 33 | + var h = el.data( "height" ); | |
| 34 | + $( "<img class='ui-icon' />" ).prop( "src", url ).prop( "width", w ).prop( "height", h ).appendTo( li ); | |
| 35 | + } | |
| 63 | 36 | |
| 64 | - // Language choice in predefined languages in Polylang Languages settings page and wizard. | |
| 65 | - // Overrides the predefined language dropdown list with our customized jquery ui selectmenu. | |
| 37 | + return li.appendTo( ul ); | |
| 38 | + } | |
| 39 | + }); | |
| 66 | 40 | |
| 67 | - // Inject flag image when jQuery UI selectmenu is created or an item is selected. | |
| 68 | - $( '#lang_list' ).on( | |
| 69 | - 'selectmenucreate selectmenuselect', | |
| 70 | - function() { | |
| 71 | - selectmenuRefreshButtonText( this ); | |
| 72 | - } | |
| 73 | - ); | |
| 74 | - // Create the jQuery UI selectmenu widget | |
| 75 | - $( '#lang_list' ).selectmenu( { width: '100%' } ); | |
| 76 | - // Overrides each element in the jQuery UI selectmenu list by injecting flag image. | |
| 77 | - $( '#lang_list' ).selectmenu( 'instance' )._renderItem = selectmenuRenderItem; | |
| 41 | + // allows to display the flag for the selected menu item | |
| 42 | + function add_icon( event, ui ) { | |
| 43 | + var sel = $( this ).find( ":selected" ); | |
| 44 | + var url = sel.data( "url" ); | |
| 78 | 45 | |
| 79 | - // Languages form | |
| 80 | - // Fills the fields based on the language dropdown list choice | |
| 81 | - $( '#add-lang #lang_list' ).on( | |
| 82 | - 'selectmenuchange', | |
| 83 | - function() { | |
| 84 | - var value = $( this ).val().split( ':' ); | |
| 85 | - var selected = $( "option:selected", this ).text().split( ' - ' ); | |
| 86 | - $( '#lang_slug' ).val( value[0] ); | |
| 87 | - $( '#lang_locale' ).val( value[1] ); | |
| 88 | - $( 'input[name="rtl"]' ).val( [value[2]] ); | |
| 89 | - $( '#lang_name' ).val( selected[0] ); | |
| 90 | - $( '#flag_list').val( value[3] ); | |
| 46 | + if ( url ) { | |
| 47 | + var txt = $( this ).iconselectmenu( "widget" ).children( ":last" ); | |
| 48 | + var w = sel.data( "width" ); | |
| 49 | + var h = sel.data( "height" ); | |
| 50 | + $( "<img class='ui-icon' />" ).prop( "src", url ).prop( "width", w ).prop( "height", h ).appendTo( txt ); | |
| 51 | + } | |
| 52 | + } | |
| 91 | 53 | |
| 92 | - // Refresh the jQuery UI selectmenu flags list. | |
| 93 | - $( '#flag_list' ).selectmenu( 'refresh' ).trigger( 'selectmenuselect' ); | |
| 94 | - } | |
| 95 | - ); | |
| 54 | + // overrides the flag dropdown list with our customized jquery ui selectmenu | |
| 55 | + $( "#flag_list" ).iconselectmenu({ | |
| 56 | + create: add_icon, | |
| 57 | + select: add_icon, | |
| 58 | + }); | |
| 96 | 59 | |
| 97 | - // strings translations | |
| 98 | - // save translations when pressing enter | |
| 99 | - $( '.translation input' ).keypress( | |
| 100 | - function( event ){ | |
| 101 | - if ( 13 === event.keyCode ) { | |
| 102 | - event.preventDefault(); | |
| 103 | - $( '#submit' ).click(); | |
| 104 | - } | |
| 105 | - } | |
| 106 | - ); | |
| 60 | + // languages form | |
| 61 | + // fills the fields based on the language dropdown list choice | |
| 62 | + $( '#lang_list' ).change(function() { | |
| 63 | + var value = $( this ).val().split( ':' ); | |
| 64 | + var selected = $( "option:selected", this ).text().split( ' - ' ); | |
| 65 | + $( '#lang_slug' ).val( value[0] ); | |
| 66 | + $( '#lang_locale' ).val( value[1] ); | |
| 67 | + $( 'input[name="rtl"]' ).val( [value[2]] ); | |
| 68 | + $( '#lang_name' ).val( selected[0] ); | |
| 69 | + $( '#flag_list option[value="' + value[3] + '"]' ).attr( 'selected', 'selected' ); | |
| 107 | 70 | |
| 108 | - // settings page | |
| 109 | - // click on configure link | |
| 110 | - $( '#the-list' ).on( | |
| 111 | - 'click', | |
| 112 | - '.configure>a', | |
| 113 | - function(){ | |
| 114 | - $( '.pll-configure' ).hide().prev().show(); | |
| 115 | - $( this ).closest( 'tr' ).hide().next().show(); | |
| 116 | - return false; | |
| 117 | - } | |
| 118 | - ); | |
| 71 | + // recreate the jquery ui selectmenu | |
| 72 | + $( "#flag_list" ).iconselectmenu( 'destroy' ).iconselectmenu({ | |
| 73 | + create: add_icon, | |
| 74 | + select: add_icon, | |
| 75 | + }) | |
| 76 | + }); | |
| 119 | 77 | |
| 120 | - // cancel | |
| 121 | - $( '#the-list' ).on( | |
| 122 | - 'click', | |
| 123 | - '.cancel', | |
| 124 | - function(){ | |
| 125 | - $( this ).closest( 'tr' ).hide().prev().show(); | |
| 126 | - } | |
| 127 | - ); | |
| 78 | + // strings translations | |
| 79 | + // save translations when pressing enter | |
| 80 | + $( '.translation input' ).keypress(function( event ){ | |
| 81 | + if ( 13 === event.keyCode ) { | |
| 82 | + event.preventDefault(); | |
| 83 | + $( '#submit' ).click(); | |
| 84 | + } | |
| 85 | + }); | |
| 128 | 86 | |
| 129 | - // save settings | |
| 130 | - $( '#the-list' ).on( | |
| 131 | - 'click', | |
| 132 | - '.save', | |
| 133 | - function(){ | |
| 134 | - var tr = $( this ).closest( 'tr' ); | |
| 135 | - var parts = tr.attr( 'id' ).split( '-' ); | |
| 87 | + // settings page | |
| 88 | + // click on configure link | |
| 89 | + $( '#the-list' ).on( 'click', '.configure>a', function(){ | |
| 90 | + $( '.pll-configure' ).hide().prev().show(); | |
| 91 | + $( this ).closest( 'tr' ).hide().next().show(); | |
| 92 | + return false; | |
| 93 | + }); | |
| 136 | 94 | |
| 137 | - var data = { | |
| 138 | - action: 'pll_save_options', | |
| 139 | - pll_ajax_settings: true, | |
| 140 | - module: parts[parts.length - 1], | |
| 141 | - _pll_nonce: $( '#_pll_nonce' ).val() | |
| 142 | - }; | |
| 95 | + // cancel | |
| 96 | + $( '#the-list' ).on( 'click', '.cancel', function(){ | |
| 97 | + $( this ).closest( 'tr' ).hide().prev().show(); | |
| 98 | + }); | |
| 143 | 99 | |
| 144 | - data = tr.find( ':input' ).serialize() + '&' + $.param( data ); | |
| 100 | + // save settings | |
| 101 | + $( '#the-list' ).on( 'click', '.save', function(){ | |
| 102 | + var tr = $( this ).closest( 'tr' ); | |
| 103 | + var parts = tr.attr( 'id' ).split( '-' ); | |
| 145 | 104 | |
| 146 | - $.post( | |
| 147 | - ajaxurl, | |
| 148 | - data, | |
| 149 | - function( response ) { | |
| 150 | - var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 151 | - $.each( | |
| 152 | - res.responses, | |
| 153 | - function() { | |
| 154 | - switch ( this.what ) { | |
| 155 | - case 'license-update': | |
| 156 | - $( '#pll-license-' + this.data ).replaceWith( this.supplemental.html ); | |
| 157 | - break; | |
| 158 | - case 'success': | |
| 159 | - tr.hide().prev().show(); // close only if there is no error | |
| 160 | - case 'error': | |
| 161 | - $( '.settings-error' ).remove(); // remove previous messages if any | |
| 162 | - $( 'h1' ).after( this.data ); | |
| 105 | + var data = { | |
| 106 | + action: 'pll_save_options', | |
| 107 | + pll_ajax_settings: true, | |
| 108 | + module: parts[parts.length - 1], | |
| 109 | + _pll_nonce: $( '#_pll_nonce' ).val() | |
| 110 | + } | |
| 163 | 111 | |
| 164 | - // Make notices dismissible | |
| 165 | - // copy paste of common.js from WP 4.2.2 | |
| 166 | - $( '.notice.is-dismissible' ).each( | |
| 167 | - function() { | |
| 168 | - var $this = $( this ), | |
| 169 | - $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ), | |
| 170 | - btnText = commonL10n.dismiss || ''; | |
| 112 | + data = tr.find( ':input' ).serialize() + '&' + $.param( data ); | |
| 171 | 113 | |
| 172 | - // Ensure plain text | |
| 173 | - $button.find( '.screen-reader-text' ).text( btnText ); | |
| 114 | + $.post( ajaxurl, data, function( response ) { | |
| 115 | + var res = wpAjax.parseAjaxResponse( response, 'ajax-response' ); | |
| 116 | + $.each( res.responses, function() { | |
| 117 | + switch ( this.what ) { | |
| 118 | + case 'license-update': | |
| 119 | + $( '#pll-license-' + this.data ).replaceWith( this.supplemental.html ); | |
| 120 | + break; | |
| 121 | + case 'success': | |
| 122 | + tr.hide().prev().show(); // close only if there is no error | |
| 123 | + case 'error': | |
| 124 | + $( '.settings-error' ).remove(); // remove previous messages if any | |
| 125 | + $( 'h1' ).after( this.data ); | |
| 174 | 126 | |
| 175 | - // Whitelist because of how the button is built. See above | |
| 176 | - $this.append( $button ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.append | |
| 127 | + // Make notices dismissible | |
| 128 | + // copy paste of common.js from WP 4.2.2 | |
| 129 | + $( '.notice.is-dismissible' ).each(function() { | |
| 130 | + var $this = $( this ), | |
| 131 | + $button = $( '<button type="button" class="notice-dismiss"><span class="screen-reader-text"></span></button>' ), | |
| 132 | + btnText = commonL10n.dismiss || ''; | |
| 177 | 133 | |
| 178 | - $button.on( | |
| 179 | - 'click.wp-dismiss-notice', | |
| 180 | - function( event ) { | |
| 181 | - event.preventDefault(); | |
| 182 | - $this.fadeTo( | |
| 183 | - 100, | |
| 184 | - 0, | |
| 185 | - function() { | |
| 186 | - $( this ).slideUp( | |
| 187 | - 100, | |
| 188 | - function() { | |
| 189 | - $( this ).remove(); | |
| 190 | - } | |
| 191 | - ); | |
| 192 | - } | |
| 193 | - ); | |
| 194 | - } | |
| 195 | - ); | |
| 196 | - } | |
| 197 | - ); | |
| 198 | - break; | |
| 199 | - } | |
| 200 | - } | |
| 201 | - ); | |
| 202 | - } | |
| 203 | - ); | |
| 204 | - } | |
| 205 | - ); | |
| 134 | + // Ensure plain text | |
| 135 | + $button.find( '.screen-reader-text' ).text( btnText ); | |
| 206 | 136 | |
| 207 | - // act when pressing enter or esc in configurations | |
| 208 | - $( '.pll-configure' ).keypress( | |
| 209 | - function( event ){ | |
| 210 | - if ( 13 === event.keyCode ) { | |
| 211 | - event.preventDefault(); | |
| 212 | - $( this ).find( '.save' ).click(); | |
| 137 | + $this.append( $button ); | |
| 138 | + | |
| 139 | + $button.on( 'click.wp-dismiss-notice', function( event ) { | |
| 140 | + event.preventDefault(); | |
| 141 | + $this.fadeTo( 100 , 0, function() { | |
| 142 | + $( this ).slideUp( 100, function() { | |
| 143 | + $( this ).remove(); | |
| 144 | + }); | |
| 145 | + }); | |
| 146 | + }); | |
| 147 | + }); | |
| 148 | + break; | |
| 213 | 149 | } |
| 150 | + }); | |
| 151 | + }); | |
| 152 | + }); | |
| 214 | 153 | |
| 215 | - if ( 27 === event.keyCode ) { | |
| 216 | - event.preventDefault(); | |
| 217 | - $( this ).find( '.cancel' ).click(); | |
| 218 | - } | |
| 219 | - } | |
| 220 | - ); | |
| 154 | + // act when pressing enter or esc in configurations | |
| 155 | + $( '.pll-configure' ).keypress(function( event ){ | |
| 156 | + if ( 13 === event.keyCode ) { | |
| 157 | + event.preventDefault(); | |
| 158 | + $( this ).find( '.save' ).click(); | |
| 159 | + } | |
| 221 | 160 | |
| 222 | - // settings URL modifications | |
| 223 | - // manages visibility of fields | |
| 224 | - $( "input[name='force_lang']" ).change( | |
| 225 | - function() { | |
| 226 | - function pll_toggle( a, test ) { | |
| 227 | - test ? a.show() : a.hide(); | |
| 228 | - } | |
| 161 | + if ( 27 === event.keyCode ) { | |
| 162 | + event.preventDefault(); | |
| 163 | + $( this ).find( '.cancel' ).click(); | |
| 164 | + } | |
| 165 | + }); | |
| 229 | 166 | |
| 230 | - var value = $( this ).val(); | |
| 231 | - pll_toggle( $( '#pll-domains-table' ), 3 == value ); | |
| 232 | - pll_toggle( $( "#pll-hide-default" ), 3 > value ); | |
| 233 | - pll_toggle( $( "#pll-rewrite" ), 2 > value ); | |
| 234 | - pll_toggle( $( "#pll-redirect-lang" ), 2 > value ); | |
| 235 | - } | |
| 236 | - ); | |
| 167 | + // settings URL modifications | |
| 168 | + // manages visibility of fields | |
| 169 | + $( "input[name='force_lang']" ).change(function() { | |
| 170 | + function pll_toggle( a, test ) { | |
| 171 | + test ? a.show() : a.hide(); | |
| 172 | + } | |
| 237 | 173 | |
| 238 | - // settings license | |
| 239 | - // deactivate button | |
| 240 | - $( '.pll-deactivate-license' ).on( | |
| 241 | - 'click', | |
| 242 | - function() { | |
| 243 | - var data = { | |
| 244 | - action: 'pll_deactivate_license', | |
| 245 | - pll_ajax_settings: true, | |
| 246 | - id: $( this ).attr( 'id' ), | |
| 247 | - _pll_nonce: $( '#_pll_nonce' ).val() | |
| 248 | - }; | |
| 249 | - $.post( | |
| 250 | - ajaxurl, | |
| 251 | - data, | |
| 252 | - function( response ){ | |
| 253 | - $( '#pll-license-' + response.id ).replaceWith( response.html ); | |
| 254 | - } | |
| 255 | - ); | |
| 256 | - } | |
| 257 | - ); | |
| 174 | + var value = $( this ).val(); | |
| 175 | + pll_toggle( $( '#pll-domains-table' ), 3 == value ); | |
| 176 | + pll_toggle( $( "#pll-hide-default" ), 3 > value ); | |
| 177 | + pll_toggle( $( "#pll-rewrite" ), 2 > value ); | |
| 178 | + pll_toggle( $( "#pll-redirect-lang" ), 2 > value ); | |
| 179 | + }); | |
| 258 | 180 | |
| 259 | - // Manage closing the metabox. | |
| 260 | - // close postboxes that should be closed | |
| 261 | - $( '.if-js-closed' ).removeClass( 'if-js-closed' ).addClass( 'closed' ); | |
| 262 | - // postboxes setup | |
| 263 | - if ( 'undefined' !== typeof postboxes ) { | |
| 264 | - postboxes.add_postbox_toggles( pagenow ); | |
| 181 | + // settings license | |
| 182 | + // deactivate button | |
| 183 | + $( '.pll-deactivate-license' ).on( 'click', function() { | |
| 184 | + var data = { | |
| 185 | + action: 'pll_deactivate_license', | |
| 186 | + pll_ajax_settings: true, | |
| 187 | + id: $( this ).attr( 'id' ), | |
| 188 | + _pll_nonce: $( '#_pll_nonce' ).val() | |
| 265 | 189 | } |
| 266 | - } | |
| 267 | -); | |
| 190 | + $.post( ajaxurl, data , function( response ){ | |
| 191 | + $( '#pll-license-' + response.id ).replaceWith( response.html ); | |
| 192 | + }); | |
| 193 | + }); | |
| 194 | + | |
| 195 | +}); | |