select2.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840
  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.displayMessage = function (params) {
  510. this.clear();
  511. this.hideLoading();
  512. var $message = $('<li role="treeitem" class="option"></li>');
  513. var message = this.options.get('translations').get(params.message);
  514. $message.text(message(params.args));
  515. this.$results.append($message);
  516. };
  517. Results.prototype.append = function (data) {
  518. this.hideLoading();
  519. var $options = [];
  520. if (data.length === 0) {
  521. if (this.$results.children().length === 0) {
  522. this.trigger('results:message', {
  523. message: 'noResults'
  524. });
  525. }
  526. return;
  527. }
  528. data = this.sort(data);
  529. for (var d = 0; d < data.length; d++) {
  530. var item = data[d];
  531. var $option = this.option(item);
  532. $options.push($option);
  533. }
  534. this.$results.append($options);
  535. };
  536. Results.prototype.sort = function (data) {
  537. return data;
  538. };
  539. Results.prototype.setClasses = function () {
  540. var self = this;
  541. this.data.current(function (selected) {
  542. var selectedIds = $.map(selected, function (s) {
  543. return s.id.toString();
  544. });
  545. var $options = self.$results.find('.option[aria-selected]');
  546. $options.each(function () {
  547. var $option = $(this);
  548. var item = $.data(this, 'data');
  549. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  550. $option.attr('aria-selected', 'true');
  551. } else {
  552. $option.attr('aria-selected', 'false');
  553. }
  554. });
  555. var $selected = $options.filter('[aria-selected=true]');
  556. // Check if there are any selected options
  557. if ($selected.length > 0) {
  558. // If there are selected options, highlight the first
  559. $selected.first().trigger('mouseenter');
  560. } else {
  561. // If there are no selected options, highlight the first option
  562. // in the dropdown
  563. $options.first().trigger('mouseenter');
  564. }
  565. });
  566. };
  567. Results.prototype.showLoading = function (params) {
  568. this.hideLoading();
  569. var loadingMore = this.options.get('translations').get('searching');
  570. var loading = {
  571. disabled: true,
  572. loading: true,
  573. text: loadingMore(params)
  574. };
  575. var $loading = this.option(loading);
  576. $loading.className += ' loading-results';
  577. this.$results.prepend($loading);
  578. };
  579. Results.prototype.hideLoading = function () {
  580. this.$results.find('.loading-results').remove();
  581. };
  582. Results.prototype.option = function (data) {
  583. var option = document.createElement('li');
  584. option.className = 'option';
  585. var attrs = {
  586. 'role': 'treeitem',
  587. 'aria-selected': 'false'
  588. };
  589. if (data.disabled) {
  590. delete attrs['aria-selected'];
  591. attrs['aria-disabled'] = 'true';
  592. }
  593. if (data.id == null) {
  594. delete attrs['aria-selected'];
  595. }
  596. if (data._resultId != null) {
  597. option.id = data._resultId;
  598. }
  599. if (data.children) {
  600. attrs.role = 'group';
  601. attrs['aria-label'] = data.text;
  602. delete attrs['aria-selected'];
  603. }
  604. for (var attr in attrs) {
  605. var val = attrs[attr];
  606. option.setAttribute(attr, val);
  607. }
  608. if (data.children) {
  609. var $option = $(option);
  610. var label = document.createElement('strong');
  611. label.className = 'group-label';
  612. var $label = $(label);
  613. this.template(data, label);
  614. var $children = [];
  615. for (var c = 0; c < data.children.length; c++) {
  616. var child = data.children[c];
  617. var $child = this.option(child);
  618. $children.push($child);
  619. }
  620. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  621. $childrenContainer.append($children);
  622. $option.append(label);
  623. $option.append($childrenContainer);
  624. } else {
  625. this.template(data, option);
  626. }
  627. $.data(option, 'data', data);
  628. return option;
  629. };
  630. Results.prototype.bind = function (container, $container) {
  631. var self = this;
  632. var id = container.id + '-results';
  633. this.$results.attr('id', id);
  634. container.on('results:all', function (params) {
  635. self.clear();
  636. self.append(params.data);
  637. if (container.isOpen()) {
  638. self.setClasses();
  639. }
  640. });
  641. container.on('results:append', function (params) {
  642. self.append(params.data);
  643. if (container.isOpen()) {
  644. self.setClasses();
  645. }
  646. });
  647. container.on('query', function (params) {
  648. self.showLoading(params);
  649. });
  650. container.on('select', function () {
  651. if (!container.isOpen()) {
  652. return;
  653. }
  654. self.setClasses();
  655. });
  656. container.on('unselect', function () {
  657. if (!container.isOpen()) {
  658. return;
  659. }
  660. self.setClasses();
  661. });
  662. container.on('open', function () {
  663. // When the dropdown is open, aria-expended="true"
  664. self.$results.attr('aria-expanded', 'true');
  665. self.$results.attr('aria-hidden', 'false');
  666. self.setClasses();
  667. self.ensureHighlightVisible();
  668. });
  669. container.on('close', function () {
  670. // When the dropdown is closed, aria-expended="false"
  671. self.$results.attr('aria-expanded', 'false');
  672. self.$results.attr('aria-hidden', 'true');
  673. self.$results.removeAttr('aria-activedescendant');
  674. });
  675. container.on('results:select', function () {
  676. var $highlighted = self.$results.find('.highlighted');
  677. if ($highlighted.length === 0) {
  678. return;
  679. }
  680. var data = $highlighted.data('data');
  681. if ($highlighted.attr('aria-selected') == 'true') {
  682. self.trigger('unselected', {
  683. data: data
  684. });
  685. } else {
  686. self.trigger('selected', {
  687. data: data
  688. });
  689. }
  690. });
  691. container.on('results:previous', function () {
  692. var $highlighted = self.$results.find('.highlighted');
  693. var $options = self.$results.find('[aria-selected]');
  694. var currentIndex = $options.index($highlighted);
  695. // If we are already at te top, don't move further
  696. if (currentIndex === 0) {
  697. return;
  698. }
  699. var nextIndex = currentIndex - 1;
  700. // If none are highlighted, highlight the first
  701. if ($highlighted.length === 0) {
  702. nextIndex = 0;
  703. }
  704. var $next = $options.eq(nextIndex);
  705. $next.trigger('mouseenter');
  706. var currentOffset = self.$results.offset().top;
  707. var nextTop = $next.offset().top;
  708. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  709. if (nextIndex === 0) {
  710. self.$results.scrollTop(0);
  711. } else if (nextTop - currentOffset < 0) {
  712. self.$results.scrollTop(nextOffset);
  713. }
  714. });
  715. container.on('results:next', function () {
  716. var $highlighted = self.$results.find('.highlighted');
  717. var $options = self.$results.find('[aria-selected]');
  718. var currentIndex = $options.index($highlighted);
  719. var nextIndex = currentIndex + 1;
  720. // If we are at the last option, stay there
  721. if (nextIndex >= $options.length) {
  722. return;
  723. }
  724. var $next = $options.eq(nextIndex);
  725. $next.trigger('mouseenter');
  726. var currentOffset = self.$results.offset().top +
  727. self.$results.outerHeight(false);
  728. var nextBottom = $next.offset().top + $next.outerHeight(false);
  729. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  730. if (nextIndex === 0) {
  731. self.$results.scrollTop(0);
  732. } else if (nextBottom > currentOffset) {
  733. self.$results.scrollTop(nextOffset);
  734. }
  735. });
  736. container.on('results:focus', function (params) {
  737. params.element.addClass('highlighted');
  738. });
  739. container.on('results:message', function (params) {
  740. self.trigger('results:message', params);
  741. });
  742. this.on('results:message', function (params) {
  743. self.displayMessage(params);
  744. });
  745. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  746. var $this = $(this);
  747. var data = $this.data('data');
  748. if ($this.attr('aria-selected') === 'true') {
  749. self.trigger('unselected', {
  750. originalEvent: evt,
  751. data: data
  752. });
  753. return;
  754. }
  755. self.trigger('selected', {
  756. originalEvent: evt,
  757. data: data
  758. });
  759. });
  760. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  761. var data = $(this).data('data');
  762. self.$results.find('.option.highlighted').removeClass('highlighted');
  763. self.trigger('results:focus', {
  764. data: data,
  765. element: $(this)
  766. });
  767. });
  768. };
  769. Results.prototype.destroy = function () {
  770. this.$results.remove();
  771. };
  772. Results.prototype.ensureHighlightVisible = function () {
  773. var $highlighted = this.$results.find('.highlighted');
  774. if ($highlighted.length === 0) {
  775. return;
  776. }
  777. var $options = this.$results.find('[aria-selected]');
  778. var currentIndex = $options.index($highlighted);
  779. var currentOffset = this.$results.offset().top;
  780. var nextTop = $highlighted.offset().top;
  781. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  782. var offsetDelta = nextTop - currentOffset;
  783. nextOffset -= $highlighted.outerHeight(false) * 2;
  784. if (currentIndex <= 2) {
  785. this.$results.scrollTop(0);
  786. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  787. this.$results.scrollTop(nextOffset);
  788. }
  789. };
  790. Results.prototype.template = function (result, container) {
  791. var template = this.options.get('templateResult');
  792. container.innerHTML = template(result);
  793. };
  794. return Results;
  795. });
  796. define('select2/keys',[
  797. ], function () {
  798. var KEYS = {
  799. BACKSPACE: 8,
  800. TAB: 9,
  801. ENTER: 13,
  802. SHIFT: 16,
  803. CTRL: 17,
  804. ALT: 18,
  805. ESC: 27,
  806. SPACE: 32,
  807. PAGE_UP: 33,
  808. PAGE_DOWN: 34,
  809. END: 35,
  810. HOME: 36,
  811. LEFT: 37,
  812. UP: 38,
  813. RIGHT: 39,
  814. DOWN: 40,
  815. DELETE: 46,
  816. isArrow: function (k) {
  817. k = k.which ? k.which : k;
  818. switch (k) {
  819. case KEY.LEFT:
  820. case KEY.RIGHT:
  821. case KEY.UP:
  822. case KEY.DOWN:
  823. return true;
  824. }
  825. return false;
  826. }
  827. };
  828. return KEYS;
  829. });
  830. define('select2/selection/base',[
  831. '../utils',
  832. '../keys'
  833. ], function (Utils, KEYS) {
  834. function BaseSelection ($element, options) {
  835. this.$element = $element;
  836. this.options = options;
  837. BaseSelection.__super__.constructor.call(this);
  838. }
  839. Utils.Extend(BaseSelection, Utils.Observable);
  840. BaseSelection.prototype.render = function () {
  841. throw new Error('The `render` method must be defined in child classes.');
  842. };
  843. BaseSelection.prototype.bind = function (container, $container) {
  844. var self = this;
  845. var id = container.id + '-container';
  846. var resultsId = container.id + '-results';
  847. this.$selection.attr('aria-owns', resultsId);
  848. this.$selection.on('keydown', function (evt) {
  849. self.trigger('keypress', evt);
  850. if (evt.which === KEYS.SPACE) {
  851. evt.preventDefault();
  852. }
  853. });
  854. container.on('results:focus', function (params) {
  855. self.$selection.attr('aria-activedescendant', params.data._resultId);
  856. });
  857. container.on('selection:update', function (params) {
  858. self.update(params.data);
  859. });
  860. container.on('open', function () {
  861. // When the dropdown is open, aria-expanded="true"
  862. self.$selection.attr('aria-expanded', 'true');
  863. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  864. var $target = $(e.target);
  865. var $select = $target.closest('.select2');
  866. var $all = $('.select2.open');
  867. $all.each(function () {
  868. var $this = $(this);
  869. if (this == $select[0]) {
  870. return;
  871. }
  872. var $element = $this.data('element');
  873. $element.select2('close');
  874. });
  875. });
  876. });
  877. container.on('close', function () {
  878. // When the dropdown is closed, aria-expanded="false"
  879. self.$selection.attr('aria-expanded', 'false');
  880. self.$selection.removeAttr('aria-activedescendant');
  881. self.$selection.focus();
  882. $(document.body).off('mousedown.select2.' + container.id);
  883. });
  884. };
  885. BaseSelection.prototype.destroy = function () {
  886. // Unbind the dropdown click handler if it exists
  887. $(document.body).off('.select2.' + container.id);
  888. };
  889. BaseSelection.prototype.update = function (data) {
  890. throw new Error('The `update` method must be defined in child classes.');
  891. };
  892. return BaseSelection;
  893. });
  894. define('select2/selection/single',[
  895. './base',
  896. '../utils',
  897. '../keys'
  898. ], function (BaseSelection, Utils, KEYS) {
  899. function SingleSelection () {
  900. SingleSelection.__super__.constructor.apply(this, arguments);
  901. }
  902. Utils.Extend(SingleSelection, BaseSelection);
  903. SingleSelection.prototype.render = function () {
  904. var $selection = $(
  905. '<span class="single-select" tabindex="0" role="combobox" ' +
  906. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  907. '<span class="rendered-selection"></span>' +
  908. '</span>'
  909. );
  910. $selection.attr('title', this.$element.attr('title'));
  911. this.$selection = $selection;
  912. return $selection;
  913. };
  914. SingleSelection.prototype.bind = function (container, $container) {
  915. var self = this;
  916. SingleSelection.__super__.bind.apply(this, arguments);
  917. var id = container.id + '-container';
  918. this.$selection.find('.rendered-selection').attr('id', id);
  919. this.$selection.attr('aria-labelledby', id);
  920. this.$selection.on('mousedown', function (evt) {
  921. // Only respond to left clicks
  922. if (evt.which !== 1) {
  923. return;
  924. }
  925. self.trigger('toggle', {
  926. originalEvent: evt
  927. });
  928. });
  929. this.$selection.on('focus', function (evt) {
  930. // User focuses on the container
  931. });
  932. this.$selection.on('blur', function (evt) {
  933. // User exits the container
  934. });
  935. container.on('selection:update', function (params) {
  936. self.update(params.data);
  937. });
  938. };
  939. SingleSelection.prototype.clear = function () {
  940. this.$selection.find('.rendered-selection').empty();
  941. };
  942. SingleSelection.prototype.display = function (data) {
  943. var template = this.options.get('templateSelection');
  944. return template(data);
  945. };
  946. SingleSelection.prototype.selectionContainer = function () {
  947. return $('<span></span>');
  948. };
  949. SingleSelection.prototype.update = function (data) {
  950. if (data.length === 0) {
  951. this.clear();
  952. return;
  953. }
  954. var selection = data[0];
  955. var formatted = this.display(selection);
  956. this.$selection.find('.rendered-selection').html(formatted);
  957. };
  958. return SingleSelection;
  959. });
  960. define('select2/selection/multiple',[
  961. './base',
  962. '../utils'
  963. ], function (BaseSelection, Utils) {
  964. function MultipleSelection ($element, options) {
  965. MultipleSelection.__super__.constructor.apply(this, arguments);
  966. }
  967. Utils.Extend(MultipleSelection, BaseSelection);
  968. MultipleSelection.prototype.render = function () {
  969. var $selection = $(
  970. '<span class="multiple-select" tabindex="0" role="combobox" ' +
  971. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  972. '<ul class="rendered-selection"></ul>' +
  973. '</span>'
  974. );
  975. $selection.attr('title', this.$element.attr('title'));
  976. this.$selection = $selection;
  977. return $selection;
  978. };
  979. MultipleSelection.prototype.bind = function (container, $container) {
  980. var self = this;
  981. MultipleSelection.__super__.bind.apply(this, arguments);
  982. this.$selection.on('click', function (evt) {
  983. self.trigger('toggle', {
  984. originalEvent: evt
  985. });
  986. });
  987. this.$selection.on('click', '.remove', function (evt) {
  988. var $remove = $(this);
  989. var $selection = $remove.parent();
  990. var data = $selection.data('data');
  991. self.trigger('unselected', {
  992. originalEvent: evt,
  993. data: data
  994. });
  995. });
  996. };
  997. MultipleSelection.prototype.clear = function () {
  998. this.$selection.find('.rendered-selection').empty();
  999. };
  1000. MultipleSelection.prototype.display = function (data) {
  1001. var template = this.options.get('templateSelection');
  1002. return template(data);
  1003. };
  1004. MultipleSelection.prototype.selectionContainer = function () {
  1005. var $container = $(
  1006. '<li class="choice">' +
  1007. '<span class="remove" role="presentation">&times;</span>' +
  1008. '</li>'
  1009. );
  1010. return $container;
  1011. };
  1012. MultipleSelection.prototype.update = function (data) {
  1013. this.clear();
  1014. if (data.length === 0) {
  1015. return;
  1016. }
  1017. var $selections = [];
  1018. for (var d = 0; d < data.length; d++) {
  1019. var selection = data[d];
  1020. var formatted = this.display(selection);
  1021. var $selection = this.selectionContainer();
  1022. $selection.append(formatted);
  1023. $selection.data('data', selection);
  1024. $selections.push($selection);
  1025. }
  1026. this.$selection.find('.rendered-selection').append($selections);
  1027. };
  1028. return MultipleSelection;
  1029. });
  1030. define('select2/selection/placeholder',[
  1031. '../utils'
  1032. ], function (Utils) {
  1033. function Placeholder (decorated, $element, options) {
  1034. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1035. decorated.call(this, $element, options);
  1036. }
  1037. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1038. if (typeof placeholder === 'string') {
  1039. placeholder = {
  1040. id: '',
  1041. text: placeholder
  1042. };
  1043. }
  1044. return placeholder;
  1045. };
  1046. Placeholder.prototype.update = function (decorated, data) {
  1047. var singlePlaceholder = (
  1048. data.length == 1 && data[0].id != this.placeholder.id
  1049. );
  1050. var multipleSelections = data.length > 1;
  1051. if (multipleSelections || singlePlaceholder) {
  1052. return decorated.call(this, data);
  1053. }
  1054. this.clear();
  1055. var $placeholder = this.selectionContainer();
  1056. $placeholder.html(this.display(this.placeholder));
  1057. $placeholder.addClass('placeholder').removeClass('choice');
  1058. this.$selection.find('.rendered-selection').append($placeholder);
  1059. };
  1060. return Placeholder;
  1061. });
  1062. define('select2/translation',[
  1063. ], function () {
  1064. function Translation (dict) {
  1065. this.dict = dict || {};
  1066. }
  1067. Translation.prototype.all = function () {
  1068. return this.dict;
  1069. };
  1070. Translation.prototype.get = function (key) {
  1071. return this.dict[key];
  1072. };
  1073. Translation.prototype.extend = function (translation) {
  1074. this.dict = $.extend({}, translation.all(), this.dict);
  1075. };
  1076. // Static functions
  1077. Translation._cache = {};
  1078. Translation.loadPath = function (path) {
  1079. if (!(path in Translation._cache)) {
  1080. var translations = require(path);
  1081. Translation._cache[path] = translations;
  1082. }
  1083. return new Translation(Translation._cache[path]);
  1084. };
  1085. return Translation;
  1086. });
  1087. define('select2/data/base',[
  1088. '../utils'
  1089. ], function (Utils) {
  1090. function BaseAdapter ($element, options) {
  1091. BaseAdapter.__super__.constructor.call(this);
  1092. }
  1093. Utils.Extend(BaseAdapter, Utils.Observable);
  1094. BaseAdapter.prototype.current = function (callback) {
  1095. throw new Error('The `current` method must be defined in child classes.');
  1096. };
  1097. BaseAdapter.prototype.query = function (params, callback) {
  1098. throw new Error('The `query` method must be defined in child classes.');
  1099. };
  1100. BaseAdapter.prototype.bind = function (container, $container) {
  1101. // Can be implemented in subclasses
  1102. };
  1103. BaseAdapter.prototype.destroy = function () {
  1104. // Can be implemented in subclasses
  1105. };
  1106. BaseAdapter.prototype.generateResultId = function (container, data) {
  1107. var id = container.id + '-result-';
  1108. id += Utils.generateChars(4);
  1109. if (data.id != null) {
  1110. id += '-' + data.id.toString();
  1111. } else {
  1112. id += '-' + Utils.generateChars(4);
  1113. }
  1114. return id;
  1115. };
  1116. return BaseAdapter;
  1117. });
  1118. define('select2/data/select',[
  1119. './base',
  1120. '../utils',
  1121. 'jquery'
  1122. ], function (BaseAdapter, Utils, $) {
  1123. function SelectAdapter ($element, options) {
  1124. this.$element = $element;
  1125. this.options = options;
  1126. SelectAdapter.__super__.constructor.call(this);
  1127. }
  1128. Utils.Extend(SelectAdapter, BaseAdapter);
  1129. SelectAdapter.prototype.current = function (callback) {
  1130. var data = [];
  1131. var self = this;
  1132. this.$element.find(':selected').each(function () {
  1133. var $option = $(this);
  1134. var option = self.item($option);
  1135. data.push(option);
  1136. });
  1137. callback(data);
  1138. };
  1139. SelectAdapter.prototype.select = function (data) {
  1140. var self = this;
  1141. if (this.$element.prop('multiple')) {
  1142. this.current(function (currentData) {
  1143. var val = [];
  1144. data = [data];
  1145. data.push.apply(data, currentData);
  1146. for (var d = 0; d < data.length; d++) {
  1147. id = data[d].id;
  1148. if (val.indexOf(id) === -1) {
  1149. val.push(id);
  1150. }
  1151. }
  1152. self.$element.val(val);
  1153. self.$element.trigger('change');
  1154. });
  1155. } else {
  1156. var val = data.id;
  1157. this.$element.val(val);
  1158. this.$element.trigger('change');
  1159. }
  1160. };
  1161. SelectAdapter.prototype.unselect = function (data) {
  1162. var self = this;
  1163. if (!this.$element.prop('multiple')) {
  1164. return;
  1165. }
  1166. this.current(function (currentData) {
  1167. var val = [];
  1168. for (var d = 0; d < currentData.length; d++) {
  1169. id = currentData[d].id;
  1170. if (id !== data.id && val.indexOf(id) === -1) {
  1171. val.push(id);
  1172. }
  1173. }
  1174. self.$element.val(val);
  1175. self.$element.trigger('change');
  1176. });
  1177. };
  1178. SelectAdapter.prototype.bind = function (container, $container) {
  1179. var self = this;
  1180. this.container = container;
  1181. container.on('select', function (params) {
  1182. self.select(params.data);
  1183. });
  1184. container.on('unselect', function (params) {
  1185. self.unselect(params.data);
  1186. });
  1187. };
  1188. SelectAdapter.prototype.destroy = function () {
  1189. // Remove anything added to child elements
  1190. this.$element.find('*').each(function () {
  1191. // Remove any custom data set by Select2
  1192. $.removeData(this, 'data');
  1193. });
  1194. };
  1195. SelectAdapter.prototype.query = function (params, callback) {
  1196. var data = [];
  1197. var self = this;
  1198. var $options = this.$element.children();
  1199. $options.each(function () {
  1200. var $option = $(this);
  1201. if (!$option.is('option') && !$option.is('optgroup')) {
  1202. return;
  1203. }
  1204. var option = self.item($option);
  1205. var matches = self.matches(params, option);
  1206. if (matches !== null) {
  1207. data.push(matches);
  1208. }
  1209. });
  1210. callback(data);
  1211. };
  1212. SelectAdapter.prototype.option = function (data) {
  1213. var option = document.createElement('option');
  1214. option.value = data.id;
  1215. if (data.disabled) {
  1216. option.disabled = true;
  1217. }
  1218. if (data.selected) {
  1219. option.selected = true;
  1220. }
  1221. option.innerText = data.text;
  1222. var $option = $(option);
  1223. var normalizedData = this._normalizeItem(data);
  1224. // Override the option's data with the combined data
  1225. $.data(option, 'data', normalizedData);
  1226. return $option;
  1227. };
  1228. SelectAdapter.prototype.item = function ($option) {
  1229. var data = {};
  1230. data = $.data($option[0], 'data');
  1231. if (data != null) {
  1232. return data;
  1233. }
  1234. if ($option.is('option')) {
  1235. data = {
  1236. id: $option.val(),
  1237. text: $option.html(),
  1238. disabled: $option.prop('disabled'),
  1239. selected: $option.prop('selected')
  1240. };
  1241. } else if ($option.is('optgroup')) {
  1242. data = {
  1243. text: $option.attr('label'),
  1244. children: []
  1245. };
  1246. var $children = $option.children('option');
  1247. var children = [];
  1248. for (var c = 0; c < $children.length; c++) {
  1249. var $child = $($children[c]);
  1250. var child = this.item($child);
  1251. children.push(child);
  1252. }
  1253. data.children = children;
  1254. }
  1255. data = this._normalizeItem(data);
  1256. $.data($option[0], 'data', data);
  1257. return data;
  1258. };
  1259. SelectAdapter.prototype._normalizeItem = function (item) {
  1260. if (item.text == null) {
  1261. item = {
  1262. id: item,
  1263. text: item
  1264. };
  1265. }
  1266. var defaults = {
  1267. selected: false,
  1268. disabled: false
  1269. };
  1270. if (item.id != null) {
  1271. item.id = item.id.toString();
  1272. }
  1273. if (item.text != null) {
  1274. item.text = item.text.toString();
  1275. }
  1276. if (item._resultId == null && item.id && this.container != null) {
  1277. item._resultId = this.generateResultId(this.container, item);
  1278. }
  1279. return $.extend({}, defaults, item);
  1280. };
  1281. SelectAdapter.prototype.matches = function (params, data) {
  1282. var matcher = this.options.get('matcher');
  1283. return matcher(params, data);
  1284. };
  1285. return SelectAdapter;
  1286. });
  1287. define('select2/data/array',[
  1288. './select',
  1289. '../utils',
  1290. 'jquery'
  1291. ], function (SelectAdapter, Utils, $) {
  1292. function ArrayAdapter ($element, options) {
  1293. var data = options.get('data');
  1294. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1295. this.convertToOptions(data);
  1296. }
  1297. Utils.Extend(ArrayAdapter, SelectAdapter);
  1298. ArrayAdapter.prototype.select = function (data) {
  1299. var $option = this.$element.find('option[value="' + data.id + '"]');
  1300. if ($option.length === 0) {
  1301. $option = this.option(data);
  1302. this.$element.append($option);
  1303. }
  1304. ArrayAdapter.__super__.select.call(this, data);
  1305. };
  1306. ArrayAdapter.prototype.convertToOptions = function (data) {
  1307. var self = this;
  1308. var $existing = this.$element.find('option');
  1309. var existingIds = $existing.map(function () {
  1310. return self.item($(this)).id;
  1311. }).get();
  1312. // Filter out all items except for the one passed in the argument
  1313. function onlyItem (item) {
  1314. return function () {
  1315. return $(this).val() == item.id;
  1316. };
  1317. }
  1318. for (var d = 0; d < data.length; d++) {
  1319. var item = data[d];
  1320. item.id = item.id.toString();
  1321. // Skip items which were pre-loaded, only merge the data
  1322. if (existingIds.indexOf(item.id) >= 0) {
  1323. var $existingOption = $existing.filter(onlyItem(item));
  1324. var existingData = this.item($existingOption);
  1325. var newData = $.extend(true, {}, existingData, item);
  1326. var $newOption = this.option(existingData);
  1327. $existingOption.replaceWith($newOption);
  1328. continue;
  1329. }
  1330. var $option = this.option(item);
  1331. this.$element.append($option);
  1332. }
  1333. };
  1334. return ArrayAdapter;
  1335. });
  1336. define('select2/data/ajax',[
  1337. './array',
  1338. '../utils',
  1339. 'jquery'
  1340. ], function (ArrayAdapter, Utils, $) {
  1341. function AjaxAdapter ($element, options) {
  1342. this.ajaxOptions = options.get('ajax');
  1343. if (this.ajaxOptions.processResults != null) {
  1344. this.processResults = this.ajaxOptions.processResults;
  1345. }
  1346. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1347. }
  1348. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1349. AjaxAdapter.prototype.processResults = function (results) {
  1350. return results;
  1351. };
  1352. AjaxAdapter.prototype.query = function (params, callback) {
  1353. var matches = [];
  1354. var self = this;
  1355. if (this._request) {
  1356. this._request.abort();
  1357. this._request = null;
  1358. }
  1359. var options = $.extend({
  1360. type: 'GET'
  1361. }, this.ajaxOptions);
  1362. if (typeof options.url === 'function') {
  1363. options.url = options.url(params);
  1364. }
  1365. if (typeof options.data === 'function') {
  1366. options.data = options.data(params);
  1367. }
  1368. function request () {
  1369. var $request = $.ajax(options);
  1370. $request.success(function (data) {
  1371. var results = self.processResults(data);
  1372. callback(results);
  1373. });
  1374. self._request = $request;
  1375. }
  1376. if (this.ajaxOptions.delay && params.term !== '') {
  1377. if (this._queryTimeout) {
  1378. window.clearTimeout(this._queryTimeout);
  1379. }
  1380. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  1381. } else {
  1382. request();
  1383. }
  1384. };
  1385. return AjaxAdapter;
  1386. });
  1387. define('select2/data/tags',[
  1388. ], function () {
  1389. function Tags (decorated, $element, options) {
  1390. var tags = options.get('tags');
  1391. decorated.call(this, $element, options);
  1392. if ($.isArray(tags)) {
  1393. for (var t = 0; t < tags.length; t++) {
  1394. var tag = tags[t];
  1395. var item = this._normalizeItem(tag);
  1396. var $option = this.option(item);
  1397. this.$element.append($option);
  1398. }
  1399. }
  1400. }
  1401. Tags.prototype.query = function (decorated, params, callback) {
  1402. var self = this;
  1403. this._removeOldTags();
  1404. if (params.term == null || params.term === '' || params.page != null) {
  1405. decorated.call(this, params, callback);
  1406. return;
  1407. }
  1408. function wrapper (data, child) {
  1409. for (var i = 0; i < data.length; i++) {
  1410. var option = data[i];
  1411. var checkChildren = (
  1412. option.children != null && !wrapper(option.children, true)
  1413. );
  1414. var checkText = option.text === params.term;
  1415. if (checkText || checkChildren) {
  1416. if (child) {
  1417. return false;
  1418. }
  1419. callback(data);
  1420. return;
  1421. }
  1422. }
  1423. if (child) {
  1424. return true;
  1425. }
  1426. var tag = self.createTag(params);
  1427. var $option = self.option(tag);
  1428. $option.attr('data-select2-tag', true);
  1429. self.$element.append($option);
  1430. self.insertTag(data, tag);
  1431. callback(data);
  1432. }
  1433. decorated.call(this, params, wrapper);
  1434. };
  1435. Tags.prototype.createTag = function (decorated, params) {
  1436. return {
  1437. id: params.term,
  1438. text: params.term
  1439. };
  1440. };
  1441. Tags.prototype.insertTag = function (_, data, tag) {
  1442. data.unshift(tag);
  1443. };
  1444. Tags.prototype._removeOldTags = function (_) {
  1445. var tag = this._lastTag;
  1446. var $options = this.$element.find('option[data-select2-tag]');
  1447. $options.each(function () {
  1448. if (this.selected) {
  1449. return;
  1450. }
  1451. $(this).remove();
  1452. });
  1453. };
  1454. return Tags;
  1455. });
  1456. define('select2/data/minimumInputLength',[
  1457. ], function () {
  1458. function MinimumInputLength (decorated, $e, options) {
  1459. this.minimumInputLength = options.get('minimumInputLength');
  1460. decorated.call(this, $e, options);
  1461. }
  1462. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  1463. params.term = params.term || '';
  1464. if (params.term.length < this.minimumInputLength) {
  1465. this.trigger('results:message', {
  1466. message: 'inputTooShort',
  1467. args: {
  1468. minimum: this.minimumInputLength,
  1469. input: params.term,
  1470. params: params
  1471. }
  1472. });
  1473. return;
  1474. }
  1475. decorated.call(this, params, callback);
  1476. };
  1477. return MinimumInputLength;
  1478. });
  1479. define('select2/dropdown',[
  1480. './utils'
  1481. ], function (Utils) {
  1482. function Dropdown ($element, options) {
  1483. this.$element = $element;
  1484. }
  1485. Utils.Extend(Dropdown, Utils.Observable);
  1486. Dropdown.prototype.render = function () {
  1487. var $dropdown = $(
  1488. '<span class="dropdown">' +
  1489. '<span class="results"></span>' +
  1490. '</span>'
  1491. );
  1492. this.$dropdown = $dropdown;
  1493. return $dropdown;
  1494. };
  1495. Dropdown.prototype.destroy = function () {
  1496. // Remove the dropdown from the DOM
  1497. this.$dropdown.remove();
  1498. };
  1499. Dropdown.prototype.bind = function (container, $container) {
  1500. // Can be implemented in subclasses
  1501. };
  1502. return Dropdown;
  1503. });
  1504. define('select2/dropdown/search',[
  1505. '../utils'
  1506. ], function (Utils) {
  1507. function Search () { }
  1508. Search.prototype.render = function (decorated) {
  1509. var $rendered = decorated.call(this);
  1510. var $search = $(
  1511. '<span class="search">' +
  1512. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1513. '</span>'
  1514. );
  1515. this.$searchContainer = $search;
  1516. this.$search = $search.find('input');
  1517. $rendered.prepend($search);
  1518. return $rendered;
  1519. };
  1520. Search.prototype.bind = function (decorated, container, $container) {
  1521. var self = this;
  1522. decorated.call(this, container, $container);
  1523. this.$search.on('keydown', function (evt) {
  1524. self.trigger('keypress', evt);
  1525. self._keyUpPrevented = evt.isDefaultPrevented();
  1526. });
  1527. this.$search.on('keyup', function (evt) {
  1528. self.handleSearch(evt);
  1529. });
  1530. container.on('open', function () {
  1531. self.$search.attr('tabindex', 0);
  1532. self.$search.focus();
  1533. });
  1534. container.on('close', function () {
  1535. self.$search.attr('tabindex', -1);
  1536. self.$search.val('');
  1537. });
  1538. container.on('results:all', function (params) {
  1539. if (params.query.term == null || params.query.term === '') {
  1540. var showSearch = self.showSearch(params);
  1541. if (showSearch) {
  1542. self.$searchContainer.show();
  1543. } else {
  1544. self.$searchContainer.hide();
  1545. }
  1546. }
  1547. });
  1548. };
  1549. Search.prototype.handleSearch = function (evt) {
  1550. if (!this._keyUpPrevented) {
  1551. var input = this.$search.val();
  1552. this.trigger('query', {
  1553. term: input
  1554. });
  1555. }
  1556. this._keyUpPrevented = false;
  1557. };
  1558. Search.prototype.showSearch = function (_, params) {
  1559. return true;
  1560. };
  1561. return Search;
  1562. });
  1563. define('select2/dropdown/hidePlaceholder',[
  1564. ], function () {
  1565. function HidePlaceholder (decorated, $element, options, dataAdapter) {
  1566. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1567. decorated.call(this, $element, options, dataAdapter);
  1568. }
  1569. HidePlaceholder.prototype.append = function (decorated, data) {
  1570. data = this.removePlaceholder(data);
  1571. decorated.call(this, data);
  1572. };
  1573. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1574. if (typeof placeholder === 'string') {
  1575. placeholder = {
  1576. id: '',
  1577. text: placeholder
  1578. };
  1579. }
  1580. return placeholder;
  1581. };
  1582. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  1583. var modifiedData = data.slice(0);
  1584. for (var d = data.length - 1; d >= 0; d--) {
  1585. var item = data[d];
  1586. if (this.placeholder.id === item.id) {
  1587. modifiedData.splice(d, 1);
  1588. }
  1589. }
  1590. return modifiedData;
  1591. };
  1592. return HidePlaceholder;
  1593. });
  1594. define('select2/dropdown/infiniteScroll',[
  1595. 'jquery'
  1596. ], function ($) {
  1597. function InfiniteScroll (decorated, $element, options, dataAdapter) {
  1598. this.lastParams = {};
  1599. decorated.call(this, $element, options, dataAdapter);
  1600. this.$loadingMore = this.createLoadingMore();
  1601. this.loading = false;
  1602. }
  1603. InfiniteScroll.prototype.append = function (decorated, data) {
  1604. this.$loadingMore.remove();
  1605. decorated.call(this, data);
  1606. if (data.length > 0) {
  1607. this.$results.append(this.$loadingMore);
  1608. }
  1609. this.loading = false;
  1610. };
  1611. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  1612. var self = this;
  1613. decorated.call(this, container, $container);
  1614. container.on('query', function (params) {
  1615. self.lastParams = params;
  1616. self.loading = true;
  1617. });
  1618. container.on('query:append', function (params) {
  1619. self.lastParams = params;
  1620. self.loading = true;
  1621. });
  1622. this.$results.on('scroll', function () {
  1623. var loadMoreVisible = $.contains(
  1624. document.documentElement,
  1625. self.$loadingMore[0]
  1626. );
  1627. if (self.loading || !loadMoreVisible) {
  1628. return;
  1629. }
  1630. var currentOffset = self.$results.offset().top +
  1631. self.$results.outerHeight(false);
  1632. var loadingMoreOffset = self.$loadingMore.offset().top +
  1633. self.$loadingMore.outerHeight(false);
  1634. if (currentOffset + 50 >= loadingMoreOffset) {
  1635. self.loadMore();
  1636. }
  1637. });
  1638. };
  1639. InfiniteScroll.prototype.loadMore = function () {
  1640. this.loading = true;
  1641. var params = $.extend({}, {page: 1}, this.lastParams);
  1642. params.page++;
  1643. this.trigger('query:append', params);
  1644. };
  1645. InfiniteScroll.prototype.createLoadingMore = function () {
  1646. var $option = $(
  1647. '<li class="option load-more" role="treeitem"></li>'
  1648. );
  1649. var message = this.options.get('translations').get('loadingMore');
  1650. $option.html(message(this.lastParams));
  1651. return $option;
  1652. };
  1653. return InfiniteScroll;
  1654. });
  1655. define('select2/i18n/en',[],function () {
  1656. return {
  1657. errorLoading: function () {
  1658. return 'The results could not be loaded.';
  1659. },
  1660. inputTooLong: function (args) {
  1661. var overChars = args.input.length - args.maximum;
  1662. var message = 'Please delete ' + overChars + ' character';
  1663. if (overChars != 1) {
  1664. message += 's';
  1665. }
  1666. return message;
  1667. },
  1668. inputTooShort: function (args) {
  1669. var remainingChars = args.minimum - args.input.length;
  1670. var message = 'Please enter ' + remainingChars + ' or more character';
  1671. if (remainingChars != 1) {
  1672. message += 's';
  1673. }
  1674. return message;
  1675. },
  1676. searching: function () {
  1677. return 'Searching…';
  1678. },
  1679. loadingMore: function () {
  1680. return 'Loading more results…';
  1681. },
  1682. maximumSelected: function (args) {
  1683. var message = 'You can only select' + args.maximum + ' item';
  1684. if (args.maximum != 1) {
  1685. message += 's';
  1686. }
  1687. return message;
  1688. },
  1689. noResults: function () {
  1690. return 'No results found';
  1691. }
  1692. };
  1693. });
  1694. define('select2/defaults',[
  1695. 'jquery',
  1696. './results',
  1697. './selection/single',
  1698. './selection/multiple',
  1699. './selection/placeholder',
  1700. './utils',
  1701. './translation',
  1702. './data/select',
  1703. './data/array',
  1704. './data/ajax',
  1705. './data/tags',
  1706. './data/minimumInputLength',
  1707. './dropdown',
  1708. './dropdown/search',
  1709. './dropdown/hidePlaceholder',
  1710. './dropdown/infiniteScroll',
  1711. './i18n/en'
  1712. ], function ($, ResultsList,
  1713. SingleSelection, MultipleSelection, Placeholder,
  1714. Utils, Translation,
  1715. SelectData, ArrayData, AjaxData, Tags, MinimumInputLength,
  1716. Dropdown, Search, HidePlaceholder, InfiniteScroll,
  1717. EnglishTranslation) {
  1718. function Defaults () {
  1719. this.reset();
  1720. }
  1721. Defaults.prototype.apply = function (options) {
  1722. options = $.extend({}, this.defaults, options);
  1723. if (options.dataAdapter == null) {
  1724. if (options.ajax != null) {
  1725. options.dataAdapter = AjaxData;
  1726. } else if (options.data != null) {
  1727. options.dataAdapter = ArrayData;
  1728. } else {
  1729. options.dataAdapter = SelectData;
  1730. }
  1731. }
  1732. if (options.minimumInputLength > 0) {
  1733. options.dataAdapter = Utils.Decorate(
  1734. options.dataAdapter,
  1735. MinimumInputLength
  1736. );
  1737. }
  1738. if (options.tags != null) {
  1739. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  1740. }
  1741. if (options.resultsAdapter == null) {
  1742. options.resultsAdapter = ResultsList;
  1743. if (options.ajax != null) {
  1744. options.resultsAdapter = Utils.Decorate(
  1745. options.resultsAdapter,
  1746. InfiniteScroll
  1747. );
  1748. }
  1749. if (options.placeholder != null) {
  1750. options.resultsAdapter = Utils.Decorate(
  1751. options.resultsAdapter,
  1752. HidePlaceholder
  1753. );
  1754. }
  1755. }
  1756. if (options.dropdownAdapter == null) {
  1757. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1758. options.dropdownAdapter = SearchableDropdown;
  1759. }
  1760. if (options.selectionAdapter == null) {
  1761. if (options.multiple) {
  1762. options.selectionAdapter = MultipleSelection;
  1763. } else {
  1764. options.selectionAdapter = SingleSelection;
  1765. }
  1766. // Add the placeholder mixin if a placeholder was specified
  1767. if (options.placeholder != null) {
  1768. options.selectionAdapter = Utils.Decorate(
  1769. options.selectionAdapter,
  1770. Placeholder
  1771. );
  1772. }
  1773. }
  1774. if (typeof options.language === 'string') {
  1775. options.language = [options.language];
  1776. }
  1777. if ($.isArray(options.language)) {
  1778. var languages = new Translation();
  1779. var languageNames = options.language.concat(this.defaults.language);
  1780. for (var l = 0; l < languageNames.length; l++) {
  1781. var name = languageNames[l];
  1782. var language = {};
  1783. try {
  1784. // Try to load it with the original name
  1785. language = Translation.loadPath(name);
  1786. } catch (e) {
  1787. // If we couldn't load it, check if it wasn't the full path
  1788. name = 'select2/i18n/' + name;
  1789. language = Translation.loadPath(name);
  1790. }
  1791. languages.extend(language);
  1792. }
  1793. options.translations = languages;
  1794. } else {
  1795. options.translations = new Translations(options.language);
  1796. }
  1797. return options;
  1798. };
  1799. Defaults.prototype.reset = function () {
  1800. function matcher (params, data) {
  1801. // Always return the object if there is nothing to compare
  1802. if ($.trim(params.term) === '') {
  1803. return data;
  1804. }
  1805. // Do a recursive check for options with children
  1806. if (data.children && data.children.length > 0) {
  1807. // Clone the data object if there are children
  1808. // This is required as we modify the object to remove any non-matches
  1809. var match = $.extend(true, {}, data);
  1810. // Check each child of the option
  1811. for (var c = data.children.length - 1; c >= 0; c--) {
  1812. var child = data.children[c];
  1813. var matches = matcher(params, child);
  1814. // If there wasn't a match, remove the object in the array
  1815. if (matches == null) {
  1816. match.children.splice(c, 1);
  1817. }
  1818. }
  1819. // If any children matched, return the new object
  1820. if (match.children.length > 0) {
  1821. return match;
  1822. }
  1823. // If there were no matching children, check just the plain object
  1824. return matcher(params, match);
  1825. }
  1826. // Check if the text contains the term
  1827. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1828. return data;
  1829. }
  1830. // If it doesn't contain the term, don't return anything
  1831. return null;
  1832. }
  1833. this.defaults = {
  1834. language: ['select2/i18n/en'],
  1835. matcher: matcher,
  1836. minimumInputLength: 0,
  1837. theme: 'default',
  1838. templateResult: function (result) {
  1839. return result.text;
  1840. },
  1841. templateSelection: function (selection) {
  1842. return selection.text;
  1843. }
  1844. };
  1845. };
  1846. var defaults = new Defaults();
  1847. return defaults;
  1848. });
  1849. define('select2/options',[
  1850. './defaults'
  1851. ], function (Defaults) {
  1852. function Options (options, $element) {
  1853. this.options = options;
  1854. if ($element != null) {
  1855. this.fromElement($element);
  1856. }
  1857. this.options = Defaults.apply(this.options);
  1858. }
  1859. Options.prototype.fromElement = function ($e) {
  1860. if (this.options.multiple == null) {
  1861. this.options.multiple = $e.prop('multiple');
  1862. }
  1863. return this;
  1864. };
  1865. Options.prototype.get = function (key) {
  1866. return this.options[key];
  1867. };
  1868. Options.prototype.set = function (key, val) {
  1869. this.options[key] = val;
  1870. };
  1871. return Options;
  1872. });
  1873. define('select2/core',[
  1874. 'jquery',
  1875. './options',
  1876. './utils',
  1877. './keys'
  1878. ], function ($, Options, Utils, KEYS) {
  1879. var Select2 = function ($element, options) {
  1880. if ($element.data('select2') != null) {
  1881. return;
  1882. }
  1883. this.$element = $element;
  1884. this.id = this._generateId($element);
  1885. options = options || {};
  1886. this.options = new Options(options, $element);
  1887. Select2.__super__.constructor.call(this);
  1888. // Set up containers and adapters
  1889. var DataAdapter = this.options.get('dataAdapter');
  1890. this.data = new DataAdapter($element, this.options);
  1891. var $container = this.render();
  1892. this._placeContainer($container);
  1893. var SelectionAdapter = this.options.get('selectionAdapter');
  1894. this.selection = new SelectionAdapter($element, this.options);
  1895. var $selection = this.selection.render();
  1896. this._placeSelection($selection);
  1897. var DropdownAdapter = this.options.get('dropdownAdapter');
  1898. this.dropdown = new DropdownAdapter($element, this.options);
  1899. var $dropdown = this.dropdown.render();
  1900. this._placeDropdown($dropdown);
  1901. var ResultsAdapter = this.options.get('resultsAdapter');
  1902. this.results = new ResultsAdapter($element, this.options, this.data);
  1903. var $results = this.results.render();
  1904. this._placeResults($results);
  1905. // Bind events
  1906. var self = this;
  1907. // Bind the container to all of the adapters
  1908. this._bindAdapters();
  1909. // Register any DOM event handlers
  1910. this._registerDomEvents();
  1911. // Register any internal event handlers
  1912. this._registerDataEvents();
  1913. this._registerSelectionEvents();
  1914. this._registerDropdownEvents();
  1915. this._registerResultsEvents();
  1916. this._registerEvents();
  1917. // Set the initial state
  1918. this.data.current(function (initialData) {
  1919. self.trigger('selection:update', {
  1920. data: initialData
  1921. });
  1922. });
  1923. // Hide the original select
  1924. $element.hide();
  1925. this._tabindex = $element.attr('tabindex') || 0;
  1926. $element.attr('tabindex', '-1');
  1927. $element.data('select2', this);
  1928. };
  1929. Utils.Extend(Select2, Utils.Observable);
  1930. Select2.prototype._generateId = function ($element) {
  1931. var id = '';
  1932. if ($element.attr('id') != null) {
  1933. id = $element.attr('id');
  1934. } else if ($element.attr('name') != null) {
  1935. id = $element.attr('name') + '-' + Utils.generateChars(2);
  1936. } else {
  1937. id = Utils.generateChars(4);
  1938. }
  1939. id = 'select2-' + id;
  1940. return id;
  1941. };
  1942. Select2.prototype._placeContainer = function ($container) {
  1943. $container.insertAfter(this.$element);
  1944. $container.width(this.$element.outerWidth(false));
  1945. };
  1946. Select2.prototype._placeSelection = function ($selection) {
  1947. var $selectionContainer = this.$container.find('.selection');
  1948. $selectionContainer.append($selection);
  1949. };
  1950. Select2.prototype._placeDropdown = function ($dropdown) {
  1951. this.$dropdown = $dropdown;
  1952. var $dropdownContainer = this.$container.find('.dropdown-wrapper');
  1953. $dropdownContainer.append($dropdown);
  1954. };
  1955. Select2.prototype._placeResults = function ($results) {
  1956. var $resultsContainer = this.$dropdown.find('.results');
  1957. $resultsContainer.append($results);
  1958. };
  1959. Select2.prototype._bindAdapters = function () {
  1960. this.data.bind(this, this.$container);
  1961. this.selection.bind(this, this.$container);
  1962. this.dropdown.bind(this, this.$container);
  1963. this.results.bind(this, this.$container);
  1964. };
  1965. Select2.prototype._registerDomEvents = function () {
  1966. var self = this;
  1967. this.$element.on('change.select2', function () {
  1968. self.data.current(function (data) {
  1969. self.trigger('selection:update', {
  1970. data: data
  1971. });
  1972. });
  1973. });
  1974. };
  1975. Select2.prototype._registerDataEvents = function () {
  1976. var self = this;
  1977. this.data.on('results:message', function (params) {
  1978. self.trigger('results:message', params);
  1979. });
  1980. };
  1981. Select2.prototype._registerSelectionEvents = function () {
  1982. var self = this;
  1983. this.selection.on('open', function () {
  1984. self.open();
  1985. });
  1986. this.selection.on('close', function () {
  1987. self.close();
  1988. });
  1989. this.selection.on('toggle', function () {
  1990. self.toggleDropdown();
  1991. });
  1992. this.selection.on('results:select', function () {
  1993. self.trigger('results:select');
  1994. });
  1995. this.selection.on('results:previous', function () {
  1996. self.trigger('results:previous');
  1997. });
  1998. this.selection.on('results:next', function () {
  1999. self.trigger('results:next');
  2000. });
  2001. this.selection.on('unselected', function (params) {
  2002. self.trigger('unselect', params);
  2003. self.close();
  2004. });
  2005. this.selection.on('keypress', function (e) {
  2006. self.trigger('keypress', e);
  2007. });
  2008. };
  2009. Select2.prototype._registerDropdownEvents = function () {
  2010. var self = this;
  2011. this.dropdown.on('query', function (params) {
  2012. self.trigger('query', params);
  2013. });
  2014. this.dropdown.on('keypress', function (e) {
  2015. self.trigger('keypress', e);
  2016. });
  2017. };
  2018. Select2.prototype._registerResultsEvents = function () {
  2019. var self = this;
  2020. this.results.on('query:append', function (params) {
  2021. self.trigger('query:append', params);
  2022. });
  2023. this.results.on('selected', function (params) {
  2024. self.trigger('select', params);
  2025. self.close();
  2026. });
  2027. this.results.on('unselected', function (params) {
  2028. self.trigger('unselect', params);
  2029. self.close();
  2030. });
  2031. this.results.on('results:focus', function (params) {
  2032. self.trigger('results:focus', params);
  2033. });
  2034. };
  2035. Select2.prototype._registerEvents = function () {
  2036. var self = this;
  2037. this.on('open', function () {
  2038. self.$container.addClass('open');
  2039. });
  2040. this.on('close', function () {
  2041. self.$container.removeClass('open');
  2042. });
  2043. this.on('query', function (params) {
  2044. this.data.query(params, function (data) {
  2045. self.trigger('results:all', {
  2046. data: data,
  2047. query: params
  2048. });
  2049. });
  2050. });
  2051. this.on('query:append', function (params) {
  2052. this.data.query(params, function (data) {
  2053. self.trigger('results:append', {
  2054. data: data,
  2055. query: params
  2056. });
  2057. });
  2058. });
  2059. this.on('keypress', function (evt) {
  2060. var key = evt.which;
  2061. if (self.isOpen()) {
  2062. if (key === KEYS.ENTER) {
  2063. self.trigger('results:select');
  2064. evt.preventDefault();
  2065. } else if (key === KEYS.UP) {
  2066. self.trigger('results:previous');
  2067. evt.preventDefault();
  2068. } else if (key === KEYS.DOWN) {
  2069. self.trigger('results:next');
  2070. evt.preventDefault();
  2071. } else if (key === KEYS.ESC || key === KEYS.TAB) {
  2072. self.close();
  2073. evt.preventDefault();
  2074. }
  2075. } else {
  2076. if (key === KEYS.ENTER || key === KEYS.SPACE) {
  2077. self.open();
  2078. evt.preventDefault();
  2079. }
  2080. }
  2081. });
  2082. };
  2083. Select2.prototype.toggleDropdown = function () {
  2084. if (this.isOpen()) {
  2085. this.close();
  2086. } else {
  2087. this.open();
  2088. }
  2089. };
  2090. Select2.prototype.open = function () {
  2091. if (this.isOpen()) {
  2092. return;
  2093. }
  2094. this.trigger('query', {});
  2095. this.trigger('open');
  2096. };
  2097. Select2.prototype.close = function () {
  2098. if (!this.isOpen()) {
  2099. return;
  2100. }
  2101. this.trigger('close');
  2102. };
  2103. Select2.prototype.isOpen = function () {
  2104. return this.$container.hasClass('open');
  2105. };
  2106. Select2.prototype.destroy = function () {
  2107. this.$container.remove();
  2108. this.$element.off('.select2');
  2109. this.$element.attr('tabindex', this._tabindex);
  2110. this.$element.show();
  2111. this.$element.removeData('select2');
  2112. this.data.destroy();
  2113. this.selection.destroy();
  2114. this.dropdown.destroy();
  2115. this.results.destroy();
  2116. this.data = null;
  2117. this.selection = null;
  2118. this.dropdown = null;
  2119. this.results = null;
  2120. };
  2121. Select2.prototype.render = function () {
  2122. var $container = $(
  2123. '<span class="select2 select2-container">' +
  2124. '<span class="selection"></span>' +
  2125. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  2126. '</span>'
  2127. );
  2128. this.$container = $container;
  2129. this.$container.addClass('select2-theme-' + this.options.get('theme'));
  2130. $container.data('element', this.$element);
  2131. return $container;
  2132. };
  2133. return Select2;
  2134. });
  2135. define('jquery.select2',[
  2136. 'jquery',
  2137. 'select2/core'
  2138. ], function ($, Select2) {
  2139. if ($.fn.select2 == null) {
  2140. $.fn.select2 = function (options) {
  2141. options = options || {};
  2142. if (typeof options === 'object') {
  2143. this.each(function () {
  2144. var instance = new Select2($(this), options);
  2145. });
  2146. } else if (typeof options === 'string') {
  2147. var instance = this.data('select2');
  2148. var args = Array.prototype.slice.call(arguments, 1);
  2149. instance[options](args);
  2150. } else {
  2151. throw new Error('Invalid arguments for Select2: ' + options);
  2152. }
  2153. };
  2154. }
  2155. return Select2;
  2156. });
  2157. require('jquery.select2'); $.fn.select2.amd = { define: define, require: require };}());