select2.js 53 KB

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