Skip to content

Commit 616023f

Browse files
committed
freeInput now works with objects
1 parent 8e042dd commit 616023f

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

dist/bootstrap-tagsinput.js

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@
266266
pushVal: function() {
267267
var self = this,
268268
val = $.map(self.items(), function(item) {
269-
return self.options.itemValue(item).toString();
269+
//return self.options.itemValue(item).toString();
270+
if(typeof self.options.itemValue(item) !== 'undefined'){
271+
return self.options.itemValue(item).toString();
272+
}
273+
else{
274+
275+
}
270276
});
271277

272278
self.$element.val(val, true).trigger('change');
@@ -280,8 +286,8 @@
280286

281287
self.options = $.extend({}, defaultOptions, options);
282288
// When itemValue is set, freeInput should always be false
283-
if (self.objectItems)
284-
self.options.freeInput = false;
289+
if (self.objectItems);
290+
//self.options.freeInput = false; //TRP 12/24/15
285291

286292
makeOptionItemFunction(self.options, 'itemValue');
287293
makeOptionItemFunction(self.options, 'itemText');
@@ -369,16 +375,16 @@
369375
self.$input.focus();
370376
}, self));
371377

372-
if (self.options.addOnBlur && self.options.freeInput) {
373-
self.$input.on('focusout', $.proxy(function(event) {
374-
// HACK: only process on focusout when no typeahead opened, to
375-
// avoid adding the typeahead text as tag
376-
if ($('.typeahead, .twitter-typeahead', self.$container).length === 0) {
377-
self.add(self.$input.val());
378-
self.$input.val('');
379-
}
380-
}, self));
381-
}
378+
if (self.options.addOnBlur && self.options.freeInput) {
379+
self.$input.on('focusout', $.proxy(function(event) {
380+
// HACK: only process on focusout when no typeahead opened, to
381+
// avoid adding the typeahead text as tag
382+
if ($('.typeahead, .twitter-typeahead', self.$container).length === 0) {
383+
self.add(self.$input.val());
384+
self.$input.val('');
385+
}
386+
}, self));
387+
}
382388

383389
// Toggle the 'focus' css class on the container when it has focus
384390
self.$container.on({
@@ -459,18 +465,41 @@
459465

460466
var text = $input.val(),
461467
maxLengthReached = self.options.maxChars && text.length >= self.options.maxChars;
468+
462469
if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) {
463470
// Only attempt to add a tag if there is data in the field
471+
//console.log("TRUE===self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)===FALSE");
472+
464473
if (text.length !== 0) {
465-
self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text);
466-
$input.val('');
474+
console.log("text.length !== 0"+text);
475+
//self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text); ////TRP 12/24/15
476+
477+
var item2 = self.$input.val();
478+
if (self.objectItems) {
479+
var beforeFreeInputItemAdd = $.Event('beforeFreeInputItemAdd', { item: item2, cancel: true });
480+
self.$element.trigger(beforeFreeInputItemAdd);
481+
if (beforeFreeInputItemAdd.cancel)
482+
return;
483+
484+
console.log('beforeFreeInputItemAdd.item');
485+
console.log(beforeFreeInputItemAdd.item);
486+
item2 = beforeFreeInputItemAdd.item;
487+
}
488+
489+
self.add(item2);
490+
self.$input.val('');
491+
// $input.val(''); //TRP 12/24/15
467492
}
468493

469494
// If the field is empty, let the event triggered fire as usual
470495
if (self.options.cancelConfirmKeysOnEmpty === false) {
496+
//console.log("self.options.cancelConfirmKeysOnEmpty === false");
471497
event.preventDefault();
472498
}
473499
}
500+
else{
501+
//console.log("FALSE===self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)");
502+
}
474503

475504
// Reset internal input's size
476505
var textLength = $input.val().length,
@@ -642,10 +671,15 @@
642671
* [13, {which: 188, shiftKey: true}]
643672
*/
644673
function keyCombinationInList(keyPressEvent, lookupList) {
674+
console.log('keyPressEvent');
675+
console.log(keyPressEvent);
676+
console.log('lookupList');
677+
console.log(lookupList);
645678
var found = false;
646679
$.each(lookupList, function (index, keyCombination) {
647680
if (typeof (keyCombination) === 'number' && keyPressEvent.which === keyCombination) {
648681
found = true;
682+
//console.log('line 657 found: true, return false');
649683
return false;
650684
}
651685

@@ -655,11 +689,14 @@
655689
ctrl = !keyCombination.hasOwnProperty('ctrlKey') || keyPressEvent.ctrlKey === keyCombination.ctrlKey;
656690
if (alt && shift && ctrl) {
657691
found = true;
692+
//console.log('line 666 found: true, return false');
658693
return false;
659694
}
660695
}
661696
});
662697

698+
//console.log('found: ');
699+
//console.log(found);
663700
return found;
664701
}
665702

0 commit comments

Comments
 (0)