| @@ -1,0 +1,138 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikWP - Libraries | |
| 4 | + * @subpackage adapter.html | |
| 5 | + * @author E4J s.r.l. | |
| 6 | + * @copyright Copyright (C) 2023 E4J s.r.l. All Rights Reserved. | |
| 7 | + * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +/** | |
| 15 | + * Utility class for form related behaviors. | |
| 16 | + * | |
| 17 | + * @since 10.1.16 | |
| 18 | + */ | |
| 19 | +abstract class JHtmlFormbehavior | |
| 20 | +{ | |
| 21 | + /** | |
| 22 | + * Method to load the Chosen JavaScript framework and supporting CSS into the document head. | |
| 23 | + * | |
| 24 | + * @param string $selector Class for Chosen elements. | |
| 25 | + * @param mixed $debug Is debugging mode on? [optional]. | |
| 26 | + * @param array $options The possible Chosen options as name => value [optional]. | |
| 27 | + * | |
| 28 | + * @return void | |
| 29 | + */ | |
| 30 | + public static function chosen($selector = '.btn-toolbar', $debug = null, $options = array()) | |
| 31 | + { | |
| 32 | + // load always select2 plugin | |
| 33 | + // JHtml::fetch('select2'); | |
| 34 | + JText::script('JGLOBAL_SELECT_AN_OPTION'); | |
| 35 | + | |
| 36 | + static $loaded = 0; | |
| 37 | + | |
| 38 | + // register only once | |
| 39 | + if (!$loaded) | |
| 40 | + { | |
| 41 | + $loaded = 1; | |
| 42 | + | |
| 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; | |
| 53 | + | |
| 54 | + // add support for chosen plugin | |
| 55 | + JFactory::getDocument()->addScriptDeclaration( | |
| 56 | +<<<JAVASCRIPT | |
| 57 | +(function($) { | |
| 58 | + 'use strict'; | |
| 59 | + | |
| 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'); | |
| 66 | + | |
| 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 | + } | |
| 72 | + | |
| 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'); | |
| 81 | + | |
| 82 | + var firstOption = $(this).find('option').first(); | |
| 83 | + | |
| 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 | + } | |
| 89 | + | |
| 90 | + // turn off search when there are 5 options or less | |
| 91 | + if ($(this).find('option').length <= 5) { | |
| 92 | + data.minimumResultsForSearch = -1; | |
| 93 | + } | |
| 94 | + | |
| 95 | + // init select2 plugin | |
| 96 | + $(this).select2(data); | |
| 97 | + } | |
| 98 | + }); | |
| 99 | + | |
| 100 | + return this; | |
| 101 | + } | |
| 102 | + } | |
| 103 | +})(jQuery); | |
| 104 | +JAVASCRIPT | |
| 105 | + ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + static $selectors = []; | |
| 109 | + | |
| 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; | |
| 117 | + | |
| 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 | + } | |
| 137 | + } | |
| 138 | +} | |