select2.js 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. ;
  379. define('select2/utils',[], function () {
  380. var Utils = {};
  381. Utils.Extend = function (ChildClass, SuperClass) {
  382. var __hasProp = {}.hasOwnProperty
  383. function BaseConstructor () {
  384. this.constructor = ChildClass;
  385. }
  386. for (var key in SuperClass) {
  387. if (__hasProp.call(SuperClass, key)) {
  388. ChildClass[key] = SuperClass[key];
  389. }
  390. }
  391. BaseConstructor.prototype = SuperClass.prototype;
  392. ChildClass.prototype = new BaseConstructor();
  393. ChildClass.__super__ = SuperClass.prototype;
  394. return ChildClass;
  395. };
  396. function getMethods (theClass) {
  397. var proto = theClass.prototype;
  398. var methods = [];
  399. for (var methodName in proto) {
  400. var m = proto[methodName];
  401. if (typeof m !== "function") {
  402. continue;
  403. }
  404. methods.push(methodName);
  405. }
  406. return methods;
  407. }
  408. Utils.Decorate = function (SuperClass, DecoratorClass) {
  409. var decoratedMethods = getMethods(DecoratorClass);
  410. var superMethods = getMethods(SuperClass);
  411. function DecoratedClass () {
  412. var unshift = Array.prototype.unshift;
  413. var argCount = DecoratorClass.prototype.constructor.length;
  414. var calledConstructor = SuperClass.prototype.constructor;
  415. if (argCount > 0) {
  416. unshift.call(arguments, SuperClass.prototype.constructor);
  417. calledConstructor = DecoratorClass.prototype.constructor;
  418. }
  419. calledConstructor.apply(this, arguments);
  420. }
  421. DecoratorClass.displayName = SuperClass.displayName;
  422. function ctr () {
  423. this.constructor = DecoratedClass;
  424. }
  425. DecoratedClass.prototype = new ctr();
  426. for (var m = 0; m < superMethods.length; m++) {
  427. var methodName = superMethods[m];
  428. DecoratedClass.prototype[methodName] = SuperClass.prototype[methodName];
  429. }
  430. for (var m = 0; m < decoratedMethods.length; m++) {
  431. var methodName = decoratedMethods[m];
  432. function calledMethod (methodName) {
  433. // Stub out the original method if it's not decorating an actual method
  434. var originalMethod = function () {};
  435. if (methodName in DecoratedClass.prototype) {
  436. originalMethod = DecoratedClass.prototype[methodName];
  437. }
  438. var decoratedMethod = DecoratorClass.prototype[methodName];
  439. return function () {
  440. var unshift = Array.prototype.unshift;
  441. unshift.call(arguments, originalMethod);
  442. return decoratedMethod.apply(this, arguments);
  443. }
  444. }
  445. DecoratedClass.prototype[methodName] = calledMethod(methodName);
  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/data/select',[
  477. '../utils',
  478. 'jquery'
  479. ], function (Utils, $) {
  480. function SelectAdapter ($element, options) {
  481. this.$element = $element;
  482. SelectAdapter.__super__.constructor.call(this);
  483. }
  484. Utils.Extend(SelectAdapter, Utils.Observable);
  485. SelectAdapter.prototype.current = function (callback) {
  486. var data = [];
  487. var self = this;
  488. this.$element.find(":selected").each(function () {
  489. var $option = $(this);
  490. var option = self.item($option);
  491. data.push(option);
  492. });
  493. callback(data);
  494. };
  495. SelectAdapter.prototype.select = function (data) {
  496. var self = this;
  497. if (this.$element.prop("multiple")) {
  498. this.current(function (currentData) {
  499. var val = [];
  500. data = [data];
  501. data.push.apply(data, currentData);
  502. for (var d = 0; d < data.length; d++) {
  503. id = data[d].id;
  504. if (val.indexOf(id) === -1) {
  505. val.push(id);
  506. }
  507. }
  508. self.$element.val(val);
  509. self.$element.trigger("change");
  510. });
  511. } else {
  512. var val = data.id;
  513. this.$element.val(val);
  514. this.$element.trigger("change");
  515. }
  516. };
  517. SelectAdapter.prototype.unselect = function (data) {
  518. var self = this;
  519. if (!this.$element.prop("multiple")) {
  520. return;
  521. }
  522. this.current(function (currentData) {
  523. var val = [];
  524. for (var d = 0; d < currentData.length; d++) {
  525. id = currentData[d].id;
  526. if (id !== data.id && val.indexOf(id) === -1) {
  527. val.push(id);
  528. }
  529. }
  530. self.$element.val(val);
  531. self.$element.trigger("change");
  532. });
  533. }
  534. SelectAdapter.prototype.bind = function (container, $container) {
  535. var self = this;
  536. container.on("select", function (params) {
  537. self.select(params.data);
  538. });
  539. container.on("unselect", function (params) {
  540. self.unselect(params.data);
  541. });
  542. }
  543. SelectAdapter.prototype.query = function (params, callback) {
  544. var data = [];
  545. var self = this;
  546. this.$element.find("option").each(function () {
  547. var $option = $(this);
  548. var option = self.item($option);
  549. if (self.matches(params, option)) {
  550. data.push(option);
  551. }
  552. });
  553. callback(data);
  554. };
  555. SelectAdapter.prototype.item = function ($option) {
  556. var data = {
  557. id: $option.val(),
  558. text: $option.html()
  559. };
  560. return data;
  561. };
  562. SelectAdapter.prototype.matches = function (params, data) {
  563. if ($.trim(params.term) == "") {
  564. return true;
  565. }
  566. if (data.text.indexOf(params.term) > -1) {
  567. return true;
  568. }
  569. return false;
  570. }
  571. return SelectAdapter;
  572. });
  573. define('select2/results',[
  574. './utils'
  575. ], function (Utils) {
  576. function Results ($element, options, dataAdapter) {
  577. this.$element = $element;
  578. this.data = dataAdapter;
  579. Results.__super__.constructor.call(this);
  580. }
  581. Utils.Extend(Results, Utils.Observable);
  582. Results.prototype.render = function () {
  583. var $results = $(
  584. '<ul class="options"></ul>'
  585. );
  586. this.$results = $results;
  587. return $results;
  588. };
  589. Results.prototype.clear = function () {
  590. this.$results.empty();
  591. };
  592. Results.prototype.append = function (data) {
  593. var $options = [];
  594. for (var d = 0; d < data.length; d++) {
  595. var item = data[d];
  596. var $option = this.option(item);
  597. $options.push($option);
  598. }
  599. this.$results.append($options);
  600. };
  601. Results.prototype.setClasses = function () {
  602. var self = this;
  603. this.data.current(function (selected) {
  604. selected = $.map(selected, function (s) { return s.id; });
  605. self.$results.find(".option.selected").removeClass("selected");
  606. var $options = self.$results.find(".option");
  607. $options.each(function () {
  608. var $option = $(this);
  609. var item = $option.data("data");
  610. if (selected.indexOf(item.id) > -1) {
  611. $option.addClass("selected");
  612. }
  613. });
  614. });
  615. };
  616. Results.prototype.option = function (data) {
  617. var $option = $(
  618. '<li class="option"></li>'
  619. );
  620. $option.html(data.text);
  621. $option.data("data", data);
  622. return $option;
  623. }
  624. Results.prototype.bind = function (container, $container) {
  625. var self = this;
  626. this.on("results:all", function (data) {
  627. self.clear();
  628. self.append(data);
  629. self.setClasses();
  630. });
  631. this.on("results:append", function (data) {
  632. self.append(data);
  633. self.setClasses();
  634. })
  635. this.$results.on("click", ".option", function (evt) {
  636. var $this = $(this);
  637. var data = $this.data("data");
  638. if ($this.hasClass("selected")) {
  639. self.trigger("unselected", {
  640. originalEvent: evt,
  641. data: data
  642. })
  643. self.setClasses();
  644. return;
  645. }
  646. self.trigger("selected", {
  647. originalEvent: evt,
  648. data: data
  649. });
  650. self.setClasses();
  651. });
  652. this.$results.on("mouseenter", ".option", function (evt) {
  653. self.$results.find(".option.highlighted").removeClass("highlighted");
  654. $(this).addClass("highlighted");
  655. });
  656. this.$results.on("mouseleave", ".option", function (evt) {
  657. $(this).removeClass("highlighted");
  658. });
  659. };
  660. return Results;
  661. })
  662. ;
  663. define('select2/dropdown',[
  664. './utils'
  665. ], function (Utils) {
  666. function Dropdown ($element, options) {
  667. this.$element = $element;
  668. }
  669. Utils.Extend(Dropdown, Utils.Observable);
  670. Dropdown.prototype.render = function () {
  671. var $dropdown = $(
  672. '<span class="">' +
  673. '<span class="results"></span>' +
  674. '</span>'
  675. );
  676. return $dropdown;
  677. }
  678. return Dropdown;
  679. })
  680. ;
  681. define('select2/selection/single',[
  682. '../utils'
  683. ], function (Utils) {
  684. function SingleSelection ($element, options) {
  685. this.$element = $element;
  686. this.options = options;
  687. SingleSelection.__super__.constructor.call(this);
  688. }
  689. Utils.Extend(SingleSelection, Utils.Observable);
  690. SingleSelection.prototype.render = function () {
  691. var $selection = $(
  692. '<span class="single-select">' +
  693. '<span class="rendered-selection"></span>' +
  694. '</span>'
  695. );
  696. this.$selection = $selection;
  697. return $selection;
  698. }
  699. SingleSelection.prototype.bind = function (container, $container) {
  700. var self = this;
  701. this.$selection.on('click', function (evt) {
  702. self.trigger("toggle", {
  703. originalEvent: evt
  704. });
  705. });
  706. container.on("selection:update", function (params) {
  707. self.update(params.data);
  708. })
  709. }
  710. SingleSelection.prototype.clear = function () {
  711. this.$selection.find(".rendered-selection").empty();
  712. }
  713. SingleSelection.prototype.display = function (data) {
  714. return data.text;
  715. }
  716. SingleSelection.prototype.update = function (data) {
  717. if (data.length == 0) {
  718. this.clear();
  719. return;
  720. }
  721. var selection = data[0];
  722. var formatted = this.display(selection);
  723. this.$selection.find(".rendered-selection").html(formatted);
  724. }
  725. return SingleSelection;
  726. });
  727. define('select2/selection/multiple',[
  728. '../utils'
  729. ], function (Utils) {
  730. function MultipleSelection ($element, options) {
  731. this.$element = $element;
  732. this.options = options;
  733. MultipleSelection.__super__.constructor.call(this);
  734. }
  735. Utils.Extend(MultipleSelection, Utils.Observable);
  736. MultipleSelection.prototype.render = function () {
  737. var $selection = $(
  738. '<span class="multiple-select">' +
  739. '<ul class="rendered-selection"></ul>' +
  740. '</span>'
  741. );
  742. this.$selection = $selection;
  743. return $selection;
  744. }
  745. MultipleSelection.prototype.bind = function (container, $container) {
  746. var self = this;
  747. this.$selection.on('click', function (evt) {
  748. self.trigger("toggle", {
  749. originalEvent: evt
  750. });
  751. });
  752. container.on("selection:update", function (params) {
  753. self.update(params.data);
  754. });
  755. }
  756. MultipleSelection.prototype.clear = function () {
  757. this.$selection.find(".rendered-selection").empty();
  758. }
  759. MultipleSelection.prototype.display = function (data) {
  760. return data.text;
  761. }
  762. MultipleSelection.prototype.update = function (data) {
  763. this.clear();
  764. if (data.length == 0) {
  765. return;
  766. }
  767. var $selections = [];
  768. for (var d = 0; d < data.length; d++) {
  769. var selection = data[d];
  770. var formatted = this.display(selection);
  771. var $selection = $('<ul class="choice"></ul>');
  772. $selection.text(formatted);
  773. $selection.data("data", data);
  774. $selections.push($selection);
  775. }
  776. this.$selection.find(".rendered-selection").append($selections);
  777. }
  778. return MultipleSelection;
  779. });
  780. define('select2/options',[
  781. './data/select',
  782. './results',
  783. './dropdown',
  784. './selection/single',
  785. './selection/multiple'
  786. ], function (SelectData, ResultsList, Dropdown, SingleSelection,
  787. MultipleSelection) {
  788. function Options (options) {
  789. this.options = options;
  790. this.dataAdapter = SelectData;
  791. this.resultsAdapter = ResultsList;
  792. this.dropdownAdapter = options.dropdownAdapter || Dropdown;
  793. this.selectionAdapter = options.selectionAdapter;
  794. if (this.selectionAdapter == null) {
  795. if (this.options.multiple) {
  796. this.selectionAdapter = MultipleSelection;
  797. } else {
  798. this.selectionAdapter = SingleSelection;
  799. }
  800. }
  801. }
  802. return Options;
  803. })
  804. ;
  805. define('select2/core',[
  806. 'jquery',
  807. './options',
  808. './utils'
  809. ], function ($, Options, Utils) {
  810. var Select2 = function ($element, options) {
  811. this.$element = $element;
  812. options = options || {};
  813. options.multiple = options.multiple || $element.prop("multiple");
  814. this.options = new Options(options);
  815. Select2.__super__.constructor.call(this);
  816. // Set up containers and adapters
  817. this.data = new this.options.dataAdapter($element, this.options);
  818. var $container = this.render();
  819. $container.insertAfter(this.$element);
  820. $container.width($element.width());
  821. this.selection = new this.options.selectionAdapter($element, this.options);
  822. var $selectionContainer = $container.find(".selection");
  823. var $selection = this.selection.render();
  824. $selectionContainer.append($selection);
  825. this.dropdown = new this.options.dropdownAdapter($element, this.options);
  826. var $dropdownContainer = $container.find(".dropdown");
  827. var $dropdown = this.dropdown.render();
  828. $dropdownContainer.append($dropdown);
  829. this.results = new this.options.resultsAdapter($element, this.options, this.data);
  830. var $resultsContainer = $dropdown.find(".results");
  831. var $results = this.results.render();
  832. $resultsContainer.append($results);
  833. // Bind events
  834. var self = this;
  835. this.data.bind(this, $container);
  836. this.selection.bind(this, $container);
  837. this.results.bind(this, $container);
  838. this.$element.on("change", function () {
  839. self.data.current(function (data) {
  840. self.trigger("selection:update", {
  841. data: data
  842. });
  843. });
  844. });
  845. this.selection.on("toggle", function () {
  846. $container.toggleClass("open");
  847. });
  848. this.results.on("selected", function (params) {
  849. self.trigger("select", params);
  850. $container.removeClass("open");
  851. });
  852. this.results.on("unselected", function (params) {
  853. self.trigger("unselect", params);
  854. $container.removeClass("open");
  855. });
  856. // Set the initial state
  857. this.data.current(function (initialData) {
  858. self.selection.update(initialData);
  859. });
  860. this.data.query({}, function (data) {
  861. self.results.trigger("results:all", data);
  862. });
  863. };
  864. Utils.Extend(Select2, Utils.Observable);
  865. Select2.prototype.render = function () {
  866. var $container = $(
  867. '<span class="select2 select2-container select2-theme-default">' +
  868. '<span class="selection"></span>' +
  869. '<span class="dropdown"></span>' +
  870. '</span>'
  871. );
  872. return $container;
  873. };
  874. return Select2;
  875. });