tag.php
3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* @package Joomla.Site
* @subpackage Layout
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('JPATH_BASE') or die;
use Joomla\Registry\Registry;
/**
* Layout variables
* ---------------------
*
* @var string $selector The id of the field
* @var string $minTermLength The minimum number of characters for the tag
* @var boolean $allowCustom Can we insert custom tags?
*/
extract($displayData);
// Tags field ajax
$chosenAjaxSettings = new Registry(
array(
'selector' => $selector,
'type' => 'GET',
'url' => JUri::root() . 'index.php?option=com_tags&task=tags.searchAjax',
'dataType' => 'json',
'jsonTermKey' => 'like',
'minTermLength' => $minTermLength
)
);
JHtml::_('formbehavior.ajaxchosen', $chosenAjaxSettings);
// Allow custom values?
if ($allowCustom)
{
JFactory::getDocument()->addScriptDeclaration(
"
jQuery(document).ready(function ($) {
var customTagPrefix = '#new#';
function tagHandler(event,element) {
// Search a highlighted result
var highlighted = $('" . $selector . "_chzn').find('li.active-result.highlighted').first();
// Add the highlighted option
if (event.which === 13 && highlighted.text() !== '')
{
// Extra check. If we have added a custom tag with element text remove it
var customOptionValue = customTagPrefix + highlighted.text();
$('" . $selector . " option').filter(function () { return $(element).val() == customOptionValue; }).remove();
// Select the highlighted result
var tagOption = $('" . $selector . " option').filter(function () { return $(element).html() == highlighted.text(); });
tagOption.attr('selected', 'selected');
}
// Add the custom tag option
else
{
var customTag = element.value;
// Extra check. Search if the custom tag already exists (typed faster than AJAX ready)
var tagOption = $('" . $selector . " option').filter(function () { return $(element).html() == customTag; });
if (tagOption.text() !== '')
{
tagOption.attr('selected', 'selected');
}
else
{
var option = $('<option>');
option.text(element.value).val(customTagPrefix + element.value);
option.attr('selected','selected');
// Append the option and repopulate the chosen field
$('" . $selector . "').append(option);
}
}
element.value = '';
$('" . $selector . "').trigger('liszt:updated');
}
// Method to add tags pressing comma
$('" . $selector . "_chzn input').keypress(function(event) {
if (event.charCode === 44)
{
// Tag is greater than the minimum required chars
if (this.value && this.value.length >= " . $minTermLength . ")
{
tagHandler(event, this);
}
// Do not add comma to tag at all
event.preventDefault();
}
});
// Method to add tags pressing enter
$('" . $selector . "_chzn input').keyup(function(event) {
// Tag is greater than the minimum required chars and enter pressed
if (event.which === 13 && this.value && this.value.length >= " . $minTermLength . ")
{
tagHandler(event,this);
event.preventDefault();
}
});
});
"
);
}