select2.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610
  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. 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. self.$results.find('.option.selected').removeClass('selected');
  515. var $options = self.$results.find('.option');
  516. $options.each(function () {
  517. var $option = $(this);
  518. var item = $option.data('data');
  519. if (item.id != null && selectedIds.indexOf(item.id.toString()) > -1) {
  520. $option.addClass('selected');
  521. }
  522. });
  523. });
  524. };
  525. Results.prototype.option = function (data) {
  526. var $option = $(
  527. '<li class="option highlightable selectable"></li>'
  528. );
  529. if (data.children) {
  530. $option.addClass('group').removeClass('highlightable selectable');
  531. var $label = $('<strong class="group-label"></strong>');
  532. $label.html(data.text);
  533. var $children = [];
  534. for (var c = 0; c < data.children.length; c++) {
  535. var child = data.children[c];
  536. var $child = this.option(child);
  537. $children.push($child);
  538. }
  539. var $childrenContainer = $('<ul class="options nested-options"></ul>');
  540. $childrenContainer.append($children);
  541. $option.append($label);
  542. $option.append($childrenContainer);
  543. } else {
  544. $option.html(data.text);
  545. }
  546. if (data.disabled) {
  547. $option.removeClass('selectable highlightable').addClass('disabled');
  548. }
  549. if (data.id == null) {
  550. $option.removeClass('selectable highlightable');
  551. }
  552. $option.data('data', data);
  553. return $option;
  554. };
  555. Results.prototype.bind = function (container, $container) {
  556. var self = this;
  557. container.on('results:all', function (params) {
  558. self.clear();
  559. self.append(params.data);
  560. self.setClasses();
  561. });
  562. container.on('results:append', function (params) {
  563. self.append(params.data);
  564. self.setClasses();
  565. });
  566. container.on('select', function () {
  567. self.setClasses();
  568. });
  569. container.on('unselect', function () {
  570. self.setClasses();
  571. });
  572. this.$results.on('mouseup', '.option.selectable', function (evt) {
  573. var $this = $(this);
  574. var data = $this.data('data');
  575. if ($this.hasClass('selected')) {
  576. self.trigger('unselected', {
  577. originalEvent: evt,
  578. data: data
  579. });
  580. return;
  581. }
  582. self.trigger('selected', {
  583. originalEvent: evt,
  584. data: data
  585. });
  586. });
  587. this.$results.on('mouseenter', '.option.highlightable', function (evt) {
  588. self.$results.find('.option.highlighted').removeClass('highlighted');
  589. $(this).addClass('highlighted');
  590. });
  591. this.$results.on('mouseleave', '.option', function (evt) {
  592. $(this).removeClass('highlighted');
  593. });
  594. };
  595. return Results;
  596. });
  597. define('select2/selection/single',[
  598. '../utils'
  599. ], function (Utils) {
  600. function SingleSelection ($element, options) {
  601. this.$element = $element;
  602. this.options = options;
  603. SingleSelection.__super__.constructor.call(this);
  604. }
  605. Utils.Extend(SingleSelection, Utils.Observable);
  606. SingleSelection.prototype.render = function () {
  607. var $selection = $(
  608. '<span class="single-select">' +
  609. '<span class="rendered-selection"></span>' +
  610. '</span>'
  611. );
  612. this.$selection = $selection;
  613. return $selection;
  614. };
  615. SingleSelection.prototype.bind = function (container, $container) {
  616. var self = this;
  617. this.$selection.on('mousedown', function (evt) {
  618. // Only respond to left clicks
  619. if (evt.which !== 1) {
  620. return;
  621. }
  622. self.trigger('toggle', {
  623. originalEvent: evt
  624. });
  625. });
  626. container.on('selection:update', function (params) {
  627. self.update(params.data);
  628. });
  629. };
  630. SingleSelection.prototype.clear = function () {
  631. this.$selection.find('.rendered-selection').empty();
  632. };
  633. SingleSelection.prototype.display = function (data) {
  634. return data.text;
  635. };
  636. SingleSelection.prototype.selectionContainer = function () {
  637. return $('<span></span>');
  638. };
  639. SingleSelection.prototype.update = function (data) {
  640. if (data.length === 0) {
  641. this.clear();
  642. return;
  643. }
  644. var selection = data[0];
  645. var formatted = this.display(selection);
  646. this.$selection.find('.rendered-selection').html(formatted);
  647. };
  648. return SingleSelection;
  649. });
  650. define('select2/selection/multiple',[
  651. '../utils'
  652. ], function (Utils) {
  653. function MultipleSelection ($element, options) {
  654. this.$element = $element;
  655. this.options = options;
  656. MultipleSelection.__super__.constructor.call(this);
  657. }
  658. Utils.Extend(MultipleSelection, Utils.Observable);
  659. MultipleSelection.prototype.render = function () {
  660. var $selection = $(
  661. '<span class="multiple-select">' +
  662. '<ul class="rendered-selection"></ul>' +
  663. '</span>'
  664. );
  665. this.$selection = $selection;
  666. return $selection;
  667. };
  668. MultipleSelection.prototype.bind = function (container, $container) {
  669. var self = this;
  670. this.$selection.on('click', function (evt) {
  671. self.trigger('toggle', {
  672. originalEvent: evt
  673. });
  674. });
  675. this.$selection.on('click', '.remove', function (evt) {
  676. var $remove = $(this);
  677. var $selection = $remove.parent();
  678. var data = $selection.data('data');
  679. self.trigger('unselected', {
  680. originalEvent: evt,
  681. data: data
  682. });
  683. });
  684. container.on('selection:update', function (params) {
  685. self.update(params.data);
  686. });
  687. };
  688. MultipleSelection.prototype.clear = function () {
  689. this.$selection.find('.rendered-selection').empty();
  690. };
  691. MultipleSelection.prototype.display = function (data) {
  692. return data.text;
  693. };
  694. MultipleSelection.prototype.selectionContainer = function () {
  695. var $container = $(
  696. '<li class="choice">' +
  697. '<span class="remove">&times;</span>' +
  698. '</li>'
  699. );
  700. return $container;
  701. };
  702. MultipleSelection.prototype.update = function (data) {
  703. this.clear();
  704. if (data.length === 0) {
  705. return;
  706. }
  707. var $selections = [];
  708. for (var d = 0; d < data.length; d++) {
  709. var selection = data[d];
  710. var formatted = this.display(selection);
  711. var $selection = this.selectionContainer();
  712. $selection.append(formatted);
  713. $selection.data('data', selection);
  714. $selections.push($selection);
  715. }
  716. this.$selection.find('.rendered-selection').append($selections);
  717. };
  718. return MultipleSelection;
  719. });
  720. define('select2/selection/placeholder',[
  721. '../utils'
  722. ], function (Utils) {
  723. function Placeholder (decorated, $element, options) {
  724. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  725. decorated.call(this, $element, options);
  726. }
  727. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  728. if (typeof placeholder === 'string') {
  729. placeholder = {
  730. id: '',
  731. text: placeholder
  732. };
  733. }
  734. return placeholder;
  735. };
  736. Placeholder.prototype.update = function (decorated, data) {
  737. var singlePlaceholder = (
  738. data.length == 1 && data[0].id != this.placeholder.id
  739. );
  740. var multipleSelections = data.length > 1;
  741. if (multipleSelections || singlePlaceholder) {
  742. return decorated.call(this, data);
  743. }
  744. this.clear();
  745. var $placeholder = this.selectionContainer();
  746. $placeholder.html(this.display(this.placeholder));
  747. $placeholder.addClass('placeholder').removeClass('choice');
  748. this.$selection.find('.rendered-selection').append($placeholder);
  749. };
  750. return Placeholder;
  751. });
  752. define('select2/data/base',[
  753. '../utils'
  754. ], function (Utils) {
  755. function BaseAdapter ($element, options) {
  756. BaseAdapter.__super__.constructor.call(this);
  757. }
  758. Utils.Extend(BaseAdapter, Utils.Observable);
  759. BaseAdapter.prototype.current = function (callback) {
  760. throw new Error('The `current` method must be defined in child classes.');
  761. };
  762. BaseAdapter.prototype.query = function (params, callback) {
  763. throw new Error('The `query` method must be defined in child classes.');
  764. };
  765. return BaseAdapter;
  766. });
  767. define('select2/data/select',[
  768. './base',
  769. '../utils',
  770. 'jquery'
  771. ], function (BaseAdapter, Utils, $) {
  772. function SelectAdapter ($element, options) {
  773. this.$element = $element;
  774. SelectAdapter.__super__.constructor.call(this);
  775. }
  776. Utils.Extend(SelectAdapter, BaseAdapter);
  777. SelectAdapter.prototype.current = function (callback) {
  778. var data = [];
  779. var self = this;
  780. this.$element.find(':selected').each(function () {
  781. var $option = $(this);
  782. var option = self.item($option);
  783. data.push(option);
  784. });
  785. callback(data);
  786. };
  787. SelectAdapter.prototype.select = function (data) {
  788. var self = this;
  789. if (this.$element.prop('multiple')) {
  790. this.current(function (currentData) {
  791. var val = [];
  792. data = [data];
  793. data.push.apply(data, currentData);
  794. for (var d = 0; d < data.length; d++) {
  795. id = data[d].id;
  796. if (val.indexOf(id) === -1) {
  797. val.push(id);
  798. }
  799. }
  800. self.$element.val(val);
  801. self.$element.trigger('change');
  802. });
  803. } else {
  804. var val = data.id;
  805. this.$element.val(val);
  806. this.$element.trigger('change');
  807. }
  808. };
  809. SelectAdapter.prototype.unselect = function (data) {
  810. var self = this;
  811. if (!this.$element.prop('multiple')) {
  812. return;
  813. }
  814. this.current(function (currentData) {
  815. var val = [];
  816. for (var d = 0; d < currentData.length; d++) {
  817. id = currentData[d].id;
  818. if (id !== data.id && val.indexOf(id) === -1) {
  819. val.push(id);
  820. }
  821. }
  822. self.$element.val(val);
  823. self.$element.trigger('change');
  824. });
  825. };
  826. SelectAdapter.prototype.bind = function (container, $container) {
  827. var self = this;
  828. container.on('select', function (params) {
  829. self.select(params.data);
  830. });
  831. container.on('unselect', function (params) {
  832. self.unselect(params.data);
  833. });
  834. };
  835. SelectAdapter.prototype.query = function (params, callback) {
  836. var data = [];
  837. var self = this;
  838. var $options = this.$element.children();
  839. $options.each(function () {
  840. var $option = $(this);
  841. if (!$option.is('option') && !$option.is('optgroup')) {
  842. return;
  843. }
  844. var option = self.item($option);
  845. var matches = self.matches(params, option);
  846. if (matches !== null) {
  847. data.push(matches);
  848. }
  849. });
  850. callback(data);
  851. };
  852. SelectAdapter.prototype.item = function ($option) {
  853. var data = $option.data('data');
  854. // If the data has already be generated, use it
  855. if (data == null) {
  856. if ($option.is('option')) {
  857. data = {
  858. id: $option.val(),
  859. text: $option.html(),
  860. disabled: $option.prop('disabled')
  861. };
  862. } else if ($option.is('optgroup')) {
  863. data = {
  864. text: $option.attr('label'),
  865. children: []
  866. };
  867. var $children = $option.children('option');
  868. var children = [];
  869. for (var c = 0; c < $children.length; c++) {
  870. var $child = $($children[c]);
  871. var child = this.item($child);
  872. children.push(child);
  873. }
  874. data.children = children;
  875. }
  876. $option.data('data', data);
  877. }
  878. return data;
  879. };
  880. SelectAdapter.prototype.matches = function (params, data) {
  881. var match = $.extend(true, {}, data);
  882. if (data.children) {
  883. for (var c = data.children.length - 1; c >= 0; c--) {
  884. var child = data.children[c];
  885. var matches = this.matches(params, child);
  886. // If there wasn't a match, remove the object in the array
  887. if (matches === null) {
  888. match.children.splice(c, 1);
  889. }
  890. }
  891. if (match.children.length > 0) {
  892. return match;
  893. }
  894. }
  895. if ($.trim(params.term) === '') {
  896. return match;
  897. }
  898. if (data.text.toUpperCase().indexOf(params.term.toUpperCase()) > -1) {
  899. return match;
  900. }
  901. return null;
  902. };
  903. return SelectAdapter;
  904. });
  905. define('select2/data/array',[
  906. './select',
  907. '../utils'
  908. ], function (SelectAdapter, Utils) {
  909. function ArrayAdapter ($element, options) {
  910. this.data = options.options.data;
  911. ArrayAdapter.__super__.constructor.call(this, $element, options);
  912. }
  913. Utils.Extend(ArrayAdapter, SelectAdapter);
  914. ArrayAdapter.prototype.select = function (data) {
  915. var self = this;
  916. this.$element.find('option').each(function () {
  917. var $option = $(this);
  918. var option = self.item($option);
  919. if (option.id == data.id.toString()) {
  920. $option.remove();
  921. }
  922. });
  923. var $option = this.option(data);
  924. this.$element.append($option);
  925. ArrayAdapter.__super__.select.call(this, data);
  926. };
  927. ArrayAdapter.prototype.option = function (data) {
  928. var $option = $('<option></option>');
  929. $option.text(data.text);
  930. $option.val(data.id);
  931. $option.data('data', data);
  932. return $option;
  933. };
  934. ArrayAdapter.prototype.query = function (params, callback) {
  935. var matches = [];
  936. var self = this;
  937. $.each(this.data, function () {
  938. var option = this;
  939. if (self.matches(params, option)) {
  940. matches.push(option);
  941. }
  942. });
  943. callback(matches);
  944. };
  945. return ArrayAdapter;
  946. });
  947. define('select2/data/ajax',[
  948. './array',
  949. '../utils',
  950. 'jquery'
  951. ], function (ArrayAdapter, Utils, $) {
  952. function AjaxAdapter ($element, options) {
  953. this.ajaxOptions = options.get('ajax');
  954. this.processResults = this.ajaxOptions.processResults ||
  955. function (results) {
  956. return results;
  957. };
  958. ArrayAdapter.__super__.constructor.call(this, $element, options);
  959. }
  960. Utils.Extend(AjaxAdapter, ArrayAdapter);
  961. AjaxAdapter.prototype.query = function (params, callback) {
  962. var matches = [];
  963. var self = this;
  964. var options = $.extend({
  965. type: 'GET'
  966. }, this.ajaxOptions);
  967. if (typeof options.url === 'function') {
  968. options.url = options.url(params);
  969. }
  970. if (typeof options.data === 'function') {
  971. options.data = options.data(params);
  972. }
  973. var $request = $.ajax(options);
  974. $request.success(function (data) {
  975. var results = self.processResults(data);
  976. callback(results);
  977. });
  978. };
  979. return AjaxAdapter;
  980. });
  981. define('select2/dropdown',[
  982. './utils'
  983. ], function (Utils) {
  984. function Dropdown ($element, options) {
  985. this.$element = $element;
  986. }
  987. Utils.Extend(Dropdown, Utils.Observable);
  988. Dropdown.prototype.render = function () {
  989. var $dropdown = $(
  990. '<span class="dropdown">' +
  991. '<span class="results"></span>' +
  992. '</span>'
  993. );
  994. return $dropdown;
  995. };
  996. Dropdown.prototype.bind = function (container, $container) {
  997. // Can be implemented in subclasses
  998. };
  999. return Dropdown;
  1000. });
  1001. define('select2/dropdown/search',[
  1002. ], function () {
  1003. function Search () { }
  1004. Search.prototype.render = function (decorated) {
  1005. var $rendered = decorated.call(this);
  1006. var $search = $(
  1007. '<span class="search">' +
  1008. '<input type="search" name="search" />' +
  1009. '</span>'
  1010. );
  1011. this.$searchContainer = $search;
  1012. this.$search = $search.find('input');
  1013. $rendered.prepend($search);
  1014. return $rendered;
  1015. };
  1016. Search.prototype.bind = function (decorated, container, $container) {
  1017. var self = this;
  1018. decorated.call(this, container, $container);
  1019. this.$search.on('keyup', function () {
  1020. container.trigger('query', {
  1021. term: $(this).val()
  1022. });
  1023. });
  1024. container.on('results:all', function (params) {
  1025. if (params.query.term == null || params.query.term === '') {
  1026. var showSearch = self.showSearch(params);
  1027. if (showSearch) {
  1028. self.$searchContainer.show();
  1029. } else {
  1030. self.$searchContainer.hide();
  1031. }
  1032. }
  1033. });
  1034. };
  1035. Search.prototype.showSearch = function (params) {
  1036. return true;
  1037. };
  1038. return Search;
  1039. });
  1040. define('select2/defaults',[
  1041. './results',
  1042. './selection/single',
  1043. './selection/multiple',
  1044. './selection/placeholder',
  1045. './utils',
  1046. './data/select',
  1047. './data/array',
  1048. './data/ajax',
  1049. './dropdown',
  1050. './dropdown/search'
  1051. ], function (ResultsList,
  1052. SingleSelection, MultipleSelection, Placeholder,
  1053. Utils,
  1054. SelectData, ArrayData, AjaxData,
  1055. Dropdown, Search) {
  1056. function Defaults () {
  1057. this.reset();
  1058. }
  1059. Defaults.prototype.apply = function (options) {
  1060. options = $.extend({}, options, this.defaults);
  1061. if (options.dataAdapter == null) {
  1062. if (options.ajax) {
  1063. options.dataAdapter = AjaxData;
  1064. } else if (options.data) {
  1065. options.dataAdapter = ArrayData;
  1066. } else {
  1067. options.dataAdapter = SelectData;
  1068. }
  1069. }
  1070. if (options.resultsAdapter == null) {
  1071. options.resultsAdapter = ResultsList;
  1072. }
  1073. if (options.dropdownAdapter == null) {
  1074. var SearchableDropdown = Utils.Decorate(Dropdown, Search);
  1075. options.dropdownAdapter = SearchableDropdown;
  1076. }
  1077. if (options.selectionAdapter == null) {
  1078. if (options.multiple) {
  1079. options.selectionAdapter = MultipleSelection;
  1080. } else {
  1081. options.selectionAdapter = SingleSelection;
  1082. }
  1083. // Add the placeholder mixin if a placeholder was specified
  1084. if (options.placeholder != null) {
  1085. options.selectionAdapter = Utils.Decorate(
  1086. options.selectionAdapter,
  1087. Placeholder
  1088. );
  1089. }
  1090. }
  1091. return options;
  1092. };
  1093. Defaults.prototype.reset = function () {
  1094. this.defaults = { };
  1095. };
  1096. var defaults = new Defaults();
  1097. return defaults;
  1098. });
  1099. define('select2/options',[
  1100. './defaults'
  1101. ], function (Defaults) {
  1102. function Options (options) {
  1103. this.options = Defaults.apply(options);
  1104. }
  1105. Options.prototype.fromElement = function ($e) {
  1106. return this;
  1107. };
  1108. Options.prototype.get = function (key) {
  1109. return this.options[key];
  1110. };
  1111. Options.prototype.set = function (key, val) {
  1112. this.options[key] = val;
  1113. };
  1114. return Options;
  1115. });
  1116. define('select2/core',[
  1117. 'jquery',
  1118. './options',
  1119. './utils'
  1120. ], function ($, Options, Utils) {
  1121. var Select2 = function ($element, options) {
  1122. this.$element = $element;
  1123. options = options || {};
  1124. options.multiple = options.multiple || $element.prop('multiple');
  1125. this.options = new Options(options);
  1126. Select2.__super__.constructor.call(this);
  1127. // Set up containers and adapters
  1128. var DataAdapter = this.options.get('dataAdapter');
  1129. this.data = new DataAdapter($element, this.options);
  1130. var $container = this.render();
  1131. this.$container = $container;
  1132. $container.insertAfter(this.$element);
  1133. $container.width($element.outerWidth(false));
  1134. var SelectionAdapter = this.options.get('selectionAdapter');
  1135. this.selection = new SelectionAdapter($element, this.options);
  1136. var $selectionContainer = $container.find('.selection');
  1137. var $selection = this.selection.render();
  1138. $selectionContainer.append($selection);
  1139. var DropdownAdapter = this.options.get('dropdownAdapter');
  1140. this.dropdown = new DropdownAdapter($element, this.options);
  1141. var $dropdownContainer = $container.find('.dropdown-wrapper');
  1142. var $dropdown = this.dropdown.render();
  1143. $dropdownContainer.append($dropdown);
  1144. var ResultsAdapter = this.options.get('resultsAdapter');
  1145. this.results = new ResultsAdapter($element, this.options, this.data);
  1146. var $resultsContainer = $dropdown.find('.results');
  1147. var $results = this.results.render();
  1148. $resultsContainer.append($results);
  1149. // Bind events
  1150. var self = this;
  1151. this.data.bind(this, $container);
  1152. this.selection.bind(this, $container);
  1153. this.dropdown.bind(this, $container);
  1154. this.results.bind(this, $container);
  1155. this.$element.on('change', function () {
  1156. self.data.current(function (data) {
  1157. self.trigger('selection:update', {
  1158. data: data
  1159. });
  1160. });
  1161. });
  1162. this.selection.on('toggle', function () {
  1163. self.toggleDropdown();
  1164. });
  1165. this.selection.on('unselected', function (params) {
  1166. self.trigger('unselect', params);
  1167. self.trigger('close');
  1168. });
  1169. this.results.on('selected', function (params) {
  1170. self.trigger('select', params);
  1171. self.trigger('close');
  1172. });
  1173. this.results.on('unselected', function (params) {
  1174. self.trigger('unselect', params);
  1175. self.trigger('close');
  1176. });
  1177. this.on('open', function () {
  1178. $container.addClass('open');
  1179. });
  1180. this.on('close', function () {
  1181. $container.removeClass('open');
  1182. });
  1183. // Set the initial state
  1184. this.data.current(function (initialData) {
  1185. self.trigger('selection:update', {
  1186. data: initialData
  1187. });
  1188. });
  1189. this.on('query', function (params) {
  1190. this.data.query(params, function (data) {
  1191. self.trigger('results:all', {
  1192. data: data,
  1193. query: params
  1194. });
  1195. });
  1196. });
  1197. this.trigger('query', {});
  1198. // Hide the original select
  1199. $element.hide();
  1200. };
  1201. Utils.Extend(Select2, Utils.Observable);
  1202. Select2.prototype.toggleDropdown = function () {
  1203. if (this.$container.hasClass('open')) {
  1204. this.trigger('close');
  1205. } else {
  1206. this.trigger('open');
  1207. }
  1208. };
  1209. Select2.prototype.render = function () {
  1210. var $container = $(
  1211. '<span class="select2 select2-container select2-theme-default">' +
  1212. '<span class="selection"></span>' +
  1213. '<span class="dropdown-wrapper"></span>' +
  1214. '</span>'
  1215. );
  1216. return $container;
  1217. };
  1218. return Select2;
  1219. });
  1220. define('jquery.select2',[
  1221. 'jquery',
  1222. 'select2/core'
  1223. ], function ($, Select2) {
  1224. if ($.fn.select2 == null) {
  1225. $.fn.select2 = function (options) {
  1226. options = options || {};
  1227. if (typeof options === 'object') {
  1228. this.each(function () {
  1229. var instance = new Select2($(this), options);
  1230. });
  1231. } else if (typeof options === 'string') {
  1232. var instance = this.data('select2');
  1233. instance[options](arguments.slice(1));
  1234. } else {
  1235. throw new Error('Invalid arguments for Select2: ' + options);
  1236. }
  1237. };
  1238. }
  1239. return Select2;
  1240. });
  1241. require('jquery.select2');