select2.js 46 KB

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