bootstrap-tokenfield.js 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. /*!
  2. * bootstrap-tokenfield
  3. * https://github.com/sliptree/bootstrap-tokenfield
  4. * Copyright 2013-2014 Sliptree and other contributors; Licensed MIT
  5. */
  6. (function (factory) {
  7. if (typeof define === 'function' && define.amd) {
  8. // AMD. Register as an anonymous module.
  9. define(['jquery'], factory);
  10. } else if (typeof exports === 'object') {
  11. // For CommonJS and CommonJS-like environments where a window with jQuery
  12. // is present, execute the factory with the jQuery instance from the window object
  13. // For environments that do not inherently posses a window with a document
  14. // (such as Node.js), expose a Tokenfield-making factory as module.exports
  15. // This accentuates the need for the creation of a real window or passing in a jQuery instance
  16. // e.g. require("bootstrap-tokenfield")(window); or require("bootstrap-tokenfield")($);
  17. module.exports = global.window && global.window.$ ?
  18. factory( global.window.$ ) :
  19. function( input ) {
  20. if ( !input.$ && !input.fn ) {
  21. throw new Error( "Tokenfield requires a window object with jQuery or a jQuery instance" );
  22. }
  23. return factory( input.$ || input );
  24. };
  25. } else {
  26. // Browser globals
  27. factory(jQuery);
  28. }
  29. }(function ($, window) {
  30. "use strict"; // jshint ;_;
  31. /* TOKENFIELD PUBLIC CLASS DEFINITION
  32. * ============================== */
  33. var Tokenfield = function (element, options) {
  34. var _self = this
  35. this.$element = $(element)
  36. this.textDirection = this.$element.css('direction');
  37. // Extend options
  38. this.options = $.extend(true, {}, $.fn.tokenfield.defaults, { tokens: this.$element.val() }, this.$element.data(), options)
  39. // Setup delimiters and trigger keys
  40. this._delimiters = (typeof this.options.delimiter === 'string') ? [this.options.delimiter] : this.options.delimiter
  41. this._triggerKeys = $.map(this._delimiters, function (delimiter) {
  42. return delimiter.charCodeAt(0);
  43. });
  44. this._firstDelimiter = this._delimiters[0];
  45. // Check for whitespace, dash and special characters
  46. var whitespace = $.inArray(' ', this._delimiters)
  47. , dash = $.inArray('-', this._delimiters)
  48. if (whitespace >= 0)
  49. this._delimiters[whitespace] = '\\s'
  50. if (dash >= 0) {
  51. delete this._delimiters[dash]
  52. this._delimiters.unshift('-')
  53. }
  54. var specialCharacters = ['\\', '$', '[', '{', '^', '.', '|', '?', '*', '+', '(', ')']
  55. $.each(this._delimiters, function (index, char) {
  56. var pos = $.inArray(char, specialCharacters)
  57. if (pos >= 0) _self._delimiters[index] = '\\' + char;
  58. });
  59. // Store original input width
  60. var elRules = (window && typeof window.getMatchedCSSRules === 'function') ? window.getMatchedCSSRules( element ) : null
  61. , elStyleWidth = element.style.width
  62. , elCSSWidth
  63. , elWidth = this.$element.width()
  64. if (elRules) {
  65. $.each( elRules, function (i, rule) {
  66. if (rule.style.width) {
  67. elCSSWidth = rule.style.width;
  68. }
  69. });
  70. }
  71. // Move original input out of the way
  72. var hidingPosition = $('body').css('direction') === 'rtl' ? 'right' : 'left',
  73. originalStyles = { position: this.$element.css('position') };
  74. originalStyles[hidingPosition] = this.$element.css(hidingPosition);
  75. this.$element
  76. .data('original-styles', originalStyles)
  77. .data('original-tabindex', this.$element.prop('tabindex'))
  78. .css('position', 'absolute')
  79. .css(hidingPosition, '-10000px')
  80. .prop('tabindex', -1)
  81. // Create a wrapper
  82. this.$wrapper = $('<div class="tokenfield form-control" />')
  83. if (this.$element.hasClass('input-lg')) this.$wrapper.addClass('input-lg')
  84. if (this.$element.hasClass('input-sm')) this.$wrapper.addClass('input-sm')
  85. if (this.textDirection === 'rtl') this.$wrapper.addClass('rtl')
  86. // Create a new input
  87. var id = this.$element.prop('id') || new Date().getTime() + '' + Math.floor((1 + Math.random()) * 100)
  88. this.$input = $('<input type="text" class="token-input" autocomplete="off" />')
  89. .appendTo( this.$wrapper )
  90. .prop( 'placeholder', this.$element.prop('placeholder') )
  91. .prop( 'id', id + '-tokenfield' )
  92. .prop( 'tabindex', this.$element.data('original-tabindex') )
  93. // Re-route original input label to new input
  94. var $label = $( 'label[for="' + this.$element.prop('id') + '"]' )
  95. if ( $label.length ) {
  96. $label.prop( 'for', this.$input.prop('id') )
  97. }
  98. // Set up a copy helper to handle copy & paste
  99. this.$copyHelper = $('<input type="text" />').css('position', 'absolute').css(hidingPosition, '-10000px').prop('tabindex', -1).prependTo( this.$wrapper )
  100. // Set wrapper width
  101. if (elStyleWidth) {
  102. this.$wrapper.css('width', elStyleWidth);
  103. }
  104. else if (elCSSWidth) {
  105. this.$wrapper.css('width', elCSSWidth);
  106. }
  107. // If input is inside inline-form with no width set, set fixed width
  108. else if (this.$element.parents('.form-inline').length) {
  109. this.$wrapper.width( elWidth )
  110. }
  111. // Set tokenfield disabled, if original or fieldset input is disabled
  112. if (this.$element.prop('disabled') || this.$element.parents('fieldset[disabled]').length) {
  113. this.disable();
  114. }
  115. // Set tokenfield readonly, if original input is readonly
  116. if (this.$element.prop('readonly')) {
  117. this.readonly();
  118. }
  119. // Set up mirror for input auto-sizing
  120. this.$mirror = $('<span style="position:absolute; top:-999px; left:0; white-space:pre;"/>');
  121. this.$input.css('min-width', this.options.minWidth + 'px')
  122. $.each([
  123. 'fontFamily',
  124. 'fontSize',
  125. 'fontWeight',
  126. 'fontStyle',
  127. 'letterSpacing',
  128. 'textTransform',
  129. 'wordSpacing',
  130. 'textIndent'
  131. ], function (i, val) {
  132. _self.$mirror[0].style[val] = _self.$input.css(val);
  133. });
  134. this.$mirror.appendTo( 'body' )
  135. // Insert tokenfield to HTML
  136. this.$wrapper.insertBefore( this.$element )
  137. this.$element.prependTo( this.$wrapper )
  138. // Calculate inner input width
  139. this.update()
  140. // Create initial tokens, if any
  141. this.setTokens(this.options.tokens, false, false)
  142. // Start listening to events
  143. this.listen()
  144. // Initialize autocomplete, if necessary
  145. if ( ! $.isEmptyObject( this.options.autocomplete ) ) {
  146. var side = this.textDirection === 'rtl' ? 'right' : 'left'
  147. , autocompleteOptions = $.extend({
  148. minLength: this.options.showAutocompleteOnFocus ? 0 : null,
  149. position: { my: side + " top", at: side + " bottom", of: this.$wrapper }
  150. }, this.options.autocomplete )
  151. this.$input.autocomplete( autocompleteOptions )
  152. }
  153. // Initialize typeahead, if necessary
  154. if ( ! $.isEmptyObject( this.options.typeahead ) ) {
  155. var typeaheadOptions = this.options.typeahead
  156. , defaults = {
  157. minLength: this.options.showAutocompleteOnFocus ? 0 : null
  158. }
  159. , args = $.isArray( typeaheadOptions ) ? typeaheadOptions : [typeaheadOptions, typeaheadOptions]
  160. args[0] = $.extend( {}, defaults, args[0] )
  161. this.$input.typeahead.apply( this.$input, args )
  162. this.typeahead = true
  163. }
  164. this.$element.trigger('tokenfield:initialize')
  165. }
  166. Tokenfield.prototype = {
  167. constructor: Tokenfield
  168. , createToken: function (attrs, triggerChange) {
  169. var _self = this
  170. if (typeof attrs === 'string') {
  171. attrs = { value: attrs, label: attrs }
  172. }
  173. if (typeof triggerChange === 'undefined') {
  174. triggerChange = true
  175. }
  176. // Normalize label and value
  177. attrs.value = $.trim(attrs.value);
  178. attrs.label = attrs.label && attrs.label.length ? $.trim(attrs.label) : attrs.value
  179. // Bail out if has no value or label, or label is too short
  180. if (!attrs.value.length || !attrs.label.length || attrs.label.length <= this.options.minLength) return
  181. // Bail out if maximum number of tokens is reached
  182. if (this.options.limit && this.getTokens().length >= this.options.limit) return
  183. // Allow changing token data before creating it
  184. var createEvent = $.Event('tokenfield:createtoken', { attrs: attrs })
  185. this.$element.trigger(createEvent)
  186. // Bail out if there if attributes are empty or event was defaultPrevented
  187. if (!createEvent.attrs || createEvent.isDefaultPrevented()) return
  188. var $token = $('<div class="token" />')
  189. .attr('data-value', attrs.value)
  190. .append('<span class="token-label" />')
  191. .append('<a href="#" class="close" tabindex="-1">&times;</a>')
  192. // Insert token into HTML
  193. if (this.$input.hasClass('tt-input')) {
  194. // If the input has typeahead enabled, insert token before it's parent
  195. this.$input.parent().before( $token )
  196. } else {
  197. this.$input.before( $token )
  198. }
  199. // Temporarily set input width to minimum
  200. this.$input.css('width', this.options.minWidth + 'px')
  201. var $tokenLabel = $token.find('.token-label')
  202. , $closeButton = $token.find('.close')
  203. // Determine maximum possible token label width
  204. if (!this.maxTokenWidth) {
  205. this.maxTokenWidth =
  206. this.$wrapper.width() - $closeButton.outerWidth() -
  207. parseInt($closeButton.css('margin-left'), 10) -
  208. parseInt($closeButton.css('margin-right'), 10) -
  209. parseInt($token.css('border-left-width'), 10) -
  210. parseInt($token.css('border-right-width'), 10) -
  211. parseInt($token.css('padding-left'), 10) -
  212. parseInt($token.css('padding-right'), 10)
  213. parseInt($tokenLabel.css('border-left-width'), 10) -
  214. parseInt($tokenLabel.css('border-right-width'), 10) -
  215. parseInt($tokenLabel.css('padding-left'), 10) -
  216. parseInt($tokenLabel.css('padding-right'), 10)
  217. parseInt($tokenLabel.css('margin-left'), 10) -
  218. parseInt($tokenLabel.css('margin-right'), 10)
  219. }
  220. $tokenLabel
  221. .text(attrs.label)
  222. .css('max-width', this.maxTokenWidth)
  223. // Listen to events on token
  224. $token
  225. .on('mousedown', function (e) {
  226. if (_self._disabled || _self._readonly) return false
  227. _self.preventDeactivation = true
  228. })
  229. .on('click', function (e) {
  230. if (_self._disabled || _self._readonly) return false
  231. _self.preventDeactivation = false
  232. if (e.ctrlKey || e.metaKey) {
  233. e.preventDefault()
  234. return _self.toggle( $token )
  235. }
  236. _self.activate( $token, e.shiftKey, e.shiftKey )
  237. })
  238. .on('dblclick', function (e) {
  239. if (_self._disabled || _self._readonly || !_self.options.allowEditing ) return false
  240. _self.edit( $token )
  241. })
  242. $closeButton
  243. .on('click', $.proxy(this.remove, this))
  244. // Trigger createdtoken event on the original field
  245. // indicating that the token is now in the DOM
  246. this.$element.trigger($.Event('tokenfield:createdtoken', {
  247. attrs: attrs,
  248. relatedTarget: $token.get(0)
  249. }))
  250. // Trigger change event on the original field
  251. if (triggerChange) {
  252. this.$element.val( this.getTokensList() ).trigger( $.Event('change', { initiator: 'tokenfield' }) )
  253. }
  254. // Update tokenfield dimensions
  255. this.update()
  256. // Return original element
  257. return this.$element.get(0)
  258. }
  259. , setTokens: function (tokens, add, triggerChange) {
  260. if (!tokens) return
  261. if (!add) this.$wrapper.find('.token').remove()
  262. if (typeof triggerChange === 'undefined') {
  263. triggerChange = true
  264. }
  265. if (typeof tokens === 'string') {
  266. if (this._delimiters.length) {
  267. // Split based on delimiters
  268. tokens = tokens.split( new RegExp( '[' + this._delimiters.join('') + ']' ) )
  269. } else {
  270. tokens = [tokens];
  271. }
  272. }
  273. var _self = this
  274. $.each(tokens, function (i, attrs) {
  275. _self.createToken(attrs, triggerChange)
  276. })
  277. return this.$element.get(0)
  278. }
  279. , getTokenData: function($token) {
  280. var data = $token.map(function() {
  281. var $token = $(this);
  282. return {
  283. value: $token.attr('data-value'),
  284. label: $token.find('.token-label').text()
  285. }
  286. }).get();
  287. if (data.length == 1) {
  288. data = data[0];
  289. }
  290. return data;
  291. }
  292. , getTokens: function(active) {
  293. var self = this
  294. , tokens = []
  295. , activeClass = active ? '.active' : '' // get active tokens only
  296. this.$wrapper.find( '.token' + activeClass ).each( function() {
  297. tokens.push( self.getTokenData( $(this) ) )
  298. })
  299. return tokens
  300. }
  301. , getTokensList: function(delimiter, beautify, active) {
  302. delimiter = delimiter || this._firstDelimiter
  303. beautify = ( typeof beautify !== 'undefined' && beautify !== null ) ? beautify : this.options.beautify
  304. var separator = delimiter + ( beautify && delimiter !== ' ' ? ' ' : '')
  305. return $.map( this.getTokens(active), function (token) {
  306. return token.value
  307. }).join(separator)
  308. }
  309. , getInput: function() {
  310. return this.$input.val()
  311. }
  312. , listen: function () {
  313. var _self = this
  314. this.$element
  315. .on('change', $.proxy(this.change, this))
  316. this.$wrapper
  317. .on('mousedown',$.proxy(this.focusInput, this))
  318. this.$input
  319. .on('focus', $.proxy(this.focus, this))
  320. .on('blur', $.proxy(this.blur, this))
  321. .on('paste', $.proxy(this.paste, this))
  322. .on('keydown', $.proxy(this.keydown, this))
  323. .on('keypress', $.proxy(this.keypress, this))
  324. .on('keyup', $.proxy(this.keyup, this))
  325. this.$copyHelper
  326. .on('focus', $.proxy(this.focus, this))
  327. .on('blur', $.proxy(this.blur, this))
  328. .on('keydown', $.proxy(this.keydown, this))
  329. .on('keyup', $.proxy(this.keyup, this))
  330. // Secondary listeners for input width calculation
  331. this.$input
  332. .on('keypress', $.proxy(this.update, this))
  333. .on('keyup', $.proxy(this.update, this))
  334. this.$input
  335. .on('autocompletecreate', function() {
  336. // Set minimum autocomplete menu width
  337. var $_menuElement = $(this).data('ui-autocomplete').menu.element
  338. var minWidth = _self.$wrapper.outerWidth() -
  339. parseInt( $_menuElement.css('border-left-width'), 10 ) -
  340. parseInt( $_menuElement.css('border-right-width'), 10 )
  341. $_menuElement.css( 'min-width', minWidth + 'px' )
  342. })
  343. .on('autocompleteselect', function (e, ui) {
  344. if (_self.createToken( ui.item )) {
  345. _self.$input.val('')
  346. if (_self.$input.data( 'edit' )) {
  347. _self.unedit(true)
  348. }
  349. }
  350. return false
  351. })
  352. .on('typeahead:selected typeahead:autocompleted', function (e, datum, dataset) {
  353. // Create token
  354. if (_self.createToken( datum )) {
  355. _self.$input.typeahead('val', '')
  356. if (_self.$input.data( 'edit' )) {
  357. _self.unedit(true)
  358. }
  359. }
  360. })
  361. // Listen to window resize
  362. $(window).on('resize', $.proxy(this.update, this ))
  363. }
  364. , keydown: function (e) {
  365. if (!this.focused) return
  366. var _self = this
  367. switch(e.keyCode) {
  368. case 8: // backspace
  369. if (!this.$input.is(document.activeElement)) break
  370. this.lastInputValue = this.$input.val()
  371. break
  372. case 37: // left arrow
  373. leftRight( this.textDirection === 'rtl' ? 'next': 'prev' )
  374. break
  375. case 38: // up arrow
  376. upDown('prev')
  377. break
  378. case 39: // right arrow
  379. leftRight( this.textDirection === 'rtl' ? 'prev': 'next' )
  380. break
  381. case 40: // down arrow
  382. upDown('next')
  383. break
  384. case 65: // a (to handle ctrl + a)
  385. if (this.$input.val().length > 0 || !(e.ctrlKey || e.metaKey)) break
  386. this.activateAll()
  387. e.preventDefault()
  388. break
  389. case 9: // tab
  390. case 13: // enter
  391. // We will handle creating tokens from autocomplete in autocomplete events
  392. if (this.$input.data('ui-autocomplete') && this.$input.data('ui-autocomplete').menu.element.find("li:has(a.ui-state-focus)").length) break
  393. // We will handle creating tokens from typeahead in typeahead events
  394. if (this.$input.hasClass('tt-input') && this.$wrapper.find('.tt-cursor').length ) break
  395. if (this.$input.hasClass('tt-input') && this.$wrapper.find('.tt-hint').val().length) break
  396. // Create token
  397. if (this.$input.is(document.activeElement) && this.$input.val().length || this.$input.data('edit')) {
  398. return this.createTokensFromInput(e, this.$input.data('edit'));
  399. }
  400. // Edit token
  401. if (e.keyCode === 13) {
  402. if (!this.$copyHelper.is(document.activeElement) || this.$wrapper.find('.token.active').length !== 1) break
  403. if (!_self.options.allowEditing) break
  404. this.edit( this.$wrapper.find('.token.active') )
  405. }
  406. }
  407. function leftRight(direction) {
  408. if (_self.$input.is(document.activeElement)) {
  409. if (_self.$input.val().length > 0) return
  410. direction += 'All'
  411. var $token = _self.$input.hasClass('tt-input') ? _self.$input.parent()[direction]('.token:first') : _self.$input[direction]('.token:first')
  412. if (!$token.length) return
  413. _self.preventInputFocus = true
  414. _self.preventDeactivation = true
  415. _self.activate( $token )
  416. e.preventDefault()
  417. } else {
  418. _self[direction]( e.shiftKey )
  419. e.preventDefault()
  420. }
  421. }
  422. function upDown(direction) {
  423. if (!e.shiftKey) return
  424. if (_self.$input.is(document.activeElement)) {
  425. if (_self.$input.val().length > 0) return
  426. var $token = _self.$input.hasClass('tt-input') ? _self.$input.parent()[direction + 'All']('.token:first') : _self.$input[direction + 'All']('.token:first')
  427. if (!$token.length) return
  428. _self.activate( $token )
  429. }
  430. var opposite = direction === 'prev' ? 'next' : 'prev'
  431. , position = direction === 'prev' ? 'first' : 'last'
  432. _self.firstActiveToken[opposite + 'All']('.token').each(function() {
  433. _self.deactivate( $(this) )
  434. })
  435. _self.activate( _self.$wrapper.find('.token:' + position), true, true )
  436. e.preventDefault()
  437. }
  438. this.lastKeyDown = e.keyCode
  439. }
  440. , keypress: function(e) {
  441. this.lastKeyPressCode = e.keyCode
  442. this.lastKeyPressCharCode = e.charCode
  443. // Comma
  444. if ($.inArray( e.charCode, this._triggerKeys) !== -1 && this.$input.is(document.activeElement)) {
  445. if (this.$input.val()) {
  446. this.createTokensFromInput(e)
  447. }
  448. return false;
  449. }
  450. }
  451. , keyup: function (e) {
  452. this.preventInputFocus = false
  453. if (!this.focused) return
  454. switch(e.keyCode) {
  455. case 8: // backspace
  456. if (this.$input.is(document.activeElement)) {
  457. if (this.$input.val().length || this.lastInputValue.length && this.lastKeyDown === 8) break
  458. this.preventDeactivation = true
  459. var $prevToken = this.$input.hasClass('tt-input') ? this.$input.parent().prevAll('.token:first') : this.$input.prevAll('.token:first')
  460. if (!$prevToken.length) break
  461. this.activate( $prevToken )
  462. } else {
  463. this.remove(e)
  464. }
  465. break
  466. case 46: // delete
  467. this.remove(e, 'next')
  468. break
  469. }
  470. this.lastKeyUp = e.keyCode
  471. }
  472. , focus: function (e) {
  473. this.focused = true
  474. this.$wrapper.addClass('focus')
  475. if (this.$input.is(document.activeElement)) {
  476. this.$wrapper.find('.active').removeClass('active')
  477. this.$firstActiveToken = null
  478. if (this.options.showAutocompleteOnFocus) {
  479. this.search()
  480. }
  481. }
  482. }
  483. , blur: function (e) {
  484. this.focused = false
  485. this.$wrapper.removeClass('focus')
  486. if (!this.preventDeactivation && !this.$element.is(document.activeElement)) {
  487. this.$wrapper.find('.active').removeClass('active')
  488. this.$firstActiveToken = null
  489. }
  490. if (!this.preventCreateTokens && (this.$input.data('edit') && !this.$input.is(document.activeElement) || this.options.createTokensOnBlur )) {
  491. this.createTokensFromInput(e)
  492. }
  493. this.preventDeactivation = false
  494. this.preventCreateTokens = false
  495. }
  496. , paste: function (e) {
  497. var _self = this
  498. // Add tokens to existing ones
  499. setTimeout(function () {
  500. _self.createTokensFromInput(e)
  501. }, 1)
  502. }
  503. , change: function (e) {
  504. if ( e.initiator === 'tokenfield' ) return // Prevent loops
  505. this.setTokens( this.$element.val() )
  506. }
  507. , createTokensFromInput: function (e, focus) {
  508. if (this.$input.val().length < this.options.minLength)
  509. return // No input, simply return
  510. var tokensBefore = this.getTokensList()
  511. this.setTokens( this.$input.val(), true )
  512. if (tokensBefore == this.getTokensList() && this.$input.val().length)
  513. return false // No tokens were added, do nothing (prevent form submit)
  514. if (this.$input.hasClass('tt-input')) {
  515. // Typeahead acts weird when simply setting input value to empty,
  516. // so we set the query to empty instead
  517. this.$input.typeahead('val', '')
  518. } else {
  519. this.$input.val('')
  520. }
  521. if (this.$input.data( 'edit' )) {
  522. this.unedit(focus)
  523. }
  524. return false // Prevent form being submitted
  525. }
  526. , next: function (add) {
  527. if (add) {
  528. var $firstActiveToken = this.$wrapper.find('.active:first')
  529. , deactivate = $firstActiveToken && this.$firstActiveToken ? $firstActiveToken.index() < this.$firstActiveToken.index() : false
  530. if (deactivate) return this.deactivate( $firstActiveToken )
  531. }
  532. var $lastActiveToken = this.$wrapper.find('.active:last')
  533. , $nextToken = $lastActiveToken.nextAll('.token:first')
  534. if (!$nextToken.length) {
  535. this.$input.focus()
  536. return
  537. }
  538. this.activate($nextToken, add)
  539. }
  540. , prev: function (add) {
  541. if (add) {
  542. var $lastActiveToken = this.$wrapper.find('.active:last')
  543. , deactivate = $lastActiveToken && this.$firstActiveToken ? $lastActiveToken.index() > this.$firstActiveToken.index() : false
  544. if (deactivate) return this.deactivate( $lastActiveToken )
  545. }
  546. var $firstActiveToken = this.$wrapper.find('.active:first')
  547. , $prevToken = $firstActiveToken.prevAll('.token:first')
  548. if (!$prevToken.length) {
  549. $prevToken = this.$wrapper.find('.token:first')
  550. }
  551. if (!$prevToken.length && !add) {
  552. this.$input.focus()
  553. return
  554. }
  555. this.activate( $prevToken, add )
  556. }
  557. , activate: function ($token, add, multi, remember) {
  558. if (!$token) return
  559. if (typeof remember === 'undefined') var remember = true
  560. if (multi) var add = true
  561. this.$copyHelper.focus()
  562. if (!add) {
  563. this.$wrapper.find('.active').removeClass('active')
  564. if (remember) {
  565. this.$firstActiveToken = $token
  566. } else {
  567. delete this.$firstActiveToken
  568. }
  569. }
  570. if (multi && this.$firstActiveToken) {
  571. // Determine first active token and the current tokens indicies
  572. // Account for the 1 hidden textarea by subtracting 1 from both
  573. var i = this.$firstActiveToken.index() - 2
  574. , a = $token.index() - 2
  575. , _self = this
  576. this.$wrapper.find('.token').slice( Math.min(i, a) + 1, Math.max(i, a) ).each( function() {
  577. _self.activate( $(this), true )
  578. })
  579. }
  580. $token.addClass('active')
  581. this.$copyHelper.val( this.getTokensList( null, null, true ) ).select()
  582. }
  583. , activateAll: function() {
  584. var _self = this
  585. this.$wrapper.find('.token').each( function (i) {
  586. _self.activate($(this), i !== 0, false, false)
  587. })
  588. }
  589. , deactivate: function($token) {
  590. if (!$token) return
  591. $token.removeClass('active')
  592. this.$copyHelper.val( this.getTokensList( null, null, true ) ).select()
  593. }
  594. , toggle: function($token) {
  595. if (!$token) return
  596. $token.toggleClass('active')
  597. this.$copyHelper.val( this.getTokensList( null, null, true ) ).select()
  598. }
  599. , edit: function ($token) {
  600. if (!$token) return
  601. var attrs = {
  602. value: $token.data('value'),
  603. label: $token.find('.token-label').text()
  604. }
  605. // Allow changing input value before editing
  606. var options = { attrs: attrs, relatedTarget: $token.get(0) }
  607. var editEvent = $.Event('tokenfield:edittoken', options)
  608. this.$element.trigger( editEvent )
  609. // Edit event can be cancelled if default is prevented
  610. if (editEvent.isDefaultPrevented()) return
  611. $token.find('.token-label').text(attrs.value)
  612. var tokenWidth = $token.outerWidth()
  613. var $_input = this.$input.hasClass('tt-input') ? this.$input.parent() : this.$input
  614. $token.replaceWith( $_input )
  615. this.preventCreateTokens = true
  616. this.$input.val( attrs.value )
  617. .select()
  618. .data( 'edit', true )
  619. .width( tokenWidth )
  620. this.update();
  621. // Indicate that token in snow being edited, and is replaced with an input field in the DOM
  622. this.$element.trigger($.Event('tokenfield:editedtoken', options ))
  623. }
  624. , unedit: function (focus) {
  625. var $_input = this.$input.hasClass('tt-input') ? this.$input.parent() : this.$input
  626. $_input.appendTo( this.$wrapper )
  627. this.$input.data('edit', false)
  628. this.$mirror.text('')
  629. this.update()
  630. // Because moving the input element around in DOM
  631. // will cause it to lose focus, we provide an option
  632. // to re-focus the input after appending it to the wrapper
  633. if (focus) {
  634. var _self = this
  635. setTimeout(function () {
  636. _self.$input.focus()
  637. }, 1)
  638. }
  639. }
  640. , remove: function (e, direction) {
  641. if (this.$input.is(document.activeElement) || this._disabled || this._readonly) return
  642. var $token = (e.type === 'click') ? $(e.target).closest('.token') : this.$wrapper.find('.token.active')
  643. if (e.type !== 'click') {
  644. if (!direction) var direction = 'prev'
  645. this[direction]()
  646. // Was it the first token?
  647. if (direction === 'prev') var firstToken = $token.first().prevAll('.token:first').length === 0
  648. }
  649. // Prepare events and their options
  650. var options = { attrs: this.getTokenData( $token ), relatedTarget: $token.get(0) }
  651. , removeEvent = $.Event('tokenfield:removetoken', options)
  652. this.$element.trigger(removeEvent);
  653. // Remove event can be intercepted and cancelled
  654. if (removeEvent.isDefaultPrevented()) return
  655. var removedEvent = $.Event('tokenfield:removedtoken', options)
  656. , changeEvent = $.Event('change', { initiator: 'tokenfield' })
  657. // Remove token from DOM
  658. $token.remove()
  659. // Trigger events
  660. this.$element.val( this.getTokensList() ).trigger( removedEvent ).trigger( changeEvent )
  661. // Focus, when necessary:
  662. // When there are no more tokens, or if this was the first token
  663. // and it was removed with backspace or it was clicked on
  664. if (!this.$wrapper.find('.token').length || e.type === 'click' || firstToken) this.$input.focus()
  665. // Adjust input width
  666. this.$input.css('width', this.options.minWidth + 'px')
  667. this.update()
  668. // Cancel original event handlers
  669. e.preventDefault()
  670. e.stopPropagation()
  671. }
  672. /**
  673. * Update tokenfield dimensions
  674. */
  675. , update: function (e) {
  676. var value = this.$input.val()
  677. , inputPaddingLeft = parseInt(this.$input.css('padding-left'), 10)
  678. , inputPaddingRight = parseInt(this.$input.css('padding-right'), 10)
  679. , inputPadding = inputPaddingLeft + inputPaddingRight
  680. if (this.$input.data('edit')) {
  681. if (!value) {
  682. value = this.$input.prop("placeholder")
  683. }
  684. if (value === this.$mirror.text()) return
  685. this.$mirror.text(value)
  686. var mirrorWidth = this.$mirror.width() + 10;
  687. if ( mirrorWidth > this.$wrapper.width() ) {
  688. return this.$input.width( this.$wrapper.width() )
  689. }
  690. this.$input.width( mirrorWidth )
  691. }
  692. else {
  693. this.$input.css( 'width', this.options.minWidth + 'px' )
  694. if (this.textDirection === 'rtl') {
  695. return this.$input.width( this.$input.offset().left + this.$input.outerWidth() - this.$wrapper.offset().left - parseInt(this.$wrapper.css('padding-left'), 10) - inputPadding - 1 )
  696. }
  697. this.$input.width( this.$wrapper.offset().left + this.$wrapper.width() + parseInt(this.$wrapper.css('padding-left'), 10) - this.$input.offset().left - inputPadding )
  698. }
  699. }
  700. , focusInput: function (e) {
  701. if ( $(e.target).closest('.token').length || $(e.target).closest('.token-input').length || $(e.target).closest('.tt-dropdown-menu').length ) return
  702. // Focus only after the current call stack has cleared,
  703. // otherwise has no effect.
  704. // Reason: mousedown is too early - input will lose focus
  705. // after mousedown. However, since the input may be moved
  706. // in DOM, there may be no click or mouseup event triggered.
  707. var _self = this
  708. setTimeout(function() {
  709. _self.$input.focus()
  710. }, 0)
  711. }
  712. , search: function () {
  713. if ( this.$input.data('ui-autocomplete') ) {
  714. this.$input.autocomplete('search')
  715. }
  716. }
  717. , disable: function () {
  718. this.setProperty('disabled', true);
  719. }
  720. , enable: function () {
  721. this.setProperty('disabled', false);
  722. }
  723. , readonly: function () {
  724. this.setProperty('readonly', true);
  725. }
  726. , writeable: function () {
  727. this.setProperty('readonly', false);
  728. }
  729. , setProperty: function(property, value) {
  730. this['_' + property] = value;
  731. this.$input.prop(property, value);
  732. this.$element.prop(property, value);
  733. this.$wrapper[ value ? 'addClass' : 'removeClass' ](property);
  734. }
  735. , destroy: function() {
  736. // Set field value
  737. this.$element.val( this.getTokensList() );
  738. // Restore styles and properties
  739. this.$element.css( this.$element.data('original-styles') );
  740. this.$element.prop( 'tabindex', this.$element.data('original-tabindex') );
  741. // Re-route tokenfield labele to original input
  742. var $label = $( 'label[for="' + this.$input.prop('id') + '"]' )
  743. if ( $label.length ) {
  744. $label.prop( 'for', this.$element.prop('id') )
  745. }
  746. // Move original element outside of tokenfield wrapper
  747. this.$element.insertBefore( this.$wrapper );
  748. // Remove tokenfield-related data
  749. this.$element.removeData('original-styles')
  750. .removeData('original-tabindex')
  751. .removeData('bs.tokenfield');
  752. // Remove tokenfield from DOM
  753. this.$wrapper.remove();
  754. var $_element = this.$element;
  755. delete this;
  756. return $_element;
  757. }
  758. }
  759. /* TOKENFIELD PLUGIN DEFINITION
  760. * ======================== */
  761. var old = $.fn.tokenfield
  762. $.fn.tokenfield = function (option, param) {
  763. var value
  764. , args = []
  765. Array.prototype.push.apply( args, arguments );
  766. var elements = this.each(function () {
  767. var $this = $(this)
  768. , data = $this.data('bs.tokenfield')
  769. , options = typeof option == 'object' && option
  770. if (typeof option === 'string' && data && data[option]) {
  771. args.shift()
  772. value = data[option].apply(data, args)
  773. } else {
  774. if (!data && typeof option !== 'string' && !param) $this.data('bs.tokenfield', (data = new Tokenfield(this, options)))
  775. }
  776. })
  777. return typeof value !== 'undefined' ? value : elements;
  778. }
  779. $.fn.tokenfield.defaults = {
  780. minWidth: 60,
  781. minLength: 0,
  782. allowEditing: true,
  783. limit: 0,
  784. autocomplete: {},
  785. typeahead: {},
  786. showAutocompleteOnFocus: false,
  787. createTokensOnBlur: false,
  788. delimiter: ',',
  789. beautify: true
  790. }
  791. $.fn.tokenfield.Constructor = Tokenfield
  792. /* TOKENFIELD NO CONFLICT
  793. * ================== */
  794. $.fn.tokenfield.noConflict = function () {
  795. $.fn.tokenfield = old
  796. return this
  797. }
  798. return Tokenfield;
  799. }));