selectize.js 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668
  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.0)
  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();
  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)) 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();
  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. var optionsMap = {};
  2875. /**
  2876. * Initializes selectize from a <input type="text"> element.
  2877. *
  2878. * @param {object} $input
  2879. * @param {object} settings_element
  2880. */
  2881. var init_textbox = function($input, settings_element) {
  2882. var i, n, values, option;
  2883. var data_raw = $input.attr(attr_data);
  2884. if (!data_raw) {
  2885. var value = $.trim($input.val() || '');
  2886. if (!settings.allowEmptyOption && !value.length) return;
  2887. values = value.split(settings.delimiter);
  2888. for (i = 0, n = values.length; i < n; i++) {
  2889. option = {};
  2890. option[field_label] = values[i];
  2891. option[field_value] = values[i];
  2892. settings_element.options.push(option);
  2893. }
  2894. settings_element.items = values;
  2895. } else {
  2896. settings_element.options = JSON.parse(data_raw);
  2897. for (i = 0, n = settings_element.options.length; i < n; i++) {
  2898. settings_element.items.push(settings_element.options[i][field_value]);
  2899. }
  2900. }
  2901. };
  2902. /**
  2903. * Initializes selectize from a <select> element.
  2904. *
  2905. * @param {object} $input
  2906. * @param {object} settings_element
  2907. */
  2908. var init_select = function($input, settings_element) {
  2909. var i, n, tagName, $children, order = 0;
  2910. var options = settings_element.options;
  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. }));