select2.js 68 KB

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