| 1 |
jQuery( document ).ready(function( $ ) { |
| 2 |
var transitionTimeout; |
| 3 |
|
| 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 |
| 23 |
|
| 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" ); |
| 30 |
|
| 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 |
} |
| 36 |
|
| 37 |
return li.appendTo( ul ); |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 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" ); |
| 45 |
|
| 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 |
} |
| 53 |
|
| 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 |
}); |
| 59 |
|
| 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' ); |
| 70 |
|
| 71 |
// recreate the jquery ui selectmenu |
| 72 |
$( "#flag_list" ).iconselectmenu( 'destroy' ).iconselectmenu({ |
| 73 |
create: add_icon, |
| 74 |
select: add_icon, |
| 75 |
}) |
| 76 |
}); |
| 77 |
|
| 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 |
}); |
| 86 |
|
| 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 |
}); |
| 94 |
|
| 95 |
// cancel |
| 96 |
$( '#the-list' ).on( 'click', '.cancel', function(){ |
| 97 |
$( this ).closest( 'tr' ).hide().prev().show(); |
| 98 |
}); |
| 99 |
|
| 100 |
// save settings |
| 101 |
$( '#the-list' ).on( 'click', '.save', function(){ |
| 102 |
var tr = $( this ).closest( 'tr' ); |
| 103 |
var parts = tr.attr( 'id' ).split( '-' ); |
| 104 |
|
| 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 |
} |
| 111 |
|
| 112 |
data = tr.find( ':input' ).serialize() + '&' + $.param( data ); |
| 113 |
|
| 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 ); |
| 126 |
|
| 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 || ''; |
| 133 |
|
| 134 |
// Ensure plain text |
| 135 |
$button.find( '.screen-reader-text' ).text( btnText ); |
| 136 |
|
| 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; |
| 149 |
} |
| 150 |
}); |
| 151 |
}); |
| 152 |
}); |
| 153 |
|
| 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 |
} |
| 160 |
|
| 161 |
if ( 27 === event.keyCode ) { |
| 162 |
event.preventDefault(); |
| 163 |
$( this ).find( '.cancel' ).click(); |
| 164 |
} |
| 165 |
}); |
| 166 |
|
| 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 |
} |
| 173 |
|
| 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 |
}); |
| 180 |
|
| 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() |
| 189 |
} |
| 190 |
$.post( ajaxurl, data , function( response ){ |
| 191 |
$( '#pll-license-' + response.id ).replaceWith( response.html ); |
| 192 |
}); |
| 193 |
}); |
| 194 |
|
| 195 |
}); |
| 196 |
|