select2.js 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403
  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.empty = function () {
  510. var $empty = $('<li role="treeitem" class="option"></li>');
  511. $empty.text(this.options.get('translations').get('noResults'));
  512. this.$results.append($empty);
  513. };
  514. Results.prototype.append = function (data) {
  515. var $options = [];
  516. if (data.length === 0) {
  517. this.empty();
  518. return;
  519. }
  520. data = this.sort(data);
  521. for (var d = 0; d < data.length; d++) {
  522. var item = data[d];
  523. var $option = this.option(item);
  524. $options.push($option);
  525. }
  526. this.$results.append($options);
  527. };
  528. Results.prototype.sort = function (data) {
  529. return data;
  530. };
  531. Results.prototype.setClasses = function () {
  532. var self = this;
  533. this.data.current(function (selected) {
  534. var selectedIds = $.map(selected, function (s) {
  535. return s.id.toString();
  536. });
  537. var $options = self.$results.find('.option[aria-selected]');
  538. $options.each(function () {
  539. var $option = $(this);
  540. var item = $option.data('data');
  541. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  542. $option.attr('aria-selected', 'true');
  543. } else {
  544. $option.attr('aria-selected', 'false');
  545. }
  546. });
  547. var $selected = $options.filter('[aria-selected=true]');
  548. // Check if there are any selected options
  549. if ($selected.length > 0) {
  550. // If there are selected options, highlight the first
  551. $selected.first().trigger('mouseenter');
  552. } else {
  553. // If there are no selected options, highlight the first option
  554. // in the dropdown
  555. $options.first().trigger('mouseenter');
  556. }
  557. });
  558. };
  559. Results.prototype.option = function (data) {
  560. var $option = $(
  561. '<li class="option" role="treeitem" aria-selected="false"></li>'
  562. );
  563. if (data.children) {
  564. $option
  565. .attr('role', 'group')
  566. .attr('aria-label', data.text)
  567. .removeAttr('aria-selected');
  568. var $label = $('<strong class="group-label"></strong>');
  569. $label.html(data.text);
  570. var $children = [];
  571. for (var c = 0; c < data.children.length; c++) {
  572. var child = data.children[c];
  573. var $child = this.option(child);
  574. $children.push($child);
  575. }
  576. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  577. $childrenContainer.append($children);
  578. $option.append($label);
  579. $option.append($childrenContainer);
  580. } else {
  581. $option.html(data.text);
  582. }
  583. if (data.disabled) {
  584. $option
  585. .removeAttr('aria-selected')
  586. .attr('aria-disabled', 'true');
  587. }
  588. if (data.id == null) {
  589. $option.removeAttr('aria-selected');
  590. }
  591. if (data._resultId != null) {
  592. $option.attr('id', data._resultId);
  593. }
  594. $option.data('data', data);
  595. return $option;
  596. };
  597. Results.prototype.bind = function (container, $container) {
  598. var self = this;
  599. var id = container.id + '-results';
  600. this.$results.attr('id', id);
  601. container.on('results:all', function (params) {
  602. self.clear();
  603. self.append(params.data);
  604. if (container.isOpen()) {
  605. self.setClasses();
  606. }
  607. });
  608. container.on('results:append', function (params) {
  609. self.append(params.data);
  610. if (container.isOpen()) {
  611. self.setClasses();
  612. }
  613. });
  614. container.on('select', function () {
  615. if (!container.isOpen()) {
  616. return;
  617. }
  618. self.setClasses();
  619. });
  620. container.on('unselect', function () {
  621. if (!container.isOpen()) {
  622. return;
  623. }
  624. self.setClasses();
  625. });
  626. container.on('open', function () {
  627. // When the dropdown is open, aria-expended="true"
  628. self.$results.attr('aria-expanded', 'true');
  629. self.$results.attr('aria-hidden', 'false');
  630. self.setClasses();
  631. self.ensureHighlightVisible();
  632. });
  633. container.on('close', function () {
  634. // When the dropdown is closed, aria-expended="false"
  635. self.$results.attr('aria-expanded', 'false');
  636. self.$results.attr('aria-hidden', 'true');
  637. self.$results.removeAttr('aria-activedescendant');
  638. });
  639. container.on('results:select', function () {
  640. var $highlighted = self.$results.find('.highlighted');
  641. if ($highlighted.length === 0) {
  642. return;
  643. }
  644. var data = $highlighted.data('data');
  645. if ($highlighted.attr('aria-selected') == 'true') {
  646. self.trigger('unselected', {
  647. data: data
  648. });
  649. } else {
  650. self.trigger('selected', {
  651. data: data
  652. });
  653. }
  654. });
  655. container.on('results:previous', function () {
  656. var $highlighted = self.$results.find('.highlighted');
  657. var $options = self.$results.find('[aria-selected]');
  658. var currentIndex = $options.index($highlighted);
  659. // If we are already at te top, don't move further
  660. if (currentIndex === 0) {
  661. return;
  662. }
  663. var nextIndex = currentIndex - 1;
  664. // If none are highlighted, highlight the first
  665. if ($highlighted.length === 0) {
  666. nextIndex = 0;
  667. }
  668. var $next = $options.eq(nextIndex);
  669. $next.trigger('mouseenter');
  670. var currentOffset = self.$results.offset().top;
  671. var nextTop = $next.offset().top;
  672. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  673. if (nextIndex === 0) {
  674. self.$results.scrollTop(0);
  675. } else if (nextTop - currentOffset < 0) {
  676. self.$results.scrollTop(nextOffset);
  677. }
  678. });
  679. container.on('results:next', function () {
  680. var $highlighted = self.$results.find('.highlighted');
  681. var $options = self.$results.find('[aria-selected]');
  682. var currentIndex = $options.index($highlighted);
  683. var nextIndex = currentIndex + 1;
  684. // If we are at the last option, stay there
  685. if (nextIndex >= $options.length) {
  686. return;
  687. }
  688. var $next = $options.eq(nextIndex);
  689. $next.trigger('mouseenter');
  690. var currentOffset = self.$results.offset().top +
  691. self.$results.outerHeight(false);
  692. var nextBottom = $next.offset().top + $next.outerHeight(false);
  693. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  694. if (nextIndex === 0) {
  695. self.$results.scrollTop(0);
  696. } else if (nextBottom > currentOffset) {
  697. self.$results.scrollTop(nextOffset);
  698. }
  699. });
  700. container.on('results:focus', function (params) {
  701. params.element.addClass('highlighted');
  702. });
  703. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  704. var $this = $(this);
  705. var data = $this.data('data');
  706. if ($this.attr('aria-selected') === 'true') {
  707. self.trigger('unselected', {
  708. originalEvent: evt,
  709. data: data
  710. });
  711. return;
  712. }
  713. self.trigger('selected', {
  714. originalEvent: evt,
  715. data: data
  716. });
  717. });
  718. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  719. var data = $(this).data('data');
  720. self.$results.find('.option.highlighted').removeClass('highlighted');
  721. self.trigger('results:focus', {
  722. data: data,
  723. element: $(this)
  724. });
  725. });
  726. this.$results.on('mouseleave', '.option', function (evt) {
  727. if ($(this).hasClass('highlighted')) {
  728. $(this).removeClass('highlighted');
  729. self.$results.removeAttr('aria-activedescendant');
  730. }
  731. });
  732. };
  733. Results.prototype.ensureHighlightVisible = function () {
  734. var $highlighted = this.$results.find('.highlighted');
  735. if ($highlighted.length === 0) {
  736. return;
  737. }
  738. var $options = this.$results.find('[aria-selected]');
  739. var currentIndex = $options.index($highlighted);
  740. var currentOffset = this.$results.offset().top;
  741. var nextTop = $highlighted.offset().top;
  742. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  743. var offsetDelta = nextTop - currentOffset;
  744. nextOffset -= $highlighted.outerHeight(false) * 2;
  745. if (currentIndex <= 2) {
  746. this.$results.scrollTop(0);
  747. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  748. this.$results.scrollTop(nextOffset);
  749. }
  750. };
  751. return Results;
  752. });
  753. define('select2/selection/base',[
  754. '../utils'
  755. ], function (Utils) {
  756. function BaseSelection ($element, options) {
  757. this.$element = $element;
  758. this.options = options;
  759. BaseSelection.__super__.constructor.call(this);
  760. }
  761. Utils.Extend(BaseSelection, Utils.Observable);
  762. BaseSelection.prototype.render = function () {
  763. throw new Error('The `render` method must be defined in child classes.');
  764. };
  765. BaseSelection.prototype.bind = function (container, $container) {
  766. var self = this;
  767. container.on('selection:update', function (params) {
  768. self.update(params.data);
  769. });
  770. container.on('open', function () {
  771. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  772. var $target = $(e.target);
  773. var $select = $target.closest('.select2');
  774. var $all = $('.select2.open');
  775. $all.each(function () {
  776. var $this = $(this);
  777. if (this == $select[0]) {
  778. return;
  779. }
  780. var $element = $this.data('element');
  781. $element.select2('close');
  782. });
  783. });
  784. container.on('close', function () {
  785. $(document.body).off('mousedown.select2.' + container.id);
  786. });
  787. });
  788. };
  789. BaseSelection.prototype.update = function (data) {
  790. throw new Error('The `update` method must be defined in child classes.');
  791. };
  792. return BaseSelection;
  793. });
  794. define('select2/keys',[
  795. ], function () {
  796. var KEYS = {
  797. BACKSPACE: 8,
  798. TAB: 9,
  799. ENTER: 13,
  800. SHIFT: 16,
  801. CTRL: 17,
  802. ALT: 18,
  803. ESC: 27,
  804. SPACE: 32,
  805. PAGE_UP: 33,
  806. PAGE_DOWN: 34,
  807. END: 35,
  808. HOME: 36,
  809. LEFT: 37,
  810. UP: 38,
  811. RIGHT: 39,
  812. DOWN: 40,
  813. DELETE: 46,
  814. isArrow: function (k) {
  815. k = k.which ? k.which : k;
  816. switch (k) {
  817. case KEY.LEFT:
  818. case KEY.RIGHT:
  819. case KEY.UP:
  820. case KEY.DOWN:
  821. return true;
  822. }
  823. return false;
  824. }
  825. };
  826. return KEYS;
  827. });
  828. define('select2/selection/single',[
  829. './base',
  830. '../utils',
  831. '../keys'
  832. ], function (BaseSelection, Utils, KEYS) {
  833. function SingleSelection () {
  834. SingleSelection.__super__.constructor.apply(this, arguments);
  835. }
  836. Utils.Extend(SingleSelection, BaseSelection);
  837. SingleSelection.prototype.render = function () {
  838. var $selection = $(
  839. '<span class="single-select" tabindex="0" role="combobox" ' +
  840. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  841. '<span class="rendered-selection"></span>' +
  842. '</span>'
  843. );
  844. $selection.attr('title', this.$element.attr('title'));
  845. this.$selection = $selection;
  846. return $selection;
  847. };
  848. SingleSelection.prototype.bind = function (container, $container) {
  849. var self = this;
  850. SingleSelection.__super__.bind.apply(this, arguments);
  851. var id = container.id + '-container';
  852. var resultsId = container.id + '-results';
  853. this.$selection.find('.rendered-selection').attr('id', id);
  854. this.$selection.attr('aria-labelledby', id);
  855. this.$selection.attr('aria-owns', resultsId);
  856. this.$selection.on('mousedown', function (evt) {
  857. // Only respond to left clicks
  858. if (evt.which !== 1) {
  859. return;
  860. }
  861. self.trigger('toggle', {
  862. originalEvent: evt
  863. });
  864. });
  865. container.on('open', function () {
  866. // When the dropdown is open, aria-expanded="true"
  867. self.$selection.attr('aria-expanded', 'true');
  868. });
  869. container.on('close', function () {
  870. // When the dropdown is closed, aria-expanded="false"
  871. self.$selection.attr('aria-expanded', 'false');
  872. self.$selection.removeAttr('aria-activedescendant');
  873. self.$selection.focus();
  874. });
  875. this.$selection.on('focus', function (evt) {
  876. // User focuses on the container
  877. });
  878. this.$selection.on('blur', function (evt) {
  879. // User exits the container
  880. });
  881. this.$selection.on('keydown', function (evt) {
  882. self.trigger('keypress', evt);
  883. if (evt.which === KEYS.SPACE) {
  884. evt.preventDefault();
  885. }
  886. });
  887. container.on('results:focus', function (params) {
  888. self.$selection.attr('aria-activedescendant', params.data._resultId);
  889. });
  890. container.on('selection:update', function (params) {
  891. self.update(params.data);
  892. });
  893. };
  894. SingleSelection.prototype.clear = function () {
  895. this.$selection.find('.rendered-selection').empty();
  896. };
  897. SingleSelection.prototype.display = function (data) {
  898. return data.text;
  899. };
  900. SingleSelection.prototype.selectionContainer = function () {
  901. return $('<span></span>');
  902. };
  903. SingleSelection.prototype.update = function (data) {
  904. if (data.length === 0) {
  905. this.clear();
  906. return;
  907. }
  908. var selection = data[0];
  909. var formatted = this.display(selection);
  910. this.$selection.find('.rendered-selection').html(formatted);
  911. };
  912. return SingleSelection;
  913. });
  914. define('select2/selection/multiple',[
  915. './base',
  916. '../utils'
  917. ], function (BaseSelection, Utils) {
  918. function MultipleSelection ($element, options) {
  919. this.$element = $element;
  920. this.options = options;
  921. MultipleSelection.__super__.constructor.call(this);
  922. }
  923. Utils.Extend(MultipleSelection, BaseSelection);
  924. MultipleSelection.prototype.render = function () {
  925. var $selection = $(
  926. '<span class="multiple-select">' +
  927. '<ul class="rendered-selection"></ul>' +
  928. '</span>'
  929. );
  930. this.$selection = $selection;
  931. return $selection;
  932. };
  933. MultipleSelection.prototype.bind = function (container, $container) {
  934. var self = this;
  935. MultipleSelection.__super__.bind.apply(this, arguments);
  936. this.$selection.on('click', function (evt) {
  937. self.trigger('toggle', {
  938. originalEvent: evt
  939. });
  940. });
  941. this.$selection.on('click', '.remove', function (evt) {
  942. var $remove = $(this);
  943. var $selection = $remove.parent();
  944. var data = $selection.data('data');
  945. self.trigger('unselected', {
  946. originalEvent: evt,
  947. data: data
  948. });
  949. });
  950. };
  951. MultipleSelection.prototype.clear = function () {
  952. this.$selection.find('.rendered-selection').empty();
  953. };
  954. MultipleSelection.prototype.display = function (data) {
  955. return data.text;
  956. };
  957. MultipleSelection.prototype.selectionContainer = function () {
  958. var $container = $(
  959. '<li class="choice">' +
  960. '<span class="remove" role="presentation">&times;</span>' +
  961. '</li>'
  962. );
  963. return $container;
  964. };
  965. MultipleSelection.prototype.update = function (data) {
  966. this.clear();
  967. if (data.length === 0) {
  968. return;
  969. }
  970. var $selections = [];
  971. for (var d = 0; d < data.length; d++) {
  972. var selection = data[d];
  973. var formatted = this.display(selection);
  974. var $selection = this.selectionContainer();
  975. $selection.append(formatted);
  976. $selection.data('data', selection);
  977. $selections.push($selection);
  978. }
  979. this.$selection.find('.rendered-selection').append($selections);
  980. };
  981. return MultipleSelection;
  982. });
  983. define('select2/selection/placeholder',[
  984. '../utils'
  985. ], function (Utils) {
  986. function Placeholder (decorated, $element, options) {
  987. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  988. decorated.call(this, $element, options);
  989. }
  990. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  991. if (typeof placeholder === 'string') {
  992. placeholder = {
  993. id: '',
  994. text: placeholder
  995. };
  996. }
  997. return placeholder;
  998. };
  999. Placeholder.prototype.update = function (decorated, data) {
  1000. var singlePlaceholder = (
  1001. data.length == 1 && data[0].id != this.placeholder.id
  1002. );
  1003. var multipleSelections = data.length > 1;
  1004. if (multipleSelections || singlePlaceholder) {
  1005. return decorated.call(this, data);
  1006. }
  1007. this.clear();
  1008. var $placeholder = this.selectionContainer();
  1009. $placeholder.html(this.display(this.placeholder));
  1010. $placeholder.addClass('placeholder').removeClass('choice');
  1011. this.$selection.find('.rendered-selection').append($placeholder);
  1012. };
  1013. return Placeholder;
  1014. });
  1015. define('select2/translation',[
  1016. ], function () {
  1017. function Translation (dict) {
  1018. this.dict = dict || {};
  1019. }
  1020. Translation.prototype.all = function () {
  1021. return this.dict;
  1022. };
  1023. Translation.prototype.get = function (key) {
  1024. return this.dict[key];
  1025. };
  1026. Translation.prototype.extend = function (translation) {
  1027. this.dict = $.extend({}, translation.all(), this.dict);
  1028. };
  1029. // Static functions
  1030. Translation._cache = {};
  1031. Translation.loadPath = function (path) {
  1032. if (!(path in Translation._cache)) {
  1033. var translations = require(path);
  1034. Translation._cache[path] = translations;
  1035. }
  1036. return new Translation(Translation._cache[path]);
  1037. };
  1038. return Translation;
  1039. });
  1040. define('select2/data/base',[
  1041. '../utils'
  1042. ], function (Utils) {
  1043. function BaseAdapter ($element, options) {
  1044. BaseAdapter.__super__.constructor.call(this);
  1045. }
  1046. Utils.Extend(BaseAdapter, Utils.Observable);
  1047. BaseAdapter.prototype.current = function (callback) {
  1048. throw new Error('The `current` method must be defined in child classes.');
  1049. };
  1050. BaseAdapter.prototype.query = function (params, callback) {
  1051. throw new Error('The `query` method must be defined in child classes.');
  1052. };
  1053. BaseAdapter.prototype.bind = function (container, $container) {
  1054. // Can be implemented in subclasses
  1055. };
  1056. BaseAdapter.prototype.generateResultId = function (container, data) {
  1057. var id = container.id + '-result-';
  1058. id += Utils.generateChars(4);
  1059. if (data.id != null) {
  1060. id += '-' + data.id.toString();
  1061. } else {
  1062. id += '-' + Utils.generateChars(4);
  1063. }
  1064. return id;
  1065. };
  1066. return BaseAdapter;
  1067. });
  1068. define('select2/data/select',[
  1069. './base',
  1070. '../utils',
  1071. 'jquery'
  1072. ], function (BaseAdapter, Utils, $) {
  1073. function SelectAdapter ($element, options) {
  1074. this.$element = $element;
  1075. SelectAdapter.__super__.constructor.call(this);
  1076. }
  1077. Utils.Extend(SelectAdapter, BaseAdapter);
  1078. SelectAdapter.prototype.current = function (callback) {
  1079. var data = [];
  1080. var self = this;
  1081. this.$element.find(':selected').each(function () {
  1082. var $option = $(this);
  1083. var option = self.item($option);
  1084. data.push(option);
  1085. });
  1086. callback(data);
  1087. };
  1088. SelectAdapter.prototype.select = function (data) {
  1089. var self = this;
  1090. if (this.$element.prop('multiple')) {
  1091. this.current(function (currentData) {
  1092. var val = [];
  1093. data = [data];
  1094. data.push.apply(data, currentData);
  1095. for (var d = 0; d < data.length; d++) {
  1096. id = data[d].id;
  1097. if (val.indexOf(id) === -1) {
  1098. val.push(id);
  1099. }
  1100. }
  1101. self.$element.val(val);
  1102. self.$element.trigger('change');
  1103. });
  1104. } else {
  1105. var val = data.id;
  1106. this.$element.val(val);
  1107. this.$element.trigger('change');
  1108. }
  1109. };
  1110. SelectAdapter.prototype.unselect = function (data) {
  1111. var self = this;
  1112. if (!this.$element.prop('multiple')) {
  1113. return;
  1114. }
  1115. this.current(function (currentData) {
  1116. var val = [];
  1117. for (var d = 0; d < currentData.length; d++) {
  1118. id = currentData[d].id;
  1119. if (id !== data.id && val.indexOf(id) === -1) {
  1120. val.push(id);
  1121. }
  1122. }
  1123. self.$element.val(val);
  1124. self.$element.trigger('change');
  1125. });
  1126. };
  1127. SelectAdapter.prototype.bind = function (container, $container) {
  1128. var self = this;
  1129. this.container = container;
  1130. container.on('select', function (params) {
  1131. self.select(params.data);
  1132. });
  1133. container.on('unselect', function (params) {
  1134. self.unselect(params.data);
  1135. });
  1136. };
  1137. SelectAdapter.prototype.query = function (params, callback) {
  1138. var data = [];
  1139. var self = this;
  1140. var $options = this.$element.children();
  1141. $options.each(function () {
  1142. var $option = $(this);
  1143. if (!$option.is('option') && !$option.is('optgroup')) {
  1144. return;
  1145. }
  1146. var option = self.item($option);
  1147. var matches = self.matches(params, option);
  1148. if (matches !== null) {
  1149. data.push(matches);
  1150. }
  1151. });
  1152. callback(data);
  1153. };
  1154. SelectAdapter.prototype.option = function (data) {
  1155. var $option = $('<option></option>');
  1156. $option.text(data.text);
  1157. $option.val(data.id);
  1158. $option.prop('disabled', data.disabled || false);
  1159. // Get any automatically generated data values
  1160. var detectedData = this.item($option);
  1161. // Merge it with the already present data
  1162. var combinedData = $.extend({}, data, detectedData);
  1163. // Override the option's data with the combined data
  1164. $option.data('data', combinedData);
  1165. return $option;
  1166. };
  1167. SelectAdapter.prototype.item = function ($option) {
  1168. var data = $option.data('data');
  1169. // If the data has already be generated, use it
  1170. if (data == null) {
  1171. if ($option.is('option')) {
  1172. data = {
  1173. id: $option.val(),
  1174. text: $option.html(),
  1175. disabled: $option.prop('disabled')
  1176. };
  1177. } else if ($option.is('optgroup')) {
  1178. data = {
  1179. text: $option.attr('label'),
  1180. children: []
  1181. };
  1182. var $children = $option.children('option');
  1183. var children = [];
  1184. for (var c = 0; c < $children.length; c++) {
  1185. var $child = $($children[c]);
  1186. var child = this.item($child);
  1187. children.push(child);
  1188. }
  1189. data.children = children;
  1190. }
  1191. if (data.id && this.container != null) {
  1192. data._resultId = this.generateResultId(this.container, data);
  1193. }
  1194. $option.data('data', data);
  1195. }
  1196. return data;
  1197. };
  1198. SelectAdapter.prototype.matches = function (params, data) {
  1199. var match = $.extend(true, {}, data);
  1200. if (data.children) {
  1201. for (var c = data.children.length - 1; c >= 0; c--) {
  1202. var child = data.children[c];
  1203. var matches = this.matches(params, child);
  1204. // If there wasn't a match, remove the object in the array
  1205. if (matches === null) {
  1206. match.children.splice(c, 1);
  1207. }
  1208. }
  1209. if (match.children.length > 0) {
  1210. return match;
  1211. }
  1212. }
  1213. if ($.trim(params.term) === '') {
  1214. return match;
  1215. }
  1216. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1217. return match;
  1218. }
  1219. return null;
  1220. };
  1221. return SelectAdapter;
  1222. });
  1223. define('select2/data/array',[
  1224. './select',
  1225. '../utils',
  1226. 'jquery'
  1227. ], function (SelectAdapter, Utils, $) {
  1228. function ArrayAdapter ($element, options) {
  1229. this.data = options.get('data');
  1230. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1231. }
  1232. Utils.Extend(ArrayAdapter, SelectAdapter);
  1233. ArrayAdapter.prototype.select = function (data) {
  1234. var self = this;
  1235. this.$element.find('option').each(function () {
  1236. var $option = $(this);
  1237. var option = self.item($option);
  1238. if (option.id == data.id.toString()) {
  1239. $option.remove();
  1240. }
  1241. });
  1242. var $option = this.option(data);
  1243. this.$element.append($option);
  1244. ArrayAdapter.__super__.select.call(this, data);
  1245. };
  1246. ArrayAdapter.prototype.query = function (params, callback) {
  1247. var matches = [];
  1248. var self = this;
  1249. $.each(this.data, function () {
  1250. var option = this;
  1251. if (self.matches(params, option)) {
  1252. matches.push(option);
  1253. }
  1254. });
  1255. callback(matches);
  1256. };
  1257. return ArrayAdapter;
  1258. });
  1259. define('select2/data/ajax',[
  1260. './array',
  1261. '../utils',
  1262. 'jquery'
  1263. ], function (ArrayAdapter, Utils, $) {
  1264. function AjaxAdapter ($element, options) {
  1265. this.ajaxOptions = options.get('ajax');
  1266. this.processResults = this.ajaxOptions.processResults ||
  1267. function (results) {
  1268. return results;
  1269. };
  1270. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1271. }
  1272. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1273. AjaxAdapter.prototype.query = function (params, callback) {
  1274. var matches = [];
  1275. var self = this;
  1276. var options = $.extend({
  1277. type: 'GET'
  1278. }, this.ajaxOptions);
  1279. if (typeof options.url === 'function') {
  1280. options.url = options.url(params);
  1281. }
  1282. if (typeof options.data === 'function') {
  1283. options.data = options.data(params);
  1284. }
  1285. var $request = $.ajax(options);
  1286. $request.success(function (data) {
  1287. var results = self.processResults(data);
  1288. callback(results);
  1289. });
  1290. };
  1291. return AjaxAdapter;
  1292. });
  1293. define('select2/data/tags',[
  1294. ], function () {
  1295. function Tags (decorated, $element, options) {
  1296. var tags = options.get('tags');
  1297. decorated.call(this, $element, options);
  1298. }
  1299. Tags.prototype.query = function (decorated, params, callback) {
  1300. var self = this;
  1301. this._removeOldTags();
  1302. if (params.term == null || params.term === '' || params.page != null) {
  1303. decorated.call(this, params, callback);
  1304. return;
  1305. }
  1306. function wrapper (data, child) {
  1307. for (var i = 0; i < data.length; i++) {
  1308. var option = data[i];
  1309. var checkChildren = (
  1310. option.children != null && !wrapper(option.children, true)
  1311. );
  1312. var checkText = option.text === params.term;
  1313. if (checkText || checkChildren) {
  1314. if (child) {
  1315. return false;
  1316. }
  1317. callback(data);
  1318. return;
  1319. }
  1320. }
  1321. if (child) {
  1322. return true;
  1323. }
  1324. var tag = self.createTag(params);
  1325. var $option = self.option(tag);
  1326. $option.attr('data-select2-tag', true);
  1327. self.$element.append($option);
  1328. self.insertTag(data, tag);
  1329. callback(data);
  1330. }
  1331. decorated.call(this, params, wrapper);
  1332. };
  1333. Tags.prototype.createTag = function (decorated, params) {
  1334. return {
  1335. id: params.term,
  1336. text: params.term
  1337. };
  1338. };
  1339. Tags.prototype.insertTag = function (_, data, tag) {
  1340. data.unshift(tag);
  1341. };
  1342. Tags.prototype._removeOldTags = function (_) {
  1343. var tag = this._lastTag;
  1344. var $options = this.$element.find('option[data-select2-tag]');
  1345. $options.each(function () {
  1346. if (this.selected) {
  1347. return;
  1348. }
  1349. $(this).remove();
  1350. });
  1351. };
  1352. return Tags;
  1353. });
  1354. define('select2/dropdown',[
  1355. './utils'
  1356. ], function (Utils) {
  1357. function Dropdown ($element, options) {
  1358. this.$element = $element;
  1359. }
  1360. Utils.Extend(Dropdown, Utils.Observable);
  1361. Dropdown.prototype.render = function () {
  1362. var $dropdown = $(
  1363. '<span class="dropdown">' +
  1364. '<span class="results"></span>' +
  1365. '</span>'
  1366. );
  1367. return $dropdown;
  1368. };
  1369. Dropdown.prototype.bind = function (container, $container) {
  1370. // Can be implemented in subclasses
  1371. };
  1372. return Dropdown;
  1373. });
  1374. define('select2/dropdown/search',[
  1375. '../utils'
  1376. ], function (Utils) {
  1377. function Search () { }
  1378. Search.prototype.render = function (decorated) {
  1379. var $rendered = decorated.call(this);
  1380. var $search = $(
  1381. '<span class="search">' +
  1382. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1383. '</span>'
  1384. );
  1385. this.$searchContainer = $search;
  1386. this.$search = $search.find('input');
  1387. $rendered.prepend($search);
  1388. return $rendered;
  1389. };
  1390. Search.prototype.bind = function (decorated, container, $container) {
  1391. var self = this;
  1392. decorated.call(this, container, $container);
  1393. this.$search.on('keydown', function (evt) {
  1394. self.trigger('keypress', evt);
  1395. self._keyUpPrevented = evt.isDefaultPrevented();
  1396. });
  1397. this.$search.on('keyup', function (evt) {
  1398. if (!self._keyUpPrevented) {
  1399. self.trigger('query', {
  1400. term: $(this).val()
  1401. });
  1402. }
  1403. self._keyUpPrevented = false;
  1404. });
  1405. container.on('open', function () {
  1406. self.$search.attr('tabindex', 0);
  1407. });
  1408. container.on('close', function () {
  1409. self.$search.attr('tabindex', -1);
  1410. self.$search.val('');
  1411. });
  1412. container.on('results:all', function (params) {
  1413. if (params.query.term == null || params.query.term === '') {
  1414. var showSearch = self.showSearch(params);
  1415. if (showSearch) {
  1416. self.$searchContainer.show();
  1417. } else {
  1418. self.$searchContainer.hide();
  1419. }
  1420. }
  1421. });
  1422. };
  1423. Search.prototype.showSearch = function (_, params) {
  1424. return true;
  1425. };
  1426. return Search;
  1427. });
  1428. define('select2/dropdown/hidePlaceholder',[
  1429. ], function () {
  1430. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1431. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1432. decorated.call(this, $element, options, dataAdapter);
  1433. }
  1434. HidePlaceholder.prototype.append = function (decorated, data) {
  1435. data = this.removePlaceholder(data);
  1436. decorated.call(this, data);
  1437. };
  1438. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1439. if (typeof placeholder === 'string') {
  1440. placeholder = {
  1441. id: '',
  1442. text: placeholder
  1443. };
  1444. }
  1445. return placeholder;
  1446. };
  1447. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1448. var modifiedData = data.slice(0);
  1449. for (var d = data.length - 1; d >= 0; d--) {
  1450. var item = data[d];
  1451. if (this.placeholder.id === item.id) {
  1452. modifiedData.splice(d, 1);
  1453. }
  1454. }
  1455. return modifiedData;
  1456. };
  1457. return HidePlaceholder;
  1458. });
  1459. define('select2/i18n/en',[],function () {
  1460. return {
  1461. noResults: function () {
  1462. return 'No results found';
  1463. }
  1464. };
  1465. });
  1466. define('select2/defaults',[
  1467. 'jquery',
  1468. './results',
  1469. './selection/single',
  1470. './selection/multiple',
  1471. './selection/placeholder',
  1472. './utils',
  1473. './translation',
  1474. './data/select',
  1475. './data/array',
  1476. './data/ajax',
  1477. './data/tags',
  1478. './dropdown',
  1479. './dropdown/search',
  1480. './dropdown/hidePlaceholder',
  1481. './i18n/en'
  1482. ], function ($, ResultsList,
  1483. SingleSelection, MultipleSelection, Placeholder,
  1484. Utils, Translation,
  1485. SelectData, ArrayData, AjaxData, Tags,
  1486. Dropdown, Search, HidePlaceholder,
  1487. EnglishTranslation) {
  1488. function Defaults () {
  1489. this.reset();
  1490. }
  1491. Defaults.prototype.apply = function (options) {
  1492. options = $.extend({}, this.defaults, options);
  1493. if (options.dataAdapter == null) {
  1494. if (options.ajax) {
  1495. options.dataAdapter = AjaxData;
  1496. } else if (options.data) {
  1497. options.dataAdapter = ArrayData;
  1498. } else {
  1499. options.dataAdapter = SelectData;
  1500. }
  1501. }
  1502. if (options.tags != null) {
  1503. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1504. }
  1505. if (options.resultsAdapter == null) {
  1506. options.resultsAdapter = ResultsList;
  1507. if (options.placeholder != null) {
  1508. options.resultsAdapter = Utils.Decorate(
  1509. options.resultsAdapter,
  1510. HidePlaceholder
  1511. );
  1512. }
  1513. }
  1514. if (options.dropdownAdapter == null) {
  1515. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1516. options.dropdownAdapter = SearchableDropdown;
  1517. }
  1518. if (options.selectionAdapter == null) {
  1519. if (options.multiple) {
  1520. options.selectionAdapter = MultipleSelection;
  1521. } else {
  1522. options.selectionAdapter = SingleSelection;
  1523. }
  1524. // Add the placeholder mixin if a placeholder was specified
  1525. if (options.placeholder != null) {
  1526. options.selectionAdapter = Utils.Decorate(
  1527. options.selectionAdapter,
  1528. Placeholder
  1529. );
  1530. }
  1531. }
  1532. if (typeof options.language === 'string') {
  1533. options.language = [options.language];
  1534. }
  1535. if ($.isArray(options.language)) {
  1536. var languages = new Translation();
  1537. var languageNames = options.language.concat(this.defaults.language);
  1538. for (var l = 0; l < languageNames.length; l++) {
  1539. var name = languageNames[l];
  1540. var language = {};
  1541. try {
  1542. // Try to load it with the original name
  1543. language = Translation.loadPath(name);
  1544. } catch (e) {
  1545. // If we couldn't load it, check if it wasn't the full path
  1546. name = 'select2/i18n/' + name;
  1547. language = Translation.loadPath(name);
  1548. }
  1549. languages.extend(language);
  1550. }
  1551. options.translations = languages;
  1552. } else {
  1553. options.translations = new Translations(options.language);
  1554. }
  1555. return options;
  1556. };
  1557. Defaults.prototype.reset = function () {
  1558. this.defaults = {
  1559. language: ['select2/i18n/en']
  1560. };
  1561. };
  1562. var defaults = new Defaults();
  1563. return defaults;
  1564. });
  1565. define('select2/options',[
  1566. './defaults'
  1567. ], function (Defaults) {
  1568. function Options (options, $element) {
  1569. this.options = options;
  1570. if ($element != null) {
  1571. this.fromElement($element);
  1572. }
  1573. this.options = Defaults.apply(this.options);
  1574. }
  1575. Options.prototype.fromElement = function ($e) {
  1576. if (this.options.multiple == null) {
  1577. this.options.multiple = $e.prop('multiple');
  1578. }
  1579. return this;
  1580. };
  1581. Options.prototype.get = function (key) {
  1582. return this.options[key];
  1583. };
  1584. Options.prototype.set = function (key, val) {
  1585. this.options[key] = val;
  1586. };
  1587. return Options;
  1588. });
  1589. define('select2/core',[
  1590. 'jquery',
  1591. './options',
  1592. './utils',
  1593. './keys'
  1594. ], function ($, Options, Utils, KEYS) {
  1595. var Select2 = function ($element, options) {
  1596. this.$element = $element;
  1597. this.id = this._generateId($element);
  1598. options = options || {};
  1599. this.options = new Options(options, $element);
  1600. Select2.__super__.constructor.call(this);
  1601. // Set up containers and adapters
  1602. var DataAdapter = this.options.get('dataAdapter');
  1603. this.data = new DataAdapter($element, this.options);
  1604. var $container = this.render();
  1605. this._placeContainer($container);
  1606. var SelectionAdapter = this.options.get('selectionAdapter');
  1607. this.selection = new SelectionAdapter($element, this.options);
  1608. var $selection = this.selection.render();
  1609. this._placeSelection($selection);
  1610. var DropdownAdapter = this.options.get('dropdownAdapter');
  1611. this.dropdown = new DropdownAdapter($element, this.options);
  1612. var $dropdown = this.dropdown.render();
  1613. this._placeDropdown($dropdown);
  1614. var ResultsAdapter = this.options.get('resultsAdapter');
  1615. this.results = new ResultsAdapter($element, this.options, this.data);
  1616. var $results = this.results.render();
  1617. this._placeResults($results);
  1618. // Bind events
  1619. var self = this;
  1620. // Bind the container to all of the adapters
  1621. this._bindAdapters();
  1622. // Register any DOM event handlers
  1623. this._registerDomEvents();
  1624. // Register any internal event handlers
  1625. this._registerSelectionEvents();
  1626. this._registerDropdownEvents();
  1627. this._registerResultsEvents();
  1628. this._registerEvents();
  1629. // Set the initial state
  1630. this.data.current(function (initialData) {
  1631. self.trigger('selection:update', {
  1632. data: initialData
  1633. });
  1634. });
  1635. // Hide the original select
  1636. $element.hide();
  1637. $element.attr('tabindex', '-1');
  1638. $element.data('select2', this);
  1639. };
  1640. Utils.Extend(Select2, Utils.Observable);
  1641. Select2.prototype._generateId = function ($element) {
  1642. var id = '';
  1643. if ($element.attr('id') != null) {
  1644. id = $element.attr('id');
  1645. } else if ($element.attr('name') != null) {
  1646. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1647. } else {
  1648. id = Utils.generateChars(4);
  1649. }
  1650. id = 'select2-' + id;
  1651. return id;
  1652. };
  1653. Select2.prototype._placeContainer = function ($container) {
  1654. $container.insertAfter(this.$element);
  1655. $container.width(this.$element.outerWidth(false));
  1656. };
  1657. Select2.prototype._placeSelection = function ($selection) {
  1658. var $selectionContainer = this.$container.find('.selection');
  1659. $selectionContainer.append($selection);
  1660. };
  1661. Select2.prototype._placeDropdown = function ($dropdown) {
  1662. this.$dropdown = $dropdown;
  1663. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1664. $dropdownContainer.append($dropdown);
  1665. };
  1666. Select2.prototype._placeResults = function ($results) {
  1667. var $resultsContainer = this.$dropdown.find('.results');
  1668. $resultsContainer.append($results);
  1669. };
  1670. Select2.prototype._bindAdapters = function () {
  1671. this.data.bind(this, this.$container);
  1672. this.selection.bind(this, this.$container);
  1673. this.dropdown.bind(this, this.$container);
  1674. this.results.bind(this, this.$container);
  1675. };
  1676. Select2.prototype._registerDomEvents = function () {
  1677. var self = this;
  1678. this.$element.on('change', function () {
  1679. self.data.current(function (data) {
  1680. self.trigger('selection:update', {
  1681. data: data
  1682. });
  1683. });
  1684. });
  1685. };
  1686. Select2.prototype._registerSelectionEvents = function () {
  1687. var self = this;
  1688. this.selection.on('open', function () {
  1689. self.open();
  1690. });
  1691. this.selection.on('close', function () {
  1692. self.close();
  1693. });
  1694. this.selection.on('toggle', function () {
  1695. self.toggleDropdown();
  1696. });
  1697. this.selection.on('results:select', function () {
  1698. self.trigger('results:select');
  1699. });
  1700. this.selection.on('results:previous', function () {
  1701. self.trigger('results:previous');
  1702. });
  1703. this.selection.on('results:next', function () {
  1704. self.trigger('results:next');
  1705. });
  1706. this.selection.on('unselected', function (params) {
  1707. self.trigger('unselect', params);
  1708. self.close();
  1709. });
  1710. this.selection.on('keypress', function (e) {
  1711. self.trigger('keypress', e);
  1712. });
  1713. };
  1714. Select2.prototype._registerDropdownEvents = function () {
  1715. var self = this;
  1716. this.dropdown.on('query', function (params) {
  1717. self.trigger('query', params);
  1718. });
  1719. this.dropdown.on('keypress', function (e) {
  1720. self.trigger('keypress', e);
  1721. });
  1722. };
  1723. Select2.prototype._registerResultsEvents = function () {
  1724. var self = this;
  1725. this.results.on('selected', function (params) {
  1726. self.trigger('select', params);
  1727. self.close();
  1728. });
  1729. this.results.on('unselected', function (params) {
  1730. self.trigger('unselect', params);
  1731. self.close();
  1732. });
  1733. this.results.on('results:focus', function (params) {
  1734. self.trigger('results:focus', params);
  1735. });
  1736. };
  1737. Select2.prototype._registerEvents = function () {
  1738. var self = this;
  1739. this.on('open', function () {
  1740. self.$container.addClass('open');
  1741. });
  1742. this.on('close', function () {
  1743. self.$container.removeClass('open');
  1744. });
  1745. this.on('query', function (params) {
  1746. this.data.query(params, function (data) {
  1747. self.trigger('results:all', {
  1748. data: data,
  1749. query: params
  1750. });
  1751. });
  1752. });
  1753. this.on('keypress', function (evt) {
  1754. var key = evt.which;
  1755. if (self.isOpen()) {
  1756. if (key === KEYS.ENTER) {
  1757. self.trigger('results:select');
  1758. evt.preventDefault();
  1759. } else if (key === KEYS.UP) {
  1760. self.trigger('results:previous');
  1761. evt.preventDefault();
  1762. } else if (key === KEYS.DOWN) {
  1763. self.trigger('results:next');
  1764. evt.preventDefault();
  1765. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1766. self.close();
  1767. evt.preventDefault();
  1768. }
  1769. } else {
  1770. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1771. self.open();
  1772. evt.preventDefault();
  1773. }
  1774. }
  1775. });
  1776. };
  1777. Select2.prototype.toggleDropdown = function () {
  1778. if (this.isOpen()) {
  1779. this.close();
  1780. } else {
  1781. this.open();
  1782. }
  1783. };
  1784. Select2.prototype.open = function () {
  1785. if (this.isOpen()) {
  1786. return;
  1787. }
  1788. this.trigger('query', {});
  1789. this.trigger('open');
  1790. };
  1791. Select2.prototype.close = function () {
  1792. if (!this.isOpen()) {
  1793. return;
  1794. }
  1795. this.trigger('close');
  1796. };
  1797. Select2.prototype.isOpen = function () {
  1798. return this.$container.hasClass('open');
  1799. };
  1800. Select2.prototype.render = function () {
  1801. var $container = $(
  1802. '<span class="select2 select2-container select2-theme-default">' +
  1803. '<span class="selection"></span>' +
  1804. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1805. '</span>'
  1806. );
  1807. this.$container = $container;
  1808. $container.data('element', this.$element);
  1809. return $container;
  1810. };
  1811. return Select2;
  1812. });
  1813. define('jquery.select2',[
  1814. 'jquery',
  1815. 'select2/core'
  1816. ], function ($, Select2) {
  1817. if ($.fn.select2 == null) {
  1818. $.fn.select2 = function (options) {
  1819. options = options || {};
  1820. if (typeof options === 'object') {
  1821. this.each(function () {
  1822. var instance = new Select2($(this), options);
  1823. });
  1824. } else if (typeof options === 'string') {
  1825. var instance = this.data('select2');
  1826. var args = Array.prototype.slice.call(arguments, 1);
  1827. instance[options](args);
  1828. } else {
  1829. throw new Error('Invalid arguments for Select2: ' + options);
  1830. }
  1831. };
  1832. }
  1833. return Select2;
  1834. });
  1835. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());