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