select2.js 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434
  1. window.$ = window.$ || {};(function() { if ($ && $.fn && $.fn.select2 && $.fn.select2.amd) { define = $.fn.select2.amd.define; require = $.fn.select2.amd.require; }/**
  2. * @license almond 0.2.9 Copyright (c) 2011-2014, The Dojo Foundation All Rights Reserved.
  3. * Available via the MIT or new BSD license.
  4. * see: http://github.com/jrburke/almond for details
  5. */
  6. //Going sloppy to avoid 'use strict' string cost, but strict practices should
  7. //be followed.
  8. /*jslint sloppy: true */
  9. /*global setTimeout: false */
  10. var requirejs, require, define;
  11. (function (undef) {
  12. var main, req, makeMap, handlers,
  13. defined = {},
  14. waiting = {},
  15. config = {},
  16. defining = {},
  17. hasOwn = Object.prototype.hasOwnProperty,
  18. aps = [].slice,
  19. jsSuffixRegExp = /\.js$/;
  20. function hasProp(obj, prop) {
  21. return hasOwn.call(obj, prop);
  22. }
  23. /**
  24. * Given a relative module name, like ./something, normalize it to
  25. * a real name that can be mapped to a path.
  26. * @param {String} name the relative name
  27. * @param {String} baseName a real name that the name arg is relative
  28. * to.
  29. * @returns {String} normalized name
  30. */
  31. function normalize(name, baseName) {
  32. var nameParts, nameSegment, mapValue, foundMap, lastIndex,
  33. foundI, foundStarMap, starI, i, j, part,
  34. baseParts = baseName && baseName.split("/"),
  35. map = config.map,
  36. starMap = (map && map['*']) || {};
  37. //Adjust any relative paths.
  38. if (name && name.charAt(0) === ".") {
  39. //If have a base name, try to normalize against it,
  40. //otherwise, assume it is a top-level require that will
  41. //be relative to baseUrl in the end.
  42. if (baseName) {
  43. //Convert baseName to array, and lop off the last part,
  44. //so that . matches that "directory" and not name of the baseName's
  45. //module. For instance, baseName of "one/two/three", maps to
  46. //"one/two/three.js", but we want the directory, "one/two" for
  47. //this normalization.
  48. baseParts = baseParts.slice(0, baseParts.length - 1);
  49. name = name.split('/');
  50. lastIndex = name.length - 1;
  51. // Node .js allowance:
  52. if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
  53. name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
  54. }
  55. name = baseParts.concat(name);
  56. //start trimDots
  57. for (i = 0; i < name.length; i += 1) {
  58. part = name[i];
  59. if (part === ".") {
  60. name.splice(i, 1);
  61. i -= 1;
  62. } else if (part === "..") {
  63. if (i === 1 && (name[2] === '..' || name[0] === '..')) {
  64. //End of the line. Keep at least one non-dot
  65. //path segment at the front so it can be mapped
  66. //correctly to disk. Otherwise, there is likely
  67. //no path mapping for a path starting with '..'.
  68. //This can still fail, but catches the most reasonable
  69. //uses of ..
  70. break;
  71. } else if (i > 0) {
  72. name.splice(i - 1, 2);
  73. i -= 2;
  74. }
  75. }
  76. }
  77. //end trimDots
  78. name = name.join("/");
  79. } else if (name.indexOf('./') === 0) {
  80. // No baseName, so this is ID is resolved relative
  81. // to baseUrl, pull off the leading dot.
  82. name = name.substring(2);
  83. }
  84. }
  85. //Apply map config if available.
  86. if ((baseParts || starMap) && map) {
  87. nameParts = name.split('/');
  88. for (i = nameParts.length; i > 0; i -= 1) {
  89. nameSegment = nameParts.slice(0, i).join("/");
  90. if (baseParts) {
  91. //Find the longest baseName segment match in the config.
  92. //So, do joins on the biggest to smallest lengths of baseParts.
  93. for (j = baseParts.length; j > 0; j -= 1) {
  94. mapValue = map[baseParts.slice(0, j).join('/')];
  95. //baseName segment has config, find if it has one for
  96. //this name.
  97. if (mapValue) {
  98. mapValue = mapValue[nameSegment];
  99. if (mapValue) {
  100. //Match, update name to the new value.
  101. foundMap = mapValue;
  102. foundI = i;
  103. break;
  104. }
  105. }
  106. }
  107. }
  108. if (foundMap) {
  109. break;
  110. }
  111. //Check for a star map match, but just hold on to it,
  112. //if there is a shorter segment match later in a matching
  113. //config, then favor over this star map.
  114. if (!foundStarMap && starMap && starMap[nameSegment]) {
  115. foundStarMap = starMap[nameSegment];
  116. starI = i;
  117. }
  118. }
  119. if (!foundMap && foundStarMap) {
  120. foundMap = foundStarMap;
  121. foundI = starI;
  122. }
  123. if (foundMap) {
  124. nameParts.splice(0, foundI, foundMap);
  125. name = nameParts.join('/');
  126. }
  127. }
  128. return name;
  129. }
  130. function makeRequire(relName, forceSync) {
  131. return function () {
  132. //A version of a require function that passes a moduleName
  133. //value for items that may need to
  134. //look up paths relative to the moduleName
  135. return req.apply(undef, aps.call(arguments, 0).concat([relName, forceSync]));
  136. };
  137. }
  138. function makeNormalize(relName) {
  139. return function (name) {
  140. return normalize(name, relName);
  141. };
  142. }
  143. function makeLoad(depName) {
  144. return function (value) {
  145. defined[depName] = value;
  146. };
  147. }
  148. function callDep(name) {
  149. if (hasProp(waiting, name)) {
  150. var args = waiting[name];
  151. delete waiting[name];
  152. defining[name] = true;
  153. main.apply(undef, args);
  154. }
  155. if (!hasProp(defined, name) && !hasProp(defining, name)) {
  156. throw new Error('No ' + name);
  157. }
  158. return defined[name];
  159. }
  160. //Turns a plugin!resource to [plugin, resource]
  161. //with the plugin being undefined if the name
  162. //did not have a plugin prefix.
  163. function splitPrefix(name) {
  164. var prefix,
  165. index = name ? name.indexOf('!') : -1;
  166. if (index > -1) {
  167. prefix = name.substring(0, index);
  168. name = name.substring(index + 1, name.length);
  169. }
  170. return [prefix, name];
  171. }
  172. /**
  173. * Makes a name map, normalizing the name, and using a plugin
  174. * for normalization if necessary. Grabs a ref to plugin
  175. * too, as an optimization.
  176. */
  177. makeMap = function (name, relName) {
  178. var plugin,
  179. parts = splitPrefix(name),
  180. prefix = parts[0];
  181. name = parts[1];
  182. if (prefix) {
  183. prefix = normalize(prefix, relName);
  184. plugin = callDep(prefix);
  185. }
  186. //Normalize according
  187. if (prefix) {
  188. if (plugin && plugin.normalize) {
  189. name = plugin.normalize(name, makeNormalize(relName));
  190. } else {
  191. name = normalize(name, relName);
  192. }
  193. } else {
  194. name = normalize(name, relName);
  195. parts = splitPrefix(name);
  196. prefix = parts[0];
  197. name = parts[1];
  198. if (prefix) {
  199. plugin = callDep(prefix);
  200. }
  201. }
  202. //Using ridiculous property names for space reasons
  203. return {
  204. f: prefix ? prefix + '!' + name : name, //fullName
  205. n: name,
  206. pr: prefix,
  207. p: plugin
  208. };
  209. };
  210. function makeConfig(name) {
  211. return function () {
  212. return (config && config.config && config.config[name]) || {};
  213. };
  214. }
  215. handlers = {
  216. require: function (name) {
  217. return makeRequire(name);
  218. },
  219. exports: function (name) {
  220. var e = defined[name];
  221. if (typeof e !== 'undefined') {
  222. return e;
  223. } else {
  224. return (defined[name] = {});
  225. }
  226. },
  227. module: function (name) {
  228. return {
  229. id: name,
  230. uri: '',
  231. exports: defined[name],
  232. config: makeConfig(name)
  233. };
  234. }
  235. };
  236. main = function (name, deps, callback, relName) {
  237. var cjsModule, depName, ret, map, i,
  238. args = [],
  239. callbackType = typeof callback,
  240. usingExports;
  241. //Use name if no relName
  242. relName = relName || name;
  243. //Call the callback to define the module, if necessary.
  244. if (callbackType === 'undefined' || callbackType === 'function') {
  245. //Pull out the defined dependencies and pass the ordered
  246. //values to the callback.
  247. //Default to [require, exports, module] if no deps
  248. deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
  249. for (i = 0; i < deps.length; i += 1) {
  250. map = makeMap(deps[i], relName);
  251. depName = map.f;
  252. //Fast path CommonJS standard dependencies.
  253. if (depName === "require") {
  254. args[i] = handlers.require(name);
  255. } else if (depName === "exports") {
  256. //CommonJS module spec 1.1
  257. args[i] = handlers.exports(name);
  258. usingExports = true;
  259. } else if (depName === "module") {
  260. //CommonJS module spec 1.1
  261. cjsModule = args[i] = handlers.module(name);
  262. } else if (hasProp(defined, depName) ||
  263. hasProp(waiting, depName) ||
  264. hasProp(defining, depName)) {
  265. args[i] = callDep(depName);
  266. } else if (map.p) {
  267. map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
  268. args[i] = defined[depName];
  269. } else {
  270. throw new Error(name + ' missing ' + depName);
  271. }
  272. }
  273. ret = callback ? callback.apply(defined[name], args) : undefined;
  274. if (name) {
  275. //If setting exports via "module" is in play,
  276. //favor that over return value and exports. After that,
  277. //favor a non-undefined return value over exports use.
  278. if (cjsModule && cjsModule.exports !== undef &&
  279. cjsModule.exports !== defined[name]) {
  280. defined[name] = cjsModule.exports;
  281. } else if (ret !== undef || !usingExports) {
  282. //Use the return value from the function.
  283. defined[name] = ret;
  284. }
  285. }
  286. } else if (name) {
  287. //May just be an object definition for the module. Only
  288. //worry about defining if have a module name.
  289. defined[name] = callback;
  290. }
  291. };
  292. requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
  293. if (typeof deps === "string") {
  294. if (handlers[deps]) {
  295. //callback in this case is really relName
  296. return handlers[deps](callback);
  297. }
  298. //Just return the module wanted. In this scenario, the
  299. //deps arg is the module name, and second arg (if passed)
  300. //is just the relName.
  301. //Normalize module name, if it contains . or ..
  302. return callDep(makeMap(deps, callback).f);
  303. } else if (!deps.splice) {
  304. //deps is a config object, not an array.
  305. config = deps;
  306. if (config.deps) {
  307. req(config.deps, config.callback);
  308. }
  309. if (!callback) {
  310. return;
  311. }
  312. if (callback.splice) {
  313. //callback is an array, which means it is a dependency list.
  314. //Adjust args if there are dependencies
  315. deps = callback;
  316. callback = relName;
  317. relName = null;
  318. } else {
  319. deps = undef;
  320. }
  321. }
  322. //Support require(['a'])
  323. callback = callback || function () {};
  324. //If relName is a function, it is an errback handler,
  325. //so remove it.
  326. if (typeof relName === 'function') {
  327. relName = forceSync;
  328. forceSync = alt;
  329. }
  330. //Simulate async callback;
  331. if (forceSync) {
  332. main(undef, deps, callback, relName);
  333. } else {
  334. //Using a non-zero value because of concern for what old browsers
  335. //do, and latest browsers "upgrade" to 4 if lower value is used:
  336. //http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
  337. //If want a value immediately, use require('id') instead -- something
  338. //that works in almond on the global level, but not guaranteed and
  339. //unlikely to work in other AMD implementations.
  340. setTimeout(function () {
  341. main(undef, deps, callback, relName);
  342. }, 4);
  343. }
  344. return req;
  345. };
  346. /**
  347. * Just drops the config on the floor, but returns req in case
  348. * the config return value is used.
  349. */
  350. req.config = function (cfg) {
  351. return req(cfg);
  352. };
  353. /**
  354. * Expose module registry for debugging and tooling
  355. */
  356. requirejs._defined = defined;
  357. define = function (name, deps, callback) {
  358. //This module may not have dependencies
  359. if (!deps.splice) {
  360. //deps is not an array, so probably means
  361. //an object literal or factory function for
  362. //the value. Adjust args.
  363. callback = deps;
  364. deps = [];
  365. }
  366. if (!hasProp(defined, name) && !hasProp(waiting, name)) {
  367. waiting[name] = [name, deps, callback];
  368. }
  369. };
  370. define.amd = {
  371. jQuery: true
  372. };
  373. }());
  374. define("almond", function(){});
  375. define('jquery',[],function () {
  376. return jQuery;
  377. });
  378. define('select2/utils',[], function () {
  379. var Utils = {};
  380. Utils.Extend = function (ChildClass, SuperClass) {
  381. var __hasProp = {}.hasOwnProperty;
  382. function BaseConstructor () {
  383. this.constructor = ChildClass;
  384. }
  385. for (var key in SuperClass) {
  386. if (__hasProp.call(SuperClass, key)) {
  387. ChildClass[key] = SuperClass[key];
  388. }
  389. }
  390. BaseConstructor.prototype = SuperClass.prototype;
  391. ChildClass.prototype = new BaseConstructor();
  392. ChildClass.__super__ = SuperClass.prototype;
  393. return ChildClass;
  394. };
  395. function getMethods (theClass) {
  396. var proto = theClass.prototype;
  397. var methods = [];
  398. for (var methodName in proto) {
  399. var m = proto[methodName];
  400. if (typeof m !== 'function') {
  401. continue;
  402. }
  403. methods.push(methodName);
  404. }
  405. return methods;
  406. }
  407. Utils.Decorate = function (SuperClass, DecoratorClass) {
  408. var decoratedMethods = getMethods(DecoratorClass);
  409. var superMethods = getMethods(SuperClass);
  410. function DecoratedClass () {
  411. var unshift = Array.prototype.unshift;
  412. var argCount = DecoratorClass.prototype.constructor.length;
  413. var calledConstructor = SuperClass.prototype.constructor;
  414. if (argCount > 0) {
  415. unshift.call(arguments, SuperClass.prototype.constructor);
  416. calledConstructor = DecoratorClass.prototype.constructor;
  417. }
  418. calledConstructor.apply(this, arguments);
  419. }
  420. DecoratorClass.displayName = SuperClass.displayName;
  421. function ctr () {
  422. this.constructor = DecoratedClass;
  423. }
  424. DecoratedClass.prototype = new ctr();
  425. for (var m = 0; m < superMethods.length; m++) {
  426. var superMethod = superMethods[m];
  427. DecoratedClass.prototype[superMethod] =
  428. SuperClass.prototype[superMethod];
  429. }
  430. var calledMethod = function (methodName) {
  431. // Stub out the original method if it's not decorating an actual method
  432. var originalMethod = function () {};
  433. if (methodName in DecoratedClass.prototype) {
  434. originalMethod = DecoratedClass.prototype[methodName];
  435. }
  436. var decoratedMethod = DecoratorClass.prototype[methodName];
  437. return function () {
  438. var unshift = Array.prototype.unshift;
  439. unshift.call(arguments, originalMethod);
  440. return decoratedMethod.apply(this, arguments);
  441. };
  442. };
  443. for (var d = 0; d < decoratedMethods.length; d++) {
  444. var decoratedMethod = decoratedMethods[d];
  445. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  446. }
  447. return DecoratedClass;
  448. };
  449. var Observable = function () {
  450. this.listeners = {};
  451. };
  452. Observable.prototype.on = function (event, callback) {
  453. this.listeners = this.listeners || {};
  454. if (event in this.listeners) {
  455. this.listeners[event].push(callback);
  456. } else {
  457. this.listeners[event] = [callback];
  458. }
  459. };
  460. Observable.prototype.trigger = function (event) {
  461. var slice = Array.prototype.slice;
  462. this.listeners = this.listeners || {};
  463. if (event in this.listeners) {
  464. this.invoke(this.listeners[event], slice.call(arguments, 1));
  465. }
  466. if ('*' in this.listeners) {
  467. this.invoke(this.listeners['*'], arguments);
  468. }
  469. };
  470. Observable.prototype.invoke = function (listeners, params) {
  471. for (var i = 0, len = listeners.length; i < len; i++) {
  472. listeners[i].apply(this, params);
  473. }
  474. };
  475. Utils.Observable = Observable;
  476. Utils.generateChars = function (length) {
  477. var chars = '';
  478. for (var i = 0; i < length; i++) {
  479. var randomChar = Math.floor(Math.random() * 36);
  480. chars += randomChar.toString(36);
  481. }
  482. return chars;
  483. };
  484. return Utils;
  485. });
  486. define('select2/results',[
  487. './utils'
  488. ], function (Utils) {
  489. function Results ($element, options, dataAdapter) {
  490. this.$element = $element;
  491. this.data = dataAdapter;
  492. this.options = options;
  493. Results.__super__.constructor.call(this);
  494. }
  495. Utils.Extend(Results, Utils.Observable);
  496. Results.prototype.render = function () {
  497. var $results = $(
  498. '<ul class="options" role="tree"></ul>'
  499. );
  500. if (this.options.get('multiple')) {
  501. $results.attr('aria-multiselectable', 'true');
  502. }
  503. this.$results = $results;
  504. return $results;
  505. };
  506. Results.prototype.clear = function () {
  507. this.$results.empty();
  508. };
  509. Results.prototype.empty = function () {
  510. var $empty = $('<li role="treeitem" class="option"></li>');
  511. $empty.text(this.options.get('translations').get('noResults'));
  512. this.$results.append($empty);
  513. };
  514. Results.prototype.append = function (data) {
  515. var $options = [];
  516. if (data.length === 0) {
  517. this.empty();
  518. return;
  519. }
  520. data = this.sort(data);
  521. for (var d = 0; d < data.length; d++) {
  522. var item = data[d];
  523. var $option = this.option(item);
  524. $options.push($option);
  525. }
  526. this.$results.append($options);
  527. };
  528. Results.prototype.sort = function (data) {
  529. return data;
  530. };
  531. Results.prototype.setClasses = function () {
  532. var self = this;
  533. this.data.current(function (selected) {
  534. var selectedIds = $.map(selected, function (s) {
  535. return s.id.toString();
  536. });
  537. var $options = self.$results.find('.option[aria-selected]');
  538. $options.each(function () {
  539. var $option = $(this);
  540. var item = $option.data('data');
  541. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  542. $option.attr('aria-selected', 'true');
  543. } else {
  544. $option.attr('aria-selected', 'false');
  545. }
  546. });
  547. var $selected = $options.filter('[aria-selected=true]');
  548. // Check if there are any selected options
  549. if ($selected.length > 0) {
  550. // If there are selected options, highlight the first
  551. $selected.first().trigger('mouseenter');
  552. } else {
  553. // If there are no selected options, highlight the first option
  554. // in the dropdown
  555. $options.first().trigger('mouseenter');
  556. }
  557. });
  558. };
  559. Results.prototype.option = function (data) {
  560. var $option = $(
  561. '<li class="option" role="treeitem" aria-selected="false"></li>'
  562. );
  563. if (data.children) {
  564. $option
  565. .attr('role', 'group')
  566. .attr('aria-label', data.text)
  567. .removeAttr('aria-selected');
  568. var $label = $('<strong class="group-label"></strong>');
  569. this.template(data, $label);
  570. var $children = [];
  571. for (var c = 0; c < data.children.length; c++) {
  572. var child = data.children[c];
  573. var $child = this.option(child);
  574. $children.push($child);
  575. }
  576. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  577. $childrenContainer.append($children);
  578. $option.append($label);
  579. $option.append($childrenContainer);
  580. } else {
  581. this.template(data, $option);
  582. }
  583. if (data.disabled) {
  584. $option
  585. .removeAttr('aria-selected')
  586. .attr('aria-disabled', 'true');
  587. }
  588. if (data.id == null) {
  589. $option.removeAttr('aria-selected');
  590. }
  591. if (data._resultId != null) {
  592. $option.attr('id', data._resultId);
  593. }
  594. $option.data('data', data);
  595. return $option;
  596. };
  597. Results.prototype.bind = function (container, $container) {
  598. var self = this;
  599. var id = container.id + '-results';
  600. this.$results.attr('id', id);
  601. container.on('results:all', function (params) {
  602. self.clear();
  603. self.append(params.data);
  604. if (container.isOpen()) {
  605. self.setClasses();
  606. }
  607. });
  608. container.on('results:append', function (params) {
  609. self.append(params.data);
  610. if (container.isOpen()) {
  611. self.setClasses();
  612. }
  613. });
  614. container.on('select', function () {
  615. if (!container.isOpen()) {
  616. return;
  617. }
  618. self.setClasses();
  619. });
  620. container.on('unselect', function () {
  621. if (!container.isOpen()) {
  622. return;
  623. }
  624. self.setClasses();
  625. });
  626. container.on('open', function () {
  627. // When the dropdown is open, aria-expended="true"
  628. self.$results.attr('aria-expanded', 'true');
  629. self.$results.attr('aria-hidden', 'false');
  630. self.setClasses();
  631. self.ensureHighlightVisible();
  632. });
  633. container.on('close', function () {
  634. // When the dropdown is closed, aria-expended="false"
  635. self.$results.attr('aria-expanded', 'false');
  636. self.$results.attr('aria-hidden', 'true');
  637. self.$results.removeAttr('aria-activedescendant');
  638. });
  639. container.on('results:select', function () {
  640. var $highlighted = self.$results.find('.highlighted');
  641. if ($highlighted.length === 0) {
  642. return;
  643. }
  644. var data = $highlighted.data('data');
  645. if ($highlighted.attr('aria-selected') == 'true') {
  646. self.trigger('unselected', {
  647. data: data
  648. });
  649. } else {
  650. self.trigger('selected', {
  651. data: data
  652. });
  653. }
  654. });
  655. container.on('results:previous', function () {
  656. var $highlighted = self.$results.find('.highlighted');
  657. var $options = self.$results.find('[aria-selected]');
  658. var currentIndex = $options.index($highlighted);
  659. // If we are already at te top, don't move further
  660. if (currentIndex === 0) {
  661. return;
  662. }
  663. var nextIndex = currentIndex - 1;
  664. // If none are highlighted, highlight the first
  665. if ($highlighted.length === 0) {
  666. nextIndex = 0;
  667. }
  668. var $next = $options.eq(nextIndex);
  669. $next.trigger('mouseenter');
  670. var currentOffset = self.$results.offset().top;
  671. var nextTop = $next.offset().top;
  672. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  673. if (nextIndex === 0) {
  674. self.$results.scrollTop(0);
  675. } else if (nextTop - currentOffset < 0) {
  676. self.$results.scrollTop(nextOffset);
  677. }
  678. });
  679. container.on('results:next', function () {
  680. var $highlighted = self.$results.find('.highlighted');
  681. var $options = self.$results.find('[aria-selected]');
  682. var currentIndex = $options.index($highlighted);
  683. var nextIndex = currentIndex + 1;
  684. // If we are at the last option, stay there
  685. if (nextIndex >= $options.length) {
  686. return;
  687. }
  688. var $next = $options.eq(nextIndex);
  689. $next.trigger('mouseenter');
  690. var currentOffset = self.$results.offset().top +
  691. self.$results.outerHeight(false);
  692. var nextBottom = $next.offset().top + $next.outerHeight(false);
  693. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  694. if (nextIndex === 0) {
  695. self.$results.scrollTop(0);
  696. } else if (nextBottom > currentOffset) {
  697. self.$results.scrollTop(nextOffset);
  698. }
  699. });
  700. container.on('results:focus', function (params) {
  701. params.element.addClass('highlighted');
  702. });
  703. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  704. var $this = $(this);
  705. var data = $this.data('data');
  706. if ($this.attr('aria-selected') === 'true') {
  707. self.trigger('unselected', {
  708. originalEvent: evt,
  709. data: data
  710. });
  711. return;
  712. }
  713. self.trigger('selected', {
  714. originalEvent: evt,
  715. data: data
  716. });
  717. });
  718. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  719. var data = $(this).data('data');
  720. self.$results.find('.option.highlighted').removeClass('highlighted');
  721. self.trigger('results:focus', {
  722. data: data,
  723. element: $(this)
  724. });
  725. });
  726. this.$results.on('mouseleave', '.option', function (evt) {
  727. if ($(this).hasClass('highlighted')) {
  728. $(this).removeClass('highlighted');
  729. self.$results.removeAttr('aria-activedescendant');
  730. }
  731. });
  732. };
  733. Results.prototype.ensureHighlightVisible = function () {
  734. var $highlighted = this.$results.find('.highlighted');
  735. if ($highlighted.length === 0) {
  736. return;
  737. }
  738. var $options = this.$results.find('[aria-selected]');
  739. var currentIndex = $options.index($highlighted);
  740. var currentOffset = this.$results.offset().top;
  741. var nextTop = $highlighted.offset().top;
  742. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  743. var offsetDelta = nextTop - currentOffset;
  744. nextOffset -= $highlighted.outerHeight(false) * 2;
  745. if (currentIndex <= 2) {
  746. this.$results.scrollTop(0);
  747. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  748. this.$results.scrollTop(nextOffset);
  749. }
  750. };
  751. Results.prototype.template = function (result, $container) {
  752. var template = this.options.get('templateResult');
  753. $container.html(template(result));
  754. };
  755. return Results;
  756. });
  757. define('select2/selection/base',[
  758. '../utils'
  759. ], function (Utils) {
  760. function BaseSelection ($element, options) {
  761. this.$element = $element;
  762. this.options = options;
  763. BaseSelection.__super__.constructor.call(this);
  764. }
  765. Utils.Extend(BaseSelection, Utils.Observable);
  766. BaseSelection.prototype.render = function () {
  767. throw new Error('The `render` method must be defined in child classes.');
  768. };
  769. BaseSelection.prototype.bind = function (container, $container) {
  770. var self = this;
  771. container.on('selection:update', function (params) {
  772. self.update(params.data);
  773. });
  774. container.on('open', function () {
  775. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  776. var $target = $(e.target);
  777. var $select = $target.closest('.select2');
  778. var $all = $('.select2.open');
  779. $all.each(function () {
  780. var $this = $(this);
  781. if (this == $select[0]) {
  782. return;
  783. }
  784. var $element = $this.data('element');
  785. $element.select2('close');
  786. });
  787. });
  788. container.on('close', function () {
  789. $(document.body).off('mousedown.select2.' + container.id);
  790. });
  791. });
  792. };
  793. BaseSelection.prototype.update = function (data) {
  794. throw new Error('The `update` method must be defined in child classes.');
  795. };
  796. return BaseSelection;
  797. });
  798. define('select2/keys',[
  799. ], function () {
  800. var KEYS = {
  801. BACKSPACE: 8,
  802. TAB: 9,
  803. ENTER: 13,
  804. SHIFT: 16,
  805. CTRL: 17,
  806. ALT: 18,
  807. ESC: 27,
  808. SPACE: 32,
  809. PAGE_UP: 33,
  810. PAGE_DOWN: 34,
  811. END: 35,
  812. HOME: 36,
  813. LEFT: 37,
  814. UP: 38,
  815. RIGHT: 39,
  816. DOWN: 40,
  817. DELETE: 46,
  818. isArrow: function (k) {
  819. k = k.which ? k.which : k;
  820. switch (k) {
  821. case KEY.LEFT:
  822. case KEY.RIGHT:
  823. case KEY.UP:
  824. case KEY.DOWN:
  825. return true;
  826. }
  827. return false;
  828. }
  829. };
  830. return KEYS;
  831. });
  832. define('select2/selection/single',[
  833. './base',
  834. '../utils',
  835. '../keys'
  836. ], function (BaseSelection, Utils, KEYS) {
  837. function SingleSelection () {
  838. SingleSelection.__super__.constructor.apply(this, arguments);
  839. }
  840. Utils.Extend(SingleSelection, BaseSelection);
  841. SingleSelection.prototype.render = function () {
  842. var $selection = $(
  843. '<span class="single-select" tabindex="0" role="combobox" ' +
  844. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  845. '<span class="rendered-selection"></span>' +
  846. '</span>'
  847. );
  848. $selection.attr('title', this.$element.attr('title'));
  849. this.$selection = $selection;
  850. return $selection;
  851. };
  852. SingleSelection.prototype.bind = function (container, $container) {
  853. var self = this;
  854. SingleSelection.__super__.bind.apply(this, arguments);
  855. var id = container.id + '-container';
  856. var resultsId = container.id + '-results';
  857. this.$selection.find('.rendered-selection').attr('id', id);
  858. this.$selection.attr('aria-labelledby', id);
  859. this.$selection.attr('aria-owns', resultsId);
  860. this.$selection.on('mousedown', function (evt) {
  861. // Only respond to left clicks
  862. if (evt.which !== 1) {
  863. return;
  864. }
  865. self.trigger('toggle', {
  866. originalEvent: evt
  867. });
  868. });
  869. container.on('open', function () {
  870. // When the dropdown is open, aria-expanded="true"
  871. self.$selection.attr('aria-expanded', 'true');
  872. });
  873. container.on('close', function () {
  874. // When the dropdown is closed, aria-expanded="false"
  875. self.$selection.attr('aria-expanded', 'false');
  876. self.$selection.removeAttr('aria-activedescendant');
  877. self.$selection.focus();
  878. });
  879. this.$selection.on('focus', function (evt) {
  880. // User focuses on the container
  881. });
  882. this.$selection.on('blur', function (evt) {
  883. // User exits the container
  884. });
  885. this.$selection.on('keydown', function (evt) {
  886. self.trigger('keypress', evt);
  887. if (evt.which === KEYS.SPACE) {
  888. evt.preventDefault();
  889. }
  890. });
  891. container.on('results:focus', function (params) {
  892. self.$selection.attr('aria-activedescendant', params.data._resultId);
  893. });
  894. container.on('selection:update', function (params) {
  895. self.update(params.data);
  896. });
  897. };
  898. SingleSelection.prototype.clear = function () {
  899. this.$selection.find('.rendered-selection').empty();
  900. };
  901. SingleSelection.prototype.display = function (data) {
  902. var template = this.options.get('templateSelection');
  903. return template(data);
  904. };
  905. SingleSelection.prototype.selectionContainer = function () {
  906. return $('<span></span>');
  907. };
  908. SingleSelection.prototype.update = function (data) {
  909. if (data.length === 0) {
  910. this.clear();
  911. return;
  912. }
  913. var selection = data[0];
  914. var formatted = this.display(selection);
  915. this.$selection.find('.rendered-selection').html(formatted);
  916. };
  917. return SingleSelection;
  918. });
  919. define('select2/selection/multiple',[
  920. './base',
  921. '../utils'
  922. ], function (BaseSelection, Utils) {
  923. function MultipleSelection ($element, options) {
  924. this.$element = $element;
  925. this.options = options;
  926. MultipleSelection.__super__.constructor.call(this);
  927. }
  928. Utils.Extend(MultipleSelection, BaseSelection);
  929. MultipleSelection.prototype.render = function () {
  930. var $selection = $(
  931. '<span class="multiple-select">' +
  932. '<ul class="rendered-selection"></ul>' +
  933. '</span>'
  934. );
  935. this.$selection = $selection;
  936. return $selection;
  937. };
  938. MultipleSelection.prototype.bind = function (container, $container) {
  939. var self = this;
  940. MultipleSelection.__super__.bind.apply(this, arguments);
  941. this.$selection.on('click', function (evt) {
  942. self.trigger('toggle', {
  943. originalEvent: evt
  944. });
  945. });
  946. this.$selection.on('click', '.remove', function (evt) {
  947. var $remove = $(this);
  948. var $selection = $remove.parent();
  949. var data = $selection.data('data');
  950. self.trigger('unselected', {
  951. originalEvent: evt,
  952. data: data
  953. });
  954. });
  955. };
  956. MultipleSelection.prototype.clear = function () {
  957. this.$selection.find('.rendered-selection').empty();
  958. };
  959. MultipleSelection.prototype.display = function (data) {
  960. var template = this.options.get('templateSelection');
  961. return template(data);
  962. };
  963. MultipleSelection.prototype.selectionContainer = function () {
  964. var $container = $(
  965. '<li class="choice">' +
  966. '<span class="remove" role="presentation">&times;</span>' +
  967. '</li>'
  968. );
  969. return $container;
  970. };
  971. MultipleSelection.prototype.update = function (data) {
  972. this.clear();
  973. if (data.length === 0) {
  974. return;
  975. }
  976. var $selections = [];
  977. for (var d = 0; d < data.length; d++) {
  978. var selection = data[d];
  979. var formatted = this.display(selection);
  980. var $selection = this.selectionContainer();
  981. $selection.append(formatted);
  982. $selection.data('data', selection);
  983. $selections.push($selection);
  984. }
  985. this.$selection.find('.rendered-selection').append($selections);
  986. };
  987. return MultipleSelection;
  988. });
  989. define('select2/selection/placeholder',[
  990. '../utils'
  991. ], function (Utils) {
  992. function Placeholder (decorated, $element, options) {
  993. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  994. decorated.call(this, $element, options);
  995. }
  996. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  997. if (typeof placeholder === 'string') {
  998. placeholder = {
  999. id: '',
  1000. text: placeholder
  1001. };
  1002. }
  1003. return placeholder;
  1004. };
  1005. Placeholder.prototype.update = function (decorated, data) {
  1006. var singlePlaceholder = (
  1007. data.length == 1 && data[0].id != this.placeholder.id
  1008. );
  1009. var multipleSelections = data.length > 1;
  1010. if (multipleSelections || singlePlaceholder) {
  1011. return decorated.call(this, data);
  1012. }
  1013. this.clear();
  1014. var $placeholder = this.selectionContainer();
  1015. $placeholder.html(this.display(this.placeholder));
  1016. $placeholder.addClass('placeholder').removeClass('choice');
  1017. this.$selection.find('.rendered-selection').append($placeholder);
  1018. };
  1019. return Placeholder;
  1020. });
  1021. define('select2/translation',[
  1022. ], function () {
  1023. function Translation (dict) {
  1024. this.dict = dict || {};
  1025. }
  1026. Translation.prototype.all = function () {
  1027. return this.dict;
  1028. };
  1029. Translation.prototype.get = function (key) {
  1030. return this.dict[key];
  1031. };
  1032. Translation.prototype.extend = function (translation) {
  1033. this.dict = $.extend({}, translation.all(), this.dict);
  1034. };
  1035. // Static functions
  1036. Translation._cache = {};
  1037. Translation.loadPath = function (path) {
  1038. if (!(path in Translation._cache)) {
  1039. var translations = require(path);
  1040. Translation._cache[path] = translations;
  1041. }
  1042. return new Translation(Translation._cache[path]);
  1043. };
  1044. return Translation;
  1045. });
  1046. define('select2/data/base',[
  1047. '../utils'
  1048. ], function (Utils) {
  1049. function BaseAdapter ($element, options) {
  1050. BaseAdapter.__super__.constructor.call(this);
  1051. }
  1052. Utils.Extend(BaseAdapter, Utils.Observable);
  1053. BaseAdapter.prototype.current = function (callback) {
  1054. throw new Error('The `current` method must be defined in child classes.');
  1055. };
  1056. BaseAdapter.prototype.query = function (params, callback) {
  1057. throw new Error('The `query` method must be defined in child classes.');
  1058. };
  1059. BaseAdapter.prototype.bind = function (container, $container) {
  1060. // Can be implemented in subclasses
  1061. };
  1062. BaseAdapter.prototype.generateResultId = function (container, data) {
  1063. var id = container.id + '-result-';
  1064. id += Utils.generateChars(4);
  1065. if (data.id != null) {
  1066. id += '-' + data.id.toString();
  1067. } else {
  1068. id += '-' + Utils.generateChars(4);
  1069. }
  1070. return id;
  1071. };
  1072. return BaseAdapter;
  1073. });
  1074. define('select2/data/select',[
  1075. './base',
  1076. '../utils',
  1077. 'jquery'
  1078. ], function (BaseAdapter, Utils, $) {
  1079. function SelectAdapter ($element, options) {
  1080. this.$element = $element;
  1081. SelectAdapter.__super__.constructor.call(this);
  1082. }
  1083. Utils.Extend(SelectAdapter, BaseAdapter);
  1084. SelectAdapter.prototype.current = function (callback) {
  1085. var data = [];
  1086. var self = this;
  1087. this.$element.find(':selected').each(function () {
  1088. var $option = $(this);
  1089. var option = self.item($option);
  1090. data.push(option);
  1091. });
  1092. callback(data);
  1093. };
  1094. SelectAdapter.prototype.select = function (data) {
  1095. var self = this;
  1096. if (this.$element.prop('multiple')) {
  1097. this.current(function (currentData) {
  1098. var val = [];
  1099. data = [data];
  1100. data.push.apply(data, currentData);
  1101. for (var d = 0; d < data.length; d++) {
  1102. id = data[d].id;
  1103. if (val.indexOf(id) === -1) {
  1104. val.push(id);
  1105. }
  1106. }
  1107. self.$element.val(val);
  1108. self.$element.trigger('change');
  1109. });
  1110. } else {
  1111. var val = data.id;
  1112. this.$element.val(val);
  1113. this.$element.trigger('change');
  1114. }
  1115. };
  1116. SelectAdapter.prototype.unselect = function (data) {
  1117. var self = this;
  1118. if (!this.$element.prop('multiple')) {
  1119. return;
  1120. }
  1121. this.current(function (currentData) {
  1122. var val = [];
  1123. for (var d = 0; d < currentData.length; d++) {
  1124. id = currentData[d].id;
  1125. if (id !== data.id && val.indexOf(id) === -1) {
  1126. val.push(id);
  1127. }
  1128. }
  1129. self.$element.val(val);
  1130. self.$element.trigger('change');
  1131. });
  1132. };
  1133. SelectAdapter.prototype.bind = function (container, $container) {
  1134. var self = this;
  1135. this.container = container;
  1136. container.on('select', function (params) {
  1137. self.select(params.data);
  1138. });
  1139. container.on('unselect', function (params) {
  1140. self.unselect(params.data);
  1141. });
  1142. };
  1143. SelectAdapter.prototype.query = function (params, callback) {
  1144. var data = [];
  1145. var self = this;
  1146. var $options = this.$element.children();
  1147. $options.each(function () {
  1148. var $option = $(this);
  1149. if (!$option.is('option') && !$option.is('optgroup')) {
  1150. return;
  1151. }
  1152. var option = self.item($option);
  1153. var matches = self.matches(params, option);
  1154. if (matches !== null) {
  1155. data.push(matches);
  1156. }
  1157. });
  1158. callback(data);
  1159. };
  1160. SelectAdapter.prototype.option = function (data) {
  1161. var $option = $('<option></option>');
  1162. $option.text(data.text);
  1163. $option.val(data.id);
  1164. $option.prop('disabled', data.disabled || false);
  1165. // Get any automatically generated data values
  1166. var detectedData = this.item($option);
  1167. // Merge it with the already present data
  1168. var combinedData = $.extend({}, data, detectedData);
  1169. // Override the option's data with the combined data
  1170. $option.data('data', combinedData);
  1171. return $option;
  1172. };
  1173. SelectAdapter.prototype.item = function ($option) {
  1174. var data = $option.data('data');
  1175. // If the data has already be generated, use it
  1176. if (data == null) {
  1177. if ($option.is('option')) {
  1178. data = {
  1179. id: $option.val(),
  1180. text: $option.html(),
  1181. disabled: $option.prop('disabled')
  1182. };
  1183. } else if ($option.is('optgroup')) {
  1184. data = {
  1185. text: $option.attr('label'),
  1186. children: []
  1187. };
  1188. var $children = $option.children('option');
  1189. var children = [];
  1190. for (var c = 0; c < $children.length; c++) {
  1191. var $child = $($children[c]);
  1192. var child = this.item($child);
  1193. children.push(child);
  1194. }
  1195. data.children = children;
  1196. }
  1197. if (data.id && this.container != null) {
  1198. data._resultId = this.generateResultId(this.container, data);
  1199. }
  1200. $option.data('data', data);
  1201. }
  1202. return data;
  1203. };
  1204. SelectAdapter.prototype.matches = function (params, data) {
  1205. var match = $.extend(true, {}, data);
  1206. if (data.children) {
  1207. for (var c = data.children.length - 1; c >= 0; c--) {
  1208. var child = data.children[c];
  1209. var matches = this.matches(params, child);
  1210. // If there wasn't a match, remove the object in the array
  1211. if (matches === null) {
  1212. match.children.splice(c, 1);
  1213. }
  1214. }
  1215. if (match.children.length > 0) {
  1216. return match;
  1217. }
  1218. }
  1219. if ($.trim(params.term) === '') {
  1220. return match;
  1221. }
  1222. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1223. return match;
  1224. }
  1225. return null;
  1226. };
  1227. return SelectAdapter;
  1228. });
  1229. define('select2/data/array',[
  1230. './select',
  1231. '../utils',
  1232. 'jquery'
  1233. ], function (SelectAdapter, Utils, $) {
  1234. function ArrayAdapter ($element, options) {
  1235. this.data = options.get('data');
  1236. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1237. }
  1238. Utils.Extend(ArrayAdapter, SelectAdapter);
  1239. ArrayAdapter.prototype.select = function (data) {
  1240. var self = this;
  1241. this.$element.find('option').each(function () {
  1242. var $option = $(this);
  1243. var option = self.item($option);
  1244. if (option.id == data.id.toString()) {
  1245. $option.remove();
  1246. }
  1247. });
  1248. var $option = this.option(data);
  1249. this.$element.append($option);
  1250. ArrayAdapter.__super__.select.call(this, data);
  1251. };
  1252. ArrayAdapter.prototype.query = function (params, callback) {
  1253. var matches = [];
  1254. var self = this;
  1255. $.each(this.data, function () {
  1256. var option = this;
  1257. if (self.matches(params, option)) {
  1258. matches.push(option);
  1259. }
  1260. });
  1261. callback(matches);
  1262. };
  1263. return ArrayAdapter;
  1264. });
  1265. define('select2/data/ajax',[
  1266. './array',
  1267. '../utils',
  1268. 'jquery'
  1269. ], function (ArrayAdapter, Utils, $) {
  1270. function AjaxAdapter ($element, options) {
  1271. this.ajaxOptions = options.get('ajax');
  1272. if (this.ajaxOptions.processResults != null) {
  1273. this.processResults = this.ajaxOptions.processResults;
  1274. }
  1275. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1276. }
  1277. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1278. AjaxAdapter.prototype.processResults = function (results) {
  1279. return results;
  1280. };
  1281. AjaxAdapter.prototype.query = function (params, callback) {
  1282. var matches = [];
  1283. var self = this;
  1284. var options = $.extend({
  1285. type: 'GET'
  1286. }, this.ajaxOptions);
  1287. if (typeof options.url === 'function') {
  1288. options.url = options.url(params);
  1289. }
  1290. if (typeof options.data === 'function') {
  1291. options.data = options.data(params);
  1292. }
  1293. function request () {
  1294. var $request = $.ajax(options);
  1295. $request.success(function (data) {
  1296. var results = self.processResults(data);
  1297. callback(results);
  1298. });
  1299. }
  1300. if (this.ajaxOptions.delay && params.term !== '') {
  1301. if (this._queryTimeout) {
  1302. window.clearTimeout(this._queryTimeout);
  1303. }
  1304. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  1305. } else {
  1306. request();
  1307. }
  1308. };
  1309. return AjaxAdapter;
  1310. });
  1311. define('select2/data/tags',[
  1312. ], function () {
  1313. function Tags (decorated, $element, options) {
  1314. var tags = options.get('tags');
  1315. decorated.call(this, $element, options);
  1316. }
  1317. Tags.prototype.query = function (decorated, params, callback) {
  1318. var self = this;
  1319. this._removeOldTags();
  1320. if (params.term == null || params.term === '' || params.page != null) {
  1321. decorated.call(this, params, callback);
  1322. return;
  1323. }
  1324. function wrapper (data, child) {
  1325. for (var i = 0; i < data.length; i++) {
  1326. var option = data[i];
  1327. var checkChildren = (
  1328. option.children != null && !wrapper(option.children, true)
  1329. );
  1330. var checkText = option.text === params.term;
  1331. if (checkText || checkChildren) {
  1332. if (child) {
  1333. return false;
  1334. }
  1335. callback(data);
  1336. return;
  1337. }
  1338. }
  1339. if (child) {
  1340. return true;
  1341. }
  1342. var tag = self.createTag(params);
  1343. var $option = self.option(tag);
  1344. $option.attr('data-select2-tag', true);
  1345. self.$element.append($option);
  1346. self.insertTag(data, tag);
  1347. callback(data);
  1348. }
  1349. decorated.call(this, params, wrapper);
  1350. };
  1351. Tags.prototype.createTag = function (decorated, params) {
  1352. return {
  1353. id: params.term,
  1354. text: params.term
  1355. };
  1356. };
  1357. Tags.prototype.insertTag = function (_, data, tag) {
  1358. data.unshift(tag);
  1359. };
  1360. Tags.prototype._removeOldTags = function (_) {
  1361. var tag = this._lastTag;
  1362. var $options = this.$element.find('option[data-select2-tag]');
  1363. $options.each(function () {
  1364. if (this.selected) {
  1365. return;
  1366. }
  1367. $(this).remove();
  1368. });
  1369. };
  1370. return Tags;
  1371. });
  1372. define('select2/dropdown',[
  1373. './utils'
  1374. ], function (Utils) {
  1375. function Dropdown ($element, options) {
  1376. this.$element = $element;
  1377. }
  1378. Utils.Extend(Dropdown, Utils.Observable);
  1379. Dropdown.prototype.render = function () {
  1380. var $dropdown = $(
  1381. '<span class="dropdown">' +
  1382. '<span class="results"></span>' +
  1383. '</span>'
  1384. );
  1385. return $dropdown;
  1386. };
  1387. Dropdown.prototype.bind = function (container, $container) {
  1388. // Can be implemented in subclasses
  1389. };
  1390. return Dropdown;
  1391. });
  1392. define('select2/dropdown/search',[
  1393. '../utils'
  1394. ], function (Utils) {
  1395. function Search () { }
  1396. Search.prototype.render = function (decorated) {
  1397. var $rendered = decorated.call(this);
  1398. var $search = $(
  1399. '<span class="search">' +
  1400. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1401. '</span>'
  1402. );
  1403. this.$searchContainer = $search;
  1404. this.$search = $search.find('input');
  1405. $rendered.prepend($search);
  1406. return $rendered;
  1407. };
  1408. Search.prototype.bind = function (decorated, container, $container) {
  1409. var self = this;
  1410. decorated.call(this, container, $container);
  1411. this.$search.on('keydown', function (evt) {
  1412. self.trigger('keypress', evt);
  1413. self._keyUpPrevented = evt.isDefaultPrevented();
  1414. });
  1415. this.$search.on('keyup', function (evt) {
  1416. if (!self._keyUpPrevented) {
  1417. self.trigger('query', {
  1418. term: $(this).val()
  1419. });
  1420. }
  1421. self._keyUpPrevented = false;
  1422. });
  1423. container.on('open', function () {
  1424. self.$search.attr('tabindex', 0);
  1425. });
  1426. container.on('close', function () {
  1427. self.$search.attr('tabindex', -1);
  1428. self.$search.val('');
  1429. });
  1430. container.on('results:all', function (params) {
  1431. if (params.query.term == null || params.query.term === '') {
  1432. var showSearch = self.showSearch(params);
  1433. if (showSearch) {
  1434. self.$searchContainer.show();
  1435. } else {
  1436. self.$searchContainer.hide();
  1437. }
  1438. }
  1439. });
  1440. };
  1441. Search.prototype.showSearch = function (_, params) {
  1442. return true;
  1443. };
  1444. return Search;
  1445. });
  1446. define('select2/dropdown/hidePlaceholder',[
  1447. ], function () {
  1448. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1449. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1450. decorated.call(this, $element, options, dataAdapter);
  1451. }
  1452. HidePlaceholder.prototype.append = function (decorated, data) {
  1453. data = this.removePlaceholder(data);
  1454. decorated.call(this, data);
  1455. };
  1456. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1457. if (typeof placeholder === 'string') {
  1458. placeholder = {
  1459. id: '',
  1460. text: placeholder
  1461. };
  1462. }
  1463. return placeholder;
  1464. };
  1465. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1466. var modifiedData = data.slice(0);
  1467. for (var d = data.length - 1; d >= 0; d--) {
  1468. var item = data[d];
  1469. if (this.placeholder.id === item.id) {
  1470. modifiedData.splice(d, 1);
  1471. }
  1472. }
  1473. return modifiedData;
  1474. };
  1475. return HidePlaceholder;
  1476. });
  1477. define('select2/i18n/en',[],function () {
  1478. return {
  1479. noResults: function () {
  1480. return 'No results found';
  1481. }
  1482. };
  1483. });
  1484. define('select2/defaults',[
  1485. 'jquery',
  1486. './results',
  1487. './selection/single',
  1488. './selection/multiple',
  1489. './selection/placeholder',
  1490. './utils',
  1491. './translation',
  1492. './data/select',
  1493. './data/array',
  1494. './data/ajax',
  1495. './data/tags',
  1496. './dropdown',
  1497. './dropdown/search',
  1498. './dropdown/hidePlaceholder',
  1499. './i18n/en'
  1500. ], function ($, ResultsList,
  1501. SingleSelection, MultipleSelection, Placeholder,
  1502. Utils, Translation,
  1503. SelectData, ArrayData, AjaxData, Tags,
  1504. Dropdown, Search, HidePlaceholder,
  1505. EnglishTranslation) {
  1506. function Defaults () {
  1507. this.reset();
  1508. }
  1509. Defaults.prototype.apply = function (options) {
  1510. options = $.extend({}, this.defaults, options);
  1511. if (options.dataAdapter == null) {
  1512. if (options.ajax) {
  1513. options.dataAdapter = AjaxData;
  1514. } else if (options.data) {
  1515. options.dataAdapter = ArrayData;
  1516. } else {
  1517. options.dataAdapter = SelectData;
  1518. }
  1519. }
  1520. if (options.tags != null) {
  1521. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1522. }
  1523. if (options.resultsAdapter == null) {
  1524. options.resultsAdapter = ResultsList;
  1525. if (options.placeholder != null) {
  1526. options.resultsAdapter = Utils.Decorate(
  1527. options.resultsAdapter,
  1528. HidePlaceholder
  1529. );
  1530. }
  1531. }
  1532. if (options.dropdownAdapter == null) {
  1533. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1534. options.dropdownAdapter = SearchableDropdown;
  1535. }
  1536. if (options.selectionAdapter == null) {
  1537. if (options.multiple) {
  1538. options.selectionAdapter = MultipleSelection;
  1539. } else {
  1540. options.selectionAdapter = SingleSelection;
  1541. }
  1542. // Add the placeholder mixin if a placeholder was specified
  1543. if (options.placeholder != null) {
  1544. options.selectionAdapter = Utils.Decorate(
  1545. options.selectionAdapter,
  1546. Placeholder
  1547. );
  1548. }
  1549. }
  1550. if (typeof options.language === 'string') {
  1551. options.language = [options.language];
  1552. }
  1553. if ($.isArray(options.language)) {
  1554. var languages = new Translation();
  1555. var languageNames = options.language.concat(this.defaults.language);
  1556. for (var l = 0; l < languageNames.length; l++) {
  1557. var name = languageNames[l];
  1558. var language = {};
  1559. try {
  1560. // Try to load it with the original name
  1561. language = Translation.loadPath(name);
  1562. } catch (e) {
  1563. // If we couldn't load it, check if it wasn't the full path
  1564. name = 'select2/i18n/' + name;
  1565. language = Translation.loadPath(name);
  1566. }
  1567. languages.extend(language);
  1568. }
  1569. options.translations = languages;
  1570. } else {
  1571. options.translations = new Translations(options.language);
  1572. }
  1573. return options;
  1574. };
  1575. Defaults.prototype.reset = function () {
  1576. this.defaults = {
  1577. language: ['select2/i18n/en'],
  1578. templateResult: function (result) {
  1579. return result.text;
  1580. },
  1581. templateSelection: function (selection) {
  1582. return selection.text;
  1583. }
  1584. };
  1585. };
  1586. var defaults = new Defaults();
  1587. return defaults;
  1588. });
  1589. define('select2/options',[
  1590. './defaults'
  1591. ], function (Defaults) {
  1592. function Options (options, $element) {
  1593. this.options = options;
  1594. if ($element != null) {
  1595. this.fromElement($element);
  1596. }
  1597. this.options = Defaults.apply(this.options);
  1598. }
  1599. Options.prototype.fromElement = function ($e) {
  1600. if (this.options.multiple == null) {
  1601. this.options.multiple = $e.prop('multiple');
  1602. }
  1603. return this;
  1604. };
  1605. Options.prototype.get = function (key) {
  1606. return this.options[key];
  1607. };
  1608. Options.prototype.set = function (key, val) {
  1609. this.options[key] = val;
  1610. };
  1611. return Options;
  1612. });
  1613. define('select2/core',[
  1614. 'jquery',
  1615. './options',
  1616. './utils',
  1617. './keys'
  1618. ], function ($, Options, Utils, KEYS) {
  1619. var Select2 = function ($element, options) {
  1620. this.$element = $element;
  1621. this.id = this._generateId($element);
  1622. options = options || {};
  1623. this.options = new Options(options, $element);
  1624. Select2.__super__.constructor.call(this);
  1625. // Set up containers and adapters
  1626. var DataAdapter = this.options.get('dataAdapter');
  1627. this.data = new DataAdapter($element, this.options);
  1628. var $container = this.render();
  1629. this._placeContainer($container);
  1630. var SelectionAdapter = this.options.get('selectionAdapter');
  1631. this.selection = new SelectionAdapter($element, this.options);
  1632. var $selection = this.selection.render();
  1633. this._placeSelection($selection);
  1634. var DropdownAdapter = this.options.get('dropdownAdapter');
  1635. this.dropdown = new DropdownAdapter($element, this.options);
  1636. var $dropdown = this.dropdown.render();
  1637. this._placeDropdown($dropdown);
  1638. var ResultsAdapter = this.options.get('resultsAdapter');
  1639. this.results = new ResultsAdapter($element, this.options, this.data);
  1640. var $results = this.results.render();
  1641. this._placeResults($results);
  1642. // Bind events
  1643. var self = this;
  1644. // Bind the container to all of the adapters
  1645. this._bindAdapters();
  1646. // Register any DOM event handlers
  1647. this._registerDomEvents();
  1648. // Register any internal event handlers
  1649. this._registerSelectionEvents();
  1650. this._registerDropdownEvents();
  1651. this._registerResultsEvents();
  1652. this._registerEvents();
  1653. // Set the initial state
  1654. this.data.current(function (initialData) {
  1655. self.trigger('selection:update', {
  1656. data: initialData
  1657. });
  1658. });
  1659. // Hide the original select
  1660. $element.hide();
  1661. $element.attr('tabindex', '-1');
  1662. $element.data('select2', this);
  1663. };
  1664. Utils.Extend(Select2, Utils.Observable);
  1665. Select2.prototype._generateId = function ($element) {
  1666. var id = '';
  1667. if ($element.attr('id') != null) {
  1668. id = $element.attr('id');
  1669. } else if ($element.attr('name') != null) {
  1670. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1671. } else {
  1672. id = Utils.generateChars(4);
  1673. }
  1674. id = 'select2-' + id;
  1675. return id;
  1676. };
  1677. Select2.prototype._placeContainer = function ($container) {
  1678. $container.insertAfter(this.$element);
  1679. $container.width(this.$element.outerWidth(false));
  1680. };
  1681. Select2.prototype._placeSelection = function ($selection) {
  1682. var $selectionContainer = this.$container.find('.selection');
  1683. $selectionContainer.append($selection);
  1684. };
  1685. Select2.prototype._placeDropdown = function ($dropdown) {
  1686. this.$dropdown = $dropdown;
  1687. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1688. $dropdownContainer.append($dropdown);
  1689. };
  1690. Select2.prototype._placeResults = function ($results) {
  1691. var $resultsContainer = this.$dropdown.find('.results');
  1692. $resultsContainer.append($results);
  1693. };
  1694. Select2.prototype._bindAdapters = function () {
  1695. this.data.bind(this, this.$container);
  1696. this.selection.bind(this, this.$container);
  1697. this.dropdown.bind(this, this.$container);
  1698. this.results.bind(this, this.$container);
  1699. };
  1700. Select2.prototype._registerDomEvents = function () {
  1701. var self = this;
  1702. this.$element.on('change', function () {
  1703. self.data.current(function (data) {
  1704. self.trigger('selection:update', {
  1705. data: data
  1706. });
  1707. });
  1708. });
  1709. };
  1710. Select2.prototype._registerSelectionEvents = function () {
  1711. var self = this;
  1712. this.selection.on('open', function () {
  1713. self.open();
  1714. });
  1715. this.selection.on('close', function () {
  1716. self.close();
  1717. });
  1718. this.selection.on('toggle', function () {
  1719. self.toggleDropdown();
  1720. });
  1721. this.selection.on('results:select', function () {
  1722. self.trigger('results:select');
  1723. });
  1724. this.selection.on('results:previous', function () {
  1725. self.trigger('results:previous');
  1726. });
  1727. this.selection.on('results:next', function () {
  1728. self.trigger('results:next');
  1729. });
  1730. this.selection.on('unselected', function (params) {
  1731. self.trigger('unselect', params);
  1732. self.close();
  1733. });
  1734. this.selection.on('keypress', function (e) {
  1735. self.trigger('keypress', e);
  1736. });
  1737. };
  1738. Select2.prototype._registerDropdownEvents = function () {
  1739. var self = this;
  1740. this.dropdown.on('query', function (params) {
  1741. self.trigger('query', params);
  1742. });
  1743. this.dropdown.on('keypress', function (e) {
  1744. self.trigger('keypress', e);
  1745. });
  1746. };
  1747. Select2.prototype._registerResultsEvents = function () {
  1748. var self = this;
  1749. this.results.on('selected', function (params) {
  1750. self.trigger('select', params);
  1751. self.close();
  1752. });
  1753. this.results.on('unselected', function (params) {
  1754. self.trigger('unselect', params);
  1755. self.close();
  1756. });
  1757. this.results.on('results:focus', function (params) {
  1758. self.trigger('results:focus', params);
  1759. });
  1760. };
  1761. Select2.prototype._registerEvents = function () {
  1762. var self = this;
  1763. this.on('open', function () {
  1764. self.$container.addClass('open');
  1765. });
  1766. this.on('close', function () {
  1767. self.$container.removeClass('open');
  1768. });
  1769. this.on('query', function (params) {
  1770. this.data.query(params, function (data) {
  1771. self.trigger('results:all', {
  1772. data: data,
  1773. query: params
  1774. });
  1775. });
  1776. });
  1777. this.on('keypress', function (evt) {
  1778. var key = evt.which;
  1779. if (self.isOpen()) {
  1780. if (key === KEYS.ENTER) {
  1781. self.trigger('results:select');
  1782. evt.preventDefault();
  1783. } else if (key === KEYS.UP) {
  1784. self.trigger('results:previous');
  1785. evt.preventDefault();
  1786. } else if (key === KEYS.DOWN) {
  1787. self.trigger('results:next');
  1788. evt.preventDefault();
  1789. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  1790. self.close();
  1791. evt.preventDefault();
  1792. }
  1793. } else {
  1794. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  1795. self.open();
  1796. evt.preventDefault();
  1797. }
  1798. }
  1799. });
  1800. };
  1801. Select2.prototype.toggleDropdown = function () {
  1802. if (this.isOpen()) {
  1803. this.close();
  1804. } else {
  1805. this.open();
  1806. }
  1807. };
  1808. Select2.prototype.open = function () {
  1809. if (this.isOpen()) {
  1810. return;
  1811. }
  1812. this.trigger('query', {});
  1813. this.trigger('open');
  1814. };
  1815. Select2.prototype.close = function () {
  1816. if (!this.isOpen()) {
  1817. return;
  1818. }
  1819. this.trigger('close');
  1820. };
  1821. Select2.prototype.isOpen = function () {
  1822. return this.$container.hasClass('open');
  1823. };
  1824. Select2.prototype.render = function () {
  1825. var $container = $(
  1826. '<span class="select2 select2-container select2-theme-default">' +
  1827. '<span class="selection"></span>' +
  1828. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  1829. '</span>'
  1830. );
  1831. this.$container = $container;
  1832. $container.data('element', this.$element);
  1833. return $container;
  1834. };
  1835. return Select2;
  1836. });
  1837. define('jquery.select2',[
  1838. 'jquery',
  1839. 'select2/core'
  1840. ], function ($, Select2) {
  1841. if ($.fn.select2 == null) {
  1842. $.fn.select2 = function (options) {
  1843. options = options || {};
  1844. if (typeof options === 'object') {
  1845. this.each(function () {
  1846. var instance = new Select2($(this), options);
  1847. });
  1848. } else if (typeof options === 'string') {
  1849. var instance = this.data('select2');
  1850. var args = Array.prototype.slice.call(arguments, 1);
  1851. instance[options](args);
  1852. } else {
  1853. throw new Error('Invalid arguments for Select2: ' + options);
  1854. }
  1855. };
  1856. }
  1857. return Select2;
  1858. });
  1859. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());