select2.js 49 KB

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