-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathInputfieldChosenSelectMultiple.js
152 lines (111 loc) · 4.02 KB
/
InputfieldChosenSelectMultiple.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
(function($) {
// From Github:
// https://github.com/mrhenry/jquery-chosen-sortable
$.fn.chosenOrder = function() {
var $this = this.first(),
$chosen = $this.siblings('.chosen-container');
return $($chosen.find('.chosen-choices li[class!="search-field"]').map( function() {
if (!this) {
return undefined;
}
return $this.find('option:contains(' + $(this).text() + ')')[0];
}));
};
$.fn.chosenSortable = function(){
var $this = this;
$this.each(function(){
var $select = $(this);
var $chosen = $select.siblings('.chosen-container');
$chosen.find('.chosen-choices').bind('mousedown', function(event){
if ($(event.target).is('span')) {
event.stopPropagation();
}
});
$chosen.find('.chosen-choices').sortable({
'placeholder' : 'ui-state-highlight chosen-ghost',
'items' : 'li:not(.search-field)',
//'update' : _update,
'tolerance' : 'pointer'
});
$select.closest('form').one('submit.sortable', function(){
var $options = $select.chosenOrder();
$options.detach().appendTo($select);
});
});
return this;
};
// Updated version of this:
// http://stackoverflow.com/questions/7385246/allow-new-values-with-chosen-js-multiple-select/12961228#12961228
$.fn.chosenAddable = function(){
var $select = this,
$chosen = $select.siblings('.chosen-container');
function moveNewPagesToInputfield(){
$(this).off("submit.addable");
var options = $select.children("[rel='add']").detach().map(function () {
return $(this).text();
}).get();
if(options.length){
// fieldname, remove "Inputfield_" in front of id
fieldName = $select.attr("id").substring(11);
$textarea = $('#_'+fieldName+'_add_items').val(options.join("\n"));
}
}
function trackNoResultEntries(event){
var stroke, _ref, target, list;
// get keycode
stroke = (_ref = event.which) != null ? _ref : event.keyCode;
target = $(event.target);
if (stroke === 9 || stroke === 13 ) {
var value;
// Get current Tags
list = $select.data('chosen').results_data.map(function(ele){
if(ele.text !== undefined) text = ele.text.toLowerCase();
else text = "";
return text;
});
value = $.trim(target.val());
if(list.indexOf(value.toLowerCase()) === -1) {
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
$('<option>')
.text(value).val(value)
.attr('selected','selected')
.attr("rel", "add")
.appendTo($select);
setTimeout(function(){ $select.trigger('chosen:updated')}, 10);
return false;
}
}
$select.closest('form').one('submit.addable', moveNewPagesToInputfield);
return this;
}
$chosen.on("keyup cut paste focus", trackNoResultEntries);
}
}(jQuery));
var initChosenMulti = function() {
var $t = $(this),
$addable = $t.closest(".InputfieldChosenSelectMultiple").find(".InputfieldPageAdd");
var tid = $t.attr('id');
var oid = tid.split('_repeater')[0];
if(typeof config === 'undefined') {
var options = {sortable: true};
} else {
var options = config[oid];
}
if($addable.length){
options["no_results_text"] = options["no_results_text_addable"];
$t.chosen(options).chosenSortable().chosenAddable();
if(!$t.data('chosen')){
$addable.css("display", "block");
}
}else{
$t.chosen(options).chosenSortable();
}
};
$(document).ready(function() {
$(".InputfieldChosenSelectMultiple select[multiple=multiple]").each(initChosenMulti);
$(document).on('reloaded opened repeateradd wiretabclick', '.InputfieldPage', function() {
$(this).find(".InputfieldChosenSelectMultiple select[multiple=multiple]").each(initChosenMulti);
});
});