123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654 |
- /*
- * bootstrap-tagsinput v0.8.0
- *
- * For details, see the web site: https://github.com/bootstrap-tagsinput/bootstrap-tagsinput
- */
- (function ($) {
- "use strict";
- var defaultOptions = {
- tagClass: function (item) {
- return 'label label-info';
- },
- focusClass: 'focus',
- itemValue: function (item) {
- return item ? item.toString() : item;
- },
- itemText: function (item) {
- return this.itemValue(item);
- },
- itemTitle: function (item) {
- return null;
- },
- freeInput: true,
- addOnBlur: true,
- maxTags: undefined,
- maxChars: undefined,
- confirmKeys: [13, 44],
- delimiter: ',',
- inputMinWidth: 80,
- delimiterRegex: null,
- cancelConfirmKeysOnEmpty: false,
- onTagExists: function (item, $tag) {
- $tag.hide().fadeIn();
- },
- trimValue: false,
- allowDuplicates: false,
- triggerChange: true
- };
- /**
- * Constructor function
- */
- function TagsInput(element, options) {
- this.isInit = true;
- this.itemsArray = [];
- this.$element = $(element);
- this.$element.hide();
- this.isSelect = (element.tagName === 'SELECT');
- this.multiple = (this.isSelect && element.hasAttribute('multiple'));
- this.objectItems = options && options.itemValue;
- this.placeholderText = element.hasAttribute('placeholder') ? this.$element.attr('placeholder') : '';
- this.inputSize = Math.max(1, this.placeholderText.length);
- this.$container = $('<div class="bootstrap-tagsinput"></div>');
- this.$input = $('<input type="text" placeholder="' + this.placeholderText + '"/>').appendTo(this.$container);
- this.$element.before(this.$container);
- this.build(options);
- this.isInit = false;
- }
- TagsInput.prototype = {
- constructor: TagsInput,
- /**
- * Adds the given item as a new tag. Pass true to dontPushVal to prevent
- * updating the elements val()
- */
- add: function (item, dontPushVal, options) {
- var self = this;
- if (self.options.maxTags && self.itemsArray.length >= self.options.maxTags)
- return;
- // Ignore falsey values, except false
- if (item !== false && !item)
- return;
- // Trim value
- if (typeof item === "string" && self.options.trimValue) {
- item = $.trim(item);
- }
- // Throw an error when trying to add an object while the itemValue option was not set
- if (typeof item === "object" && !self.objectItems)
- throw("Can't add objects when itemValue option is not set");
- // Ignore strings only containg whitespace
- if (item.toString().match(/^\s*$/))
- return;
- // If SELECT but not multiple, remove current tag
- if (self.isSelect && !self.multiple && self.itemsArray.length > 0)
- self.remove(self.itemsArray[0]);
- if (typeof item === "string" && this.$element[0].tagName === 'INPUT') {
- var delimiter = (self.options.delimiterRegex) ? self.options.delimiterRegex : self.options.delimiter;
- var items = item.split(delimiter);
- if (items.length > 1) {
- for (var i = 0; i < items.length; i++) {
- this.add(items[i], true);
- }
- if (!dontPushVal)
- self.pushVal(self.options.triggerChange);
- return;
- }
- }
- var itemValue = self.options.itemValue(item),
- itemText = self.options.itemText(item),
- tagClass = self.options.tagClass(item),
- itemTitle = self.options.itemTitle(item);
- // Ignore items allready added
- var existing = $.grep(self.itemsArray, function (item) {
- return self.options.itemValue(item) === itemValue;
- })[0];
- if (existing && !self.options.allowDuplicates) {
- // Invoke onTagExists
- if (self.options.onTagExists) {
- var $existingTag = $(".tag", self.$container).filter(function () {
- return $(this).data("item") === existing;
- });
- self.options.onTagExists(item, $existingTag);
- }
- return;
- }
- // if length greater than limit
- if (self.items().toString().length + item.length + 1 > self.options.maxInputLength)
- return;
- // raise beforeItemAdd arg
- var beforeItemAddEvent = $.Event('beforeItemAdd', {item: item, cancel: false, options: options});
- self.$element.trigger(beforeItemAddEvent);
- if (beforeItemAddEvent.cancel)
- return;
- // register item in internal array and map
- self.itemsArray.push(item);
- // add a tag element
- var $tag = $('<span class="tag ' + htmlEncode(tagClass) + (itemTitle !== null ? ('" title="' + itemTitle) : '') + '">' + htmlEncode(itemText) + '<span data-role="remove"></span></span>');
- $tag.data('item', item);
- self.findInputWrapper().before($tag);
- $tag.after(' ');
- // Check to see if the tag exists in its raw or uri-encoded form
- var optionExists = (
- $('option[value="' + encodeURIComponent(itemValue) + '"]', self.$element).length ||
- $('option[value="' + htmlEncode(itemValue) + '"]', self.$element).length
- );
- // add <option /> if item represents a value not present in one of the <select />'s options
- if (self.isSelect && !optionExists) {
- var $option = $('<option selected>' + htmlEncode(itemText) + '</option>');
- $option.data('item', item);
- $option.attr('value', itemValue);
- self.$element.append($option);
- }
- if (!dontPushVal)
- self.pushVal(self.options.triggerChange);
- // Add class when reached maxTags
- if (self.options.maxTags === self.itemsArray.length || self.items().toString().length === self.options.maxInputLength)
- self.$container.addClass('bootstrap-tagsinput-max');
- // If using autocomplete, once the tag has been added, clear the autocomplete value so it does not stick around in the input.
- if (self.options.autocomplete) {
- // self.$input.autocomplete('clear');
- }
- if (this.isInit) {
- self.$element.trigger($.Event('itemAddedOnInit', {item: item, options: options}));
- } else {
- self.$element.trigger($.Event('itemAdded', {item: item, options: options}));
- }
- },
- /**
- * Removes the given item. Pass true to dontPushVal to prevent updating the
- * elements val()
- */
- remove: function (item, dontPushVal, options) {
- var self = this;
- if (self.objectItems) {
- if (typeof item === "object")
- item = $.grep(self.itemsArray, function (other) {
- return self.options.itemValue(other) == self.options.itemValue(item);
- });
- else
- item = $.grep(self.itemsArray, function (other) {
- return self.options.itemValue(other) == item;
- });
- item = item[item.length - 1];
- }
- if (item) {
- var beforeItemRemoveEvent = $.Event('beforeItemRemove', {item: item, cancel: false, options: options});
- self.$element.trigger(beforeItemRemoveEvent);
- if (beforeItemRemoveEvent.cancel)
- return;
- $('.tag', self.$container).filter(function () {
- return $(this).data('item') === item;
- }).remove();
- $('option', self.$element).filter(function () {
- return $(this).data('item') === item;
- }).remove();
- if ($.inArray(item, self.itemsArray) !== -1)
- self.itemsArray.splice($.inArray(item, self.itemsArray), 1);
- }
- if (!dontPushVal)
- self.pushVal(self.options.triggerChange);
- // Remove class when reached maxTags
- if (self.options.maxTags > self.itemsArray.length)
- self.$container.removeClass('bootstrap-tagsinput-max');
- self.$element.trigger($.Event('itemRemoved', {item: item, options: options}));
- },
- /**
- * Removes all items
- */
- removeAll: function () {
- var self = this;
- $('.tag', self.$container).remove();
- $('option', self.$element).remove();
- while (self.itemsArray.length > 0)
- self.itemsArray.pop();
- self.pushVal(self.options.triggerChange);
- },
- /**
- * Refreshes the tags so they match the text/value of their corresponding
- * item.
- */
- refresh: function () {
- var self = this;
- $('.tag', self.$container).each(function () {
- var $tag = $(this),
- item = $tag.data('item'),
- itemValue = self.options.itemValue(item),
- itemText = self.options.itemText(item),
- tagClass = self.options.tagClass(item);
- // Update tag's class and inner text
- $tag.attr('class', null);
- $tag.addClass('tag ' + htmlEncode(tagClass));
- $tag.contents().filter(function () {
- return this.nodeType == 3;
- })[0].nodeValue = htmlEncode(itemText);
- if (self.isSelect) {
- var option = $('option', self.$element).filter(function () {
- return $(this).data('item') === item;
- });
- option.attr('value', itemValue);
- }
- });
- },
- /**
- * Returns the items added as tags
- */
- items: function () {
- return this.itemsArray;
- },
- /**
- * Assembly value by retrieving the value of each item, and set it on the
- * element.
- */
- pushVal: function () {
- var self = this,
- val = $.map(self.items(), function (item) {
- return self.options.itemValue(item).toString();
- });
- self.$element.val(val, true);
- if (self.options.triggerChange)
- self.$element.trigger('change');
- },
- /**
- * Initializes the tags input behaviour on the element
- */
- build: function (options) {
- var self = this;
- self.options = $.extend({}, defaultOptions, options);
- // When itemValue is set, freeInput should always be false
- if (self.objectItems)
- self.options.freeInput = false;
- makeOptionItemFunction(self.options, 'itemValue');
- makeOptionItemFunction(self.options, 'itemText');
- makeOptionFunction(self.options, 'tagClass');
- if (self.options.autocomplete) {
- var autocomplete = self.options.autocomplete || {};
- // makeOptionFunction(autocomplete, 'lookup');
- self.$input.autocomplete($.extend(true, {}, {
- type: 'post',
- params: function (q) {
- var params = {};
- params['name'] = self.$element.prop("name");
- params['tags'] = self.$element.val();
- return params;
- },
- onSelect: function (suggestion) {
- self.add(suggestion.value);
- self.$input.val('').width(self.options.inputMinWidth).focus();
- }
- }, autocomplete));
- }
- self.$container.on('click', $.proxy(function (event) {
- if (!self.$element.attr('disabled')) {
- self.$input.removeAttr('disabled');
- }
- self.$input.focus();
- if (self.options.autocomplete && self.$input.autocomplete().visible) {
- clearTimeout(self.$input.autocomplete().blurTimeoutId);
- }
- }, self));
- if (self.options.addOnBlur && self.options.freeInput) {
- self.$input.on('focusout', $.proxy(function (event) {
- // HACK: only process on focusout when no autocomplete opened, to
- // avoid adding the autocomplete text as tag
- if (!self.options.autocomplete) {
- self.add(self.$input.val());
- self.$input.val('').css("width", self.options.inputMinWidth);
- } else {
- var autocomplete = self.$input.autocomplete();
- setTimeout(function () {
- if (!autocomplete.visible && self.$input.val() !== '') {
- self.add(self.$input.val());
- self.$input.val('').css("width", self.options.inputMinWidth);
- }
- }, 210);
- }
- }, self));
- }
- // Toggle the 'focus' css class on the container when it has focus
- self.$container.on({
- focusin: function () {
- self.$container.addClass(self.options.focusClass);
- },
- focusout: function () {
- self.$container.removeClass(self.options.focusClass);
- },
- });
- self.$container.on('keydown', 'input', $.proxy(function (event) {
- var $input = $(event.target),
- $inputWrapper = self.findInputWrapper();
- if (self.$element.attr('disabled')) {
- self.$input.attr('disabled', 'disabled');
- return;
- }
- switch (event.which) {
- // BACKSPACE
- case 8:
- if (doGetCaretPosition($input[0]) === 0) {
- var prev = $inputWrapper.prev();
- if (prev.length) {
- self.remove(prev.data('item'));
- }
- }
- break;
- // DELETE
- case 46:
- if (doGetCaretPosition($input[0]) === 0) {
- var next = $inputWrapper.next();
- if (next.length) {
- self.remove(next.data('item'));
- }
- }
- break;
- // LEFT ARROW
- case 37:
- // Try to move the input before the previous tag
- var $prevTag = $inputWrapper.prev();
- if ($input.val().length === 0 && $prevTag[0]) {
- $prevTag.before($inputWrapper);
- $input.focus();
- }
- break;
- // RIGHT ARROW
- case 39:
- // Try to move the input after the next tag
- var $nextTag = $inputWrapper.next();
- if ($input.val().length === 0 && $nextTag[0]) {
- $nextTag.after($inputWrapper);
- $input.focus();
- }
- break;
- default:
- // ignore
- }
- $input.css('width', self.getInputTextWidth());
- }, self));
- self.$container.on('keypress', 'input', $.proxy(function (event) {
- var $input = $(event.target);
- if (self.$element.attr('disabled')) {
- self.$input.attr('disabled', 'disabled');
- return;
- }
- var text = $input.val(),
- maxLengthReached = self.options.maxChars && text.length >= self.options.maxChars;
- if (self.options.freeInput && (keyCombinationInList(event, self.options.confirmKeys) || maxLengthReached)) {
- // Only attempt to add a tag if there is data in the field
- if (text.length !== 0) {
- self.add(maxLengthReached ? text.substr(0, self.options.maxChars) : text);
- $input.val('').css("width", self.options.inputMinWidth);
- }
- // If the field is empty, let the event triggered fire as usual
- if (self.options.cancelConfirmKeysOnEmpty === false) {
- event.preventDefault();
- }
- }
- $input.css('width', self.getInputTextWidth());
- }, self));
- // Remove icon clicked
- self.$container.on('click', '[data-role=remove]', $.proxy(function (event) {
- if (self.$element.attr('disabled')) {
- return;
- }
- self.remove($(event.target).closest('.tag').data('item'));
- }, self));
- // Only add existing value as tags when using strings as tags
- if (self.options.itemValue === defaultOptions.itemValue) {
- if (self.$element[0].tagName === 'INPUT') {
- self.add(self.$element.val());
- } else {
- $('option', self.$element).each(function () {
- self.add($(this).attr('value'), true);
- });
- }
- }
- },
- /**
- * Removes all tagsinput behaviour and unregsiter all event handlers
- */
- destroy: function () {
- var self = this;
- // Unbind events
- self.$container.off('keypress', 'input');
- self.$container.off('click', '[role=remove]');
- self.$container.remove();
- self.$element.removeData('tagsinput');
- self.$element.show();
- },
- /**
- * Sets focus on the tagsinput
- */
- focus: function () {
- this.$input.focus();
- },
- /**
- * Returns the internal input element
- */
- input: function () {
- return this.$input;
- },
- /**
- * Returns the element which is wrapped around the internal input. This
- * is normally the $container, but typeahead.js moves the $input element.
- */
- findInputWrapper: function () {
- var elt = this.$input[0],
- container = this.$container[0];
- while (elt && elt.parentNode !== container)
- elt = elt.parentNode;
- return $(elt);
- },
- /**
- * Get input text width
- * @returns {number}
- */
- getInputTextWidth: function () {
- var $input = this.$input;
- var self = this;
- var msgspan = $input.next().length > 0 ? $input.next() : $("<span />").addClass("tagsinput-text").insertAfter($input);
- var textWidth = msgspan.text($input.val()).outerWidth();
- textWidth = Math.max(textWidth, self.options.inputMinWidth);
- textWidth = Math.min(textWidth, self.$container.innerWidth());
- return textWidth;
- }
- };
- /**
- * Register JQuery plugin
- */
- $.fn.tagsinput = function (arg1, arg2, arg3) {
- var results = [];
- this.each(function () {
- var tagsinput = $(this).data('tagsinput');
- // Initialize a new tags input
- if (!tagsinput) {
- tagsinput = new TagsInput(this, $.extend(true, {}, arg1, $(this).data('tagsinput-options') || {}));
- $(this).data('tagsinput', tagsinput);
- results.push(tagsinput);
- if (this.tagName === 'SELECT') {
- $('option', $(this)).attr('selected', 'selected');
- }
- // Init tags from $(this).val()
- $(this).val($(this).val());
- } else if (!arg1 && !arg2) {
- // tagsinput already exists
- // no function, trying to init
- results.push(tagsinput);
- } else if (tagsinput[arg1] !== undefined) {
- // Invoke function on existing tags input
- if (tagsinput[arg1].length === 3 && arg3 !== undefined) {
- var retVal = tagsinput[arg1](arg2, null, arg3);
- } else {
- var retVal = tagsinput[arg1](arg2);
- }
- if (retVal !== undefined)
- results.push(retVal);
- }
- });
- return results.length > 1 ? results : results[0];
- };
- $.fn.tagsinput.Constructor = TagsInput;
- /**
- * Most options support both a string or number as well as a function as
- * option value. This function makes sure that the option with the given
- * key in the given options is wrapped in a function
- */
- function makeOptionItemFunction(options, key) {
- if (typeof options[key] !== 'function') {
- var propertyName = options[key];
- options[key] = function (item) {
- return item[propertyName];
- };
- }
- }
- function makeOptionFunction(options, key) {
- if (typeof options[key] !== 'function') {
- var value = options[key];
- options[key] = function () {
- return value;
- };
- }
- }
- /**
- * HtmlEncodes the given value
- */
- var htmlEncodeContainer = $('<div />');
- function htmlEncode(value) {
- if (value) {
- return htmlEncodeContainer.text(value).html();
- } else {
- return '';
- }
- }
- /**
- * Returns the position of the caret in the given input field
- * http://flightschool.acylt.com/devnotes/caret-position-woes/
- */
- function doGetCaretPosition(oField) {
- var iCaretPos = 0;
- if (document.selection) {
- oField.focus();
- var oSel = document.selection.createRange();
- oSel.moveStart('character', -oField.value.length);
- iCaretPos = oSel.text.length;
- } else if (oField.selectionStart || oField.selectionStart == '0') {
- iCaretPos = oField.selectionStart;
- }
- return (iCaretPos);
- }
- /**
- * Returns boolean indicates whether user has pressed an expected key combination.
- * @param object keyPressEvent: JavaScript event object, refer
- * http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
- * @param object lookupList: expected key combinations, as in:
- * [13, {which: 188, shiftKey: true}]
- */
- function keyCombinationInList(keyPressEvent, lookupList) {
- var found = false;
- $.each(lookupList, function (index, keyCombination) {
- if (typeof (keyCombination) === 'number' && keyPressEvent.which === keyCombination) {
- found = true;
- return false;
- }
- if (keyPressEvent.which === keyCombination.which) {
- var alt = !keyCombination.hasOwnProperty('altKey') || keyPressEvent.altKey === keyCombination.altKey,
- shift = !keyCombination.hasOwnProperty('shiftKey') || keyPressEvent.shiftKey === keyCombination.shiftKey,
- ctrl = !keyCombination.hasOwnProperty('ctrlKey') || keyPressEvent.ctrlKey === keyCombination.ctrlKey;
- if (alt && shift && ctrl) {
- found = true;
- return false;
- }
- }
- });
- return found;
- }
- })(window.jQuery);
|