-
Notifications
You must be signed in to change notification settings - Fork 6
/
superbly-tagfield.js
323 lines (284 loc) · 10.2 KB
/
superbly-tagfield.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*!
* superbly tagfield v0.6
* http://www.superbly.ch
*
* Copyright 2011, Manuel Spierenburg
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://www.superbly.ch/licenses/mit-license.txt
* http://www.superbly.ch/licenses/gpl-2.0.txt
*
* Date: 27-11-2011
*/
(function($){
$.fn.superblyTagField = function(userOptions) {
var settings = {
caseSensitive:true,
allowNewTags:true,
allowedTagsNumber:false,
showTagsNumber:10,
addItemOnBlur:false,
preset:[],
tags:[],
onRemove:function(tag) { return true; }
};
if(userOptions) {
$.extend(settings, userOptions);
}
superblyTagField(this, settings);
return this;
};
var keyMap = {
downArrow:40,
upArrow:38,
enter:13,
tab:9,
backspace:8
}
function superblyTagField(tagField,settings) {
var tags = settings.tags.sort();
var preset = settings.preset;
var caseSensitive = settings.caseSensitive;
var allowNewTags = settings.allowNewTags;
var allowedTagsNumber = settings.allowedTagsNumber;
var showTagsNumber = settings.showTagsNumber;
var addItemOnBlur = settings.addItemOnBlur;
var onRemove = settings.onRemove;
var tagstmp = tags.slice();
// prepare needed vars
var inserted = new Array();
var selectedIndex = null;
var currentValue = null;
var currentItem = null;
var hoverSuggestItems = false;
tagField.css('display', 'none');
var superblyMarkup = '<div class="superblyTagfieldDiv"><ul class="superblyTagItems"><li class="superblyTagInputItem"><input class="superblyTagInput" type="text" autocomplete="false"><ul class="superblySuggestItems"></ul></li></ul><div class="superblyTagfieldClearer"></div></div>';
tagField.after(superblyMarkup);
var tagInput = $(".superblyTagInput", tagField.next());
var suggestList = tagInput.next();
var inputItem = tagInput.parent();
var tagList = inputItem.parent();
// set presets
for(i in preset){
addItemWithFocus(preset[i],false);
}
// events
suggestList.mouseover(function(e){
hoverSuggestItems = true;
});
suggestList.mouseleave(function(e){
hoverSuggestItems = false;
});
tagInput.keyup(function(e){
if((allowedTagsNumber == false) || (inserted.length < allowedTagsNumber)){
suggest($(this).val());
}
});
tagInput.focusout(function(e){
if(!hoverSuggestItems){
suggestList.css('display', 'none');
}
});
tagInput.focus(function(e){
currentValue = null;
});
tagInput.keydown(function(e){
if(e.keyCode == keyMap.downArrow) {
selectDown();
}else if(e.keyCode == keyMap.upArrow) {
selectUp()
}else if(e.keyCode == keyMap.enter || e.keyCode == keyMap.tab) {
checkForItem();
// prevent default action for enter
return e.keyCode != keyMap.enter;
}else if(e.keyCode == keyMap.backspace){
// backspace
if(tagInput.val() == ''){
removeLastItem();
}
updateTagInputWidth();
} else {
updateTagInputWidth();
}
});
if (addItemOnBlur) {
tagInput.blur(function(e){
checkForItem();
});
}
tagList.parent().click(function(e){
tagInput.focus();
});
// functions
function setValue(){
tagField.val(inserted.join(','));
}
function updateTagInputWidth()
{
/*
* To make tag wrapping behave as expected, dynamically adjust
* the tag input's width to its content's width
* The best way to get the content's width in pixels is to add it
* to the DOM, grab the width, then remove it from the DOM.
*/
var temp = $("<span />").text(tagInput.val()).appendTo(inputItem);
var width = temp.width();
temp.remove();
tagInput.width(width + 20);
}
function checkForItem(value){
if(allowedTagsNumber != false){
if(inserted.length >= allowedTagsNumber){
return;
}
}
if(currentItem != null){
addItem(currentItem);
} else if(allowNewTags){
var value = tagInput.val();
if(value != null && value != ''){
addItem(value);
}
}
}
function addItem(value){
addItemWithFocus(value,true);
}
function addItemWithFocus(value,setFocusToInputField){
var caseInSensitiveFound = false;
if(!caseSensitive){
$.each(inserted,function(index, val) {
if (caseInSensitiveFound == false && (val.toLowerCase() == value.toLowerCase())) {
caseInSensitiveFound = true;
return true;
}
});
}
var index = jQuery.inArray(value,tagstmp);
if((jQuery.inArray(value,inserted) == -1) && ( index > -1 || allowNewTags) && !caseInSensitiveFound){
//delete from tags
if(index >-1){
tagstmp.splice(index,1);
}
inserted.push(value);
inputItem.before("<li class='superblyTagItem'><span>" + value + "</span><a> x</a></li>");
tagInput.val("");
currentValue = null;
currentItem = null;
// add remove click event
var new_index = tagList.children('.superblyTagItem').size()-1;
$(tagList.children('.superblyTagItem')[new_index]).children('a').click(function(e){
var value = $($(this).parent('.superblyTagItem').children('span')[0]).text();
removeItem(value);
});
}
suggestList.css('display', 'none');
updateTagInputWidth();
if(setFocusToInputField){
tagInput.focus();
}
setValue();
if((allowedTagsNumber != false) && (inserted.length >= allowedTagsNumber)){
disableAddItem()
}
}
function disableAddItem(){
tagInput.attr('disabled','disabled');
suggestList.css('display','none');
}
function enableAddItem(){
tagInput.removeAttr('disabled');
suggestList.show();
}
function removeItem(value){
if(onRemove(value) === false) {
return false;
}
var index = jQuery.inArray(value,tags);
var tmpIndex = jQuery.inArray(value,tagstmp);
if(index > -1 && tmpIndex == -1){
tagstmp.push(value);
}
index = jQuery.inArray(value,inserted);
if(index > -1){
inserted.splice(index,1);
tagList.children(".superblyTagItem").filter(function(){return $('span', this).html() == value;}).remove();
}
tagstmp.sort();
tagInput.focus();
setValue();
if((allowedTagsNumber != false) && (inserted.length < allowedTagsNumber)){
enableAddItem();
}
}
function removeLastItem(){
var last_index = inserted.length-1;
var last_value = inserted[last_index];
removeItem(last_value);
}
function getSuggestionsArray(value){
var suggestions = new Array();
var count = 0;
for(key in tagstmp){
if(showTagsNumber <= count){
break;
}
// if beginning is same
var lower_case_tag = tagstmp[key].toLocaleLowerCase();
var lower_cast_value = value.toLowerCase();
if(lower_case_tag.indexOf(lower_cast_value) == 0){
suggestions.push(tagstmp[key]);
count++;
}
}
return suggestions;
}
function suggest(value){
suggestList.show();
if(value == currentValue){
return false;
}
currentValue = value;
suggestList.empty();
var suggestions = getSuggestionsArray(value);
for(key in suggestions){
suggestList.append("<li class='superblySuggestItem'>" + suggestions[key] + "</li>");
}
var suggestionItems = suggestList.children('.superblySuggestItem');
// add click event to suggest items
suggestionItems.click(function(e){
addItem($(this).html());
});
selectedIndex=null;
if(!allowNewTags){
selectedIndex=0;
$(suggestionItems[selectedIndex]).addClass("selected");
currentItem = $(suggestionItems[selectedIndex]).html();
}
}
function selectDown(){
var suggestions = suggestList.children('.superblySuggestItem');
var size = suggestions.size();
if(selectedIndex == null){
selectedIndex=0;
}else if(selectedIndex < size-1){
$(suggestions[selectedIndex]).removeClass("selected");
selectedIndex++;
}
$(suggestions[selectedIndex]).addClass("selected");
currentItem = $(suggestions[selectedIndex]).html();
}
function selectUp(){
if(selectedIndex == 0){
selectedIndex=null;
currentItem = null;
tagInput.focus();
} else if(selectedIndex >0){
var suggestions = suggestList.children('.superblySuggestItem');
$(suggestions[selectedIndex]).removeClass("selected");
selectedIndex--;
$(suggestions[selectedIndex]).addClass("selected");
currentItem = $(suggestions[selectedIndex]).html();
}
}
}
})(jQuery);