select2.js 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911
  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. return Utils;
  475. });
  476. define('select2/results',[
  477. './utils'
  478. ], function (Utils) {
  479. function Results ($element, options, dataAdapter) {
  480. this.$element = $element;
  481. this.data = dataAdapter;
  482. Results.__super__.constructor.call(this);
  483. }
  484. Utils.Extend(Results, Utils.Observable);
  485. Results.prototype.render = function () {
  486. var $results = $(
  487. '<ul class="options" role="listbox"></ul>'
  488. );
  489. this.$results = $results;
  490. return $results;
  491. };
  492. Results.prototype.clear = function () {
  493. this.$results.empty();
  494. };
  495. Results.prototype.append = function (data) {
  496. var $options = [];
  497. data = this.sort(data);
  498. for (var d = 0; d < data.length; d++) {
  499. var item = data[d];
  500. var $option = this.option(item);
  501. $options.push($option);
  502. }
  503. this.$results.append($options);
  504. };
  505. Results.prototype.sort = function (data) {
  506. return data;
  507. };
  508. Results.prototype.setClasses = function () {
  509. var self = this;
  510. this.data.current(function (selected) {
  511. var selectedIds = $.map(selected, function (s) {
  512. return s.id.toString();
  513. });
  514. var $options = self.$results.find('.option[aria-selected]');
  515. $options.each(function () {
  516. var $option = $(this);
  517. var item = $option.data('data');
  518. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  519. $option.attr('aria-selected', 'true');
  520. } else {
  521. $option.attr('aria-selected', 'false');
  522. }
  523. });
  524. var $selected = $options.filter('[aria-selected=true]');
  525. // Check if there are any selected options
  526. if ($selected.length > 0) {
  527. // If there are selected options, highlight the first
  528. $selected.first().trigger('mouseenter');
  529. } else {
  530. // If there are no selected options, highlight the first option
  531. // in the dropdown
  532. $options.first().trigger('mouseenter');
  533. }
  534. });
  535. };
  536. Results.prototype.option = function (data) {
  537. var $option = $(
  538. '<li class="option" role="option" aria-selected="false"></li>'
  539. );
  540. if (data.children) {
  541. $option
  542. .addClass('group')
  543. .removeAttr('aria-selected');
  544. var $label = $('<strong class="group-label"></strong>');
  545. $label.html(data.text);
  546. var $children = [];
  547. for (var c = 0; c < data.children.length; c++) {
  548. var child = data.children[c];
  549. var $child = this.option(child);
  550. $children.push($child);
  551. }
  552. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  553. $childrenContainer.append($children);
  554. $option.append($label);
  555. $option.append($childrenContainer);
  556. } else {
  557. $option.html(data.text);
  558. }
  559. if (data.disabled) {
  560. $option
  561. .removeAttr('aria-selected')
  562. .attr('aria-disabled', 'true');
  563. }
  564. if (data.id == null) {
  565. $option.removeAttr('aria-selected');
  566. }
  567. if (data._resultId != null) {
  568. $option.attr('id', data._resultId);
  569. }
  570. $option.data('data', data);
  571. return $option;
  572. };
  573. Results.prototype.bind = function (container, $container) {
  574. var self = this;
  575. container.on('results:all', function (params) {
  576. self.clear();
  577. self.append(params.data);
  578. self.setClasses();
  579. });
  580. container.on('results:append', function (params) {
  581. self.append(params.data);
  582. self.setClasses();
  583. });
  584. container.on('select', function () {
  585. self.setClasses();
  586. });
  587. container.on('unselect', function () {
  588. self.setClasses();
  589. });
  590. container.on('open', function () {
  591. // When the dropdown is open, aria-expended="true"
  592. self.$results.attr('aria-expanded', 'true');
  593. self.setClasses();
  594. });
  595. container.on('close', function () {
  596. // When the dropdown is closed, aria-expended="false"
  597. self.$results.attr('aria-expanded', 'false');
  598. });
  599. container.on('results:select', function () {
  600. var $highlighted = self.$results.find('.highlighted');
  601. if ($highlighted.length === 0) {
  602. return;
  603. }
  604. var data = $highlighted.data('data');
  605. if ($highlighted.attr('aria-selected') == 'true') {
  606. self.trigger('unselected', {
  607. data: data
  608. });
  609. } else {
  610. self.trigger('selected', {
  611. data: data
  612. });
  613. }
  614. });
  615. container.on('results:previous', function () {
  616. var $highlighted = self.$results.find('.highlighted');
  617. var $options = self.$results.find('[aria-selected]');
  618. var currentIndex = $options.index($highlighted);
  619. // If we are already at te top, don't move further
  620. if (currentIndex === 0) {
  621. return;
  622. }
  623. var nextIndex = currentIndex - 1;
  624. // If none are highlighted, highlight the first
  625. if ($highlighted.length === 0) {
  626. nextIndex = 0;
  627. }
  628. var $next = $options.eq(nextIndex);
  629. $next.trigger('mouseenter');
  630. var currentOffset = self.$results.offset().top;
  631. var nextTop = $next.offset().top;
  632. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  633. if (nextIndex === 0) {
  634. self.$results.scrollTop(0);
  635. } else if (nextTop - currentOffset < 0) {
  636. self.$results.scrollTop(nextOffset);
  637. }
  638. });
  639. container.on('results:next', function () {
  640. var $highlighted = self.$results.find('.highlighted');
  641. var $options = self.$results.find('[aria-selected]');
  642. var currentIndex = $options.index($highlighted);
  643. var nextIndex = currentIndex + 1;
  644. // If we are at the last option, stay there
  645. if (nextIndex >= $options.length) {
  646. return;
  647. }
  648. var $next = $options.eq(nextIndex);
  649. $next.trigger('mouseenter');
  650. var currentOffset = self.$results.offset().top +
  651. self.$results.outerHeight(false);
  652. var nextBottom = $next.offset().top + $next.outerHeight(false);
  653. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  654. if (nextIndex === 0) {
  655. self.$results.scrollTop(0);
  656. } else if (nextBottom > currentOffset) {
  657. self.$results.scrollTop(nextOffset);
  658. }
  659. });
  660. this.$results.on('mouseup', '.option[aria-selected]', function (evt) {
  661. var $this = $(this);
  662. var data = $this.data('data');
  663. if ($this.attr('aria-selected') === 'true') {
  664. self.trigger('unselected', {
  665. originalEvent: evt,
  666. data: data
  667. });
  668. return;
  669. }
  670. self.trigger('selected', {
  671. originalEvent: evt,
  672. data: data
  673. });
  674. });
  675. this.$results.on('mouseenter', '.option[aria-selected]', function (evt) {
  676. self.$results.find('.option.highlighted').removeClass('highlighted');
  677. $(this).addClass('highlighted');
  678. });
  679. this.$results.on('mouseleave', '.option', function (evt) {
  680. $(this).removeClass('highlighted');
  681. });
  682. };
  683. return Results;
  684. });
  685. define('select2/selection/base',[
  686. '../utils'
  687. ], function (Utils) {
  688. function BaseSelection ($element, options) {
  689. this.$element = $element;
  690. this.options = options;
  691. BaseSelection.__super__.constructor.call(this);
  692. }
  693. Utils.Extend(BaseSelection, Utils.Observable);
  694. BaseSelection.prototype.render = function () {
  695. throw new Error('The `render` method must be defined in child classes.');
  696. };
  697. BaseSelection.prototype.bind = function (container, $container) {
  698. var self = this;
  699. container.on('selection:update', function (params) {
  700. self.update(params.data);
  701. });
  702. };
  703. BaseSelection.prototype.update = function (data) {
  704. throw new Error('The `update` method must be defined in child classes.');
  705. };
  706. return BaseSelection;
  707. });
  708. define('select2/keys',[
  709. ], function () {
  710. var KEYS = {
  711. BACKSPACE: 8,
  712. TAB: 9,
  713. ENTER: 13,
  714. SHIFT: 16,
  715. CTRL: 17,
  716. ALT: 18,
  717. ESC: 27,
  718. SPACE: 32,
  719. PAGE_UP: 33,
  720. PAGE_DOWN: 34,
  721. END: 35,
  722. HOME: 36,
  723. LEFT: 37,
  724. UP: 38,
  725. RIGHT: 39,
  726. DOWN: 40,
  727. DELETE: 46,
  728. isArrow: function (k) {
  729. k = k.which ? k.which : k;
  730. switch (k) {
  731. case KEY.LEFT:
  732. case KEY.RIGHT:
  733. case KEY.UP:
  734. case KEY.DOWN:
  735. return true;
  736. }
  737. return false;
  738. }
  739. };
  740. return KEYS;
  741. });
  742. define('select2/selection/single',[
  743. './base',
  744. '../utils',
  745. '../keys'
  746. ], function (BaseSelection, Utils, KEYS) {
  747. function SingleSelection () {
  748. SingleSelection.__super__.constructor.apply(this, arguments);
  749. }
  750. Utils.Extend(SingleSelection, BaseSelection);
  751. SingleSelection.prototype.render = function () {
  752. var $selection = $(
  753. '<span class="single-select" tabindex="0" role="combobox" ' +
  754. 'aria-autocomplete="list" aria-haspopup="true" aria-expanded="false">' +
  755. '<span class="rendered-selection"></span>' +
  756. '</span>'
  757. );
  758. $selection.attr('title', this.$element.attr('title'));
  759. var id = 'select2-container-';
  760. for (var i = 0; i < 4; i++) {
  761. var r = Math.floor(Math.random() * 16);
  762. id += r.toString(16);
  763. }
  764. $selection.find('.rendered-selection').attr('id', id);
  765. $selection.attr('aria-labelledby', id);
  766. this.$selection = $selection;
  767. return $selection;
  768. };
  769. SingleSelection.prototype.bind = function (container, $container) {
  770. var self = this;
  771. SingleSelection.__super__.bind.apply(this, arguments);
  772. this.$selection.on('mousedown', function (evt) {
  773. // Only respond to left clicks
  774. if (evt.which !== 1) {
  775. return;
  776. }
  777. self.trigger('toggle', {
  778. originalEvent: evt
  779. });
  780. });
  781. container.on('open', function () {
  782. // When the dropdown is open, aria-expended="true"
  783. self.$selection.attr('aria-expanded', 'true');
  784. });
  785. container.on('close', function () {
  786. // When the dropdown is closed, aria-expended="false"
  787. self.$selection.attr('aria-expanded', 'false');
  788. });
  789. this.$selection.on('focus', function (evt) {
  790. // User focuses on the container
  791. });
  792. this.$selection.on('blur', function (evt) {
  793. // User exits the container
  794. });
  795. this.$selection.on('keydown', function (evt) {
  796. var key = evt.which;
  797. if (container.isOpen()) {
  798. if (key == KEYS.ENTER) {
  799. self.trigger('results:select');
  800. } else if (key == KEYS.UP) {
  801. self.trigger('results:previous');
  802. evt.preventDefault();
  803. } else if (key == KEYS.DOWN) {
  804. self.trigger('results:next');
  805. evt.preventDefault();
  806. }
  807. } else {
  808. if (key == KEYS.ENTER || key == KEYS.SPACE) {
  809. self.trigger('open');
  810. }
  811. }
  812. });
  813. container.on('selection:update', function (params) {
  814. self.update(params.data);
  815. });
  816. };
  817. SingleSelection.prototype.clear = function () {
  818. this.$selection.find('.rendered-selection').empty();
  819. };
  820. SingleSelection.prototype.display = function (data) {
  821. return data.text;
  822. };
  823. SingleSelection.prototype.selectionContainer = function () {
  824. return $('<span></span>');
  825. };
  826. SingleSelection.prototype.update = function (data) {
  827. if (data.length === 0) {
  828. this.clear();
  829. return;
  830. }
  831. var selection = data[0];
  832. var formatted = this.display(selection);
  833. this.$selection.find('.rendered-selection').html(formatted);
  834. if (data[0]._resultId != null) {
  835. this.$selection.attr('aria-activedescendent', data[0]._resultId);
  836. }
  837. };
  838. return SingleSelection;
  839. });
  840. define('select2/selection/multiple',[
  841. './base',
  842. '../utils'
  843. ], function (BaseSelection, Utils) {
  844. function MultipleSelection ($element, options) {
  845. this.$element = $element;
  846. this.options = options;
  847. MultipleSelection.__super__.constructor.call(this);
  848. }
  849. Utils.Extend(MultipleSelection, BaseSelection);
  850. MultipleSelection.prototype.render = function () {
  851. var $selection = $(
  852. '<span class="multiple-select">' +
  853. '<ul class="rendered-selection"></ul>' +
  854. '</span>'
  855. );
  856. this.$selection = $selection;
  857. return $selection;
  858. };
  859. MultipleSelection.prototype.bind = function (container, $container) {
  860. var self = this;
  861. MultipleSelection.__super__.bind.apply(this, arguments);
  862. this.$selection.on('click', function (evt) {
  863. self.trigger('toggle', {
  864. originalEvent: evt
  865. });
  866. });
  867. this.$selection.on('click', '.remove', function (evt) {
  868. var $remove = $(this);
  869. var $selection = $remove.parent();
  870. var data = $selection.data('data');
  871. self.trigger('unselected', {
  872. originalEvent: evt,
  873. data: data
  874. });
  875. });
  876. };
  877. MultipleSelection.prototype.clear = function () {
  878. this.$selection.find('.rendered-selection').empty();
  879. };
  880. MultipleSelection.prototype.display = function (data) {
  881. return data.text;
  882. };
  883. MultipleSelection.prototype.selectionContainer = function () {
  884. var $container = $(
  885. '<li class="choice">' +
  886. '<span class="remove" role="presentation">&times;</span>' +
  887. '</li>'
  888. );
  889. return $container;
  890. };
  891. MultipleSelection.prototype.update = function (data) {
  892. this.clear();
  893. if (data.length === 0) {
  894. return;
  895. }
  896. var $selections = [];
  897. for (var d = 0; d < data.length; d++) {
  898. var selection = data[d];
  899. var formatted = this.display(selection);
  900. var $selection = this.selectionContainer();
  901. $selection.append(formatted);
  902. $selection.data('data', selection);
  903. $selections.push($selection);
  904. }
  905. this.$selection.find('.rendered-selection').append($selections);
  906. };
  907. return MultipleSelection;
  908. });
  909. define('select2/selection/placeholder',[
  910. '../utils'
  911. ], function (Utils) {
  912. function Placeholder (decorated, $element, options) {
  913. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  914. decorated.call(this, $element, options);
  915. }
  916. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  917. if (typeof placeholder === 'string') {
  918. placeholder = {
  919. id: '',
  920. text: placeholder
  921. };
  922. }
  923. return placeholder;
  924. };
  925. Placeholder.prototype.update = function (decorated, data) {
  926. var singlePlaceholder = (
  927. data.length == 1 && data[0].id != this.placeholder.id
  928. );
  929. var multipleSelections = data.length > 1;
  930. if (multipleSelections || singlePlaceholder) {
  931. return decorated.call(this, data);
  932. }
  933. this.clear();
  934. var $placeholder = this.selectionContainer();
  935. $placeholder.html(this.display(this.placeholder));
  936. $placeholder.addClass('placeholder').removeClass('choice');
  937. this.$selection.find('.rendered-selection').append($placeholder);
  938. };
  939. return Placeholder;
  940. });
  941. define('select2/data/base',[
  942. '../utils'
  943. ], function (Utils) {
  944. function BaseAdapter ($element, options) {
  945. BaseAdapter.__super__.constructor.call(this);
  946. }
  947. Utils.Extend(BaseAdapter, Utils.Observable);
  948. BaseAdapter.prototype.current = function (callback) {
  949. throw new Error('The `current` method must be defined in child classes.');
  950. };
  951. BaseAdapter.prototype.query = function (params, callback) {
  952. throw new Error('The `query` method must be defined in child classes.');
  953. };
  954. BaseAdapter.prototype.bind = function (container, $container) {
  955. // Can be implemented in subclasses
  956. };
  957. BaseAdapter.prototype.generateResultId = function (data) {
  958. var id = 'select2-result-';
  959. for (var i = 0; i < 4; i++) {
  960. var r = Math.floor(Math.random() * 16);
  961. id += r.toString(16);
  962. }
  963. if (data.id != null) {
  964. id += '-' + data.id.toString();
  965. } else {
  966. for (var s = 0; s < 4; s++) {
  967. var idChar = Math.floor(Math.random() * 16);
  968. id += idChar.toString(16);
  969. }
  970. }
  971. return id;
  972. };
  973. return BaseAdapter;
  974. });
  975. define('select2/data/select',[
  976. './base',
  977. '../utils',
  978. 'jquery'
  979. ], function (BaseAdapter, Utils, $) {
  980. function SelectAdapter ($element, options) {
  981. this.$element = $element;
  982. SelectAdapter.__super__.constructor.call(this);
  983. }
  984. Utils.Extend(SelectAdapter, BaseAdapter);
  985. SelectAdapter.prototype.current = function (callback) {
  986. var data = [];
  987. var self = this;
  988. this.$element.find(':selected').each(function () {
  989. var $option = $(this);
  990. var option = self.item($option);
  991. data.push(option);
  992. });
  993. callback(data);
  994. };
  995. SelectAdapter.prototype.select = function (data) {
  996. var self = this;
  997. if (this.$element.prop('multiple')) {
  998. this.current(function (currentData) {
  999. var val = [];
  1000. data = [data];
  1001. data.push.apply(data, currentData);
  1002. for (var d = 0; d < data.length; d++) {
  1003. id = data[d].id;
  1004. if (val.indexOf(id) === -1) {
  1005. val.push(id);
  1006. }
  1007. }
  1008. self.$element.val(val);
  1009. self.$element.trigger('change');
  1010. });
  1011. } else {
  1012. var val = data.id;
  1013. this.$element.val(val);
  1014. this.$element.trigger('change');
  1015. }
  1016. };
  1017. SelectAdapter.prototype.unselect = function (data) {
  1018. var self = this;
  1019. if (!this.$element.prop('multiple')) {
  1020. return;
  1021. }
  1022. this.current(function (currentData) {
  1023. var val = [];
  1024. for (var d = 0; d < currentData.length; d++) {
  1025. id = currentData[d].id;
  1026. if (id !== data.id && val.indexOf(id) === -1) {
  1027. val.push(id);
  1028. }
  1029. }
  1030. self.$element.val(val);
  1031. self.$element.trigger('change');
  1032. });
  1033. };
  1034. SelectAdapter.prototype.bind = function (container, $container) {
  1035. var self = this;
  1036. container.on('select', function (params) {
  1037. self.select(params.data);
  1038. });
  1039. container.on('unselect', function (params) {
  1040. self.unselect(params.data);
  1041. });
  1042. };
  1043. SelectAdapter.prototype.query = function (params, callback) {
  1044. var data = [];
  1045. var self = this;
  1046. var $options = this.$element.children();
  1047. $options.each(function () {
  1048. var $option = $(this);
  1049. if (!$option.is('option') && !$option.is('optgroup')) {
  1050. return;
  1051. }
  1052. var option = self.item($option);
  1053. var matches = self.matches(params, option);
  1054. if (matches !== null) {
  1055. data.push(matches);
  1056. }
  1057. });
  1058. callback(data);
  1059. };
  1060. SelectAdapter.prototype.item = function ($option) {
  1061. var data = $option.data('data');
  1062. // If the data has already be generated, use it
  1063. if (data == null) {
  1064. if ($option.is('option')) {
  1065. data = {
  1066. id: $option.val(),
  1067. text: $option.html(),
  1068. disabled: $option.prop('disabled')
  1069. };
  1070. } else if ($option.is('optgroup')) {
  1071. data = {
  1072. text: $option.attr('label'),
  1073. children: []
  1074. };
  1075. var $children = $option.children('option');
  1076. var children = [];
  1077. for (var c = 0; c < $children.length; c++) {
  1078. var $child = $($children[c]);
  1079. var child = this.item($child);
  1080. children.push(child);
  1081. }
  1082. data.children = children;
  1083. }
  1084. if (data.id) {
  1085. data._resultId = this.generateResultId(data);
  1086. }
  1087. $option.data('data', data);
  1088. }
  1089. return data;
  1090. };
  1091. SelectAdapter.prototype.matches = function (params, data) {
  1092. var match = $.extend(true, {}, data);
  1093. if (data.children) {
  1094. for (var c = data.children.length - 1; c >= 0; c--) {
  1095. var child = data.children[c];
  1096. var matches = this.matches(params, child);
  1097. // If there wasn't a match, remove the object in the array
  1098. if (matches === null) {
  1099. match.children.splice(c, 1);
  1100. }
  1101. }
  1102. if (match.children.length > 0) {
  1103. return match;
  1104. }
  1105. }
  1106. if ($.trim(params.term) === '') {
  1107. return match;
  1108. }
  1109. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  1110. return match;
  1111. }
  1112. return null;
  1113. };
  1114. return SelectAdapter;
  1115. });
  1116. define('select2/data/array',[
  1117. './select',
  1118. '../utils'
  1119. ], function (SelectAdapter, Utils) {
  1120. function ArrayAdapter ($element, options) {
  1121. this.data = options.options.data;
  1122. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1123. }
  1124. Utils.Extend(ArrayAdapter, SelectAdapter);
  1125. ArrayAdapter.prototype.select = function (data) {
  1126. var self = this;
  1127. this.$element.find('option').each(function () {
  1128. var $option = $(this);
  1129. var option = self.item($option);
  1130. if (option.id == data.id.toString()) {
  1131. $option.remove();
  1132. }
  1133. });
  1134. var $option = this.option(data);
  1135. this.$element.append($option);
  1136. ArrayAdapter.__super__.select.call(this, data);
  1137. };
  1138. ArrayAdapter.prototype.option = function (data) {
  1139. var $option = $('<option></option>');
  1140. $option.text(data.text);
  1141. $option.val(data.id);
  1142. $option.data('data', data);
  1143. return $option;
  1144. };
  1145. ArrayAdapter.prototype.query = function (params, callback) {
  1146. var matches = [];
  1147. var self = this;
  1148. $.each(this.data, function () {
  1149. var option = this;
  1150. if (self.matches(params, option)) {
  1151. matches.push(option);
  1152. }
  1153. });
  1154. callback(matches);
  1155. };
  1156. return ArrayAdapter;
  1157. });
  1158. define('select2/data/ajax',[
  1159. './array',
  1160. '../utils',
  1161. 'jquery'
  1162. ], function (ArrayAdapter, Utils, $) {
  1163. function AjaxAdapter ($element, options) {
  1164. this.ajaxOptions = options.get('ajax');
  1165. this.processResults = this.ajaxOptions.processResults ||
  1166. function (results) {
  1167. return results;
  1168. };
  1169. ArrayAdapter.__super__.constructor.call(this, $element, options);
  1170. }
  1171. Utils.Extend(AjaxAdapter, ArrayAdapter);
  1172. AjaxAdapter.prototype.query = function (params, callback) {
  1173. var matches = [];
  1174. var self = this;
  1175. var options = $.extend({
  1176. type: 'GET'
  1177. }, this.ajaxOptions);
  1178. if (typeof options.url === 'function') {
  1179. options.url = options.url(params);
  1180. }
  1181. if (typeof options.data === 'function') {
  1182. options.data = options.data(params);
  1183. }
  1184. var $request = $.ajax(options);
  1185. $request.success(function (data) {
  1186. var results = self.processResults(data);
  1187. callback(results);
  1188. });
  1189. };
  1190. return AjaxAdapter;
  1191. });
  1192. define('select2/dropdown',[
  1193. './utils'
  1194. ], function (Utils) {
  1195. function Dropdown ($element, options) {
  1196. this.$element = $element;
  1197. }
  1198. Utils.Extend(Dropdown, Utils.Observable);
  1199. Dropdown.prototype.render = function () {
  1200. var $dropdown = $(
  1201. '<span class="dropdown">' +
  1202. '<span class="results"></span>' +
  1203. '</span>'
  1204. );
  1205. return $dropdown;
  1206. };
  1207. Dropdown.prototype.bind = function (container, $container) {
  1208. // Can be implemented in subclasses
  1209. };
  1210. return Dropdown;
  1211. });
  1212. define('select2/dropdown/search',[
  1213. ], function () {
  1214. function Search () { }
  1215. Search.prototype.render = function (decorated) {
  1216. var $rendered = decorated.call(this);
  1217. var $search = $(
  1218. '<span class="search">' +
  1219. '<input type="search" name="search" tabindex="-1" role="textbox" />' +
  1220. '</span>'
  1221. );
  1222. this.$searchContainer = $search;
  1223. this.$search = $search.find('input');
  1224. $rendered.prepend($search);
  1225. return $rendered;
  1226. };
  1227. Search.prototype.bind = function (decorated, container, $container) {
  1228. var self = this;
  1229. decorated.call(this, container, $container);
  1230. this.$search.on('keyup', function () {
  1231. container.trigger('query', {
  1232. term: $(this).val()
  1233. });
  1234. });
  1235. container.on('open', function () {
  1236. self.$search.attr('tabindex', 0);
  1237. });
  1238. container.on('close', function () {
  1239. self.$search.attr('tabindex', -1);
  1240. });
  1241. container.on('results:all', function (params) {
  1242. if (params.query.term == null || params.query.term === '') {
  1243. var showSearch = self.showSearch(params);
  1244. if (showSearch) {
  1245. self.$searchContainer.show();
  1246. } else {
  1247. self.$searchContainer.hide();
  1248. }
  1249. }
  1250. });
  1251. };
  1252. Search.prototype.showSearch = function (params) {
  1253. return true;
  1254. };
  1255. return Search;
  1256. });
  1257. define('select2/defaults',[
  1258. './results',
  1259. './selection/single',
  1260. './selection/multiple',
  1261. './selection/placeholder',
  1262. './utils',
  1263. './data/select',
  1264. './data/array',
  1265. './data/ajax',
  1266. './dropdown',
  1267. './dropdown/search'
  1268. ], function (ResultsList,
  1269. SingleSelection, MultipleSelection, Placeholder,
  1270. Utils,
  1271. SelectData, ArrayData, AjaxData,
  1272. Dropdown, Search) {
  1273. function Defaults () {
  1274. this.reset();
  1275. }
  1276. Defaults.prototype.apply = function (options) {
  1277. options = $.extend({}, options, this.defaults);
  1278. if (options.dataAdapter == null) {
  1279. if (options.ajax) {
  1280. options.dataAdapter = AjaxData;
  1281. } else if (options.data) {
  1282. options.dataAdapter = ArrayData;
  1283. } else {
  1284. options.dataAdapter = SelectData;
  1285. }
  1286. }
  1287. if (options.resultsAdapter == null) {
  1288. options.resultsAdapter = ResultsList;
  1289. }
  1290. if (options.dropdownAdapter == null) {
  1291. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1292. options.dropdownAdapter = SearchableDropdown;
  1293. }
  1294. if (options.selectionAdapter == null) {
  1295. if (options.multiple) {
  1296. options.selectionAdapter = MultipleSelection;
  1297. } else {
  1298. options.selectionAdapter = SingleSelection;
  1299. }
  1300. // Add the placeholder mixin if a placeholder was specified
  1301. if (options.placeholder != null) {
  1302. options.selectionAdapter = Utils.Decorate(
  1303. options.selectionAdapter,
  1304. Placeholder
  1305. );
  1306. }
  1307. }
  1308. return options;
  1309. };
  1310. Defaults.prototype.reset = function () {
  1311. this.defaults = { };
  1312. };
  1313. var defaults = new Defaults();
  1314. return defaults;
  1315. });
  1316. define('select2/options',[
  1317. './defaults'
  1318. ], function (Defaults) {
  1319. function Options (options) {
  1320. this.options = Defaults.apply(options);
  1321. }
  1322. Options.prototype.fromElement = function ($e) {
  1323. return this;
  1324. };
  1325. Options.prototype.get = function (key) {
  1326. return this.options[key];
  1327. };
  1328. Options.prototype.set = function (key, val) {
  1329. this.options[key] = val;
  1330. };
  1331. return Options;
  1332. });
  1333. define('select2/core',[
  1334. 'jquery',
  1335. './options',
  1336. './utils'
  1337. ], function ($, Options, Utils) {
  1338. var Select2 = function ($element, options) {
  1339. this.$element = $element;
  1340. options = options || {};
  1341. options.multiple = options.multiple || $element.prop('multiple');
  1342. this.options = new Options(options);
  1343. Select2.__super__.constructor.call(this);
  1344. // Set up containers and adapters
  1345. var DataAdapter = this.options.get('dataAdapter');
  1346. this.data = new DataAdapter($element, this.options);
  1347. var $container = this.render();
  1348. this.$container = $container;
  1349. $container.insertAfter(this.$element);
  1350. $container.width($element.outerWidth(false));
  1351. var SelectionAdapter = this.options.get('selectionAdapter');
  1352. this.selection = new SelectionAdapter($element, this.options);
  1353. var $selectionContainer = $container.find('.selection');
  1354. var $selection = this.selection.render();
  1355. $selectionContainer.append($selection);
  1356. var DropdownAdapter = this.options.get('dropdownAdapter');
  1357. this.dropdown = new DropdownAdapter($element, this.options);
  1358. var $dropdownContainer = $container.find('.dropdown-wrapper');
  1359. var $dropdown = this.dropdown.render();
  1360. $dropdownContainer.append($dropdown);
  1361. var ResultsAdapter = this.options.get('resultsAdapter');
  1362. this.results = new ResultsAdapter($element, this.options, this.data);
  1363. var $resultsContainer = $dropdown.find('.results');
  1364. var $results = this.results.render();
  1365. $resultsContainer.append($results);
  1366. // Bind events
  1367. var self = this;
  1368. this.data.bind(this, $container);
  1369. this.selection.bind(this, $container);
  1370. this.dropdown.bind(this, $container);
  1371. this.results.bind(this, $container);
  1372. this.$element.on('change', function () {
  1373. self.data.current(function (data) {
  1374. self.trigger('selection:update', {
  1375. data: data
  1376. });
  1377. });
  1378. });
  1379. this.selection.on('open', function () {
  1380. self.trigger('open');
  1381. });
  1382. this.selection.on('close', function () {
  1383. self.trigger('close');
  1384. });
  1385. this.selection.on('toggle', function () {
  1386. self.toggleDropdown();
  1387. });
  1388. this.selection.on('results:select', function () {
  1389. self.trigger('results:select');
  1390. });
  1391. this.selection.on('results:previous', function () {
  1392. self.trigger('results:previous');
  1393. });
  1394. this.selection.on('results:next', function () {
  1395. self.trigger('results:next');
  1396. });
  1397. this.selection.on('unselected', function (params) {
  1398. self.trigger('unselect', params);
  1399. self.trigger('close');
  1400. });
  1401. this.results.on('selected', function (params) {
  1402. self.trigger('select', params);
  1403. self.trigger('close');
  1404. });
  1405. this.results.on('unselected', function (params) {
  1406. self.trigger('unselect', params);
  1407. self.trigger('close');
  1408. });
  1409. this.on('open', function () {
  1410. $container.addClass('open');
  1411. });
  1412. this.on('close', function () {
  1413. $container.removeClass('open');
  1414. });
  1415. // Set the initial state
  1416. this.data.current(function (initialData) {
  1417. self.trigger('selection:update', {
  1418. data: initialData
  1419. });
  1420. });
  1421. this.on('query', function (params) {
  1422. this.data.query(params, function (data) {
  1423. self.trigger('results:all', {
  1424. data: data,
  1425. query: params
  1426. });
  1427. });
  1428. });
  1429. this.trigger('query', {});
  1430. // Hide the original select
  1431. $element.hide();
  1432. $element.attr('tabindex', '-1');
  1433. };
  1434. Utils.Extend(Select2, Utils.Observable);
  1435. Select2.prototype.toggleDropdown = function () {
  1436. if (this.isOpen()) {
  1437. this.trigger('close');
  1438. } else {
  1439. this.trigger('open');
  1440. }
  1441. };
  1442. Select2.prototype.isOpen = function () {
  1443. return this.$container.hasClass('open');
  1444. };
  1445. Select2.prototype.render = function () {
  1446. var $container = $(
  1447. '<span class="select2 select2-container select2-theme-default">' +
  1448. '<span class="selection"></span>' +
  1449. '<span class="dropdown-wrapper"></span>' +
  1450. '</span>'
  1451. );
  1452. return $container;
  1453. };
  1454. return Select2;
  1455. });
  1456. define('jquery.select2',[
  1457. 'jquery',
  1458. 'select2/core'
  1459. ], function ($, Select2) {
  1460. if ($.fn.select2 == null) {
  1461. $.fn.select2 = function (options) {
  1462. options = options || {};
  1463. if (typeof options === 'object') {
  1464. this.each(function () {
  1465. var instance = new Select2($(this), options);
  1466. });
  1467. } else if (typeof options === 'string') {
  1468. var instance = this.data('select2');
  1469. instance[options](arguments.slice(1));
  1470. } else {
  1471. throw new Error('Invalid arguments for Select2: ' + options);
  1472. }
  1473. };
  1474. }
  1475. return Select2;
  1476. });
  1477. require('jquery.select2');