select2.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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"></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. for (var d = 0; d < data.length; d++) {
  498. var item = data[d];
  499. var $option = this.option(item);
  500. $options.push($option);
  501. }
  502. this.$results.append($options);
  503. };
  504. Results.prototype.setClasses = function () {
  505. var self = this;
  506. this.data.current(function (selected) {
  507. selected = $.map(selected, function (s) { return s.id; });
  508. self.$results.find('.option.selected').removeClass('selected');
  509. var $options = self.$results.find('.option');
  510. $options.each(function () {
  511. var $option = $(this);
  512. var item = $option.data('data');
  513. if (selected.indexOf(item.id.toString()) > -1) {
  514. $option.addClass('selected');
  515. }
  516. });
  517. });
  518. };
  519. Results.prototype.option = function (data) {
  520. var $option = $(
  521. '<li class="option highlightable selectable"></li>'
  522. );
  523. if (data.children && data.children.length > 0) {
  524. $option.addClass('group').removeClass('highlightable selectable');
  525. var $label = $('<strong class="group-label"></strong>');
  526. $label.html(data.text);
  527. var $children = [];
  528. for (var c = 0; c < data.children.length; c++) {
  529. var child = data.children[c];
  530. var $child = this.option(child);
  531. $children.push($child);
  532. }
  533. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  534. $childrenContainer.append($children);
  535. $option.append($label);
  536. $option.append($childrenContainer);
  537. } else {
  538. $option.html(data.text);
  539. }
  540. if (data.disabled) {
  541. $option.removeClass('selectable highlightable').addClass('disabled');
  542. }
  543. $option.data('data', data);
  544. return $option;
  545. };
  546. Results.prototype.bind = function (container, $container) {
  547. var self = this;
  548. this.on('results:all', function (data) {
  549. self.clear();
  550. self.append(data);
  551. self.setClasses();
  552. });
  553. this.on('results:append', function (data) {
  554. self.append(data);
  555. self.setClasses();
  556. });
  557. this.$results.on('mouseup', '.option.selectable', function (evt) {
  558. var $this = $(this);
  559. var data = $this.data('data');
  560. if ($this.hasClass('selected')) {
  561. self.trigger('unselected', {
  562. originalEvent: evt,
  563. data: data
  564. });
  565. self.setClasses();
  566. return;
  567. }
  568. self.trigger('selected', {
  569. originalEvent: evt,
  570. data: data
  571. });
  572. self.setClasses();
  573. });
  574. this.$results.on('mouseenter', '.option.highlightable', function (evt) {
  575. self.$results.find('.option.highlighted').removeClass('highlighted');
  576. $(this).addClass('highlighted');
  577. });
  578. this.$results.on('mouseleave', '.option', function (evt) {
  579. $(this).removeClass('highlighted');
  580. });
  581. };
  582. return Results;
  583. });
  584. define('select2/dropdown',[
  585. './utils'
  586. ], function (Utils) {
  587. function Dropdown ($element, options) {
  588. this.$element = $element;
  589. }
  590. Utils.Extend(Dropdown, Utils.Observable);
  591. Dropdown.prototype.render = function () {
  592. var $dropdown = $(
  593. '<span class="dropdown">' +
  594. '<span class="results"></span>' +
  595. '</span>'
  596. );
  597. return $dropdown;
  598. };
  599. return Dropdown;
  600. });
  601. define('select2/selection/single',[
  602. '../utils'
  603. ], function (Utils) {
  604. function SingleSelection ($element, options) {
  605. this.$element = $element;
  606. this.options = options;
  607. SingleSelection.__super__.constructor.call(this);
  608. }
  609. Utils.Extend(SingleSelection, Utils.Observable);
  610. SingleSelection.prototype.render = function () {
  611. var $selection = $(
  612. '<span class="single-select">' +
  613. '<span class="rendered-selection"></span>' +
  614. '</span>'
  615. );
  616. this.$selection = $selection;
  617. return $selection;
  618. };
  619. SingleSelection.prototype.bind = function (container, $container) {
  620. var self = this;
  621. this.$selection.on('mousedown', function (evt) {
  622. // Only respond to left clicks
  623. if (evt.which !== 1) {
  624. return;
  625. }
  626. self.trigger('toggle', {
  627. originalEvent: evt
  628. });
  629. });
  630. container.on('selection:update', function (params) {
  631. self.update(params.data);
  632. });
  633. };
  634. SingleSelection.prototype.clear = function () {
  635. this.$selection.find('.rendered-selection').empty();
  636. };
  637. SingleSelection.prototype.display = function (data) {
  638. return data.text;
  639. };
  640. SingleSelection.prototype.update = function (data) {
  641. if (data.length === 0) {
  642. this.clear();
  643. return;
  644. }
  645. var selection = data[0];
  646. var formatted = this.display(selection);
  647. this.$selection.find('.rendered-selection').html(formatted);
  648. };
  649. return SingleSelection;
  650. });
  651. define('select2/selection/multiple',[
  652. '../utils'
  653. ], function (Utils) {
  654. function MultipleSelection ($element, options) {
  655. this.$element = $element;
  656. this.options = options;
  657. MultipleSelection.__super__.constructor.call(this);
  658. }
  659. Utils.Extend(MultipleSelection, Utils.Observable);
  660. MultipleSelection.prototype.render = function () {
  661. var $selection = $(
  662. '<span class="multiple-select">' +
  663. '<ul class="rendered-selection"></ul>' +
  664. '</span>'
  665. );
  666. this.$selection = $selection;
  667. return $selection;
  668. };
  669. MultipleSelection.prototype.bind = function (container, $container) {
  670. var self = this;
  671. this.$selection.on('click', function (evt) {
  672. self.trigger('toggle', {
  673. originalEvent: evt
  674. });
  675. });
  676. container.on('selection:update', function (params) {
  677. self.update(params.data);
  678. });
  679. };
  680. MultipleSelection.prototype.clear = function () {
  681. this.$selection.find('.rendered-selection').empty();
  682. };
  683. MultipleSelection.prototype.display = function (data) {
  684. return data.text;
  685. };
  686. MultipleSelection.prototype.update = function (data) {
  687. this.clear();
  688. if (data.length === 0) {
  689. return;
  690. }
  691. var $selections = [];
  692. for (var d = 0; d < data.length; d++) {
  693. var selection = data[d];
  694. var formatted = this.display(selection);
  695. var $selection = $('<ul class="choice"></ul>');
  696. $selection.text(formatted);
  697. $selection.data('data', data);
  698. $selections.push($selection);
  699. }
  700. this.$selection.find('.rendered-selection').append($selections);
  701. };
  702. return MultipleSelection;
  703. });
  704. define('select2/data/base',[
  705. '../utils'
  706. ], function (Utils) {
  707. function BaseAdapter ($element, options) {
  708. BaseAdapter.__super__.constructor.call(this);
  709. }
  710. Utils.Extend(BaseAdapter, Utils.Observable);
  711. BaseAdapter.prototype.current = function (callback) {
  712. throw new Error('The `current` method must be defined in child classes.');
  713. };
  714. BaseAdapter.prototype.query = function (params, callback) {
  715. throw new Error('The `query` method must be defined in child classes.');
  716. };
  717. return BaseAdapter;
  718. });
  719. define('select2/data/select',[
  720. './base',
  721. '../utils',
  722. 'jquery'
  723. ], function (BaseAdapter, Utils, $) {
  724. function SelectAdapter ($element, options) {
  725. this.$element = $element;
  726. SelectAdapter.__super__.constructor.call(this);
  727. }
  728. Utils.Extend(SelectAdapter, BaseAdapter);
  729. SelectAdapter.prototype.current = function (callback) {
  730. var data = [];
  731. var self = this;
  732. this.$element.find(':selected').each(function () {
  733. var $option = $(this);
  734. var option = self.item($option);
  735. data.push(option);
  736. });
  737. callback(data);
  738. };
  739. SelectAdapter.prototype.select = function (data) {
  740. var self = this;
  741. if (this.$element.prop('multiple')) {
  742. this.current(function (currentData) {
  743. var val = [];
  744. data = [data];
  745. data.push.apply(data, currentData);
  746. for (var d = 0; d < data.length; d++) {
  747. id = data[d].id;
  748. if (val.indexOf(id) === -1) {
  749. val.push(id);
  750. }
  751. }
  752. self.$element.val(val);
  753. self.$element.trigger('change');
  754. });
  755. } else {
  756. var val = data.id;
  757. this.$element.val(val);
  758. this.$element.trigger('change');
  759. }
  760. };
  761. SelectAdapter.prototype.unselect = function (data) {
  762. var self = this;
  763. if (!this.$element.prop('multiple')) {
  764. return;
  765. }
  766. this.current(function (currentData) {
  767. var val = [];
  768. for (var d = 0; d < currentData.length; d++) {
  769. id = currentData[d].id;
  770. if (id !== data.id && val.indexOf(id) === -1) {
  771. val.push(id);
  772. }
  773. }
  774. self.$element.val(val);
  775. self.$element.trigger('change');
  776. });
  777. };
  778. SelectAdapter.prototype.bind = function (container, $container) {
  779. var self = this;
  780. container.on('select', function (params) {
  781. self.select(params.data);
  782. });
  783. container.on('unselect', function (params) {
  784. self.unselect(params.data);
  785. });
  786. };
  787. SelectAdapter.prototype.query = function (params, callback) {
  788. var data = [];
  789. var self = this;
  790. var $options = this.$element.children();
  791. $options.each(function () {
  792. var $option = $(this);
  793. if (!$option.is('option') && !$option.is('optgroup')) {
  794. return;
  795. }
  796. var option = self.item($option);
  797. var matches = self.matches(params, option);
  798. if (matches !== null) {
  799. data.push(matches);
  800. }
  801. });
  802. callback(data);
  803. };
  804. SelectAdapter.prototype.item = function ($option) {
  805. var data = $option.data('data');
  806. // If the data has already be generated, use it
  807. if (data == null) {
  808. if ($option.is('option')) {
  809. data = {
  810. id: $option.val(),
  811. text: $option.html(),
  812. disabled: $option.prop('disabled')
  813. };
  814. } else if ($option.is('optgroup')) {
  815. data = {
  816. id: -1,
  817. text: $option.attr('label'),
  818. children: []
  819. };
  820. var $children = $option.children('option');
  821. var children = [];
  822. for (var c = 0; c < $children.length; c++) {
  823. var $child = $($children[c]);
  824. var child = this.item($child);
  825. children.push(child);
  826. }
  827. data.children = children;
  828. }
  829. $option.data('data', data);
  830. }
  831. return data;
  832. };
  833. SelectAdapter.prototype.matches = function (params, data) {
  834. var match = $.extend(true, {}, data);
  835. if (data.children) {
  836. for (var c = data.children.length - 1; c >= 0; c--) {
  837. var child = data.children[c];
  838. var matches = this.matches(params, child);
  839. // If there wasn't a match, remove the object in the array
  840. if (matches === null) {
  841. match.children.splice(c, 1);
  842. }
  843. }
  844. if (match.children.length > 0) {
  845. return match;
  846. }
  847. }
  848. if ($.trim(params.term) === '') {
  849. return match;
  850. }
  851. if (data.text.indexOf(params.term) > -1) {
  852. return match;
  853. }
  854. return null;
  855. };
  856. return SelectAdapter;
  857. });
  858. define('select2/data/array',[
  859. './select',
  860. '../utils'
  861. ], function (SelectAdapter, Utils) {
  862. function ArrayAdapter ($element, options) {
  863. this.data = options.options.data;
  864. ArrayAdapter.__super__.constructor.call(this, $element, options);
  865. }
  866. Utils.Extend(ArrayAdapter, SelectAdapter);
  867. ArrayAdapter.prototype.select = function (data) {
  868. var self = this;
  869. this.$element.find('option').each(function () {
  870. var $option = $(this);
  871. var option = self.item($option);
  872. if (option.id == data.id.toString()) {
  873. $option.remove();
  874. }
  875. });
  876. var $option = this.option(data);
  877. this.$element.append($option);
  878. ArrayAdapter.__super__.select.call(this, data);
  879. };
  880. ArrayAdapter.prototype.option = function (data) {
  881. var $option = $('<option></option>');
  882. $option.text(data.text);
  883. $option.val(data.id);
  884. $option.data('data', data);
  885. return $option;
  886. };
  887. ArrayAdapter.prototype.query = function (params, callback) {
  888. var matches = [];
  889. var self = this;
  890. $.each(this.data, function () {
  891. var option = this;
  892. if (self.matches(params, option)) {
  893. matches.push(option);
  894. }
  895. });
  896. callback(matches);
  897. };
  898. return ArrayAdapter;
  899. });
  900. define('select2/data/ajax',[
  901. './array',
  902. '../utils',
  903. 'jquery'
  904. ], function (ArrayAdapter, Utils, $) {
  905. function AjaxAdapter ($element, options) {
  906. this.ajaxOptions = options.options.ajax;
  907. this.processResults = this.ajaxOptions.processResults ||
  908. function (results) {
  909. return results;
  910. };
  911. ArrayAdapter.__super__.constructor.call(this, $element, options);
  912. }
  913. Utils.Extend(AjaxAdapter, ArrayAdapter);
  914. AjaxAdapter.prototype.query = function (params, callback) {
  915. var matches = [];
  916. var self = this;
  917. var options = $.extend({
  918. type: 'GET'
  919. }, this.ajaxOptions);
  920. if (typeof options.url === 'function') {
  921. options.url = options.url(params);
  922. }
  923. if (typeof options.data === 'function') {
  924. options.data = options.data(params);
  925. }
  926. var $request = $.ajax(options);
  927. $request.success(function (data) {
  928. var results = self.processResults(data);
  929. callback(results);
  930. });
  931. };
  932. return AjaxAdapter;
  933. });
  934. define('select2/options',[
  935. './results',
  936. './dropdown',
  937. './selection/single',
  938. './selection/multiple',
  939. './data/select',
  940. './data/array',
  941. './data/ajax'
  942. ], function (ResultsList, Dropdown, SingleSelection, MultipleSelection,
  943. SelectData, ArrayData, AjaxData) {
  944. function Options (options) {
  945. this.options = options;
  946. if (options.ajax) {
  947. this.dataAdapter = this.dataAdapter || AjaxData;
  948. } else if (options.data) {
  949. this.dataAdapter = this.dataAdapter || ArrayData;
  950. } else {
  951. this.dataAdapter = this.dataAdapter || SelectData;
  952. }
  953. this.resultsAdapter = ResultsList;
  954. this.dropdownAdapter = options.dropdownAdapter || Dropdown;
  955. this.selectionAdapter = options.selectionAdapter;
  956. if (this.selectionAdapter == null) {
  957. if (this.options.multiple) {
  958. this.selectionAdapter = MultipleSelection;
  959. } else {
  960. this.selectionAdapter = SingleSelection;
  961. }
  962. }
  963. }
  964. return Options;
  965. });
  966. define('select2/core',[
  967. 'jquery',
  968. './options',
  969. './utils'
  970. ], function ($, Options, Utils) {
  971. var Select2 = function ($element, options) {
  972. this.$element = $element;
  973. options = options || {};
  974. options.multiple = options.multiple || $element.prop('multiple');
  975. this.options = new Options(options);
  976. Select2.__super__.constructor.call(this);
  977. // Set up containers and adapters
  978. this.data = new this.options.dataAdapter($element, this.options);
  979. var $container = this.render();
  980. this.$container = $container;
  981. $container.insertAfter(this.$element);
  982. $container.width($element.width());
  983. this.selection = new this.options.selectionAdapter($element, this.options);
  984. var $selectionContainer = $container.find('.selection');
  985. var $selection = this.selection.render();
  986. $selectionContainer.append($selection);
  987. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  988. var $dropdownContainer = $container.find('.dropdown-wrapper');
  989. var $dropdown = this.dropdown.render();
  990. $dropdownContainer.append($dropdown);
  991. this.results = new this.options.resultsAdapter(
  992. $element, this.options, this.data);
  993. var $resultsContainer = $dropdown.find('.results');
  994. var $results = this.results.render();
  995. $resultsContainer.append($results);
  996. // Bind events
  997. var self = this;
  998. this.data.bind(this, $container);
  999. this.selection.bind(this, $container);
  1000. this.results.bind(this, $container);
  1001. this.$element.on('change', function () {
  1002. self.data.current(function (data) {
  1003. self.trigger('selection:update', {
  1004. data: data
  1005. });
  1006. });
  1007. });
  1008. this.selection.on('toggle', function () {
  1009. self.toggleDropdown();
  1010. });
  1011. this.results.on('selected', function (params) {
  1012. self.trigger('select', params);
  1013. self.trigger('close');
  1014. });
  1015. this.results.on('unselected', function (params) {
  1016. self.trigger('unselect', params);
  1017. self.trigger('close');
  1018. });
  1019. this.on('open', function () {
  1020. $container.addClass('open');
  1021. });
  1022. this.on('close', function () {
  1023. $container.removeClass('open');
  1024. });
  1025. // Set the initial state
  1026. this.data.current(function (initialData) {
  1027. self.trigger('selection:update', {
  1028. data: initialData
  1029. });
  1030. });
  1031. this.data.query({}, function (data) {
  1032. self.results.trigger('results:all', data);
  1033. });
  1034. // Hide the original select
  1035. $element.hide();
  1036. };
  1037. Utils.Extend(Select2, Utils.Observable);
  1038. Select2.prototype.toggleDropdown = function () {
  1039. if (this.$container.hasClass('open')) {
  1040. this.trigger('close');
  1041. } else {
  1042. this.trigger('open');
  1043. }
  1044. };
  1045. Select2.prototype.render = function () {
  1046. var $container = $(
  1047. '<span class="select2 select2-container select2-theme-default">' +
  1048. '<span class="selection"></span>' +
  1049. '<span class="dropdown-wrapper"></span>' +
  1050. '</span>'
  1051. );
  1052. return $container;
  1053. };
  1054. return Select2;
  1055. });
  1056. define('jquery.select2',[
  1057. 'jquery',
  1058. 'select2/core'
  1059. ], function ($, Select2) {
  1060. if ($.fn.select2 == null) {
  1061. $.fn.select2 = function (options) {
  1062. options = options || {};
  1063. if (typeof options === 'object') {
  1064. this.each(function () {
  1065. var instance = new Select2($(this), options);
  1066. });
  1067. } else if (typeof options === 'string') {
  1068. var instance = this.data('select2');
  1069. instance[options](arguments.slice(1));
  1070. } else {
  1071. throw new Error('Invalid arguments for Select2: ' + options);
  1072. }
  1073. };
  1074. }
  1075. return Select2;
  1076. });
  1077. require('jquery.select2');