selectize.js 105 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  1. /**
  2. * sifter.js
  3. * Copyright (c) 2013 Brian Reavis & contributors
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  6. * file except in compliance with the License. You may obtain a copy of the License at:
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under
  10. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  11. * ANY KIND, either express or implied. See the License for the specific language
  12. * governing permissions and limitations under the License.
  13. *
  14. * @author Brian Reavis <brian@thirdroute.com>
  15. */
  16. (function(root, factory) {
  17. if (typeof define === 'function' && define.amd) {
  18. define('sifter', factory);
  19. } else if (typeof exports === 'object') {
  20. module.exports = factory();
  21. } else {
  22. root.Sifter = factory();
  23. }
  24. }(this, function() {
  25. /**
  26. * Textually searches arrays and hashes of objects
  27. * by property (or multiple properties). Designed
  28. * specifically for autocomplete.
  29. *
  30. * @constructor
  31. * @param {array|object} items
  32. * @param {object} items
  33. */
  34. var Sifter = function(items, settings) {
  35. this.items = items;
  36. this.settings = settings || {diacritics: true};
  37. };
  38. /**
  39. * Splits a search string into an array of individual
  40. * regexps to be used to match results.
  41. *
  42. * @param {string} query
  43. * @returns {array}
  44. */
  45. Sifter.prototype.tokenize = function(query) {
  46. query = trim(String(query || '').toLowerCase());
  47. if (!query || !query.length) return [];
  48. var i, n, regex, letter;
  49. var tokens = [];
  50. var words = query.split(/ +/);
  51. for (i = 0, n = words.length; i < n; i++) {
  52. regex = escape_regex(words[i]);
  53. if (this.settings.diacritics) {
  54. for (letter in DIACRITICS) {
  55. if (DIACRITICS.hasOwnProperty(letter)) {
  56. regex = regex.replace(new RegExp(letter, 'g'), DIACRITICS[letter]);
  57. }
  58. }
  59. }
  60. tokens.push({
  61. string : words[i],
  62. regex : new RegExp(regex, 'i')
  63. });
  64. }
  65. return tokens;
  66. };
  67. /**
  68. * Iterates over arrays and hashes.
  69. *
  70. * ```
  71. * this.iterator(this.items, function(item, id) {
  72. * // invoked for each item
  73. * });
  74. * ```
  75. *
  76. * @param {array|object} object
  77. */
  78. Sifter.prototype.iterator = function(object, callback) {
  79. var iterator;
  80. if (is_array(object)) {
  81. iterator = Array.prototype.forEach || function(callback) {
  82. for (var i = 0, n = this.length; i < n; i++) {
  83. callback(this[i], i, this);
  84. }
  85. };
  86. } else {
  87. iterator = function(callback) {
  88. for (var key in this) {
  89. if (this.hasOwnProperty(key)) {
  90. callback(this[key], key, this);
  91. }
  92. }
  93. };
  94. }
  95. iterator.apply(object, [callback]);
  96. };
  97. /**
  98. * Returns a function to be used to score individual results.
  99. *
  100. * Good matches will have a higher score than poor matches.
  101. * If an item is not a match, 0 will be returned by the function.
  102. *
  103. * @param {object|string} search
  104. * @param {object} options (optional)
  105. * @returns {function}
  106. */
  107. Sifter.prototype.getScoreFunction = function(search, options) {
  108. var self, fields, tokens, token_count;
  109. self = this;
  110. search = self.prepareSearch(search, options);
  111. tokens = search.tokens;
  112. fields = search.options.fields;
  113. token_count = tokens.length;
  114. /**
  115. * Calculates how close of a match the
  116. * given value is against a search token.
  117. *
  118. * @param {mixed} value
  119. * @param {object} token
  120. * @return {number}
  121. */
  122. var scoreValue = function(value, token) {
  123. var score, pos;
  124. if (!value) return 0;
  125. value = String(value || '');
  126. pos = value.search(token.regex);
  127. if (pos === -1) return 0;
  128. score = token.string.length / value.length;
  129. if (pos === 0) score += 0.5;
  130. return score;
  131. };
  132. /**
  133. * Calculates the score of an object
  134. * against the search query.
  135. *
  136. * @param {object} token
  137. * @param {object} data
  138. * @return {number}
  139. */
  140. var scoreObject = (function() {
  141. var field_count = fields.length;
  142. if (!field_count) {
  143. return function() { return 0; };
  144. }
  145. if (field_count === 1) {
  146. return function(token, data) {
  147. return scoreValue(data[fields[0]], token);
  148. };
  149. }
  150. return function(token, data) {
  151. for (var i = 0, sum = 0; i < field_count; i++) {
  152. sum += scoreValue(data[fields[i]], token);
  153. }
  154. return sum / field_count;
  155. };
  156. })();
  157. if (!token_count) {
  158. return function() { return 0; };
  159. }
  160. if (token_count === 1) {
  161. return function(data) {
  162. return scoreObject(tokens[0], data);
  163. };
  164. }
  165. if (search.options.conjunction === 'and') {
  166. return function(data) {
  167. var score;
  168. for (var i = 0, sum = 0; i < token_count; i++) {
  169. score = scoreObject(tokens[i], data);
  170. if (score <= 0) return 0;
  171. sum += score;
  172. }
  173. return sum / token_count;
  174. };
  175. } else {
  176. return function(data) {
  177. for (var i = 0, sum = 0; i < token_count; i++) {
  178. sum += scoreObject(tokens[i], data);
  179. }
  180. return sum / token_count;
  181. };
  182. }
  183. };
  184. /**
  185. * Returns a function that can be used to compare two
  186. * results, for sorting purposes. If no sorting should
  187. * be performed, `null` will be returned.
  188. *
  189. * @param {string|object} search
  190. * @param {object} options
  191. * @return function(a,b)
  192. */
  193. Sifter.prototype.getSortFunction = function(search, options) {
  194. var i, n, self, field, fields, fields_count, multiplier, multipliers, get_field, implicit_score, sort;
  195. self = this;
  196. search = self.prepareSearch(search, options);
  197. sort = (!search.query && options.sort_empty) || options.sort;
  198. /**
  199. * Fetches the specified sort field value
  200. * from a search result item.
  201. *
  202. * @param {string} name
  203. * @param {object} result
  204. * @return {mixed}
  205. */
  206. get_field = function(name, result) {
  207. if (name === '$score') return result.score;
  208. return self.items[result.id][name];
  209. };
  210. // parse options
  211. fields = [];
  212. if (sort) {
  213. for (i = 0, n = sort.length; i < n; i++) {
  214. if (search.query || sort[i].field !== '$score') {
  215. fields.push(sort[i]);
  216. }
  217. }
  218. }
  219. // the "$score" field is implied to be the primary
  220. // sort field, unless it's manually specified
  221. if (search.query) {
  222. implicit_score = true;
  223. for (i = 0, n = fields.length; i < n; i++) {
  224. if (fields[i].field === '$score') {
  225. implicit_score = false;
  226. break;
  227. }
  228. }
  229. if (implicit_score) {
  230. fields.unshift({field: '$score', direction: 'desc'});
  231. }
  232. } else {
  233. for (i = 0, n = fields.length; i < n; i++) {
  234. if (fields[i].field === '$score') {
  235. fields.splice(i, 1);
  236. break;
  237. }
  238. }
  239. }
  240. multipliers = [];
  241. for (i = 0, n = fields.length; i < n; i++) {
  242. multipliers.push(fields[i].direction === 'desc' ? -1 : 1);
  243. }
  244. // build function
  245. fields_count = fields.length;
  246. if (!fields_count) {
  247. return null;
  248. } else if (fields_count === 1) {
  249. field = fields[0].field;
  250. multiplier = multipliers[0];
  251. return function(a, b) {
  252. return multiplier * cmp(
  253. get_field(field, a),
  254. get_field(field, b)
  255. );
  256. };
  257. } else {
  258. return function(a, b) {
  259. var i, result, a_value, b_value, field;
  260. for (i = 0; i < fields_count; i++) {
  261. field = fields[i].field;
  262. result = multipliers[i] * cmp(
  263. get_field(field, a),
  264. get_field(field, b)
  265. );
  266. if (result) return result;
  267. }
  268. return 0;
  269. };
  270. }
  271. };
  272. /**
  273. * Parses a search query and returns an object
  274. * with tokens and fields ready to be populated
  275. * with results.
  276. *
  277. * @param {string} query
  278. * @param {object} options
  279. * @returns {object}
  280. */
  281. Sifter.prototype.prepareSearch = function(query, options) {
  282. if (typeof query === 'object') return query;
  283. options = extend({}, options);
  284. var option_fields = options.fields;
  285. var option_sort = options.sort;
  286. var option_sort_empty = options.sort_empty;
  287. if (option_fields && !is_array(option_fields)) options.fields = [option_fields];
  288. if (option_sort && !is_array(option_sort)) options.sort = [option_sort];
  289. if (option_sort_empty && !is_array(option_sort_empty)) options.sort_empty = [option_sort_empty];
  290. return {
  291. options : options,
  292. query : String(query || '').toLowerCase(),
  293. tokens : this.tokenize(query),
  294. total : 0,
  295. items : []
  296. };
  297. };
  298. /**
  299. * Searches through all items and returns a sorted array of matches.
  300. *
  301. * The `options` parameter can contain:
  302. *
  303. * - fields {string|array}
  304. * - sort {array}
  305. * - score {function}
  306. * - filter {bool}
  307. * - limit {integer}
  308. *
  309. * Returns an object containing:
  310. *
  311. * - options {object}
  312. * - query {string}
  313. * - tokens {array}
  314. * - total {int}
  315. * - items {array}
  316. *
  317. * @param {string} query
  318. * @param {object} options
  319. * @returns {object}
  320. */
  321. Sifter.prototype.search = function(query, options) {
  322. var self = this, value, score, search, calculateScore;
  323. var fn_sort;
  324. var fn_score;
  325. search = this.prepareSearch(query, options);
  326. options = search.options;
  327. query = search.query;
  328. // generate result scoring function
  329. fn_score = options.score || self.getScoreFunction(search);
  330. // perform search and sort
  331. if (query.length) {
  332. self.iterator(self.items, function(item, id) {
  333. score = fn_score(item);
  334. if (options.filter === false || score > 0) {
  335. search.items.push({'score': score, 'id': id});
  336. }
  337. });
  338. } else {
  339. self.iterator(self.items, function(item, id) {
  340. search.items.push({'score': 1, 'id': id});
  341. });
  342. }
  343. fn_sort = self.getSortFunction(search, options);
  344. if (fn_sort) search.items.sort(fn_sort);
  345. // apply limits
  346. search.total = search.items.length;
  347. if (typeof options.limit === 'number') {
  348. search.items = search.items.slice(0, options.limit);
  349. }
  350. return search;
  351. };
  352. // utilities
  353. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  354. var cmp = function(a, b) {
  355. if (typeof a === 'number' && typeof b === 'number') {
  356. return a > b ? 1 : (a < b ? -1 : 0);
  357. }
  358. a = asciifold(String(a || ''));
  359. b = asciifold(String(b || ''));
  360. if (a > b) return 1;
  361. if (b > a) return -1;
  362. return 0;
  363. };
  364. var extend = function(a, b) {
  365. var i, n, k, object;
  366. for (i = 1, n = arguments.length; i < n; i++) {
  367. object = arguments[i];
  368. if (!object) continue;
  369. for (k in object) {
  370. if (object.hasOwnProperty(k)) {
  371. a[k] = object[k];
  372. }
  373. }
  374. }
  375. return a;
  376. };
  377. var trim = function(str) {
  378. return (str + '').replace(/^\s+|\s+$|/g, '');
  379. };
  380. var escape_regex = function(str) {
  381. return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
  382. };
  383. var is_array = Array.isArray || ($ && $.isArray) || function(object) {
  384. return Object.prototype.toString.call(object) === '[object Array]';
  385. };
  386. var DIACRITICS = {
  387. 'a': '[aÀÁÂÃÄÅàáâãäåĀāąĄ]',
  388. 'c': '[cÇçćĆčČ]',
  389. 'd': '[dđĐďĎ]',
  390. 'e': '[eÈÉÊËèéêëěĚĒēęĘ]',
  391. 'i': '[iÌÍÎÏìíîïĪī]',
  392. 'l': '[lłŁ]',
  393. 'n': '[nÑñňŇńŃ]',
  394. 'o': '[oÒÓÔÕÕÖØòóôõöøŌō]',
  395. 'r': '[rřŘ]',
  396. 's': '[sŠšśŚ]',
  397. 't': '[tťŤ]',
  398. 'u': '[uÙÚÛÜùúûüůŮŪū]',
  399. 'y': '[yŸÿýÝ]',
  400. 'z': '[zŽžżŻźŹ]'
  401. };
  402. var asciifold = (function() {
  403. var i, n, k, chunk;
  404. var foreignletters = '';
  405. var lookup = {};
  406. for (k in DIACRITICS) {
  407. if (DIACRITICS.hasOwnProperty(k)) {
  408. chunk = DIACRITICS[k].substring(2, DIACRITICS[k].length - 1);
  409. foreignletters += chunk;
  410. for (i = 0, n = chunk.length; i < n; i++) {
  411. lookup[chunk.charAt(i)] = k;
  412. }
  413. }
  414. }
  415. var regexp = new RegExp('[' + foreignletters + ']', 'g');
  416. return function(str) {
  417. return str.replace(regexp, function(foreignletter) {
  418. return lookup[foreignletter];
  419. }).toLowerCase();
  420. };
  421. })();
  422. // export
  423. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  424. return Sifter;
  425. }));
  426. /**
  427. * microplugin.js
  428. * Copyright (c) 2013 Brian Reavis & contributors
  429. *
  430. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  431. * file except in compliance with the License. You may obtain a copy of the License at:
  432. * http://www.apache.org/licenses/LICENSE-2.0
  433. *
  434. * Unless required by applicable law or agreed to in writing, software distributed under
  435. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  436. * ANY KIND, either express or implied. See the License for the specific language
  437. * governing permissions and limitations under the License.
  438. *
  439. * @author Brian Reavis <brian@thirdroute.com>
  440. */
  441. (function(root, factory) {
  442. if (typeof define === 'function' && define.amd) {
  443. define('microplugin', factory);
  444. } else if (typeof exports === 'object') {
  445. module.exports = factory();
  446. } else {
  447. root.MicroPlugin = factory();
  448. }
  449. }(this, function() {
  450. var MicroPlugin = {};
  451. MicroPlugin.mixin = function(Interface) {
  452. Interface.plugins = {};
  453. /**
  454. * Initializes the listed plugins (with options).
  455. * Acceptable formats:
  456. *
  457. * List (without options):
  458. * ['a', 'b', 'c']
  459. *
  460. * List (with options):
  461. * [{'name': 'a', options: {}}, {'name': 'b', options: {}}]
  462. *
  463. * Hash (with options):
  464. * {'a': { ... }, 'b': { ... }, 'c': { ... }}
  465. *
  466. * @param {mixed} plugins
  467. */
  468. Interface.prototype.initializePlugins = function(plugins) {
  469. var i, n, key;
  470. var self = this;
  471. var queue = [];
  472. self.plugins = {
  473. names : [],
  474. settings : {},
  475. requested : {},
  476. loaded : {}
  477. };
  478. if (utils.isArray(plugins)) {
  479. for (i = 0, n = plugins.length; i < n; i++) {
  480. if (typeof plugins[i] === 'string') {
  481. queue.push(plugins[i]);
  482. } else {
  483. self.plugins.settings[plugins[i].name] = plugins[i].options;
  484. queue.push(plugins[i].name);
  485. }
  486. }
  487. } else if (plugins) {
  488. for (key in plugins) {
  489. if (plugins.hasOwnProperty(key)) {
  490. self.plugins.settings[key] = plugins[key];
  491. queue.push(key);
  492. }
  493. }
  494. }
  495. while (queue.length) {
  496. self.require(queue.shift());
  497. }
  498. };
  499. Interface.prototype.loadPlugin = function(name) {
  500. var self = this;
  501. var plugins = self.plugins;
  502. var plugin = Interface.plugins[name];
  503. if (!Interface.plugins.hasOwnProperty(name)) {
  504. throw new Error('Unable to find "' + name + '" plugin');
  505. }
  506. plugins.requested[name] = true;
  507. plugins.loaded[name] = plugin.fn.apply(self, [self.plugins.settings[name] || {}]);
  508. plugins.names.push(name);
  509. };
  510. /**
  511. * Initializes a plugin.
  512. *
  513. * @param {string} name
  514. */
  515. Interface.prototype.require = function(name) {
  516. var self = this;
  517. var plugins = self.plugins;
  518. if (!self.plugins.loaded.hasOwnProperty(name)) {
  519. if (plugins.requested[name]) {
  520. throw new Error('Plugin has circular dependency ("' + name + '")');
  521. }
  522. self.loadPlugin(name);
  523. }
  524. return plugins.loaded[name];
  525. };
  526. /**
  527. * Registers a plugin.
  528. *
  529. * @param {string} name
  530. * @param {function} fn
  531. */
  532. Interface.define = function(name, fn) {
  533. Interface.plugins[name] = {
  534. 'name' : name,
  535. 'fn' : fn
  536. };
  537. };
  538. };
  539. var utils = {
  540. isArray: Array.isArray || function(vArg) {
  541. return Object.prototype.toString.call(vArg) === '[object Array]';
  542. }
  543. };
  544. return MicroPlugin;
  545. }));
  546. /**
  547. * selectize.js (v0.12.1)
  548. * Copyright (c) 2013–2015 Brian Reavis & contributors
  549. *
  550. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  551. * file except in compliance with the License. You may obtain a copy of the License at:
  552. * http://www.apache.org/licenses/LICENSE-2.0
  553. *
  554. * Unless required by applicable law or agreed to in writing, software distributed under
  555. * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
  556. * ANY KIND, either express or implied. See the License for the specific language
  557. * governing permissions and limitations under the License.
  558. *
  559. * @author Brian Reavis <brian@thirdroute.com>
  560. */
  561. /*jshint curly:false */
  562. /*jshint browser:true */
  563. (function(root, factory) {
  564. if (typeof define === 'function' && define.amd) {
  565. define('selectize', ['jquery','sifter','microplugin'], factory);
  566. } else if (typeof exports === 'object') {
  567. module.exports = factory(require('jquery'), require('sifter'), require('microplugin'));
  568. } else {
  569. root.Selectize = factory(root.jQuery, root.Sifter, root.MicroPlugin);
  570. }
  571. }(this, function($, Sifter, MicroPlugin) {
  572. 'use strict';
  573. var highlight = function($element, pattern) {
  574. if (typeof pattern === 'string' && !pattern.length) return;
  575. var regex = (typeof pattern === 'string') ? new RegExp(pattern, 'i') : pattern;
  576. var highlight = function(node) {
  577. var skip = 0;
  578. if (node.nodeType === 3) {
  579. var pos = node.data.search(regex);
  580. if (pos >= 0 && node.data.length > 0) {
  581. var match = node.data.match(regex);
  582. var spannode = document.createElement('span');
  583. spannode.className = 'highlight';
  584. var middlebit = node.splitText(pos);
  585. var endbit = middlebit.splitText(match[0].length);
  586. var middleclone = middlebit.cloneNode(true);
  587. spannode.appendChild(middleclone);
  588. middlebit.parentNode.replaceChild(spannode, middlebit);
  589. skip = 1;
  590. }
  591. } else if (node.nodeType === 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
  592. for (var i = 0; i < node.childNodes.length; ++i) {
  593. i += highlight(node.childNodes[i]);
  594. }
  595. }
  596. return skip;
  597. };
  598. return $element.each(function() {
  599. highlight(this);
  600. });
  601. };
  602. var MicroEvent = function() {};
  603. MicroEvent.prototype = {
  604. on: function(event, fct){
  605. this._events = this._events || {};
  606. this._events[event] = this._events[event] || [];
  607. this._events[event].push(fct);
  608. },
  609. off: function(event, fct){
  610. var n = arguments.length;
  611. if (n === 0) return delete this._events;
  612. if (n === 1) return delete this._events[event];
  613. this._events = this._events || {};
  614. if (event in this._events === false) return;
  615. this._events[event].splice(this._events[event].indexOf(fct), 1);
  616. },
  617. trigger: function(event /* , args... */){
  618. this._events = this._events || {};
  619. if (event in this._events === false) return;
  620. for (var i = 0; i < this._events[event].length; i++){
  621. this._events[event][i].apply(this, Array.prototype.slice.call(arguments, 1));
  622. }
  623. }
  624. };
  625. /**
  626. * Mixin will delegate all MicroEvent.js function in the destination object.
  627. *
  628. * - MicroEvent.mixin(Foobar) will make Foobar able to use MicroEvent
  629. *
  630. * @param {object} the object which will support MicroEvent
  631. */
  632. MicroEvent.mixin = function(destObject){
  633. var props = ['on', 'off', 'trigger'];
  634. for (var i = 0; i < props.length; i++){
  635. destObject.prototype[props[i]] = MicroEvent.prototype[props[i]];
  636. }
  637. };
  638. var IS_MAC = /Mac/.test(navigator.userAgent);
  639. var KEY_A = 65;
  640. var KEY_COMMA = 188;
  641. var KEY_RETURN = 13;
  642. var KEY_ESC = 27;
  643. var KEY_LEFT = 37;
  644. var KEY_UP = 38;
  645. var KEY_P = 80;
  646. var KEY_RIGHT = 39;
  647. var KEY_DOWN = 40;
  648. var KEY_N = 78;
  649. var KEY_BACKSPACE = 8;
  650. var KEY_DELETE = 46;
  651. var KEY_SHIFT = 16;
  652. var KEY_CMD = IS_MAC ? 91 : 17;
  653. var KEY_CTRL = IS_MAC ? 18 : 17;
  654. var KEY_TAB = 9;
  655. var TAG_SELECT = 1;
  656. var TAG_INPUT = 2;
  657. // for now, android support in general is too spotty to support validity
  658. var SUPPORTS_VALIDITY_API = !/android/i.test(window.navigator.userAgent) && !!document.createElement('form').validity;
  659. var isset = function(object) {
  660. return typeof object !== 'undefined';
  661. };
  662. /**
  663. * Converts a scalar to its best string representation
  664. * for hash keys and HTML attribute values.
  665. *
  666. * Transformations:
  667. * 'str' -> 'str'
  668. * null -> ''
  669. * undefined -> ''
  670. * true -> '1'
  671. * false -> '0'
  672. * 0 -> '0'
  673. * 1 -> '1'
  674. *
  675. * @param {string} value
  676. * @returns {string|null}
  677. */
  678. var hash_key = function(value) {
  679. if (typeof value === 'undefined' || value === null) return null;
  680. if (typeof value === 'boolean') return value ? '1' : '0';
  681. return value + '';
  682. };
  683. /**
  684. * Escapes a string for use within HTML.
  685. *
  686. * @param {string} str
  687. * @returns {string}
  688. */
  689. var escape_html = function(str) {
  690. return (str + '')
  691. .replace(/&/g, '&amp;')
  692. .replace(/</g, '&lt;')
  693. .replace(/>/g, '&gt;')
  694. .replace(/"/g, '&quot;');
  695. };
  696. /**
  697. * Escapes "$" characters in replacement strings.
  698. *
  699. * @param {string} str
  700. * @returns {string}
  701. */
  702. var escape_replace = function(str) {
  703. return (str + '').replace(/\$/g, '$$$$');
  704. };
  705. var hook = {};
  706. /**
  707. * Wraps `method` on `self` so that `fn`
  708. * is invoked before the original method.
  709. *
  710. * @param {object} self
  711. * @param {string} method
  712. * @param {function} fn
  713. */
  714. hook.before = function(self, method, fn) {
  715. var original = self[method];
  716. self[method] = function() {
  717. fn.apply(self, arguments);
  718. return original.apply(self, arguments);
  719. };
  720. };
  721. /**
  722. * Wraps `method` on `self` so that `fn`
  723. * is invoked after the original method.
  724. *
  725. * @param {object} self
  726. * @param {string} method
  727. * @param {function} fn
  728. */
  729. hook.after = function(self, method, fn) {
  730. var original = self[method];
  731. self[method] = function() {
  732. var result = original.apply(self, arguments);
  733. fn.apply(self, arguments);
  734. return result;
  735. };
  736. };
  737. /**
  738. * Wraps `fn` so that it can only be invoked once.
  739. *
  740. * @param {function} fn
  741. * @returns {function}
  742. */
  743. var once = function(fn) {
  744. var called = false;
  745. return function() {
  746. if (called) return;
  747. called = true;
  748. fn.apply(this, arguments);
  749. };
  750. };
  751. /**
  752. * Wraps `fn` so that it can only be called once
  753. * every `delay` milliseconds (invoked on the falling edge).
  754. *
  755. * @param {function} fn
  756. * @param {int} delay
  757. * @returns {function}
  758. */
  759. var debounce = function(fn, delay) {
  760. var timeout;
  761. return function() {
  762. var self = this;
  763. var args = arguments;
  764. window.clearTimeout(timeout);
  765. timeout = window.setTimeout(function() {
  766. fn.apply(self, args);
  767. }, delay);
  768. };
  769. };
  770. /**
  771. * Debounce all fired events types listed in `types`
  772. * while executing the provided `fn`.
  773. *
  774. * @param {object} self
  775. * @param {array} types
  776. * @param {function} fn
  777. */
  778. var debounce_events = function(self, types, fn) {
  779. var type;
  780. var trigger = self.trigger;
  781. var event_args = {};
  782. // override trigger method
  783. self.trigger = function() {
  784. var type = arguments[0];
  785. if (types.indexOf(type) !== -1) {
  786. event_args[type] = arguments;
  787. } else {
  788. return trigger.apply(self, arguments);
  789. }
  790. };
  791. // invoke provided function
  792. fn.apply(self, []);
  793. self.trigger = trigger;
  794. // trigger queued events
  795. for (type in event_args) {
  796. if (event_args.hasOwnProperty(type)) {
  797. trigger.apply(self, event_args[type]);
  798. }
  799. }
  800. };
  801. /**
  802. * A workaround for http://bugs.jquery.com/ticket/6696
  803. *
  804. * @param {object} $parent - Parent element to listen on.
  805. * @param {string} event - Event name.
  806. * @param {string} selector - Descendant selector to filter by.
  807. * @param {function} fn - Event handler.
  808. */
  809. var watchChildEvent = function($parent, event, selector, fn) {
  810. $parent.on(event, selector, function(e) {
  811. var child = e.target;
  812. while (child && child.parentNode !== $parent[0]) {
  813. child = child.parentNode;
  814. }
  815. e.currentTarget = child;
  816. return fn.apply(this, [e]);
  817. });
  818. };
  819. /**
  820. * Determines the current selection within a text input control.
  821. * Returns an object containing:
  822. * - start
  823. * - length
  824. *
  825. * @param {object} input
  826. * @returns {object}
  827. */
  828. var getSelection = function(input) {
  829. var result = {};
  830. if ('selectionStart' in input) {
  831. result.start = input.selectionStart;
  832. result.length = input.selectionEnd - result.start;
  833. } else if (document.selection) {
  834. input.focus();
  835. var sel = document.selection.createRange();
  836. var selLen = document.selection.createRange().text.length;
  837. sel.moveStart('character', -input.value.length);
  838. result.start = sel.text.length - selLen;
  839. result.length = selLen;
  840. }
  841. return result;
  842. };
  843. /**
  844. * Copies CSS properties from one element to another.
  845. *
  846. * @param {object} $from
  847. * @param {object} $to
  848. * @param {array} properties
  849. */
  850. var transferStyles = function($from, $to, properties) {
  851. var i, n, styles = {};
  852. if (properties) {
  853. for (i = 0, n = properties.length; i < n; i++) {
  854. styles[properties[i]] = $from.css(properties[i]);
  855. }
  856. } else {
  857. styles = $from.css();
  858. }
  859. $to.css(styles);
  860. };
  861. /**
  862. * Measures the width of a string within a
  863. * parent element (in pixels).
  864. *
  865. * @param {string} str
  866. * @param {object} $parent
  867. * @returns {int}
  868. */
  869. var measureString = function(str, $parent) {
  870. if (!str) {
  871. return 0;
  872. }
  873. var $test = $('<test>').css({
  874. position: 'absolute',
  875. top: -99999,
  876. left: -99999,
  877. width: 'auto',
  878. padding: 0,
  879. whiteSpace: 'pre'
  880. }).text(str).appendTo('body');
  881. transferStyles($parent, $test, [
  882. 'letterSpacing',
  883. 'fontSize',
  884. 'fontFamily',
  885. 'fontWeight',
  886. 'textTransform'
  887. ]);
  888. var width = $test.width();
  889. $test.remove();
  890. return width;
  891. };
  892. /**
  893. * Sets up an input to grow horizontally as the user
  894. * types. If the value is changed manually, you can
  895. * trigger the "update" handler to resize:
  896. *
  897. * $input.trigger('update');
  898. *
  899. * @param {object} $input
  900. */
  901. var autoGrow = function($input) {
  902. var currentWidth = null;
  903. var update = function(e, options) {
  904. var value, keyCode, printable, placeholder, width;
  905. var shift, character, selection;
  906. e = e || window.event || {};
  907. options = options || {};
  908. if (e.metaKey || e.altKey) return;
  909. if (!options.force && $input.data('grow') === false) return;
  910. value = $input.val();
  911. if (e.type && e.type.toLowerCase() === 'keydown') {
  912. keyCode = e.keyCode;
  913. printable = (
  914. (keyCode >= 97 && keyCode <= 122) || // a-z
  915. (keyCode >= 65 && keyCode <= 90) || // A-Z
  916. (keyCode >= 48 && keyCode <= 57) || // 0-9
  917. keyCode === 32 // space
  918. );
  919. if (keyCode === KEY_DELETE || keyCode === KEY_BACKSPACE) {
  920. selection = getSelection($input[0]);
  921. if (selection.length) {
  922. value = value.substring(0, selection.start) + value.substring(selection.start + selection.length);
  923. } else if (keyCode === KEY_BACKSPACE && selection.start) {
  924. value = value.substring(0, selection.start - 1) + value.substring(selection.start + 1);
  925. } else if (keyCode === KEY_DELETE && typeof selection.start !== 'undefined') {
  926. value = value.substring(0, selection.start) + value.substring(selection.start + 1);
  927. }
  928. } else if (printable) {
  929. shift = e.shiftKey;
  930. character = String.fromCharCode(e.keyCode);
  931. if (shift) character = character.toUpperCase();
  932. else character = character.toLowerCase();
  933. value += character;
  934. }
  935. }
  936. placeholder = $input.attr('placeholder');
  937. if (!value && placeholder) {
  938. value = placeholder;
  939. }
  940. width = measureString(value, $input) + 4;
  941. if (width !== currentWidth) {
  942. currentWidth = width;
  943. $input.width(width);
  944. $input.triggerHandler('resize');
  945. }
  946. };
  947. $input.on('keydown keyup update blur', update);
  948. update();
  949. };
  950. var Selectize = function($input, settings) {
  951. var key, i, n, dir, input, self = this;
  952. input = $input[0];
  953. input.selectize = self;
  954. // detect rtl environment
  955. var computedStyle = window.getComputedStyle && window.getComputedStyle(input, null);
  956. dir = computedStyle ? computedStyle.getPropertyValue('direction') : input.currentStyle && input.currentStyle.direction;
  957. dir = dir || $input.parents('[dir]:first').attr('dir') || '';
  958. // setup default state
  959. $.extend(self, {
  960. order : 0,
  961. settings : settings,
  962. $input : $input,
  963. tabIndex : $input.attr('tabindex') || '',
  964. tagType : input.tagName.toLowerCase() === 'select' ? TAG_SELECT : TAG_INPUT,
  965. rtl : /rtl/i.test(dir),
  966. eventNS : '.selectize' + (++Selectize.count),
  967. highlightedValue : null,
  968. isOpen : false,
  969. isDisabled : false,
  970. isRequired : $input.is('[required]'),
  971. isInvalid : false,
  972. isLocked : false,
  973. isFocused : false,
  974. isInputHidden : false,
  975. isSetup : false,
  976. isShiftDown : false,
  977. isCmdDown : false,
  978. isCtrlDown : false,
  979. ignoreFocus : false,
  980. ignoreBlur : false,
  981. ignoreHover : false,
  982. hasOptions : false,
  983. currentResults : null,
  984. lastValue : '',
  985. caretPos : 0,
  986. loading : 0,
  987. loadedSearches : {},
  988. $activeOption : null,
  989. $activeItems : [],
  990. optgroups : {},
  991. options : {},
  992. userOptions : {},
  993. items : [],
  994. renderCache : {},
  995. onSearchChange : settings.loadThrottle === null ? self.onSearchChange : debounce(self.onSearchChange, settings.loadThrottle)
  996. });
  997. // search system
  998. self.sifter = new Sifter(this.options, {diacritics: settings.diacritics});
  999. // build options table
  1000. if (self.settings.options) {
  1001. for (i = 0, n = self.settings.options.length; i < n; i++) {
  1002. self.registerOption(self.settings.options[i]);
  1003. }
  1004. delete self.settings.options;
  1005. }
  1006. // build optgroup table
  1007. if (self.settings.optgroups) {
  1008. for (i = 0, n = self.settings.optgroups.length; i < n; i++) {
  1009. self.registerOptionGroup(self.settings.optgroups[i]);
  1010. }
  1011. delete self.settings.optgroups;
  1012. }
  1013. // option-dependent defaults
  1014. self.settings.mode = self.settings.mode || (self.settings.maxItems === 1 ? 'single' : 'multi');
  1015. if (typeof self.settings.hideSelected !== 'boolean') {
  1016. self.settings.hideSelected = self.settings.mode === 'multi';
  1017. }
  1018. self.initializePlugins(self.settings.plugins);
  1019. self.setupCallbacks();
  1020. self.setupTemplates();
  1021. self.setup();
  1022. };
  1023. // mixins
  1024. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1025. MicroEvent.mixin(Selectize);
  1026. MicroPlugin.mixin(Selectize);
  1027. // methods
  1028. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  1029. $.extend(Selectize.prototype, {
  1030. /**
  1031. * Creates all elements and sets up event bindings.
  1032. */
  1033. setup: function() {
  1034. var self = this;
  1035. var settings = self.settings;
  1036. var eventNS = self.eventNS;
  1037. var $window = $(window);
  1038. var $document = $(document);
  1039. var $input = self.$input;
  1040. var $wrapper;
  1041. var $control;
  1042. var $control_input;
  1043. var $dropdown;
  1044. var $dropdown_content;
  1045. var $dropdown_parent;
  1046. var inputMode;
  1047. var timeout_blur;
  1048. var timeout_focus;
  1049. var classes;
  1050. var classes_plugins;
  1051. inputMode = self.settings.mode;
  1052. classes = $input.attr('class') || '';
  1053. $wrapper = $('<div>').addClass(settings.wrapperClass).addClass(classes).addClass(inputMode);
  1054. $control = $('<div>').addClass(settings.inputClass).addClass('items').appendTo($wrapper);
  1055. $control_input = $('<input type="text" autocomplete="off" />').appendTo($control).attr('tabindex', $input.is(':disabled') ? '-1' : self.tabIndex);
  1056. $dropdown_parent = $(settings.dropdownParent || $wrapper);
  1057. $dropdown = $('<div>').addClass(settings.dropdownClass).addClass(inputMode).hide().appendTo($dropdown_parent);
  1058. $dropdown_content = $('<div>').addClass(settings.dropdownContentClass).appendTo($dropdown);
  1059. if(self.settings.copyClassesToDropdown) {
  1060. $dropdown.addClass(classes);
  1061. }
  1062. $wrapper.css({
  1063. width: $input[0].style.width
  1064. });
  1065. if (self.plugins.names.length) {
  1066. classes_plugins = 'plugin-' + self.plugins.names.join(' plugin-');
  1067. $wrapper.addClass(classes_plugins);
  1068. $dropdown.addClass(classes_plugins);
  1069. }
  1070. if ((settings.maxItems === null || settings.maxItems > 1) && self.tagType === TAG_SELECT) {
  1071. $input.attr('multiple', 'multiple');
  1072. }
  1073. if (self.settings.placeholder) {
  1074. $control_input.attr('placeholder', settings.placeholder);
  1075. }
  1076. // if splitOn was not passed in, construct it from the delimiter to allow pasting universally
  1077. if (!self.settings.splitOn && self.settings.delimiter) {
  1078. var delimiterEscaped = self.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
  1079. self.settings.splitOn = new RegExp('\\s*' + delimiterEscaped + '+\\s*');
  1080. }
  1081. if ($input.attr('autocorrect')) {
  1082. $control_input.attr('autocorrect', $input.attr('autocorrect'));
  1083. }
  1084. if ($input.attr('autocapitalize')) {
  1085. $control_input.attr('autocapitalize', $input.attr('autocapitalize'));
  1086. }
  1087. self.$wrapper = $wrapper;
  1088. self.$control = $control;
  1089. self.$control_input = $control_input;
  1090. self.$dropdown = $dropdown;
  1091. self.$dropdown_content = $dropdown_content;
  1092. $dropdown.on('mouseenter', '[data-selectable]', function() { return self.onOptionHover.apply(self, arguments); });
  1093. $dropdown.on('mousedown click', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); });
  1094. watchChildEvent($control, 'mousedown', '*:not(input)', function() { return self.onItemSelect.apply(self, arguments); });
  1095. autoGrow($control_input);
  1096. $control.on({
  1097. mousedown : function() { return self.onMouseDown.apply(self, arguments); },
  1098. click : function() { return self.onClick.apply(self, arguments); }
  1099. });
  1100. $control_input.on({
  1101. mousedown : function(e) { e.stopPropagation(); },
  1102. keydown : function() { return self.onKeyDown.apply(self, arguments); },
  1103. keyup : function() { return self.onKeyUp.apply(self, arguments); },
  1104. keypress : function() { return self.onKeyPress.apply(self, arguments); },
  1105. resize : function() { self.positionDropdown.apply(self, []); },
  1106. blur : function() { return self.onBlur.apply(self, arguments); },
  1107. focus : function() { self.ignoreBlur = false; return self.onFocus.apply(self, arguments); },
  1108. paste : function() { return self.onPaste.apply(self, arguments); }
  1109. });
  1110. $document.on('keydown' + eventNS, function(e) {
  1111. self.isCmdDown = e[IS_MAC ? 'metaKey' : 'ctrlKey'];
  1112. self.isCtrlDown = e[IS_MAC ? 'altKey' : 'ctrlKey'];
  1113. self.isShiftDown = e.shiftKey;
  1114. });
  1115. $document.on('keyup' + eventNS, function(e) {
  1116. if (e.keyCode === KEY_CTRL) self.isCtrlDown = false;
  1117. if (e.keyCode === KEY_SHIFT) self.isShiftDown = false;
  1118. if (e.keyCode === KEY_CMD) self.isCmdDown = false;
  1119. });
  1120. $document.on('mousedown' + eventNS, function(e) {
  1121. if (self.isFocused) {
  1122. // prevent events on the dropdown scrollbar from causing the control to blur
  1123. if (e.target === self.$dropdown[0] || e.target.parentNode === self.$dropdown[0]) {
  1124. return false;
  1125. }
  1126. // blur on click outside
  1127. if (!self.$control.has(e.target).length && e.target !== self.$control[0]) {
  1128. self.blur(e.target);
  1129. }
  1130. }
  1131. });
  1132. $window.on(['scroll' + eventNS, 'resize' + eventNS].join(' '), function() {
  1133. if (self.isOpen) {
  1134. self.positionDropdown.apply(self, arguments);
  1135. }
  1136. });
  1137. $window.on('mousemove' + eventNS, function() {
  1138. self.ignoreHover = false;
  1139. });
  1140. // store original children and tab index so that they can be
  1141. // restored when the destroy() method is called.
  1142. this.revertSettings = {
  1143. $children : $input.children().detach(),
  1144. tabindex : $input.attr('tabindex')
  1145. };
  1146. $input.attr('tabindex', -1).hide().after(self.$wrapper);
  1147. if ($.isArray(settings.items)) {
  1148. self.setValue(settings.items);
  1149. delete settings.items;
  1150. }
  1151. // feature detect for the validation API
  1152. if (SUPPORTS_VALIDITY_API) {
  1153. $input.on('invalid' + eventNS, function(e) {
  1154. e.preventDefault();
  1155. self.isInvalid = true;
  1156. self.refreshState();
  1157. });
  1158. }
  1159. self.updateOriginalInput();
  1160. self.refreshItems();
  1161. self.refreshState();
  1162. self.updatePlaceholder();
  1163. self.isSetup = true;
  1164. if ($input.is(':disabled')) {
  1165. self.disable();
  1166. }
  1167. self.on('change', this.onChange);
  1168. $input.data('selectize', self);
  1169. $input.addClass('selectized');
  1170. self.trigger('initialize');
  1171. // preload options
  1172. if (settings.preload === true) {
  1173. self.onSearchChange('');
  1174. }
  1175. },
  1176. /**
  1177. * Sets up default rendering functions.
  1178. */
  1179. setupTemplates: function() {
  1180. var self = this;
  1181. var field_label = self.settings.labelField;
  1182. var field_optgroup = self.settings.optgroupLabelField;
  1183. var templates = {
  1184. 'optgroup': function(data) {
  1185. return '<div class="optgroup">' + data.html + '</div>';
  1186. },
  1187. 'optgroup_header': function(data, escape) {
  1188. return '<div class="optgroup-header">' + escape(data[field_optgroup]) + '</div>';
  1189. },
  1190. 'option': function(data, escape) {
  1191. return '<div class="option">' + escape(data[field_label]) + '</div>';
  1192. },
  1193. 'item': function(data, escape) {
  1194. return '<div class="item">' + escape(data[field_label]) + '</div>';
  1195. },
  1196. 'option_create': function(data, escape) {
  1197. return '<div class="create">Add <strong>' + escape(data.input) + '</strong>&hellip;</div>';
  1198. }
  1199. };
  1200. self.settings.render = $.extend({}, templates, self.settings.render);
  1201. },
  1202. /**
  1203. * Maps fired events to callbacks provided
  1204. * in the settings used when creating the control.
  1205. */
  1206. setupCallbacks: function() {
  1207. var key, fn, callbacks = {
  1208. 'initialize' : 'onInitialize',
  1209. 'change' : 'onChange',
  1210. 'item_add' : 'onItemAdd',
  1211. 'item_remove' : 'onItemRemove',
  1212. 'clear' : 'onClear',
  1213. 'option_add' : 'onOptionAdd',
  1214. 'option_remove' : 'onOptionRemove',
  1215. 'option_clear' : 'onOptionClear',
  1216. 'optgroup_add' : 'onOptionGroupAdd',
  1217. 'optgroup_remove' : 'onOptionGroupRemove',
  1218. 'optgroup_clear' : 'onOptionGroupClear',
  1219. 'dropdown_open' : 'onDropdownOpen',
  1220. 'dropdown_close' : 'onDropdownClose',
  1221. 'type' : 'onType',
  1222. 'load' : 'onLoad',
  1223. 'focus' : 'onFocus',
  1224. 'blur' : 'onBlur'
  1225. };
  1226. for (key in callbacks) {
  1227. if (callbacks.hasOwnProperty(key)) {
  1228. fn = this.settings[callbacks[key]];
  1229. if (fn) this.on(key, fn);
  1230. }
  1231. }
  1232. },
  1233. /**
  1234. * Triggered when the main control element
  1235. * has a click event.
  1236. *
  1237. * @param {object} e
  1238. * @return {boolean}
  1239. */
  1240. onClick: function(e) {
  1241. var self = this;
  1242. // necessary for mobile webkit devices (manual focus triggering
  1243. // is ignored unless invoked within a click event)
  1244. if (!self.isFocused) {
  1245. self.focus();
  1246. e.preventDefault();
  1247. }
  1248. },
  1249. /**
  1250. * Triggered when the main control element
  1251. * has a mouse down event.
  1252. *
  1253. * @param {object} e
  1254. * @return {boolean}
  1255. */
  1256. onMouseDown: function(e) {
  1257. var self = this;
  1258. var defaultPrevented = e.isDefaultPrevented();
  1259. var $target = $(e.target);
  1260. if (self.isFocused) {
  1261. // retain focus by preventing native handling. if the
  1262. // event target is the input it should not be modified.
  1263. // otherwise, text selection within the input won't work.
  1264. if (e.target !== self.$control_input[0]) {
  1265. if (self.settings.mode === 'single') {
  1266. // toggle dropdown
  1267. self.isOpen ? self.close() : self.open();
  1268. } else if (!defaultPrevented) {
  1269. self.setActiveItem(null);
  1270. }
  1271. return false;
  1272. }
  1273. } else {
  1274. // give control focus
  1275. if (!defaultPrevented) {
  1276. window.setTimeout(function() {
  1277. self.focus();
  1278. }, 0);
  1279. }
  1280. }
  1281. },
  1282. /**
  1283. * Triggered when the value of the control has been changed.
  1284. * This should propagate the event to the original DOM
  1285. * input / select element.
  1286. */
  1287. onChange: function() {
  1288. this.$input.trigger('change');
  1289. },
  1290. /**
  1291. * Triggered on <input> paste.
  1292. *
  1293. * @param {object} e
  1294. * @returns {boolean}
  1295. */
  1296. onPaste: function(e) {
  1297. var self = this;
  1298. if (self.isFull() || self.isInputHidden || self.isLocked) {
  1299. e.preventDefault();
  1300. } else {
  1301. // If a regex or string is included, this will split the pasted
  1302. // input and create Items for each separate value
  1303. if (self.settings.splitOn) {
  1304. setTimeout(function() {
  1305. var splitInput = $.trim(self.$control_input.val() || '').split(self.settings.splitOn);
  1306. for (var i = 0, n = splitInput.length; i < n; i++) {
  1307. self.createItem(splitInput[i]);
  1308. }
  1309. }, 0);
  1310. }
  1311. }
  1312. },
  1313. /**
  1314. * Triggered on <input> keypress.
  1315. *
  1316. * @param {object} e
  1317. * @returns {boolean}
  1318. */
  1319. onKeyPress: function(e) {
  1320. if (this.isLocked) return e && e.preventDefault();
  1321. var character = String.fromCharCode(e.keyCode || e.which);
  1322. if (this.settings.create && this.settings.mode === 'multi' && character === this.settings.delimiter) {
  1323. this.createItem();
  1324. e.preventDefault();
  1325. return false;
  1326. }
  1327. },
  1328. /**
  1329. * Triggered on <input> keydown.
  1330. *
  1331. * @param {object} e
  1332. * @returns {boolean}
  1333. */
  1334. onKeyDown: function(e) {
  1335. var isInput = e.target === this.$control_input[0];
  1336. var self = this;
  1337. if (self.isLocked) {
  1338. if (e.keyCode !== KEY_TAB) {
  1339. e.preventDefault();
  1340. }
  1341. return;
  1342. }
  1343. switch (e.keyCode) {
  1344. case KEY_A:
  1345. if (self.isCmdDown) {
  1346. self.selectAll();
  1347. return;
  1348. }
  1349. break;
  1350. case KEY_ESC:
  1351. if (self.isOpen) {
  1352. e.preventDefault();
  1353. e.stopPropagation();
  1354. self.close();
  1355. }
  1356. return;
  1357. case KEY_N:
  1358. if (!e.ctrlKey || e.altKey) break;
  1359. case KEY_DOWN:
  1360. if (!self.isOpen && self.hasOptions) {
  1361. self.open();
  1362. } else if (self.$activeOption) {
  1363. self.ignoreHover = true;
  1364. var $next = self.getAdjacentOption(self.$activeOption, 1);
  1365. if ($next.length) self.setActiveOption($next, true, true);
  1366. }
  1367. e.preventDefault();
  1368. return;
  1369. case KEY_P:
  1370. if (!e.ctrlKey || e.altKey) break;
  1371. case KEY_UP:
  1372. if (self.$activeOption) {
  1373. self.ignoreHover = true;
  1374. var $prev = self.getAdjacentOption(self.$activeOption, -1);
  1375. if ($prev.length) self.setActiveOption($prev, true, true);
  1376. }
  1377. e.preventDefault();
  1378. return;
  1379. case KEY_RETURN:
  1380. if (self.isOpen && self.$activeOption) {
  1381. self.onOptionSelect({currentTarget: self.$activeOption});
  1382. e.preventDefault();
  1383. }
  1384. return;
  1385. case KEY_LEFT:
  1386. self.advanceSelection(-1, e);
  1387. return;
  1388. case KEY_RIGHT:
  1389. self.advanceSelection(1, e);
  1390. return;
  1391. case KEY_TAB:
  1392. if (self.settings.selectOnTab && self.isOpen && self.$activeOption) {
  1393. self.onOptionSelect({currentTarget: self.$activeOption});
  1394. // Default behaviour is to jump to the next field, we only want this
  1395. // if the current field doesn't accept any more entries
  1396. if (!self.isFull()) {
  1397. e.preventDefault();
  1398. }
  1399. }
  1400. if (self.settings.create && self.createItem()) {
  1401. e.preventDefault();
  1402. }
  1403. return;
  1404. case KEY_BACKSPACE:
  1405. case KEY_DELETE:
  1406. self.deleteSelection(e);
  1407. return;
  1408. }
  1409. if ((self.isFull() || self.isInputHidden) && !(IS_MAC ? e.metaKey : e.ctrlKey)) {
  1410. e.preventDefault();
  1411. return;
  1412. }
  1413. },
  1414. /**
  1415. * Triggered on <input> keyup.
  1416. *
  1417. * @param {object} e
  1418. * @returns {boolean}
  1419. */
  1420. onKeyUp: function(e) {
  1421. var self = this;
  1422. if (self.isLocked) return e && e.preventDefault();
  1423. var value = self.$control_input.val() || '';
  1424. if (self.lastValue !== value) {
  1425. self.lastValue = value;
  1426. self.onSearchChange(value);
  1427. self.refreshOptions();
  1428. self.trigger('type', value);
  1429. }
  1430. },
  1431. /**
  1432. * Invokes the user-provide option provider / loader.
  1433. *
  1434. * Note: this function is debounced in the Selectize
  1435. * constructor (by `settings.loadDelay` milliseconds)
  1436. *
  1437. * @param {string} value
  1438. */
  1439. onSearchChange: function(value) {
  1440. var self = this;
  1441. var fn = self.settings.load;
  1442. if (!fn) return;
  1443. if (self.loadedSearches.hasOwnProperty(value)) return;
  1444. self.loadedSearches[value] = true;
  1445. self.load(function(callback) {
  1446. fn.apply(self, [value, callback]);
  1447. });
  1448. },
  1449. /**
  1450. * Triggered on <input> focus.
  1451. *
  1452. * @param {object} e (optional)
  1453. * @returns {boolean}
  1454. */
  1455. onFocus: function(e) {
  1456. var self = this;
  1457. var wasFocused = self.isFocused;
  1458. if (self.isDisabled) {
  1459. self.blur();
  1460. e && e.preventDefault();
  1461. return false;
  1462. }
  1463. if (self.ignoreFocus) return;
  1464. self.isFocused = true;
  1465. if (self.settings.preload === 'focus') self.onSearchChange('');
  1466. if (!wasFocused) self.trigger('focus');
  1467. if (!self.$activeItems.length) {
  1468. self.showInput();
  1469. self.setActiveItem(null);
  1470. self.refreshOptions(!!self.settings.openOnFocus);
  1471. }
  1472. self.refreshState();
  1473. },
  1474. /**
  1475. * Triggered on <input> blur.
  1476. *
  1477. * @param {object} e
  1478. * @param {Element} dest
  1479. */
  1480. onBlur: function(e, dest) {
  1481. var self = this;
  1482. if (!self.isFocused) return;
  1483. self.isFocused = false;
  1484. if (self.ignoreFocus) {
  1485. return;
  1486. } else if (!self.ignoreBlur && document.activeElement === self.$dropdown_content[0]) {
  1487. // necessary to prevent IE closing the dropdown when the scrollbar is clicked
  1488. self.ignoreBlur = true;
  1489. self.onFocus(e);
  1490. return;
  1491. }
  1492. var deactivate = function() {
  1493. self.close();
  1494. self.setTextboxValue('');
  1495. self.setActiveItem(null);
  1496. self.setActiveOption(null);
  1497. self.setCaret(self.items.length);
  1498. self.refreshState();
  1499. // IE11 bug: element still marked as active
  1500. (dest || document.body).focus();
  1501. self.ignoreFocus = false;
  1502. self.trigger('blur');
  1503. };
  1504. self.ignoreFocus = true;
  1505. if (self.settings.create && self.settings.createOnBlur) {
  1506. self.createItem(null, false, deactivate);
  1507. } else {
  1508. deactivate();
  1509. }
  1510. },
  1511. /**
  1512. * Triggered when the user rolls over
  1513. * an option in the autocomplete dropdown menu.
  1514. *
  1515. * @param {object} e
  1516. * @returns {boolean}
  1517. */
  1518. onOptionHover: function(e) {
  1519. if (this.ignoreHover) return;
  1520. this.setActiveOption(e.currentTarget, false);
  1521. },
  1522. /**
  1523. * Triggered when the user clicks on an option
  1524. * in the autocomplete dropdown menu.
  1525. *
  1526. * @param {object} e
  1527. * @returns {boolean}
  1528. */
  1529. onOptionSelect: function(e) {
  1530. var value, $target, $option, self = this;
  1531. if (e.preventDefault) {
  1532. e.preventDefault();
  1533. e.stopPropagation();
  1534. }
  1535. $target = $(e.currentTarget);
  1536. if ($target.hasClass('create')) {
  1537. self.createItem(null, function() {
  1538. if (self.settings.closeAfterSelect) {
  1539. self.close();
  1540. }
  1541. });
  1542. } else {
  1543. value = $target.attr('data-value');
  1544. if (typeof value !== 'undefined') {
  1545. self.lastQuery = null;
  1546. self.setTextboxValue('');
  1547. self.addItem(value);
  1548. if (self.settings.closeAfterSelect) {
  1549. self.close();
  1550. } else if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) {
  1551. self.setActiveOption(self.getOption(value));
  1552. }
  1553. }
  1554. }
  1555. },
  1556. /**
  1557. * Triggered when the user clicks on an item
  1558. * that has been selected.
  1559. *
  1560. * @param {object} e
  1561. * @returns {boolean}
  1562. */
  1563. onItemSelect: function(e) {
  1564. var self = this;
  1565. if (self.isLocked) return;
  1566. if (self.settings.mode === 'multi') {
  1567. e.preventDefault();
  1568. self.setActiveItem(e.currentTarget, e);
  1569. }
  1570. },
  1571. /**
  1572. * Invokes the provided method that provides
  1573. * results to a callback---which are then added
  1574. * as options to the control.
  1575. *
  1576. * @param {function} fn
  1577. */
  1578. load: function(fn) {
  1579. var self = this;
  1580. var $wrapper = self.$wrapper.addClass(self.settings.loadingClass);
  1581. self.loading++;
  1582. fn.apply(self, [function(results) {
  1583. self.loading = Math.max(self.loading - 1, 0);
  1584. if (results && results.length) {
  1585. self.addOption(results);
  1586. self.refreshOptions(self.isFocused && !self.isInputHidden);
  1587. }
  1588. if (!self.loading) {
  1589. $wrapper.removeClass(self.settings.loadingClass);
  1590. }
  1591. self.trigger('load', results);
  1592. }]);
  1593. },
  1594. /**
  1595. * Sets the input field of the control to the specified value.
  1596. *
  1597. * @param {string} value
  1598. */
  1599. setTextboxValue: function(value) {
  1600. var $input = this.$control_input;
  1601. var changed = $input.val() !== value;
  1602. if (changed) {
  1603. $input.val(value).triggerHandler('update');
  1604. this.lastValue = value;
  1605. }
  1606. },
  1607. /**
  1608. * Returns the value of the control. If multiple items
  1609. * can be selected (e.g. <select multiple>), this returns
  1610. * an array. If only one item can be selected, this
  1611. * returns a string.
  1612. *
  1613. * @returns {mixed}
  1614. */
  1615. getValue: function() {
  1616. if (this.tagType === TAG_SELECT && this.$input.attr('multiple')) {
  1617. return this.items;
  1618. } else {
  1619. return this.items.join(this.settings.delimiter);
  1620. }
  1621. },
  1622. /**
  1623. * Resets the selected items to the given value.
  1624. *
  1625. * @param {mixed} value
  1626. */
  1627. setValue: function(value, silent) {
  1628. var events = silent ? [] : ['change'];
  1629. debounce_events(this, events, function() {
  1630. this.clear(silent);
  1631. this.addItems(value, silent);
  1632. });
  1633. },
  1634. /**
  1635. * Sets the selected item.
  1636. *
  1637. * @param {object} $item
  1638. * @param {object} e (optional)
  1639. */
  1640. setActiveItem: function($item, e) {
  1641. var self = this;
  1642. var eventName;
  1643. var i, idx, begin, end, item, swap;
  1644. var $last;
  1645. if (self.settings.mode === 'single') return;
  1646. $item = $($item);
  1647. // clear the active selection
  1648. if (!$item.length) {
  1649. $(self.$activeItems).removeClass('active');
  1650. self.$activeItems = [];
  1651. if (self.isFocused) {
  1652. self.showInput();
  1653. }
  1654. return;
  1655. }
  1656. // modify selection
  1657. eventName = e && e.type.toLowerCase();
  1658. if (eventName === 'mousedown' && self.isShiftDown && self.$activeItems.length) {
  1659. $last = self.$control.children('.active:last');
  1660. begin = Array.prototype.indexOf.apply(self.$control[0].childNodes, [$last[0]]);
  1661. end = Array.prototype.indexOf.apply(self.$control[0].childNodes, [$item[0]]);
  1662. if (begin > end) {
  1663. swap = begin;
  1664. begin = end;
  1665. end = swap;
  1666. }
  1667. for (i = begin; i <= end; i++) {
  1668. item = self.$control[0].childNodes[i];
  1669. if (self.$activeItems.indexOf(item) === -1) {
  1670. $(item).addClass('active');
  1671. self.$activeItems.push(item);
  1672. }
  1673. }
  1674. e.preventDefault();
  1675. } else if ((eventName === 'mousedown' && self.isCtrlDown) || (eventName === 'keydown' && this.isShiftDown)) {
  1676. if ($item.hasClass('active')) {
  1677. idx = self.$activeItems.indexOf($item[0]);
  1678. self.$activeItems.splice(idx, 1);
  1679. $item.removeClass('active');
  1680. } else {
  1681. self.$activeItems.push($item.addClass('active')[0]);
  1682. }
  1683. } else {
  1684. $(self.$activeItems).removeClass('active');
  1685. self.$activeItems = [$item.addClass('active')[0]];
  1686. }
  1687. // ensure control has focus
  1688. self.hideInput();
  1689. if (!this.isFocused) {
  1690. self.focus();
  1691. }
  1692. },
  1693. /**
  1694. * Sets the selected item in the dropdown menu
  1695. * of available options.
  1696. *
  1697. * @param {object} $object
  1698. * @param {boolean} scroll
  1699. * @param {boolean} animate
  1700. */
  1701. setActiveOption: function($option, scroll, animate) {
  1702. var height_menu, height_item, y;
  1703. var scroll_top, scroll_bottom;
  1704. var self = this;
  1705. if (self.$activeOption) self.$activeOption.removeClass('active');
  1706. self.$activeOption = null;
  1707. $option = $($option);
  1708. if (!$option.length) return;
  1709. self.$activeOption = $option.addClass('active');
  1710. if (scroll || !isset(scroll)) {
  1711. height_menu = self.$dropdown_content.height();
  1712. height_item = self.$activeOption.outerHeight(true);
  1713. scroll = self.$dropdown_content.scrollTop() || 0;
  1714. y = self.$activeOption.offset().top - self.$dropdown_content.offset().top + scroll;
  1715. scroll_top = y;
  1716. scroll_bottom = y - height_menu + height_item;
  1717. if (y + height_item > height_menu + scroll) {
  1718. self.$dropdown_content.stop().animate({scrollTop: scroll_bottom}, animate ? self.settings.scrollDuration : 0);
  1719. } else if (y < scroll) {
  1720. self.$dropdown_content.stop().animate({scrollTop: scroll_top}, animate ? self.settings.scrollDuration : 0);
  1721. }
  1722. }
  1723. },
  1724. /**
  1725. * Selects all items (CTRL + A).
  1726. */
  1727. selectAll: function() {
  1728. var self = this;
  1729. if (self.settings.mode === 'single') return;
  1730. self.$activeItems = Array.prototype.slice.apply(self.$control.children(':not(input)').addClass('active'));
  1731. if (self.$activeItems.length) {
  1732. self.hideInput();
  1733. self.close();
  1734. }
  1735. self.focus();
  1736. },
  1737. /**
  1738. * Hides the input element out of view, while
  1739. * retaining its focus.
  1740. */
  1741. hideInput: function() {
  1742. var self = this;
  1743. self.setTextboxValue('');
  1744. self.$control_input.css({opacity: 0, position: 'absolute', left: self.rtl ? 10000 : -10000});
  1745. self.isInputHidden = true;
  1746. },
  1747. /**
  1748. * Restores input visibility.
  1749. */
  1750. showInput: function() {
  1751. this.$control_input.css({opacity: 1, position: 'relative', left: 0});
  1752. this.isInputHidden = false;
  1753. },
  1754. /**
  1755. * Gives the control focus.
  1756. */
  1757. focus: function() {
  1758. var self = this;
  1759. if (self.isDisabled) return;
  1760. self.ignoreFocus = true;
  1761. self.$control_input[0].focus();
  1762. window.setTimeout(function() {
  1763. self.ignoreFocus = false;
  1764. self.onFocus();
  1765. }, 0);
  1766. },
  1767. /**
  1768. * Forces the control out of focus.
  1769. *
  1770. * @param {Element} dest
  1771. */
  1772. blur: function(dest) {
  1773. this.$control_input[0].blur();
  1774. this.onBlur(null, dest);
  1775. },
  1776. /**
  1777. * Returns a function that scores an object
  1778. * to show how good of a match it is to the
  1779. * provided query.
  1780. *
  1781. * @param {string} query
  1782. * @param {object} options
  1783. * @return {function}
  1784. */
  1785. getScoreFunction: function(query) {
  1786. return this.sifter.getScoreFunction(query, this.getSearchOptions());
  1787. },
  1788. /**
  1789. * Returns search options for sifter (the system
  1790. * for scoring and sorting results).
  1791. *
  1792. * @see https://github.com/brianreavis/sifter.js
  1793. * @return {object}
  1794. */
  1795. getSearchOptions: function() {
  1796. var settings = this.settings;
  1797. var sort = settings.sortField;
  1798. if (typeof sort === 'string') {
  1799. sort = [{field: sort}];
  1800. }
  1801. return {
  1802. fields : settings.searchField,
  1803. conjunction : settings.searchConjunction,
  1804. sort : sort
  1805. };
  1806. },
  1807. /**
  1808. * Searches through available options and returns
  1809. * a sorted array of matches.
  1810. *
  1811. * Returns an object containing:
  1812. *
  1813. * - query {string}
  1814. * - tokens {array}
  1815. * - total {int}
  1816. * - items {array}
  1817. *
  1818. * @param {string} query
  1819. * @returns {object}
  1820. */
  1821. search: function(query) {
  1822. var i, value, score, result, calculateScore;
  1823. var self = this;
  1824. var settings = self.settings;
  1825. var options = this.getSearchOptions();
  1826. // validate user-provided result scoring function
  1827. if (settings.score) {
  1828. calculateScore = self.settings.score.apply(this, [query]);
  1829. if (typeof calculateScore !== 'function') {
  1830. throw new Error('Selectize "score" setting must be a function that returns a function');
  1831. }
  1832. }
  1833. // perform search
  1834. if (query !== self.lastQuery) {
  1835. self.lastQuery = query;
  1836. result = self.sifter.search(query, $.extend(options, {score: calculateScore}));
  1837. self.currentResults = result;
  1838. } else {
  1839. result = $.extend(true, {}, self.currentResults);
  1840. }
  1841. // filter out selected items
  1842. if (settings.hideSelected) {
  1843. for (i = result.items.length - 1; i >= 0; i--) {
  1844. if (self.items.indexOf(hash_key(result.items[i].id)) !== -1) {
  1845. result.items.splice(i, 1);
  1846. }
  1847. }
  1848. }
  1849. return result;
  1850. },
  1851. /**
  1852. * Refreshes the list of available options shown
  1853. * in the autocomplete dropdown menu.
  1854. *
  1855. * @param {boolean} triggerDropdown
  1856. */
  1857. refreshOptions: function(triggerDropdown) {
  1858. var i, j, k, n, groups, groups_order, option, option_html, optgroup, optgroups, html, html_children, has_create_option;
  1859. var $active, $active_before, $create;
  1860. if (typeof triggerDropdown === 'undefined') {
  1861. triggerDropdown = true;
  1862. }
  1863. var self = this;
  1864. var query = $.trim(self.$control_input.val());
  1865. var results = self.search(query);
  1866. var $dropdown_content = self.$dropdown_content;
  1867. var active_before = self.$activeOption && hash_key(self.$activeOption.attr('data-value'));
  1868. // build markup
  1869. n = results.items.length;
  1870. if (typeof self.settings.maxOptions === 'number') {
  1871. n = Math.min(n, self.settings.maxOptions);
  1872. }
  1873. // render and group available options individually
  1874. groups = {};
  1875. groups_order = [];
  1876. for (i = 0; i < n; i++) {
  1877. option = self.options[results.items[i].id];
  1878. option_html = self.render('option', option);
  1879. optgroup = option[self.settings.optgroupField] || '';
  1880. optgroups = $.isArray(optgroup) ? optgroup : [optgroup];
  1881. for (j = 0, k = optgroups && optgroups.length; j < k; j++) {
  1882. optgroup = optgroups[j];
  1883. if (!self.optgroups.hasOwnProperty(optgroup)) {
  1884. optgroup = '';
  1885. }
  1886. if (!groups.hasOwnProperty(optgroup)) {
  1887. groups[optgroup] = [];
  1888. groups_order.push(optgroup);
  1889. }
  1890. groups[optgroup].push(option_html);
  1891. }
  1892. }
  1893. // sort optgroups
  1894. if (this.settings.lockOptgroupOrder) {
  1895. groups_order.sort(function(a, b) {
  1896. var a_order = self.optgroups[a].$order || 0;
  1897. var b_order = self.optgroups[b].$order || 0;
  1898. return a_order - b_order;
  1899. });
  1900. }
  1901. // render optgroup headers & join groups
  1902. html = [];
  1903. for (i = 0, n = groups_order.length; i < n; i++) {
  1904. optgroup = groups_order[i];
  1905. if (self.optgroups.hasOwnProperty(optgroup) && groups[optgroup].length) {
  1906. // render the optgroup header and options within it,
  1907. // then pass it to the wrapper template
  1908. html_children = self.render('optgroup_header', self.optgroups[optgroup]) || '';
  1909. html_children += groups[optgroup].join('');
  1910. html.push(self.render('optgroup', $.extend({}, self.optgroups[optgroup], {
  1911. html: html_children
  1912. })));
  1913. } else {
  1914. html.push(groups[optgroup].join(''));
  1915. }
  1916. }
  1917. $dropdown_content.html(html.join(''));
  1918. // highlight matching terms inline
  1919. if (self.settings.highlight && results.query.length && results.tokens.length) {
  1920. for (i = 0, n = results.tokens.length; i < n; i++) {
  1921. highlight($dropdown_content, results.tokens[i].regex);
  1922. }
  1923. }
  1924. // add "selected" class to selected options
  1925. if (!self.settings.hideSelected) {
  1926. for (i = 0, n = self.items.length; i < n; i++) {
  1927. self.getOption(self.items[i]).addClass('selected');
  1928. }
  1929. }
  1930. // add create option
  1931. has_create_option = self.canCreate(query);
  1932. if (has_create_option) {
  1933. $dropdown_content.prepend(self.render('option_create', {input: query}));
  1934. $create = $($dropdown_content[0].childNodes[0]);
  1935. }
  1936. // activate
  1937. self.hasOptions = results.items.length > 0 || has_create_option;
  1938. if (self.hasOptions) {
  1939. if (results.items.length > 0) {
  1940. $active_before = active_before && self.getOption(active_before);
  1941. if ($active_before && $active_before.length) {
  1942. $active = $active_before;
  1943. } else if (self.settings.mode === 'single' && self.items.length) {
  1944. $active = self.getOption(self.items[0]);
  1945. }
  1946. if (!$active || !$active.length) {
  1947. if ($create && !self.settings.addPrecedence) {
  1948. $active = self.getAdjacentOption($create, 1);
  1949. } else {
  1950. $active = $dropdown_content.find('[data-selectable]:first');
  1951. }
  1952. }
  1953. } else {
  1954. $active = $create;
  1955. }
  1956. self.setActiveOption($active);
  1957. if (triggerDropdown && !self.isOpen) { self.open(); }
  1958. } else {
  1959. self.setActiveOption(null);
  1960. if (triggerDropdown && self.isOpen) { self.close(); }
  1961. }
  1962. },
  1963. /**
  1964. * Adds an available option. If it already exists,
  1965. * nothing will happen. Note: this does not refresh
  1966. * the options list dropdown (use `refreshOptions`
  1967. * for that).
  1968. *
  1969. * Usage:
  1970. *
  1971. * this.addOption(data)
  1972. *
  1973. * @param {object|array} data
  1974. */
  1975. addOption: function(data) {
  1976. var i, n, value, self = this;
  1977. if ($.isArray(data)) {
  1978. for (i = 0, n = data.length; i < n; i++) {
  1979. self.addOption(data[i]);
  1980. }
  1981. return;
  1982. }
  1983. if (value = self.registerOption(data)) {
  1984. self.userOptions[value] = true;
  1985. self.lastQuery = null;
  1986. self.trigger('option_add', value, data);
  1987. }
  1988. },
  1989. /**
  1990. * Registers an option to the pool of options.
  1991. *
  1992. * @param {object} data
  1993. * @return {boolean|string}
  1994. */
  1995. registerOption: function(data) {
  1996. var key = hash_key(data[this.settings.valueField]);
  1997. if ((!key || this.options.hasOwnProperty(key)) && !this.settings.allowEmptyOption) return false;
  1998. data.$order = data.$order || ++this.order;
  1999. this.options[key] = data;
  2000. return key;
  2001. },
  2002. /**
  2003. * Registers an option group to the pool of option groups.
  2004. *
  2005. * @param {object} data
  2006. * @return {boolean|string}
  2007. */
  2008. registerOptionGroup: function(data) {
  2009. var key = hash_key(data[this.settings.optgroupValueField]);
  2010. if (!key) return false;
  2011. data.$order = data.$order || ++this.order;
  2012. this.optgroups[key] = data;
  2013. return key;
  2014. },
  2015. /**
  2016. * Registers a new optgroup for options
  2017. * to be bucketed into.
  2018. *
  2019. * @param {string} id
  2020. * @param {object} data
  2021. */
  2022. addOptionGroup: function(id, data) {
  2023. data[this.settings.optgroupValueField] = id;
  2024. if (id = this.registerOptionGroup(data)) {
  2025. this.trigger('optgroup_add', id, data);
  2026. }
  2027. },
  2028. /**
  2029. * Removes an existing option group.
  2030. *
  2031. * @param {string} id
  2032. */
  2033. removeOptionGroup: function(id) {
  2034. if (this.optgroups.hasOwnProperty(id)) {
  2035. delete this.optgroups[id];
  2036. this.renderCache = {};
  2037. this.trigger('optgroup_remove', id);
  2038. }
  2039. },
  2040. /**
  2041. * Clears all existing option groups.
  2042. */
  2043. clearOptionGroups: function() {
  2044. this.optgroups = {};
  2045. this.renderCache = {};
  2046. this.trigger('optgroup_clear');
  2047. },
  2048. /**
  2049. * Updates an option available for selection. If
  2050. * it is visible in the selected items or options
  2051. * dropdown, it will be re-rendered automatically.
  2052. *
  2053. * @param {string} value
  2054. * @param {object} data
  2055. */
  2056. updateOption: function(value, data) {
  2057. var self = this;
  2058. var $item, $item_new;
  2059. var value_new, index_item, cache_items, cache_options, order_old;
  2060. value = hash_key(value);
  2061. value_new = hash_key(data[self.settings.valueField]);
  2062. // sanity checks
  2063. if (value === null) return;
  2064. if (!self.options.hasOwnProperty(value)) return;
  2065. if (typeof value_new !== 'string') throw new Error('Value must be set in option data');
  2066. order_old = self.options[value].$order;
  2067. // update references
  2068. if (value_new !== value) {
  2069. delete self.options[value];
  2070. index_item = self.items.indexOf(value);
  2071. if (index_item !== -1) {
  2072. self.items.splice(index_item, 1, value_new);
  2073. }
  2074. }
  2075. data.$order = data.$order || order_old;
  2076. self.options[value_new] = data;
  2077. // invalidate render cache
  2078. cache_items = self.renderCache['item'];
  2079. cache_options = self.renderCache['option'];
  2080. if (cache_items) {
  2081. delete cache_items[value];
  2082. delete cache_items[value_new];
  2083. }
  2084. if (cache_options) {
  2085. delete cache_options[value];
  2086. delete cache_options[value_new];
  2087. }
  2088. // update the item if it's selected
  2089. if (self.items.indexOf(value_new) !== -1) {
  2090. $item = self.getItem(value);
  2091. $item_new = $(self.render('item', data));
  2092. if ($item.hasClass('active')) $item_new.addClass('active');
  2093. $item.replaceWith($item_new);
  2094. }
  2095. // invalidate last query because we might have updated the sortField
  2096. self.lastQuery = null;
  2097. // update dropdown contents
  2098. if (self.isOpen) {
  2099. self.refreshOptions(false);
  2100. }
  2101. },
  2102. /**
  2103. * Removes a single option.
  2104. *
  2105. * @param {string} value
  2106. * @param {boolean} silent
  2107. */
  2108. removeOption: function(value, silent) {
  2109. var self = this;
  2110. value = hash_key(value);
  2111. var cache_items = self.renderCache['item'];
  2112. var cache_options = self.renderCache['option'];
  2113. if (cache_items) delete cache_items[value];
  2114. if (cache_options) delete cache_options[value];
  2115. delete self.userOptions[value];
  2116. delete self.options[value];
  2117. self.lastQuery = null;
  2118. self.trigger('option_remove', value);
  2119. self.removeItem(value, silent);
  2120. },
  2121. /**
  2122. * Clears all options.
  2123. */
  2124. clearOptions: function() {
  2125. var self = this;
  2126. self.loadedSearches = {};
  2127. self.userOptions = {};
  2128. self.renderCache = {};
  2129. self.options = self.sifter.items = {};
  2130. self.lastQuery = null;
  2131. self.trigger('option_clear');
  2132. self.clear();
  2133. },
  2134. /**
  2135. * Returns the jQuery element of the option
  2136. * matching the given value.
  2137. *
  2138. * @param {string} value
  2139. * @returns {object}
  2140. */
  2141. getOption: function(value) {
  2142. return this.getElementWithValue(value, this.$dropdown_content.find('[data-selectable]'));
  2143. },
  2144. /**
  2145. * Returns the jQuery element of the next or
  2146. * previous selectable option.
  2147. *
  2148. * @param {object} $option
  2149. * @param {int} direction can be 1 for next or -1 for previous
  2150. * @return {object}
  2151. */
  2152. getAdjacentOption: function($option, direction) {
  2153. var $options = this.$dropdown.find('[data-selectable]');
  2154. var index = $options.index($option) + direction;
  2155. return index >= 0 && index < $options.length ? $options.eq(index) : $();
  2156. },
  2157. /**
  2158. * Finds the first element with a "data-value" attribute
  2159. * that matches the given value.
  2160. *
  2161. * @param {mixed} value
  2162. * @param {object} $els
  2163. * @return {object}
  2164. */
  2165. getElementWithValue: function(value, $els) {
  2166. value = hash_key(value);
  2167. if (typeof value !== 'undefined' && value !== null) {
  2168. for (var i = 0, n = $els.length; i < n; i++) {
  2169. if ($els[i].getAttribute('data-value') === value) {
  2170. return $($els[i]);
  2171. }
  2172. }
  2173. }
  2174. return $();
  2175. },
  2176. /**
  2177. * Returns the jQuery element of the item
  2178. * matching the given value.
  2179. *
  2180. * @param {string} value
  2181. * @returns {object}
  2182. */
  2183. getItem: function(value) {
  2184. return this.getElementWithValue(value, this.$control.children());
  2185. },
  2186. /**
  2187. * "Selects" multiple items at once. Adds them to the list
  2188. * at the current caret position.
  2189. *
  2190. * @param {string} value
  2191. * @param {boolean} silent
  2192. */
  2193. addItems: function(values, silent) {
  2194. var items = $.isArray(values) ? values : [values];
  2195. for (var i = 0, n = items.length; i < n; i++) {
  2196. this.isPending = (i < n - 1);
  2197. this.addItem(items[i], silent);
  2198. }
  2199. },
  2200. /**
  2201. * "Selects" an item. Adds it to the list
  2202. * at the current caret position.
  2203. *
  2204. * @param {string} value
  2205. * @param {boolean} silent
  2206. */
  2207. addItem: function(value, silent) {
  2208. var events = silent ? [] : ['change'];
  2209. debounce_events(this, events, function() {
  2210. var $item, $option, $options;
  2211. var self = this;
  2212. var inputMode = self.settings.mode;
  2213. var i, active, value_next, wasFull;
  2214. value = hash_key(value);
  2215. if (self.items.indexOf(value) !== -1) {
  2216. if (inputMode === 'single') self.close();
  2217. return;
  2218. }
  2219. if (!self.options.hasOwnProperty(value)) return;
  2220. if (inputMode === 'single') self.clear(silent);
  2221. if (inputMode === 'multi' && self.isFull()) return;
  2222. $item = $(self.render('item', self.options[value]));
  2223. wasFull = self.isFull();
  2224. self.items.splice(self.caretPos, 0, value);
  2225. self.insertAtCaret($item);
  2226. if (!self.isPending || (!wasFull && self.isFull())) {
  2227. self.refreshState();
  2228. }
  2229. if (self.isSetup) {
  2230. $options = self.$dropdown_content.find('[data-selectable]');
  2231. // update menu / remove the option (if this is not one item being added as part of series)
  2232. if (!self.isPending) {
  2233. $option = self.getOption(value);
  2234. value_next = self.getAdjacentOption($option, 1).attr('data-value');
  2235. self.refreshOptions(self.isFocused && inputMode !== 'single');
  2236. if (value_next) {
  2237. self.setActiveOption(self.getOption(value_next));
  2238. }
  2239. }
  2240. // hide the menu if the maximum number of items have been selected or no options are left
  2241. if (!$options.length || self.isFull()) {
  2242. self.close();
  2243. } else {
  2244. self.positionDropdown();
  2245. }
  2246. self.updatePlaceholder();
  2247. self.trigger('item_add', value, $item);
  2248. self.updateOriginalInput({silent: silent});
  2249. }
  2250. });
  2251. },
  2252. /**
  2253. * Removes the selected item matching
  2254. * the provided value.
  2255. *
  2256. * @param {string} value
  2257. */
  2258. removeItem: function(value, silent) {
  2259. var self = this;
  2260. var $item, i, idx;
  2261. $item = (typeof value === 'object') ? value : self.getItem(value);
  2262. value = hash_key($item.attr('data-value'));
  2263. i = self.items.indexOf(value);
  2264. if (i !== -1) {
  2265. $item.remove();
  2266. if ($item.hasClass('active')) {
  2267. idx = self.$activeItems.indexOf($item[0]);
  2268. self.$activeItems.splice(idx, 1);
  2269. }
  2270. self.items.splice(i, 1);
  2271. self.lastQuery = null;
  2272. if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) {
  2273. self.removeOption(value, silent);
  2274. }
  2275. if (i < self.caretPos) {
  2276. self.setCaret(self.caretPos - 1);
  2277. }
  2278. self.refreshState();
  2279. self.updatePlaceholder();
  2280. self.updateOriginalInput({silent: silent});
  2281. self.positionDropdown();
  2282. self.trigger('item_remove', value, $item);
  2283. }
  2284. },
  2285. /**
  2286. * Invokes the `create` method provided in the
  2287. * selectize options that should provide the data
  2288. * for the new item, given the user input.
  2289. *
  2290. * Once this completes, it will be added
  2291. * to the item list.
  2292. *
  2293. * @param {string} value
  2294. * @param {boolean} [triggerDropdown]
  2295. * @param {function} [callback]
  2296. * @return {boolean}
  2297. */
  2298. createItem: function(input, triggerDropdown) {
  2299. var self = this;
  2300. var caret = self.caretPos;
  2301. input = input || $.trim(self.$control_input.val() || '');
  2302. var callback = arguments[arguments.length - 1];
  2303. if (typeof callback !== 'function') callback = function() {};
  2304. if (typeof triggerDropdown !== 'boolean') {
  2305. triggerDropdown = true;
  2306. }
  2307. if (!self.canCreate(input)) {
  2308. callback();
  2309. return false;
  2310. }
  2311. self.lock();
  2312. var setup = (typeof self.settings.create === 'function') ? this.settings.create : function(input) {
  2313. var data = {};
  2314. data[self.settings.labelField] = input;
  2315. data[self.settings.valueField] = input;
  2316. return data;
  2317. };
  2318. var create = once(function(data) {
  2319. self.unlock();
  2320. if (!data || typeof data !== 'object') return callback();
  2321. var value = hash_key(data[self.settings.valueField]);
  2322. if (typeof value !== 'string') return callback();
  2323. self.setTextboxValue('');
  2324. self.addOption(data);
  2325. self.setCaret(caret);
  2326. self.addItem(value);
  2327. self.refreshOptions(triggerDropdown && self.settings.mode !== 'single');
  2328. callback(data);
  2329. });
  2330. var output = setup.apply(this, [input, create]);
  2331. if (typeof output !== 'undefined') {
  2332. create(output);
  2333. }
  2334. return true;
  2335. },
  2336. /**
  2337. * Re-renders the selected item lists.
  2338. */
  2339. refreshItems: function() {
  2340. this.lastQuery = null;
  2341. if (this.isSetup) {
  2342. this.addItem(this.items);
  2343. }
  2344. this.refreshState();
  2345. this.updateOriginalInput();
  2346. },
  2347. /**
  2348. * Updates all state-dependent attributes
  2349. * and CSS classes.
  2350. */
  2351. refreshState: function() {
  2352. var invalid, self = this;
  2353. if (self.isRequired) {
  2354. if (self.items.length) self.isInvalid = false;
  2355. self.$control_input.prop('required', invalid);
  2356. }
  2357. self.refreshClasses();
  2358. },
  2359. /**
  2360. * Updates all state-dependent CSS classes.
  2361. */
  2362. refreshClasses: function() {
  2363. var self = this;
  2364. var isFull = self.isFull();
  2365. var isLocked = self.isLocked;
  2366. self.$wrapper
  2367. .toggleClass('rtl', self.rtl);
  2368. self.$control
  2369. .toggleClass('focus', self.isFocused)
  2370. .toggleClass('disabled', self.isDisabled)
  2371. .toggleClass('required', self.isRequired)
  2372. .toggleClass('invalid', self.isInvalid)
  2373. .toggleClass('locked', isLocked)
  2374. .toggleClass('full', isFull).toggleClass('not-full', !isFull)
  2375. .toggleClass('input-active', self.isFocused && !self.isInputHidden)
  2376. .toggleClass('dropdown-active', self.isOpen)
  2377. .toggleClass('has-options', !$.isEmptyObject(self.options))
  2378. .toggleClass('has-items', self.items.length > 0);
  2379. self.$control_input.data('grow', !isFull && !isLocked);
  2380. },
  2381. /**
  2382. * Determines whether or not more items can be added
  2383. * to the control without exceeding the user-defined maximum.
  2384. *
  2385. * @returns {boolean}
  2386. */
  2387. isFull: function() {
  2388. return this.settings.maxItems !== null && this.items.length >= this.settings.maxItems;
  2389. },
  2390. /**
  2391. * Refreshes the original <select> or <input>
  2392. * element to reflect the current state.
  2393. */
  2394. updateOriginalInput: function(opts) {
  2395. var i, n, options, label, self = this;
  2396. opts = opts || {};
  2397. if (self.tagType === TAG_SELECT) {
  2398. options = [];
  2399. for (i = 0, n = self.items.length; i < n; i++) {
  2400. label = self.options[self.items[i]][self.settings.labelField] || '';
  2401. options.push('<option value="' + escape_html(self.items[i]) + '" selected="selected">' + escape_html(label) + '</option>');
  2402. }
  2403. if (!options.length && !this.$input.attr('multiple')) {
  2404. options.push('<option value="" selected="selected"></option>');
  2405. }
  2406. self.$input.html(options.join(''));
  2407. } else {
  2408. self.$input.val(self.getValue());
  2409. self.$input.attr('value',self.$input.val());
  2410. }
  2411. if (self.isSetup) {
  2412. if (!opts.silent) {
  2413. self.trigger('change', self.$input.val());
  2414. }
  2415. }
  2416. },
  2417. /**
  2418. * Shows/hide the input placeholder depending
  2419. * on if there items in the list already.
  2420. */
  2421. updatePlaceholder: function() {
  2422. if (!this.settings.placeholder) return;
  2423. var $input = this.$control_input;
  2424. if (this.items.length) {
  2425. $input.removeAttr('placeholder');
  2426. } else {
  2427. $input.attr('placeholder', this.settings.placeholder);
  2428. }
  2429. $input.triggerHandler('update', {force: true});
  2430. },
  2431. /**
  2432. * Shows the autocomplete dropdown containing
  2433. * the available options.
  2434. */
  2435. open: function() {
  2436. var self = this;
  2437. if (self.isLocked || self.isOpen || (self.settings.mode === 'multi' && self.isFull())) return;
  2438. self.focus();
  2439. self.isOpen = true;
  2440. self.refreshState();
  2441. self.$dropdown.css({visibility: 'hidden', display: 'block'});
  2442. self.positionDropdown();
  2443. self.$dropdown.css({visibility: 'visible'});
  2444. self.trigger('dropdown_open', self.$dropdown);
  2445. },
  2446. /**
  2447. * Closes the autocomplete dropdown menu.
  2448. */
  2449. close: function() {
  2450. var self = this;
  2451. var trigger = self.isOpen;
  2452. if (self.settings.mode === 'single' && self.items.length) {
  2453. self.hideInput();
  2454. }
  2455. self.isOpen = false;
  2456. self.$dropdown.hide();
  2457. self.setActiveOption(null);
  2458. self.refreshState();
  2459. if (trigger) self.trigger('dropdown_close', self.$dropdown);
  2460. },
  2461. /**
  2462. * Calculates and applies the appropriate
  2463. * position of the dropdown.
  2464. */
  2465. positionDropdown: function() {
  2466. var $control = this.$control;
  2467. var offset = this.settings.dropdownParent === 'body' ? $control.offset() : $control.position();
  2468. offset.top += $control.outerHeight(true);
  2469. this.$dropdown.css({
  2470. width : $control.outerWidth(),
  2471. top : offset.top,
  2472. left : offset.left
  2473. });
  2474. },
  2475. /**
  2476. * Resets / clears all selected items
  2477. * from the control.
  2478. *
  2479. * @param {boolean} silent
  2480. */
  2481. clear: function(silent) {
  2482. var self = this;
  2483. if (!self.items.length) return;
  2484. self.$control.children(':not(input)').remove();
  2485. self.items = [];
  2486. self.lastQuery = null;
  2487. self.setCaret(0);
  2488. self.setActiveItem(null);
  2489. self.updatePlaceholder();
  2490. self.updateOriginalInput({silent: silent});
  2491. self.refreshState();
  2492. self.showInput();
  2493. self.trigger('clear');
  2494. },
  2495. /**
  2496. * A helper method for inserting an element
  2497. * at the current caret position.
  2498. *
  2499. * @param {object} $el
  2500. */
  2501. insertAtCaret: function($el) {
  2502. var caret = Math.min(this.caretPos, this.items.length);
  2503. if (caret === 0) {
  2504. this.$control.prepend($el);
  2505. } else {
  2506. $(this.$control[0].childNodes[caret]).before($el);
  2507. }
  2508. this.setCaret(caret + 1);
  2509. },
  2510. /**
  2511. * Removes the current selected item(s).
  2512. *
  2513. * @param {object} e (optional)
  2514. * @returns {boolean}
  2515. */
  2516. deleteSelection: function(e) {
  2517. var i, n, direction, selection, values, caret, option_select, $option_select, $tail;
  2518. var self = this;
  2519. direction = (e && e.keyCode === KEY_BACKSPACE) ? -1 : 1;
  2520. selection = getSelection(self.$control_input[0]);
  2521. if (self.$activeOption && !self.settings.hideSelected) {
  2522. option_select = self.getAdjacentOption(self.$activeOption, -1).attr('data-value');
  2523. }
  2524. // determine items that will be removed
  2525. values = [];
  2526. if (self.$activeItems.length) {
  2527. $tail = self.$control.children('.active:' + (direction > 0 ? 'last' : 'first'));
  2528. caret = self.$control.children(':not(input)').index($tail);
  2529. if (direction > 0) { caret++; }
  2530. for (i = 0, n = self.$activeItems.length; i < n; i++) {
  2531. values.push($(self.$activeItems[i]).attr('data-value'));
  2532. }
  2533. if (e) {
  2534. e.preventDefault();
  2535. e.stopPropagation();
  2536. }
  2537. } else if ((self.isFocused || self.settings.mode === 'single') && self.items.length) {
  2538. if (direction < 0 && selection.start === 0 && selection.length === 0) {
  2539. values.push(self.items[self.caretPos - 1]);
  2540. } else if (direction > 0 && selection.start === self.$control_input.val().length) {
  2541. values.push(self.items[self.caretPos]);
  2542. }
  2543. }
  2544. // allow the callback to abort
  2545. if (!values.length || (typeof self.settings.onDelete === 'function' && self.settings.onDelete.apply(self, [values]) === false)) {
  2546. return false;
  2547. }
  2548. // perform removal
  2549. if (typeof caret !== 'undefined') {
  2550. self.setCaret(caret);
  2551. }
  2552. while (values.length) {
  2553. self.removeItem(values.pop());
  2554. }
  2555. self.showInput();
  2556. self.positionDropdown();
  2557. self.refreshOptions(true);
  2558. // select previous option
  2559. if (option_select) {
  2560. $option_select = self.getOption(option_select);
  2561. if ($option_select.length) {
  2562. self.setActiveOption($option_select);
  2563. }
  2564. }
  2565. return true;
  2566. },
  2567. /**
  2568. * Selects the previous / next item (depending
  2569. * on the `direction` argument).
  2570. *
  2571. * > 0 - right
  2572. * < 0 - left
  2573. *
  2574. * @param {int} direction
  2575. * @param {object} e (optional)
  2576. */
  2577. advanceSelection: function(direction, e) {
  2578. var tail, selection, idx, valueLength, cursorAtEdge, $tail;
  2579. var self = this;
  2580. if (direction === 0) return;
  2581. if (self.rtl) direction *= -1;
  2582. tail = direction > 0 ? 'last' : 'first';
  2583. selection = getSelection(self.$control_input[0]);
  2584. if (self.isFocused && !self.isInputHidden) {
  2585. valueLength = self.$control_input.val().length;
  2586. cursorAtEdge = direction < 0
  2587. ? selection.start === 0 && selection.length === 0
  2588. : selection.start === valueLength;
  2589. if (cursorAtEdge && !valueLength) {
  2590. self.advanceCaret(direction, e);
  2591. }
  2592. } else {
  2593. $tail = self.$control.children('.active:' + tail);
  2594. if ($tail.length) {
  2595. idx = self.$control.children(':not(input)').index($tail);
  2596. self.setActiveItem(null);
  2597. self.setCaret(direction > 0 ? idx + 1 : idx);
  2598. }
  2599. }
  2600. },
  2601. /**
  2602. * Moves the caret left / right.
  2603. *
  2604. * @param {int} direction
  2605. * @param {object} e (optional)
  2606. */
  2607. advanceCaret: function(direction, e) {
  2608. var self = this, fn, $adj;
  2609. if (direction === 0) return;
  2610. fn = direction > 0 ? 'next' : 'prev';
  2611. if (self.isShiftDown) {
  2612. $adj = self.$control_input[fn]();
  2613. if ($adj.length) {
  2614. self.hideInput();
  2615. self.setActiveItem($adj);
  2616. e && e.preventDefault();
  2617. }
  2618. } else {
  2619. self.setCaret(self.caretPos + direction);
  2620. }
  2621. },
  2622. /**
  2623. * Moves the caret to the specified index.
  2624. *
  2625. * @param {int} i
  2626. */
  2627. setCaret: function(i) {
  2628. var self = this;
  2629. if (self.settings.mode === 'single') {
  2630. i = self.items.length;
  2631. } else {
  2632. i = Math.max(0, Math.min(self.items.length, i));
  2633. }
  2634. if(!self.isPending) {
  2635. // the input must be moved by leaving it in place and moving the
  2636. // siblings, due to the fact that focus cannot be restored once lost
  2637. // on mobile webkit devices
  2638. var j, n, fn, $children, $child;
  2639. $children = self.$control.children(':not(input)');
  2640. for (j = 0, n = $children.length; j < n; j++) {
  2641. $child = $($children[j]).detach();
  2642. if (j < i) {
  2643. self.$control_input.before($child);
  2644. } else {
  2645. self.$control.append($child);
  2646. }
  2647. }
  2648. }
  2649. self.caretPos = i;
  2650. },
  2651. /**
  2652. * Disables user input on the control. Used while
  2653. * items are being asynchronously created.
  2654. */
  2655. lock: function() {
  2656. this.close();
  2657. this.isLocked = true;
  2658. this.refreshState();
  2659. },
  2660. /**
  2661. * Re-enables user input on the control.
  2662. */
  2663. unlock: function() {
  2664. this.isLocked = false;
  2665. this.refreshState();
  2666. },
  2667. /**
  2668. * Disables user input on the control completely.
  2669. * While disabled, it cannot receive focus.
  2670. */
  2671. disable: function() {
  2672. var self = this;
  2673. self.$input.prop('disabled', true);
  2674. self.$control_input.prop('disabled', true).prop('tabindex', -1);
  2675. self.isDisabled = true;
  2676. self.lock();
  2677. },
  2678. /**
  2679. * Enables the control so that it can respond
  2680. * to focus and user input.
  2681. */
  2682. enable: function() {
  2683. var self = this;
  2684. self.$input.prop('disabled', false);
  2685. self.$control_input.prop('disabled', false).prop('tabindex', self.tabIndex);
  2686. self.isDisabled = false;
  2687. self.unlock();
  2688. },
  2689. /**
  2690. * Completely destroys the control and
  2691. * unbinds all event listeners so that it can
  2692. * be garbage collected.
  2693. */
  2694. destroy: function() {
  2695. var self = this;
  2696. var eventNS = self.eventNS;
  2697. var revertSettings = self.revertSettings;
  2698. self.trigger('destroy');
  2699. self.off();
  2700. self.$wrapper.remove();
  2701. self.$dropdown.remove();
  2702. self.$input
  2703. .html('')
  2704. .append(revertSettings.$children)
  2705. .removeAttr('tabindex')
  2706. .removeClass('selectized')
  2707. .attr({tabindex: revertSettings.tabindex})
  2708. .show();
  2709. self.$control_input.removeData('grow');
  2710. self.$input.removeData('selectize');
  2711. $(window).off(eventNS);
  2712. $(document).off(eventNS);
  2713. $(document.body).off(eventNS);
  2714. delete self.$input[0].selectize;
  2715. },
  2716. /**
  2717. * A helper method for rendering "item" and
  2718. * "option" templates, given the data.
  2719. *
  2720. * @param {string} templateName
  2721. * @param {object} data
  2722. * @returns {string}
  2723. */
  2724. render: function(templateName, data) {
  2725. var value, id, label;
  2726. var html = '';
  2727. var cache = false;
  2728. var self = this;
  2729. var regex_tag = /^[\t \r\n]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i;
  2730. if (templateName === 'option' || templateName === 'item') {
  2731. value = hash_key(data[self.settings.valueField]);
  2732. cache = !!value;
  2733. }
  2734. // pull markup from cache if it exists
  2735. if (cache) {
  2736. if (!isset(self.renderCache[templateName])) {
  2737. self.renderCache[templateName] = {};
  2738. }
  2739. if (self.renderCache[templateName].hasOwnProperty(value)) {
  2740. return self.renderCache[templateName][value];
  2741. }
  2742. }
  2743. // render markup
  2744. html = self.settings.render[templateName].apply(this, [data, escape_html]);
  2745. // add mandatory attributes
  2746. if (templateName === 'option' || templateName === 'option_create') {
  2747. html = html.replace(regex_tag, '<$1 data-selectable');
  2748. }
  2749. if (templateName === 'optgroup') {
  2750. id = data[self.settings.optgroupValueField] || '';
  2751. html = html.replace(regex_tag, '<$1 data-group="' + escape_replace(escape_html(id)) + '"');
  2752. }
  2753. if (templateName === 'option' || templateName === 'item') {
  2754. html = html.replace(regex_tag, '<$1 data-value="' + escape_replace(escape_html(value || '')) + '"');
  2755. }
  2756. // update cache
  2757. if (cache) {
  2758. self.renderCache[templateName][value] = html;
  2759. }
  2760. return html;
  2761. },
  2762. /**
  2763. * Clears the render cache for a template. If
  2764. * no template is given, clears all render
  2765. * caches.
  2766. *
  2767. * @param {string} templateName
  2768. */
  2769. clearCache: function(templateName) {
  2770. var self = this;
  2771. if (typeof templateName === 'undefined') {
  2772. self.renderCache = {};
  2773. } else {
  2774. delete self.renderCache[templateName];
  2775. }
  2776. },
  2777. /**
  2778. * Determines whether or not to display the
  2779. * create item prompt, given a user input.
  2780. *
  2781. * @param {string} input
  2782. * @return {boolean}
  2783. */
  2784. canCreate: function(input) {
  2785. var self = this;
  2786. if (!self.settings.create) return false;
  2787. var filter = self.settings.createFilter;
  2788. return input.length
  2789. && (typeof filter !== 'function' || filter.apply(self, [input]))
  2790. && (typeof filter !== 'string' || new RegExp(filter).test(input))
  2791. && (!(filter instanceof RegExp) || filter.test(input));
  2792. }
  2793. });
  2794. Selectize.count = 0;
  2795. Selectize.defaults = {
  2796. options: [],
  2797. optgroups: [],
  2798. plugins: [],
  2799. delimiter: ',',
  2800. splitOn: null, // regexp or string for splitting up values from a paste command
  2801. persist: true,
  2802. diacritics: true,
  2803. create: false,
  2804. createOnBlur: false,
  2805. createFilter: null,
  2806. highlight: true,
  2807. openOnFocus: true,
  2808. maxOptions: 1000,
  2809. maxItems: null,
  2810. hideSelected: null,
  2811. addPrecedence: false,
  2812. selectOnTab: false,
  2813. preload: false,
  2814. allowEmptyOption: false,
  2815. closeAfterSelect: false,
  2816. scrollDuration: 60,
  2817. loadThrottle: 300,
  2818. loadingClass: 'loading',
  2819. dataAttr: 'data-data',
  2820. optgroupField: 'optgroup',
  2821. valueField: 'value',
  2822. labelField: 'text',
  2823. optgroupLabelField: 'label',
  2824. optgroupValueField: 'value',
  2825. lockOptgroupOrder: false,
  2826. sortField: '$order',
  2827. searchField: ['text'],
  2828. searchConjunction: 'and',
  2829. mode: null,
  2830. wrapperClass: 'selectize-control',
  2831. inputClass: 'selectize-input',
  2832. dropdownClass: 'selectize-dropdown',
  2833. dropdownContentClass: 'selectize-dropdown-content',
  2834. dropdownParent: null,
  2835. copyClassesToDropdown: true,
  2836. /*
  2837. load : null, // function(query, callback) { ... }
  2838. score : null, // function(search) { ... }
  2839. onInitialize : null, // function() { ... }
  2840. onChange : null, // function(value) { ... }
  2841. onItemAdd : null, // function(value, $item) { ... }
  2842. onItemRemove : null, // function(value) { ... }
  2843. onClear : null, // function() { ... }
  2844. onOptionAdd : null, // function(value, data) { ... }
  2845. onOptionRemove : null, // function(value) { ... }
  2846. onOptionClear : null, // function() { ... }
  2847. onOptionGroupAdd : null, // function(id, data) { ... }
  2848. onOptionGroupRemove : null, // function(id) { ... }
  2849. onOptionGroupClear : null, // function() { ... }
  2850. onDropdownOpen : null, // function($dropdown) { ... }
  2851. onDropdownClose : null, // function($dropdown) { ... }
  2852. onType : null, // function(str) { ... }
  2853. onDelete : null, // function(values) { ... }
  2854. */
  2855. render: {
  2856. /*
  2857. item: null,
  2858. optgroup: null,
  2859. optgroup_header: null,
  2860. option: null,
  2861. option_create: null
  2862. */
  2863. }
  2864. };
  2865. $.fn.selectize = function(settings_user) {
  2866. var defaults = $.fn.selectize.defaults;
  2867. var settings = $.extend({}, defaults, settings_user);
  2868. var attr_data = settings.dataAttr;
  2869. var field_label = settings.labelField;
  2870. var field_value = settings.valueField;
  2871. var field_optgroup = settings.optgroupField;
  2872. var field_optgroup_label = settings.optgroupLabelField;
  2873. var field_optgroup_value = settings.optgroupValueField;
  2874. /**
  2875. * Initializes selectize from a <input type="text"> element.
  2876. *
  2877. * @param {object} $input
  2878. * @param {object} settings_element
  2879. */
  2880. var init_textbox = function($input, settings_element) {
  2881. var i, n, values, option;
  2882. var data_raw = $input.attr(attr_data);
  2883. if (!data_raw) {
  2884. var value = $.trim($input.val() || '');
  2885. if (!settings.allowEmptyOption && !value.length) return;
  2886. values = value.split(settings.delimiter);
  2887. for (i = 0, n = values.length; i < n; i++) {
  2888. option = {};
  2889. option[field_label] = values[i];
  2890. option[field_value] = values[i];
  2891. settings_element.options.push(option);
  2892. }
  2893. settings_element.items = values;
  2894. } else {
  2895. settings_element.options = JSON.parse(data_raw);
  2896. for (i = 0, n = settings_element.options.length; i < n; i++) {
  2897. settings_element.items.push(settings_element.options[i][field_value]);
  2898. }
  2899. }
  2900. };
  2901. /**
  2902. * Initializes selectize from a <select> element.
  2903. *
  2904. * @param {object} $input
  2905. * @param {object} settings_element
  2906. */
  2907. var init_select = function($input, settings_element) {
  2908. var i, n, tagName, $children, order = 0;
  2909. var options = settings_element.options;
  2910. var optionsMap = {};
  2911. var readData = function($el) {
  2912. var data = attr_data && $el.attr(attr_data);
  2913. if (typeof data === 'string' && data.length) {
  2914. return JSON.parse(data);
  2915. }
  2916. return null;
  2917. };
  2918. var addOption = function($option, group) {
  2919. $option = $($option);
  2920. var value = hash_key($option.attr('value'));
  2921. if (!value && !settings.allowEmptyOption) return;
  2922. // if the option already exists, it's probably been
  2923. // duplicated in another optgroup. in this case, push
  2924. // the current group to the "optgroup" property on the
  2925. // existing option so that it's rendered in both places.
  2926. if (optionsMap.hasOwnProperty(value)) {
  2927. if (group) {
  2928. var arr = optionsMap[value][field_optgroup];
  2929. if (!arr) {
  2930. optionsMap[value][field_optgroup] = group;
  2931. } else if (!$.isArray(arr)) {
  2932. optionsMap[value][field_optgroup] = [arr, group];
  2933. } else {
  2934. arr.push(group);
  2935. }
  2936. }
  2937. return;
  2938. }
  2939. var option = readData($option) || {};
  2940. option[field_label] = option[field_label] || $option.text();
  2941. option[field_value] = option[field_value] || value;
  2942. option[field_optgroup] = option[field_optgroup] || group;
  2943. optionsMap[value] = option;
  2944. options.push(option);
  2945. if ($option.is(':selected')) {
  2946. settings_element.items.push(value);
  2947. }
  2948. };
  2949. var addGroup = function($optgroup) {
  2950. var i, n, id, optgroup, $options;
  2951. $optgroup = $($optgroup);
  2952. id = $optgroup.attr('label');
  2953. if (id) {
  2954. optgroup = readData($optgroup) || {};
  2955. optgroup[field_optgroup_label] = id;
  2956. optgroup[field_optgroup_value] = id;
  2957. settings_element.optgroups.push(optgroup);
  2958. }
  2959. $options = $('option', $optgroup);
  2960. for (i = 0, n = $options.length; i < n; i++) {
  2961. addOption($options[i], id);
  2962. }
  2963. };
  2964. settings_element.maxItems = $input.attr('multiple') ? null : 1;
  2965. $children = $input.children();
  2966. for (i = 0, n = $children.length; i < n; i++) {
  2967. tagName = $children[i].tagName.toLowerCase();
  2968. if (tagName === 'optgroup') {
  2969. addGroup($children[i]);
  2970. } else if (tagName === 'option') {
  2971. addOption($children[i]);
  2972. }
  2973. }
  2974. };
  2975. return this.each(function() {
  2976. if (this.selectize) return;
  2977. var instance;
  2978. var $input = $(this);
  2979. var tag_name = this.tagName.toLowerCase();
  2980. var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder');
  2981. if (!placeholder && !settings.allowEmptyOption) {
  2982. placeholder = $input.children('option[value=""]').text();
  2983. }
  2984. var settings_element = {
  2985. 'placeholder' : placeholder,
  2986. 'options' : [],
  2987. 'optgroups' : [],
  2988. 'items' : []
  2989. };
  2990. if (tag_name === 'select') {
  2991. init_select($input, settings_element);
  2992. } else {
  2993. init_textbox($input, settings_element);
  2994. }
  2995. instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings_user));
  2996. });
  2997. };
  2998. $.fn.selectize.defaults = Selectize.defaults;
  2999. $.fn.selectize.support = {
  3000. validity: SUPPORTS_VALIDITY_API
  3001. };
  3002. Selectize.define('drag_drop', function(options) {
  3003. if (!$.fn.sortable) throw new Error('The "drag_drop" plugin requires jQuery UI "sortable".');
  3004. if (this.settings.mode !== 'multi') return;
  3005. var self = this;
  3006. self.lock = (function() {
  3007. var original = self.lock;
  3008. return function() {
  3009. var sortable = self.$control.data('sortable');
  3010. if (sortable) sortable.disable();
  3011. return original.apply(self, arguments);
  3012. };
  3013. })();
  3014. self.unlock = (function() {
  3015. var original = self.unlock;
  3016. return function() {
  3017. var sortable = self.$control.data('sortable');
  3018. if (sortable) sortable.enable();
  3019. return original.apply(self, arguments);
  3020. };
  3021. })();
  3022. self.setup = (function() {
  3023. var original = self.setup;
  3024. return function() {
  3025. original.apply(this, arguments);
  3026. var $control = self.$control.sortable({
  3027. items: '[data-value]',
  3028. forcePlaceholderSize: true,
  3029. disabled: self.isLocked,
  3030. start: function(e, ui) {
  3031. ui.placeholder.css('width', ui.helper.css('width'));
  3032. $control.css({overflow: 'visible'});
  3033. },
  3034. stop: function() {
  3035. $control.css({overflow: 'hidden'});
  3036. var active = self.$activeItems ? self.$activeItems.slice() : null;
  3037. var values = [];
  3038. $control.children('[data-value]').each(function() {
  3039. values.push($(this).attr('data-value'));
  3040. });
  3041. self.setValue(values);
  3042. self.setActiveItem(active);
  3043. }
  3044. });
  3045. };
  3046. })();
  3047. });
  3048. Selectize.define('dropdown_header', function(options) {
  3049. var self = this;
  3050. options = $.extend({
  3051. title : 'Untitled',
  3052. headerClass : 'selectize-dropdown-header',
  3053. titleRowClass : 'selectize-dropdown-header-title',
  3054. labelClass : 'selectize-dropdown-header-label',
  3055. closeClass : 'selectize-dropdown-header-close',
  3056. html: function(data) {
  3057. return (
  3058. '<div class="' + data.headerClass + '">' +
  3059. '<div class="' + data.titleRowClass + '">' +
  3060. '<span class="' + data.labelClass + '">' + data.title + '</span>' +
  3061. '<a href="javascript:void(0)" class="' + data.closeClass + '">&times;</a>' +
  3062. '</div>' +
  3063. '</div>'
  3064. );
  3065. }
  3066. }, options);
  3067. self.setup = (function() {
  3068. var original = self.setup;
  3069. return function() {
  3070. original.apply(self, arguments);
  3071. self.$dropdown_header = $(options.html(options));
  3072. self.$dropdown.prepend(self.$dropdown_header);
  3073. };
  3074. })();
  3075. });
  3076. Selectize.define('optgroup_columns', function(options) {
  3077. var self = this;
  3078. options = $.extend({
  3079. equalizeWidth : true,
  3080. equalizeHeight : true
  3081. }, options);
  3082. this.getAdjacentOption = function($option, direction) {
  3083. var $options = $option.closest('[data-group]').find('[data-selectable]');
  3084. var index = $options.index($option) + direction;
  3085. return index >= 0 && index < $options.length ? $options.eq(index) : $();
  3086. };
  3087. this.onKeyDown = (function() {
  3088. var original = self.onKeyDown;
  3089. return function(e) {
  3090. var index, $option, $options, $optgroup;
  3091. if (this.isOpen && (e.keyCode === KEY_LEFT || e.keyCode === KEY_RIGHT)) {
  3092. self.ignoreHover = true;
  3093. $optgroup = this.$activeOption.closest('[data-group]');
  3094. index = $optgroup.find('[data-selectable]').index(this.$activeOption);
  3095. if(e.keyCode === KEY_LEFT) {
  3096. $optgroup = $optgroup.prev('[data-group]');
  3097. } else {
  3098. $optgroup = $optgroup.next('[data-group]');
  3099. }
  3100. $options = $optgroup.find('[data-selectable]');
  3101. $option = $options.eq(Math.min($options.length - 1, index));
  3102. if ($option.length) {
  3103. this.setActiveOption($option);
  3104. }
  3105. return;
  3106. }
  3107. return original.apply(this, arguments);
  3108. };
  3109. })();
  3110. var getScrollbarWidth = function() {
  3111. var div;
  3112. var width = getScrollbarWidth.width;
  3113. var doc = document;
  3114. if (typeof width === 'undefined') {
  3115. div = doc.createElement('div');
  3116. div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>';
  3117. div = div.firstChild;
  3118. doc.body.appendChild(div);
  3119. width = getScrollbarWidth.width = div.offsetWidth - div.clientWidth;
  3120. doc.body.removeChild(div);
  3121. }
  3122. return width;
  3123. };
  3124. var equalizeSizes = function() {
  3125. var i, n, height_max, width, width_last, width_parent, $optgroups;
  3126. $optgroups = $('[data-group]', self.$dropdown_content);
  3127. n = $optgroups.length;
  3128. if (!n || !self.$dropdown_content.width()) return;
  3129. if (options.equalizeHeight) {
  3130. height_max = 0;
  3131. for (i = 0; i < n; i++) {
  3132. height_max = Math.max(height_max, $optgroups.eq(i).height());
  3133. }
  3134. $optgroups.css({height: height_max});
  3135. }
  3136. if (options.equalizeWidth) {
  3137. width_parent = self.$dropdown_content.innerWidth() - getScrollbarWidth();
  3138. width = Math.round(width_parent / n);
  3139. $optgroups.css({width: width});
  3140. if (n > 1) {
  3141. width_last = width_parent - width * (n - 1);
  3142. $optgroups.eq(n - 1).css({width: width_last});
  3143. }
  3144. }
  3145. };
  3146. if (options.equalizeHeight || options.equalizeWidth) {
  3147. hook.after(this, 'positionDropdown', equalizeSizes);
  3148. hook.after(this, 'refreshOptions', equalizeSizes);
  3149. }
  3150. });
  3151. Selectize.define('remove_button', function(options) {
  3152. if (this.settings.mode === 'single') return;
  3153. options = $.extend({
  3154. label : '&times;',
  3155. title : 'Remove',
  3156. className : 'remove',
  3157. append : true
  3158. }, options);
  3159. var self = this;
  3160. var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>';
  3161. /**
  3162. * Appends an element as a child (with raw HTML).
  3163. *
  3164. * @param {string} html_container
  3165. * @param {string} html_element
  3166. * @return {string}
  3167. */
  3168. var append = function(html_container, html_element) {
  3169. var pos = html_container.search(/(<\/[^>]+>\s*)$/);
  3170. return html_container.substring(0, pos) + html_element + html_container.substring(pos);
  3171. };
  3172. this.setup = (function() {
  3173. var original = self.setup;
  3174. return function() {
  3175. // override the item rendering method to add the button to each
  3176. if (options.append) {
  3177. var render_item = self.settings.render.item;
  3178. self.settings.render.item = function(data) {
  3179. return append(render_item.apply(this, arguments), html);
  3180. };
  3181. }
  3182. original.apply(this, arguments);
  3183. // add event listener
  3184. this.$control.on('click', '.' + options.className, function(e) {
  3185. e.preventDefault();
  3186. if (self.isLocked) return;
  3187. var $item = $(e.currentTarget).parent();
  3188. self.setActiveItem($item);
  3189. if (self.deleteSelection()) {
  3190. self.setCaret(self.items.length);
  3191. }
  3192. });
  3193. };
  3194. })();
  3195. });
  3196. Selectize.define('restore_on_backspace', function(options) {
  3197. var self = this;
  3198. options.text = options.text || function(option) {
  3199. return option[this.settings.labelField];
  3200. };
  3201. this.onKeyDown = (function() {
  3202. var original = self.onKeyDown;
  3203. return function(e) {
  3204. var index, option;
  3205. if (e.keyCode === KEY_BACKSPACE && this.$control_input.val() === '' && !this.$activeItems.length) {
  3206. index = this.caretPos - 1;
  3207. if (index >= 0 && index < this.items.length) {
  3208. option = this.options[this.items[index]];
  3209. if (this.deleteSelection(e)) {
  3210. this.setTextboxValue(options.text.apply(this, [option]));
  3211. this.refreshOptions(true);
  3212. }
  3213. e.preventDefault();
  3214. return;
  3215. }
  3216. }
  3217. return original.apply(this, arguments);
  3218. };
  3219. })();
  3220. });
  3221. return Selectize;
  3222. }));