| 1 |
jQuery( function( $ ) { |
| 2 |
var widgets_container, widgets_selector, flags; |
| 3 |
|
| 4 |
if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) { |
| 5 |
flags = pll_widgets.flags; |
| 6 |
} |
| 7 |
|
| 8 |
/** |
| 9 |
* Prepend widget titles with a flag once a language is selected. |
| 10 |
* @param {object} widget The widget element. |
| 11 |
* @return {void} Nothing. |
| 12 |
*/ |
| 13 |
function add_flag( widget ) { |
| 14 |
if ( ! flags ) { |
| 15 |
return; |
| 16 |
} |
| 17 |
widget = $( widget ); |
| 18 |
var title = $( '.widget-top .widget-title h3', widget ), |
| 19 |
locale = $( '.pll-lang-choice option:selected', widget ).val(), |
| 20 |
icon = ( locale && flags.hasOwnProperty( locale ) ) ? flags[ locale ] : null; |
| 21 |
|
| 22 |
if ( icon ) { |
| 23 |
icon += ' '; |
| 24 |
var current = $( '.pll-lang', title ); |
| 25 |
if ( current.length ) { |
| 26 |
current.html( icon ); |
| 27 |
} else { |
| 28 |
flag = '<span class="pll-lang">' + icon + '</span>'; |
| 29 |
title.prepend( flag ); |
| 30 |
} |
| 31 |
} else { |
| 32 |
$( '.pll-lang', title ).remove(); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
if ( 'undefined' !== typeof wp.customize ) { |
| 37 |
|
| 38 |
widgets_container = $( '#customize-controls' ); |
| 39 |
widgets_selector = '.customize-control .widget'; |
| 40 |
|
| 41 |
/** |
| 42 |
* WP Customizer add control listener. |
| 43 |
* |
| 44 |
* @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading |
| 45 |
* |
| 46 |
* @param {object} control The control type. |
| 47 |
* @return {void} Nothing. |
| 48 |
*/ |
| 49 |
function customize_add_flag( control ) { |
| 50 |
if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) { |
| 51 |
return; |
| 52 |
} |
| 53 |
|
| 54 |
/* |
| 55 |
* Make sure the widget's contents are embedded; normally this is done |
| 56 |
* when the control is expanded, for DOM performance reasons. |
| 57 |
*/ |
| 58 |
control.embedWidgetContent(); |
| 59 |
|
| 60 |
// Now we know for sure the widget is fully embedded. |
| 61 |
add_flag( control.container.find( '.widget' ) ); |
| 62 |
} |
| 63 |
wp.customize.control.each( customize_add_flag ); |
| 64 |
wp.customize.control.bind( 'add', customize_add_flag ); |
| 65 |
|
| 66 |
} else { |
| 67 |
|
| 68 |
widgets_container = $( '#widgets-right' ); |
| 69 |
widgets_selector = '.widget'; |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
// Add flags on load. |
| 74 |
$( widgets_selector, widgets_container ).each( function() { |
| 75 |
add_flag( this ); |
| 76 |
} ); |
| 77 |
|
| 78 |
// Update flags. |
| 79 |
widgets_container.on( 'change', '.pll-lang-choice', function() { |
| 80 |
add_flag( $( this ).parents( '.widget' ) ); |
| 81 |
} ); |
| 82 |
|
| 83 |
} ); |
| 84 |
|