| 1 |
var __webpack_exports__ = {}; |
| 2 |
/** |
| 3 |
* Adds a flag to the widgets filtered by a language. |
| 4 |
* |
| 5 |
* @package Polylang |
| 6 |
*/ |
| 7 |
|
| 8 |
jQuery( |
| 9 |
function( $ ) { |
| 10 |
var widgets_container, |
| 11 |
widgets_selector, |
| 12 |
flags, |
| 13 |
isBlockEditor = 'undefined' !== typeof wp.blockEditor; |
| 14 |
|
| 15 |
if ( 'undefined' !== typeof pll_widgets && pll_widgets.hasOwnProperty( 'flags' ) ) { |
| 16 |
flags = pll_widgets.flags; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Prepend widget titles with a flag once a language is selected. |
| 21 |
* |
| 22 |
* @param {object} widget The widget element. |
| 23 |
* @return {void} Nothing. |
| 24 |
*/ |
| 25 |
function add_flag( widget ) { |
| 26 |
if ( ! flags ) { |
| 27 |
return; |
| 28 |
} |
| 29 |
widget = $( widget ); |
| 30 |
var title = isBlockEditor ? widget.prev('h3') : $( '.widget-top .widget-title h3', widget ), |
| 31 |
locale = $( '.pll-lang-choice option:selected', widget ).val(), |
| 32 |
// Icon is HTML built and come from server side and is well escaped when necessary |
| 33 |
icon = ( locale && flags.hasOwnProperty( locale ) ) ? flags[ locale ] : null; |
| 34 |
|
| 35 |
if ( icon ) { |
| 36 |
icon += ' '; |
| 37 |
var current = $( '.pll-lang', title ); |
| 38 |
if ( current.length ) { |
| 39 |
current.html( icon ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html |
| 40 |
} else { |
| 41 |
flag = $( '<span />' ).addClass( 'pll-lang' ).html( icon ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.html |
| 42 |
// See the comment above about the icon which is safe. So it is also safe to prepend flag which uses icon. |
| 43 |
title.prepend( flag ); // phpcs:ignore WordPressVIPMinimum.JS.HTMLExecutingFunctions.prepend |
| 44 |
} |
| 45 |
} else { |
| 46 |
$( '.pll-lang', title ).remove(); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
if ( isBlockEditor ) { |
| 51 |
|
| 52 |
widgets_container = $( '.edit-widgets-main-block-list' ); |
| 53 |
widgets_selector = '.widget'; |
| 54 |
|
| 55 |
// Update flags when we click on the legacy widget to display its form. |
| 56 |
widgets_container.on( |
| 57 |
'click', |
| 58 |
'.wp-block-legacy-widget', |
| 59 |
function() { |
| 60 |
add_flag( $( this ).find( '.widget' ) ); |
| 61 |
} |
| 62 |
); |
| 63 |
|
| 64 |
} else { |
| 65 |
if ( 'undefined' !== typeof wp.customize ) { |
| 66 |
|
| 67 |
widgets_container = $( '#customize-controls' ); |
| 68 |
widgets_selector = '.customize-control .widget'; |
| 69 |
|
| 70 |
/** |
| 71 |
* WP Customizer add control listener. |
| 72 |
* |
| 73 |
* @link https://wordpress.stackexchange.com/questions/256536/callback-after-wordpress-customizer-complete-loading |
| 74 |
* |
| 75 |
* @param {object} control The control type. |
| 76 |
* @return {void} Nothing. |
| 77 |
*/ |
| 78 |
function customize_add_flag( control ) { |
| 79 |
if ( ! control.extended( wp.customize.Widgets.WidgetControl ) ) { |
| 80 |
return; |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* Make sure the widget's contents are embedded; normally this is done |
| 85 |
* when the control is expanded, for DOM performance reasons. |
| 86 |
*/ |
| 87 |
control.embedWidgetContent(); |
| 88 |
|
| 89 |
// Now we know for sure the widget is fully embedded. |
| 90 |
add_flag( control.container.find( '.widget' ) ); |
| 91 |
} |
| 92 |
wp.customize.control.each( customize_add_flag ); |
| 93 |
wp.customize.control.bind( 'add', customize_add_flag ); |
| 94 |
|
| 95 |
} else { |
| 96 |
|
| 97 |
widgets_container = $( '#widgets-right' ); |
| 98 |
widgets_selector = '.widget'; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
// Add flags on load. |
| 103 |
$( widgets_selector, widgets_container ).each( |
| 104 |
function() { |
| 105 |
add_flag( this ); |
| 106 |
} |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
// Update flags. |
| 111 |
widgets_container.on( |
| 112 |
'change', |
| 113 |
'.pll-lang-choice', |
| 114 |
function() { |
| 115 |
add_flag( $( this ).parents( '.widget' ) ); |
| 116 |
} |
| 117 |
); |
| 118 |
|
| 119 |
function pll_toggle( a, test ) { |
| 120 |
test ? a.show() : a.hide(); |
| 121 |
} |
| 122 |
|
| 123 |
// Remove all options if dropdown is checked. |
| 124 |
$( '.widgets-sortables,.control-section-sidebar,.edit-widgets-main-block-list' ).on( |
| 125 |
'change', |
| 126 |
'.pll-dropdown', |
| 127 |
function() { |
| 128 |
var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 129 |
pll_toggle( $( '.no-dropdown-' + this_id ), true != $( this ).prop( 'checked' ) ); |
| 130 |
} |
| 131 |
); |
| 132 |
|
| 133 |
// Disallow unchecking both show names and show flags. |
| 134 |
var options = ['-show_flags', '-show_names']; |
| 135 |
$.each( |
| 136 |
options, |
| 137 |
function( i, v ) { |
| 138 |
$( '.widgets-sortables,.control-section-sidebar,.edit-widgets-main-block-list' ).on( |
| 139 |
'change', |
| 140 |
'.pll' + v, |
| 141 |
function() { |
| 142 |
var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 143 |
if ( true != $( this ).prop( 'checked' ) ) { |
| 144 |
$( '#widget-' + this_id + options[ 1 - i ] ).prop( 'checked', true ); |
| 145 |
} |
| 146 |
} |
| 147 |
); |
| 148 |
} |
| 149 |
); |
| 150 |
|
| 151 |
} |
| 152 |
); |
| 153 |
|
| 154 |
|