You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

6554 lines
229 KiB

2 years ago
  1. /*!
  2. * Select2 4.0.6-rc.1
  3. * https://select2.github.io
  4. *
  5. * Released under the MIT license
  6. * https://github.com/select2/select2/blob/master/LICENSE.md
  7. */
  8. ; (function (factory) {
  9. if (typeof define === 'function' && define.amd) {
  10. // AMD. Register as an anonymous module.
  11. define(['jquery'], factory);
  12. } else if (typeof module === 'object' && module.exports) {
  13. // Node/CommonJS
  14. module.exports = function (root, jQuery) {
  15. if (jQuery === undefined) {
  16. // require('jQuery') returns a factory that requires window to
  17. // build a jQuery instance, we normalize how we use modules
  18. // that require this pattern but the window provided is a noop
  19. // if it's defined (how jquery works)
  20. if (typeof window !== 'undefined') {
  21. jQuery = require('jquery');
  22. }
  23. else {
  24. jQuery = require('jquery')(root);
  25. }
  26. }
  27. factory(jQuery);
  28. return jQuery;
  29. };
  30. } else {
  31. // Browser globals
  32. factory(jQuery);
  33. }
  34. }(function (jQuery) {
  35. // This is needed so we can catch the AMD loader configuration and use it
  36. // The inner file should be wrapped (by `banner.start.js`) in a function that
  37. // returns the AMD loader references.
  38. var S2 = (function () {
  39. // Restore the Select2 AMD loader so it can be used
  40. // Needed mostly in the language files, where the loader is not inserted
  41. if (jQuery && jQuery.fn && jQuery.fn.select2 && jQuery.fn.select2.amd) {
  42. var S2 = jQuery.fn.select2.amd;
  43. }
  44. var S2; (function () {
  45. if (!S2 || !S2.requirejs) {
  46. if (!S2) { S2 = {}; } else { require = S2; }
  47. /**
  48. * @license almond 0.3.3 Copyright jQuery Foundation and other contributors.
  49. * Released under MIT license, http://github.com/requirejs/almond/LICENSE
  50. */
  51. //Going sloppy to avoid 'use strict' string cost, but strict practices should
  52. //be followed.
  53. /*global setTimeout: false */
  54. var requirejs, require, define;
  55. (function (undef) {
  56. var main, req, makeMap, handlers,
  57. defined = {},
  58. waiting = {},
  59. config = {},
  60. defining = {},
  61. hasOwn = Object.prototype.hasOwnProperty,
  62. aps = [].slice,
  63. jsSuffixRegExp = /\.js$/;
  64. function hasProp(obj, prop) {
  65. return hasOwn.call(obj, prop);
  66. }
  67. /**
  68. * Given a relative module name, like ./something, normalize it to
  69. * a real name that can be mapped to a path.
  70. * @param {String} name the relative name
  71. * @param {String} baseName a real name that the name arg is relative
  72. * to.
  73. * @returns {String} normalized name
  74. */
  75. function normalize(name, baseName) {
  76. var nameParts, nameSegment, mapValue, foundMap, lastIndex,
  77. foundI, foundStarMap, starI, i, j, part, normalizedBaseParts,
  78. baseParts = baseName && baseName.split("/"),
  79. map = config.map,
  80. starMap = (map && map['*']) || {};
  81. //Adjust any relative paths.
  82. if (name) {
  83. name = name.split('/');
  84. lastIndex = name.length - 1;
  85. // If wanting node ID compatibility, strip .js from end
  86. // of IDs. Have to do this here, and not in nameToUrl
  87. // because node allows either .js or non .js to map
  88. // to same file.
  89. if (config.nodeIdCompat && jsSuffixRegExp.test(name[lastIndex])) {
  90. name[lastIndex] = name[lastIndex].replace(jsSuffixRegExp, '');
  91. }
  92. // Starts with a '.' so need the baseName
  93. if (name[0].charAt(0) === '.' && baseParts) {
  94. //Convert baseName to array, and lop off the last part,
  95. //so that . matches that 'directory' and not name of the baseName's
  96. //module. For instance, baseName of 'one/two/three', maps to
  97. //'one/two/three.js', but we want the directory, 'one/two' for
  98. //this normalization.
  99. normalizedBaseParts = baseParts.slice(0, baseParts.length - 1);
  100. name = normalizedBaseParts.concat(name);
  101. }
  102. //start trimDots
  103. for (i = 0; i < name.length; i++) {
  104. part = name[i];
  105. if (part === '.') {
  106. name.splice(i, 1);
  107. i -= 1;
  108. } else if (part === '..') {
  109. // If at the start, or previous value is still ..,
  110. // keep them so that when converted to a path it may
  111. // still work when converted to a path, even though
  112. // as an ID it is less than ideal. In larger point
  113. // releases, may be better to just kick out an error.
  114. if (i === 0 || (i === 1 && name[2] === '..') || name[i - 1] === '..') {
  115. continue;
  116. } else if (i > 0) {
  117. name.splice(i - 1, 2);
  118. i -= 2;
  119. }
  120. }
  121. }
  122. //end trimDots
  123. name = name.join('/');
  124. }
  125. //Apply map config if available.
  126. if ((baseParts || starMap) && map) {
  127. nameParts = name.split('/');
  128. for (i = nameParts.length; i > 0; i -= 1) {
  129. nameSegment = nameParts.slice(0, i).join("/");
  130. if (baseParts) {
  131. //Find the longest baseName segment match in the config.
  132. //So, do joins on the biggest to smallest lengths of baseParts.
  133. for (j = baseParts.length; j > 0; j -= 1) {
  134. mapValue = map[baseParts.slice(0, j).join('/')];
  135. //baseName segment has config, find if it has one for
  136. //this name.
  137. if (mapValue) {
  138. mapValue = mapValue[nameSegment];
  139. if (mapValue) {
  140. //Match, update name to the new value.
  141. foundMap = mapValue;
  142. foundI = i;
  143. break;
  144. }
  145. }
  146. }
  147. }
  148. if (foundMap) {
  149. break;
  150. }
  151. //Check for a star map match, but just hold on to it,
  152. //if there is a shorter segment match later in a matching
  153. //config, then favor over this star map.
  154. if (!foundStarMap && starMap && starMap[nameSegment]) {
  155. foundStarMap = starMap[nameSegment];
  156. starI = i;
  157. }
  158. }
  159. if (!foundMap && foundStarMap) {
  160. foundMap = foundStarMap;
  161. foundI = starI;
  162. }
  163. if (foundMap) {
  164. nameParts.splice(0, foundI, foundMap);
  165. name = nameParts.join('/');
  166. }
  167. }
  168. return name;
  169. }
  170. function makeRequire(relName, forceSync) {
  171. return function () {
  172. //A version of a require function that passes a moduleName
  173. //value for items that may need to
  174. //look up paths relative to the moduleName
  175. var args = aps.call(arguments, 0);
  176. //If first arg is not require('string'), and there is only
  177. //one arg, it is the array form without a callback. Insert
  178. //a null so that the following concat is correct.
  179. if (typeof args[0] !== 'string' && args.length === 1) {
  180. args.push(null);
  181. }
  182. return req.apply(undef, args.concat([relName, forceSync]));
  183. };
  184. }
  185. function makeNormalize(relName) {
  186. return function (name) {
  187. return normalize(name, relName);
  188. };
  189. }
  190. function makeLoad(depName) {
  191. return function (value) {
  192. defined[depName] = value;
  193. };
  194. }
  195. function callDep(name) {
  196. if (hasProp(waiting, name)) {
  197. var args = waiting[name];
  198. delete waiting[name];
  199. defining[name] = true;
  200. main.apply(undef, args);
  201. }
  202. if (!hasProp(defined, name) && !hasProp(defining, name)) {
  203. throw new Error('No ' + name);
  204. }
  205. return defined[name];
  206. }
  207. //Turns a plugin!resource to [plugin, resource]
  208. //with the plugin being undefined if the name
  209. //did not have a plugin prefix.
  210. function splitPrefix(name) {
  211. var prefix,
  212. index = name ? name.indexOf('!') : -1;
  213. if (index > -1) {
  214. prefix = name.substring(0, index);
  215. name = name.substring(index + 1, name.length);
  216. }
  217. return [prefix, name];
  218. }
  219. //Creates a parts array for a relName where first part is plugin ID,
  220. //second part is resource ID. Assumes relName has already been normalized.
  221. function makeRelParts(relName) {
  222. return relName ? splitPrefix(relName) : [];
  223. }
  224. /**
  225. * Makes a name map, normalizing the name, and using a plugin
  226. * for normalization if necessary. Grabs a ref to plugin
  227. * too, as an optimization.
  228. */
  229. makeMap = function (name, relParts) {
  230. var plugin,
  231. parts = splitPrefix(name),
  232. prefix = parts[0],
  233. relResourceName = relParts[1];
  234. name = parts[1];
  235. if (prefix) {
  236. prefix = normalize(prefix, relResourceName);
  237. plugin = callDep(prefix);
  238. }
  239. //Normalize according
  240. if (prefix) {
  241. if (plugin && plugin.normalize) {
  242. name = plugin.normalize(name, makeNormalize(relResourceName));
  243. } else {
  244. name = normalize(name, relResourceName);
  245. }
  246. } else {
  247. name = normalize(name, relResourceName);
  248. parts = splitPrefix(name);
  249. prefix = parts[0];
  250. name = parts[1];
  251. if (prefix) {
  252. plugin = callDep(prefix);
  253. }
  254. }
  255. //Using ridiculous property names for space reasons
  256. return {
  257. f: prefix ? prefix + '!' + name : name, //fullName
  258. n: name,
  259. pr: prefix,
  260. p: plugin
  261. };
  262. };
  263. function makeConfig(name) {
  264. return function () {
  265. return (config && config.config && config.config[name]) || {};
  266. };
  267. }
  268. handlers = {
  269. require: function (name) {
  270. return makeRequire(name);
  271. },
  272. exports: function (name) {
  273. var e = defined[name];
  274. if (typeof e !== 'undefined') {
  275. return e;
  276. } else {
  277. return (defined[name] = {});
  278. }
  279. },
  280. module: function (name) {
  281. return {
  282. id: name,
  283. uri: '',
  284. exports: defined[name],
  285. config: makeConfig(name)
  286. };
  287. }
  288. };
  289. main = function (name, deps, callback, relName) {
  290. var cjsModule, depName, ret, map, i, relParts,
  291. args = [],
  292. callbackType = typeof callback,
  293. usingExports;
  294. //Use name if no relName
  295. relName = relName || name;
  296. relParts = makeRelParts(relName);
  297. //Call the callback to define the module, if necessary.
  298. if (callbackType === 'undefined' || callbackType === 'function') {
  299. //Pull out the defined dependencies and pass the ordered
  300. //values to the callback.
  301. //Default to [require, exports, module] if no deps
  302. deps = !deps.length && callback.length ? ['require', 'exports', 'module'] : deps;
  303. for (i = 0; i < deps.length; i += 1) {
  304. map = makeMap(deps[i], relParts);
  305. depName = map.f;
  306. //Fast path CommonJS standard dependencies.
  307. if (depName === "require") {
  308. args[i] = handlers.require(name);
  309. } else if (depName === "exports") {
  310. //CommonJS module spec 1.1
  311. args[i] = handlers.exports(name);
  312. usingExports = true;
  313. } else if (depName === "module") {
  314. //CommonJS module spec 1.1
  315. cjsModule = args[i] = handlers.module(name);
  316. } else if (hasProp(defined, depName) ||
  317. hasProp(waiting, depName) ||
  318. hasProp(defining, depName)) {
  319. args[i] = callDep(depName);
  320. } else if (map.p) {
  321. map.p.load(map.n, makeRequire(relName, true), makeLoad(depName), {});
  322. args[i] = defined[depName];
  323. } else {
  324. throw new Error(name + ' missing ' + depName);
  325. }
  326. }
  327. ret = callback ? callback.apply(defined[name], args) : undefined;
  328. if (name) {
  329. //If setting exports via "module" is in play,
  330. //favor that over return value and exports. After that,
  331. //favor a non-undefined return value over exports use.
  332. if (cjsModule && cjsModule.exports !== undef &&
  333. cjsModule.exports !== defined[name]) {
  334. defined[name] = cjsModule.exports;
  335. } else if (ret !== undef || !usingExports) {
  336. //Use the return value from the function.
  337. defined[name] = ret;
  338. }
  339. }
  340. } else if (name) {
  341. //May just be an object definition for the module. Only
  342. //worry about defining if have a module name.
  343. defined[name] = callback;
  344. }
  345. };
  346. requirejs = require = req = function (deps, callback, relName, forceSync, alt) {
  347. if (typeof deps === "string") {
  348. if (handlers[deps]) {
  349. //callback in this case is really relName
  350. return handlers[deps](callback);
  351. }
  352. //Just return the module wanted. In this scenario, the
  353. //deps arg is the module name, and second arg (if passed)
  354. //is just the relName.
  355. //Normalize module name, if it contains . or ..
  356. return callDep(makeMap(deps, makeRelParts(callback)).f);
  357. } else if (!deps.splice) {
  358. //deps is a config object, not an array.
  359. config = deps;
  360. if (config.deps) {
  361. req(config.deps, config.callback);
  362. }
  363. if (!callback) {
  364. return;
  365. }
  366. if (callback.splice) {
  367. //callback is an array, which means it is a dependency list.
  368. //Adjust args if there are dependencies
  369. deps = callback;
  370. callback = relName;
  371. relName = null;
  372. } else {
  373. deps = undef;
  374. }
  375. }
  376. //Support require(['a'])
  377. callback = callback || function () { };
  378. //If relName is a function, it is an errback handler,
  379. //so remove it.
  380. if (typeof relName === 'function') {
  381. relName = forceSync;
  382. forceSync = alt;
  383. }
  384. //Simulate async callback;
  385. if (forceSync) {
  386. main(undef, deps, callback, relName);
  387. } else {
  388. //Using a non-zero value because of concern for what old browsers
  389. //do, and latest browsers "upgrade" to 4 if lower value is used:
  390. //http://www.whatwg.org/specs/web-apps/current-work/multipage/timers.html#dom-windowtimers-settimeout:
  391. //If want a value immediately, use require('id') instead -- something
  392. //that works in almond on the global level, but not guaranteed and
  393. //unlikely to work in other AMD implementations.
  394. setTimeout(function () {
  395. main(undef, deps, callback, relName);
  396. }, 4);
  397. }
  398. return req;
  399. };
  400. /**
  401. * Just drops the config on the floor, but returns req in case
  402. * the config return value is used.
  403. */
  404. req.config = function (cfg) {
  405. return req(cfg);
  406. };
  407. /**
  408. * Expose module registry for debugging and tooling
  409. */
  410. requirejs._defined = defined;
  411. define = function (name, deps, callback) {
  412. if (typeof name !== 'string') {
  413. throw new Error('See almond README: incorrect module build, no module name');
  414. }
  415. //This module may not have dependencies
  416. if (!deps.splice) {
  417. //deps is not an array, so probably means
  418. //an object literal or factory function for
  419. //the value. Adjust args.
  420. callback = deps;
  421. deps = [];
  422. }
  423. if (!hasProp(defined, name) && !hasProp(waiting, name)) {
  424. waiting[name] = [name, deps, callback];
  425. }
  426. };
  427. define.amd = {
  428. jQuery: true
  429. };
  430. }());
  431. S2.requirejs = requirejs; S2.require = require; S2.define = define;
  432. }
  433. }());
  434. S2.define("almond", function () { });
  435. /* global jQuery:false, $:false */
  436. S2.define('jquery', [], function () {
  437. var _$ = jQuery || $;
  438. if (_$ == null && console && console.error) {
  439. console.error(
  440. 'Select2: An instance of jQuery or a jQuery-compatible library was not ' +
  441. 'found. Make sure that you are including jQuery before Select2 on your ' +
  442. 'web page.'
  443. );
  444. }
  445. return _$;
  446. });
  447. S2.define('select2/utils', [
  448. 'jquery'
  449. ], function ($) {
  450. var Utils = {};
  451. Utils.Extend = function (ChildClass, SuperClass) {
  452. var __hasProp = {}.hasOwnProperty;
  453. function BaseConstructor() {
  454. this.constructor = ChildClass;
  455. }
  456. for (var key in SuperClass) {
  457. if (__hasProp.call(SuperClass, key)) {
  458. ChildClass[key] = SuperClass[key];
  459. }
  460. }
  461. BaseConstructor.prototype = SuperClass.prototype;
  462. ChildClass.prototype = new BaseConstructor();
  463. ChildClass.__super__ = SuperClass.prototype;
  464. return ChildClass;
  465. };
  466. function getMethods(theClass) {
  467. var proto = theClass.prototype;
  468. var methods = [];
  469. for (var methodName in proto) {
  470. var m = proto[methodName];
  471. if (typeof m !== 'function') {
  472. continue;
  473. }
  474. if (methodName === 'constructor') {
  475. continue;
  476. }
  477. methods.push(methodName);
  478. }
  479. return methods;
  480. }
  481. Utils.Decorate = function (SuperClass, DecoratorClass) {
  482. var decoratedMethods = getMethods(DecoratorClass);
  483. var superMethods = getMethods(SuperClass);
  484. function DecoratedClass() {
  485. var unshift = Array.prototype.unshift;
  486. var argCount = DecoratorClass.prototype.constructor.length;
  487. var calledConstructor = SuperClass.prototype.constructor;
  488. if (argCount > 0) {
  489. unshift.call(arguments, SuperClass.prototype.constructor);
  490. calledConstructor = DecoratorClass.prototype.constructor;
  491. }
  492. calledConstructor.apply(this, arguments);
  493. }
  494. DecoratorClass.displayName = SuperClass.displayName;
  495. function ctr() {
  496. this.constructor = DecoratedClass;
  497. }
  498. DecoratedClass.prototype = new ctr();
  499. for (var m = 0; m < superMethods.length; m++) {
  500. var superMethod = superMethods[m];
  501. DecoratedClass.prototype[superMethod] =
  502. SuperClass.prototype[superMethod];
  503. }
  504. var calledMethod = function (methodName) {
  505. // Stub out the original method if it's not decorating an actual method
  506. var originalMethod = function () { };
  507. if (methodName in DecoratedClass.prototype) {
  508. originalMethod = DecoratedClass.prototype[methodName];
  509. }
  510. var decoratedMethod = DecoratorClass.prototype[methodName];
  511. return function () {
  512. var unshift = Array.prototype.unshift;
  513. unshift.call(arguments, originalMethod);
  514. return decoratedMethod.apply(this, arguments);
  515. };
  516. };
  517. for (var d = 0; d < decoratedMethods.length; d++) {
  518. var decoratedMethod = decoratedMethods[d];
  519. DecoratedClass.prototype[decoratedMethod] = calledMethod(decoratedMethod);
  520. }
  521. return DecoratedClass;
  522. };
  523. var Observable = function () {
  524. this.listeners = {};
  525. };
  526. Observable.prototype.on = function (event, callback) {
  527. this.listeners = this.listeners || {};
  528. if (event in this.listeners) {
  529. this.listeners[event].push(callback);
  530. } else {
  531. this.listeners[event] = [callback];
  532. }
  533. };
  534. Observable.prototype.trigger = function (event) {
  535. var slice = Array.prototype.slice;
  536. var params = slice.call(arguments, 1);
  537. this.listeners = this.listeners || {};
  538. // Params should always come in as an array
  539. if (params == null) {
  540. params = [];
  541. }
  542. // If there are no arguments to the event, use a temporary object
  543. if (params.length === 0) {
  544. params.push({});
  545. }
  546. // Set the `_type` of the first object to the event
  547. params[0]._type = event;
  548. if (event in this.listeners) {
  549. this.invoke(this.listeners[event], slice.call(arguments, 1));
  550. }
  551. if ('*' in this.listeners) {
  552. this.invoke(this.listeners['*'], arguments);
  553. }
  554. };
  555. Observable.prototype.invoke = function (listeners, params) {
  556. for (var i = 0, len = listeners.length; i < len; i++) {
  557. listeners[i].apply(this, params);
  558. }
  559. };
  560. Utils.Observable = Observable;
  561. Utils.generateChars = function (length) {
  562. var chars = '';
  563. for (var i = 0; i < length; i++) {
  564. var randomChar = Math.floor(Math.random() * 36);
  565. chars += randomChar.toString(36);
  566. }
  567. return chars;
  568. };
  569. Utils.bind = function (func, context) {
  570. return function () {
  571. func.apply(context, arguments);
  572. };
  573. };
  574. Utils._convertData = function (data) {
  575. for (var originalKey in data) {
  576. var keys = originalKey.split('-');
  577. var dataLevel = data;
  578. if (keys.length === 1) {
  579. continue;
  580. }
  581. for (var k = 0; k < keys.length; k++) {
  582. var key = keys[k];
  583. // Lowercase the first letter
  584. // By default, dash-separated becomes camelCase
  585. key = key.substring(0, 1).toLowerCase() + key.substring(1);
  586. if (!(key in dataLevel)) {
  587. dataLevel[key] = {};
  588. }
  589. if (k == keys.length - 1) {
  590. dataLevel[key] = data[originalKey];
  591. }
  592. dataLevel = dataLevel[key];
  593. }
  594. delete data[originalKey];
  595. }
  596. return data;
  597. };
  598. Utils.hasScroll = function (index, el) {
  599. // Adapted from the function created by @ShadowScripter
  600. // and adapted by @BillBarry on the Stack Exchange Code Review website.
  601. // The original code can be found at
  602. // http://codereview.stackexchange.com/q/13338
  603. // and was designed to be used with the Sizzle selector engine.
  604. var $el = $(el);
  605. var overflowX = el.style.overflowX;
  606. var overflowY = el.style.overflowY;
  607. //Check both x and y declarations
  608. if (overflowX === overflowY &&
  609. (overflowY === 'hidden' || overflowY === 'visible')) {
  610. return false;
  611. }
  612. if (overflowX === 'scroll' || overflowY === 'scroll') {
  613. return true;
  614. }
  615. return ($el.innerHeight() < el.scrollHeight ||
  616. $el.innerWidth() < el.scrollWidth);
  617. };
  618. Utils.escapeMarkup = function (markup) {
  619. var replaceMap = {
  620. '\\': '&#92;',
  621. '&': '&amp;',
  622. '<': '&lt;',
  623. '>': '&gt;',
  624. '"': '&quot;',
  625. '\'': '&#39;',
  626. '/': '&#47;'
  627. };
  628. // Do not try to escape the markup if it's not a string
  629. if (typeof markup !== 'string') {
  630. return markup;
  631. }
  632. return String(markup).replace(/[&<>"'\/\\]/g, function (match) {
  633. return replaceMap[match];
  634. });
  635. };
  636. // Append an array of jQuery nodes to a given element.
  637. Utils.appendMany = function ($element, $nodes) {
  638. // jQuery 1.7.x does not support $.fn.append() with an array
  639. // Fall back to a jQuery object collection using $.fn.add()
  640. if ($.fn.jquery.substr(0, 3) === '1.7') {
  641. var $jqNodes = $();
  642. $.map($nodes, function (node) {
  643. $jqNodes = $jqNodes.add(node);
  644. });
  645. $nodes = $jqNodes;
  646. }
  647. $element.append($nodes);
  648. };
  649. // Cache objects in Utils.__cache instead of $.data (see #4346)
  650. Utils.__cache = {};
  651. var id = 0;
  652. Utils.GetUniqueElementId = function (element) {
  653. // Get a unique element Id. If element has no id,
  654. // creates a new unique number, stores it in the id
  655. // attribute and returns the new id.
  656. // If an id already exists, it simply returns it.
  657. var select2Id = element.getAttribute('data-select2-id');
  658. if (select2Id == null) {
  659. // If element has id, use it.
  660. if (element.id) {
  661. select2Id = element.id;
  662. element.setAttribute('data-select2-id', select2Id);
  663. } else {
  664. element.setAttribute('data-select2-id', ++id);
  665. select2Id = id.toString();
  666. }
  667. }
  668. return select2Id;
  669. };
  670. Utils.StoreData = function (element, name, value) {
  671. // Stores an item in the cache for a specified element.
  672. // name is the cache key.
  673. var id = Utils.GetUniqueElementId(element);
  674. if (!Utils.__cache[id]) {
  675. Utils.__cache[id] = {};
  676. }
  677. Utils.__cache[id][name] = value;
  678. };
  679. Utils.GetData = function (element, name) {
  680. // Retrieves a value from the cache by its key (name)
  681. // name is optional. If no name specified, return
  682. // all cache items for the specified element.
  683. // and for a specified element.
  684. var id = Utils.GetUniqueElementId(element);
  685. if (name) {
  686. if (Utils.__cache[id]) {
  687. return Utils.__cache[id][name] != null ?
  688. Utils.__cache[id][name] :
  689. $(element).data(name); // Fallback to HTML5 data attribs.
  690. }
  691. return $(element).data(name); // Fallback to HTML5 data attribs.
  692. } else {
  693. return Utils.__cache[id];
  694. }
  695. };
  696. Utils.RemoveData = function (element) {
  697. // Removes all cached items for a specified element.
  698. var id = Utils.GetUniqueElementId(element);
  699. if (Utils.__cache[id] != null) {
  700. delete Utils.__cache[id];
  701. }
  702. };
  703. return Utils;
  704. });
  705. S2.define('select2/results', [
  706. 'jquery',
  707. './utils'
  708. ], function ($, Utils) {
  709. function Results($element, options, dataAdapter) {
  710. this.$element = $element;
  711. this.data = dataAdapter;
  712. this.options = options;
  713. Results.__super__.constructor.call(this);
  714. }
  715. Utils.Extend(Results, Utils.Observable);
  716. Results.prototype.render = function () {
  717. var $results = $(
  718. '<ul class="select2-results__options" role="tree"></ul>'
  719. );
  720. if (this.options.get('multiple')) {
  721. $results.attr('aria-multiselectable', 'true');
  722. }
  723. this.$results = $results;
  724. return $results;
  725. };
  726. Results.prototype.clear = function () {
  727. this.$results.empty();
  728. };
  729. Results.prototype.displayMessage = function (params) {
  730. var escapeMarkup = this.options.get('escapeMarkup');
  731. this.clear();
  732. this.hideLoading();
  733. var $message = $(
  734. '<li role="treeitem" aria-live="assertive"' +
  735. ' class="select2-results__option"></li>'
  736. );
  737. var message = this.options.get('translations').get(params.message);
  738. $message.append(
  739. escapeMarkup(
  740. message(params.args)
  741. )
  742. );
  743. $message[0].className += ' select2-results__message';
  744. this.$results.append($message);
  745. };
  746. Results.prototype.hideMessages = function () {
  747. this.$results.find('.select2-results__message').remove();
  748. };
  749. Results.prototype.append = function (data) {
  750. this.hideLoading();
  751. var $options = [];
  752. if (data.results == null || data.results.length === 0) {
  753. if (this.$results.children().length === 0) {
  754. this.trigger('results:message', {
  755. message: 'noResults'
  756. });
  757. }
  758. return;
  759. }
  760. data.results = this.sort(data.results);
  761. for (var d = 0; d < data.results.length; d++) {
  762. var item = data.results[d];
  763. var $option = this.option(item);
  764. $options.push($option);
  765. }
  766. this.$results.append($options);
  767. };
  768. Results.prototype.position = function ($results, $dropdown) {
  769. var $resultsContainer = $dropdown.find('.select2-results');
  770. $resultsContainer.append($results);
  771. };
  772. Results.prototype.sort = function (data) {
  773. var sorter = this.options.get('sorter');
  774. return sorter(data);
  775. };
  776. Results.prototype.highlightFirstItem = function () {
  777. var $options = this.$results
  778. .find('.select2-results__option[aria-selected]');
  779. var $selected = $options.filter('[aria-selected=true]');
  780. // Check if there are any selected options
  781. if ($selected.length > 0) {
  782. // If there are selected options, highlight the first
  783. $selected.first().trigger('mouseenter');
  784. } else {
  785. // If there are no selected options, highlight the first option
  786. // in the dropdown
  787. $options.first().trigger('mouseenter');
  788. }
  789. this.ensureHighlightVisible();
  790. };
  791. Results.prototype.setClasses = function () {
  792. var self = this;
  793. this.data.current(function (selected) {
  794. var selectedIds = $.map(selected, function (s) {
  795. return s.id.toString();
  796. });
  797. var $options = self.$results
  798. .find('.select2-results__option[aria-selected]');
  799. $options.each(function () {
  800. var $option = $(this);
  801. var item = Utils.GetData(this, 'data');
  802. // id needs to be converted to a string when comparing
  803. var id = '' + item.id;
  804. if ((item.element != null && item.element.selected) ||
  805. (item.element == null && $.inArray(id, selectedIds) > -1)) {
  806. $option.attr('aria-selected', 'true');
  807. } else {
  808. $option.attr('aria-selected', 'false');
  809. }
  810. });
  811. });
  812. };
  813. Results.prototype.showLoading = function (params) {
  814. this.hideLoading();
  815. var loadingMore = this.options.get('translations').get('searching');
  816. var loading = {
  817. disabled: true,
  818. loading: true,
  819. text: loadingMore(params)
  820. };
  821. var $loading = this.option(loading);
  822. $loading.className += ' loading-results';
  823. this.$results.prepend($loading);
  824. };
  825. Results.prototype.hideLoading = function () {
  826. this.$results.find('.loading-results').remove();
  827. };
  828. Results.prototype.option = function (data) {
  829. var option = document.createElement('li');
  830. option.className = 'select2-results__option';
  831. var attrs = {
  832. 'role': 'treeitem',
  833. 'aria-selected': 'false'
  834. };
  835. if (data.disabled) {
  836. delete attrs['aria-selected'];
  837. attrs['aria-disabled'] = 'true';
  838. }
  839. if (data.id == null) {
  840. delete attrs['aria-selected'];
  841. }
  842. if (data._resultId != null) {
  843. option.id = data._resultId;
  844. }
  845. if (data.title) {
  846. option.title = data.title;
  847. }
  848. if (data.children) {
  849. attrs.role = 'group';
  850. attrs['aria-label'] = data.text;
  851. delete attrs['aria-selected'];
  852. }
  853. for (var attr in attrs) {
  854. var val = attrs[attr];
  855. option.setAttribute(attr, val);
  856. }
  857. if (data.children) {
  858. var $option = $(option);
  859. var label = document.createElement('strong');
  860. label.className = 'select2-results__group';
  861. var $label = $(label);
  862. this.template(data, label);
  863. var $children = [];
  864. for (var c = 0; c < data.children.length; c++) {
  865. var child = data.children[c];
  866. var $child = this.option(child);
  867. $children.push($child);
  868. }
  869. var $childrenContainer = $('<ul></ul>', {
  870. 'class': 'select2-results__options select2-results__options--nested'
  871. });
  872. $childrenContainer.append($children);
  873. $option.append(label);
  874. $option.append($childrenContainer);
  875. } else {
  876. this.template(data, option);
  877. }
  878. Utils.StoreData(option, 'data', data);
  879. return option;
  880. };
  881. Results.prototype.bind = function (container, $container) {
  882. var self = this;
  883. var id = container.id + '-results';
  884. this.$results.attr('id', id);
  885. container.on('results:all', function (params) {
  886. self.clear();
  887. self.append(params.data);
  888. if (container.isOpen()) {
  889. self.setClasses();
  890. self.highlightFirstItem();
  891. }
  892. });
  893. container.on('results:append', function (params) {
  894. self.append(params.data);
  895. if (container.isOpen()) {
  896. self.setClasses();
  897. }
  898. });
  899. container.on('query', function (params) {
  900. self.hideMessages();
  901. self.showLoading(params);
  902. });
  903. container.on('select', function () {
  904. if (!container.isOpen()) {
  905. return;
  906. }
  907. self.setClasses();
  908. self.highlightFirstItem();
  909. });
  910. container.on('unselect', function () {
  911. if (!container.isOpen()) {
  912. return;
  913. }
  914. self.setClasses();
  915. self.highlightFirstItem();
  916. });
  917. container.on('open', function () {
  918. // When the dropdown is open, aria-expended="true"
  919. self.$results.attr('aria-expanded', 'true');
  920. self.$results.attr('aria-hidden', 'false');
  921. self.setClasses();
  922. self.ensureHighlightVisible();
  923. });
  924. container.on('close', function () {
  925. // When the dropdown is closed, aria-expended="false"
  926. self.$results.attr('aria-expanded', 'false');
  927. self.$results.attr('aria-hidden', 'true');
  928. self.$results.removeAttr('aria-activedescendant');
  929. });
  930. container.on('results:toggle', function () {
  931. var $highlighted = self.getHighlightedResults();
  932. if ($highlighted.length === 0) {
  933. return;
  934. }
  935. $highlighted.trigger('mouseup');
  936. });
  937. container.on('results:select', function () {
  938. var $highlighted = self.getHighlightedResults();
  939. if ($highlighted.length === 0) {
  940. return;
  941. }
  942. var data = Utils.GetData($highlighted[0], 'data');
  943. if ($highlighted.attr('aria-selected') == 'true') {
  944. self.trigger('close', {});
  945. } else {
  946. self.trigger('select', {
  947. data: data
  948. });
  949. }
  950. });
  951. container.on('results:previous', function () {
  952. var $highlighted = self.getHighlightedResults();
  953. var $options = self.$results.find('[aria-selected]');
  954. var currentIndex = $options.index($highlighted);
  955. // If we are already at te top, don't move further
  956. // If no options, currentIndex will be -1
  957. if (currentIndex <= 0) {
  958. return;
  959. }
  960. var nextIndex = currentIndex - 1;
  961. // If none are highlighted, highlight the first
  962. if ($highlighted.length === 0) {
  963. nextIndex = 0;
  964. }
  965. var $next = $options.eq(nextIndex);
  966. $next.trigger('mouseenter');
  967. var currentOffset = self.$results.offset().top;
  968. var nextTop = $next.offset().top;
  969. var nextOffset = self.$results.scrollTop() + (nextTop - currentOffset);
  970. if (nextIndex === 0) {
  971. self.$results.scrollTop(0);
  972. } else if (nextTop - currentOffset < 0) {
  973. self.$results.scrollTop(nextOffset);
  974. }
  975. });
  976. container.on('results:next', function () {
  977. var $highlighted = self.getHighlightedResults();
  978. var $options = self.$results.find('[aria-selected]');
  979. var currentIndex = $options.index($highlighted);
  980. var nextIndex = currentIndex + 1;
  981. // If we are at the last option, stay there
  982. if (nextIndex >= $options.length) {
  983. return;
  984. }
  985. var $next = $options.eq(nextIndex);
  986. $next.trigger('mouseenter');
  987. var currentOffset = self.$results.offset().top +
  988. self.$results.outerHeight(false);
  989. var nextBottom = $next.offset().top + $next.outerHeight(false);
  990. var nextOffset = self.$results.scrollTop() + nextBottom - currentOffset;
  991. if (nextIndex === 0) {
  992. self.$results.scrollTop(0);
  993. } else if (nextBottom > currentOffset) {
  994. self.$results.scrollTop(nextOffset);
  995. }
  996. });
  997. container.on('results:focus', function (params) {
  998. params.element.addClass('select2-results__option--highlighted');
  999. });
  1000. container.on('results:message', function (params) {
  1001. self.displayMessage(params);
  1002. });
  1003. if ($.fn.mousewheel) {
  1004. this.$results.on('mousewheel', function (e) {
  1005. var top = self.$results.scrollTop();
  1006. var bottom = self.$results.get(0).scrollHeight - top + e.deltaY;
  1007. var isAtTop = e.deltaY > 0 && top - e.deltaY <= 0;
  1008. var isAtBottom = e.deltaY < 0 && bottom <= self.$results.height();
  1009. if (isAtTop) {
  1010. self.$results.scrollTop(0);
  1011. e.preventDefault();
  1012. e.stopPropagation();
  1013. } else if (isAtBottom) {
  1014. self.$results.scrollTop(
  1015. self.$results.get(0).scrollHeight - self.$results.height()
  1016. );
  1017. e.preventDefault();
  1018. e.stopPropagation();
  1019. }
  1020. });
  1021. }
  1022. this.$results.on('mouseup', '.select2-results__option[aria-selected]',
  1023. function (evt) {
  1024. var $this = $(this);
  1025. var data = Utils.GetData(this, 'data');
  1026. if ($this.attr('aria-selected') === 'true') {
  1027. if (self.options.get('multiple')) {
  1028. self.trigger('unselect', {
  1029. originalEvent: evt,
  1030. data: data
  1031. });
  1032. } else {
  1033. self.trigger('close', {});
  1034. }
  1035. return;
  1036. }
  1037. self.trigger('select', {
  1038. originalEvent: evt,
  1039. data: data
  1040. });
  1041. });
  1042. this.$results.on('mouseenter', '.select2-results__option[aria-selected]',
  1043. function (evt) {
  1044. var data = Utils.GetData(this, 'data');
  1045. self.getHighlightedResults()
  1046. .removeClass('select2-results__option--highlighted');
  1047. self.trigger('results:focus', {
  1048. data: data,
  1049. element: $(this)
  1050. });
  1051. });
  1052. };
  1053. Results.prototype.getHighlightedResults = function () {
  1054. var $highlighted = this.$results
  1055. .find('.select2-results__option--highlighted');
  1056. return $highlighted;
  1057. };
  1058. Results.prototype.destroy = function () {
  1059. this.$results.remove();
  1060. };
  1061. Results.prototype.ensureHighlightVisible = function () {
  1062. var $highlighted = this.getHighlightedResults();
  1063. if ($highlighted.length === 0) {
  1064. return;
  1065. }
  1066. var $options = this.$results.find('[aria-selected]');
  1067. var currentIndex = $options.index($highlighted);
  1068. var currentOffset = this.$results.offset().top;
  1069. var nextTop = $highlighted.offset().top;
  1070. var nextOffset = this.$results.scrollTop() + (nextTop - currentOffset);
  1071. var offsetDelta = nextTop - currentOffset;
  1072. nextOffset -= $highlighted.outerHeight(false) * 2;
  1073. if (currentIndex <= 2) {
  1074. this.$results.scrollTop(0);
  1075. } else if (offsetDelta > this.$results.outerHeight() || offsetDelta < 0) {
  1076. this.$results.scrollTop(nextOffset);
  1077. }
  1078. };
  1079. Results.prototype.template = function (result, container) {
  1080. var template = this.options.get('templateResult');
  1081. var escapeMarkup = this.options.get('escapeMarkup');
  1082. var content = template(result, container);
  1083. if (content == null) {
  1084. container.style.display = 'none';
  1085. } else if (typeof content === 'string') {
  1086. container.innerHTML = escapeMarkup(content);
  1087. } else {
  1088. $(container).append(content);
  1089. }
  1090. };
  1091. return Results;
  1092. });
  1093. S2.define('select2/keys', [
  1094. ], function () {
  1095. var KEYS = {
  1096. BACKSPACE: 8,
  1097. TAB: 9,
  1098. ENTER: 13,
  1099. SHIFT: 16,
  1100. CTRL: 17,
  1101. ALT: 18,
  1102. ESC: 27,
  1103. SPACE: 32,
  1104. PAGE_UP: 33,
  1105. PAGE_DOWN: 34,
  1106. END: 35,
  1107. HOME: 36,
  1108. LEFT: 37,
  1109. UP: 38,
  1110. RIGHT: 39,
  1111. DOWN: 40,
  1112. DELETE: 46
  1113. };
  1114. return KEYS;
  1115. });
  1116. S2.define('select2/selection/base', [
  1117. 'jquery',
  1118. '../utils',
  1119. '../keys'
  1120. ], function ($, Utils, KEYS) {
  1121. function BaseSelection($element, options) {
  1122. this.$element = $element;
  1123. this.options = options;
  1124. BaseSelection.__super__.constructor.call(this);
  1125. }
  1126. Utils.Extend(BaseSelection, Utils.Observable);
  1127. BaseSelection.prototype.render = function () {
  1128. var $selection = $(
  1129. '<span class="select2-selection" role="combobox" ' +
  1130. ' aria-haspopup="true" aria-expanded="false">' +
  1131. '</span>'
  1132. );
  1133. this._tabindex = 0;
  1134. if (Utils.GetData(this.$element[0], 'old-tabindex') != null) {
  1135. this._tabindex = Utils.GetData(this.$element[0], 'old-tabindex');
  1136. } else if (this.$element.attr('tabindex') != null) {
  1137. this._tabindex = this.$element.attr('tabindex');
  1138. }
  1139. $selection.attr('title', this.$element.attr('title'));
  1140. $selection.attr('tabindex', this._tabindex);
  1141. this.$selection = $selection;
  1142. return $selection;
  1143. };
  1144. BaseSelection.prototype.bind = function (container, $container) {
  1145. var self = this;
  1146. var id = container.id + '-container';
  1147. var resultsId = container.id + '-results';
  1148. this.container = container;
  1149. this.$selection.on('focus', function (evt) {
  1150. self.trigger('focus', evt);
  1151. });
  1152. this.$selection.on('blur', function (evt) {
  1153. self._handleBlur(evt);
  1154. });
  1155. this.$selection.on('keydown', function (evt) {
  1156. self.trigger('keypress', evt);
  1157. if (evt.which === KEYS.SPACE) {
  1158. evt.preventDefault();
  1159. }
  1160. });
  1161. container.on('results:focus', function (params) {
  1162. self.$selection.attr('aria-activedescendant', params.data._resultId);
  1163. });
  1164. container.on('selection:update', function (params) {
  1165. self.update(params.data);
  1166. });
  1167. container.on('open', function () {
  1168. // When the dropdown is open, aria-expanded="true"
  1169. self.$selection.attr('aria-expanded', 'true');
  1170. self.$selection.attr('aria-owns', resultsId);
  1171. self._attachCloseHandler(container);
  1172. });
  1173. container.on('close', function () {
  1174. // When the dropdown is closed, aria-expanded="false"
  1175. self.$selection.attr('aria-expanded', 'false');
  1176. self.$selection.removeAttr('aria-activedescendant');
  1177. self.$selection.removeAttr('aria-owns');
  1178. self.$selection.focus();
  1179. window.setTimeout(function () {
  1180. self.$selection.focus();
  1181. }, 0);
  1182. self._detachCloseHandler(container);
  1183. });
  1184. container.on('enable', function () {
  1185. self.$selection.attr('tabindex', self._tabindex);
  1186. });
  1187. container.on('disable', function () {
  1188. self.$selection.attr('tabindex', '-1');
  1189. });
  1190. };
  1191. BaseSelection.prototype._handleBlur = function (evt) {
  1192. var self = this;
  1193. // This needs to be delayed as the active element is the body when the tab
  1194. // key is pressed, possibly along with others.
  1195. window.setTimeout(function () {
  1196. // Don't trigger `blur` if the focus is still in the selection
  1197. if (
  1198. (document.activeElement == self.$selection[0]) ||
  1199. ($.contains(self.$selection[0], document.activeElement))
  1200. ) {
  1201. return;
  1202. }
  1203. self.trigger('blur', evt);
  1204. }, 1);
  1205. };
  1206. BaseSelection.prototype._attachCloseHandler = function (container) {
  1207. var self = this;
  1208. $(document.body).on('mousedown.select2.' + container.id, function (e) {
  1209. var $target = $(e.target);
  1210. var $select = $target.closest('.select2');
  1211. var $all = $('.select2.select2-container--open');
  1212. $all.each(function () {
  1213. var $this = $(this);
  1214. if (this == $select[0]) {
  1215. return;
  1216. }
  1217. var $element = Utils.GetData(this, 'element');
  1218. $element.select2('close');
  1219. });
  1220. });
  1221. };
  1222. BaseSelection.prototype._detachCloseHandler = function (container) {
  1223. $(document.body).off('mousedown.select2.' + container.id);
  1224. };
  1225. BaseSelection.prototype.position = function ($selection, $container) {
  1226. var $selectionContainer = $container.find('.selection');
  1227. $selectionContainer.append($selection);
  1228. };
  1229. BaseSelection.prototype.destroy = function () {
  1230. this._detachCloseHandler(this.container);
  1231. };
  1232. BaseSelection.prototype.update = function (data) {
  1233. throw new Error('The `update` method must be defined in child classes.');
  1234. };
  1235. return BaseSelection;
  1236. });
  1237. S2.define('select2/selection/single', [
  1238. 'jquery',
  1239. './base',
  1240. '../utils',
  1241. '../keys'
  1242. ], function ($, BaseSelection, Utils, KEYS) {
  1243. function SingleSelection() {
  1244. SingleSelection.__super__.constructor.apply(this, arguments);
  1245. }
  1246. Utils.Extend(SingleSelection, BaseSelection);
  1247. SingleSelection.prototype.render = function () {
  1248. var $selection = SingleSelection.__super__.render.call(this);
  1249. $selection.addClass('select2-selection--single');
  1250. $selection.html(
  1251. '<span class="select2-selection__rendered"></span>' +
  1252. '<span class="select2-selection__arrow" role="presentation">' +
  1253. '<b role="presentation"></b>' +
  1254. '</span>'
  1255. );
  1256. return $selection;
  1257. };
  1258. SingleSelection.prototype.bind = function (container, $container) {
  1259. var self = this;
  1260. SingleSelection.__super__.bind.apply(this, arguments);
  1261. var id = container.id + '-container';
  1262. this.$selection.find('.select2-selection__rendered')
  1263. .attr('id', id)
  1264. .attr('role', 'textbox')
  1265. .attr('aria-readonly', 'true');
  1266. this.$selection.attr('aria-labelledby', id);
  1267. this.$selection.on('mousedown', function (evt) {
  1268. // Only respond to left clicks
  1269. if (evt.which !== 1) {
  1270. return;
  1271. }
  1272. self.trigger('toggle', {
  1273. originalEvent: evt
  1274. });
  1275. });
  1276. this.$selection.on('focus', function (evt) {
  1277. // User focuses on the container
  1278. });
  1279. this.$selection.on('blur', function (evt) {
  1280. // User exits the container
  1281. });
  1282. container.on('focus', function (evt) {
  1283. if (!container.isOpen()) {
  1284. self.$selection.focus();
  1285. }
  1286. });
  1287. };
  1288. SingleSelection.prototype.clear = function () {
  1289. var $rendered = this.$selection.find('.select2-selection__rendered');
  1290. $rendered.empty();
  1291. $rendered.removeAttr('title'); // clear tooltip on empty
  1292. };
  1293. SingleSelection.prototype.display = function (data, container) {
  1294. var template = this.options.get('templateSelection');
  1295. var escapeMarkup = this.options.get('escapeMarkup');
  1296. return escapeMarkup(template(data, container));
  1297. };
  1298. SingleSelection.prototype.selectionContainer = function () {
  1299. return $('<span></span>');
  1300. };
  1301. SingleSelection.prototype.update = function (data) {
  1302. if (data.length === 0) {
  1303. this.clear();
  1304. return;
  1305. }
  1306. var selection = data[0];
  1307. var $rendered = this.$selection.find('.select2-selection__rendered');
  1308. var formatted = this.display(selection, $rendered);
  1309. $rendered.empty().append(formatted);
  1310. $rendered.attr('title', selection.title || selection.text);
  1311. };
  1312. return SingleSelection;
  1313. });
  1314. S2.define('select2/selection/multiple', [
  1315. 'jquery',
  1316. './base',
  1317. '../utils'
  1318. ], function ($, BaseSelection, Utils) {
  1319. function MultipleSelection($element, options) {
  1320. MultipleSelection.__super__.constructor.apply(this, arguments);
  1321. }
  1322. Utils.Extend(MultipleSelection, BaseSelection);
  1323. MultipleSelection.prototype.render = function () {
  1324. var $selection = MultipleSelection.__super__.render.call(this);
  1325. $selection.addClass('select2-selection--multiple');
  1326. $selection.html(
  1327. '<ul class="select2-selection__rendered"></ul>'
  1328. );
  1329. return $selection;
  1330. };
  1331. MultipleSelection.prototype.bind = function (container, $container) {
  1332. var self = this;
  1333. MultipleSelection.__super__.bind.apply(this, arguments);
  1334. this.$selection.on('click', function (evt) {
  1335. self.trigger('toggle', {
  1336. originalEvent: evt
  1337. });
  1338. });
  1339. this.$selection.on(
  1340. 'click',
  1341. '.select2-selection__choice__remove',
  1342. function (evt) {
  1343. // Ignore the event if it is disabled
  1344. if (self.options.get('disabled')) {
  1345. return;
  1346. }
  1347. var $remove = $(this);
  1348. var $selection = $remove.parent();
  1349. var data = Utils.GetData($selection[0], 'data');
  1350. self.trigger('unselect', {
  1351. originalEvent: evt,
  1352. data: data
  1353. });
  1354. }
  1355. );
  1356. };
  1357. MultipleSelection.prototype.clear = function () {
  1358. var $rendered = this.$selection.find('.select2-selection__rendered');
  1359. $rendered.empty();
  1360. $rendered.removeAttr('title');
  1361. };
  1362. MultipleSelection.prototype.display = function (data, container) {
  1363. var template = this.options.get('templateSelection');
  1364. var escapeMarkup = this.options.get('escapeMarkup');
  1365. return escapeMarkup(template(data, container));
  1366. };
  1367. MultipleSelection.prototype.selectionContainer = function () {
  1368. var $container = $(
  1369. '<li class="select2-selection__choice">' +
  1370. '<span class="select2-selection__choice__remove" role="presentation">' +
  1371. '&times;' +
  1372. '</span>' +
  1373. '</li>'
  1374. );
  1375. return $container;
  1376. };
  1377. MultipleSelection.prototype.update = function (data) {
  1378. this.clear();
  1379. if (data.length === 0) {
  1380. return;
  1381. }
  1382. var $selections = [];
  1383. for (var d = 0; d < data.length; d++) {
  1384. var selection = data[d];
  1385. var $selection = this.selectionContainer();
  1386. var formatted = this.display(selection, $selection);
  1387. $selection.append(formatted);
  1388. $selection.attr('title', selection.title || selection.text);
  1389. Utils.StoreData($selection[0], 'data', selection);
  1390. $selections.push($selection);
  1391. }
  1392. var $rendered = this.$selection.find('.select2-selection__rendered');
  1393. Utils.appendMany($rendered, $selections);
  1394. };
  1395. return MultipleSelection;
  1396. });
  1397. S2.define('select2/selection/placeholder', [
  1398. '../utils'
  1399. ], function (Utils) {
  1400. function Placeholder(decorated, $element, options) {
  1401. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  1402. decorated.call(this, $element, options);
  1403. }
  1404. Placeholder.prototype.normalizePlaceholder = function (_, placeholder) {
  1405. if (typeof placeholder === 'string') {
  1406. placeholder = {
  1407. id: '',
  1408. text: placeholder
  1409. };
  1410. }
  1411. return placeholder;
  1412. };
  1413. Placeholder.prototype.createPlaceholder = function (decorated, placeholder) {
  1414. var $placeholder = this.selectionContainer();
  1415. $placeholder.html(this.display(placeholder));
  1416. $placeholder.addClass('select2-selection__placeholder')
  1417. .removeClass('select2-selection__choice');
  1418. return $placeholder;
  1419. };
  1420. Placeholder.prototype.update = function (decorated, data) {
  1421. var singlePlaceholder = (
  1422. data.length == 1 && data[0].id != this.placeholder.id
  1423. );
  1424. var multipleSelections = data.length > 1;
  1425. if (multipleSelections || singlePlaceholder) {
  1426. return decorated.call(this, data);
  1427. }
  1428. this.clear();
  1429. var $placeholder = this.createPlaceholder(this.placeholder);
  1430. this.$selection.find('.select2-selection__rendered').append($placeholder);
  1431. };
  1432. return Placeholder;
  1433. });
  1434. S2.define('select2/selection/allowClear', [
  1435. 'jquery',
  1436. '../keys',
  1437. '../utils'
  1438. ], function ($, KEYS, Utils) {
  1439. function AllowClear() { }
  1440. AllowClear.prototype.bind = function (decorated, container, $container) {
  1441. var self = this;
  1442. decorated.call(this, container, $container);
  1443. if (this.placeholder == null) {
  1444. if (this.options.get('debug') && window.console && console.error) {
  1445. console.error(
  1446. 'Select2: The `allowClear` option should be used in combination ' +
  1447. 'with the `placeholder` option.'
  1448. );
  1449. }
  1450. }
  1451. this.$selection.on('mousedown', '.select2-selection__clear',
  1452. function (evt) {
  1453. self._handleClear(evt);
  1454. });
  1455. container.on('keypress', function (evt) {
  1456. self._handleKeyboardClear(evt, container);
  1457. });
  1458. };
  1459. AllowClear.prototype._handleClear = function (_, evt) {
  1460. // Ignore the event if it is disabled
  1461. if (this.options.get('disabled')) {
  1462. return;
  1463. }
  1464. var $clear = this.$selection.find('.select2-selection__clear');
  1465. // Ignore the event if nothing has been selected
  1466. if ($clear.length === 0) {
  1467. return;
  1468. }
  1469. evt.stopPropagation();
  1470. var data = Utils.GetData($clear[0], 'data');
  1471. var previousVal = this.$element.val();
  1472. this.$element.val(this.placeholder.id);
  1473. var unselectData = {
  1474. data: data
  1475. };
  1476. this.trigger('clear', unselectData);
  1477. if (unselectData.prevented) {
  1478. this.$element.val(previousVal);
  1479. return;
  1480. }
  1481. for (var d = 0; d < data.length; d++) {
  1482. unselectData = {
  1483. data: data[d]
  1484. };
  1485. // Trigger the `unselect` event, so people can prevent it from being
  1486. // cleared.
  1487. this.trigger('unselect', unselectData);
  1488. // If the event was prevented, don't clear it out.
  1489. if (unselectData.prevented) {
  1490. this.$element.val(previousVal);
  1491. return;
  1492. }
  1493. }
  1494. this.$element.trigger('change');
  1495. this.trigger('toggle', {});
  1496. };
  1497. AllowClear.prototype._handleKeyboardClear = function (_, evt, container) {
  1498. if (container.isOpen()) {
  1499. return;
  1500. }
  1501. if (evt.which == KEYS.DELETE || evt.which == KEYS.BACKSPACE) {
  1502. this._handleClear(evt);
  1503. }
  1504. };
  1505. AllowClear.prototype.update = function (decorated, data) {
  1506. decorated.call(this, data);
  1507. if (this.$selection.find('.select2-selection__placeholder').length > 0 ||
  1508. data.length === 0) {
  1509. return;
  1510. }
  1511. var $remove = $(
  1512. '<span class="select2-selection__clear">' +
  1513. '&times;' +
  1514. '</span>'
  1515. );
  1516. Utils.StoreData($remove[0], 'data', data);
  1517. this.$selection.find('.select2-selection__rendered').prepend($remove);
  1518. };
  1519. return AllowClear;
  1520. });
  1521. S2.define('select2/selection/search', [
  1522. 'jquery',
  1523. '../utils',
  1524. '../keys'
  1525. ], function ($, Utils, KEYS) {
  1526. function Search(decorated, $element, options) {
  1527. decorated.call(this, $element, options);
  1528. }
  1529. Search.prototype.render = function (decorated) {
  1530. var $search = $(
  1531. '<li class="select2-search select2-search--inline">' +
  1532. '<input class="select2-search__field" type="search" tabindex="-1"' +
  1533. ' autocomplete="off" autocorrect="off" autocapitalize="none"' +
  1534. ' spellcheck="false" role="textbox" aria-autocomplete="list" />' +
  1535. '</li>'
  1536. );
  1537. this.$searchContainer = $search;
  1538. this.$search = $search.find('input');
  1539. var $rendered = decorated.call(this);
  1540. this._transferTabIndex();
  1541. return $rendered;
  1542. };
  1543. Search.prototype.bind = function (decorated, container, $container) {
  1544. var self = this;
  1545. decorated.call(this, container, $container);
  1546. container.on('open', function () {
  1547. self.$search.trigger('focus');
  1548. });
  1549. container.on('close', function () {
  1550. self.$search.val('');
  1551. self.$search.removeAttr('aria-activedescendant');
  1552. self.$search.trigger('focus');
  1553. });
  1554. container.on('enable', function () {
  1555. self.$search.prop('disabled', false);
  1556. self._transferTabIndex();
  1557. });
  1558. container.on('disable', function () {
  1559. self.$search.prop('disabled', true);
  1560. });
  1561. container.on('focus', function (evt) {
  1562. self.$search.trigger('focus');
  1563. });
  1564. container.on('results:focus', function (params) {
  1565. self.$search.attr('aria-activedescendant', params.id);
  1566. });
  1567. this.$selection.on('focusin', '.select2-search--inline', function (evt) {
  1568. self.trigger('focus', evt);
  1569. });
  1570. this.$selection.on('focusout', '.select2-search--inline', function (evt) {
  1571. self._handleBlur(evt);
  1572. });
  1573. this.$selection.on('keydown', '.select2-search--inline', function (evt) {
  1574. evt.stopPropagation();
  1575. self.trigger('keypress', evt);
  1576. self._keyUpPrevented = evt.isDefaultPrevented();
  1577. var key = evt.which;
  1578. if (key === KEYS.BACKSPACE && self.$search.val() === '') {
  1579. var $previousChoice = self.$searchContainer
  1580. .prev('.select2-selection__choice');
  1581. if ($previousChoice.length > 0) {
  1582. var item = Utils.GetData($previousChoice[0], 'data');
  1583. self.searchRemoveChoice(item);
  1584. evt.preventDefault();
  1585. }
  1586. }
  1587. });
  1588. // Try to detect the IE version should the `documentMode` property that
  1589. // is stored on the document. This is only implemented in IE and is
  1590. // slightly cleaner than doing a user agent check.
  1591. // This property is not available in Edge, but Edge also doesn't have
  1592. // this bug.
  1593. var msie = document.documentMode;
  1594. var disableInputEvents = msie && msie <= 11;
  1595. // Workaround for browsers which do not support the `input` event
  1596. // This will prevent double-triggering of events for browsers which support
  1597. // both the `keyup` and `input` events.
  1598. this.$selection.on(
  1599. 'input.searchcheck',
  1600. '.select2-search--inline',
  1601. function (evt) {
  1602. // IE will trigger the `input` event when a placeholder is used on a
  1603. // search box. To get around this issue, we are forced to ignore all
  1604. // `input` events in IE and keep using `keyup`.
  1605. if (disableInputEvents) {
  1606. self.$selection.off('input.search input.searchcheck');
  1607. return;
  1608. }
  1609. // Unbind the duplicated `keyup` event
  1610. self.$selection.off('keyup.search');
  1611. }
  1612. );
  1613. this.$selection.on(
  1614. 'keyup.search input.search',
  1615. '.select2-search--inline',
  1616. function (evt) {
  1617. // IE will trigger the `input` event when a placeholder is used on a
  1618. // search box. To get around this issue, we are forced to ignore all
  1619. // `input` events in IE and keep using `keyup`.
  1620. if (disableInputEvents && evt.type === 'input') {
  1621. self.$selection.off('input.search input.searchcheck');
  1622. return;
  1623. }
  1624. var key = evt.which;
  1625. // We can freely ignore events from modifier keys
  1626. if (key == KEYS.SHIFT || key == KEYS.CTRL || key == KEYS.ALT) {
  1627. return;
  1628. }
  1629. // Tabbing will be handled during the `keydown` phase
  1630. if (key == KEYS.TAB) {
  1631. return;
  1632. }
  1633. self.handleSearch(evt);
  1634. }
  1635. );
  1636. };
  1637. /**
  1638. * This method will transfer the tabindex attribute from the rendered
  1639. * selection to the search box. This allows for the search box to be used as
  1640. * the primary focus instead of the selection container.
  1641. *
  1642. * @private
  1643. */
  1644. Search.prototype._transferTabIndex = function (decorated) {
  1645. this.$search.attr('tabindex', this.$selection.attr('tabindex'));
  1646. this.$selection.attr('tabindex', '-1');
  1647. };
  1648. Search.prototype.createPlaceholder = function (decorated, placeholder) {
  1649. this.$search.attr('placeholder', placeholder.text);
  1650. };
  1651. Search.prototype.update = function (decorated, data) {
  1652. var searchHadFocus = this.$search[0] == document.activeElement;
  1653. this.$search.attr('placeholder', '');
  1654. decorated.call(this, data);
  1655. this.$selection.find('.select2-selection__rendered')
  1656. .append(this.$searchContainer);
  1657. this.resizeSearch();
  1658. if (searchHadFocus) {
  1659. var isTagInput = this.$element.find('[data-select2-tag]').length;
  1660. if (isTagInput) {
  1661. // fix IE11 bug where tag input lost focus
  1662. this.$element.focus();
  1663. } else {
  1664. this.$search.focus();
  1665. }
  1666. }
  1667. };
  1668. Search.prototype.handleSearch = function () {
  1669. this.resizeSearch();
  1670. if (!this._keyUpPrevented) {
  1671. var input = this.$search.val();
  1672. this.trigger('query', {
  1673. term: input
  1674. });
  1675. }
  1676. this._keyUpPrevented = false;
  1677. };
  1678. Search.prototype.searchRemoveChoice = function (decorated, item) {
  1679. this.trigger('unselect', {
  1680. data: item
  1681. });
  1682. this.$search.val(item.text);
  1683. this.handleSearch();
  1684. };
  1685. Search.prototype.resizeSearch = function () {
  1686. this.$search.css('width', '25px');
  1687. var width = '';
  1688. if (this.$search.attr('placeholder') !== '') {
  1689. width = this.$selection.find('.select2-selection__rendered').innerWidth();
  1690. } else {
  1691. var minimumWidth = this.$search.val().length + 1;
  1692. width = (minimumWidth * 0.75) + 'em';
  1693. }
  1694. this.$search.css('width', width);
  1695. };
  1696. return Search;
  1697. });
  1698. S2.define('select2/selection/eventRelay', [
  1699. 'jquery'
  1700. ], function ($) {
  1701. function EventRelay() { }
  1702. EventRelay.prototype.bind = function (decorated, container, $container) {
  1703. var self = this;
  1704. var relayEvents = [
  1705. 'open', 'opening',
  1706. 'close', 'closing',
  1707. 'select', 'selecting',
  1708. 'unselect', 'unselecting',
  1709. 'clear', 'clearing'
  1710. ];
  1711. var preventableEvents = [
  1712. 'opening', 'closing', 'selecting', 'unselecting', 'clearing'
  1713. ];
  1714. decorated.call(this, container, $container);
  1715. container.on('*', function (name, params) {
  1716. // Ignore events that should not be relayed
  1717. if ($.inArray(name, relayEvents) === -1) {
  1718. return;
  1719. }
  1720. // The parameters should always be an object
  1721. params = params || {};
  1722. // Generate the jQuery event for the Select2 event
  1723. var evt = $.Event('select2:' + name, {
  1724. params: params
  1725. });
  1726. self.$element.trigger(evt);
  1727. // Only handle preventable events if it was one
  1728. if ($.inArray(name, preventableEvents) === -1) {
  1729. return;
  1730. }
  1731. params.prevented = evt.isDefaultPrevented();
  1732. });
  1733. };
  1734. return EventRelay;
  1735. });
  1736. S2.define('select2/translation', [
  1737. 'jquery',
  1738. 'require'
  1739. ], function ($, require) {
  1740. function Translation(dict) {
  1741. this.dict = dict || {};
  1742. }
  1743. Translation.prototype.all = function () {
  1744. return this.dict;
  1745. };
  1746. Translation.prototype.get = function (key) {
  1747. return this.dict[key];
  1748. };
  1749. Translation.prototype.extend = function (translation) {
  1750. this.dict = $.extend({}, translation.all(), this.dict);
  1751. };
  1752. // Static functions
  1753. Translation._cache = {};
  1754. Translation.loadPath = function (path) {
  1755. if (!(path in Translation._cache)) {
  1756. var translations = require(path);
  1757. Translation._cache[path] = translations;
  1758. }
  1759. return new Translation(Translation._cache[path]);
  1760. };
  1761. return Translation;
  1762. });
  1763. S2.define('select2/diacritics', [
  1764. ], function () {
  1765. var diacritics = {
  1766. '\u24B6': 'A',
  1767. '\uFF21': 'A',
  1768. '\u00C0': 'A',
  1769. '\u00C1': 'A',
  1770. '\u00C2': 'A',
  1771. '\u1EA6': 'A',
  1772. '\u1EA4': 'A',
  1773. '\u1EAA': 'A',
  1774. '\u1EA8': 'A',
  1775. '\u00C3': 'A',
  1776. '\u0100': 'A',
  1777. '\u0102': 'A',
  1778. '\u1EB0': 'A',
  1779. '\u1EAE': 'A',
  1780. '\u1EB4': 'A',
  1781. '\u1EB2': 'A',
  1782. '\u0226': 'A',
  1783. '\u01E0': 'A',
  1784. '\u00C4': 'A',
  1785. '\u01DE': 'A',
  1786. '\u1EA2': 'A',
  1787. '\u00C5': 'A',
  1788. '\u01FA': 'A',
  1789. '\u01CD': 'A',
  1790. '\u0200': 'A',
  1791. '\u0202': 'A',
  1792. '\u1EA0': 'A',
  1793. '\u1EAC': 'A',
  1794. '\u1EB6': 'A',
  1795. '\u1E00': 'A',
  1796. '\u0104': 'A',
  1797. '\u023A': 'A',
  1798. '\u2C6F': 'A',
  1799. '\uA732': 'AA',
  1800. '\u00C6': 'AE',
  1801. '\u01FC': 'AE',
  1802. '\u01E2': 'AE',
  1803. '\uA734': 'AO',
  1804. '\uA736': 'AU',
  1805. '\uA738': 'AV',
  1806. '\uA73A': 'AV',
  1807. '\uA73C': 'AY',
  1808. '\u24B7': 'B',
  1809. '\uFF22': 'B',
  1810. '\u1E02': 'B',
  1811. '\u1E04': 'B',
  1812. '\u1E06': 'B',
  1813. '\u0243': 'B',
  1814. '\u0182': 'B',
  1815. '\u0181': 'B',
  1816. '\u24B8': 'C',
  1817. '\uFF23': 'C',
  1818. '\u0106': 'C',
  1819. '\u0108': 'C',
  1820. '\u010A': 'C',
  1821. '\u010C': 'C',
  1822. '\u00C7': 'C',
  1823. '\u1E08': 'C',
  1824. '\u0187': 'C',
  1825. '\u023B': 'C',
  1826. '\uA73E': 'C',
  1827. '\u24B9': 'D',
  1828. '\uFF24': 'D',
  1829. '\u1E0A': 'D',
  1830. '\u010E': 'D',
  1831. '\u1E0C': 'D',
  1832. '\u1E10': 'D',
  1833. '\u1E12': 'D',
  1834. '\u1E0E': 'D',
  1835. '\u0110': 'D',
  1836. '\u018B': 'D',
  1837. '\u018A': 'D',
  1838. '\u0189': 'D',
  1839. '\uA779': 'D',
  1840. '\u01F1': 'DZ',
  1841. '\u01C4': 'DZ',
  1842. '\u01F2': 'Dz',
  1843. '\u01C5': 'Dz',
  1844. '\u24BA': 'E',
  1845. '\uFF25': 'E',
  1846. '\u00C8': 'E',
  1847. '\u00C9': 'E',
  1848. '\u00CA': 'E',
  1849. '\u1EC0': 'E',
  1850. '\u1EBE': 'E',
  1851. '\u1EC4': 'E',
  1852. '\u1EC2': 'E',
  1853. '\u1EBC': 'E',
  1854. '\u0112': 'E',
  1855. '\u1E14': 'E',
  1856. '\u1E16': 'E',
  1857. '\u0114': 'E',
  1858. '\u0116': 'E',
  1859. '\u00CB': 'E',
  1860. '\u1EBA': 'E',
  1861. '\u011A': 'E',
  1862. '\u0204': 'E',
  1863. '\u0206': 'E',
  1864. '\u1EB8': 'E',
  1865. '\u1EC6': 'E',
  1866. '\u0228': 'E',
  1867. '\u1E1C': 'E',
  1868. '\u0118': 'E',
  1869. '\u1E18': 'E',
  1870. '\u1E1A': 'E',
  1871. '\u0190': 'E',
  1872. '\u018E': 'E',
  1873. '\u24BB': 'F',
  1874. '\uFF26': 'F',
  1875. '\u1E1E': 'F',
  1876. '\u0191': 'F',
  1877. '\uA77B': 'F',
  1878. '\u24BC': 'G',
  1879. '\uFF27': 'G',
  1880. '\u01F4': 'G',
  1881. '\u011C': 'G',
  1882. '\u1E20': 'G',
  1883. '\u011E': 'G',
  1884. '\u0120': 'G',
  1885. '\u01E6': 'G',
  1886. '\u0122': 'G',
  1887. '\u01E4': 'G',
  1888. '\u0193': 'G',
  1889. '\uA7A0': 'G',
  1890. '\uA77D': 'G',
  1891. '\uA77E': 'G',
  1892. '\u24BD': 'H',
  1893. '\uFF28': 'H',
  1894. '\u0124': 'H',
  1895. '\u1E22': 'H',
  1896. '\u1E26': 'H',
  1897. '\u021E': 'H',
  1898. '\u1E24': 'H',
  1899. '\u1E28': 'H',
  1900. '\u1E2A': 'H',
  1901. '\u0126': 'H',
  1902. '\u2C67': 'H',
  1903. '\u2C75': 'H',
  1904. '\uA78D': 'H',
  1905. '\u24BE': 'I',
  1906. '\uFF29': 'I',
  1907. '\u00CC': 'I',
  1908. '\u00CD': 'I',
  1909. '\u00CE': 'I',
  1910. '\u0128': 'I',
  1911. '\u012A': 'I',
  1912. '\u012C': 'I',
  1913. '\u0130': 'I',
  1914. '\u00CF': 'I',
  1915. '\u1E2E': 'I',
  1916. '\u1EC8': 'I',
  1917. '\u01CF': 'I',
  1918. '\u0208': 'I',
  1919. '\u020A': 'I',
  1920. '\u1ECA': 'I',
  1921. '\u012E': 'I',
  1922. '\u1E2C': 'I',
  1923. '\u0197': 'I',
  1924. '\u24BF': 'J',
  1925. '\uFF2A': 'J',
  1926. '\u0134': 'J',
  1927. '\u0248': 'J',
  1928. '\u24C0': 'K',
  1929. '\uFF2B': 'K',
  1930. '\u1E30': 'K',
  1931. '\u01E8': 'K',
  1932. '\u1E32': 'K',
  1933. '\u0136': 'K',
  1934. '\u1E34': 'K',
  1935. '\u0198': 'K',
  1936. '\u2C69': 'K',
  1937. '\uA740': 'K',
  1938. '\uA742': 'K',
  1939. '\uA744': 'K',
  1940. '\uA7A2': 'K',
  1941. '\u24C1': 'L',
  1942. '\uFF2C': 'L',
  1943. '\u013F': 'L',
  1944. '\u0139': 'L',
  1945. '\u013D': 'L',
  1946. '\u1E36': 'L',
  1947. '\u1E38': 'L',
  1948. '\u013B': 'L',
  1949. '\u1E3C': 'L',
  1950. '\u1E3A': 'L',
  1951. '\u0141': 'L',
  1952. '\u023D': 'L',
  1953. '\u2C62': 'L',
  1954. '\u2C60': 'L',
  1955. '\uA748': 'L',
  1956. '\uA746': 'L',
  1957. '\uA780': 'L',
  1958. '\u01C7': 'LJ',
  1959. '\u01C8': 'Lj',
  1960. '\u24C2': 'M',
  1961. '\uFF2D': 'M',
  1962. '\u1E3E': 'M',
  1963. '\u1E40': 'M',
  1964. '\u1E42': 'M',
  1965. '\u2C6E': 'M',
  1966. '\u019C': 'M',
  1967. '\u24C3': 'N',
  1968. '\uFF2E': 'N',
  1969. '\u01F8': 'N',
  1970. '\u0143': 'N',
  1971. '\u00D1': 'N',
  1972. '\u1E44': 'N',
  1973. '\u0147': 'N',
  1974. '\u1E46': 'N',
  1975. '\u0145': 'N',
  1976. '\u1E4A': 'N',
  1977. '\u1E48': 'N',
  1978. '\u0220': 'N',
  1979. '\u019D': 'N',
  1980. '\uA790': 'N',
  1981. '\uA7A4': 'N',
  1982. '\u01CA': 'NJ',
  1983. '\u01CB': 'Nj',
  1984. '\u24C4': 'O',
  1985. '\uFF2F': 'O',
  1986. '\u00D2': 'O',
  1987. '\u00D3': 'O',
  1988. '\u00D4': 'O',
  1989. '\u1ED2': 'O',
  1990. '\u1ED0': 'O',
  1991. '\u1ED6': 'O',
  1992. '\u1ED4': 'O',
  1993. '\u00D5': 'O',
  1994. '\u1E4C': 'O',
  1995. '\u022C': 'O',
  1996. '\u1E4E': 'O',
  1997. '\u014C': 'O',
  1998. '\u1E50': 'O',
  1999. '\u1E52': 'O',
  2000. '\u014E': 'O',
  2001. '\u022E': 'O',
  2002. '\u0230': 'O',
  2003. '\u00D6': 'O',
  2004. '\u022A': 'O',
  2005. '\u1ECE': 'O',
  2006. '\u0150': 'O',
  2007. '\u01D1': 'O',
  2008. '\u020C': 'O',
  2009. '\u020E': 'O',
  2010. '\u01A0': 'O',
  2011. '\u1EDC': 'O',
  2012. '\u1EDA': 'O',
  2013. '\u1EE0': 'O',
  2014. '\u1EDE': 'O',
  2015. '\u1EE2': 'O',
  2016. '\u1ECC': 'O',
  2017. '\u1ED8': 'O',
  2018. '\u01EA': 'O',
  2019. '\u01EC': 'O',
  2020. '\u00D8': 'O',
  2021. '\u01FE': 'O',
  2022. '\u0186': 'O',
  2023. '\u019F': 'O',
  2024. '\uA74A': 'O',
  2025. '\uA74C': 'O',
  2026. '\u01A2': 'OI',
  2027. '\uA74E': 'OO',
  2028. '\u0222': 'OU',
  2029. '\u24C5': 'P',
  2030. '\uFF30': 'P',
  2031. '\u1E54': 'P',
  2032. '\u1E56': 'P',
  2033. '\u01A4': 'P',
  2034. '\u2C63': 'P',
  2035. '\uA750': 'P',
  2036. '\uA752': 'P',
  2037. '\uA754': 'P',
  2038. '\u24C6': 'Q',
  2039. '\uFF31': 'Q',
  2040. '\uA756': 'Q',
  2041. '\uA758': 'Q',
  2042. '\u024A': 'Q',
  2043. '\u24C7': 'R',
  2044. '\uFF32': 'R',
  2045. '\u0154': 'R',
  2046. '\u1E58': 'R',
  2047. '\u0158': 'R',
  2048. '\u0210': 'R',
  2049. '\u0212': 'R',
  2050. '\u1E5A': 'R',
  2051. '\u1E5C': 'R',
  2052. '\u0156': 'R',
  2053. '\u1E5E': 'R',
  2054. '\u024C': 'R',
  2055. '\u2C64': 'R',
  2056. '\uA75A': 'R',
  2057. '\uA7A6': 'R',
  2058. '\uA782': 'R',
  2059. '\u24C8': 'S',
  2060. '\uFF33': 'S',
  2061. '\u1E9E': 'S',
  2062. '\u015A': 'S',
  2063. '\u1E64': 'S',
  2064. '\u015C': 'S',
  2065. '\u1E60': 'S',
  2066. '\u0160': 'S',
  2067. '\u1E66': 'S',
  2068. '\u1E62': 'S',
  2069. '\u1E68': 'S',
  2070. '\u0218': 'S',
  2071. '\u015E': 'S',
  2072. '\u2C7E': 'S',
  2073. '\uA7A8': 'S',
  2074. '\uA784': 'S',
  2075. '\u24C9': 'T',
  2076. '\uFF34': 'T',
  2077. '\u1E6A': 'T',
  2078. '\u0164': 'T',
  2079. '\u1E6C': 'T',
  2080. '\u021A': 'T',
  2081. '\u0162': 'T',
  2082. '\u1E70': 'T',
  2083. '\u1E6E': 'T',
  2084. '\u0166': 'T',
  2085. '\u01AC': 'T',
  2086. '\u01AE': 'T',
  2087. '\u023E': 'T',
  2088. '\uA786': 'T',
  2089. '\uA728': 'TZ',
  2090. '\u24CA': 'U',
  2091. '\uFF35': 'U',
  2092. '\u00D9': 'U',
  2093. '\u00DA': 'U',
  2094. '\u00DB': 'U',
  2095. '\u0168': 'U',
  2096. '\u1E78': 'U',
  2097. '\u016A': 'U',
  2098. '\u1E7A': 'U',
  2099. '\u016C': 'U',
  2100. '\u00DC': 'U',
  2101. '\u01DB': 'U',
  2102. '\u01D7': 'U',
  2103. '\u01D5': 'U',
  2104. '\u01D9': 'U',
  2105. '\u1EE6': 'U',
  2106. '\u016E': 'U',
  2107. '\u0170': 'U',
  2108. '\u01D3': 'U',
  2109. '\u0214': 'U',
  2110. '\u0216': 'U',
  2111. '\u01AF': 'U',
  2112. '\u1EEA': 'U',
  2113. '\u1EE8': 'U',
  2114. '\u1EEE': 'U',
  2115. '\u1EEC': 'U',
  2116. '\u1EF0': 'U',
  2117. '\u1EE4': 'U',
  2118. '\u1E72': 'U',
  2119. '\u0172': 'U',
  2120. '\u1E76': 'U',
  2121. '\u1E74': 'U',
  2122. '\u0244': 'U',
  2123. '\u24CB': 'V',
  2124. '\uFF36': 'V',
  2125. '\u1E7C': 'V',
  2126. '\u1E7E': 'V',
  2127. '\u01B2': 'V',
  2128. '\uA75E': 'V',
  2129. '\u0245': 'V',
  2130. '\uA760': 'VY',
  2131. '\u24CC': 'W',
  2132. '\uFF37': 'W',
  2133. '\u1E80': 'W',
  2134. '\u1E82': 'W',
  2135. '\u0174': 'W',
  2136. '\u1E86': 'W',
  2137. '\u1E84': 'W',
  2138. '\u1E88': 'W',
  2139. '\u2C72': 'W',
  2140. '\u24CD': 'X',
  2141. '\uFF38': 'X',
  2142. '\u1E8A': 'X',
  2143. '\u1E8C': 'X',
  2144. '\u24CE': 'Y',
  2145. '\uFF39': 'Y',
  2146. '\u1EF2': 'Y',
  2147. '\u00DD': 'Y',
  2148. '\u0176': 'Y',
  2149. '\u1EF8': 'Y',
  2150. '\u0232': 'Y',
  2151. '\u1E8E': 'Y',
  2152. '\u0178': 'Y',
  2153. '\u1EF6': 'Y',
  2154. '\u1EF4': 'Y',
  2155. '\u01B3': 'Y',
  2156. '\u024E': 'Y',
  2157. '\u1EFE': 'Y',
  2158. '\u24CF': 'Z',
  2159. '\uFF3A': 'Z',
  2160. '\u0179': 'Z',
  2161. '\u1E90': 'Z',
  2162. '\u017B': 'Z',
  2163. '\u017D': 'Z',
  2164. '\u1E92': 'Z',
  2165. '\u1E94': 'Z',
  2166. '\u01B5': 'Z',
  2167. '\u0224': 'Z',
  2168. '\u2C7F': 'Z',
  2169. '\u2C6B': 'Z',
  2170. '\uA762': 'Z',
  2171. '\u24D0': 'a',
  2172. '\uFF41': 'a',
  2173. '\u1E9A': 'a',
  2174. '\u00E0': 'a',
  2175. '\u00E1': 'a',
  2176. '\u00E2': 'a',
  2177. '\u1EA7': 'a',
  2178. '\u1EA5': 'a',
  2179. '\u1EAB': 'a',
  2180. '\u1EA9': 'a',
  2181. '\u00E3': 'a',
  2182. '\u0101': 'a',
  2183. '\u0103': 'a',
  2184. '\u1EB1': 'a',
  2185. '\u1EAF': 'a',
  2186. '\u1EB5': 'a',
  2187. '\u1EB3': 'a',
  2188. '\u0227': 'a',
  2189. '\u01E1': 'a',
  2190. '\u00E4': 'a',
  2191. '\u01DF': 'a',
  2192. '\u1EA3': 'a',
  2193. '\u00E5': 'a',
  2194. '\u01FB': 'a',
  2195. '\u01CE': 'a',
  2196. '\u0201': 'a',
  2197. '\u0203': 'a',
  2198. '\u1EA1': 'a',
  2199. '\u1EAD': 'a',
  2200. '\u1EB7': 'a',
  2201. '\u1E01': 'a',
  2202. '\u0105': 'a',
  2203. '\u2C65': 'a',
  2204. '\u0250': 'a',
  2205. '\uA733': 'aa',
  2206. '\u00E6': 'ae',
  2207. '\u01FD': 'ae',
  2208. '\u01E3': 'ae',
  2209. '\uA735': 'ao',
  2210. '\uA737': 'au',
  2211. '\uA739': 'av',
  2212. '\uA73B': 'av',
  2213. '\uA73D': 'ay',
  2214. '\u24D1': 'b',
  2215. '\uFF42': 'b',
  2216. '\u1E03': 'b',
  2217. '\u1E05': 'b',
  2218. '\u1E07': 'b',
  2219. '\u0180': 'b',
  2220. '\u0183': 'b',
  2221. '\u0253': 'b',
  2222. '\u24D2': 'c',
  2223. '\uFF43': 'c',
  2224. '\u0107': 'c',
  2225. '\u0109': 'c',
  2226. '\u010B': 'c',
  2227. '\u010D': 'c',
  2228. '\u00E7': 'c',
  2229. '\u1E09': 'c',
  2230. '\u0188': 'c',
  2231. '\u023C': 'c',
  2232. '\uA73F': 'c',
  2233. '\u2184': 'c',
  2234. '\u24D3': 'd',
  2235. '\uFF44': 'd',
  2236. '\u1E0B': 'd',
  2237. '\u010F': 'd',
  2238. '\u1E0D': 'd',
  2239. '\u1E11': 'd',
  2240. '\u1E13': 'd',
  2241. '\u1E0F': 'd',
  2242. '\u0111': 'd',
  2243. '\u018C': 'd',
  2244. '\u0256': 'd',
  2245. '\u0257': 'd',
  2246. '\uA77A': 'd',
  2247. '\u01F3': 'dz',
  2248. '\u01C6': 'dz',
  2249. '\u24D4': 'e',
  2250. '\uFF45': 'e',
  2251. '\u00E8': 'e',
  2252. '\u00E9': 'e',
  2253. '\u00EA': 'e',
  2254. '\u1EC1': 'e',
  2255. '\u1EBF': 'e',
  2256. '\u1EC5': 'e',
  2257. '\u1EC3': 'e',
  2258. '\u1EBD': 'e',
  2259. '\u0113': 'e',
  2260. '\u1E15': 'e',
  2261. '\u1E17': 'e',
  2262. '\u0115': 'e',
  2263. '\u0117': 'e',
  2264. '\u00EB': 'e',
  2265. '\u1EBB': 'e',
  2266. '\u011B': 'e',
  2267. '\u0205': 'e',
  2268. '\u0207': 'e',
  2269. '\u1EB9': 'e',
  2270. '\u1EC7': 'e',
  2271. '\u0229': 'e',
  2272. '\u1E1D': 'e',
  2273. '\u0119': 'e',
  2274. '\u1E19': 'e',
  2275. '\u1E1B': 'e',
  2276. '\u0247': 'e',
  2277. '\u025B': 'e',
  2278. '\u01DD': 'e',
  2279. '\u24D5': 'f',
  2280. '\uFF46': 'f',
  2281. '\u1E1F': 'f',
  2282. '\u0192': 'f',
  2283. '\uA77C': 'f',
  2284. '\u24D6': 'g',
  2285. '\uFF47': 'g',
  2286. '\u01F5': 'g',
  2287. '\u011D': 'g',
  2288. '\u1E21': 'g',
  2289. '\u011F': 'g',
  2290. '\u0121': 'g',
  2291. '\u01E7': 'g',
  2292. '\u0123': 'g',
  2293. '\u01E5': 'g',
  2294. '\u0260': 'g',
  2295. '\uA7A1': 'g',
  2296. '\u1D79': 'g',
  2297. '\uA77F': 'g',
  2298. '\u24D7': 'h',
  2299. '\uFF48': 'h',
  2300. '\u0125': 'h',
  2301. '\u1E23': 'h',
  2302. '\u1E27': 'h',
  2303. '\u021F': 'h',
  2304. '\u1E25': 'h',
  2305. '\u1E29': 'h',
  2306. '\u1E2B': 'h',
  2307. '\u1E96': 'h',
  2308. '\u0127': 'h',
  2309. '\u2C68': 'h',
  2310. '\u2C76': 'h',
  2311. '\u0265': 'h',
  2312. '\u0195': 'hv',
  2313. '\u24D8': 'i',
  2314. '\uFF49': 'i',
  2315. '\u00EC': 'i',
  2316. '\u00ED': 'i',
  2317. '\u00EE': 'i',
  2318. '\u0129': 'i',
  2319. '\u012B': 'i',
  2320. '\u012D': 'i',
  2321. '\u00EF': 'i',
  2322. '\u1E2F': 'i',
  2323. '\u1EC9': 'i',
  2324. '\u01D0': 'i',
  2325. '\u0209': 'i',
  2326. '\u020B': 'i',
  2327. '\u1ECB': 'i',
  2328. '\u012F': 'i',
  2329. '\u1E2D': 'i',
  2330. '\u0268': 'i',
  2331. '\u0131': 'i',
  2332. '\u24D9': 'j',
  2333. '\uFF4A': 'j',
  2334. '\u0135': 'j',
  2335. '\u01F0': 'j',
  2336. '\u0249': 'j',
  2337. '\u24DA': 'k',
  2338. '\uFF4B': 'k',
  2339. '\u1E31': 'k',
  2340. '\u01E9': 'k',
  2341. '\u1E33': 'k',
  2342. '\u0137': 'k',
  2343. '\u1E35': 'k',
  2344. '\u0199': 'k',
  2345. '\u2C6A': 'k',
  2346. '\uA741': 'k',
  2347. '\uA743': 'k',
  2348. '\uA745': 'k',
  2349. '\uA7A3': 'k',
  2350. '\u24DB': 'l',
  2351. '\uFF4C': 'l',
  2352. '\u0140': 'l',
  2353. '\u013A': 'l',
  2354. '\u013E': 'l',
  2355. '\u1E37': 'l',
  2356. '\u1E39': 'l',
  2357. '\u013C': 'l',
  2358. '\u1E3D': 'l',
  2359. '\u1E3B': 'l',
  2360. '\u017F': 'l',
  2361. '\u0142': 'l',
  2362. '\u019A': 'l',
  2363. '\u026B': 'l',
  2364. '\u2C61': 'l',
  2365. '\uA749': 'l',
  2366. '\uA781': 'l',
  2367. '\uA747': 'l',
  2368. '\u01C9': 'lj',
  2369. '\u24DC': 'm',
  2370. '\uFF4D': 'm',
  2371. '\u1E3F': 'm',
  2372. '\u1E41': 'm',
  2373. '\u1E43': 'm',
  2374. '\u0271': 'm',
  2375. '\u026F': 'm',
  2376. '\u24DD': 'n',
  2377. '\uFF4E': 'n',
  2378. '\u01F9': 'n',
  2379. '\u0144': 'n',
  2380. '\u00F1': 'n',
  2381. '\u1E45': 'n',
  2382. '\u0148': 'n',
  2383. '\u1E47': 'n',
  2384. '\u0146': 'n',
  2385. '\u1E4B': 'n',
  2386. '\u1E49': 'n',
  2387. '\u019E': 'n',
  2388. '\u0272': 'n',
  2389. '\u0149': 'n',
  2390. '\uA791': 'n',
  2391. '\uA7A5': 'n',
  2392. '\u01CC': 'nj',
  2393. '\u24DE': 'o',
  2394. '\uFF4F': 'o',
  2395. '\u00F2': 'o',
  2396. '\u00F3': 'o',
  2397. '\u00F4': 'o',
  2398. '\u1ED3': 'o',
  2399. '\u1ED1': 'o',
  2400. '\u1ED7': 'o',
  2401. '\u1ED5': 'o',
  2402. '\u00F5': 'o',
  2403. '\u1E4D': 'o',
  2404. '\u022D': 'o',
  2405. '\u1E4F': 'o',
  2406. '\u014D': 'o',
  2407. '\u1E51': 'o',
  2408. '\u1E53': 'o',
  2409. '\u014F': 'o',
  2410. '\u022F': 'o',
  2411. '\u0231': 'o',
  2412. '\u00F6': 'o',
  2413. '\u022B': 'o',
  2414. '\u1ECF': 'o',
  2415. '\u0151': 'o',
  2416. '\u01D2': 'o',
  2417. '\u020D': 'o',
  2418. '\u020F': 'o',
  2419. '\u01A1': 'o',
  2420. '\u1EDD': 'o',
  2421. '\u1EDB': 'o',
  2422. '\u1EE1': 'o',
  2423. '\u1EDF': 'o',
  2424. '\u1EE3': 'o',
  2425. '\u1ECD': 'o',
  2426. '\u1ED9': 'o',
  2427. '\u01EB': 'o',
  2428. '\u01ED': 'o',
  2429. '\u00F8': 'o',
  2430. '\u01FF': 'o',
  2431. '\u0254': 'o',
  2432. '\uA74B': 'o',
  2433. '\uA74D': 'o',
  2434. '\u0275': 'o',
  2435. '\u01A3': 'oi',
  2436. '\u0223': 'ou',
  2437. '\uA74F': 'oo',
  2438. '\u24DF': 'p',
  2439. '\uFF50': 'p',
  2440. '\u1E55': 'p',
  2441. '\u1E57': 'p',
  2442. '\u01A5': 'p',
  2443. '\u1D7D': 'p',
  2444. '\uA751': 'p',
  2445. '\uA753': 'p',
  2446. '\uA755': 'p',
  2447. '\u24E0': 'q',
  2448. '\uFF51': 'q',
  2449. '\u024B': 'q',
  2450. '\uA757': 'q',
  2451. '\uA759': 'q',
  2452. '\u24E1': 'r',
  2453. '\uFF52': 'r',
  2454. '\u0155': 'r',
  2455. '\u1E59': 'r',
  2456. '\u0159': 'r',
  2457. '\u0211': 'r',
  2458. '\u0213': 'r',
  2459. '\u1E5B': 'r',
  2460. '\u1E5D': 'r',
  2461. '\u0157': 'r',
  2462. '\u1E5F': 'r',
  2463. '\u024D': 'r',
  2464. '\u027D': 'r',
  2465. '\uA75B': 'r',
  2466. '\uA7A7': 'r',
  2467. '\uA783': 'r',
  2468. '\u24E2': 's',
  2469. '\uFF53': 's',
  2470. '\u00DF': 's',
  2471. '\u015B': 's',
  2472. '\u1E65': 's',
  2473. '\u015D': 's',
  2474. '\u1E61': 's',
  2475. '\u0161': 's',
  2476. '\u1E67': 's',
  2477. '\u1E63': 's',
  2478. '\u1E69': 's',
  2479. '\u0219': 's',
  2480. '\u015F': 's',
  2481. '\u023F': 's',
  2482. '\uA7A9': 's',
  2483. '\uA785': 's',
  2484. '\u1E9B': 's',
  2485. '\u24E3': 't',
  2486. '\uFF54': 't',
  2487. '\u1E6B': 't',
  2488. '\u1E97': 't',
  2489. '\u0165': 't',
  2490. '\u1E6D': 't',
  2491. '\u021B': 't',
  2492. '\u0163': 't',
  2493. '\u1E71': 't',
  2494. '\u1E6F': 't',
  2495. '\u0167': 't',
  2496. '\u01AD': 't',
  2497. '\u0288': 't',
  2498. '\u2C66': 't',
  2499. '\uA787': 't',
  2500. '\uA729': 'tz',
  2501. '\u24E4': 'u',
  2502. '\uFF55': 'u',
  2503. '\u00F9': 'u',
  2504. '\u00FA': 'u',
  2505. '\u00FB': 'u',
  2506. '\u0169': 'u',
  2507. '\u1E79': 'u',
  2508. '\u016B': 'u',
  2509. '\u1E7B': 'u',
  2510. '\u016D': 'u',
  2511. '\u00FC': 'u',
  2512. '\u01DC': 'u',
  2513. '\u01D8': 'u',
  2514. '\u01D6': 'u',
  2515. '\u01DA': 'u',
  2516. '\u1EE7': 'u',
  2517. '\u016F': 'u',
  2518. '\u0171': 'u',
  2519. '\u01D4': 'u',
  2520. '\u0215': 'u',
  2521. '\u0217': 'u',
  2522. '\u01B0': 'u',
  2523. '\u1EEB': 'u',
  2524. '\u1EE9': 'u',
  2525. '\u1EEF': 'u',
  2526. '\u1EED': 'u',
  2527. '\u1EF1': 'u',
  2528. '\u1EE5': 'u',
  2529. '\u1E73': 'u',
  2530. '\u0173': 'u',
  2531. '\u1E77': 'u',
  2532. '\u1E75': 'u',
  2533. '\u0289': 'u',
  2534. '\u24E5': 'v',
  2535. '\uFF56': 'v',
  2536. '\u1E7D': 'v',
  2537. '\u1E7F': 'v',
  2538. '\u028B': 'v',
  2539. '\uA75F': 'v',
  2540. '\u028C': 'v',
  2541. '\uA761': 'vy',
  2542. '\u24E6': 'w',
  2543. '\uFF57': 'w',
  2544. '\u1E81': 'w',
  2545. '\u1E83': 'w',
  2546. '\u0175': 'w',
  2547. '\u1E87': 'w',
  2548. '\u1E85': 'w',
  2549. '\u1E98': 'w',
  2550. '\u1E89': 'w',
  2551. '\u2C73': 'w',
  2552. '\u24E7': 'x',
  2553. '\uFF58': 'x',
  2554. '\u1E8B': 'x',
  2555. '\u1E8D': 'x',
  2556. '\u24E8': 'y',
  2557. '\uFF59': 'y',
  2558. '\u1EF3': 'y',
  2559. '\u00FD': 'y',
  2560. '\u0177': 'y',
  2561. '\u1EF9': 'y',
  2562. '\u0233': 'y',
  2563. '\u1E8F': 'y',
  2564. '\u00FF': 'y',
  2565. '\u1EF7': 'y',
  2566. '\u1E99': 'y',
  2567. '\u1EF5': 'y',
  2568. '\u01B4': 'y',
  2569. '\u024F': 'y',
  2570. '\u1EFF': 'y',
  2571. '\u24E9': 'z',
  2572. '\uFF5A': 'z',
  2573. '\u017A': 'z',
  2574. '\u1E91': 'z',
  2575. '\u017C': 'z',
  2576. '\u017E': 'z',
  2577. '\u1E93': 'z',
  2578. '\u1E95': 'z',
  2579. '\u01B6': 'z',
  2580. '\u0225': 'z',
  2581. '\u0240': 'z',
  2582. '\u2C6C': 'z',
  2583. '\uA763': 'z',
  2584. '\u0386': '\u0391',
  2585. '\u0388': '\u0395',
  2586. '\u0389': '\u0397',
  2587. '\u038A': '\u0399',
  2588. '\u03AA': '\u0399',
  2589. '\u038C': '\u039F',
  2590. '\u038E': '\u03A5',
  2591. '\u03AB': '\u03A5',
  2592. '\u038F': '\u03A9',
  2593. '\u03AC': '\u03B1',
  2594. '\u03AD': '\u03B5',
  2595. '\u03AE': '\u03B7',
  2596. '\u03AF': '\u03B9',
  2597. '\u03CA': '\u03B9',
  2598. '\u0390': '\u03B9',
  2599. '\u03CC': '\u03BF',
  2600. '\u03CD': '\u03C5',
  2601. '\u03CB': '\u03C5',
  2602. '\u03B0': '\u03C5',
  2603. '\u03C9': '\u03C9',
  2604. '\u03C2': '\u03C3'
  2605. };
  2606. return diacritics;
  2607. });
  2608. S2.define('select2/data/base', [
  2609. '../utils'
  2610. ], function (Utils) {
  2611. function BaseAdapter($element, options) {
  2612. BaseAdapter.__super__.constructor.call(this);
  2613. }
  2614. Utils.Extend(BaseAdapter, Utils.Observable);
  2615. BaseAdapter.prototype.current = function (callback) {
  2616. throw new Error('The `current` method must be defined in child classes.');
  2617. };
  2618. BaseAdapter.prototype.query = function (params, callback) {
  2619. throw new Error('The `query` method must be defined in child classes.');
  2620. };
  2621. BaseAdapter.prototype.bind = function (container, $container) {
  2622. // Can be implemented in subclasses
  2623. };
  2624. BaseAdapter.prototype.destroy = function () {
  2625. // Can be implemented in subclasses
  2626. };
  2627. BaseAdapter.prototype.generateResultId = function (container, data) {
  2628. var id = container.id + '-result-';
  2629. id += Utils.generateChars(4);
  2630. if (data.id != null) {
  2631. id += '-' + data.id.toString();
  2632. } else {
  2633. id += '-' + Utils.generateChars(4);
  2634. }
  2635. return id;
  2636. };
  2637. return BaseAdapter;
  2638. });
  2639. S2.define('select2/data/select', [
  2640. './base',
  2641. '../utils',
  2642. 'jquery'
  2643. ], function (BaseAdapter, Utils, $) {
  2644. function SelectAdapter($element, options) {
  2645. this.$element = $element;
  2646. this.options = options;
  2647. SelectAdapter.__super__.constructor.call(this);
  2648. }
  2649. Utils.Extend(SelectAdapter, BaseAdapter);
  2650. SelectAdapter.prototype.current = function (callback) {
  2651. var data = [];
  2652. var self = this;
  2653. this.$element.find(':selected').each(function () {
  2654. var $option = $(this);
  2655. var option = self.item($option);
  2656. data.push(option);
  2657. });
  2658. callback(data);
  2659. };
  2660. SelectAdapter.prototype.select = function (data) {
  2661. var self = this;
  2662. data.selected = true;
  2663. // If data.element is a DOM node, use it instead
  2664. if ($(data.element).is('option')) {
  2665. data.element.selected = true;
  2666. this.$element.trigger('change');
  2667. return;
  2668. }
  2669. if (this.$element.prop('multiple')) {
  2670. this.current(function (currentData) {
  2671. var val = [];
  2672. data = [data];
  2673. data.push.apply(data, currentData);
  2674. for (var d = 0; d < data.length; d++) {
  2675. var id = data[d].id;
  2676. if ($.inArray(id, val) === -1) {
  2677. val.push(id);
  2678. }
  2679. }
  2680. self.$element.val(val);
  2681. self.$element.trigger('change');
  2682. });
  2683. } else {
  2684. var val = data.id;
  2685. this.$element.val(val);
  2686. this.$element.trigger('change');
  2687. }
  2688. };
  2689. SelectAdapter.prototype.unselect = function (data) {
  2690. var self = this;
  2691. if (!this.$element.prop('multiple')) {
  2692. return;
  2693. }
  2694. data.selected = false;
  2695. if ($(data.element).is('option')) {
  2696. data.element.selected = false;
  2697. this.$element.trigger('change');
  2698. return;
  2699. }
  2700. this.current(function (currentData) {
  2701. var val = [];
  2702. for (var d = 0; d < currentData.length; d++) {
  2703. var id = currentData[d].id;
  2704. if (id !== data.id && $.inArray(id, val) === -1) {
  2705. val.push(id);
  2706. }
  2707. }
  2708. self.$element.val(val);
  2709. self.$element.trigger('change');
  2710. });
  2711. };
  2712. SelectAdapter.prototype.bind = function (container, $container) {
  2713. var self = this;
  2714. this.container = container;
  2715. container.on('select', function (params) {
  2716. self.select(params.data);
  2717. });
  2718. container.on('unselect', function (params) {
  2719. self.unselect(params.data);
  2720. });
  2721. };
  2722. SelectAdapter.prototype.destroy = function () {
  2723. // Remove anything added to child elements
  2724. this.$element.find('*').each(function () {
  2725. // Remove any custom data set by Select2
  2726. Utils.RemoveData(this);
  2727. });
  2728. };
  2729. SelectAdapter.prototype.query = function (params, callback) {
  2730. var data = [];
  2731. var self = this;
  2732. var $options = this.$element.children();
  2733. $options.each(function () {
  2734. var $option = $(this);
  2735. if (!$option.is('option') && !$option.is('optgroup')) {
  2736. return;
  2737. }
  2738. var option = self.item($option);
  2739. var matches = self.matches(params, option);
  2740. if (matches !== null) {
  2741. data.push(matches);
  2742. }
  2743. });
  2744. callback({
  2745. results: data
  2746. });
  2747. };
  2748. SelectAdapter.prototype.addOptions = function ($options) {
  2749. Utils.appendMany(this.$element, $options);
  2750. };
  2751. SelectAdapter.prototype.option = function (data) {
  2752. var option;
  2753. if (data.children) {
  2754. option = document.createElement('optgroup');
  2755. option.label = data.text;
  2756. } else {
  2757. option = document.createElement('option');
  2758. if (option.textContent !== undefined) {
  2759. option.textContent = data.text;
  2760. } else {
  2761. option.innerText = data.text;
  2762. }
  2763. }
  2764. if (data.id !== undefined) {
  2765. option.value = data.id;
  2766. }
  2767. if (data.disabled) {
  2768. option.disabled = true;
  2769. }
  2770. if (data.selected) {
  2771. option.selected = true;
  2772. }
  2773. if (data.title) {
  2774. option.title = data.title;
  2775. }
  2776. var $option = $(option);
  2777. var normalizedData = this._normalizeItem(data);
  2778. normalizedData.element = option;
  2779. // Override the option's data with the combined data
  2780. Utils.StoreData(option, 'data', normalizedData);
  2781. return $option;
  2782. };
  2783. SelectAdapter.prototype.item = function ($option) {
  2784. var data = {};
  2785. data = Utils.GetData($option[0], 'data');
  2786. if (data != null) {
  2787. return data;
  2788. }
  2789. if ($option.is('option')) {
  2790. data = {
  2791. id: $option.val(),
  2792. text: $option.text(),
  2793. disabled: $option.prop('disabled'),
  2794. selected: $option.prop('selected'),
  2795. title: $option.prop('title')
  2796. };
  2797. } else if ($option.is('optgroup')) {
  2798. data = {
  2799. text: $option.prop('label'),
  2800. children: [],
  2801. title: $option.prop('title')
  2802. };
  2803. var $children = $option.children('option');
  2804. var children = [];
  2805. for (var c = 0; c < $children.length; c++) {
  2806. var $child = $($children[c]);
  2807. var child = this.item($child);
  2808. children.push(child);
  2809. }
  2810. data.children = children;
  2811. }
  2812. data = this._normalizeItem(data);
  2813. data.element = $option[0];
  2814. Utils.StoreData($option[0], 'data', data);
  2815. return data;
  2816. };
  2817. SelectAdapter.prototype._normalizeItem = function (item) {
  2818. if (item !== Object(item)) {
  2819. item = {
  2820. id: item,
  2821. text: item
  2822. };
  2823. }
  2824. item = $.extend({}, {
  2825. text: ''
  2826. }, item);
  2827. var defaults = {
  2828. selected: false,
  2829. disabled: false
  2830. };
  2831. if (item.id != null) {
  2832. item.id = item.id.toString();
  2833. }
  2834. if (item.text != null) {
  2835. item.text = item.text.toString();
  2836. }
  2837. if (item._resultId == null && item.id && this.container != null) {
  2838. item._resultId = this.generateResultId(this.container, item);
  2839. }
  2840. return $.extend({}, defaults, item);
  2841. };
  2842. SelectAdapter.prototype.matches = function (params, data) {
  2843. var matcher = this.options.get('matcher');
  2844. return matcher(params, data);
  2845. };
  2846. return SelectAdapter;
  2847. });
  2848. S2.define('select2/data/array', [
  2849. './select',
  2850. '../utils',
  2851. 'jquery'
  2852. ], function (SelectAdapter, Utils, $) {
  2853. function ArrayAdapter($element, options) {
  2854. var data = options.get('data') || [];
  2855. ArrayAdapter.__super__.constructor.call(this, $element, options);
  2856. this.addOptions(this.convertToOptions(data));
  2857. }
  2858. Utils.Extend(ArrayAdapter, SelectAdapter);
  2859. ArrayAdapter.prototype.select = function (data) {
  2860. var $option = this.$element.find('option').filter(function (i, elm) {
  2861. return elm.value == data.id.toString();
  2862. });
  2863. if ($option.length === 0) {
  2864. $option = this.option(data);
  2865. this.addOptions($option);
  2866. }
  2867. ArrayAdapter.__super__.select.call(this, data);
  2868. };
  2869. ArrayAdapter.prototype.convertToOptions = function (data) {
  2870. var self = this;
  2871. var $existing = this.$element.find('option');
  2872. var existingIds = $existing.map(function () {
  2873. return self.item($(this)).id;
  2874. }).get();
  2875. var $options = [];
  2876. // Filter out all items except for the one passed in the argument
  2877. function onlyItem(item) {
  2878. return function () {
  2879. return $(this).val() == item.id;
  2880. };
  2881. }
  2882. for (var d = 0; d < data.length; d++) {
  2883. var item = this._normalizeItem(data[d]);
  2884. // Skip items which were pre-loaded, only merge the data
  2885. if ($.inArray(item.id, existingIds) >= 0) {
  2886. var $existingOption = $existing.filter(onlyItem(item));
  2887. var existingData = this.item($existingOption);
  2888. var newData = $.extend(true, {}, item, existingData);
  2889. var $newOption = this.option(newData);
  2890. $existingOption.replaceWith($newOption);
  2891. continue;
  2892. }
  2893. var $option = this.option(item);
  2894. if (item.children) {
  2895. var $children = this.convertToOptions(item.children);
  2896. Utils.appendMany($option, $children);
  2897. }
  2898. $options.push($option);
  2899. }
  2900. return $options;
  2901. };
  2902. return ArrayAdapter;
  2903. });
  2904. S2.define('select2/data/ajax', [
  2905. './array',
  2906. '../utils',
  2907. 'jquery'
  2908. ], function (ArrayAdapter, Utils, $) {
  2909. function AjaxAdapter($element, options) {
  2910. this.ajaxOptions = this._applyDefaults(options.get('ajax'));
  2911. if (this.ajaxOptions.processResults != null) {
  2912. this.processResults = this.ajaxOptions.processResults;
  2913. }
  2914. AjaxAdapter.__super__.constructor.call(this, $element, options);
  2915. }
  2916. Utils.Extend(AjaxAdapter, ArrayAdapter);
  2917. AjaxAdapter.prototype._applyDefaults = function (options) {
  2918. var defaults = {
  2919. data: function (params) {
  2920. return $.extend({}, params, {
  2921. q: params.term
  2922. });
  2923. },
  2924. transport: function (params, success, failure) {
  2925. var $request = $.ajax(params);
  2926. $request.then(success);
  2927. $request.fail(failure);
  2928. return $request;
  2929. }
  2930. };
  2931. return $.extend({}, defaults, options, true);
  2932. };
  2933. AjaxAdapter.prototype.processResults = function (results) {
  2934. return results;
  2935. };
  2936. AjaxAdapter.prototype.query = function (params, callback) {
  2937. var matches = [];
  2938. var self = this;
  2939. if (this._request != null) {
  2940. // JSONP requests cannot always be aborted
  2941. if ($.isFunction(this._request.abort)) {
  2942. this._request.abort();
  2943. }
  2944. this._request = null;
  2945. }
  2946. var options = $.extend({
  2947. type: 'GET'
  2948. }, this.ajaxOptions);
  2949. if (typeof options.url === 'function') {
  2950. options.url = options.url.call(this.$element, params);
  2951. }
  2952. if (typeof options.data === 'function') {
  2953. options.data = options.data.call(this.$element, params);
  2954. }
  2955. function request() {
  2956. var $request = options.transport(options, function (data) {
  2957. var results = self.processResults(data, params);
  2958. if (self.options.get('debug') && window.console && console.error) {
  2959. // Check to make sure that the response included a `results` key.
  2960. if (!results || !results.results || !$.isArray(results.results)) {
  2961. console.error(
  2962. 'Select2: The AJAX results did not return an array in the ' +
  2963. '`results` key of the response.'
  2964. );
  2965. }
  2966. }
  2967. callback(results);
  2968. }, function () {
  2969. // Attempt to detect if a request was aborted
  2970. // Only works if the transport exposes a status property
  2971. if ('status' in $request &&
  2972. ($request.status === 0 || $request.status === '0')) {
  2973. return;
  2974. }
  2975. self.trigger('results:message', {
  2976. message: 'errorLoading'
  2977. });
  2978. });
  2979. self._request = $request;
  2980. }
  2981. if (this.ajaxOptions.delay && params.term != null) {
  2982. if (this._queryTimeout) {
  2983. window.clearTimeout(this._queryTimeout);
  2984. }
  2985. this._queryTimeout = window.setTimeout(request, this.ajaxOptions.delay);
  2986. } else {
  2987. request();
  2988. }
  2989. };
  2990. return AjaxAdapter;
  2991. });
  2992. S2.define('select2/data/tags', [
  2993. 'jquery'
  2994. ], function ($) {
  2995. function Tags(decorated, $element, options) {
  2996. var tags = options.get('tags');
  2997. var createTag = options.get('createTag');
  2998. if (createTag !== undefined) {
  2999. this.createTag = createTag;
  3000. }
  3001. var insertTag = options.get('insertTag');
  3002. if (insertTag !== undefined) {
  3003. this.insertTag = insertTag;
  3004. }
  3005. decorated.call(this, $element, options);
  3006. if ($.isArray(tags)) {
  3007. for (var t = 0; t < tags.length; t++) {
  3008. var tag = tags[t];
  3009. var item = this._normalizeItem(tag);
  3010. var $option = this.option(item);
  3011. this.$element.append($option);
  3012. }
  3013. }
  3014. }
  3015. Tags.prototype.query = function (decorated, params, callback) {
  3016. var self = this;
  3017. this._removeOldTags();
  3018. if (params.term == null || params.page != null) {
  3019. decorated.call(this, params, callback);
  3020. return;
  3021. }
  3022. function wrapper(obj, child) {
  3023. var data = obj.results;
  3024. for (var i = 0; i < data.length; i++) {
  3025. var option = data[i];
  3026. var checkChildren = (
  3027. option.children != null &&
  3028. !wrapper({
  3029. results: option.children
  3030. }, true)
  3031. );
  3032. var optionText = (option.text || '').toUpperCase();
  3033. var paramsTerm = (params.term || '').toUpperCase();
  3034. var checkText = optionText === paramsTerm;
  3035. if (checkText || checkChildren) {
  3036. if (child) {
  3037. return false;
  3038. }
  3039. obj.data = data;
  3040. callback(obj);
  3041. return;
  3042. }
  3043. }
  3044. if (child) {
  3045. return true;
  3046. }
  3047. var tag = self.createTag(params);
  3048. if (tag != null) {
  3049. var $option = self.option(tag);
  3050. $option.attr('data-select2-tag', true);
  3051. self.addOptions([$option]);
  3052. self.insertTag(data, tag);
  3053. }
  3054. obj.results = data;
  3055. callback(obj);
  3056. }
  3057. decorated.call(this, params, wrapper);
  3058. };
  3059. Tags.prototype.createTag = function (decorated, params) {
  3060. var term = $.trim(params.term);
  3061. if (term === '') {
  3062. return null;
  3063. }
  3064. return {
  3065. id: term,
  3066. text: term
  3067. };
  3068. };
  3069. Tags.prototype.insertTag = function (_, data, tag) {
  3070. data.unshift(tag);
  3071. };
  3072. Tags.prototype._removeOldTags = function (_) {
  3073. var tag = this._lastTag;
  3074. var $options = this.$element.find('option[data-select2-tag]');
  3075. $options.each(function () {
  3076. if (this.selected) {
  3077. return;
  3078. }
  3079. $(this).remove();
  3080. });
  3081. };
  3082. return Tags;
  3083. });
  3084. S2.define('select2/data/tokenizer', [
  3085. 'jquery'
  3086. ], function ($) {
  3087. function Tokenizer(decorated, $element, options) {
  3088. var tokenizer = options.get('tokenizer');
  3089. if (tokenizer !== undefined) {
  3090. this.tokenizer = tokenizer;
  3091. }
  3092. decorated.call(this, $element, options);
  3093. }
  3094. Tokenizer.prototype.bind = function (decorated, container, $container) {
  3095. decorated.call(this, container, $container);
  3096. this.$search = container.dropdown.$search || container.selection.$search ||
  3097. $container.find('.select2-search__field');
  3098. };
  3099. Tokenizer.prototype.query = function (decorated, params, callback) {
  3100. var self = this;
  3101. function createAndSelect(data) {
  3102. // Normalize the data object so we can use it for checks
  3103. var item = self._normalizeItem(data);
  3104. // Check if the data object already exists as a tag
  3105. // Select it if it doesn't
  3106. var $existingOptions = self.$element.find('option').filter(function () {
  3107. return $(this).val() === item.id;
  3108. });
  3109. // If an existing option wasn't found for it, create the option
  3110. if (!$existingOptions.length) {
  3111. var $option = self.option(item);
  3112. $option.attr('data-select2-tag', true);
  3113. self._removeOldTags();
  3114. self.addOptions([$option]);
  3115. }
  3116. // Select the item, now that we know there is an option for it
  3117. select(item);
  3118. }
  3119. function select(data) {
  3120. self.trigger('select', {
  3121. data: data
  3122. });
  3123. }
  3124. params.term = params.term || '';
  3125. var tokenData = this.tokenizer(params, this.options, createAndSelect);
  3126. if (tokenData.term !== params.term) {
  3127. // Replace the search term if we have the search box
  3128. if (this.$search.length) {
  3129. this.$search.val(tokenData.term);
  3130. this.$search.focus();
  3131. }
  3132. params.term = tokenData.term;
  3133. }
  3134. decorated.call(this, params, callback);
  3135. };
  3136. Tokenizer.prototype.tokenizer = function (_, params, options, callback) {
  3137. var separators = options.get('tokenSeparators') || [];
  3138. var term = params.term;
  3139. var i = 0;
  3140. var createTag = this.createTag || function (params) {
  3141. return {
  3142. id: params.term,
  3143. text: params.term
  3144. };
  3145. };
  3146. while (i < term.length) {
  3147. var termChar = term[i];
  3148. if ($.inArray(termChar, separators) === -1) {
  3149. i++;
  3150. continue;
  3151. }
  3152. var part = term.substr(0, i);
  3153. var partParams = $.extend({}, params, {
  3154. term: part
  3155. });
  3156. var data = createTag(partParams);
  3157. if (data == null) {
  3158. i++;
  3159. continue;
  3160. }
  3161. callback(data);
  3162. // Reset the term to not include the tokenized portion
  3163. term = term.substr(i + 1) || '';
  3164. i = 0;
  3165. }
  3166. return {
  3167. term: term
  3168. };
  3169. };
  3170. return Tokenizer;
  3171. });
  3172. S2.define('select2/data/minimumInputLength', [
  3173. ], function () {
  3174. function MinimumInputLength(decorated, $e, options) {
  3175. this.minimumInputLength = options.get('minimumInputLength');
  3176. decorated.call(this, $e, options);
  3177. }
  3178. MinimumInputLength.prototype.query = function (decorated, params, callback) {
  3179. params.term = params.term || '';
  3180. if (params.term.length < this.minimumInputLength) {
  3181. this.trigger('results:message', {
  3182. message: 'inputTooShort',
  3183. args: {
  3184. minimum: this.minimumInputLength,
  3185. input: params.term,
  3186. params: params
  3187. }
  3188. });
  3189. return;
  3190. }
  3191. decorated.call(this, params, callback);
  3192. };
  3193. return MinimumInputLength;
  3194. });
  3195. S2.define('select2/data/maximumInputLength', [
  3196. ], function () {
  3197. function MaximumInputLength(decorated, $e, options) {
  3198. this.maximumInputLength = options.get('maximumInputLength');
  3199. decorated.call(this, $e, options);
  3200. }
  3201. MaximumInputLength.prototype.query = function (decorated, params, callback) {
  3202. params.term = params.term || '';
  3203. if (this.maximumInputLength > 0 &&
  3204. params.term.length > this.maximumInputLength) {
  3205. this.trigger('results:message', {
  3206. message: 'inputTooLong',
  3207. args: {
  3208. maximum: this.maximumInputLength,
  3209. input: params.term,
  3210. params: params
  3211. }
  3212. });
  3213. return;
  3214. }
  3215. decorated.call(this, params, callback);
  3216. };
  3217. return MaximumInputLength;
  3218. });
  3219. S2.define('select2/data/maximumSelectionLength', [
  3220. ], function () {
  3221. function MaximumSelectionLength(decorated, $e, options) {
  3222. this.maximumSelectionLength = options.get('maximumSelectionLength');
  3223. decorated.call(this, $e, options);
  3224. }
  3225. MaximumSelectionLength.prototype.query =
  3226. function (decorated, params, callback) {
  3227. var self = this;
  3228. this.current(function (currentData) {
  3229. var count = currentData != null ? currentData.length : 0;
  3230. if (self.maximumSelectionLength > 0 &&
  3231. count >= self.maximumSelectionLength) {
  3232. self.trigger('results:message', {
  3233. message: 'maximumSelected',
  3234. args: {
  3235. maximum: self.maximumSelectionLength
  3236. }
  3237. });
  3238. return;
  3239. }
  3240. decorated.call(self, params, callback);
  3241. });
  3242. };
  3243. return MaximumSelectionLength;
  3244. });
  3245. S2.define('select2/dropdown', [
  3246. 'jquery',
  3247. './utils'
  3248. ], function ($, Utils) {
  3249. function Dropdown($element, options) {
  3250. this.$element = $element;
  3251. this.options = options;
  3252. Dropdown.__super__.constructor.call(this);
  3253. }
  3254. Utils.Extend(Dropdown, Utils.Observable);
  3255. Dropdown.prototype.render = function () {
  3256. var $dropdown = $(
  3257. '<span class="select2-dropdown">' +
  3258. '<span class="select2-results"></span>' +
  3259. '</span>'
  3260. );
  3261. $dropdown.attr('dir', this.options.get('dir'));
  3262. this.$dropdown = $dropdown;
  3263. return $dropdown;
  3264. };
  3265. Dropdown.prototype.bind = function () {
  3266. // Should be implemented in subclasses
  3267. };
  3268. Dropdown.prototype.position = function ($dropdown, $container) {
  3269. // Should be implmented in subclasses
  3270. };
  3271. Dropdown.prototype.destroy = function () {
  3272. // Remove the dropdown from the DOM
  3273. this.$dropdown.remove();
  3274. };
  3275. return Dropdown;
  3276. });
  3277. S2.define('select2/dropdown/search', [
  3278. 'jquery',
  3279. '../utils'
  3280. ], function ($, Utils) {
  3281. function Search() { }
  3282. Search.prototype.render = function (decorated) {
  3283. var $rendered = decorated.call(this);
  3284. var $search = $(
  3285. '<span class="select2-search select2-search--dropdown">' +
  3286. '<input class="select2-search__field" type="search" tabindex="-1"' +
  3287. ' autocomplete="off" autocorrect="off" autocapitalize="none"' +
  3288. ' spellcheck="false" role="textbox" />' +
  3289. '</span>'
  3290. );
  3291. this.$searchContainer = $search;
  3292. this.$search = $search.find('input');
  3293. $rendered.prepend($search);
  3294. return $rendered;
  3295. };
  3296. Search.prototype.bind = function (decorated, container, $container) {
  3297. var self = this;
  3298. decorated.call(this, container, $container);
  3299. this.$search.on('keydown', function (evt) {
  3300. self.trigger('keypress', evt);
  3301. self._keyUpPrevented = evt.isDefaultPrevented();
  3302. });
  3303. // Workaround for browsers which do not support the `input` event
  3304. // This will prevent double-triggering of events for browsers which support
  3305. // both the `keyup` and `input` events.
  3306. this.$search.on('input', function (evt) {
  3307. // Unbind the duplicated `keyup` event
  3308. $(this).off('keyup');
  3309. });
  3310. this.$search.on('keyup input', function (evt) {
  3311. self.handleSearch(evt);
  3312. });
  3313. container.on('open', function () {
  3314. self.$search.attr('tabindex', 0);
  3315. self.$search.focus();
  3316. window.setTimeout(function () {
  3317. self.$search.focus();
  3318. }, 0);
  3319. });
  3320. container.on('close', function () {
  3321. self.$search.attr('tabindex', -1);
  3322. self.$search.val('');
  3323. self.$search.blur();
  3324. });
  3325. container.on('focus', function () {
  3326. if (!container.isOpen()) {
  3327. self.$search.focus();
  3328. }
  3329. });
  3330. container.on('results:all', function (params) {
  3331. if (params.query.term == null || params.query.term === '') {
  3332. var showSearch = self.showSearch(params);
  3333. if (showSearch) {
  3334. self.$searchContainer.removeClass('select2-search--hide');
  3335. } else {
  3336. self.$searchContainer.addClass('select2-search--hide');
  3337. }
  3338. }
  3339. });
  3340. };
  3341. Search.prototype.handleSearch = function (evt) {
  3342. if (!this._keyUpPrevented) {
  3343. var input = this.$search.val();
  3344. this.trigger('query', {
  3345. term: input
  3346. });
  3347. }
  3348. this._keyUpPrevented = false;
  3349. };
  3350. Search.prototype.showSearch = function (_, params) {
  3351. return true;
  3352. };
  3353. return Search;
  3354. });
  3355. S2.define('select2/dropdown/hidePlaceholder', [
  3356. ], function () {
  3357. function HidePlaceholder(decorated, $element, options, dataAdapter) {
  3358. this.placeholder = this.normalizePlaceholder(options.get('placeholder'));
  3359. decorated.call(this, $element, options, dataAdapter);
  3360. }
  3361. HidePlaceholder.prototype.append = function (decorated, data) {
  3362. data.results = this.removePlaceholder(data.results);
  3363. decorated.call(this, data);
  3364. };
  3365. HidePlaceholder.prototype.normalizePlaceholder = function (_, placeholder) {
  3366. if (typeof placeholder === 'string') {
  3367. placeholder = {
  3368. id: '',
  3369. text: placeholder
  3370. };
  3371. }
  3372. return placeholder;
  3373. };
  3374. HidePlaceholder.prototype.removePlaceholder = function (_, data) {
  3375. var modifiedData = data.slice(0);
  3376. for (var d = data.length - 1; d >= 0; d--) {
  3377. var item = data[d];
  3378. if (this.placeholder.id === item.id) {
  3379. modifiedData.splice(d, 1);
  3380. }
  3381. }
  3382. return modifiedData;
  3383. };
  3384. return HidePlaceholder;
  3385. });
  3386. S2.define('select2/dropdown/infiniteScroll', [
  3387. 'jquery'
  3388. ], function ($) {
  3389. function InfiniteScroll(decorated, $element, options, dataAdapter) {
  3390. this.lastParams = {};
  3391. decorated.call(this, $element, options, dataAdapter);
  3392. this.$loadingMore = this.createLoadingMore();
  3393. this.loading = false;
  3394. }
  3395. InfiniteScroll.prototype.append = function (decorated, data) {
  3396. this.$loadingMore.remove();
  3397. this.loading = false;
  3398. decorated.call(this, data);
  3399. if (this.showLoadingMore(data)) {
  3400. this.$results.append(this.$loadingMore);
  3401. }
  3402. };
  3403. InfiniteScroll.prototype.bind = function (decorated, container, $container) {
  3404. var self = this;
  3405. decorated.call(this, container, $container);
  3406. container.on('query', function (params) {
  3407. self.lastParams = params;
  3408. self.loading = true;
  3409. });
  3410. container.on('query:append', function (params) {
  3411. self.lastParams = params;
  3412. self.loading = true;
  3413. });
  3414. this.$results.on('scroll', function () {
  3415. var isLoadMoreVisible = $.contains(
  3416. document.documentElement,
  3417. self.$loadingMore[0]
  3418. );
  3419. if (self.loading || !isLoadMoreVisible) {
  3420. return;
  3421. }
  3422. var currentOffset = self.$results.offset().top +
  3423. self.$results.outerHeight(false);
  3424. var loadingMoreOffset = self.$loadingMore.offset().top +
  3425. self.$loadingMore.outerHeight(false);
  3426. if (currentOffset + 50 >= loadingMoreOffset) {
  3427. self.loadMore();
  3428. }
  3429. });
  3430. };
  3431. InfiniteScroll.prototype.loadMore = function () {
  3432. this.loading = true;
  3433. var params = $.extend({}, { page: 1 }, this.lastParams);
  3434. params.page++;
  3435. this.trigger('query:append', params);
  3436. };
  3437. InfiniteScroll.prototype.showLoadingMore = function (_, data) {
  3438. return data.pagination && data.pagination.more;
  3439. };
  3440. InfiniteScroll.prototype.createLoadingMore = function () {
  3441. var $option = $(
  3442. '<li ' +
  3443. 'class="select2-results__option select2-results__option--load-more"' +
  3444. 'role="treeitem" aria-disabled="true"></li>'
  3445. );
  3446. var message = this.options.get('translations').get('loadingMore');
  3447. $option.html(message(this.lastParams));
  3448. return $option;
  3449. };
  3450. return InfiniteScroll;
  3451. });
  3452. S2.define('select2/dropdown/attachBody', [
  3453. 'jquery',
  3454. '../utils'
  3455. ], function ($, Utils) {
  3456. function AttachBody(decorated, $element, options) {
  3457. this.$dropdownParent = options.get('dropdownParent') || $(document.body);
  3458. decorated.call(this, $element, options);
  3459. }
  3460. AttachBody.prototype.bind = function (decorated, container, $container) {
  3461. var self = this;
  3462. var setupResultsEvents = false;
  3463. decorated.call(this, container, $container);
  3464. container.on('open', function () {
  3465. self._showDropdown();
  3466. self._attachPositioningHandler(container);
  3467. if (!setupResultsEvents) {
  3468. setupResultsEvents = true;
  3469. container.on('results:all', function () {
  3470. self._positionDropdown();
  3471. self._resizeDropdown();
  3472. });
  3473. container.on('results:append', function () {
  3474. self._positionDropdown();
  3475. self._resizeDropdown();
  3476. });
  3477. }
  3478. });
  3479. container.on('close', function () {
  3480. self._hideDropdown();
  3481. self._detachPositioningHandler(container);
  3482. });
  3483. this.$dropdownContainer.on('mousedown', function (evt) {
  3484. evt.stopPropagation();
  3485. });
  3486. };
  3487. AttachBody.prototype.destroy = function (decorated) {
  3488. decorated.call(this);
  3489. this.$dropdownContainer.remove();
  3490. };
  3491. AttachBody.prototype.position = function (decorated, $dropdown, $container) {
  3492. // Clone all of the container classes
  3493. $dropdown.attr('class', $container.attr('class'));
  3494. $dropdown.removeClass('select2');
  3495. $dropdown.addClass('select2-container--open');
  3496. $dropdown.css({
  3497. position: 'absolute',
  3498. top: -999999
  3499. });
  3500. this.$container = $container;
  3501. };
  3502. AttachBody.prototype.render = function (decorated) {
  3503. var $container = $('<span></span>');
  3504. var $dropdown = decorated.call(this);
  3505. $container.append($dropdown);
  3506. this.$dropdownContainer = $container;
  3507. return $container;
  3508. };
  3509. AttachBody.prototype._hideDropdown = function (decorated) {
  3510. this.$dropdownContainer.detach();
  3511. };
  3512. AttachBody.prototype._attachPositioningHandler =
  3513. function (decorated, container) {
  3514. var self = this;
  3515. var scrollEvent = 'scroll.select2.' + container.id;
  3516. var resizeEvent = 'resize.select2.' + container.id;
  3517. var orientationEvent = 'orientationchange.select2.' + container.id;
  3518. var $watchers = this.$container.parents().filter(Utils.hasScroll);
  3519. $watchers.each(function () {
  3520. Utils.StoreData(this, 'select2-scroll-position', {
  3521. x: $(this).scrollLeft(),
  3522. y: $(this).scrollTop()
  3523. });
  3524. });
  3525. $watchers.on(scrollEvent, function (ev) {
  3526. var position = Utils.GetData(this, 'select2-scroll-position');
  3527. $(this).scrollTop(position.y);
  3528. });
  3529. $(window).on(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent,
  3530. function (e) {
  3531. self._positionDropdown();
  3532. self._resizeDropdown();
  3533. });
  3534. };
  3535. AttachBody.prototype._detachPositioningHandler =
  3536. function (decorated, container) {
  3537. var scrollEvent = 'scroll.select2.' + container.id;
  3538. var resizeEvent = 'resize.select2.' + container.id;
  3539. var orientationEvent = 'orientationchange.select2.' + container.id;
  3540. var $watchers = this.$container.parents().filter(Utils.hasScroll);
  3541. $watchers.off(scrollEvent);
  3542. $(window).off(scrollEvent + ' ' + resizeEvent + ' ' + orientationEvent);
  3543. };
  3544. AttachBody.prototype._positionDropdown = function () {
  3545. var $window = $(window);
  3546. var isCurrentlyAbove = this.$dropdown.hasClass('select2-dropdown--above');
  3547. var isCurrentlyBelow = this.$dropdown.hasClass('select2-dropdown--below');
  3548. var newDirection = null;
  3549. var offset = this.$container.offset();
  3550. offset.bottom = offset.top + this.$container.outerHeight(false);
  3551. var container = {
  3552. height: this.$container.outerHeight(false)
  3553. };
  3554. container.top = offset.top;
  3555. container.bottom = offset.top + container.height;
  3556. var dropdown = {
  3557. height: this.$dropdown.outerHeight(false)
  3558. };
  3559. var viewport = {
  3560. top: $window.scrollTop(),
  3561. bottom: $window.scrollTop() + $window.height()
  3562. };
  3563. var enoughRoomAbove = viewport.top < (offset.top - dropdown.height);
  3564. var enoughRoomBelow = viewport.bottom > (offset.bottom + dropdown.height);
  3565. var css = {
  3566. left: offset.left,
  3567. top: container.bottom
  3568. };
  3569. // Determine what the parent element is to use for calciulating the offset
  3570. var $offsetParent = this.$dropdownParent;
  3571. // For statically positoned elements, we need to get the element
  3572. // that is determining the offset
  3573. if ($offsetParent.css('position') === 'static') {
  3574. $offsetParent = $offsetParent.offsetParent();
  3575. }
  3576. var parentOffset = $offsetParent.offset();
  3577. css.top -= parentOffset.top;
  3578. css.left -= parentOffset.left;
  3579. if (!isCurrentlyAbove && !isCurrentlyBelow) {
  3580. newDirection = 'below';
  3581. }
  3582. if (!enoughRoomBelow && enoughRoomAbove && !isCurrentlyAbove) {
  3583. newDirection = 'above';
  3584. } else if (!enoughRoomAbove && enoughRoomBelow && isCurrentlyAbove) {
  3585. newDirection = 'below';
  3586. }
  3587. if (newDirection == 'above' ||
  3588. (isCurrentlyAbove && newDirection !== 'below')) {
  3589. css.top = container.top - parentOffset.top - dropdown.height;
  3590. }
  3591. if (newDirection != null) {
  3592. this.$dropdown
  3593. .removeClass('select2-dropdown--below select2-dropdown--above')
  3594. .addClass('select2-dropdown--' + newDirection);
  3595. this.$container
  3596. .removeClass('select2-container--below select2-container--above')
  3597. .addClass('select2-container--' + newDirection);
  3598. }
  3599. this.$dropdownContainer.css(css);
  3600. };
  3601. AttachBody.prototype._resizeDropdown = function () {
  3602. var css = {
  3603. width: this.$container.outerWidth(false) + 'px'
  3604. };
  3605. if (this.options.get('dropdownAutoWidth')) {
  3606. css.minWidth = css.width;
  3607. css.position = 'relative';
  3608. css.width = 'auto';
  3609. }
  3610. this.$dropdown.css(css);
  3611. };
  3612. AttachBody.prototype._showDropdown = function (decorated) {
  3613. this.$dropdownContainer.appendTo(this.$dropdownParent);
  3614. this._positionDropdown();
  3615. this._resizeDropdown();
  3616. };
  3617. return AttachBody;
  3618. });
  3619. S2.define('select2/dropdown/minimumResultsForSearch', [
  3620. ], function () {
  3621. function countResults(data) {
  3622. var count = 0;
  3623. for (var d = 0; d < data.length; d++) {
  3624. var item = data[d];
  3625. if (item.children) {
  3626. count += countResults(item.children);
  3627. } else {
  3628. count++;
  3629. }
  3630. }
  3631. return count;
  3632. }
  3633. function MinimumResultsForSearch(decorated, $element, options, dataAdapter) {
  3634. this.minimumResultsForSearch = options.get('minimumResultsForSearch');
  3635. if (this.minimumResultsForSearch < 0) {
  3636. this.minimumResultsForSearch = Infinity;
  3637. }
  3638. decorated.call(this, $element, options, dataAdapter);
  3639. }
  3640. MinimumResultsForSearch.prototype.showSearch = function (decorated, params) {
  3641. if (countResults(params.data.results) < this.minimumResultsForSearch) {
  3642. return false;
  3643. }
  3644. return decorated.call(this, params);
  3645. };
  3646. return MinimumResultsForSearch;
  3647. });
  3648. S2.define('select2/dropdown/selectOnClose', [
  3649. '../utils'
  3650. ], function (Utils) {
  3651. function SelectOnClose() { }
  3652. SelectOnClose.prototype.bind = function (decorated, container, $container) {
  3653. var self = this;
  3654. decorated.call(this, container, $container);
  3655. container.on('close', function (params) {
  3656. self._handleSelectOnClose(params);
  3657. });
  3658. };
  3659. SelectOnClose.prototype._handleSelectOnClose = function (_, params) {
  3660. if (params && params.originalSelect2Event != null) {
  3661. var event = params.originalSelect2Event;
  3662. // Don't select an item if the close event was triggered from a select or
  3663. // unselect event
  3664. if (event._type === 'select' || event._type === 'unselect') {
  3665. return;
  3666. }
  3667. }
  3668. var $highlightedResults = this.getHighlightedResults();
  3669. // Only select highlighted results
  3670. if ($highlightedResults.length < 1) {
  3671. return;
  3672. }
  3673. var data = Utils.GetData($highlightedResults[0], 'data');
  3674. // Don't re-select already selected resulte
  3675. if (
  3676. (data.element != null && data.element.selected) ||
  3677. (data.element == null && data.selected)
  3678. ) {
  3679. return;
  3680. }
  3681. this.trigger('select', {
  3682. data: data
  3683. });
  3684. };
  3685. return SelectOnClose;
  3686. });
  3687. S2.define('select2/dropdown/closeOnSelect', [
  3688. ], function () {
  3689. function CloseOnSelect() { }
  3690. CloseOnSelect.prototype.bind = function (decorated, container, $container) {
  3691. var self = this;
  3692. decorated.call(this, container, $container);
  3693. container.on('select', function (evt) {
  3694. self._selectTriggered(evt);
  3695. });
  3696. container.on('unselect', function (evt) {
  3697. self._selectTriggered(evt);
  3698. });
  3699. };
  3700. CloseOnSelect.prototype._selectTriggered = function (_, evt) {
  3701. var originalEvent = evt.originalEvent;
  3702. // Don't close if the control key is being held
  3703. if (originalEvent && originalEvent.ctrlKey) {
  3704. return;
  3705. }
  3706. this.trigger('close', {
  3707. originalEvent: originalEvent,
  3708. originalSelect2Event: evt
  3709. });
  3710. };
  3711. return CloseOnSelect;
  3712. });
  3713. S2.define('select2/i18n/en', [], function () {
  3714. // English
  3715. return {
  3716. errorLoading: function () {
  3717. return 'The results could not be loaded.';
  3718. },
  3719. inputTooLong: function (args) {
  3720. var overChars = args.input.length - args.maximum;
  3721. var message = 'Please delete ' + overChars + ' character';
  3722. if (overChars != 1) {
  3723. message += 's';
  3724. }
  3725. return message;
  3726. },
  3727. inputTooShort: function (args) {
  3728. var remainingChars = args.minimum - args.input.length;
  3729. var message = 'Please enter ' + remainingChars + ' or more characters';
  3730. return message;
  3731. },
  3732. loadingMore: function () {
  3733. return 'Loading more results…';
  3734. },
  3735. maximumSelected: function (args) {
  3736. var message = 'You can only select ' + args.maximum + ' item';
  3737. if (args.maximum != 1) {
  3738. message += 's';
  3739. }
  3740. return message;
  3741. },
  3742. noResults: function () {
  3743. return 'No results found';
  3744. },
  3745. searching: function () {
  3746. return 'Searching…';
  3747. }
  3748. };
  3749. });
  3750. S2.define('select2/defaults', [
  3751. 'jquery',
  3752. 'require',
  3753. './results',
  3754. './selection/single',
  3755. './selection/multiple',
  3756. './selection/placeholder',
  3757. './selection/allowClear',
  3758. './selection/search',
  3759. './selection/eventRelay',
  3760. './utils',
  3761. './translation',
  3762. './diacritics',
  3763. './data/select',
  3764. './data/array',
  3765. './data/ajax',
  3766. './data/tags',
  3767. './data/tokenizer',
  3768. './data/minimumInputLength',
  3769. './data/maximumInputLength',
  3770. './data/maximumSelectionLength',
  3771. './dropdown',
  3772. './dropdown/search',
  3773. './dropdown/hidePlaceholder',
  3774. './dropdown/infiniteScroll',
  3775. './dropdown/attachBody',
  3776. './dropdown/minimumResultsForSearch',
  3777. './dropdown/selectOnClose',
  3778. './dropdown/closeOnSelect',
  3779. './i18n/en'
  3780. ], function ($, require,
  3781. ResultsList,
  3782. SingleSelection, MultipleSelection, Placeholder, AllowClear,
  3783. SelectionSearch, EventRelay,
  3784. Utils, Translation, DIACRITICS,
  3785. SelectData, ArrayData, AjaxData, Tags, Tokenizer,
  3786. MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
  3787. Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
  3788. AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
  3789. EnglishTranslation) {
  3790. function Defaults() {
  3791. this.reset();
  3792. }
  3793. Defaults.prototype.apply = function (options) {
  3794. options = $.extend(true, {}, this.defaults, options);
  3795. if (options.dataAdapter == null) {
  3796. if (options.ajax != null) {
  3797. options.dataAdapter = AjaxData;
  3798. } else if (options.data != null) {
  3799. options.dataAdapter = ArrayData;
  3800. } else {
  3801. options.dataAdapter = SelectData;
  3802. }
  3803. if (options.minimumInputLength > 0) {
  3804. options.dataAdapter = Utils.Decorate(
  3805. options.dataAdapter,
  3806. MinimumInputLength
  3807. );
  3808. }
  3809. if (options.maximumInputLength > 0) {
  3810. options.dataAdapter = Utils.Decorate(
  3811. options.dataAdapter,
  3812. MaximumInputLength
  3813. );
  3814. }
  3815. if (options.maximumSelectionLength > 0) {
  3816. options.dataAdapter = Utils.Decorate(
  3817. options.dataAdapter,
  3818. MaximumSelectionLength
  3819. );
  3820. }
  3821. if (options.tags) {
  3822. options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
  3823. }
  3824. if (options.tokenSeparators != null || options.tokenizer != null) {
  3825. options.dataAdapter = Utils.Decorate(
  3826. options.dataAdapter,
  3827. Tokenizer
  3828. );
  3829. }
  3830. if (options.query != null) {
  3831. var Query = require(options.amdBase + 'compat/query');
  3832. options.dataAdapter = Utils.Decorate(
  3833. options.dataAdapter,
  3834. Query
  3835. );
  3836. }
  3837. if (options.initSelection != null) {
  3838. var InitSelection = require(options.amdBase + 'compat/initSelection');
  3839. options.dataAdapter = Utils.Decorate(
  3840. options.dataAdapter,
  3841. InitSelection
  3842. );
  3843. }
  3844. }
  3845. if (options.resultsAdapter == null) {
  3846. options.resultsAdapter = ResultsList;
  3847. if (options.ajax != null) {
  3848. options.resultsAdapter = Utils.Decorate(
  3849. options.resultsAdapter,
  3850. InfiniteScroll
  3851. );
  3852. }
  3853. if (options.placeholder != null) {
  3854. options.resultsAdapter = Utils.Decorate(
  3855. options.resultsAdapter,
  3856. HidePlaceholder
  3857. );
  3858. }
  3859. if (options.selectOnClose) {
  3860. options.resultsAdapter = Utils.Decorate(
  3861. options.resultsAdapter,
  3862. SelectOnClose
  3863. );
  3864. }
  3865. }
  3866. if (options.dropdownAdapter == null) {
  3867. if (options.multiple) {
  3868. options.dropdownAdapter = Dropdown;
  3869. } else {
  3870. var SearchableDropdown = Utils.Decorate(Dropdown, DropdownSearch);
  3871. options.dropdownAdapter = SearchableDropdown;
  3872. }
  3873. if (options.minimumResultsForSearch !== 0) {
  3874. options.dropdownAdapter = Utils.Decorate(
  3875. options.dropdownAdapter,
  3876. MinimumResultsForSearch
  3877. );
  3878. }
  3879. if (options.closeOnSelect) {
  3880. options.dropdownAdapter = Utils.Decorate(
  3881. options.dropdownAdapter,
  3882. CloseOnSelect
  3883. );
  3884. }
  3885. if (
  3886. options.dropdownCssClass != null ||
  3887. options.dropdownCss != null ||
  3888. options.adaptDropdownCssClass != null
  3889. ) {
  3890. var DropdownCSS = require(options.amdBase + 'compat/dropdownCss');
  3891. options.dropdownAdapter = Utils.Decorate(
  3892. options.dropdownAdapter,
  3893. DropdownCSS
  3894. );
  3895. }
  3896. options.dropdownAdapter = Utils.Decorate(
  3897. options.dropdownAdapter,
  3898. AttachBody
  3899. );
  3900. }
  3901. if (options.selectionAdapter == null) {
  3902. if (options.multiple) {
  3903. options.selectionAdapter = MultipleSelection;
  3904. } else {
  3905. options.selectionAdapter = SingleSelection;
  3906. }
  3907. // Add the placeholder mixin if a placeholder was specified
  3908. if (options.placeholder != null) {
  3909. options.selectionAdapter = Utils.Decorate(
  3910. options.selectionAdapter,
  3911. Placeholder
  3912. );
  3913. }
  3914. if (options.allowClear) {
  3915. options.selectionAdapter = Utils.Decorate(
  3916. options.selectionAdapter,
  3917. AllowClear
  3918. );
  3919. }
  3920. if (options.multiple) {
  3921. options.selectionAdapter = Utils.Decorate(
  3922. options.selectionAdapter,
  3923. SelectionSearch
  3924. );
  3925. }
  3926. if (
  3927. options.containerCssClass != null ||
  3928. options.containerCss != null ||
  3929. options.adaptContainerCssClass != null
  3930. ) {
  3931. var ContainerCSS = require(options.amdBase + 'compat/containerCss');
  3932. options.selectionAdapter = Utils.Decorate(
  3933. options.selectionAdapter,
  3934. ContainerCSS
  3935. );
  3936. }
  3937. options.selectionAdapter = Utils.Decorate(
  3938. options.selectionAdapter,
  3939. EventRelay
  3940. );
  3941. }
  3942. if (typeof options.language === 'string') {
  3943. // Check if the language is specified with a region
  3944. if (options.language.indexOf('-') > 0) {
  3945. // Extract the region information if it is included
  3946. var languageParts = options.language.split('-');
  3947. var baseLanguage = languageParts[0];
  3948. options.language = [options.language, baseLanguage];
  3949. } else {
  3950. options.language = [options.language];
  3951. }
  3952. }
  3953. if ($.isArray(options.language)) {
  3954. var languages = new Translation();
  3955. options.language.push('en');
  3956. var languageNames = options.language;
  3957. for (var l = 0; l < languageNames.length; l++) {
  3958. var name = languageNames[l];
  3959. var language = {};
  3960. try {
  3961. // Try to load it with the original name
  3962. language = Translation.loadPath(name);
  3963. } catch (e) {
  3964. try {
  3965. // If we couldn't load it, check if it wasn't the full path
  3966. name = this.defaults.amdLanguageBase + name;
  3967. language = Translation.loadPath(name);
  3968. } catch (ex) {
  3969. // The translation could not be loaded at all. Sometimes this is
  3970. // because of a configuration problem, other times this can be
  3971. // because of how Select2 helps load all possible translation files.
  3972. if (options.debug && window.console && console.warn) {
  3973. console.warn(
  3974. 'Select2: The language file for "' + name + '" could not be ' +
  3975. 'automatically loaded. A fallback will be used instead.'
  3976. );
  3977. }
  3978. continue;
  3979. }
  3980. }
  3981. languages.extend(language);
  3982. }
  3983. options.translations = languages;
  3984. } else {
  3985. var baseTranslation = Translation.loadPath(
  3986. this.defaults.amdLanguageBase + 'en'
  3987. );
  3988. var customTranslation = new Translation(options.language);
  3989. customTranslation.extend(baseTranslation);
  3990. options.translations = customTranslation;
  3991. }
  3992. return options;
  3993. };
  3994. Defaults.prototype.reset = function () {
  3995. function stripDiacritics(text) {
  3996. // Used 'uni range + named function' from http://jsperf.com/diacritics/18
  3997. function match(a) {
  3998. return DIACRITICS[a] || a;
  3999. }
  4000. return text.replace(/[^\u0000-\u007E]/g, match);
  4001. }
  4002. function matcher(params, data) {
  4003. // Always return the object if there is nothing to compare
  4004. if ($.trim(params.term) === '') {
  4005. return data;
  4006. }
  4007. // Do a recursive check for options with children
  4008. if (data.children && data.children.length > 0) {
  4009. // Clone the data object if there are children
  4010. // This is required as we modify the object to remove any non-matches
  4011. var match = $.extend(true, {}, data);
  4012. // Check each child of the option
  4013. for (var c = data.children.length - 1; c >= 0; c--) {
  4014. var child = data.children[c];
  4015. var matches = matcher(params, child);
  4016. // If there wasn't a match, remove the object in the array
  4017. if (matches == null) {
  4018. match.children.splice(c, 1);
  4019. }
  4020. }
  4021. // If any children matched, return the new object
  4022. if (match.children.length > 0) {
  4023. return match;
  4024. }
  4025. // If there were no matching children, check just the plain object
  4026. return matcher(params, match);
  4027. }
  4028. var original = stripDiacritics(data.text).toUpperCase();
  4029. var term = stripDiacritics(params.term).toUpperCase();
  4030. // Check if the text contains the term
  4031. if (original.indexOf(term) > -1) {
  4032. return data;
  4033. }
  4034. // If it doesn't contain the term, don't return anything
  4035. return null;
  4036. }
  4037. this.defaults = {
  4038. amdBase: './',
  4039. amdLanguageBase: './i18n/',
  4040. closeOnSelect: true,
  4041. debug: false,
  4042. dropdownAutoWidth: false,
  4043. escapeMarkup: Utils.escapeMarkup,
  4044. language: EnglishTranslation,
  4045. matcher: matcher,
  4046. minimumInputLength: 0,
  4047. maximumInputLength: 0,
  4048. maximumSelectionLength: 0,
  4049. minimumResultsForSearch: 0,
  4050. selectOnClose: false,
  4051. sorter: function (data) {
  4052. return data;
  4053. },
  4054. templateResult: function (result) {
  4055. return result.text;
  4056. },
  4057. templateSelection: function (selection) {
  4058. return selection.text;
  4059. },
  4060. theme: 'default',
  4061. width: 'resolve'
  4062. };
  4063. };
  4064. Defaults.prototype.set = function (key, value) {
  4065. var camelKey = $.camelCase(key);
  4066. var data = {};
  4067. data[camelKey] = value;
  4068. var convertedData = Utils._convertData(data);
  4069. $.extend(true, this.defaults, convertedData);
  4070. };
  4071. var defaults = new Defaults();
  4072. return defaults;
  4073. });
  4074. S2.define('select2/options', [
  4075. 'require',
  4076. 'jquery',
  4077. './defaults',
  4078. './utils'
  4079. ], function (require, $, Defaults, Utils) {
  4080. function Options(options, $element) {
  4081. this.options = options;
  4082. if ($element != null) {
  4083. this.fromElement($element);
  4084. }
  4085. this.options = Defaults.apply(this.options);
  4086. if ($element && $element.is('input')) {
  4087. var InputCompat = require(this.get('amdBase') + 'compat/inputData');
  4088. this.options.dataAdapter = Utils.Decorate(
  4089. this.options.dataAdapter,
  4090. InputCompat
  4091. );
  4092. }
  4093. }
  4094. Options.prototype.fromElement = function ($e) {
  4095. var excludedData = ['select2'];
  4096. if (this.options.multiple == null) {
  4097. this.options.multiple = $e.prop('multiple');
  4098. }
  4099. if (this.options.disabled == null) {
  4100. this.options.disabled = $e.prop('disabled');
  4101. }
  4102. if (this.options.language == null) {
  4103. if ($e.prop('lang')) {
  4104. this.options.language = $e.prop('lang').toLowerCase();
  4105. } else if ($e.closest('[lang]').prop('lang')) {
  4106. this.options.language = $e.closest('[lang]').prop('lang');
  4107. }
  4108. }
  4109. if (this.options.dir == null) {
  4110. if ($e.prop('dir')) {
  4111. this.options.dir = $e.prop('dir');
  4112. } else if ($e.closest('[dir]').prop('dir')) {
  4113. this.options.dir = $e.closest('[dir]').prop('dir');
  4114. } else {
  4115. this.options.dir = 'ltr';
  4116. }
  4117. }
  4118. $e.prop('disabled', this.options.disabled);
  4119. $e.prop('multiple', this.options.multiple);
  4120. if (Utils.GetData($e[0], 'select2Tags')) {
  4121. if (this.options.debug && window.console && console.warn) {
  4122. console.warn(
  4123. 'Select2: The `data-select2-tags` attribute has been changed to ' +
  4124. 'use the `data-data` and `data-tags="true"` attributes and will be ' +
  4125. 'removed in future versions of Select2.'
  4126. );
  4127. }
  4128. Utils.StoreData($e[0], 'data', Utils.GetData($e[0], 'select2Tags'));
  4129. Utils.StoreData($e[0], 'tags', true);
  4130. }
  4131. if (Utils.GetData($e[0], 'ajaxUrl')) {
  4132. if (this.options.debug && window.console && console.warn) {
  4133. console.warn(
  4134. 'Select2: The `data-ajax-url` attribute has been changed to ' +
  4135. '`data-ajax--url` and support for the old attribute will be removed' +
  4136. ' in future versions of Select2.'
  4137. );
  4138. }
  4139. $e.attr('ajax--url', Utils.GetData($e[0], 'ajaxUrl'));
  4140. Utils.StoreData($e[0], 'ajax-Url', Utils.GetData($e[0], 'ajaxUrl'));
  4141. }
  4142. var dataset = {};
  4143. // Prefer the element's `dataset` attribute if it exists
  4144. // jQuery 1.x does not correctly handle data attributes with multiple dashes
  4145. if ($.fn.jquery && $.fn.jquery.substr(0, 2) == '1.' && $e[0].dataset) {
  4146. dataset = $.extend(true, {}, $e[0].dataset, Utils.GetData($e[0]));
  4147. } else {
  4148. dataset = Utils.GetData($e[0]);
  4149. }
  4150. var data = $.extend(true, {}, dataset);
  4151. data = Utils._convertData(data);
  4152. for (var key in data) {
  4153. if ($.inArray(key, excludedData) > -1) {
  4154. continue;
  4155. }
  4156. if ($.isPlainObject(this.options[key])) {
  4157. $.extend(this.options[key], data[key]);
  4158. } else {
  4159. this.options[key] = data[key];
  4160. }
  4161. }
  4162. return this;
  4163. };
  4164. Options.prototype.get = function (key) {
  4165. return this.options[key];
  4166. };
  4167. Options.prototype.set = function (key, val) {
  4168. this.options[key] = val;
  4169. };
  4170. return Options;
  4171. });
  4172. S2.define('select2/core', [
  4173. 'jquery',
  4174. './options',
  4175. './utils',
  4176. './keys'
  4177. ], function ($, Options, Utils, KEYS) {
  4178. var Select2 = function ($element, options) {
  4179. if (Utils.GetData($element[0], 'select2') != null) {
  4180. Utils.GetData($element[0], 'select2').destroy();
  4181. }
  4182. this.$element = $element;
  4183. this.id = this._generateId($element);
  4184. options = options || {};
  4185. this.options = new Options(options, $element);
  4186. Select2.__super__.constructor.call(this);
  4187. // Set up the tabindex
  4188. var tabindex = $element.attr('tabindex') || 0;
  4189. Utils.StoreData($element[0], 'old-tabindex', tabindex);
  4190. $element.attr('tabindex', '-1');
  4191. // Set up containers and adapters
  4192. var DataAdapter = this.options.get('dataAdapter');
  4193. this.dataAdapter = new DataAdapter($element, this.options);
  4194. var $container = this.render();
  4195. this._placeContainer($container);
  4196. var SelectionAdapter = this.options.get('selectionAdapter');
  4197. this.selection = new SelectionAdapter($element, this.options);
  4198. this.$selection = this.selection.render();
  4199. this.selection.position(this.$selection, $container);
  4200. var DropdownAdapter = this.options.get('dropdownAdapter');
  4201. this.dropdown = new DropdownAdapter($element, this.options);
  4202. this.$dropdown = this.dropdown.render();
  4203. this.dropdown.position(this.$dropdown, $container);
  4204. var ResultsAdapter = this.options.get('resultsAdapter');
  4205. this.results = new ResultsAdapter($element, this.options, this.dataAdapter);
  4206. this.$results = this.results.render();
  4207. this.results.position(this.$results, this.$dropdown);
  4208. // Bind events
  4209. var self = this;
  4210. // Bind the container to all of the adapters
  4211. this._bindAdapters();
  4212. // Register any DOM event handlers
  4213. this._registerDomEvents();
  4214. // Register any internal event handlers
  4215. this._registerDataEvents();
  4216. this._registerSelectionEvents();
  4217. this._registerDropdownEvents();
  4218. this._registerResultsEvents();
  4219. this._registerEvents();
  4220. // Set the initial state
  4221. this.dataAdapter.current(function (initialData) {
  4222. self.trigger('selection:update', {
  4223. data: initialData
  4224. });
  4225. });
  4226. // Hide the original select
  4227. $element.addClass('select2-hidden-accessible');
  4228. $element.attr('aria-hidden', 'true');
  4229. // Synchronize any monitored attributes
  4230. this._syncAttributes();
  4231. Utils.StoreData($element[0], 'select2', this);
  4232. // Ensure backwards compatibility with $element.data('select2').
  4233. $element.data('select2', this);
  4234. };
  4235. Utils.Extend(Select2, Utils.Observable);
  4236. Select2.prototype._generateId = function ($element) {
  4237. var id = '';
  4238. if ($element.attr('id') != null) {
  4239. id = $element.attr('id');
  4240. } else if ($element.attr('name') != null) {
  4241. id = $element.attr('name') + '-' + Utils.generateChars(2);
  4242. } else {
  4243. id = Utils.generateChars(4);
  4244. }
  4245. id = id.replace(/(:|\.|\[|\]|,)/g, '');
  4246. id = 'select2-' + id;
  4247. return id;
  4248. };
  4249. Select2.prototype._placeContainer = function ($container) {
  4250. $container.insertAfter(this.$element);
  4251. var width = this._resolveWidth(this.$element, this.options.get('width'));
  4252. if (width != null) {
  4253. $container.css('width', width);
  4254. }
  4255. };
  4256. Select2.prototype._resolveWidth = function ($element, method) {
  4257. var WIDTH = /^width:(([-+]?([0-9]*\.)?[0-9]+)(px|em|ex|%|in|cm|mm|pt|pc))/i;
  4258. if (method == 'resolve') {
  4259. var styleWidth = this._resolveWidth($element, 'style');
  4260. if (styleWidth != null) {
  4261. return styleWidth;
  4262. }
  4263. return this._resolveWidth($element, 'element');
  4264. }
  4265. if (method == 'element') {
  4266. var elementWidth = $element.outerWidth(false);
  4267. if (elementWidth <= 0) {
  4268. return 'auto';
  4269. }
  4270. return elementWidth + 'px';
  4271. }
  4272. if (method == 'style') {
  4273. var style = $element.attr('style');
  4274. if (typeof (style) !== 'string') {
  4275. return null;
  4276. }
  4277. var attrs = style.split(';');
  4278. for (var i = 0, l = attrs.length; i < l; i = i + 1) {
  4279. var attr = attrs[i].replace(/\s/g, '');
  4280. var matches = attr.match(WIDTH);
  4281. if (matches !== null && matches.length >= 1) {
  4282. return matches[1];
  4283. }
  4284. }
  4285. return null;
  4286. }
  4287. return method;
  4288. };
  4289. Select2.prototype._bindAdapters = function () {
  4290. this.dataAdapter.bind(this, this.$container);
  4291. this.selection.bind(this, this.$container);
  4292. this.dropdown.bind(this, this.$container);
  4293. this.results.bind(this, this.$container);
  4294. };
  4295. Select2.prototype._registerDomEvents = function () {
  4296. var self = this;
  4297. this.$element.on('change.select2', function () {
  4298. self.dataAdapter.current(function (data) {
  4299. self.trigger('selection:update', {
  4300. data: data
  4301. });
  4302. });
  4303. });
  4304. this.$element.on('focus.select2', function (evt) {
  4305. self.trigger('focus', evt);
  4306. });
  4307. this._syncA = Utils.bind(this._syncAttributes, this);
  4308. this._syncS = Utils.bind(this._syncSubtree, this);
  4309. if (this.$element[0].attachEvent) {
  4310. this.$element[0].attachEvent('onpropertychange', this._syncA);
  4311. }
  4312. var observer = window.MutationObserver ||
  4313. window.WebKitMutationObserver ||
  4314. window.MozMutationObserver
  4315. ;
  4316. if (observer != null) {
  4317. this._observer = new observer(function (mutations) {
  4318. $.each(mutations, self._syncA);
  4319. $.each(mutations, self._syncS);
  4320. });
  4321. this._observer.observe(this.$element[0], {
  4322. attributes: true,
  4323. childList: true,
  4324. subtree: false
  4325. });
  4326. } else if (this.$element[0].addEventListener) {
  4327. this.$element[0].addEventListener(
  4328. 'DOMAttrModified',
  4329. self._syncA,
  4330. false
  4331. );
  4332. this.$element[0].addEventListener(
  4333. 'DOMNodeInserted',
  4334. self._syncS,
  4335. false
  4336. );
  4337. this.$element[0].addEventListener(
  4338. 'DOMNodeRemoved',
  4339. self._syncS,
  4340. false
  4341. );
  4342. }
  4343. };
  4344. Select2.prototype._registerDataEvents = function () {
  4345. var self = this;
  4346. this.dataAdapter.on('*', function (name, params) {
  4347. self.trigger(name, params);
  4348. });
  4349. };
  4350. Select2.prototype._registerSelectionEvents = function () {
  4351. var self = this;
  4352. var nonRelayEvents = ['toggle', 'focus'];
  4353. this.selection.on('toggle', function () {
  4354. self.toggleDropdown();
  4355. });
  4356. this.selection.on('focus', function (params) {
  4357. self.focus(params);
  4358. });
  4359. this.selection.on('*', function (name, params) {
  4360. if ($.inArray(name, nonRelayEvents) !== -1) {
  4361. return;
  4362. }
  4363. self.trigger(name, params);
  4364. });
  4365. };
  4366. Select2.prototype._registerDropdownEvents = function () {
  4367. var self = this;
  4368. this.dropdown.on('*', function (name, params) {
  4369. self.trigger(name, params);
  4370. });
  4371. };
  4372. Select2.prototype._registerResultsEvents = function () {
  4373. var self = this;
  4374. this.results.on('*', function (name, params) {
  4375. self.trigger(name, params);
  4376. });
  4377. };
  4378. Select2.prototype._registerEvents = function () {
  4379. var self = this;
  4380. this.on('open', function () {
  4381. self.$container.addClass('select2-container--open');
  4382. });
  4383. this.on('close', function () {
  4384. self.$container.removeClass('select2-container--open');
  4385. });
  4386. this.on('enable', function () {
  4387. self.$container.removeClass('select2-container--disabled');
  4388. });
  4389. this.on('disable', function () {
  4390. self.$container.addClass('select2-container--disabled');
  4391. });
  4392. this.on('blur', function () {
  4393. self.$container.removeClass('select2-container--focus');
  4394. });
  4395. this.on('query', function (params) {
  4396. if (!self.isOpen()) {
  4397. self.trigger('open', {});
  4398. }
  4399. this.dataAdapter.query(params, function (data) {
  4400. self.trigger('results:all', {
  4401. data: data,
  4402. query: params
  4403. });
  4404. });
  4405. });
  4406. this.on('query:append', function (params) {
  4407. this.dataAdapter.query(params, function (data) {
  4408. self.trigger('results:append', {
  4409. data: data,
  4410. query: params
  4411. });
  4412. });
  4413. });
  4414. this.on('keypress', function (evt) {
  4415. var key = evt.which;
  4416. if (self.isOpen()) {
  4417. if (key === KEYS.ESC || key === KEYS.TAB ||
  4418. (key === KEYS.UP && evt.altKey)) {
  4419. self.close();
  4420. evt.preventDefault();
  4421. } else if (key === KEYS.ENTER) {
  4422. self.trigger('results:select', {});
  4423. evt.preventDefault();
  4424. } else if ((key === KEYS.SPACE && evt.ctrlKey)) {
  4425. self.trigger('results:toggle', {});
  4426. evt.preventDefault();
  4427. } else if (key === KEYS.UP) {
  4428. self.trigger('results:previous', {});
  4429. evt.preventDefault();
  4430. } else if (key === KEYS.DOWN) {
  4431. self.trigger('results:next', {});
  4432. evt.preventDefault();
  4433. }
  4434. } else {
  4435. if (key === KEYS.ENTER || key === KEYS.SPACE ||
  4436. (key === KEYS.DOWN && evt.altKey)) {
  4437. self.open();
  4438. evt.preventDefault();
  4439. }
  4440. }
  4441. });
  4442. };
  4443. Select2.prototype._syncAttributes = function () {
  4444. this.options.set('disabled', this.$element.prop('disabled'));
  4445. if (this.options.get('disabled')) {
  4446. if (this.isOpen()) {
  4447. this.close();
  4448. }
  4449. this.trigger('disable', {});
  4450. } else {
  4451. this.trigger('enable', {});
  4452. }
  4453. };
  4454. Select2.prototype._syncSubtree = function (evt, mutations) {
  4455. var changed = false;
  4456. var self = this;
  4457. // Ignore any mutation events raised for elements that aren't options or
  4458. // optgroups. This handles the case when the select element is destroyed
  4459. if (
  4460. evt && evt.target && (
  4461. evt.target.nodeName !== 'OPTION' && evt.target.nodeName !== 'OPTGROUP'
  4462. )
  4463. ) {
  4464. return;
  4465. }
  4466. if (!mutations) {
  4467. // If mutation events aren't supported, then we can only assume that the
  4468. // change affected the selections
  4469. changed = true;
  4470. } else if (mutations.addedNodes && mutations.addedNodes.length > 0) {
  4471. for (var n = 0; n < mutations.addedNodes.length; n++) {
  4472. var node = mutations.addedNodes[n];
  4473. if (node.selected) {
  4474. changed = true;
  4475. }
  4476. }
  4477. } else if (mutations.removedNodes && mutations.removedNodes.length > 0) {
  4478. changed = true;
  4479. }
  4480. // Only re-pull the data if we think there is a change
  4481. if (changed) {
  4482. this.dataAdapter.current(function (currentData) {
  4483. self.trigger('selection:update', {
  4484. data: currentData
  4485. });
  4486. });
  4487. }
  4488. };
  4489. /**
  4490. * Override the trigger method to automatically trigger pre-events when
  4491. * there are events that can be prevented.
  4492. */
  4493. Select2.prototype.trigger = function (name, args) {
  4494. var actualTrigger = Select2.__super__.trigger;
  4495. var preTriggerMap = {
  4496. 'open': 'opening',
  4497. 'close': 'closing',
  4498. 'select': 'selecting',
  4499. 'unselect': 'unselecting',
  4500. 'clear': 'clearing'
  4501. };
  4502. if (args === undefined) {
  4503. args = {};
  4504. }
  4505. if (name in preTriggerMap) {
  4506. var preTriggerName = preTriggerMap[name];
  4507. var preTriggerArgs = {
  4508. prevented: false,
  4509. name: name,
  4510. args: args
  4511. };
  4512. actualTrigger.call(this, preTriggerName, preTriggerArgs);
  4513. if (preTriggerArgs.prevented) {
  4514. args.prevented = true;
  4515. return;
  4516. }
  4517. }
  4518. actualTrigger.call(this, name, args);
  4519. };
  4520. Select2.prototype.toggleDropdown = function () {
  4521. if (this.options.get('disabled')) {
  4522. return;
  4523. }
  4524. if (this.isOpen()) {
  4525. this.close();
  4526. } else {
  4527. this.open();
  4528. }
  4529. };
  4530. Select2.prototype.open = function () {
  4531. if (this.isOpen()) {
  4532. return;
  4533. }
  4534. this.trigger('query', {});
  4535. };
  4536. Select2.prototype.close = function () {
  4537. if (!this.isOpen()) {
  4538. return;
  4539. }
  4540. this.trigger('close', {});
  4541. };
  4542. Select2.prototype.isOpen = function () {
  4543. return this.$container.hasClass('select2-container--open');
  4544. };
  4545. Select2.prototype.hasFocus = function () {
  4546. return this.$container.hasClass('select2-container--focus');
  4547. };
  4548. Select2.prototype.focus = function (data) {
  4549. // No need to re-trigger focus events if we are already focused
  4550. if (this.hasFocus()) {
  4551. return;
  4552. }
  4553. this.$container.addClass('select2-container--focus');
  4554. this.trigger('focus', {});
  4555. };
  4556. Select2.prototype.enable = function (args) {
  4557. if (this.options.get('debug') && window.console && console.warn) {
  4558. console.warn(
  4559. 'Select2: The `select2("enable")` method has been deprecated and will' +
  4560. ' be removed in later Select2 versions. Use $element.prop("disabled")' +
  4561. ' instead.'
  4562. );
  4563. }
  4564. if (args == null || args.length === 0) {
  4565. args = [true];
  4566. }
  4567. var disabled = !args[0];
  4568. this.$element.prop('disabled', disabled);
  4569. };
  4570. Select2.prototype.data = function () {
  4571. if (this.options.get('debug') &&
  4572. arguments.length > 0 && window.console && console.warn) {
  4573. console.warn(
  4574. 'Select2: Data can no longer be set using `select2("data")`. You ' +
  4575. 'should consider setting the value instead using `$element.val()`.'
  4576. );
  4577. }
  4578. var data = [];
  4579. this.dataAdapter.current(function (currentData) {
  4580. data = currentData;
  4581. });
  4582. return data;
  4583. };
  4584. Select2.prototype.val = function (args) {
  4585. if (this.options.get('debug') && window.console && console.warn) {
  4586. console.warn(
  4587. 'Select2: The `select2("val")` method has been deprecated and will be' +
  4588. ' removed in later Select2 versions. Use $element.val() instead.'
  4589. );
  4590. }
  4591. if (args == null || args.length === 0) {
  4592. return this.$element.val();
  4593. }
  4594. var newVal = args[0];
  4595. if ($.isArray(newVal)) {
  4596. newVal = $.map(newVal, function (obj) {
  4597. return obj.toString();
  4598. });
  4599. }
  4600. this.$element.val(newVal).trigger('change');
  4601. };
  4602. Select2.prototype.destroy = function () {
  4603. this.$container.remove();
  4604. if (this.$element[0].detachEvent) {
  4605. this.$element[0].detachEvent('onpropertychange', this._syncA);
  4606. }
  4607. if (this._observer != null) {
  4608. this._observer.disconnect();
  4609. this._observer = null;
  4610. } else if (this.$element[0].removeEventListener) {
  4611. this.$element[0]
  4612. .removeEventListener('DOMAttrModified', this._syncA, false);
  4613. this.$element[0]
  4614. .removeEventListener('DOMNodeInserted', this._syncS, false);
  4615. this.$element[0]
  4616. .removeEventListener('DOMNodeRemoved', this._syncS, false);
  4617. }
  4618. this._syncA = null;
  4619. this._syncS = null;
  4620. this.$element.off('.select2');
  4621. this.$element.attr('tabindex',
  4622. Utils.GetData(this.$element[0], 'old-tabindex'));
  4623. this.$element.removeClass('select2-hidden-accessible');
  4624. this.$element.attr('aria-hidden', 'false');
  4625. Utils.RemoveData(this.$element[0]);
  4626. this.$element.removeData('select2');
  4627. this.dataAdapter.destroy();
  4628. this.selection.destroy();
  4629. this.dropdown.destroy();
  4630. this.results.destroy();
  4631. this.dataAdapter = null;
  4632. this.selection = null;
  4633. this.dropdown = null;
  4634. this.results = null;
  4635. };
  4636. Select2.prototype.render = function () {
  4637. var $container = $(
  4638. '<span class="select2 select2-container">' +
  4639. '<span class="selection"></span>' +
  4640. '<span class="dropdown-wrapper" aria-hidden="true"></span>' +
  4641. '</span>'
  4642. );
  4643. $container.attr('dir', this.options.get('dir'));
  4644. this.$container = $container;
  4645. this.$container.addClass('select2-container--' + this.options.get('theme'));
  4646. Utils.StoreData($container[0], 'element', this.$element);
  4647. return $container;
  4648. };
  4649. return Select2;
  4650. });
  4651. S2.define('select2/compat/utils', [
  4652. 'jquery'
  4653. ], function ($) {
  4654. function syncCssClasses($dest, $src, adapter) {
  4655. var classes, replacements = [], adapted;
  4656. classes = $.trim($dest.attr('class'));
  4657. if (classes) {
  4658. classes = '' + classes; // for IE which returns object
  4659. $(classes.split(/\s+/)).each(function () {
  4660. // Save all Select2 classes
  4661. if (this.indexOf('select2-') === 0) {
  4662. replacements.push(this);
  4663. }
  4664. });
  4665. }
  4666. classes = $.trim($src.attr('class'));
  4667. if (classes) {
  4668. classes = '' + classes; // for IE which returns object
  4669. $(classes.split(/\s+/)).each(function () {
  4670. // Only adapt non-Select2 classes
  4671. if (this.indexOf('select2-') !== 0) {
  4672. adapted = adapter(this);
  4673. if (adapted != null) {
  4674. replacements.push(adapted);
  4675. }
  4676. }
  4677. });
  4678. }
  4679. $dest.attr('class', replacements.join(' '));
  4680. }
  4681. return {
  4682. syncCssClasses: syncCssClasses
  4683. };
  4684. });
  4685. S2.define('select2/compat/containerCss', [
  4686. 'jquery',
  4687. './utils'
  4688. ], function ($, CompatUtils) {
  4689. // No-op CSS adapter that discards all classes by default
  4690. function _containerAdapter(clazz) {
  4691. return null;
  4692. }
  4693. function ContainerCSS() { }
  4694. ContainerCSS.prototype.render = function (decorated) {
  4695. var $container = decorated.call(this);
  4696. var containerCssClass = this.options.get('containerCssClass') || '';
  4697. if ($.isFunction(containerCssClass)) {
  4698. containerCssClass = containerCssClass(this.$element);
  4699. }
  4700. var containerCssAdapter = this.options.get('adaptContainerCssClass');
  4701. containerCssAdapter = containerCssAdapter || _containerAdapter;
  4702. if (containerCssClass.indexOf(':all:') !== -1) {
  4703. containerCssClass = containerCssClass.replace(':all:', '');
  4704. var _cssAdapter = containerCssAdapter;
  4705. containerCssAdapter = function (clazz) {
  4706. var adapted = _cssAdapter(clazz);
  4707. if (adapted != null) {
  4708. // Append the old one along with the adapted one
  4709. return adapted + ' ' + clazz;
  4710. }
  4711. return clazz;
  4712. };
  4713. }
  4714. var containerCss = this.options.get('containerCss') || {};
  4715. if ($.isFunction(containerCss)) {
  4716. containerCss = containerCss(this.$element);
  4717. }
  4718. CompatUtils.syncCssClasses($container, this.$element, containerCssAdapter);
  4719. $container.css(containerCss);
  4720. $container.addClass(containerCssClass);
  4721. return $container;
  4722. };
  4723. return ContainerCSS;
  4724. });
  4725. S2.define('select2/compat/dropdownCss', [
  4726. 'jquery',
  4727. './utils'
  4728. ], function ($, CompatUtils) {
  4729. // No-op CSS adapter that discards all classes by default
  4730. function _dropdownAdapter(clazz) {
  4731. return null;
  4732. }
  4733. function DropdownCSS() { }
  4734. DropdownCSS.prototype.render = function (decorated) {
  4735. var $dropdown = decorated.call(this);
  4736. var dropdownCssClass = this.options.get('dropdownCssClass') || '';
  4737. if ($.isFunction(dropdownCssClass)) {
  4738. dropdownCssClass = dropdownCssClass(this.$element);
  4739. }
  4740. var dropdownCssAdapter = this.options.get('adaptDropdownCssClass');
  4741. dropdownCssAdapter = dropdownCssAdapter || _dropdownAdapter;
  4742. if (dropdownCssClass.indexOf(':all:') !== -1) {
  4743. dropdownCssClass = dropdownCssClass.replace(':all:', '');
  4744. var _cssAdapter = dropdownCssAdapter;
  4745. dropdownCssAdapter = function (clazz) {
  4746. var adapted = _cssAdapter(clazz);
  4747. if (adapted != null) {
  4748. // Append the old one along with the adapted one
  4749. return adapted + ' ' + clazz;
  4750. }
  4751. return clazz;
  4752. };
  4753. }
  4754. var dropdownCss = this.options.get('dropdownCss') || {};
  4755. if ($.isFunction(dropdownCss)) {
  4756. dropdownCss = dropdownCss(this.$element);
  4757. }
  4758. CompatUtils.syncCssClasses($dropdown, this.$element, dropdownCssAdapter);
  4759. $dropdown.css(dropdownCss);
  4760. $dropdown.addClass(dropdownCssClass);
  4761. return $dropdown;
  4762. };
  4763. return DropdownCSS;
  4764. });
  4765. S2.define('select2/compat/initSelection', [
  4766. 'jquery'
  4767. ], function ($) {
  4768. function InitSelection(decorated, $element, options) {
  4769. if (options.get('debug') && window.console && console.warn) {
  4770. console.warn(
  4771. 'Select2: The `initSelection` option has been deprecated in favor' +
  4772. ' of a custom data adapter that overrides the `current` method. ' +
  4773. 'This method is now called multiple times instead of a single ' +
  4774. 'time when the instance is initialized. Support will be removed ' +
  4775. 'for the `initSelection` option in future versions of Select2'
  4776. );
  4777. }
  4778. this.initSelection = options.get('initSelection');
  4779. this._isInitialized = false;
  4780. decorated.call(this, $element, options);
  4781. }
  4782. InitSelection.prototype.current = function (decorated, callback) {
  4783. var self = this;
  4784. if (this._isInitialized) {
  4785. decorated.call(this, callback);
  4786. return;
  4787. }
  4788. this.initSelection.call(null, this.$element, function (data) {
  4789. self._isInitialized = true;
  4790. if (!$.isArray(data)) {
  4791. data = [data];
  4792. }
  4793. callback(data);
  4794. });
  4795. };
  4796. return InitSelection;
  4797. });
  4798. S2.define('select2/compat/inputData', [
  4799. 'jquery',
  4800. '../utils'
  4801. ], function ($, Utils) {
  4802. function InputData(decorated, $element, options) {
  4803. this._currentData = [];
  4804. this._valueSeparator = options.get('valueSeparator') || ',';
  4805. if ($element.prop('type') === 'hidden') {
  4806. if (options.get('debug') && console && console.warn) {
  4807. console.warn(
  4808. 'Select2: Using a hidden input with Select2 is no longer ' +
  4809. 'supported and may stop working in the future. It is recommended ' +
  4810. 'to use a `<select>` element instead.'
  4811. );
  4812. }
  4813. }
  4814. decorated.call(this, $element, options);
  4815. }
  4816. InputData.prototype.current = function (_, callback) {
  4817. function getSelected(data, selectedIds) {
  4818. var selected = [];
  4819. if (data.selected || $.inArray(data.id, selectedIds) !== -1) {
  4820. data.selected = true;
  4821. selected.push(data);
  4822. } else {
  4823. data.selected = false;
  4824. }
  4825. if (data.children) {
  4826. selected.push.apply(selected, getSelected(data.children, selectedIds));
  4827. }
  4828. return selected;
  4829. }
  4830. var selected = [];
  4831. for (var d = 0; d < this._currentData.length; d++) {
  4832. var data = this._currentData[d];
  4833. selected.push.apply(
  4834. selected,
  4835. getSelected(
  4836. data,
  4837. this.$element.val().split(
  4838. this._valueSeparator
  4839. )
  4840. )
  4841. );
  4842. }
  4843. callback(selected);
  4844. };
  4845. InputData.prototype.select = function (_, data) {
  4846. if (!this.options.get('multiple')) {
  4847. this.current(function (allData) {
  4848. $.map(allData, function (data) {
  4849. data.selected = false;
  4850. });
  4851. });
  4852. this.$element.val(data.id);
  4853. this.$element.trigger('change');
  4854. } else {
  4855. var value = this.$element.val();
  4856. value += this._valueSeparator + data.id;
  4857. this.$element.val(value);
  4858. this.$element.trigger('change');
  4859. }
  4860. };
  4861. InputData.prototype.unselect = function (_, data) {
  4862. var self = this;
  4863. data.selected = false;
  4864. this.current(function (allData) {
  4865. var values = [];
  4866. for (var d = 0; d < allData.length; d++) {
  4867. var item = allData[d];
  4868. if (data.id == item.id) {
  4869. continue;
  4870. }
  4871. values.push(item.id);
  4872. }
  4873. self.$element.val(values.join(self._valueSeparator));
  4874. self.$element.trigger('change');
  4875. });
  4876. };
  4877. InputData.prototype.query = function (_, params, callback) {
  4878. var results = [];
  4879. for (var d = 0; d < this._currentData.length; d++) {
  4880. var data = this._currentData[d];
  4881. var matches = this.matches(params, data);
  4882. if (matches !== null) {
  4883. results.push(matches);
  4884. }
  4885. }
  4886. callback({
  4887. results: results
  4888. });
  4889. };
  4890. InputData.prototype.addOptions = function (_, $options) {
  4891. var options = $.map($options, function ($option) {
  4892. return Utils.GetData($option[0], 'data');
  4893. });
  4894. this._currentData.push.apply(this._currentData, options);
  4895. };
  4896. return InputData;
  4897. });
  4898. S2.define('select2/compat/matcher', [
  4899. 'jquery'
  4900. ], function ($) {
  4901. function oldMatcher(matcher) {
  4902. function wrappedMatcher(params, data) {
  4903. var match = $.extend(true, {}, data);
  4904. if (params.term == null || $.trim(params.term) === '') {
  4905. return match;
  4906. }
  4907. if (data.children) {
  4908. for (var c = data.children.length - 1; c >= 0; c--) {
  4909. var child = data.children[c];
  4910. // Check if the child object matches
  4911. // The old matcher returned a boolean true or false
  4912. var doesMatch = matcher(params.term, child.text, child);
  4913. // If the child didn't match, pop it off
  4914. if (!doesMatch) {
  4915. match.children.splice(c, 1);
  4916. }
  4917. }
  4918. if (match.children.length > 0) {
  4919. return match;
  4920. }
  4921. }
  4922. if (matcher(params.term, data.text, data)) {
  4923. return match;
  4924. }
  4925. return null;
  4926. }
  4927. return wrappedMatcher;
  4928. }
  4929. return oldMatcher;
  4930. });
  4931. S2.define('select2/compat/query', [
  4932. ], function () {
  4933. function Query(decorated, $element, options) {
  4934. if (options.get('debug') && window.console && console.warn) {
  4935. console.warn(
  4936. 'Select2: The `query` option has been deprecated in favor of a ' +
  4937. 'custom data adapter that overrides the `query` method. Support ' +
  4938. 'will be removed for the `query` option in future versions of ' +
  4939. 'Select2.'
  4940. );
  4941. }
  4942. decorated.call(this, $element, options);
  4943. }
  4944. Query.prototype.query = function (_, params, callback) {
  4945. params.callback = callback;
  4946. var query = this.options.get('query');
  4947. query.call(null, params);
  4948. };
  4949. return Query;
  4950. });
  4951. S2.define('select2/dropdown/attachContainer', [
  4952. ], function () {
  4953. function AttachContainer(decorated, $element, options) {
  4954. decorated.call(this, $element, options);
  4955. }
  4956. AttachContainer.prototype.position =
  4957. function (decorated, $dropdown, $container) {
  4958. var $dropdownContainer = $container.find('.dropdown-wrapper');
  4959. $dropdownContainer.append($dropdown);
  4960. $dropdown.addClass('select2-dropdown--below');
  4961. $container.addClass('select2-container--below');
  4962. };
  4963. return AttachContainer;
  4964. });
  4965. S2.define('select2/dropdown/stopPropagation', [
  4966. ], function () {
  4967. function StopPropagation() { }
  4968. StopPropagation.prototype.bind = function (decorated, container, $container) {
  4969. decorated.call(this, container, $container);
  4970. var stoppedEvents = [
  4971. 'blur',
  4972. 'change',
  4973. 'click',
  4974. 'dblclick',
  4975. 'focus',
  4976. 'focusin',
  4977. 'focusout',
  4978. 'input',
  4979. 'keydown',
  4980. 'keyup',
  4981. 'keypress',
  4982. 'mousedown',
  4983. 'mouseenter',
  4984. 'mouseleave',
  4985. 'mousemove',
  4986. 'mouseover',
  4987. 'mouseup',
  4988. 'search',
  4989. 'touchend',
  4990. 'touchstart'
  4991. ];
  4992. this.$dropdown.on(stoppedEvents.join(' '), function (evt) {
  4993. evt.stopPropagation();
  4994. });
  4995. };
  4996. return StopPropagation;
  4997. });
  4998. S2.define('select2/selection/stopPropagation', [
  4999. ], function () {
  5000. function StopPropagation() { }
  5001. StopPropagation.prototype.bind = function (decorated, container, $container) {
  5002. decorated.call(this, container, $container);
  5003. var stoppedEvents = [
  5004. 'blur',
  5005. 'change',
  5006. 'click',
  5007. 'dblclick',
  5008. 'focus',
  5009. 'focusin',
  5010. 'focusout',
  5011. 'input',
  5012. 'keydown',
  5013. 'keyup',
  5014. 'keypress',
  5015. 'mousedown',
  5016. 'mouseenter',
  5017. 'mouseleave',
  5018. 'mousemove',
  5019. 'mouseover',
  5020. 'mouseup',
  5021. 'search',
  5022. 'touchend',
  5023. 'touchstart'
  5024. ];
  5025. this.$selection.on(stoppedEvents.join(' '), function (evt) {
  5026. evt.stopPropagation();
  5027. });
  5028. };
  5029. return StopPropagation;
  5030. });
  5031. /*!
  5032. * jQuery Mousewheel 3.1.13
  5033. *
  5034. * Copyright jQuery Foundation and other contributors
  5035. * Released under the MIT license
  5036. * http://jquery.org/license
  5037. */
  5038. (function (factory) {
  5039. if (typeof S2.define === 'function' && S2.define.amd) {
  5040. // AMD. Register as an anonymous module.
  5041. S2.define('jquery-mousewheel', ['jquery'], factory);
  5042. } else if (typeof exports === 'object') {
  5043. // Node/CommonJS style for Browserify
  5044. module.exports = factory;
  5045. } else {
  5046. // Browser globals
  5047. factory(jQuery);
  5048. }
  5049. }(function ($) {
  5050. var toFix = ['wheel', 'mousewheel', 'DOMMouseScroll', 'MozMousePixelScroll'],
  5051. toBind = ('onwheel' in document || document.documentMode >= 9) ?
  5052. ['wheel'] : ['mousewheel', 'DomMouseScroll', 'MozMousePixelScroll'],
  5053. slice = Array.prototype.slice,
  5054. nullLowestDeltaTimeout, lowestDelta;
  5055. if ($.event.fixHooks) {
  5056. for (var i = toFix.length; i;) {
  5057. $.event.fixHooks[toFix[--i]] = $.event.mouseHooks;
  5058. }
  5059. }
  5060. var special = $.event.special.mousewheel = {
  5061. version: '3.1.12',
  5062. setup: function () {
  5063. if (this.addEventListener) {
  5064. for (var i = toBind.length; i;) {
  5065. this.addEventListener(toBind[--i], handler, false);
  5066. }
  5067. } else {
  5068. this.onmousewheel = handler;
  5069. }
  5070. // Store the line height and page height for this particular element
  5071. $.data(this, 'mousewheel-line-height', special.getLineHeight(this));
  5072. $.data(this, 'mousewheel-page-height', special.getPageHeight(this));
  5073. },
  5074. teardown: function () {
  5075. if (this.removeEventListener) {
  5076. for (var i = toBind.length; i;) {
  5077. this.removeEventListener(toBind[--i], handler, false);
  5078. }
  5079. } else {
  5080. this.onmousewheel = null;
  5081. }
  5082. // Clean up the data we added to the element
  5083. $.removeData(this, 'mousewheel-line-height');
  5084. $.removeData(this, 'mousewheel-page-height');
  5085. },
  5086. getLineHeight: function (elem) {
  5087. var $elem = $(elem),
  5088. $parent = $elem['offsetParent' in $.fn ? 'offsetParent' : 'parent']();
  5089. if (!$parent.length) {
  5090. $parent = $('body');
  5091. }
  5092. return parseInt($parent.css('fontSize'), 10) || parseInt($elem.css('fontSize'), 10) || 16;
  5093. },
  5094. getPageHeight: function (elem) {
  5095. return $(elem).height();
  5096. },
  5097. settings: {
  5098. adjustOldDeltas: true, // see shouldAdjustOldDeltas() below
  5099. normalizeOffset: true // calls getBoundingClientRect for each event
  5100. }
  5101. };
  5102. $.fn.extend({
  5103. mousewheel: function (fn) {
  5104. return fn ? this.bind('mousewheel', fn) : this.trigger('mousewheel');
  5105. },
  5106. unmousewheel: function (fn) {
  5107. return this.unbind('mousewheel', fn);
  5108. }
  5109. });
  5110. function handler(event) {
  5111. var orgEvent = event || window.event,
  5112. args = slice.call(arguments, 1),
  5113. delta = 0,
  5114. deltaX = 0,
  5115. deltaY = 0,
  5116. absDelta = 0,
  5117. offsetX = 0,
  5118. offsetY = 0;
  5119. event = $.event.fix(orgEvent);
  5120. event.type = 'mousewheel';
  5121. // Old school scrollwheel delta
  5122. if ('detail' in orgEvent) { deltaY = orgEvent.detail * -1; }
  5123. if ('wheelDelta' in orgEvent) { deltaY = orgEvent.wheelDelta; }
  5124. if ('wheelDeltaY' in orgEvent) { deltaY = orgEvent.wheelDeltaY; }
  5125. if ('wheelDeltaX' in orgEvent) { deltaX = orgEvent.wheelDeltaX * -1; }
  5126. // Firefox < 17 horizontal scrolling related to DOMMouseScroll event
  5127. if ('axis' in orgEvent && orgEvent.axis === orgEvent.HORIZONTAL_AXIS) {
  5128. deltaX = deltaY * -1;
  5129. deltaY = 0;
  5130. }
  5131. // Set delta to be deltaY or deltaX if deltaY is 0 for backwards compatabilitiy
  5132. delta = deltaY === 0 ? deltaX : deltaY;
  5133. // New school wheel delta (wheel event)
  5134. if ('deltaY' in orgEvent) {
  5135. deltaY = orgEvent.deltaY * -1;
  5136. delta = deltaY;
  5137. }
  5138. if ('deltaX' in orgEvent) {
  5139. deltaX = orgEvent.deltaX;
  5140. if (deltaY === 0) { delta = deltaX * -1; }
  5141. }
  5142. // No change actually happened, no reason to go any further
  5143. if (deltaY === 0 && deltaX === 0) { return; }
  5144. // Need to convert lines and pages to pixels if we aren't already in pixels
  5145. // There are three delta modes:
  5146. // * deltaMode 0 is by pixels, nothing to do
  5147. // * deltaMode 1 is by lines
  5148. // * deltaMode 2 is by pages
  5149. if (orgEvent.deltaMode === 1) {
  5150. var lineHeight = $.data(this, 'mousewheel-line-height');
  5151. delta *= lineHeight;
  5152. deltaY *= lineHeight;
  5153. deltaX *= lineHeight;
  5154. } else if (orgEvent.deltaMode === 2) {
  5155. var pageHeight = $.data(this, 'mousewheel-page-height');
  5156. delta *= pageHeight;
  5157. deltaY *= pageHeight;
  5158. deltaX *= pageHeight;
  5159. }
  5160. // Store lowest absolute delta to normalize the delta values
  5161. absDelta = Math.max(Math.abs(deltaY), Math.abs(deltaX));
  5162. if (!lowestDelta || absDelta < lowestDelta) {
  5163. lowestDelta = absDelta;
  5164. // Adjust older deltas if necessary
  5165. if (shouldAdjustOldDeltas(orgEvent, absDelta)) {
  5166. lowestDelta /= 40;
  5167. }
  5168. }
  5169. // Adjust older deltas if necessary
  5170. if (shouldAdjustOldDeltas(orgEvent, absDelta)) {
  5171. // Divide all the things by 40!
  5172. delta /= 40;
  5173. deltaX /= 40;
  5174. deltaY /= 40;
  5175. }
  5176. // Get a whole, normalized value for the deltas
  5177. delta = Math[delta >= 1 ? 'floor' : 'ceil'](delta / lowestDelta);
  5178. deltaX = Math[deltaX >= 1 ? 'floor' : 'ceil'](deltaX / lowestDelta);
  5179. deltaY = Math[deltaY >= 1 ? 'floor' : 'ceil'](deltaY / lowestDelta);
  5180. // Normalise offsetX and offsetY properties
  5181. if (special.settings.normalizeOffset && this.getBoundingClientRect) {
  5182. var boundingRect = this.getBoundingClientRect();
  5183. offsetX = event.clientX - boundingRect.left;
  5184. offsetY = event.clientY - boundingRect.top;
  5185. }
  5186. // Add information to the event object
  5187. event.deltaX = deltaX;
  5188. event.deltaY = deltaY;
  5189. event.deltaFactor = lowestDelta;
  5190. event.offsetX = offsetX;
  5191. event.offsetY = offsetY;
  5192. // Go ahead and set deltaMode to 0 since we converted to pixels
  5193. // Although this is a little odd since we overwrite the deltaX/Y
  5194. // properties with normalized deltas.
  5195. event.deltaMode = 0;
  5196. // Add event and delta to the front of the arguments
  5197. args.unshift(event, delta, deltaX, deltaY);
  5198. // Clearout lowestDelta after sometime to better
  5199. // handle multiple device types that give different
  5200. // a different lowestDelta
  5201. // Ex: trackpad = 3 and mouse wheel = 120
  5202. if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
  5203. nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);
  5204. return ($.event.dispatch || $.event.handle).apply(this, args);
  5205. }
  5206. function nullLowestDelta() {
  5207. lowestDelta = null;
  5208. }
  5209. function shouldAdjustOldDeltas(orgEvent, absDelta) {
  5210. // If this is an older event and the delta is divisable by 120,
  5211. // then we are assuming that the browser is treating this as an
  5212. // older mouse wheel event and that we should divide the deltas
  5213. // by 40 to try and get a more usable deltaFactor.
  5214. // Side note, this actually impacts the reported scroll distance
  5215. // in older browsers and can cause scrolling to be slower than native.
  5216. // Turn this off by setting $.event.special.mousewheel.settings.adjustOldDeltas to false.
  5217. return special.settings.adjustOldDeltas && orgEvent.type === 'mousewheel' && absDelta % 120 === 0;
  5218. }
  5219. }));
  5220. S2.define('jquery.select2', [
  5221. 'jquery',
  5222. 'jquery-mousewheel',
  5223. './select2/core',
  5224. './select2/defaults',
  5225. './select2/utils'
  5226. ], function ($, _, Select2, Defaults, Utils) {
  5227. if ($.fn.select2 == null) {
  5228. // All methods that should return the element
  5229. var thisMethods = ['open', 'close', 'destroy'];
  5230. $.fn.select2 = function (options) {
  5231. options = options || {};
  5232. if (typeof options === 'object') {
  5233. this.each(function () {
  5234. var instanceOptions = $.extend(true, {}, options);
  5235. var instance = new Select2($(this), instanceOptions);
  5236. });
  5237. return this;
  5238. } else if (typeof options === 'string') {
  5239. var ret;
  5240. var args = Array.prototype.slice.call(arguments, 1);
  5241. this.each(function () {
  5242. var instance = Utils.GetData(this, 'select2');
  5243. if (instance == null && window.console && console.error) {
  5244. console.error(
  5245. 'The select2(\'' + options + '\') method was called on an ' +
  5246. 'element that is not using Select2.'
  5247. );
  5248. }
  5249. ret = instance[options].apply(instance, args);
  5250. });
  5251. // Check if we should be returning `this`
  5252. if ($.inArray(options, thisMethods) > -1) {
  5253. return this;
  5254. }
  5255. return ret;
  5256. } else {
  5257. throw new Error('Invalid arguments for Select2: ' + options);
  5258. }
  5259. };
  5260. }
  5261. if ($.fn.select2.defaults == null) {
  5262. $.fn.select2.defaults = Defaults;
  5263. }
  5264. return Select2;
  5265. });
  5266. // Return the AMD loader configuration so it can be used outside of this file
  5267. return {
  5268. define: S2.define,
  5269. require: S2.require
  5270. };
  5271. }());
  5272. // Autoload the jQuery bindings
  5273. // We know that all of the modules exist above this, so we're safe
  5274. var select2 = S2.require('jquery.select2');
  5275. // Hold the AMD module references on the jQuery function that was just loaded
  5276. // This allows Select2 to use the internal loader outside of this file, such
  5277. // as in the language files.
  5278. jQuery.fn.select2.amd = S2;
  5279. // Return the Select2 instance for anyone who is importing it.
  5280. return select2;
  5281. }));