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