select2.js 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784
  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 = $.data(this, '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 option = document.createElement('li');
  567. option.className = 'option';
  568. var attrs = {
  569. 'role': 'treeitem',
  570. 'aria-selected': 'false'
  571. };
  572. if (data.disabled) {
  573. delete attrs['aria-selected'];
  574. attrs['aria-disabled'] = 'true';
  575. }
  576. if (data.id == null) {
  577. delete attrs['aria-selected'];
  578. }
  579. if (data._resultId != null) {
  580. option.id = data._resultId;
  581. }
  582. if (data.children) {
  583. attrs.role = 'group';
  584. attrs['aria-label'] = data.text;
  585. delete attrs['aria-selected'];
  586. }
  587. for (var attr in attrs) {
  588. var val = attrs[attr];
  589. option.setAttribute(attr, val);
  590. }
  591. if (data.children) {
  592. var $option = $(option);
  593. var label = document.createElement('strong');
  594. label.className = 'group-label';
  595. var $label = $(label);
  596. this.template(data, label);
  597. var $children = [];
  598. for (var c = 0; c < data.children.length; c++) {
  599. var child = data.children[c];
  600. var $child = this.option(child);
  601. $children.push($child);
  602. }
  603. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  604. $childrenContainer.append($children);
  605. $option.append(label);
  606. $option.append($childrenContainer);
  607. } else {
  608. this.template(data, option);
  609. }
  610. $.data(option, 'data', data);
  611. return option;
  612. };
  613. Results.prototype.bind = function (container, $container) {
  614. var self = this;
  615. var id = container.id + '-results';
  616. this.$results.attr('id', id);
  617. container.on('results:all', function (params) {
  618. self.clear();
  619. self.append(params.data);
  620. if (container.isOpen()) {
  621. self.setClasses();
  622. }
  623. });
  624. container.on('results:append', function (params) {
  625. self.append(params.data);
  626. if (container.isOpen()) {
  627. self.setClasses();
  628. }
  629. });
  630. container.on('select', function () {
  631. if (!container.isOpen()) {
  632. return;
  633. }
  634. self.setClasses();
  635. });
  636. container.on('unselect', function () {
  637. if (!container.isOpen()) {
  638. return;
  639. }
  640. self.setClasses();
  641. });
  642. container.on('open', function () {
  643. // When the dropdown is open, aria-expended="true"
  644. self.$results.attr('aria-expanded', 'true');
  645. self.$results.attr('aria-hidden', 'false');
  646. self.setClasses();
  647. self.ensureHighlightVisible();
  648. });
  649. container.on('close', function () {
  650. // When the dropdown is closed, aria-expended="false"
  651. self.$results.attr('aria-expanded', 'false');
  652. self.$results.attr('aria-hidden', 'true');
  653. self.$results.removeAttr('aria-activedescendant');
  654. });
  655. container.on('results:select', function () {
  656. var $highlighted = self.$results.find('.highlighted');
  657. if ($highlighted.length === 0) {
  658. return;
  659. }
  660. var data = $highlighted.data('data');
  661. if ($highlighted.attr('aria-selected') == 'true') {
  662. self.trigger('unselected', {
  663. data: data
  664. });
  665. } else {
  666. self.trigger('selected', {
  667. data: data
  668. });
  669. }
  670. });
  671. container.on('results:previous', function () {
  672. var $highlighted = self.$results.find('.highlighted');
  673. var $options = self.$results.find('[aria-selected]');
  674. var currentIndex = $options.index($highlighted);
  675. // If we are already at te top, don't move further
  676. if (currentIndex === 0) {
  677. return;
  678. }
  679. var nextIndex = currentIndex - 1;
  680. // If none are highlighted, highlight the first
  681. if ($highlighted.length === 0) {
  682. nextIndex = 0;
  683. }
  684. var $next = $options.eq(nextIndex);
  685. $next.trigger('mouseenter');
  686. var currentOffset = self.$results.offset().top;
  687. var nextTop = $next.offset().top;
  688. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  689. if (nextIndex === 0) {
  690. self.$results.scrollTop(0);
  691. } else if (nextTop - currentOffset < 0) {
  692. self.$results.scrollTop(nextOffset);
  693. }
  694. });
  695. container.on('results:next', function () {
  696. var $highlighted = self.$results.find('.highlighted');
  697. var $options = self.$results.find('[aria-selected]');
  698. var currentIndex = $options.index($highlighted);
  699. var nextIndex = currentIndex + 1;
  700. // If we are at the last option, stay there
  701. if (nextIndex >= $options.length) {
  702. return;
  703. }
  704. var $next = $options.eq(nextIndex);
  705. $next.trigger('mouseenter');
  706. var currentOffset = self.$results.offset().top +
  707. self.$results.outerHeight(false);
  708. var nextBottom = $next.offset().top + $next.outerHeight(false);
  709. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  710. if (nextIndex === 0) {
  711. self.$results.scrollTop(0);
  712. } else if (nextBottom > currentOffset) {
  713. self.$results.scrollTop(nextOffset);
  714. }
  715. });
  716. container.on('results:focus', function (params) {
  717. params.element.addClass('highlighted');
  718. });
  719. container.on('results:message', function (params) {
  720. self.trigger('results:message', params);
  721. });
  722. this.on('results:message', function (params) {
  723. self.displayMessage(params);
  724. });
  725. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  726. var $this = $(this);
  727. var data = $this.data('data');
  728. if ($this.attr('aria-selected') === 'true') {
  729. self.trigger('unselected', {
  730. originalEvent: evt,
  731. data: data
  732. });
  733. return;
  734. }
  735. self.trigger('selected', {
  736. originalEvent: evt,
  737. data: data
  738. });
  739. });
  740. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  741. var data = $(this).data('data');
  742. self.$results.find('.option.highlighted').removeClass('highlighted');
  743. self.trigger('results:focus', {
  744. data: data,
  745. element: $(this)
  746. });
  747. });
  748. this.$results.on('mouseleave', '.option', function (evt) {
  749. if ($(this).hasClass('highlighted')) {
  750. $(this).removeClass('highlighted');
  751. self.$results.removeAttr('aria-activedescendant');
  752. }
  753. });
  754. };
  755. Results.prototype.destroy = function () {
  756. this.$results.remove();
  757. };
  758. Results.prototype.ensureHighlightVisible = function () {
  759. var $highlighted = this.$results.find('.highlighted');
  760. if ($highlighted.length === 0) {
  761. return;
  762. }
  763. var $options = this.$results.find('[aria-selected]');
  764. var currentIndex = $options.index($highlighted);
  765. var currentOffset = this.$results.offset().top;
  766. var nextTop = $highlighted.offset().top;
  767. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  768. var offsetDelta = nextTop - currentOffset;
  769. nextOffset -= $highlighted.outerHeight(false) * 2;
  770. if (currentIndex <= 2) {
  771. this.$results.scrollTop(0);
  772. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  773. this.$results.scrollTop(nextOffset);
  774. }
  775. };
  776. Results.prototype.template = function (result, container) {
  777. var template = this.options.get('templateResult');
  778. container.innerHTML = template(result);
  779. };
  780. return Results;
  781. });
  782. define('select2/selection/base',[
  783. '../utils'
  784. ], function (Utils) {
  785. function BaseSelection ($element, options) {
  786. this.$element = $element;
  787. this.options = options;
  788. BaseSelection.__super__.constructor.call(this);
  789. }
  790. Utils.Extend(BaseSelection, Utils.Observable);
  791. BaseSelection.prototype.render = function () {
  792. throw new Error('The `render` method must be defined in child classes.');
  793. };
  794. BaseSelection.prototype.bind = function (container, $container) {
  795. var self = this;
  796. container.on('selection:update', function (params) {
  797. self.update(params.data);
  798. });
  799. container.on('open', function () {
  800. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  801. var $target = $(e.target);
  802. var $select = $target.closest('.select2');
  803. var $all = $('.select2.open');
  804. $all.each(function () {
  805. var $this = $(this);
  806. if (this == $select[0]) {
  807. return;
  808. }
  809. var $element = $this.data('element');
  810. $element.select2('close');
  811. });
  812. });
  813. container.on('close', function () {
  814. $(document.body).off('mousedown.select2.' + container.id);
  815. });
  816. });
  817. };
  818. BaseSelection.prototype.destroy = function () {
  819. // Unbind the dropdown click handler if it exists
  820. $(document.body).off('.select2.' + container.id);
  821. };
  822. BaseSelection.prototype.update = function (data) {
  823. throw new Error('The `update` method must be defined in child classes.');
  824. };
  825. return BaseSelection;
  826. });
  827. define('select2/keys',[
  828. ], function () {
  829. var KEYS = {
  830. BACKSPACE: 8,
  831. TAB: 9,
  832. ENTER: 13,
  833. SHIFT: 16,
  834. CTRL: 17,
  835. ALT: 18,
  836. ESC: 27,
  837. SPACE: 32,
  838. PAGE_UP: 33,
  839. PAGE_DOWN: 34,
  840. END: 35,
  841. HOME: 36,
  842. LEFT: 37,
  843. UP: 38,
  844. RIGHT: 39,
  845. DOWN: 40,
  846. DELETE: 46,
  847. isArrow: function (k) {
  848. k = k.which ? k.which : k;
  849. switch (k) {
  850. case KEY.LEFT:
  851. case KEY.RIGHT:
  852. case KEY.UP:
  853. case KEY.DOWN:
  854. return true;
  855. }
  856. return false;
  857. }
  858. };
  859. return KEYS;
  860. });
  861. define('select2/selection/single',[
  862. './base',
  863. '../utils',
  864. '../keys'
  865. ], function (BaseSelection, Utils, KEYS) {
  866. function SingleSelection () {
  867. SingleSelection.__super__.constructor.apply(this, arguments);
  868. }
  869. Utils.Extend(SingleSelection, BaseSelection);
  870. SingleSelection.prototype.render = function () {
  871. var $selection = $(
  872. '<span class="single-select" tabindex="0" role="combobox" ' +
  873. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  874. '<span class="rendered-selection"></span>' +
  875. '</span>'
  876. );
  877. $selection.attr('title', this.$element.attr('title'));
  878. this.$selection = $selection;
  879. return $selection;
  880. };
  881. SingleSelection.prototype.bind = function (container, $container) {
  882. var self = this;
  883. SingleSelection.__super__.bind.apply(this, arguments);
  884. var id = container.id + '-container';
  885. var resultsId = container.id + '-results';
  886. this.$selection.find('.rendered-selection').attr('id', id);
  887. this.$selection.attr('aria-labelledby', id);
  888. this.$selection.attr('aria-owns', resultsId);
  889. this.$selection.on('mousedown', function (evt) {
  890. // Only respond to left clicks
  891. if (evt.which !== 1) {
  892. return;
  893. }
  894. self.trigger('toggle', {
  895. originalEvent: evt
  896. });
  897. });
  898. container.on('open', function () {
  899. // When the dropdown is open, aria-expanded="true"
  900. self.$selection.attr('aria-expanded', 'true');
  901. });
  902. container.on('close', function () {
  903. // When the dropdown is closed, aria-expanded="false"
  904. self.$selection.attr('aria-expanded', 'false');
  905. self.$selection.removeAttr('aria-activedescendant');
  906. self.$selection.focus();
  907. });
  908. this.$selection.on('focus', function (evt) {
  909. // User focuses on the container
  910. });
  911. this.$selection.on('blur', function (evt) {
  912. // User exits the container
  913. });
  914. this.$selection.on('keydown', function (evt) {
  915. self.trigger('keypress', evt);
  916. if (evt.which === KEYS.SPACE) {
  917. evt.preventDefault();
  918. }
  919. });
  920. container.on('results:focus', function (params) {
  921. self.$selection.attr('aria-activedescendant', params.data._resultId);
  922. });
  923. container.on('selection:update', function (params) {
  924. self.update(params.data);
  925. });
  926. };
  927. SingleSelection.prototype.clear = function () {
  928. this.$selection.find('.rendered-selection').empty();
  929. };
  930. SingleSelection.prototype.display = function (data) {
  931. var template = this.options.get('templateSelection');
  932. return template(data);
  933. };
  934. SingleSelection.prototype.selectionContainer = function () {
  935. return $('<span></span>');
  936. };
  937. SingleSelection.prototype.update = function (data) {
  938. if (data.length === 0) {
  939. this.clear();
  940. return;
  941. }
  942. var selection = data[0];
  943. var formatted = this.display(selection);
  944. this.$selection.find('.rendered-selection').html(formatted);
  945. };
  946. return SingleSelection;
  947. });
  948. define('select2/selection/multiple',[
  949. './base',
  950. '../utils'
  951. ], function (BaseSelection, Utils) {
  952. function MultipleSelection ($element, options) {
  953. MultipleSelection.__super__.constructor.apply(this, arguments);
  954. }
  955. Utils.Extend(MultipleSelection, BaseSelection);
  956. MultipleSelection.prototype.render = function () {
  957. var $selection = $(
  958. '<span class="multiple-select">' +
  959. '<ul class="rendered-selection"></ul>' +
  960. '</span>'
  961. );
  962. this.$selection = $selection;
  963. return $selection;
  964. };
  965. MultipleSelection.prototype.bind = function (container, $container) {
  966. var self = this;
  967. MultipleSelection.__super__.bind.apply(this, arguments);
  968. this.$selection.on('click', function (evt) {
  969. self.trigger('toggle', {
  970. originalEvent: evt
  971. });
  972. });
  973. this.$selection.on('click', '.remove', function (evt) {
  974. var $remove = $(this);
  975. var $selection = $remove.parent();
  976. var data = $selection.data('data');
  977. self.trigger('unselected', {
  978. originalEvent: evt,
  979. data: data
  980. });
  981. });
  982. };
  983. MultipleSelection.prototype.clear = function () {
  984. this.$selection.find('.rendered-selection').empty();
  985. };
  986. MultipleSelection.prototype.display = function (data) {
  987. var template = this.options.get('templateSelection');
  988. return template(data);
  989. };
  990. MultipleSelection.prototype.selectionContainer = function () {
  991. var $container = $(
  992. '<li class="choice">' +
  993. '<span class="remove" role="presentation">&times;</span>' +
  994. '</li>'
  995. );
  996. return $container;
  997. };
  998. MultipleSelection.prototype.update = function (data) {
  999. this.clear();
  1000. if (data.length === 0) {
  1001. return;
  1002. }
  1003. var $selections = [];
  1004. for (var d = 0; d < data.length; d++) {
  1005. var selection = data[d];
  1006. var formatted = this.display(selection);
  1007. var $selection = this.selectionContainer();
  1008. $selection.append(formatted);
  1009. $selection.data('data', selection);
  1010. $selections.push($selection);
  1011. }
  1012. this.$selection.find('.rendered-selection').append($selections);
  1013. };
  1014. return MultipleSelection;
  1015. });
  1016. define('select2/selection/placeholder',[
  1017. '../utils'
  1018. ], function (Utils) {
  1019. function Placeholder (decorated, $element, options) {
  1020. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1021. decorated.call(this, $element, options);
  1022. }
  1023. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1024. if (typeof placeholder === 'string') {
  1025. placeholder = {
  1026. id: '',
  1027. text: placeholder
  1028. };
  1029. }
  1030. return placeholder;
  1031. };
  1032. Placeholder.prototype.update = function (decorated, data) {
  1033. var singlePlaceholder = (
  1034. data.length == 1 && data[0].id != this.placeholder.id
  1035. );
  1036. var multipleSelections = data.length > 1;
  1037. if (multipleSelections || singlePlaceholder) {
  1038. return decorated.call(this, data);
  1039. }
  1040. this.clear();
  1041. var $placeholder = this.selectionContainer();
  1042. $placeholder.html(this.display(this.placeholder));
  1043. $placeholder.addClass('placeholder').removeClass('choice');
  1044. this.$selection.find('.rendered-selection').append($placeholder);
  1045. };
  1046. return Placeholder;
  1047. });
  1048. define('select2/translation',[
  1049. ], function () {
  1050. function Translation (dict) {
  1051. this.dict = dict || {};
  1052. }
  1053. Translation.prototype.all = function () {
  1054. return this.dict;
  1055. };
  1056. Translation.prototype.get = function (key) {
  1057. return this.dict[key];
  1058. };
  1059. Translation.prototype.extend = function (translation) {
  1060. this.dict = $.extend({}, translation.all(), this.dict);
  1061. };
  1062. // Static functions
  1063. Translation._cache = {};
  1064. Translation.loadPath = function (path) {
  1065. if (!(path in Translation._cache)) {
  1066. var translations = require(path);
  1067. Translation._cache[path] = translations;
  1068. }
  1069. return new Translation(Translation._cache[path]);
  1070. };
  1071. return Translation;
  1072. });
  1073. define('select2/data/base',[
  1074. '../utils'
  1075. ], function (Utils) {
  1076. function BaseAdapter ($element, options) {
  1077. BaseAdapter.__super__.constructor.call(this);
  1078. }
  1079. Utils.Extend(BaseAdapter, Utils.Observable);
  1080. BaseAdapter.prototype.current = function (callback) {
  1081. throw new Error('The `current` method must be defined in child classes.');
  1082. };
  1083. BaseAdapter.prototype.query = function (params, callback) {
  1084. throw new Error('The `query` method must be defined in child classes.');
  1085. };
  1086. BaseAdapter.prototype.bind = function (container, $container) {
  1087. // Can be implemented in subclasses
  1088. };
  1089. BaseAdapter.prototype.destroy = function () {
  1090. // Can be implemented in subclasses
  1091. };
  1092. BaseAdapter.prototype.generateResultId = function (container, data) {
  1093. var id = container.id + '-result-';
  1094. id += Utils.generateChars(4);
  1095. if (data.id != null) {
  1096. id += '-' + data.id.toString();
  1097. } else {
  1098. id += '-' + Utils.generateChars(4);
  1099. }
  1100. return id;
  1101. };
  1102. return BaseAdapter;
  1103. });
  1104. define('select2/data/select',[
  1105. './base',
  1106. '../utils',
  1107. 'jquery'
  1108. ], function (BaseAdapter, Utils, $) {
  1109. function SelectAdapter ($element, options) {
  1110. this.$element = $element;
  1111. this.options = options;
  1112. SelectAdapter.__super__.constructor.call(this);
  1113. }
  1114. Utils.Extend(SelectAdapter, BaseAdapter);
  1115. SelectAdapter.prototype.current = function (callback) {
  1116. var data = [];
  1117. var self = this;
  1118. this.$element.find(':selected').each(function () {
  1119. var $option = $(this);
  1120. var option = self.item($option);
  1121. data.push(option);
  1122. });
  1123. callback(data);
  1124. };
  1125. SelectAdapter.prototype.select = function (data) {
  1126. var self = this;
  1127. if (this.$element.prop('multiple')) {
  1128. this.current(function (currentData) {
  1129. var val = [];
  1130. data = [data];
  1131. data.push.apply(data, currentData);
  1132. for (var d = 0; d < data.length; d++) {
  1133. id = data[d].id;
  1134. if (val.indexOf(id) === -1) {
  1135. val.push(id);
  1136. }
  1137. }
  1138. self.$element.val(val);
  1139. self.$element.trigger('change');
  1140. });
  1141. } else {
  1142. var val = data.id;
  1143. this.$element.val(val);
  1144. this.$element.trigger('change');
  1145. }
  1146. };
  1147. SelectAdapter.prototype.unselect = function (data) {
  1148. var self = this;
  1149. if (!this.$element.prop('multiple')) {
  1150. return;
  1151. }
  1152. this.current(function (currentData) {
  1153. var val = [];
  1154. for (var d = 0; d < currentData.length; d++) {
  1155. id = currentData[d].id;
  1156. if (id !== data.id && val.indexOf(id) === -1) {
  1157. val.push(id);
  1158. }
  1159. }
  1160. self.$element.val(val);
  1161. self.$element.trigger('change');
  1162. });
  1163. };
  1164. SelectAdapter.prototype.bind = function (container, $container) {
  1165. var self = this;
  1166. this.container = container;
  1167. container.on('select', function (params) {
  1168. self.select(params.data);
  1169. });
  1170. container.on('unselect', function (params) {
  1171. self.unselect(params.data);
  1172. });
  1173. };
  1174. SelectAdapter.prototype.destroy = function () {
  1175. // Remove anything added to child elements
  1176. this.$element.find('*').each(function () {
  1177. // Remove any custom data set by Select2
  1178. $.removeData(this, 'data');
  1179. });
  1180. };
  1181. SelectAdapter.prototype.query = function (params, callback) {
  1182. var data = [];
  1183. var self = this;
  1184. var $options = this.$element.children();
  1185. $options.each(function () {
  1186. var $option = $(this);
  1187. if (!$option.is('option') && !$option.is('optgroup')) {
  1188. return;
  1189. }
  1190. var option = self.item($option);
  1191. var matches = self.matches(params, option);
  1192. if (matches !== null) {
  1193. data.push(matches);
  1194. }
  1195. });
  1196. callback(data);
  1197. };
  1198. SelectAdapter.prototype.option = function (data) {
  1199. var option = document.createElement('option');
  1200. option.value = data.id;
  1201. if (data.disabled) {
  1202. option.disabled = true;
  1203. }
  1204. if (data.selected) {
  1205. option.selected = true;
  1206. }
  1207. option.innerText = data.text;
  1208. var $option = $(option);
  1209. var normalizedData = this._normalizeItem(data);
  1210. // Override the option's data with the combined data
  1211. $.data(option, 'data', normalizedData);
  1212. return $option;
  1213. };
  1214. SelectAdapter.prototype.item = function ($option) {
  1215. var data = {};
  1216. data = $.data($option[0], 'data');
  1217. if (data != null) {
  1218. return data;
  1219. }
  1220. if ($option.is('option')) {
  1221. data = {
  1222. id: $option.val(),
  1223. text: $option.html(),
  1224. disabled: $option.prop('disabled'),
  1225. selected: $option.prop('selected')
  1226. };
  1227. } else if ($option.is('optgroup')) {
  1228. data = {
  1229. text: $option.attr('label'),
  1230. children: []
  1231. };
  1232. var $children = $option.children('option');
  1233. var children = [];
  1234. for (var c = 0; c < $children.length; c++) {
  1235. var $child = $($children[c]);
  1236. var child = this.item($child);
  1237. children.push(child);
  1238. }
  1239. data.children = children;
  1240. }
  1241. data = this._normalizeItem(data);
  1242. $.data($option[0], 'data', data);
  1243. return data;
  1244. };
  1245. SelectAdapter.prototype._normalizeItem = function (item) {
  1246. if (item.text == null) {
  1247. item = {
  1248. id: item,
  1249. text: item
  1250. };
  1251. }
  1252. var defaults = {
  1253. selected: false,
  1254. disabled: false
  1255. };
  1256. if (item.id != null) {
  1257. item.id = item.id.toString();
  1258. }
  1259. if (item.text != null) {
  1260. item.text = item.text.toString();
  1261. }
  1262. if (item._resultId == null && item.id && this.container != null) {
  1263. item._resultId = this.generateResultId(this.container, item);
  1264. }
  1265. return $.extend({}, defaults, item);
  1266. };
  1267. SelectAdapter.prototype.matches = function (params, data) {
  1268. var matcher = this.options.get('matcher');
  1269. return matcher(params, data);
  1270. };
  1271. return SelectAdapter;
  1272. });
  1273. define('select2/data/array',[
  1274. './select',
  1275. '../utils',
  1276. 'jquery'
  1277. ], function (SelectAdapter, Utils, $) {
  1278. function ArrayAdapter ($element, options) {
  1279. var data = options.get('data');
  1280. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1281. this.convertToOptions(data);
  1282. }
  1283. Utils.Extend(ArrayAdapter, SelectAdapter);
  1284. ArrayAdapter.prototype.select = function (data) {
  1285. var $option = this.$element.find('option[value="' + data.id + '"]');
  1286. if ($option.length === 0) {
  1287. $option = this.option(data);
  1288. this.$element.append($option);
  1289. }
  1290. ArrayAdapter.__super__.select.call(this, data);
  1291. };
  1292. ArrayAdapter.prototype.convertToOptions = function (data) {
  1293. var self = this;
  1294. var $existing = this.$element.find('option');
  1295. var existingIds = $existing.map(function () {
  1296. return self.item($(this)).id;
  1297. }).get();
  1298. // Filter out all items except for the one passed in the argument
  1299. function onlyItem (item) {
  1300. return function () {
  1301. return $(this).val() == item.id;
  1302. };
  1303. }
  1304. for (var d = 0; d < data.length; d++) {
  1305. var item = data[d];
  1306. item.id = item.id.toString();
  1307. // Skip items which were pre-loaded, only merge the data
  1308. if (existingIds.indexOf(item.id) >= 0) {
  1309. var $existingOption = $existing.filter(onlyItem(item));
  1310. var existingData = this.item($existingOption);
  1311. var newData = $.extend(true, {}, existingData, item);
  1312. var $newOption = this.option(existingData);
  1313. $existingOption.replaceWith($newOption);
  1314. continue;
  1315. }
  1316. var $option = this.option(item);
  1317. this.$element.append($option);
  1318. }
  1319. };
  1320. return ArrayAdapter;
  1321. });
  1322. define('select2/data/ajax',[
  1323. './array',
  1324. '../utils',
  1325. 'jquery'
  1326. ], function (ArrayAdapter, Utils, $) {
  1327. function AjaxAdapter ($element, options) {
  1328. this.ajaxOptions = options.get('ajax');
  1329. if (this.ajaxOptions.processResults != null) {
  1330. this.processResults = this.ajaxOptions.processResults;
  1331. }
  1332. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1333. }
  1334. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1335. AjaxAdapter.prototype.processResults = function (results) {
  1336. return results;
  1337. };
  1338. AjaxAdapter.prototype.query = function (params, callback) {
  1339. var matches = [];
  1340. var self = this;
  1341. var options = $.extend({
  1342. type: 'GET'
  1343. }, this.ajaxOptions);
  1344. if (typeof options.url === 'function') {
  1345. options.url = options.url(params);
  1346. }
  1347. if (typeof options.data === 'function') {
  1348. options.data = options.data(params);
  1349. }
  1350. function request () {
  1351. var $request = $.ajax(options);
  1352. $request.success(function (data) {
  1353. var results = self.processResults(data);
  1354. callback(results);
  1355. });
  1356. }
  1357. if (this.ajaxOptions.delay && params.term !== '') {
  1358. if (this._queryTimeout) {
  1359. window.clearTimeout(this._queryTimeout);
  1360. }
  1361. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  1362. } else {
  1363. request();
  1364. }
  1365. };
  1366. return AjaxAdapter;
  1367. });
  1368. define('select2/data/tags',[
  1369. ], function () {
  1370. function Tags (decorated, $element, options) {
  1371. var tags = options.get('tags');
  1372. decorated.call(this, $element, options);
  1373. if ($.isArray(tags)) {
  1374. for (var t = 0; t < tags.length; t++) {
  1375. var tag = tags[t];
  1376. var item = this._normalizeItem(tag);
  1377. var $option = this.option(item);
  1378. this.$element.append($option);
  1379. }
  1380. }
  1381. }
  1382. Tags.prototype.query = function (decorated, params, callback) {
  1383. var self = this;
  1384. this._removeOldTags();
  1385. if (params.term == null || params.term === '' || params.page != null) {
  1386. decorated.call(this, params, callback);
  1387. return;
  1388. }
  1389. function wrapper (data, child) {
  1390. for (var i = 0; i < data.length; i++) {
  1391. var option = data[i];
  1392. var checkChildren = (
  1393. option.children != null && !wrapper(option.children, true)
  1394. );
  1395. var checkText = option.text === params.term;
  1396. if (checkText || checkChildren) {
  1397. if (child) {
  1398. return false;
  1399. }
  1400. callback(data);
  1401. return;
  1402. }
  1403. }
  1404. if (child) {
  1405. return true;
  1406. }
  1407. var tag = self.createTag(params);
  1408. var $option = self.option(tag);
  1409. $option.attr('data-select2-tag', true);
  1410. self.$element.append($option);
  1411. self.insertTag(data, tag);
  1412. callback(data);
  1413. }
  1414. decorated.call(this, params, wrapper);
  1415. };
  1416. Tags.prototype.createTag = function (decorated, params) {
  1417. return {
  1418. id: params.term,
  1419. text: params.term
  1420. };
  1421. };
  1422. Tags.prototype.insertTag = function (_, data, tag) {
  1423. data.unshift(tag);
  1424. };
  1425. Tags.prototype._removeOldTags = function (_) {
  1426. var tag = this._lastTag;
  1427. var $options = this.$element.find('option[data-select2-tag]');
  1428. $options.each(function () {
  1429. if (this.selected) {
  1430. return;
  1431. }
  1432. $(this).remove();
  1433. });
  1434. };
  1435. return Tags;
  1436. });
  1437. define('select2/data/minimumInputLength',[
  1438. ], function () {
  1439. function MinimumInputLength (decorated, $e, options) {
  1440. this.minimumInputLength = options.get('minimumInputLength');
  1441. decorated.call(this, $e, options);
  1442. }
  1443. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1444. params.term = params.term || '';
  1445. if (params.term.length < this.minimumInputLength) {
  1446. this.trigger('results:message', {
  1447. message: 'inputTooShort',
  1448. args: {
  1449. minimum: this.minimumInputLength,
  1450. input: params.term,
  1451. params: params
  1452. }
  1453. });
  1454. return;
  1455. }
  1456. decorated.call(this, params, callback);
  1457. };
  1458. return MinimumInputLength;
  1459. });
  1460. define('select2/dropdown',[
  1461. './utils'
  1462. ], function (Utils) {
  1463. function Dropdown ($element, options) {
  1464. this.$element = $element;
  1465. }
  1466. Utils.Extend(Dropdown, Utils.Observable);
  1467. Dropdown.prototype.render = function () {
  1468. var $dropdown = $(
  1469. '<span class="dropdown">' +
  1470. '<span class="results"></span>' +
  1471. '</span>'
  1472. );
  1473. this.$dropdown = $dropdown;
  1474. return $dropdown;
  1475. };
  1476. Dropdown.prototype.destroy = function () {
  1477. // Remove the dropdown from the DOM
  1478. this.$dropdown.remove();
  1479. };
  1480. Dropdown.prototype.bind = function (container, $container) {
  1481. // Can be implemented in subclasses
  1482. };
  1483. return Dropdown;
  1484. });
  1485. define('select2/dropdown/search',[
  1486. '../utils'
  1487. ], function (Utils) {
  1488. function Search () { }
  1489. Search.prototype.render = function (decorated) {
  1490. var $rendered = decorated.call(this);
  1491. var $search = $(
  1492. '<span class="search">' +
  1493. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1494. '</span>'
  1495. );
  1496. this.$searchContainer = $search;
  1497. this.$search = $search.find('input');
  1498. $rendered.prepend($search);
  1499. return $rendered;
  1500. };
  1501. Search.prototype.bind = function (decorated, container, $container) {
  1502. var self = this;
  1503. decorated.call(this, container, $container);
  1504. this.$search.on('keydown', function (evt) {
  1505. self.trigger('keypress', evt);
  1506. self._keyUpPrevented = evt.isDefaultPrevented();
  1507. });
  1508. this.$search.on('keyup', function (evt) {
  1509. self.handleSearch(evt);
  1510. });
  1511. container.on('open', function () {
  1512. self.$search.attr('tabindex', 0);
  1513. self.$search.focus();
  1514. });
  1515. container.on('close', function () {
  1516. self.$search.attr('tabindex', -1);
  1517. self.$search.val('');
  1518. });
  1519. container.on('results:all', function (params) {
  1520. if (params.query.term == null || params.query.term === '') {
  1521. var showSearch = self.showSearch(params);
  1522. if (showSearch) {
  1523. self.$searchContainer.show();
  1524. } else {
  1525. self.$searchContainer.hide();
  1526. }
  1527. }
  1528. });
  1529. };
  1530. Search.prototype.handleSearch = function (evt) {
  1531. if (!this._keyUpPrevented) {
  1532. var input = this.$search.val();
  1533. this.trigger('query', {
  1534. term: input
  1535. });
  1536. }
  1537. this._keyUpPrevented = false;
  1538. };
  1539. Search.prototype.showSearch = function (_, params) {
  1540. return true;
  1541. };
  1542. return Search;
  1543. });
  1544. define('select2/dropdown/hidePlaceholder',[
  1545. ], function () {
  1546. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1547. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1548. decorated.call(this, $element, options, dataAdapter);
  1549. }
  1550. HidePlaceholder.prototype.append = function (decorated, data) {
  1551. data = this.removePlaceholder(data);
  1552. decorated.call(this, data);
  1553. };
  1554. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1555. if (typeof placeholder === 'string') {
  1556. placeholder = {
  1557. id: '',
  1558. text: placeholder
  1559. };
  1560. }
  1561. return placeholder;
  1562. };
  1563. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1564. var modifiedData = data.slice(0);
  1565. for (var d = data.length - 1; d >= 0; d--) {
  1566. var item = data[d];
  1567. if (this.placeholder.id === item.id) {
  1568. modifiedData.splice(d, 1);
  1569. }
  1570. }
  1571. return modifiedData;
  1572. };
  1573. return HidePlaceholder;
  1574. });
  1575. define('select2/dropdown/infiniteScroll',[
  1576. 'jquery'
  1577. ], function ($) {
  1578. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1579. this.lastParams = {};
  1580. decorated.call(this, $element, options, dataAdapter);
  1581. this.$loadingMore = this.createLoadingMore();
  1582. this.loading = false;
  1583. }
  1584. InfiniteScroll.prototype.append = function (decorated, data) {
  1585. this.$loadingMore.remove();
  1586. decorated.call(this, data);
  1587. if (data.length > 0) {
  1588. this.$results.append(this.$loadingMore);
  1589. }
  1590. this.loading = false;
  1591. };
  1592. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1593. var self = this;
  1594. decorated.call(this, container, $container);
  1595. container.on('query', function (params) {
  1596. self.lastParams = params;
  1597. self.loading = true;
  1598. });
  1599. container.on('query:append', function (params) {
  1600. self.lastParams = params;
  1601. self.loading = true;
  1602. });
  1603. this.$results.on('scroll', function () {
  1604. var loadMoreVisible = $.contains(
  1605. document.documentElement,
  1606. self.$loadingMore[0]
  1607. );
  1608. if (self.loading || !loadMoreVisible) {
  1609. return;
  1610. }
  1611. var currentOffset = self.$results.offset().top +
  1612. self.$results.outerHeight(false);
  1613. var loadingMoreOffset = self.$loadingMore.offset().top +
  1614. self.$loadingMore.outerHeight(false);
  1615. if (currentOffset + 50 >= loadingMoreOffset) {
  1616. self.loadMore();
  1617. }
  1618. });
  1619. };
  1620. InfiniteScroll.prototype.loadMore = function () {
  1621. this.loading = true;
  1622. var params = $.extend({}, {page: 1}, this.lastParams);
  1623. params.page++;
  1624. this.trigger('query:append', params);
  1625. };
  1626. InfiniteScroll.prototype.createLoadingMore = function () {
  1627. var $option = $(
  1628. '<li class="option load-more" role="treeitem"></li>'
  1629. );
  1630. var message = this.options.get('translations').get('loadingMore');
  1631. $option.html(message(this.lastParams));
  1632. return $option;
  1633. };
  1634. return InfiniteScroll;
  1635. });
  1636. define('select2/i18n/en',[],function () {
  1637. return {
  1638. inputTooShort: function (args) {
  1639. var remainingChars = args.minimum - args.input.length;
  1640. var message = 'Please enter ' + remainingChars + ' or more character';
  1641. if (remainingChars != 1) {
  1642. message += 's';
  1643. }
  1644. return message;
  1645. },
  1646. loadingMore: function () {
  1647. return 'Loading more results…';
  1648. },
  1649. noResults: function () {
  1650. return 'No results found';
  1651. }
  1652. };
  1653. });
  1654. define('select2/defaults',[
  1655. 'jquery',
  1656. './results',
  1657. './selection/single',
  1658. './selection/multiple',
  1659. './selection/placeholder',
  1660. './utils',
  1661. './translation',
  1662. './data/select',
  1663. './data/array',
  1664. './data/ajax',
  1665. './data/tags',
  1666. './data/minimumInputLength',
  1667. './dropdown',
  1668. './dropdown/search',
  1669. './dropdown/hidePlaceholder',
  1670. './dropdown/infiniteScroll',
  1671. './i18n/en'
  1672. ], function ($, ResultsList,
  1673. SingleSelection, MultipleSelection, Placeholder,
  1674. Utils, Translation,
  1675. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1676. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1677. EnglishTranslation) {
  1678. function Defaults () {
  1679. this.reset();
  1680. }
  1681. Defaults.prototype.apply = function (options) {
  1682. options = $.extend({}, this.defaults, options);
  1683. if (options.dataAdapter == null) {
  1684. if (options.ajax != null) {
  1685. options.dataAdapter = AjaxData;
  1686. } else if (options.data != null) {
  1687. options.dataAdapter = ArrayData;
  1688. } else {
  1689. options.dataAdapter = SelectData;
  1690. }
  1691. }
  1692. if (options.minimumInputLength > 0) {
  1693. options.dataAdapter = Utils.Decorate(
  1694. options.dataAdapter,
  1695. MinimumInputLength
  1696. );
  1697. }
  1698. if (options.tags != null) {
  1699. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1700. }
  1701. if (options.resultsAdapter == null) {
  1702. options.resultsAdapter = ResultsList;
  1703. if (options.ajax != null) {
  1704. options.resultsAdapter = Utils.Decorate(
  1705. options.resultsAdapter,
  1706. InfiniteScroll
  1707. );
  1708. }
  1709. if (options.placeholder != null) {
  1710. options.resultsAdapter = Utils.Decorate(
  1711. options.resultsAdapter,
  1712. HidePlaceholder
  1713. );
  1714. }
  1715. }
  1716. if (options.dropdownAdapter == null) {
  1717. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1718. options.dropdownAdapter = SearchableDropdown;
  1719. }
  1720. if (options.selectionAdapter == null) {
  1721. if (options.multiple) {
  1722. options.selectionAdapter = MultipleSelection;
  1723. } else {
  1724. options.selectionAdapter = SingleSelection;
  1725. }
  1726. // Add the placeholder mixin if a placeholder was specified
  1727. if (options.placeholder != null) {
  1728. options.selectionAdapter = Utils.Decorate(
  1729. options.selectionAdapter,
  1730. Placeholder
  1731. );
  1732. }
  1733. }
  1734. if (typeof options.language === 'string') {
  1735. options.language = [options.language];
  1736. }
  1737. if ($.isArray(options.language)) {
  1738. var languages = new Translation();
  1739. var languageNames = options.language.concat(this.defaults.language);
  1740. for (var l = 0; l < languageNames.length; l++) {
  1741. var name = languageNames[l];
  1742. var language = {};
  1743. try {
  1744. // Try to load it with the original name
  1745. language = Translation.loadPath(name);
  1746. } catch (e) {
  1747. // If we couldn't load it, check if it wasn't the full path
  1748. name = 'select2/i18n/' + name;
  1749. language = Translation.loadPath(name);
  1750. }
  1751. languages.extend(language);
  1752. }
  1753. options.translations = languages;
  1754. } else {
  1755. options.translations = new Translations(options.language);
  1756. }
  1757. return options;
  1758. };
  1759. Defaults.prototype.reset = function () {
  1760. function matcher (params, data) {
  1761. // Always return the object if there is nothing to compare
  1762. if ($.trim(params.term) === '') {
  1763. return data;
  1764. }
  1765. // Do a recursive check for options with children
  1766. if (data.children && data.children.length > 0) {
  1767. // Clone the data object if there are children
  1768. // This is required as we modify the object to remove any non-matches
  1769. var match = $.extend(true, {}, data);
  1770. // Check each child of the option
  1771. for (var c = data.children.length - 1; c >= 0; c--) {
  1772. var child = data.children[c];
  1773. var matches = matcher(params, child);
  1774. // If there wasn't a match, remove the object in the array
  1775. if (matches == null) {
  1776. match.children.splice(c, 1);
  1777. }
  1778. }
  1779. // If any children matched, return the new object
  1780. if (match.children.length > 0) {
  1781. return match;
  1782. }
  1783. // If there were no matching children, check just the plain object
  1784. return matcher(params, match);
  1785. }
  1786. // Check if the text contains the term
  1787. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1788. return data;
  1789. }
  1790. // If it doesn't contain the term, don't return anything
  1791. return null;
  1792. }
  1793. this.defaults = {
  1794. language: ['select2/i18n/en'],
  1795. matcher: matcher,
  1796. minimumInputLength: 0,
  1797. theme: 'default',
  1798. templateResult: function (result) {
  1799. return result.text;
  1800. },
  1801. templateSelection: function (selection) {
  1802. return selection.text;
  1803. }
  1804. };
  1805. };
  1806. var defaults = new Defaults();
  1807. return defaults;
  1808. });
  1809. define('select2/options',[
  1810. './defaults'
  1811. ], function (Defaults) {
  1812. function Options (options, $element) {
  1813. this.options = options;
  1814. if ($element != null) {
  1815. this.fromElement($element);
  1816. }
  1817. this.options = Defaults.apply(this.options);
  1818. }
  1819. Options.prototype.fromElement = function ($e) {
  1820. if (this.options.multiple == null) {
  1821. this.options.multiple = $e.prop('multiple');
  1822. }
  1823. return this;
  1824. };
  1825. Options.prototype.get = function (key) {
  1826. return this.options[key];
  1827. };
  1828. Options.prototype.set = function (key, val) {
  1829. this.options[key] = val;
  1830. };
  1831. return Options;
  1832. });
  1833. define('select2/core',[
  1834. 'jquery',
  1835. './options',
  1836. './utils',
  1837. './keys'
  1838. ], function ($, Options, Utils, KEYS) {
  1839. var Select2 = function ($element, options) {
  1840. if ($element.data('select2') != null) {
  1841. return;
  1842. }
  1843. this.$element = $element;
  1844. this.id = this._generateId($element);
  1845. options = options || {};
  1846. this.options = new Options(options, $element);
  1847. Select2.__super__.constructor.call(this);
  1848. // Set up containers and adapters
  1849. var DataAdapter = this.options.get('dataAdapter');
  1850. this.data = new DataAdapter($element, this.options);
  1851. var $container = this.render();
  1852. this._placeContainer($container);
  1853. var SelectionAdapter = this.options.get('selectionAdapter');
  1854. this.selection = new SelectionAdapter($element, this.options);
  1855. var $selection = this.selection.render();
  1856. this._placeSelection($selection);
  1857. var DropdownAdapter = this.options.get('dropdownAdapter');
  1858. this.dropdown = new DropdownAdapter($element, this.options);
  1859. var $dropdown = this.dropdown.render();
  1860. this._placeDropdown($dropdown);
  1861. var ResultsAdapter = this.options.get('resultsAdapter');
  1862. this.results = new ResultsAdapter($element, this.options, this.data);
  1863. var $results = this.results.render();
  1864. this._placeResults($results);
  1865. // Bind events
  1866. var self = this;
  1867. // Bind the container to all of the adapters
  1868. this._bindAdapters();
  1869. // Register any DOM event handlers
  1870. this._registerDomEvents();
  1871. // Register any internal event handlers
  1872. this._registerDataEvents();
  1873. this._registerSelectionEvents();
  1874. this._registerDropdownEvents();
  1875. this._registerResultsEvents();
  1876. this._registerEvents();
  1877. // Set the initial state
  1878. this.data.current(function (initialData) {
  1879. self.trigger('selection:update', {
  1880. data: initialData
  1881. });
  1882. });
  1883. // Hide the original select
  1884. $element.hide();
  1885. this._tabindex = $element.attr('tabindex') || 0;
  1886. $element.attr('tabindex', '-1');
  1887. $element.data('select2', this);
  1888. };
  1889. Utils.Extend(Select2, Utils.Observable);
  1890. Select2.prototype._generateId = function ($element) {
  1891. var id = '';
  1892. if ($element.attr('id') != null) {
  1893. id = $element.attr('id');
  1894. } else if ($element.attr('name') != null) {
  1895. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1896. } else {
  1897. id = Utils.generateChars(4);
  1898. }
  1899. id = 'select2-' + id;
  1900. return id;
  1901. };
  1902. Select2.prototype._placeContainer = function ($container) {
  1903. $container.insertAfter(this.$element);
  1904. $container.width(this.$element.outerWidth(false));
  1905. };
  1906. Select2.prototype._placeSelection = function ($selection) {
  1907. var $selectionContainer = this.$container.find('.selection');
  1908. $selectionContainer.append($selection);
  1909. };
  1910. Select2.prototype._placeDropdown = function ($dropdown) {
  1911. this.$dropdown = $dropdown;
  1912. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1913. $dropdownContainer.append($dropdown);
  1914. };
  1915. Select2.prototype._placeResults = function ($results) {
  1916. var $resultsContainer = this.$dropdown.find('.results');
  1917. $resultsContainer.append($results);
  1918. };
  1919. Select2.prototype._bindAdapters = function () {
  1920. this.data.bind(this, this.$container);
  1921. this.selection.bind(this, this.$container);
  1922. this.dropdown.bind(this, this.$container);
  1923. this.results.bind(this, this.$container);
  1924. };
  1925. Select2.prototype._registerDomEvents = function () {
  1926. var self = this;
  1927. this.$element.on('change.select2', function () {
  1928. self.data.current(function (data) {
  1929. self.trigger('selection:update', {
  1930. data: data
  1931. });
  1932. });
  1933. });
  1934. };
  1935. Select2.prototype._registerDataEvents = function () {
  1936. var self = this;
  1937. this.data.on('results:message', function (params) {
  1938. self.trigger('results:message', params);
  1939. });
  1940. };
  1941. Select2.prototype._registerSelectionEvents = function () {
  1942. var self = this;
  1943. this.selection.on('open', function () {
  1944. self.open();
  1945. });
  1946. this.selection.on('close', function () {
  1947. self.close();
  1948. });
  1949. this.selection.on('toggle', function () {
  1950. self.toggleDropdown();
  1951. });
  1952. this.selection.on('results:select', function () {
  1953. self.trigger('results:select');
  1954. });
  1955. this.selection.on('results:previous', function () {
  1956. self.trigger('results:previous');
  1957. });
  1958. this.selection.on('results:next', function () {
  1959. self.trigger('results:next');
  1960. });
  1961. this.selection.on('unselected', function (params) {
  1962. self.trigger('unselect', params);
  1963. self.close();
  1964. });
  1965. this.selection.on('keypress', function (e) {
  1966. self.trigger('keypress', e);
  1967. });
  1968. };
  1969. Select2.prototype._registerDropdownEvents = function () {
  1970. var self = this;
  1971. this.dropdown.on('query', function (params) {
  1972. self.trigger('query', params);
  1973. });
  1974. this.dropdown.on('keypress', function (e) {
  1975. self.trigger('keypress', e);
  1976. });
  1977. };
  1978. Select2.prototype._registerResultsEvents = function () {
  1979. var self = this;
  1980. this.results.on('query:append', function (params) {
  1981. self.trigger('query:append', params);
  1982. });
  1983. this.results.on('selected', function (params) {
  1984. self.trigger('select', params);
  1985. self.close();
  1986. });
  1987. this.results.on('unselected', function (params) {
  1988. self.trigger('unselect', params);
  1989. self.close();
  1990. });
  1991. this.results.on('results:focus', function (params) {
  1992. self.trigger('results:focus', params);
  1993. });
  1994. };
  1995. Select2.prototype._registerEvents = function () {
  1996. var self = this;
  1997. this.on('open', function () {
  1998. self.$container.addClass('open');
  1999. });
  2000. this.on('close', function () {
  2001. self.$container.removeClass('open');
  2002. });
  2003. this.on('query', function (params) {
  2004. this.data.query(params, function (data) {
  2005. self.trigger('results:all', {
  2006. data: data,
  2007. query: params
  2008. });
  2009. });
  2010. });
  2011. this.on('query:append', function (params) {
  2012. this.data.query(params, function (data) {
  2013. self.trigger('results:append', {
  2014. data: data,
  2015. query: params
  2016. });
  2017. });
  2018. });
  2019. this.on('keypress', function (evt) {
  2020. var key = evt.which;
  2021. if (self.isOpen()) {
  2022. if (key === KEYS.ENTER) {
  2023. self.trigger('results:select');
  2024. evt.preventDefault();
  2025. } else if (key === KEYS.UP) {
  2026. self.trigger('results:previous');
  2027. evt.preventDefault();
  2028. } else if (key === KEYS.DOWN) {
  2029. self.trigger('results:next');
  2030. evt.preventDefault();
  2031. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  2032. self.close();
  2033. evt.preventDefault();
  2034. }
  2035. } else {
  2036. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  2037. self.open();
  2038. evt.preventDefault();
  2039. }
  2040. }
  2041. });
  2042. };
  2043. Select2.prototype.toggleDropdown = function () {
  2044. if (this.isOpen()) {
  2045. this.close();
  2046. } else {
  2047. this.open();
  2048. }
  2049. };
  2050. Select2.prototype.open = function () {
  2051. if (this.isOpen()) {
  2052. return;
  2053. }
  2054. this.trigger('query', {});
  2055. this.trigger('open');
  2056. };
  2057. Select2.prototype.close = function () {
  2058. if (!this.isOpen()) {
  2059. return;
  2060. }
  2061. this.trigger('close');
  2062. };
  2063. Select2.prototype.isOpen = function () {
  2064. return this.$container.hasClass('open');
  2065. };
  2066. Select2.prototype.destroy = function () {
  2067. this.$container.remove();
  2068. this.$element.off('.select2');
  2069. this.$element.attr('tabindex', this._tabindex);
  2070. this.$element.show();
  2071. this.$element.removeData('select2');
  2072. this.data.destroy();
  2073. this.selection.destroy();
  2074. this.dropdown.destroy();
  2075. this.results.destroy();
  2076. this.data = null;
  2077. this.selection = null;
  2078. this.dropdown = null;
  2079. this.results = null;
  2080. };
  2081. Select2.prototype.render = function () {
  2082. var $container = $(
  2083. '<span class="select2 select2-container">' +
  2084. '<span class="selection"></span>' +
  2085. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  2086. '</span>'
  2087. );
  2088. this.$container = $container;
  2089. this.$container.addClass('select2-theme-' + this.options.get('theme'));
  2090. $container.data('element', this.$element);
  2091. return $container;
  2092. };
  2093. return Select2;
  2094. });
  2095. define('jquery.select2',[
  2096. 'jquery',
  2097. 'select2/core'
  2098. ], function ($, Select2) {
  2099. if ($.fn.select2 == null) {
  2100. $.fn.select2 = function (options) {
  2101. options = options || {};
  2102. if (typeof options === 'object') {
  2103. this.each(function () {
  2104. var instance = new Select2($(this), options);
  2105. });
  2106. } else if (typeof options === 'string') {
  2107. var instance = this.data('select2');
  2108. var args = Array.prototype.slice.call(arguments, 1);
  2109. instance[options](args);
  2110. } else {
  2111. throw new Error('Invalid arguments for Select2: ' + options);
  2112. }
  2113. };
  2114. }
  2115. return Select2;
  2116. });
  2117. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());