| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package VikWP - Libraries |
| 4 |
* @subpackage adapter.html |
| 5 |
* @author E4J s.r.l. |
| 6 |
* @copyright Copyright (C) 2021 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 |
static $loaded = 0; |
| 33 |
|
| 34 |
// register only once |
| 35 |
if ($loaded) |
| 36 |
{ |
| 37 |
return; |
| 38 |
} |
| 39 |
|
| 40 |
$loaded = 1; |
| 41 |
|
| 42 |
$version = new JVersion(); |
| 43 |
|
| 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; |
| 54 |
|
| 55 |
// load always select2 plugin |
| 56 |
// JHtml::fetch('select2'); |
| 57 |
JText::script('JGLOBAL_SELECT_AN_OPTION'); |
| 58 |
|
| 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'); |
| 68 |
|
| 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 |
} |
| 74 |
|
| 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'); |
| 83 |
|
| 84 |
var firstOption = jQuery(this).find('option').first(); |
| 85 |
|
| 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; |
| 90 |
} |
| 91 |
|
| 92 |
// turn off search when there are 5 options or less |
| 93 |
if (jQuery(this).find('option').length <= 5) { |
| 94 |
data.minimumResultsForSearch = -1; |
| 95 |
} |
| 96 |
|
| 97 |
// init select2 plugin |
| 98 |
jQuery(this).select2(data); |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
return this; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 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 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
|