| @@ -28,91 +28,111 @@ | ||
| 28 | 28 | * @return void |
| 29 | 29 | */ |
| 30 | 30 | public static function chosen($selector = '.btn-toolbar', $debug = null, $options = array()) |
| 31 | 31 | { |
| 32 | + // load always select2 plugin | |
| 33 | + // JHtml::fetch('select2'); | |
| 34 | + JText::script('JGLOBAL_SELECT_AN_OPTION'); | |
| 35 | + | |
| 32 | 36 | static $loaded = 0; |
| 33 | 37 | |
| 34 | 38 | // register only once |
| 35 | - if ($loaded) | |
| 39 | + if (!$loaded) | |
| 36 | 40 | { |
| 37 | - return; | |
| 38 | - } | |
| 41 | + $loaded = 1; | |
| 39 | 42 | |
| 40 | - $loaded = 1; | |
| 43 | + /** | |
| 44 | + * Use select2 rendering only in case of WordPress 5.2 or lower. | |
| 45 | + * Starting from WordPress 5.3 the dropdowns are rendered properly | |
| 46 | + * and there is no need to use a different layout. | |
| 47 | + * Contrarily, we will lose the possibility of searching the options. | |
| 48 | + * | |
| 49 | + * @since 10.1.30 | |
| 50 | + */ | |
| 51 | + // $use_select2 = version_compare((new JVersion)->getShortVersion(), '5.3', '<') ? 1 : 0; | |
| 52 | + $use_select2 = 0; | |
| 41 | 53 | |
| 42 | - $version = new JVersion(); | |
| 54 | + // add support for chosen plugin | |
| 55 | + JFactory::getDocument()->addScriptDeclaration( | |
| 56 | +<<<JAVASCRIPT | |
| 57 | +(function($) { | |
| 58 | + 'use strict'; | |
| 43 | 59 | |
| 44 | - /** | |
| 45 | - * Use select2 rendering only in case of WordPress 5.2 or lower. | |
| 46 | - * Starting from WordPress 5.3 the dropdowns are rendered properly | |
| 47 | - * and there is no need to use a different layout. | |
| 48 | - * Contrarily, we will lose the possibility of searching the options. | |
| 49 | - * | |
| 50 | - * @since 10.1.30 | |
| 51 | - */ | |
| 52 | - // $use_select2 = version_compare($version->getShortVersion(), '5.3', '<') ? 1 : 0; | |
| 53 | - $use_select2 = 0; | |
| 60 | + if ($.fn.chosen === undefined) { | |
| 61 | + $.fn.chosen = function(data) { | |
| 62 | + // iterate all selected elements | |
| 63 | + $(this).each(function() { | |
| 64 | + // check is we have a multiple select | |
| 65 | + var isMultiple = $(this).prop('multiple'); | |
| 54 | 66 | |
| 55 | - // load always select2 plugin | |
| 56 | - // JHtml::fetch('select2'); | |
| 57 | - JText::script('JGLOBAL_SELECT_AN_OPTION'); | |
| 67 | + if (!$use_select2 && !isMultiple) { | |
| 68 | + // do not go ahead in case we don't need a custom plugin | |
| 69 | + // to handle standard dropdowns | |
| 70 | + return this; | |
| 71 | + } | |
| 58 | 72 | |
| 59 | - // add support for chosen plugin | |
| 60 | - JFactory::getDocument()->addScriptDeclaration( | |
| 61 | -<<<JS | |
| 62 | -if (jQuery.fn.chosen === undefined) { | |
| 63 | - jQuery.fn.chosen = function(data) { | |
| 64 | - // iterate all selected elements | |
| 65 | - jQuery(this).each(function() { | |
| 66 | - // check is we have a multiple select | |
| 67 | - var isMultiple = jQuery(this).prop('multiple'); | |
| 73 | + if (data !== undefined) { | |
| 74 | + // invoke requested method (e.g. destroy) | |
| 75 | + $(this).select2(data); | |
| 76 | + } else { | |
| 77 | + data = {}; | |
| 78 | + data.width = isMultiple ? 300 : 200; | |
| 79 | + data.allowClear = $(this).hasClass('required') ? false : true; | |
| 80 | + data.placeholder = Joomla.JText._('JGLOBAL_SELECT_AN_OPTION'); | |
| 68 | 81 | |
| 69 | - if (!$use_select2 && !isMultiple) { | |
| 70 | - // do not go ahead in case we don't need a custom plugin | |
| 71 | - // to handle standard dropdowns | |
| 72 | - return this; | |
| 73 | - } | |
| 82 | + var firstOption = $(this).find('option').first(); | |
| 74 | 83 | |
| 75 | - if (data !== undefined) { | |
| 76 | - // invoke requested method (e.g. destroy) | |
| 77 | - jQuery(this).select2(data); | |
| 78 | - } else { | |
| 79 | - data = {}; | |
| 80 | - data.width = isMultiple ? 300 : 200; | |
| 81 | - data.allowClear = jQuery(this).hasClass('required') ? false : true; | |
| 82 | - data.placeholder = Joomla.JText._('JGLOBAL_SELECT_AN_OPTION'); | |
| 84 | + // in case we don't have an empty option, unset placeholder | |
| 85 | + if (!isMultiple && firstOption.length && firstOption.val().length > 0) { | |
| 86 | + data.allowClear = false; | |
| 87 | + data.placeholder = null; | |
| 88 | + } | |
| 83 | 89 | |
| 84 | - var firstOption = jQuery(this).find('option').first(); | |
| 90 | + // turn off search when there are 5 options or less | |
| 91 | + if ($(this).find('option').length <= 5) { | |
| 92 | + data.minimumResultsForSearch = -1; | |
| 93 | + } | |
| 85 | 94 | |
| 86 | - // in case we don't have an empty option, unset placeholder | |
| 87 | - if (!isMultiple && firstOption.length && firstOption.val().length > 0) { | |
| 88 | - data.allowClear = false; | |
| 89 | - data.placeholder = null; | |
| 95 | + // init select2 plugin | |
| 96 | + $(this).select2(data); | |
| 90 | 97 | } |
| 98 | + }); | |
| 91 | 99 | |
| 92 | - // turn off search when there are 5 options or less | |
| 93 | - if (jQuery(this).find('option').length <= 5) { | |
| 94 | - data.minimumResultsForSearch = -1; | |
| 95 | - } | |
| 100 | + return this; | |
| 101 | + } | |
| 102 | + } | |
| 103 | +})(jQuery); | |
| 104 | +JAVASCRIPT | |
| 105 | + ); | |
| 106 | + } | |
| 96 | 107 | |
| 97 | - // init select2 plugin | |
| 98 | - jQuery(this).select2(data); | |
| 99 | - } | |
| 100 | - }); | |
| 108 | + static $selectors = []; | |
| 101 | 109 | |
| 102 | - return this; | |
| 103 | - } | |
| 104 | -} | |
| 110 | + /** | |
| 111 | + * The system is now able to initialize multiple selectors as a result of multiple calls. | |
| 112 | + * | |
| 113 | + * @since 10.1.63 | |
| 114 | + */ | |
| 115 | + if (!isset($selectors[$selector])) { | |
| 116 | + $selectors[$selector] = 1; | |
| 105 | 117 | |
| 106 | -jQuery(document).ready(function() { | |
| 107 | - jQuery('{$selector}') | |
| 108 | - .find('select') | |
| 109 | - .chosen() | |
| 110 | - .on('chosen:updated', function() { | |
| 111 | - // refresh select2 value when triggered | |
| 112 | - // jQuery(this).select2('val', jQuery(this).val()); | |
| 113 | - }); | |
| 114 | -}); | |
| 115 | -JS | |
| 116 | - ); | |
| 118 | + // init requested selectors | |
| 119 | + JFactory::getDocument()->addScriptDeclaration( | |
| 120 | +<<<JAVASCRIPT | |
| 121 | +(function($) { | |
| 122 | + 'use strict'; | |
| 123 | + | |
| 124 | + $(function() { | |
| 125 | + $('{$selector}') | |
| 126 | + .find('select') | |
| 127 | + .chosen() | |
| 128 | + .on('chosen:updated', function() { | |
| 129 | + // refresh select2 value when triggered | |
| 130 | + // $(this).select2('val', $(this).val()); | |
| 131 | + }); | |
| 132 | + }); | |
| 133 | +})(jQuery); | |
| 134 | +JAVASCRIPT | |
| 135 | + ); | |
| 136 | + } | |
| 117 | 137 | } |
| 118 | 138 | } |