select2.js 57 KB

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