select2.js 59 KB

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