selectize.js 98 KB

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