select2.js 48 KB

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