select2.js 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
  2. * @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
  3. * Available via the MIT or new BSD license.
  4. * see: http://github.com/jrburke/almond for details
  5. */
  6. //Going sloppy to avoid 'use strict' string cost, but strict practices should
  7. //be followed.
  8. /*jslint sloppy: true */
  9. /*global setTimeout: false */
  10. var requirejs, require, define;
  11. (function (undef) {
  12. var main, req, makeMap, handlers,
  13. defined = {},
  14. waiting = {},
  15. config = {},
  16. defining = {},
  17. hasOwn = Object.prototype.hasOwnProperty,
  18. aps = [].slice,
  19. jsSuffixRegExp = /\.js$/;
  20. function hasProp(obj, prop) {
  21. return hasOwn.call(obj, prop);
  22. }
  23. /**
  24. * Given a relative module name, like ./something, normalize it to
  25. * a real name that can be mapped to a path.
  26. * @param {String} name the relative name
  27. * @param {String} baseName a real name that the name arg is relative
  28. * to.
  29. * @returns {String} normalized name
  30. */
  31. function normalize(name, baseName) {
  32. var nameParts, nameSegment, mapValue, foundMap, lastIndex,
  33. foundI, foundStarMap, starI, i, j, part,
  34. baseParts = baseName && baseName.split("/"),
  35. map = config.map,
  36. starMap = (map && map['*']) || {};
  37. //Adjust any relative paths.
  38. if (name && name.charAt(0) === ".") {
  39. //If have a base name, try to normalize against it,
  40. //otherwise, assume it is a top-level require that will
  41. //be relative to baseUrl in the end.
  42. if (baseName) {
  43. //Convert baseName to array, and lop off the last part,
  44. //so that . matches that "directory" and not name of the baseName's
  45. //module. For instance, baseName of "one/two/three", maps to
  46. //"one/two/three.js", but we want the directory, "one/two" for
  47. //this normalization.
  48. baseParts = baseParts.slice(0, baseParts.length - 1);
  49. name = name.split('/');
  50. lastIndex = name.length - 1;
  51. // Node .js allowance:
  52. if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
  53. name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
  54. }
  55. name = baseParts.concat(name);
  56. //start trimDots
  57. for (i = 0; i < name.length; i += 1) {
  58. part = name[i];
  59. if (part === ".") {
  60. name.splice(i, 1);
  61. i -= 1;
  62. } else if (part === "..") {
  63. if (i === 1 && (name[2] === '..' || name[0] === '..')) {
  64. //End of the line. Keep at least one non-dot
  65. //path segment at the front so it can be mapped
  66. //correctly to disk. Otherwise, there is likely
  67. //no path mapping for a path starting with '..'.
  68. //This can still fail, but catches the most reasonable
  69. //uses of ..
  70. break;
  71. } else if (i > 0) {
  72. name.splice(i - 1, 2);
  73. i -= 2;
  74. }
  75. }
  76. }
  77. //end trimDots
  78. name = name.join("/");
  79. } else if (name.indexOf('./') === 0) {
  80. // No baseName, so this is ID is resolved relative
  81. // to baseUrl, pull off the leading dot.
  82. name = name.substring(2);
  83. }
  84. }
  85. //Apply map config if available.
  86. if ((baseParts || starMap) && map) {
  87. nameParts = name.split('/');
  88. for (i = nameParts.length; i > 0; i -= 1) {
  89. nameSegment = nameParts.slice(0, i).join("/");
  90. if (baseParts) {
  91. //Find the longest baseName segment match in the config.
  92. //So, do joins on the biggest to smallest lengths of baseParts.
  93. for (j = baseParts.length; j > 0; j -= 1) {
  94. mapValue = map[baseParts.slice(0, j).join('/')];
  95. //baseName segment has config, find if it has one for
  96. //this name.
  97. if (mapValue) {
  98. mapValue = mapValue[nameSegment];
  99. if (mapValue) {
  100. //Match, update name to the new value.
  101. foundMap = mapValue;
  102. foundI = i;
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. if (foundMap) {
  109. break;
  110. }
  111. //Check for a star map match, but just hold on to it,
  112. //if there is a shorter segment match later in a matching
  113. //config, then favor over this star map.
  114. if (!foundStarMap && starMap && starMap[nameSegment]) {
  115. foundStarMap = starMap[nameSegment];
  116. starI = i;
  117. }
  118. }
  119. if (!foundMap && foundStarMap) {
  120. foundMap = foundStarMap;
  121. foundI = starI;
  122. }
  123. if (foundMap) {
  124. nameParts.splice(0, foundI, foundMap);
  125. name = nameParts.join('/');
  126. }
  127. }
  128. return name;
  129. }
  130. function makeRequire(relName, forceSync) {
  131. return function () {
  132. //A version of a require function that passes a moduleName
  133. //value for items that may need to
  134. //look up paths relative to the moduleName
  135. return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
  136. };
  137. }
  138. function makeNormalize(relName) {
  139. return function (name) {
  140. return normalize(name, relName);
  141. };
  142. }
  143. function makeLoad(depName) {
  144. return function (value) {
  145. defined[depName] = value;
  146. };
  147. }
  148. function callDep(name) {
  149. if (hasProp(waiting, name)) {
  150. var args = waiting[name];
  151. delete waiting[name];
  152. defining[name] = true;
  153. main.apply(undef, args);
  154. }
  155. if (!hasProp(defined, name) && !hasProp(defining, name)) {
  156. throw new Error('No ' + name);
  157. }
  158. return defined[name];
  159. }
  160. //Turns a plugin!resource to [plugin, resource]
  161. //with the plugin being undefined if the name
  162. //did not have a plugin prefix.
  163. function splitPrefix(name) {
  164. var prefix,
  165. index = name ? name.indexOf('!') : -1;
  166. if (index > -1) {
  167. prefix = name.substring(0, index);
  168. name = name.substring(index + 1, name.length);
  169. }
  170. return [prefix, name];
  171. }
  172. /**
  173. * Makes a name map, normalizing the name, and using a plugin
  174. * for normalization if necessary. Grabs a ref to plugin
  175. * too, as an optimization.
  176. */
  177. makeMap = function (name, relName) {
  178. var plugin,
  179. parts = splitPrefix(name),
  180. prefix = parts[0];
  181. name = parts[1];
  182. if (prefix) {
  183. prefix = normalize(prefix, relName);
  184. plugin = callDep(prefix);
  185. }
  186. //Normalize according
  187. if (prefix) {
  188. if (plugin && plugin.normalize) {
  189. name = plugin.normalize(name, makeNormalize(relName));
  190. } else {
  191. name = normalize(name, relName);
  192. }
  193. } else {
  194. name = normalize(name, relName);
  195. parts = splitPrefix(name);
  196. prefix = parts[0];
  197. name = parts[1];
  198. if (prefix) {
  199. plugin = callDep(prefix);
  200. }
  201. }
  202. //Using ridiculous property names for space reasons
  203. return {
  204. f: prefix ? prefix + '!' + name : name, //fullName
  205. n: name,
  206. pr: prefix,
  207. p: plugin
  208. };
  209. };
  210. function makeConfig(name) {
  211. return function () {
  212. return (config && config.config && config.config[name]) || {};
  213. };
  214. }
  215. handlers = {
  216. require: function (name) {
  217. return makeRequire(name);
  218. },
  219. exports: function (name) {
  220. var e = defined[name];
  221. if (typeof e !== 'undefined') {
  222. return e;
  223. } else {
  224. return (defined[name] = {});
  225. }
  226. },
  227. module: function (name) {
  228. return {
  229. id: name,
  230. uri: '',
  231. exports: defined[name],
  232. config: makeConfig(name)
  233. };
  234. }
  235. };
  236. main = function (name, deps, callback, relName) {
  237. var cjsModule, depName, ret, map, i,
  238. args = [],
  239. callbackType = typeof callback,
  240. usingExports;
  241. //Use name if no relName
  242. relName = relName || name;
  243. //Call the callback to define the module, if necessary.
  244. if (callbackType === 'undefined' || callbackType === 'function') {
  245. //Pull out the defined dependencies and pass the ordered
  246. //values to the callback.
  247. //Default to [require, exports, module] if no deps
  248. deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
  249. for (i = 0; i < deps.length; i += 1) {
  250. map = makeMap(deps[i], relName);
  251. depName = map.f;
  252. //Fast path CommonJS standard dependencies.
  253. if (depName === "require") {
  254. args[i] = handlers.require(name);
  255. } else if (depName === "exports") {
  256. //CommonJS module spec 1.1
  257. args[i] = handlers.exports(name);
  258. usingExports = true;
  259. } else if (depName === "module") {
  260. //CommonJS module spec 1.1
  261. cjsModule = args[i] = handlers.module(name);
  262. } else if (hasProp(defined, depName) ||
  263. hasProp(waiting, depName) ||
  264. hasProp(defining, depName)) {
  265. args[i] = callDep(depName);
  266. } else if (map.p) {
  267. map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
  268. args[i] = defined[depName];
  269. } else {
  270. throw new Error(name + ' missing ' + depName);
  271. }
  272. }
  273. ret = callback ? callback.apply(defined[name], args) : undefined;
  274. if (name) {
  275. //If setting exports via "module" is in play,
  276. //favor that over return value and exports. After that,
  277. //favor a non-undefined return value over exports use.
  278. if (cjsModule && cjsModule.exports !== undef &&
  279. cjsModule.exports !== defined[name]) {
  280. defined[name] = cjsModule.exports;
  281. } else if (ret !== undef || !usingExports) {
  282. //Use the return value from the function.
  283. defined[name] = ret;
  284. }
  285. }
  286. } else if (name) {
  287. //May just be an object definition for the module. Only
  288. //worry about defining if have a module name.
  289. defined[name] = callback;
  290. }
  291. };
  292. requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
  293. if (typeof deps === "string") {
  294. if (handlers[deps]) {
  295. //callback in this case is really relName
  296. return handlers[deps](callback);
  297. }
  298. //Just return the module wanted. In this scenario, the
  299. //deps arg is the module name, and second arg (if passed)
  300. //is just the relName.
  301. //Normalize module name, if it contains . or ..
  302. return callDep(makeMap(deps, callback).f);
  303. } else if (!deps.splice) {
  304. //deps is a config object, not an array.
  305. config = deps;
  306. if (config.deps) {
  307. req(config.deps, config.callback);
  308. }
  309. if (!callback) {
  310. return;
  311. }
  312. if (callback.splice) {
  313. //callback is an array, which means it is a dependency list.
  314. //Adjust args if there are dependencies
  315. deps = callback;
  316. callback = relName;
  317. relName = null;
  318. } else {
  319. deps = undef;
  320. }
  321. }
  322. //Support require(['a'])
  323. callback = callback || function () {};
  324. //If relName is a function, it is an errback handler,
  325. //so remove it.
  326. if (typeof relName === 'function') {
  327. relName = forceSync;
  328. forceSync = alt;
  329. }
  330. //Simulate async callback;
  331. if (forceSync) {
  332. main(undef, deps, callback, relName);
  333. } else {
  334. //Using a non-zero value because of concern for what old browsers
  335. //do, and latest browsers "upgrade" to 4 if lower value is used:
  336. //http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
  337. //If want a value immediately, use require('id') instead -- something
  338. //that works in almond on the global level, but not guaranteed and
  339. //unlikely to work in other AMD implementations.
  340. setTimeout(function () {
  341. main(undef, deps, callback, relName);
  342. }, 4);
  343. }
  344. return req;
  345. };
  346. /**
  347. * Just drops the config on the floor, but returns req in case
  348. * the config return value is used.
  349. */
  350. req.config = function (cfg) {
  351. return req(cfg);
  352. };
  353. /**
  354. * Expose module registry for debugging and tooling
  355. */
  356. requirejs._defined = defined;
  357. define = function (name, deps, callback) {
  358. //This module may not have dependencies
  359. if (!deps.splice) {
  360. //deps is not an array, so probably means
  361. //an object literal or factory function for
  362. //the value. Adjust args.
  363. callback = deps;
  364. deps = [];
  365. }
  366. if (!hasProp(defined, name) && !hasProp(waiting, name)) {
  367. waiting[name] = [name, deps, callback];
  368. }
  369. };
  370. define.amd = {
  371. jQuery: true
  372. };
  373. }());
  374. define("almond", function(){});
  375. define('jquery',[],function () {
  376. return jQuery;
  377. });
  378. define('select2/utils',[], function () {
  379. var Utils = {};
  380. Utils.Extend = function (ChildClass, SuperClass) {
  381. var __hasProp = {}.hasOwnProperty;
  382. function BaseConstructor () {
  383. this.constructor = ChildClass;
  384. }
  385. for (var key in SuperClass) {
  386. if (__hasProp.call(SuperClass, key)) {
  387. ChildClass[key] = SuperClass[key];
  388. }
  389. }
  390. BaseConstructor.prototype = SuperClass.prototype;
  391. ChildClass.prototype = new BaseConstructor();
  392. ChildClass.__super__ = SuperClass.prototype;
  393. return ChildClass;
  394. };
  395. function getMethods (theClass) {
  396. var proto = theClass.prototype;
  397. var methods = [];
  398. for (var methodName in proto) {
  399. var m = proto[methodName];
  400. if (typeof m !== 'function') {
  401. continue;
  402. }
  403. methods.push(methodName);
  404. }
  405. return methods;
  406. }
  407. Utils.Decorate = function (SuperClass, DecoratorClass) {
  408. var decoratedMethods = getMethods(DecoratorClass);
  409. var superMethods = getMethods(SuperClass);
  410. function DecoratedClass () {
  411. var unshift = Array.prototype.unshift;
  412. var argCount = DecoratorClass.prototype.constructor.length;
  413. var calledConstructor = SuperClass.prototype.constructor;
  414. if (argCount > 0) {
  415. unshift.call(arguments, SuperClass.prototype.constructor);
  416. calledConstructor = DecoratorClass.prototype.constructor;
  417. }
  418. calledConstructor.apply(this, arguments);
  419. }
  420. DecoratorClass.displayName = SuperClass.displayName;
  421. function ctr () {
  422. this.constructor = DecoratedClass;
  423. }
  424. DecoratedClass.prototype = new ctr();
  425. for (var m = 0; m < superMethods.length; m++) {
  426. var superMethod = superMethods[m];
  427. DecoratedClass.prototype[superMethod] =
  428. SuperClass.prototype[superMethod];
  429. }
  430. var calledMethod = function (methodName) {
  431. // Stub out the original method if it's not decorating an actual method
  432. var originalMethod = function () {};
  433. if (methodName in DecoratedClass.prototype) {
  434. originalMethod = DecoratedClass.prototype[methodName];
  435. }
  436. var decoratedMethod = DecoratorClass.prototype[methodName];
  437. return function () {
  438. var unshift = Array.prototype.unshift;
  439. unshift.call(arguments, originalMethod);
  440. return decoratedMethod.apply(this, arguments);
  441. };
  442. };
  443. for (var d = 0; d < decoratedMethods.length; d++) {
  444. var decoratedMethod = decoratedMethods[d];
  445. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  446. }
  447. return DecoratedClass;
  448. };
  449. var Observable = function () {
  450. this.listeners = {};
  451. };
  452. Observable.prototype.on = function (event, callback) {
  453. this.listeners = this.listeners || {};
  454. if (event in this.listeners) {
  455. this.listeners[event].push(callback);
  456. } else {
  457. this.listeners[event] = [callback];
  458. }
  459. };
  460. Observable.prototype.trigger = function (event) {
  461. var slice = Array.prototype.slice;
  462. this.listeners = this.listeners || {};
  463. if (event in this.listeners) {
  464. this.invoke(this.listeners[event], slice.call(arguments, 1));
  465. }
  466. if ('*' in this.listeners) {
  467. this.invoke(this.listeners['*'], arguments);
  468. }
  469. };
  470. Observable.prototype.invoke = function (listeners, params) {
  471. for (var i = 0, len = listeners.length; i < len; i++) {
  472. listeners[i].apply(this, params);
  473. }
  474. };
  475. Utils.Observable = Observable;
  476. Utils.generateChars = function (length) {
  477. var chars = '';
  478. for (var i = 0; i < length; i++) {
  479. var randomChar = Math.floor(Math.random() * 36);
  480. chars += randomChar.toString(36);
  481. }
  482. return chars;
  483. };
  484. return Utils;
  485. });
  486. define('select2/results',[
  487. './utils'
  488. ], function (Utils) {
  489. function Results ($element, options, dataAdapter) {
  490. this.$element = $element;
  491. this.data = dataAdapter;
  492. this.options = options;
  493. Results.__super__.constructor.call(this);
  494. }
  495. Utils.Extend(Results, Utils.Observable);
  496. Results.prototype.render = function () {
  497. var $results = $(
  498. '<ul class="options" role="tree"></ul>'
  499. );
  500. if (this.options.get('multiple')) {
  501. $results.attr('aria-multiselectable', 'true');
  502. }
  503. this.$results = $results;
  504. return $results;
  505. };
  506. Results.prototype.clear = function () {
  507. this.$results.empty();
  508. };
  509. Results.prototype.displayMessage = function (params) {
  510. this.clear();
  511. var $message = $('<li role="treeitem" class="option"></li>');
  512. var message = this.options.get('translations').get(params.message);
  513. $message.text(message(params.args));
  514. this.$results.append($message);
  515. };
  516. Results.prototype.append = function (data) {
  517. var $options = [];
  518. if (data.length === 0) {
  519. if (this.$results.children().length === 0) {
  520. this.trigger('results:message', {
  521. message: 'noResults'
  522. });
  523. }
  524. return;
  525. }
  526. data = this.sort(data);
  527. for (var d = 0; d < data.length; d++) {
  528. var item = data[d];
  529. var $option = this.option(item);
  530. $options.push($option);
  531. }
  532. this.$results.append($options);
  533. };
  534. Results.prototype.sort = function (data) {
  535. return data;
  536. };
  537. Results.prototype.setClasses = function () {
  538. var self = this;
  539. this.data.current(function (selected) {
  540. var selectedIds = $.map(selected, function (s) {
  541. return s.id.toString();
  542. });
  543. var $options = self.$results.find('.option[aria-selected]');
  544. $options.each(function () {
  545. var $option = $(this);
  546. var item = $option.data('data');
  547. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  548. $option.attr('aria-selected', 'true');
  549. } else {
  550. $option.attr('aria-selected', 'false');
  551. }
  552. });
  553. var $selected = $options.filter('[aria-selected=true]');
  554. // Check if there are any selected options
  555. if ($selected.length > 0) {
  556. // If there are selected options, highlight the first
  557. $selected.first().trigger('mouseenter');
  558. } else {
  559. // If there are no selected options, highlight the first option
  560. // in the dropdown
  561. $options.first().trigger('mouseenter');
  562. }
  563. });
  564. };
  565. Results.prototype.option = function (data) {
  566. var attrs = {
  567. 'class': 'option',
  568. 'role': 'treeitem',
  569. 'aria-selected': 'false'
  570. };
  571. if (data.disabled) {
  572. delete attrs['aria-selected'];
  573. attrs['aria-disabled'] = 'true';
  574. }
  575. if (data.id == null) {
  576. delete attrs['aria-selected'];
  577. }
  578. if (data._resultId != null) {
  579. attrs.id = data._resultId;
  580. }
  581. if (data.children) {
  582. attrs.role = 'group';
  583. attrs['aria-label'] = data.text;
  584. delete attrs['aria-selected'];
  585. }
  586. var html = '<li';
  587. for (var attr in attrs) {
  588. var val = attrs[attr];
  589. html += ' ' + attr + '="' + val + '"';
  590. }
  591. html += '></li>';
  592. var $option = $(html);
  593. if (data.children) {
  594. var $label = $('<strong class="group-label"></strong>');
  595. this.template(data, $label);
  596. var $children = [];
  597. for (var c = 0; c < data.children.length; c++) {
  598. var child = data.children[c];
  599. var $child = this.option(child);
  600. $children.push($child);
  601. }
  602. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  603. $childrenContainer.append($children);
  604. $option.append($label);
  605. $option.append($childrenContainer);
  606. } else {
  607. this.template(data, $option);
  608. }
  609. $option.data('data', data);
  610. return $option;
  611. };
  612. Results.prototype.bind = function (container, $container) {
  613. var self = this;
  614. var id = container.id + '-results';
  615. this.$results.attr('id', id);
  616. container.on('results:all', function (params) {
  617. self.clear();
  618. self.append(params.data);
  619. if (container.isOpen()) {
  620. self.setClasses();
  621. }
  622. });
  623. container.on('results:append', function (params) {
  624. self.append(params.data);
  625. if (container.isOpen()) {
  626. self.setClasses();
  627. }
  628. });
  629. container.on('select', function () {
  630. if (!container.isOpen()) {
  631. return;
  632. }
  633. self.setClasses();
  634. });
  635. container.on('unselect', function () {
  636. if (!container.isOpen()) {
  637. return;
  638. }
  639. self.setClasses();
  640. });
  641. container.on('open', function () {
  642. // When the dropdown is open, aria-expended="true"
  643. self.$results.attr('aria-expanded', 'true');
  644. self.$results.attr('aria-hidden', 'false');
  645. self.setClasses();
  646. self.ensureHighlightVisible();
  647. });
  648. container.on('close', function () {
  649. // When the dropdown is closed, aria-expended="false"
  650. self.$results.attr('aria-expanded', 'false');
  651. self.$results.attr('aria-hidden', 'true');
  652. self.$results.removeAttr('aria-activedescendant');
  653. });
  654. container.on('results:select', function () {
  655. var $highlighted = self.$results.find('.highlighted');
  656. if ($highlighted.length === 0) {
  657. return;
  658. }
  659. var data = $highlighted.data('data');
  660. if ($highlighted.attr('aria-selected') == 'true') {
  661. self.trigger('unselected', {
  662. data: data
  663. });
  664. } else {
  665. self.trigger('selected', {
  666. data: data
  667. });
  668. }
  669. });
  670. container.on('results:previous', function () {
  671. var $highlighted = self.$results.find('.highlighted');
  672. var $options = self.$results.find('[aria-selected]');
  673. var currentIndex = $options.index($highlighted);
  674. // If we are already at te top, don't move further
  675. if (currentIndex === 0) {
  676. return;
  677. }
  678. var nextIndex = currentIndex - 1;
  679. // If none are highlighted, highlight the first
  680. if ($highlighted.length === 0) {
  681. nextIndex = 0;
  682. }
  683. var $next = $options.eq(nextIndex);
  684. $next.trigger('mouseenter');
  685. var currentOffset = self.$results.offset().top;
  686. var nextTop = $next.offset().top;
  687. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  688. if (nextIndex === 0) {
  689. self.$results.scrollTop(0);
  690. } else if (nextTop - currentOffset < 0) {
  691. self.$results.scrollTop(nextOffset);
  692. }
  693. });
  694. container.on('results:next', function () {
  695. var $highlighted = self.$results.find('.highlighted');
  696. var $options = self.$results.find('[aria-selected]');
  697. var currentIndex = $options.index($highlighted);
  698. var nextIndex = currentIndex + 1;
  699. // If we are at the last option, stay there
  700. if (nextIndex >= $options.length) {
  701. return;
  702. }
  703. var $next = $options.eq(nextIndex);
  704. $next.trigger('mouseenter');
  705. var currentOffset = self.$results.offset().top +
  706. self.$results.outerHeight(false);
  707. var nextBottom = $next.offset().top + $next.outerHeight(false);
  708. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  709. if (nextIndex === 0) {
  710. self.$results.scrollTop(0);
  711. } else if (nextBottom > currentOffset) {
  712. self.$results.scrollTop(nextOffset);
  713. }
  714. });
  715. container.on('results:focus', function (params) {
  716. params.element.addClass('highlighted');
  717. });
  718. container.on('results:message', function (params) {
  719. self.trigger('results:message', params);
  720. });
  721. this.on('results:message', function (params) {
  722. self.displayMessage(params);
  723. });
  724. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  725. var $this = $(this);
  726. var data = $this.data('data');
  727. if ($this.attr('aria-selected') === 'true') {
  728. self.trigger('unselected', {
  729. originalEvent: evt,
  730. data: data
  731. });
  732. return;
  733. }
  734. self.trigger('selected', {
  735. originalEvent: evt,
  736. data: data
  737. });
  738. });
  739. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  740. var data = $(this).data('data');
  741. self.$results.find('.option.highlighted').removeClass('highlighted');
  742. self.trigger('results:focus', {
  743. data: data,
  744. element: $(this)
  745. });
  746. });
  747. this.$results.on('mouseleave', '.option', function (evt) {
  748. if ($(this).hasClass('highlighted')) {
  749. $(this).removeClass('highlighted');
  750. self.$results.removeAttr('aria-activedescendant');
  751. }
  752. });
  753. };
  754. Results.prototype.ensureHighlightVisible = function () {
  755. var $highlighted = this.$results.find('.highlighted');
  756. if ($highlighted.length === 0) {
  757. return;
  758. }
  759. var $options = this.$results.find('[aria-selected]');
  760. var currentIndex = $options.index($highlighted);
  761. var currentOffset = this.$results.offset().top;
  762. var nextTop = $highlighted.offset().top;
  763. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  764. var offsetDelta = nextTop - currentOffset;
  765. nextOffset -= $highlighted.outerHeight(false) * 2;
  766. if (currentIndex <= 2) {
  767. this.$results.scrollTop(0);
  768. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  769. this.$results.scrollTop(nextOffset);
  770. }
  771. };
  772. Results.prototype.template = function (result, $container) {
  773. var template = this.options.get('templateResult');
  774. $container.html(template(result));
  775. };
  776. return Results;
  777. });
  778. define('select2/selection/base',[
  779. '../utils'
  780. ], function (Utils) {
  781. function BaseSelection ($element, options) {
  782. this.$element = $element;
  783. this.options = options;
  784. BaseSelection.__super__.constructor.call(this);
  785. }
  786. Utils.Extend(BaseSelection, Utils.Observable);
  787. BaseSelection.prototype.render = function () {
  788. throw new Error('The `render` method must be defined in child classes.');
  789. };
  790. BaseSelection.prototype.bind = function (container, $container) {
  791. var self = this;
  792. container.on('selection:update', function (params) {
  793. self.update(params.data);
  794. });
  795. container.on('open', function () {
  796. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  797. var $target = $(e.target);
  798. var $select = $target.closest('.select2');
  799. var $all = $('.select2.open');
  800. $all.each(function () {
  801. var $this = $(this);
  802. if (this == $select[0]) {
  803. return;
  804. }
  805. var $element = $this.data('element');
  806. $element.select2('close');
  807. });
  808. });
  809. container.on('close', function () {
  810. $(document.body).off('mousedown.select2.' + container.id);
  811. });
  812. });
  813. };
  814. BaseSelection.prototype.update = function (data) {
  815. throw new Error('The `update` method must be defined in child classes.');
  816. };
  817. return BaseSelection;
  818. });
  819. define('select2/keys',[
  820. ], function () {
  821. var KEYS = {
  822. BACKSPACE: 8,
  823. TAB: 9,
  824. ENTER: 13,
  825. SHIFT: 16,
  826. CTRL: 17,
  827. ALT: 18,
  828. ESC: 27,
  829. SPACE: 32,
  830. PAGE_UP: 33,
  831. PAGE_DOWN: 34,
  832. END: 35,
  833. HOME: 36,
  834. LEFT: 37,
  835. UP: 38,
  836. RIGHT: 39,
  837. DOWN: 40,
  838. DELETE: 46,
  839. isArrow: function (k) {
  840. k = k.which ? k.which : k;
  841. switch (k) {
  842. case KEY.LEFT:
  843. case KEY.RIGHT:
  844. case KEY.UP:
  845. case KEY.DOWN:
  846. return true;
  847. }
  848. return false;
  849. }
  850. };
  851. return KEYS;
  852. });
  853. define('select2/selection/single',[
  854. './base',
  855. '../utils',
  856. '../keys'
  857. ], function (BaseSelection, Utils, KEYS) {
  858. function SingleSelection () {
  859. SingleSelection.__super__.constructor.apply(this, arguments);
  860. }
  861. Utils.Extend(SingleSelection, BaseSelection);
  862. SingleSelection.prototype.render = function () {
  863. var $selection = $(
  864. '<span class="single-select" tabindex="0" role="combobox" ' +
  865. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  866. '<span class="rendered-selection"></span>' +
  867. '</span>'
  868. );
  869. $selection.attr('title', this.$element.attr('title'));
  870. this.$selection = $selection;
  871. return $selection;
  872. };
  873. SingleSelection.prototype.bind = function (container, $container) {
  874. var self = this;
  875. SingleSelection.__super__.bind.apply(this, arguments);
  876. var id = container.id + '-container';
  877. var resultsId = container.id + '-results';
  878. this.$selection.find('.rendered-selection').attr('id', id);
  879. this.$selection.attr('aria-labelledby', id);
  880. this.$selection.attr('aria-owns', resultsId);
  881. this.$selection.on('mousedown', function (evt) {
  882. // Only respond to left clicks
  883. if (evt.which !== 1) {
  884. return;
  885. }
  886. self.trigger('toggle', {
  887. originalEvent: evt
  888. });
  889. });
  890. container.on('open', function () {
  891. // When the dropdown is open, aria-expanded="true"
  892. self.$selection.attr('aria-expanded', 'true');
  893. });
  894. container.on('close', function () {
  895. // When the dropdown is closed, aria-expanded="false"
  896. self.$selection.attr('aria-expanded', 'false');
  897. self.$selection.removeAttr('aria-activedescendant');
  898. self.$selection.focus();
  899. });
  900. this.$selection.on('focus', function (evt) {
  901. // User focuses on the container
  902. });
  903. this.$selection.on('blur', function (evt) {
  904. // User exits the container
  905. });
  906. this.$selection.on('keydown', function (evt) {
  907. self.trigger('keypress', evt);
  908. if (evt.which === KEYS.SPACE) {
  909. evt.preventDefault();
  910. }
  911. });
  912. container.on('results:focus', function (params) {
  913. self.$selection.attr('aria-activedescendant', params.data._resultId);
  914. });
  915. container.on('selection:update', function (params) {
  916. self.update(params.data);
  917. });
  918. };
  919. SingleSelection.prototype.clear = function () {
  920. this.$selection.find('.rendered-selection').empty();
  921. };
  922. SingleSelection.prototype.display = function (data) {
  923. var template = this.options.get('templateSelection');
  924. return template(data);
  925. };
  926. SingleSelection.prototype.selectionContainer = function () {
  927. return $('<span></span>');
  928. };
  929. SingleSelection.prototype.update = function (data) {
  930. if (data.length === 0) {
  931. this.clear();
  932. return;
  933. }
  934. var selection = data[0];
  935. var formatted = this.display(selection);
  936. this.$selection.find('.rendered-selection').html(formatted);
  937. };
  938. return SingleSelection;
  939. });
  940. define('select2/selection/multiple',[
  941. './base',
  942. '../utils'
  943. ], function (BaseSelection, Utils) {
  944. function MultipleSelection ($element, options) {
  945. MultipleSelection.__super__.constructor.apply(this, arguments);
  946. }
  947. Utils.Extend(MultipleSelection, BaseSelection);
  948. MultipleSelection.prototype.render = function () {
  949. var $selection = $(
  950. '<span class="multiple-select">' +
  951. '<ul class="rendered-selection"></ul>' +
  952. '</span>'
  953. );
  954. this.$selection = $selection;
  955. return $selection;
  956. };
  957. MultipleSelection.prototype.bind = function (container, $container) {
  958. var self = this;
  959. MultipleSelection.__super__.bind.apply(this, arguments);
  960. this.$selection.on('click', function (evt) {
  961. self.trigger('toggle', {
  962. originalEvent: evt
  963. });
  964. });
  965. this.$selection.on('click', '.remove', function (evt) {
  966. var $remove = $(this);
  967. var $selection = $remove.parent();
  968. var data = $selection.data('data');
  969. self.trigger('unselected', {
  970. originalEvent: evt,
  971. data: data
  972. });
  973. });
  974. };
  975. MultipleSelection.prototype.clear = function () {
  976. this.$selection.find('.rendered-selection').empty();
  977. };
  978. MultipleSelection.prototype.display = function (data) {
  979. var template = this.options.get('templateSelection');
  980. return template(data);
  981. };
  982. MultipleSelection.prototype.selectionContainer = function () {
  983. var $container = $(
  984. '<li class="choice">' +
  985. '<span class="remove" role="presentation">&times;</span>' +
  986. '</li>'
  987. );
  988. return $container;
  989. };
  990. MultipleSelection.prototype.update = function (data) {
  991. this.clear();
  992. if (data.length === 0) {
  993. return;
  994. }
  995. var $selections = [];
  996. for (var d = 0; d < data.length; d++) {
  997. var selection = data[d];
  998. var formatted = this.display(selection);
  999. var $selection = this.selectionContainer();
  1000. $selection.append(formatted);
  1001. $selection.data('data', selection);
  1002. $selections.push($selection);
  1003. }
  1004. this.$selection.find('.rendered-selection').append($selections);
  1005. };
  1006. return MultipleSelection;
  1007. });
  1008. define('select2/selection/placeholder',[
  1009. '../utils'
  1010. ], function (Utils) {
  1011. function Placeholder (decorated, $element, options) {
  1012. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1013. decorated.call(this, $element, options);
  1014. }
  1015. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1016. if (typeof placeholder === 'string') {
  1017. placeholder = {
  1018. id: '',
  1019. text: placeholder
  1020. };
  1021. }
  1022. return placeholder;
  1023. };
  1024. Placeholder.prototype.update = function (decorated, data) {
  1025. var singlePlaceholder = (
  1026. data.length == 1 && data[0].id != this.placeholder.id
  1027. );
  1028. var multipleSelections = data.length > 1;
  1029. if (multipleSelections || singlePlaceholder) {
  1030. return decorated.call(this, data);
  1031. }
  1032. this.clear();
  1033. var $placeholder = this.selectionContainer();
  1034. $placeholder.html(this.display(this.placeholder));
  1035. $placeholder.addClass('placeholder').removeClass('choice');
  1036. this.$selection.find('.rendered-selection').append($placeholder);
  1037. };
  1038. return Placeholder;
  1039. });
  1040. define('select2/translation',[
  1041. ], function () {
  1042. function Translation (dict) {
  1043. this.dict = dict || {};
  1044. }
  1045. Translation.prototype.all = function () {
  1046. return this.dict;
  1047. };
  1048. Translation.prototype.get = function (key) {
  1049. return this.dict[key];
  1050. };
  1051. Translation.prototype.extend = function (translation) {
  1052. this.dict = $.extend({}, translation.all(), this.dict);
  1053. };
  1054. // Static functions
  1055. Translation._cache = {};
  1056. Translation.loadPath = function (path) {
  1057. if (!(path in Translation._cache)) {
  1058. var translations = require(path);
  1059. Translation._cache[path] = translations;
  1060. }
  1061. return new Translation(Translation._cache[path]);
  1062. };
  1063. return Translation;
  1064. });
  1065. define('select2/data/base',[
  1066. '../utils'
  1067. ], function (Utils) {
  1068. function BaseAdapter ($element, options) {
  1069. BaseAdapter.__super__.constructor.call(this);
  1070. }
  1071. Utils.Extend(BaseAdapter, Utils.Observable);
  1072. BaseAdapter.prototype.current = function (callback) {
  1073. throw new Error('The `current` method must be defined in child classes.');
  1074. };
  1075. BaseAdapter.prototype.query = function (params, callback) {
  1076. throw new Error('The `query` method must be defined in child classes.');
  1077. };
  1078. BaseAdapter.prototype.bind = function (container, $container) {
  1079. // Can be implemented in subclasses
  1080. };
  1081. BaseAdapter.prototype.generateResultId = function (container, data) {
  1082. var id = container.id + '-result-';
  1083. id += Utils.generateChars(4);
  1084. if (data.id != null) {
  1085. id += '-' + data.id.toString();
  1086. } else {
  1087. id += '-' + Utils.generateChars(4);
  1088. }
  1089. return id;
  1090. };
  1091. return BaseAdapter;
  1092. });
  1093. define('select2/data/select',[
  1094. './base',
  1095. '../utils',
  1096. 'jquery'
  1097. ], function (BaseAdapter, Utils, $) {
  1098. function SelectAdapter ($element, options) {
  1099. this.$element = $element;
  1100. this.options = options;
  1101. SelectAdapter.__super__.constructor.call(this);
  1102. }
  1103. Utils.Extend(SelectAdapter, BaseAdapter);
  1104. SelectAdapter.prototype.current = function (callback) {
  1105. var data = [];
  1106. var self = this;
  1107. this.$element.find(':selected').each(function () {
  1108. var $option = $(this);
  1109. var option = self.item($option);
  1110. data.push(option);
  1111. });
  1112. callback(data);
  1113. };
  1114. SelectAdapter.prototype.select = function (data) {
  1115. var self = this;
  1116. if (this.$element.prop('multiple')) {
  1117. this.current(function (currentData) {
  1118. var val = [];
  1119. data = [data];
  1120. data.push.apply(data, currentData);
  1121. for (var d = 0; d < data.length; d++) {
  1122. id = data[d].id;
  1123. if (val.indexOf(id) === -1) {
  1124. val.push(id);
  1125. }
  1126. }
  1127. self.$element.val(val);
  1128. self.$element.trigger('change');
  1129. });
  1130. } else {
  1131. var val = data.id;
  1132. this.$element.val(val);
  1133. this.$element.trigger('change');
  1134. }
  1135. };
  1136. SelectAdapter.prototype.unselect = function (data) {
  1137. var self = this;
  1138. if (!this.$element.prop('multiple')) {
  1139. return;
  1140. }
  1141. this.current(function (currentData) {
  1142. var val = [];
  1143. for (var d = 0; d < currentData.length; d++) {
  1144. id = currentData[d].id;
  1145. if (id !== data.id && val.indexOf(id) === -1) {
  1146. val.push(id);
  1147. }
  1148. }
  1149. self.$element.val(val);
  1150. self.$element.trigger('change');
  1151. });
  1152. };
  1153. SelectAdapter.prototype.bind = function (container, $container) {
  1154. var self = this;
  1155. this.container = container;
  1156. container.on('select', function (params) {
  1157. self.select(params.data);
  1158. });
  1159. container.on('unselect', function (params) {
  1160. self.unselect(params.data);
  1161. });
  1162. };
  1163. SelectAdapter.prototype.query = function (params, callback) {
  1164. var data = [];
  1165. var self = this;
  1166. var $options = this.$element.children();
  1167. $options.each(function () {
  1168. var $option = $(this);
  1169. if (!$option.is('option') && !$option.is('optgroup')) {
  1170. return;
  1171. }
  1172. var option = self.item($option);
  1173. var matches = self.matches(params, option);
  1174. if (matches !== null) {
  1175. data.push(matches);
  1176. }
  1177. });
  1178. callback(data);
  1179. };
  1180. SelectAdapter.prototype.option = function (data) {
  1181. var $option = $('<option></option>');
  1182. $option.text(data.text);
  1183. $option.val(data.id);
  1184. $option.prop('disabled', data.disabled || false);
  1185. $option.prop('selected', data.selected || false);
  1186. // Get any automatically generated data values
  1187. var detectedData = this.item($option);
  1188. // Merge it with the already present data
  1189. var combinedData = $.extend({}, data, detectedData);
  1190. // Override the option's data with the combined data
  1191. $option.data('data', combinedData);
  1192. return $option;
  1193. };
  1194. SelectAdapter.prototype.item = function ($option) {
  1195. var data = $option.data('data');
  1196. // If the data has already be generated, use it
  1197. if (data == null) {
  1198. if ($option.is('option')) {
  1199. data = {
  1200. id: $option.val(),
  1201. text: $option.html(),
  1202. disabled: $option.prop('disabled')
  1203. };
  1204. } else if ($option.is('optgroup')) {
  1205. data = {
  1206. text: $option.attr('label'),
  1207. children: []
  1208. };
  1209. var $children = $option.children('option');
  1210. var children = [];
  1211. for (var c = 0; c < $children.length; c++) {
  1212. var $child = $($children[c]);
  1213. var child = this.item($child);
  1214. children.push(child);
  1215. }
  1216. data.children = children;
  1217. }
  1218. if (data.id && this.container != null) {
  1219. data._resultId = this.generateResultId(this.container, data);
  1220. }
  1221. data.selected = $option.prop('selected');
  1222. $option.data('data', data);
  1223. }
  1224. return data;
  1225. };
  1226. SelectAdapter.prototype.matches = function (params, data) {
  1227. var matcher = this.options.get('matcher');
  1228. return matcher(params, data);
  1229. };
  1230. return SelectAdapter;
  1231. });
  1232. define('select2/data/array',[
  1233. './select',
  1234. '../utils',
  1235. 'jquery'
  1236. ], function (SelectAdapter, Utils, $) {
  1237. function ArrayAdapter ($element, options) {
  1238. var data = options.get('data');
  1239. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1240. this.convertToOptions(data);
  1241. }
  1242. Utils.Extend(ArrayAdapter, SelectAdapter);
  1243. ArrayAdapter.prototype.convertToOptions = function (data) {
  1244. var self = this;
  1245. var $existing = this.$element.find('option');
  1246. var existingIds = $existing.map(function () {
  1247. return self.item($(this)).id;
  1248. }).get();
  1249. // Filter out all items except for the one passed in the argument
  1250. function onlyItem (item) {
  1251. return function () {
  1252. return $(this).val() == item.id;
  1253. };
  1254. }
  1255. for (var d = 0; d < data.length; d++) {
  1256. var item = data[d];
  1257. item.id = item.id.toString();
  1258. // Skip items which were pre-loaded, only merge the data
  1259. if (existingIds.indexOf(item.id) >= 0) {
  1260. var $existingOption = $existing.filter(onlyItem(item));
  1261. var existingData = this.item($existingOption);
  1262. var newData = $.extend(true, {}, existingData, item);
  1263. var $newOption = this.option(existingData);
  1264. $existingOption.replaceWith($newOption);
  1265. continue;
  1266. }
  1267. var option = this.option(item);
  1268. this.$element.append(option);
  1269. }
  1270. };
  1271. return ArrayAdapter;
  1272. });
  1273. define('select2/data/ajax',[
  1274. './array',
  1275. '../utils',
  1276. 'jquery'
  1277. ], function (ArrayAdapter, Utils, $) {
  1278. function AjaxAdapter ($element, options) {
  1279. this.ajaxOptions = options.get('ajax');
  1280. if (this.ajaxOptions.processResults != null) {
  1281. this.processResults = this.ajaxOptions.processResults;
  1282. }
  1283. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1284. }
  1285. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1286. AjaxAdapter.prototype.processResults = function (results) {
  1287. return results;
  1288. };
  1289. AjaxAdapter.prototype.query = function (params, callback) {
  1290. var matches = [];
  1291. var self = this;
  1292. var options = $.extend({
  1293. type: 'GET'
  1294. }, this.ajaxOptions);
  1295. if (typeof options.url === 'function') {
  1296. options.url = options.url(params);
  1297. }
  1298. if (typeof options.data === 'function') {
  1299. options.data = options.data(params);
  1300. }
  1301. function request () {
  1302. var $request = $.ajax(options);
  1303. $request.success(function (data) {
  1304. var results = self.processResults(data);
  1305. callback(results);
  1306. });
  1307. }
  1308. if (this.ajaxOptions.delay && params.term !== '') {
  1309. if (this._queryTimeout) {
  1310. window.clearTimeout(this._queryTimeout);
  1311. }
  1312. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  1313. } else {
  1314. request();
  1315. }
  1316. };
  1317. return AjaxAdapter;
  1318. });
  1319. define('select2/data/tags',[
  1320. ], function () {
  1321. function Tags (decorated, $element, options) {
  1322. var tags = options.get('tags');
  1323. decorated.call(this, $element, options);
  1324. }
  1325. Tags.prototype.query = function (decorated, params, callback) {
  1326. var self = this;
  1327. this._removeOldTags();
  1328. if (params.term == null || params.term === '' || params.page != null) {
  1329. decorated.call(this, params, callback);
  1330. return;
  1331. }
  1332. function wrapper (data, child) {
  1333. for (var i = 0; i < data.length; i++) {
  1334. var option = data[i];
  1335. var checkChildren = (
  1336. option.children != null && !wrapper(option.children, true)
  1337. );
  1338. var checkText = option.text === params.term;
  1339. if (checkText || checkChildren) {
  1340. if (child) {
  1341. return false;
  1342. }
  1343. callback(data);
  1344. return;
  1345. }
  1346. }
  1347. if (child) {
  1348. return true;
  1349. }
  1350. var tag = self.createTag(params);
  1351. var $option = self.option(tag);
  1352. $option.attr('data-select2-tag', true);
  1353. self.$element.append($option);
  1354. self.insertTag(data, tag);
  1355. callback(data);
  1356. }
  1357. decorated.call(this, params, wrapper);
  1358. };
  1359. Tags.prototype.createTag = function (decorated, params) {
  1360. return {
  1361. id: params.term,
  1362. text: params.term
  1363. };
  1364. };
  1365. Tags.prototype.insertTag = function (_, data, tag) {
  1366. data.unshift(tag);
  1367. };
  1368. Tags.prototype._removeOldTags = function (_) {
  1369. var tag = this._lastTag;
  1370. var $options = this.$element.find('option[data-select2-tag]');
  1371. $options.each(function () {
  1372. if (this.selected) {
  1373. return;
  1374. }
  1375. $(this).remove();
  1376. });
  1377. };
  1378. return Tags;
  1379. });
  1380. define('select2/data/minimumInputLength',[
  1381. ], function () {
  1382. function MinimumInputLength (decorated, $e, options) {
  1383. this.minimumInputLength = options.get('minimumInputLength');
  1384. decorated.call(this, $e, options);
  1385. }
  1386. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1387. params.term = params.term || '';
  1388. if (params.term.length < this.minimumInputLength) {
  1389. this.trigger('results:message', {
  1390. message: 'inputTooShort',
  1391. args: {
  1392. minimum: this.minimumInputLength,
  1393. input: params.term,
  1394. params: params
  1395. }
  1396. });
  1397. return;
  1398. }
  1399. decorated.call(this, params, callback);
  1400. };
  1401. return MinimumInputLength;
  1402. });
  1403. define('select2/dropdown',[
  1404. './utils'
  1405. ], function (Utils) {
  1406. function Dropdown ($element, options) {
  1407. this.$element = $element;
  1408. }
  1409. Utils.Extend(Dropdown, Utils.Observable);
  1410. Dropdown.prototype.render = function () {
  1411. var $dropdown = $(
  1412. '<span class="dropdown">' +
  1413. '<span class="results"></span>' +
  1414. '</span>'
  1415. );
  1416. return $dropdown;
  1417. };
  1418. Dropdown.prototype.bind = function (container, $container) {
  1419. // Can be implemented in subclasses
  1420. };
  1421. return Dropdown;
  1422. });
  1423. define('select2/dropdown/search',[
  1424. '../utils'
  1425. ], function (Utils) {
  1426. function Search () { }
  1427. Search.prototype.render = function (decorated) {
  1428. var $rendered = decorated.call(this);
  1429. var $search = $(
  1430. '<span class="search">' +
  1431. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1432. '</span>'
  1433. );
  1434. this.$searchContainer = $search;
  1435. this.$search = $search.find('input');
  1436. $rendered.prepend($search);
  1437. return $rendered;
  1438. };
  1439. Search.prototype.bind = function (decorated, container, $container) {
  1440. var self = this;
  1441. decorated.call(this, container, $container);
  1442. this.$search.on('keydown', function (evt) {
  1443. self.trigger('keypress', evt);
  1444. self._keyUpPrevented = evt.isDefaultPrevented();
  1445. });
  1446. this.$search.on('keyup', function (evt) {
  1447. self.handleSearch(evt);
  1448. });
  1449. container.on('open', function () {
  1450. self.$search.attr('tabindex', 0);
  1451. });
  1452. container.on('close', function () {
  1453. self.$search.attr('tabindex', -1);
  1454. self.$search.val('');
  1455. });
  1456. container.on('results:all', function (params) {
  1457. if (params.query.term == null || params.query.term === '') {
  1458. var showSearch = self.showSearch(params);
  1459. if (showSearch) {
  1460. self.$searchContainer.show();
  1461. } else {
  1462. self.$searchContainer.hide();
  1463. }
  1464. }
  1465. });
  1466. };
  1467. Search.prototype.handleSearch = function (evt) {
  1468. if (!this._keyUpPrevented) {
  1469. var input = this.$search.val();
  1470. this.trigger('query', {
  1471. term: input
  1472. });
  1473. }
  1474. this._keyUpPrevented = false;
  1475. };
  1476. Search.prototype.showSearch = function (_, params) {
  1477. return true;
  1478. };
  1479. return Search;
  1480. });
  1481. define('select2/dropdown/hidePlaceholder',[
  1482. ], function () {
  1483. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1484. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1485. decorated.call(this, $element, options, dataAdapter);
  1486. }
  1487. HidePlaceholder.prototype.append = function (decorated, data) {
  1488. data = this.removePlaceholder(data);
  1489. decorated.call(this, data);
  1490. };
  1491. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1492. if (typeof placeholder === 'string') {
  1493. placeholder = {
  1494. id: '',
  1495. text: placeholder
  1496. };
  1497. }
  1498. return placeholder;
  1499. };
  1500. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1501. var modifiedData = data.slice(0);
  1502. for (var d = data.length - 1; d >= 0; d--) {
  1503. var item = data[d];
  1504. if (this.placeholder.id === item.id) {
  1505. modifiedData.splice(d, 1);
  1506. }
  1507. }
  1508. return modifiedData;
  1509. };
  1510. return HidePlaceholder;
  1511. });
  1512. define('select2/dropdown/infiniteScroll',[
  1513. 'jquery'
  1514. ], function ($) {
  1515. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1516. this.lastParams = {};
  1517. decorated.call(this, $element, options, dataAdapter);
  1518. this.$loadingMore = this.createLoadingMore();
  1519. this.loading = false;
  1520. }
  1521. InfiniteScroll.prototype.append = function (decorated, data) {
  1522. this.$loadingMore.remove();
  1523. decorated.call(this, data);
  1524. if (data.length > 0) {
  1525. this.$results.append(this.$loadingMore);
  1526. }
  1527. this.loading = false;
  1528. };
  1529. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1530. var self = this;
  1531. decorated.call(this, container, $container);
  1532. container.on('query', function (params) {
  1533. self.lastParams = params;
  1534. self.loading = true;
  1535. });
  1536. container.on('query:append', function (params) {
  1537. self.lastParams = params;
  1538. self.loading = true;
  1539. });
  1540. this.$results.on('scroll', function () {
  1541. var loadMoreVisible = $.contains(
  1542. document.documentElement,
  1543. self.$loadingMore[0]
  1544. );
  1545. if (self.loading || !loadMoreVisible) {
  1546. return;
  1547. }
  1548. var currentOffset = self.$results.offset().top +
  1549. self.$results.outerHeight(false);
  1550. var loadingMoreOffset = self.$loadingMore.offset().top +
  1551. self.$loadingMore.outerHeight(false);
  1552. if (currentOffset + 50 >= loadingMoreOffset) {
  1553. self.loadMore();
  1554. }
  1555. });
  1556. };
  1557. InfiniteScroll.prototype.loadMore = function () {
  1558. this.loading = true;
  1559. var params = $.extend({}, {page: 1}, this.lastParams);
  1560. params.page++;
  1561. this.trigger('query:append', params);
  1562. };
  1563. InfiniteScroll.prototype.createLoadingMore = function () {
  1564. var $option = $(
  1565. '<li class="option load-more" role="treeitem"></li>'
  1566. );
  1567. var message = this.options.get('translations').get('loadingMore');
  1568. $option.html(message(this.lastParams));
  1569. return $option;
  1570. };
  1571. return InfiniteScroll;
  1572. });
  1573. define('select2/i18n/en',[],function () {
  1574. return {
  1575. inputTooShort: function (args) {
  1576. var remainingChars = args.minimum - args.input.length;
  1577. var message = 'Please enter ' + remainingChars + ' or more character';
  1578. if (remainingChars != 1) {
  1579. message += 's';
  1580. }
  1581. return message;
  1582. },
  1583. loadingMore: function () {
  1584. return 'Loading more results…';
  1585. },
  1586. noResults: function () {
  1587. return 'No results found';
  1588. }
  1589. };
  1590. });
  1591. define('select2/defaults',[
  1592. 'jquery',
  1593. './results',
  1594. './selection/single',
  1595. './selection/multiple',
  1596. './selection/placeholder',
  1597. './utils',
  1598. './translation',
  1599. './data/select',
  1600. './data/array',
  1601. './data/ajax',
  1602. './data/tags',
  1603. './data/minimumInputLength',
  1604. './dropdown',
  1605. './dropdown/search',
  1606. './dropdown/hidePlaceholder',
  1607. './dropdown/infiniteScroll',
  1608. './i18n/en'
  1609. ], function ($, ResultsList,
  1610. SingleSelection, MultipleSelection, Placeholder,
  1611. Utils, Translation,
  1612. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1613. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1614. EnglishTranslation) {
  1615. function Defaults () {
  1616. this.reset();
  1617. }
  1618. Defaults.prototype.apply = function (options) {
  1619. options = $.extend({}, this.defaults, options);
  1620. if (options.dataAdapter == null) {
  1621. if (options.ajax != null) {
  1622. options.dataAdapter = AjaxData;
  1623. } else if (options.data != null) {
  1624. options.dataAdapter = ArrayData;
  1625. } else {
  1626. options.dataAdapter = SelectData;
  1627. }
  1628. }
  1629. if (options.minimumInputLength > 0) {
  1630. options.dataAdapter = Utils.Decorate(
  1631. options.dataAdapter,
  1632. MinimumInputLength
  1633. );
  1634. }
  1635. if (options.tags != null) {
  1636. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1637. }
  1638. if (options.resultsAdapter == null) {
  1639. options.resultsAdapter = ResultsList;
  1640. if (options.ajax != null) {
  1641. options.resultsAdapter = Utils.Decorate(
  1642. options.resultsAdapter,
  1643. InfiniteScroll
  1644. );
  1645. }
  1646. if (options.placeholder != null) {
  1647. options.resultsAdapter = Utils.Decorate(
  1648. options.resultsAdapter,
  1649. HidePlaceholder
  1650. );
  1651. }
  1652. }
  1653. if (options.dropdownAdapter == null) {
  1654. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1655. options.dropdownAdapter = SearchableDropdown;
  1656. }
  1657. if (options.selectionAdapter == null) {
  1658. if (options.multiple) {
  1659. options.selectionAdapter = MultipleSelection;
  1660. } else {
  1661. options.selectionAdapter = SingleSelection;
  1662. }
  1663. // Add the placeholder mixin if a placeholder was specified
  1664. if (options.placeholder != null) {
  1665. options.selectionAdapter = Utils.Decorate(
  1666. options.selectionAdapter,
  1667. Placeholder
  1668. );
  1669. }
  1670. }
  1671. if (typeof options.language === 'string') {
  1672. options.language = [options.language];
  1673. }
  1674. if ($.isArray(options.language)) {
  1675. var languages = new Translation();
  1676. var languageNames = options.language.concat(this.defaults.language);
  1677. for (var l = 0; l < languageNames.length; l++) {
  1678. var name = languageNames[l];
  1679. var language = {};
  1680. try {
  1681. // Try to load it with the original name
  1682. language = Translation.loadPath(name);
  1683. } catch (e) {
  1684. // If we couldn't load it, check if it wasn't the full path
  1685. name = 'select2/i18n/' + name;
  1686. language = Translation.loadPath(name);
  1687. }
  1688. languages.extend(language);
  1689. }
  1690. options.translations = languages;
  1691. } else {
  1692. options.translations = new Translations(options.language);
  1693. }
  1694. return options;
  1695. };
  1696. Defaults.prototype.reset = function () {
  1697. function matcher (params, data) {
  1698. var match = $.extend(true, {}, data);
  1699. if (data.children) {
  1700. for (var c = data.children.length - 1; c >= 0; c--) {
  1701. var child = data.children[c];
  1702. var matches = matcher(params, child);
  1703. // If there wasn't a match, remove the object in the array
  1704. if (matches === null) {
  1705. match.children.splice(c, 1);
  1706. }
  1707. }
  1708. if (match.children.length > 0) {
  1709. return match;
  1710. }
  1711. }
  1712. if ($.trim(params.term) === '') {
  1713. return match;
  1714. }
  1715. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1716. return match;
  1717. }
  1718. return null;
  1719. }
  1720. this.defaults = {
  1721. language: ['select2/i18n/en'],
  1722. matcher: matcher,
  1723. minimumInputLength: 0,
  1724. templateResult: function (result) {
  1725. return result.text;
  1726. },
  1727. templateSelection: function (selection) {
  1728. return selection.text;
  1729. }
  1730. };
  1731. };
  1732. var defaults = new Defaults();
  1733. return defaults;
  1734. });
  1735. define('select2/options',[
  1736. './defaults'
  1737. ], function (Defaults) {
  1738. function Options (options, $element) {
  1739. this.options = options;
  1740. if ($element != null) {
  1741. this.fromElement($element);
  1742. }
  1743. this.options = Defaults.apply(this.options);
  1744. }
  1745. Options.prototype.fromElement = function ($e) {
  1746. if (this.options.multiple == null) {
  1747. this.options.multiple = $e.prop('multiple');
  1748. }
  1749. return this;
  1750. };
  1751. Options.prototype.get = function (key) {
  1752. return this.options[key];
  1753. };
  1754. Options.prototype.set = function (key, val) {
  1755. this.options[key] = val;
  1756. };
  1757. return Options;
  1758. });
  1759. define('select2/core',[
  1760. 'jquery',
  1761. './options',
  1762. './utils',
  1763. './keys'
  1764. ], function ($, Options, Utils, KEYS) {
  1765. var Select2 = function ($element, options) {
  1766. this.$element = $element;
  1767. this.id = this._generateId($element);
  1768. options = options || {};
  1769. this.options = new Options(options, $element);
  1770. Select2.__super__.constructor.call(this);
  1771. // Set up containers and adapters
  1772. var DataAdapter = this.options.get('dataAdapter');
  1773. this.data = new DataAdapter($element, this.options);
  1774. var $container = this.render();
  1775. this._placeContainer($container);
  1776. var SelectionAdapter = this.options.get('selectionAdapter');
  1777. this.selection = new SelectionAdapter($element, this.options);
  1778. var $selection = this.selection.render();
  1779. this._placeSelection($selection);
  1780. var DropdownAdapter = this.options.get('dropdownAdapter');
  1781. this.dropdown = new DropdownAdapter($element, this.options);
  1782. var $dropdown = this.dropdown.render();
  1783. this._placeDropdown($dropdown);
  1784. var ResultsAdapter = this.options.get('resultsAdapter');
  1785. this.results = new ResultsAdapter($element, this.options, this.data);
  1786. var $results = this.results.render();
  1787. this._placeResults($results);
  1788. // Bind events
  1789. var self = this;
  1790. // Bind the container to all of the adapters
  1791. this._bindAdapters();
  1792. // Register any DOM event handlers
  1793. this._registerDomEvents();
  1794. // Register any internal event handlers
  1795. this._registerDataEvents();
  1796. this._registerSelectionEvents();
  1797. this._registerDropdownEvents();
  1798. this._registerResultsEvents();
  1799. this._registerEvents();
  1800. // Set the initial state
  1801. this.data.current(function (initialData) {
  1802. self.trigger('selection:update', {
  1803. data: initialData
  1804. });
  1805. });
  1806. // Hide the original select
  1807. $element.hide();
  1808. $element.attr('tabindex', '-1');
  1809. $element.data('select2', this);
  1810. };
  1811. Utils.Extend(Select2, Utils.Observable);
  1812. Select2.prototype._generateId = function ($element) {
  1813. var id = '';
  1814. if ($element.attr('id') != null) {
  1815. id = $element.attr('id');
  1816. } else if ($element.attr('name') != null) {
  1817. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1818. } else {
  1819. id = Utils.generateChars(4);
  1820. }
  1821. id = 'select2-' + id;
  1822. return id;
  1823. };
  1824. Select2.prototype._placeContainer = function ($container) {
  1825. $container.insertAfter(this.$element);
  1826. $container.width(this.$element.outerWidth(false));
  1827. };
  1828. Select2.prototype._placeSelection = function ($selection) {
  1829. var $selectionContainer = this.$container.find('.selection');
  1830. $selectionContainer.append($selection);
  1831. };
  1832. Select2.prototype._placeDropdown = function ($dropdown) {
  1833. this.$dropdown = $dropdown;
  1834. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1835. $dropdownContainer.append($dropdown);
  1836. };
  1837. Select2.prototype._placeResults = function ($results) {
  1838. var $resultsContainer = this.$dropdown.find('.results');
  1839. $resultsContainer.append($results);
  1840. };
  1841. Select2.prototype._bindAdapters = function () {
  1842. this.data.bind(this, this.$container);
  1843. this.selection.bind(this, this.$container);
  1844. this.dropdown.bind(this, this.$container);
  1845. this.results.bind(this, this.$container);
  1846. };
  1847. Select2.prototype._registerDomEvents = function () {
  1848. var self = this;
  1849. this.$element.on('change', function () {
  1850. self.data.current(function (data) {
  1851. self.trigger('selection:update', {
  1852. data: data
  1853. });
  1854. });
  1855. });
  1856. };
  1857. Select2.prototype._registerDataEvents = function () {
  1858. var self = this;
  1859. this.data.on('results:message', function (params) {
  1860. self.trigger('results:message', params);
  1861. });
  1862. };
  1863. Select2.prototype._registerSelectionEvents = function () {
  1864. var self = this;
  1865. this.selection.on('open', function () {
  1866. self.open();
  1867. });
  1868. this.selection.on('close', function () {
  1869. self.close();
  1870. });
  1871. this.selection.on('toggle', function () {
  1872. self.toggleDropdown();
  1873. });
  1874. this.selection.on('results:select', function () {
  1875. self.trigger('results:select');
  1876. });
  1877. this.selection.on('results:previous', function () {
  1878. self.trigger('results:previous');
  1879. });
  1880. this.selection.on('results:next', function () {
  1881. self.trigger('results:next');
  1882. });
  1883. this.selection.on('unselected', function (params) {
  1884. self.trigger('unselect', params);
  1885. self.close();
  1886. });
  1887. this.selection.on('keypress', function (e) {
  1888. self.trigger('keypress', e);
  1889. });
  1890. };
  1891. Select2.prototype._registerDropdownEvents = function () {
  1892. var self = this;
  1893. this.dropdown.on('query', function (params) {
  1894. self.trigger('query', params);
  1895. });
  1896. this.dropdown.on('keypress', function (e) {
  1897. self.trigger('keypress', e);
  1898. });
  1899. };
  1900. Select2.prototype._registerResultsEvents = function () {
  1901. var self = this;
  1902. this.results.on('query:append', function (params) {
  1903. self.trigger('query:append', params);
  1904. });
  1905. this.results.on('selected', function (params) {
  1906. self.trigger('select', params);
  1907. self.close();
  1908. });
  1909. this.results.on('unselected', function (params) {
  1910. self.trigger('unselect', params);
  1911. self.close();
  1912. });
  1913. this.results.on('results:focus', function (params) {
  1914. self.trigger('results:focus', params);
  1915. });
  1916. };
  1917. Select2.prototype._registerEvents = function () {
  1918. var self = this;
  1919. this.on('open', function () {
  1920. self.$container.addClass('open');
  1921. });
  1922. this.on('close', function () {
  1923. self.$container.removeClass('open');
  1924. });
  1925. this.on('query', function (params) {
  1926. this.data.query(params, function (data) {
  1927. self.trigger('results:all', {
  1928. data: data,
  1929. query: params
  1930. });
  1931. });
  1932. });
  1933. this.on('query:append', function (params) {
  1934. this.data.query(params, function (data) {
  1935. self.trigger('results:append', {
  1936. data: data,
  1937. query: params
  1938. });
  1939. });
  1940. });
  1941. this.on('keypress', function (evt) {
  1942. var key = evt.which;
  1943. if (self.isOpen()) {
  1944. if (key === KEYS.ENTER) {
  1945. self.trigger('results:select');
  1946. evt.preventDefault();
  1947. } else if (key === KEYS.UP) {
  1948. self.trigger('results:previous');
  1949. evt.preventDefault();
  1950. } else if (key === KEYS.DOWN) {
  1951. self.trigger('results:next');
  1952. evt.preventDefault();
  1953. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1954. self.close();
  1955. evt.preventDefault();
  1956. }
  1957. } else {
  1958. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1959. self.open();
  1960. evt.preventDefault();
  1961. }
  1962. }
  1963. });
  1964. };
  1965. Select2.prototype.toggleDropdown = function () {
  1966. if (this.isOpen()) {
  1967. this.close();
  1968. } else {
  1969. this.open();
  1970. }
  1971. };
  1972. Select2.prototype.open = function () {
  1973. if (this.isOpen()) {
  1974. return;
  1975. }
  1976. this.trigger('query', {});
  1977. this.trigger('open');
  1978. };
  1979. Select2.prototype.close = function () {
  1980. if (!this.isOpen()) {
  1981. return;
  1982. }
  1983. this.trigger('close');
  1984. };
  1985. Select2.prototype.isOpen = function () {
  1986. return this.$container.hasClass('open');
  1987. };
  1988. Select2.prototype.render = function () {
  1989. var $container = $(
  1990. '<span class="select2 select2-container select2-theme-default">' +
  1991. '<span class="selection"></span>' +
  1992. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1993. '</span>'
  1994. );
  1995. this.$container = $container;
  1996. $container.data('element', this.$element);
  1997. return $container;
  1998. };
  1999. return Select2;
  2000. });
  2001. define('jquery.select2',[
  2002. 'jquery',
  2003. 'select2/core'
  2004. ], function ($, Select2) {
  2005. if ($.fn.select2 == null) {
  2006. $.fn.select2 = function (options) {
  2007. options = options || {};
  2008. if (typeof options === 'object') {
  2009. this.each(function () {
  2010. var instance = new Select2($(this), options);
  2011. });
  2012. } else if (typeof options === 'string') {
  2013. var instance = this.data('select2');
  2014. var args = Array.prototype.slice.call(arguments, 1);
  2015. instance[options](args);
  2016. } else {
  2017. throw new Error('Invalid arguments for Select2: ' + options);
  2018. }
  2019. };
  2020. }
  2021. return Select2;
  2022. });
  2023. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());