forked from sproutsocial/inputosaurus-text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputosaurus.js
511 lines (392 loc) · 13.7 KB
/
inputosaurus.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/**
* Inputosaurus Text
*
* Must be instantiated on an <input> element
* Allows multiple input items. Each item is represented with a removable tag that appears to be inside the input area.
*
* @requires:
*
* jQuery 1.7+
* jQueryUI 1.8+ Core
*
* @version 0.1.6
* @author Dan Kielp <[email protected]>
* @created October 3,2012
*
*/
(function($) {
var inputosaurustext = {
version: "0.1.6",
eventprefix: "inputosaurus",
options: {
// bindable events
//
// 'change' - triggered whenever a tag is added or removed (should be similar to binding the the change event of the instantiated input
// 'keyup' - keyup event on the newly created input
// while typing, the user can separate values using these delimiters
// the value tags are created on the fly when an inputDelimiter is detected
inputDelimiters : [',', ';'],
// this separator is used to rejoin all input items back to the value of the original <input>
outputDelimiter : ',',
allowDuplicates : false,
parseOnBlur : false,
// optional wrapper for widget
wrapperElement : null,
width : null,
// simply passing an autoComplete source (array, string or function) will instantiate autocomplete functionality
autoCompleteSource : '',
// When forcing users to select from the autocomplete list, allow them to press 'Enter' to select an item if it's the only option left.
activateFinalResult : false,
// manipulate and return the input value after parseInput() parsing
// the array of tag names is passed and expected to be returned as an array after manipulation
parseHook : null,
// define a placeholder to display when the input is empty
placeholder: null,
},
_create: function() {
var widget = this,
els = {},
o = widget.options,
placeholder = o.placeholder || this.element.attr('placeholder') || null;
this._chosenValues = [];
// Create the elements
els.ul = $('<ul class="inputosaurus-container">');
els.input = $('<input type="text" />');
els.inputCont = $('<li class="inputosaurus-input inputosaurus-required"></li>');
els.origInputCont = $('<li class="inputosaurus-input-hidden inputosaurus-required">');
// define starting placeholder
if (placeholder) {
o.placeholder = placeholder;
els.input.attr('placeholder', o.placeholder);
if (o.width) {
els.input.css('min-width', o.width - 50);
}
}
o.wrapperElement && o.wrapperElement.append(els.ul);
this.element.replaceWith(o.wrapperElement || els.ul);
els.origInputCont.append(this.element).hide();
els.inputCont.append(els.input);
els.ul.append(els.inputCont);
els.ul.append(els.origInputCont);
o.width && els.ul.css('width', o.width);
this.elements = els;
widget._attachEvents();
// if instantiated input already contains a value, parse that junk
if($.trim(this.element.val())){
els.input.val( this.element.val() );
this.parseInput();
}
this._instAutocomplete();
},
_instAutocomplete : function() {
if(this.options.autoCompleteSource){
var widget = this;
this.elements.input.autocomplete({
position : {
of : this.elements.ul
},
source : this.options.autoCompleteSource,
minLength : 1,
select : function(ev, ui){
ev.preventDefault();
widget.elements.input.val(ui.item.value);
widget.parseInput();
},
open : function() {
// Older versions of jQueryUI have a different namespace
var auto = $(this).data('ui-autocomplete') || $(this).data('autocomplete');
var menu = auto.menu,
$menuItems;
// zIndex will force the element on top of anything (like a dialog it's in)
menu.element.zIndex && menu.element.zIndex($(this).zIndex() + 1);
menu.element.width(widget.elements.ul.outerWidth());
// auto-activate the result if it's the only one
if(widget.options.activateFinalResult){
$menuItems = menu.element.find('li');
// activate single item to allow selection upon pressing 'Enter'
if($menuItems.size() === 1){
menu[menu.activate ? 'activate' : 'focus']($.Event('click'), $menuItems);
}
}
}
});
}
},
_autoCompleteMenuPosition : function() {
var widget;
if(this.options.autoCompleteSource){
widget = this.elements.input.data('ui-autocomplete') || this.elements.input.data('autocomplete');
widget && widget.menu.element.position({
of: this.elements.ul,
my: 'left top',
at: 'left bottom',
collision: 'none'
});
}
},
/*_closeAutoCompleteMenu : function() {
if(this.options.autoCompleteSource){
this.elements.input.autocomplete('close');
}
},*/
parseInput : function(ev) {
var widget = (ev && ev.data.widget) || this,
val,
delimiterFound = false,
values = [];
val = widget.elements.input.val();
val && (delimiterFound = widget._containsDelimiter(val));
if(delimiterFound !== false){
values = val.split(delimiterFound);
} else if(!ev || ev.which === $.ui.keyCode.ENTER && !$('.ui-menu-item .ui-state-focus').size() && !$('#ui-active-menuitem').size()){
values.push(val);
ev && ev.preventDefault();
// prevent autoComplete menu click from causing a false 'blur'
} else if(ev.type === 'blur' && !$('#ui-active-menuitem').size()){
values.push(val);
}
$.isFunction(widget.options.parseHook) && (values = widget.options.parseHook(values));
if(values.length){
widget._setChosen(values);
widget.elements.input.val('');
widget._resizeInput();
}
widget._resetPlaceholder();
},
_inputFocus : function(ev) {
var widget = ev.data.widget || this;
widget.elements.input.value || (widget.options.autoCompleteSource.length && widget.elements.input.autocomplete('search', ''));
},
_inputKeypress : function(ev) {
var widget = ev.data.widget || this;
ev.type === 'keyup' && widget._trigger('keyup', ev, widget);
switch(ev.which){
case $.ui.keyCode.BACKSPACE:
ev.type === 'keydown' && widget._inputBackspace(ev);
break;
case $.ui.keyCode.LEFT:
ev.type === 'keydown' && widget._inputBackspace(ev);
break;
default :
widget.parseInput(ev);
widget._resizeInput(ev);
}
// reposition autoComplete menu as <ul> grows and shrinks vertically
if(widget.options.autoCompleteSource){
setTimeout(function(){widget._autoCompleteMenuPosition.call(widget);}, 200);
}
},
// the input dynamically resizes based on the length of its value
_resizeInput : function(ev) {
var widget = (ev && ev.data.widget) || this,
maxWidth = widget.elements.ul.width(),
val = widget.elements.input.val(),
txtWidth = 25 + val.length * 8;
widget.elements.input.width(txtWidth < maxWidth ? txtWidth : maxWidth);
},
// resets placeholder on representative input
_resetPlaceholder: function () {
var placeholder = this.options.placeholder,
input = this.elements.input,
width = this.options.width || 'inherit';
if (placeholder && this.element.val().length === 0) {
input.attr('placeholder', placeholder).css('min-width', width - 50)
}else {
input.attr('placeholder', '').css('min-width', 'inherit')
}
},
// if our input contains no value and backspace has been pressed, select the last tag
_inputBackspace : function(ev) {
var widget = (ev && ev.data.widget) || this;
lastTag = widget.elements.ul.find('li:not(.inputosaurus-required):last');
// IE goes back in history if the event isn't stopped
ev.stopPropagation();
if((!$(ev.currentTarget).val() || (('selectionStart' in ev.currentTarget) && ev.currentTarget.selectionStart === 0 && ev.currentTarget.selectionEnd === 0)) && lastTag.size()){
ev.preventDefault();
lastTag.find('a').focus();
}
},
_editTag : function(ev) {
var widget = (ev && ev.data.widget) || this,
tagName = '',
tagKey = $(ev.currentTarget).closest('li').data('inputosaurus');
if(!tagKey){
return true;
}
ev.preventDefault();
$.each(widget._chosenValues, function(i,v) {
v.key === tagKey && (tagName = v.value);
});
widget.elements.input.val(tagName);
widget._removeTag(ev);
widget._resizeInput(ev);
},
_tagKeypress : function(ev) {
var widget = ev.data.widget;
switch(ev.which){
case $.ui.keyCode.BACKSPACE:
ev && ev.preventDefault();
ev && ev.stopPropagation();
$(ev.currentTarget).trigger('click');
break;
// 'e' - edit tag (removes tag and places value into visible input
case 69:
widget._editTag(ev);
break;
case $.ui.keyCode.LEFT:
ev.type === 'keydown' && widget._prevTag(ev);
break;
case $.ui.keyCode.RIGHT:
ev.type === 'keydown' && widget._nextTag(ev);
break;
case $.ui.keyCode.DOWN:
ev.type === 'keydown' && widget._focus(ev);
break;
}
},
// select the previous tag or input if no more tags exist
_prevTag : function(ev) {
var widget = (ev && ev.data.widget) || this,
tag = $(ev.currentTarget).closest('li'),
previous = tag.prev();
if(previous.is('li')){
previous.find('a').focus();
} else {
widget._focus();
}
},
// select the next tag or input if no more tags exist
_nextTag : function(ev) {
var widget = (ev && ev.data.widget) || this,
tag = $(ev.currentTarget).closest('li'),
next = tag.next();
if(next.is('li:not(.inputosaurus-input)')){
next.find('a').focus();
} else {
widget._focus();
}
},
// return the inputDelimiter that was detected or false if none were found
_containsDelimiter : function(tagStr) {
var found = false;
$.each(this.options.inputDelimiters, function(k,v) {
if(tagStr.indexOf(v) !== -1){
found = v;
}
});
return found;
},
_setChosen : function(valArr) {
var self = this;
if(!$.isArray(valArr)){
return false;
}
$.each(valArr, function(k,v) {
var exists = false,
obj = {
key : '',
value : ''
};
v = $.trim(v);
$.each(self._chosenValues, function(kk,vv) {
vv.value === v && (exists = true);
});
if(v !== '' && (!exists || self.options.allowDuplicates)){
obj.key = 'mi_' + Math.random().toString( 16 ).slice( 2, 10 );
obj.value = v;
self._chosenValues.push(obj);
self._renderTags();
}
});
self._setValue(self._buildValue());
},
_buildValue : function() {
var widget = this,
value = '';
$.each(this._chosenValues, function(k,v) {
value += value.length ? widget.options.outputDelimiter + v.value : v.value;
});
return value;
},
_setValue : function(value) {
var val = this.element.val();
if(val !== value){
this.element.val(value);
this._trigger('change');
}
},
// @name text for tag
// @className optional className for <li>
_createTag : function(name, key, className) {
className = className ? ' class="' + className + '"' : '';
if(name !== undefined){
return $('<li' + className + ' data-inputosaurus="' + key + '"><span>' + name + '</span> <a href="javascript:void(0);" class="ficon">✖</a></li>');
}
},
_renderTags : function() {
var self = this;
this.elements.ul.find('li:not(.inputosaurus-required)').remove();
$.each(this._chosenValues, function(k,v) {
var el = self._createTag(v.value, v.key);
self.elements.ul.find('li.inputosaurus-input').before(el);
});
},
_removeTag : function(ev) {
var key = $(ev.currentTarget).closest('li').data('inputosaurus'),
indexFound = false,
widget = (ev && ev.data.widget) || this;
$.each(widget._chosenValues, function(k,v) {
if(key === v.key){
indexFound = k;
}
});
indexFound !== false && widget._chosenValues.splice(indexFound, 1);
widget._setValue(widget._buildValue());
$(ev.currentTarget).closest('li').remove();
widget.elements.input.focus();
},
_focus : function(ev) {
var widget = (ev && ev.data.widget) || this;
if(!ev || !$(ev.target).closest('li').data('inputosaurus')){
widget.elements.input.focus();
}
},
_tagFocus : function(ev) {
$(ev.currentTarget).parent()[ev.type === 'focusout' ? 'removeClass' : 'addClass']('inputosaurus-selected');
},
refresh : function() {
var delim = this.options.outputDelimiter,
val = this.element.val(),
values = [];
values.push(val);
delim && (values = val.split(delim));
if(values.length){
this._chosenValues = [];
$.isFunction(this.options.parseHook) && (values = this.options.parseHook(values));
this._setChosen(values);
this._renderTags();
this.elements.input.val('');
this._resizeInput();
}
},
_attachEvents : function() {
var widget = this;
this.elements.input.on('keyup.inputosaurus', {widget : widget}, this._inputKeypress);
this.elements.input.on('keydown.inputosaurus', {widget : widget}, this._inputKeypress);
this.elements.input.on('change.inputosaurus', {widget : widget}, this._inputKeypress);
this.elements.input.on('focus.inputosaurus', {widget : widget}, this._inputFocus);
this.options.parseOnBlur && this.elements.input.on('blur.inputosaurus', {widget : widget}, this.parseInput);
this.elements.ul.on('click.inputosaurus', {widget : widget}, this._focus);
this.elements.ul.on('click.inputosaurus', 'a', {widget : widget}, this._removeTag);
this.elements.ul.on('dblclick.inputosaurus', 'li', {widget : widget}, this._editTag);
this.elements.ul.on('focus.inputosaurus', 'a', {widget : widget}, this._tagFocus);
this.elements.ul.on('blur.inputosaurus', 'a', {widget : widget}, this._tagFocus);
this.elements.ul.on('keydown.inputosaurus', 'a', {widget : widget}, this._tagKeypress);
},
_destroy: function() {
this.elements.input.unbind('.inputosaurus');
this.elements.ul.replaceWith(this.element);
}
};
$.widget("ui.inputosaurus", inputosaurustext);
})(jQuery);