| 1 |
/** |
| 2 |
* Selectize (v0.15.2) |
| 3 |
* https://selectize.dev |
| 4 |
* |
| 5 |
* Copyright (c) 2013-2015 Brian Reavis & contributors |
| 6 |
* Copyright (c) 2020-2026 Selectize Team & contributors |
| 7 |
* |
| 8 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this |
| 9 |
* file except in compliance with the License. You may obtain a copy of the License at: |
| 10 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
* |
| 12 |
* Unless required by applicable law or agreed to in writing, software distributed under |
| 13 |
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 14 |
* ANY KIND, either express or implied. See the License for the specific language |
| 15 |
* governing permissions and limitations under the License. |
| 16 |
* |
| 17 |
* @author Brian Reavis <brian@thirdroute.com> |
| 18 |
* @author Ris Adams <selectize@risadams.com> |
| 19 |
*/ |
| 20 |
(function (root, factory) { |
| 21 |
if (typeof define === 'function' && define.amd) { |
| 22 |
define(['jquery'], factory); |
| 23 |
} else if (typeof module === 'object' && typeof module.exports === 'object') { |
| 24 |
module.exports = factory(require('jquery')); |
| 25 |
} else { |
| 26 |
root.Selectize = factory(root.jQuery); |
| 27 |
} |
| 28 |
}(this, function ($) { |
| 29 |
'use strict'; |
| 30 |
var highlight = function ($element, pattern) { |
| 31 |
if (typeof pattern === 'string' && !pattern.length) return; |
| 32 |
var regex = (typeof pattern === 'string') ? new RegExp(pattern, 'i') : pattern; |
| 33 |
|
| 34 |
var highlight = function (node) { |
| 35 |
var skip = 0; |
| 36 |
if (node.nodeType === 3) { |
| 37 |
var pos = node.data.search(regex); |
| 38 |
if (pos >= 0 && node.data.length > 0) { |
| 39 |
var match = node.data.match(regex); |
| 40 |
var spannode = document.createElement('span'); |
| 41 |
spannode.className = 'highlight'; |
| 42 |
var middlebit = node.splitText(pos); |
| 43 |
var endbit = middlebit.splitText(match[0].length); |
| 44 |
var middleclone = middlebit.cloneNode(true); |
| 45 |
spannode.appendChild(middleclone); |
| 46 |
middlebit.parentNode.replaceChild(spannode, middlebit); |
| 47 |
skip = 1; |
| 48 |
} |
| 49 |
} |
| 50 |
else if (node.nodeType === 1 && node.childNodes && !/(script|style)/i.test(node.tagName) && (node.className !== 'highlight' || node.tagName !== 'SPAN')) { |
| 51 |
for (var i = 0; i < node.childNodes.length; ++i) { |
| 52 |
i += highlight(node.childNodes[i]); |
| 53 |
} |
| 54 |
} |
| 55 |
return skip; |
| 56 |
}; |
| 57 |
|
| 58 |
return $element.each(function () { |
| 59 |
highlight(this); |
| 60 |
}); |
| 61 |
}; |
| 62 |
|
| 63 |
$.fn.removeHighlight = function () { |
| 64 |
return this.find("span.highlight").each(function () { |
| 65 |
this.parentNode.firstChild.nodeName; |
| 66 |
var parent = this.parentNode; |
| 67 |
parent.replaceChild(this.firstChild, this); |
| 68 |
parent.normalize(); |
| 69 |
}).end(); |
| 70 |
}; |
| 71 |
|
| 72 |
var MicroEvent = function () { }; |
| 73 |
MicroEvent.prototype = { |
| 74 |
on: function (event, fct) { |
| 75 |
this._events = this._events || {}; |
| 76 |
this._events[event] = this._events[event] || []; |
| 77 |
this._events[event].push(fct); |
| 78 |
}, |
| 79 |
off: function (event, fct) { |
| 80 |
var n = arguments.length; |
| 81 |
if (n === 0) return delete this._events; |
| 82 |
if (n === 1) return delete this._events[event]; |
| 83 |
|
| 84 |
this._events = this._events || {}; |
| 85 |
if (event in this._events === false) return; |
| 86 |
this._events[event].splice(this._events[event].indexOf(fct), 1); |
| 87 |
}, |
| 88 |
trigger: function (event ) { |
| 89 |
const events = this._events = this._events || {}; |
| 90 |
if (event in events === false) return; |
| 91 |
for (var i = 0; i < events[event].length; i++) { |
| 92 |
events[event][i].apply(this, Array.prototype.slice.call(arguments, 1)); |
| 93 |
} |
| 94 |
} |
| 95 |
}; |
| 96 |
|
| 97 |
MicroEvent.mixin = function (destObject) { |
| 98 |
var props = ['on', 'off', 'trigger']; |
| 99 |
for (var i = 0; i < props.length; i++) { |
| 100 |
destObject.prototype[props[i]] = MicroEvent.prototype[props[i]]; |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
var MicroPlugin = {}; |
| 105 |
MicroPlugin.mixin = function (Interface) { |
| 106 |
|
| 107 |
Interface.plugins = {}; |
| 108 |
|
| 109 |
Interface.prototype.initializePlugins = function (plugins) { |
| 110 |
var i, n, key; |
| 111 |
var self = this; |
| 112 |
var queue = []; |
| 113 |
|
| 114 |
self.plugins = { |
| 115 |
names: [], |
| 116 |
settings: {}, |
| 117 |
requested: {}, |
| 118 |
loaded: {} |
| 119 |
}; |
| 120 |
|
| 121 |
if (isArray(plugins)) { |
| 122 |
for (i = 0, n = plugins.length; i < n; i++) { |
| 123 |
if (typeof plugins[i] === 'string') { |
| 124 |
queue.push(plugins[i]); |
| 125 |
} else { |
| 126 |
self.plugins.settings[plugins[i].name] = plugins[i].options; |
| 127 |
queue.push(plugins[i].name); |
| 128 |
} |
| 129 |
} |
| 130 |
} else if (plugins) { |
| 131 |
for (key in plugins) { |
| 132 |
if (plugins.hasOwnProperty(key)) { |
| 133 |
self.plugins.settings[key] = plugins[key]; |
| 134 |
queue.push(key); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
while (queue.length) { |
| 140 |
self.require(queue.shift()); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
|
| 145 |
Interface.prototype.loadPlugin = function (name) { |
| 146 |
var self = this; |
| 147 |
var plugins = self.plugins; |
| 148 |
var plugin = Interface.plugins[name]; |
| 149 |
|
| 150 |
if (!Interface.plugins.hasOwnProperty(name)) { |
| 151 |
throw new Error('Unable to find "' + name + '" plugin'); |
| 152 |
} |
| 153 |
|
| 154 |
plugins.requested[name] = true; |
| 155 |
plugins.loaded[name] = plugin.fn.apply(self, [self.plugins.settings[name] || {}]); |
| 156 |
plugins.names.push(name); |
| 157 |
}; |
| 158 |
|
| 159 |
Interface.prototype.require = function (name) { |
| 160 |
var self = this; |
| 161 |
var plugins = self.plugins; |
| 162 |
|
| 163 |
if (!self.plugins.loaded.hasOwnProperty(name)) { |
| 164 |
if (plugins.requested[name]) { |
| 165 |
throw new Error('Plugin has circular dependency ("' + name + '")'); |
| 166 |
} |
| 167 |
self.loadPlugin(name); |
| 168 |
} |
| 169 |
|
| 170 |
return plugins.loaded[name]; |
| 171 |
}; |
| 172 |
|
| 173 |
Interface.define = function (name, fn) { |
| 174 |
Interface.plugins[name] = { |
| 175 |
'name': name, |
| 176 |
'fn': fn |
| 177 |
}; |
| 178 |
}; |
| 179 |
}; |
| 180 |
|
| 181 |
const nanoid = (t = 21) => crypto.getRandomValues(new Uint8Array(t)) |
| 182 |
.reduce(((t, e) => |
| 183 |
t += (e &= 63) < 36 ? e.toString(36) : |
| 184 |
e < 62 ? (e - 26).toString(36).toUpperCase() |
| 185 |
: e > 62 ? "-" : "_"), ""); |
| 186 |
|
| 187 |
|
| 188 |
var Sifter = function (items, settings) { |
| 189 |
this.items = items; |
| 190 |
this.settings = settings || { diacritics: true }; |
| 191 |
}; |
| 192 |
|
| 193 |
Sifter.prototype.tokenize = function (query, respect_word_boundaries) { |
| 194 |
query = trim(String(query || '').toLowerCase()); |
| 195 |
if (!query || !query.length) return []; |
| 196 |
|
| 197 |
var i, n, regex, letter; |
| 198 |
var tokens = []; |
| 199 |
var words = query.split(/ +/); |
| 200 |
|
| 201 |
for (i = 0, n = words.length; i < n; i++) { |
| 202 |
regex = escape_regex(words[i]); |
| 203 |
if (this.settings.diacritics) { |
| 204 |
for (letter in DIACRITICS) { |
| 205 |
if (DIACRITICS.hasOwnProperty(letter)) { |
| 206 |
regex = regex.replace(new RegExp(letter, 'g'), DIACRITICS[letter]); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
if (respect_word_boundaries) regex = "\\b" + regex |
| 211 |
tokens.push({ |
| 212 |
string: words[i], |
| 213 |
regex: new RegExp(regex, 'i') |
| 214 |
}); |
| 215 |
} |
| 216 |
|
| 217 |
return tokens; |
| 218 |
}; |
| 219 |
|
| 220 |
Sifter.prototype.iterator = function (object, callback) { |
| 221 |
var iterator; |
| 222 |
if (is_array(object)) { |
| 223 |
iterator = Array.prototype.forEach || function (callback) { |
| 224 |
for (var i = 0, n = this.length; i < n; i++) { |
| 225 |
callback(this[i], i, this); |
| 226 |
} |
| 227 |
}; |
| 228 |
} else { |
| 229 |
iterator = function (callback) { |
| 230 |
for (var key in this) { |
| 231 |
if (this.hasOwnProperty(key)) { |
| 232 |
callback(this[key], key, this); |
| 233 |
} |
| 234 |
} |
| 235 |
}; |
| 236 |
} |
| 237 |
|
| 238 |
iterator.apply(object, [callback]); |
| 239 |
}; |
| 240 |
|
| 241 |
Sifter.prototype.getScoreFunction = function (search, options) { |
| 242 |
var self, fields, tokens, token_count, nesting; |
| 243 |
|
| 244 |
self = this; |
| 245 |
search = self.prepareSearch(search, options); |
| 246 |
tokens = search.tokens; |
| 247 |
fields = search.options.fields; |
| 248 |
token_count = tokens.length; |
| 249 |
nesting = search.options.nesting; |
| 250 |
|
| 251 |
var scoreValue = function (value, token) { |
| 252 |
var score, pos; |
| 253 |
|
| 254 |
if (!value) return 0; |
| 255 |
value = String(value || ''); |
| 256 |
pos = value.search(token.regex); |
| 257 |
if (pos === -1) return 0; |
| 258 |
score = token.string.length / value.length; |
| 259 |
if (pos === 0) score += 0.5; |
| 260 |
return score; |
| 261 |
}; |
| 262 |
|
| 263 |
var scoreObject = (function () { |
| 264 |
var field_count = fields.length; |
| 265 |
if (!field_count) { |
| 266 |
return function () { return 0; }; |
| 267 |
} |
| 268 |
if (field_count === 1) { |
| 269 |
return function (token, data) { |
| 270 |
return scoreValue(getattr(data, fields[0], nesting), token); |
| 271 |
}; |
| 272 |
} |
| 273 |
return function (token, data) { |
| 274 |
for (var i = 0, sum = 0; i < field_count; i++) { |
| 275 |
sum += scoreValue(getattr(data, fields[i], nesting), token); |
| 276 |
} |
| 277 |
return sum / field_count; |
| 278 |
}; |
| 279 |
})(); |
| 280 |
|
| 281 |
if (!token_count) { |
| 282 |
return function () { return 0; }; |
| 283 |
} |
| 284 |
if (token_count === 1) { |
| 285 |
return function (data) { |
| 286 |
return scoreObject(tokens[0], data); |
| 287 |
}; |
| 288 |
} |
| 289 |
|
| 290 |
if (search.options.conjunction === 'and') { |
| 291 |
return function (data) { |
| 292 |
var score; |
| 293 |
for (var i = 0, sum = 0; i < token_count; i++) { |
| 294 |
score = scoreObject(tokens[i], data); |
| 295 |
if (score <= 0) return 0; |
| 296 |
sum += score; |
| 297 |
} |
| 298 |
return sum / token_count; |
| 299 |
}; |
| 300 |
} else { |
| 301 |
return function (data) { |
| 302 |
for (var i = 0, sum = 0; i < token_count; i++) { |
| 303 |
sum += scoreObject(tokens[i], data); |
| 304 |
} |
| 305 |
return sum / token_count; |
| 306 |
}; |
| 307 |
} |
| 308 |
}; |
| 309 |
|
| 310 |
Sifter.prototype.getSortFunction = function (search, options) { |
| 311 |
var i, n, self, field, fields, fields_count, multiplier, multipliers, get_field, implicit_score, sort; |
| 312 |
|
| 313 |
self = this; |
| 314 |
search = self.prepareSearch(search, options); |
| 315 |
sort = (!search.query && options.sort_empty) || options.sort; |
| 316 |
|
| 317 |
get_field = function (name, result) { |
| 318 |
if (name === '$score') return result.score; |
| 319 |
return getattr(self.items[result.id], name, options.nesting); |
| 320 |
}; |
| 321 |
|
| 322 |
fields = []; |
| 323 |
if (sort) { |
| 324 |
for (i = 0, n = sort.length; i < n; i++) { |
| 325 |
if (search.query || sort[i].field !== '$score') { |
| 326 |
fields.push(sort[i]); |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (search.query) { |
| 332 |
implicit_score = true; |
| 333 |
for (i = 0, n = fields.length; i < n; i++) { |
| 334 |
if (fields[i].field === '$score') { |
| 335 |
implicit_score = false; |
| 336 |
break; |
| 337 |
} |
| 338 |
} |
| 339 |
if (implicit_score) { |
| 340 |
fields.unshift({ field: '$score', direction: 'desc' }); |
| 341 |
} |
| 342 |
} else { |
| 343 |
for (i = 0, n = fields.length; i < n; i++) { |
| 344 |
if (fields[i].field === '$score') { |
| 345 |
fields.splice(i, 1); |
| 346 |
break; |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
|
| 351 |
multipliers = []; |
| 352 |
for (i = 0, n = fields.length; i < n; i++) { |
| 353 |
multipliers.push(fields[i].direction === 'desc' ? -1 : 1); |
| 354 |
} |
| 355 |
|
| 356 |
fields_count = fields.length; |
| 357 |
if (!fields_count) { |
| 358 |
return null; |
| 359 |
} else if (fields_count === 1) { |
| 360 |
field = fields[0].field; |
| 361 |
multiplier = multipliers[0]; |
| 362 |
return function (a, b) { |
| 363 |
return multiplier * cmp( |
| 364 |
get_field(field, a), |
| 365 |
get_field(field, b) |
| 366 |
); |
| 367 |
}; |
| 368 |
} else { |
| 369 |
return function (a, b) { |
| 370 |
var i, result, a_value, b_value, field; |
| 371 |
for (i = 0; i < fields_count; i++) { |
| 372 |
field = fields[i].field; |
| 373 |
result = multipliers[i] * cmp( |
| 374 |
get_field(field, a), |
| 375 |
get_field(field, b) |
| 376 |
); |
| 377 |
if (result) return result; |
| 378 |
} |
| 379 |
return 0; |
| 380 |
}; |
| 381 |
} |
| 382 |
}; |
| 383 |
|
| 384 |
Sifter.prototype.prepareSearch = function (query, options) { |
| 385 |
if (typeof query === 'object') return query; |
| 386 |
|
| 387 |
options = extend({}, options); |
| 388 |
|
| 389 |
var option_fields = options.fields; |
| 390 |
var option_sort = options.sort; |
| 391 |
var option_sort_empty = options.sort_empty; |
| 392 |
|
| 393 |
if (option_fields && !is_array(option_fields)) options.fields = [option_fields]; |
| 394 |
if (option_sort && !is_array(option_sort)) options.sort = [option_sort]; |
| 395 |
if (option_sort_empty && !is_array(option_sort_empty)) options.sort_empty = [option_sort_empty]; |
| 396 |
|
| 397 |
return { |
| 398 |
options: options, |
| 399 |
query: String(query || '').toLowerCase(), |
| 400 |
tokens: this.tokenize(query, options.respect_word_boundaries), |
| 401 |
total: 0, |
| 402 |
items: [] |
| 403 |
}; |
| 404 |
}; |
| 405 |
|
| 406 |
Sifter.prototype.search = function (query, options) { |
| 407 |
var self = this, value, score, search, calculateScore; |
| 408 |
var fn_sort; |
| 409 |
var fn_score; |
| 410 |
|
| 411 |
search = this.prepareSearch(query, options); |
| 412 |
options = search.options; |
| 413 |
query = search.query; |
| 414 |
|
| 415 |
fn_score = options.score || self.getScoreFunction(search); |
| 416 |
|
| 417 |
if (query.length) { |
| 418 |
self.iterator(self.items, function (item, id) { |
| 419 |
score = fn_score(item); |
| 420 |
if (options.filter === false || score > 0) { |
| 421 |
search.items.push({ 'score': score, 'id': id }); |
| 422 |
} |
| 423 |
}); |
| 424 |
} else { |
| 425 |
self.iterator(self.items, function (item, id) { |
| 426 |
search.items.push({ 'score': 1, 'id': id }); |
| 427 |
}); |
| 428 |
} |
| 429 |
|
| 430 |
fn_sort = self.getSortFunction(search, options); |
| 431 |
if (fn_sort) search.items.sort(fn_sort); |
| 432 |
|
| 433 |
search.total = search.items.length; |
| 434 |
if (typeof options.limit === 'number') { |
| 435 |
search.items = search.items.slice(0, options.limit); |
| 436 |
} |
| 437 |
|
| 438 |
return search; |
| 439 |
}; |
| 440 |
|
| 441 |
var cmp = function (a, b) { |
| 442 |
if (typeof a === 'number' && typeof b === 'number') { |
| 443 |
return a > b ? 1 : (a < b ? -1 : 0); |
| 444 |
} |
| 445 |
a = asciifold(String(a || '')); |
| 446 |
b = asciifold(String(b || '')); |
| 447 |
if (a > b) return 1; |
| 448 |
if (b > a) return -1; |
| 449 |
return 0; |
| 450 |
}; |
| 451 |
|
| 452 |
var extend = function (a, b) { |
| 453 |
var i, n, k, object; |
| 454 |
for (i = 1, n = arguments.length; i < n; i++) { |
| 455 |
object = arguments[i]; |
| 456 |
if (!object) continue; |
| 457 |
for (k in object) { |
| 458 |
if (object.hasOwnProperty(k)) { |
| 459 |
a[k] = object[k]; |
| 460 |
} |
| 461 |
} |
| 462 |
} |
| 463 |
return a; |
| 464 |
}; |
| 465 |
|
| 466 |
var getattr = function (obj, name, nesting) { |
| 467 |
if (!obj || !name) return; |
| 468 |
if (!nesting) return obj[name]; |
| 469 |
var names = name.split("."); |
| 470 |
while (names.length && (obj = obj[names.shift()])); |
| 471 |
return obj; |
| 472 |
}; |
| 473 |
|
| 474 |
var trim = function (str) { |
| 475 |
return (str + '').replace(/^\s+|\s+$|/g, ''); |
| 476 |
}; |
| 477 |
|
| 478 |
var escape_regex = function (str) { |
| 479 |
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1'); |
| 480 |
}; |
| 481 |
|
| 482 |
var is_array = Array.isArray || function (object) { |
| 483 |
return Object.prototype.toString.call(object) === '[object Array]'; |
| 484 |
}; |
| 485 |
|
| 486 |
var DIACRITICS = { |
| 487 |
'a': '[aḀḁĂăÂâǍǎȺⱥȦȧẠạÄäÀàÁáĀāÃã� |
| 488 |
å� |
| 489 |
ĄÃ� |
| 490 |
Ą]', |
| 491 |
'b': '[b␢βΒB฿𐌁ᛒ]', |
| 492 |
'c': '[cĆćĈĉČčĊċC̄c̄ÇçḈḉȻȼƇƈɕᴄCc]', |
| 493 |
'd': '[dĎďḊḋḐḑḌḍḒḓḎḏĐđD̦d̦ƉɖƊɗƋƌᵭᶁᶑȡ� |
| 494 |
Ddð]', |
| 495 |
'e': '[eÉéÈèÊêḘḙĚěĔĕẼẽḚḛẺẻĖėËëĒēȨȩĘęᶒɆɇȄ� |
| 496 |
ẾếỀềỄ� |
| 497 |
ỂểḜḝḖḗḔḕȆȇẸẹỆệⱸᴇE� |
| 498 |
ɘǝƏƐε]', |
| 499 |
'f': '[fƑƒḞḟ]', |
| 500 |
'g': '[gɢ₲ǤǥĜĝĞğĢģƓɠĠġ]', |
| 501 |
'h': '[hĤĥĦħḨḩẖẖḤḥḢḣɦʰǶƕ]', |
| 502 |
'i': '[iÍíÌìĬĭÎîǏǐÏïḮḯĨĩĮįĪīỈỉȈȉȊȋỊịḬḭƗɨɨ̆ᵻᶖİiIıɪIi]', |
| 503 |
'j': '[jȷĴĵɈɉʝɟʲ]', |
| 504 |
'k': '[kƘƙꝀꝁḰḱǨǩḲḳḴḵκϰ₭]', |
| 505 |
'l': '[lŁłĽľĻļĹĺḶḷḸḹḼḽḺḻĿŀȽƚⱠⱡⱢɫɬ� |
| 506 |
ɭȴʟLl]', |
| 507 |
'n': '[nŃńǸǹŇňÑñṄ� |
| 508 |
� |
| 509 |
ņṆṇṊṋṈṉN̈n̈ƝɲȠƞᵰᶇɳȵɴNnŊŋ]', |
| 510 |
'o': '[oØøÖöÓóÒòÔôǑǒŐőŎŏȮȯỌọƟɵƠơỎỏŌōÕõǪǫȌȍՕ� |
| 511 |
]', |
| 512 |
'p': '[pṔṕṖṗⱣᵽƤƥᵱ]', |
| 513 |
'q': '[qꝖꝗʠɊɋꝘꝙq̃]', |
| 514 |
'r': '[rŔŕɌɍŘřŖŗṘṙȐȑȒȓṚṛⱤɽ]', |
| 515 |
's': '[sŚśṠṡṢṣꞨꞩŜŝŠšŞşȘșS̈s̈]', |
| 516 |
't': '[tŤťṪṫŢţṬṭƮʈȚțṰṱṮṯƬƭ]', |
| 517 |
'u': '[uŬŭɄʉỤụÜüÚúÙùÛûǓǔŰűŬŭƯưỦủŪūŨũŲųȔȕ∪]', |
| 518 |
'v': '[vṼṽṾṿƲʋꝞꝟⱱʋ]', |
| 519 |
'w': '[wẂẃẀẁŴŵẄ� |
| 520 |
ẆẇẈẉ]', |
| 521 |
'x': '[xẌẍẊẋχ]', |
| 522 |
'y': '[yÝýỲỳŶŷŸÿỸỹẎẏỴỵɎɏƳƴ]', |
| 523 |
'z': '[zŹźẐẑŽžŻżẒẓẔẕƵƶ]' |
| 524 |
}; |
| 525 |
|
| 526 |
var asciifold = (function () { |
| 527 |
var i, n, k, chunk; |
| 528 |
var i18nChars = ''; |
| 529 |
var lookup = {}; |
| 530 |
for (k in DIACRITICS) { |
| 531 |
if (DIACRITICS.hasOwnProperty(k)) { |
| 532 |
chunk = DIACRITICS[k].substring(2, DIACRITICS[k].length - 1); |
| 533 |
i18nChars += chunk; |
| 534 |
for (i = 0, n = chunk.length; i < n; i++) { |
| 535 |
lookup[chunk.charAt(i)] = k; |
| 536 |
} |
| 537 |
} |
| 538 |
} |
| 539 |
var regexp = new RegExp('[' + i18nChars + ']', 'g'); |
| 540 |
return function (str) { |
| 541 |
return str.replace(regexp, function (i18nChar) { |
| 542 |
return lookup[i18nChar]; |
| 543 |
}).toLowerCase(); |
| 544 |
}; |
| 545 |
})(); |
| 546 |
|
| 547 |
var IS_MAC = uaDetect("macOS", /Mac/); |
| 548 |
var KEY_A = 65; |
| 549 |
var KEY_COMMA = 188; |
| 550 |
var KEY_RETURN = 13; |
| 551 |
var KEY_ESC = 27; |
| 552 |
var KEY_LEFT = 37; |
| 553 |
var KEY_UP = 38; |
| 554 |
var KEY_P = 80; |
| 555 |
var KEY_RIGHT = 39; |
| 556 |
var KEY_DOWN = 40; |
| 557 |
var KEY_N = 78; |
| 558 |
var KEY_BACKSPACE = 8; |
| 559 |
var KEY_DELETE = 46; |
| 560 |
var KEY_SHIFT = 16; |
| 561 |
var KEY_CMD = IS_MAC ? 91 : 17; |
| 562 |
var KEY_CTRL = IS_MAC ? 18 : 17; |
| 563 |
var KEY_TAB = 9; |
| 564 |
var TAG_SELECT = 1; |
| 565 |
var TAG_INPUT = 2; |
| 566 |
|
| 567 |
var SUPPORTS_VALIDITY_API = !uaDetect("Android", /android/i) && !!document.createElement('input').validity; |
| 568 |
|
| 569 |
var isset = function (object) { |
| 570 |
return typeof object !== 'undefined'; |
| 571 |
}; |
| 572 |
|
| 573 |
var isArray = Array.isArray || function (vArg) { |
| 574 |
return Object.prototype.toString.call(vArg) === '[object Array]'; |
| 575 |
} |
| 576 |
|
| 577 |
var hash_key = function (value) { |
| 578 |
if (typeof value === 'undefined' || value === null) return null; |
| 579 |
if (typeof value === 'boolean') return value ? '1' : '0'; |
| 580 |
return value + ''; |
| 581 |
}; |
| 582 |
|
| 583 |
var escape_html = function (str) { |
| 584 |
return (str + '') |
| 585 |
.replace(/&/g, '&') |
| 586 |
.replace(/</g, '<') |
| 587 |
.replace(/>/g, '>') |
| 588 |
.replace(/"/g, '"'); |
| 589 |
}; |
| 590 |
|
| 591 |
var escape_replace = function (str) { |
| 592 |
return (str + '').replace(/\$/g, '$$$$'); |
| 593 |
}; |
| 594 |
|
| 595 |
var hook = {}; |
| 596 |
|
| 597 |
hook.before = function (self, method, fn) { |
| 598 |
var original = self[method]; |
| 599 |
self[method] = function () { |
| 600 |
fn.apply(self, arguments); |
| 601 |
return original.apply(self, arguments); |
| 602 |
}; |
| 603 |
}; |
| 604 |
|
| 605 |
hook.after = function (self, method, fn) { |
| 606 |
var original = self[method]; |
| 607 |
self[method] = function () { |
| 608 |
var result = original.apply(self, arguments); |
| 609 |
fn.apply(self, arguments); |
| 610 |
return result; |
| 611 |
}; |
| 612 |
}; |
| 613 |
|
| 614 |
var once = function (fn) { |
| 615 |
var called = false; |
| 616 |
return function () { |
| 617 |
if (called) return; |
| 618 |
called = true; |
| 619 |
fn.apply(this, arguments); |
| 620 |
}; |
| 621 |
}; |
| 622 |
|
| 623 |
var debounce = function (fn, delay) { |
| 624 |
var timeout; |
| 625 |
return function () { |
| 626 |
var self = this; |
| 627 |
var args = arguments; |
| 628 |
window.clearTimeout(timeout); |
| 629 |
timeout = window.setTimeout(function () { |
| 630 |
fn.apply(self, args); |
| 631 |
}, delay); |
| 632 |
}; |
| 633 |
}; |
| 634 |
|
| 635 |
var debounce_events = function (self, types, fn) { |
| 636 |
var type; |
| 637 |
var trigger = self.trigger; |
| 638 |
var event_args = {}; |
| 639 |
|
| 640 |
self.trigger = function () { |
| 641 |
var type = arguments[0]; |
| 642 |
if (types.indexOf(type) !== -1) { |
| 643 |
event_args[type] = arguments; |
| 644 |
} else { |
| 645 |
return trigger.apply(self, arguments); |
| 646 |
} |
| 647 |
}; |
| 648 |
|
| 649 |
fn.apply(self, []); |
| 650 |
self.trigger = trigger; |
| 651 |
|
| 652 |
for (type in event_args) { |
| 653 |
if (event_args.hasOwnProperty(type)) { |
| 654 |
trigger.apply(self, event_args[type]); |
| 655 |
} |
| 656 |
} |
| 657 |
}; |
| 658 |
|
| 659 |
var watchChildEvent = function ($parent, event, selector, fn) { |
| 660 |
$parent.on(event, selector, function (e) { |
| 661 |
var child = e.target; |
| 662 |
while (child && child.parentNode !== $parent[0]) { |
| 663 |
child = child.parentNode; |
| 664 |
} |
| 665 |
e.currentTarget = child; |
| 666 |
return fn.apply(this, [e]); |
| 667 |
}); |
| 668 |
}; |
| 669 |
|
| 670 |
var getInputSelection = function (input) { |
| 671 |
var result = {}; |
| 672 |
if (input === undefined) { |
| 673 |
console.warn('WARN getInputSelection cannot locate input control'); |
| 674 |
return result; |
| 675 |
} |
| 676 |
if ('selectionStart' in input) { |
| 677 |
result.start = input.selectionStart; |
| 678 |
result.length = input.selectionEnd - result.start; |
| 679 |
} else if (document.selection) { |
| 680 |
input.focus(); |
| 681 |
var sel = document.selection.createRange(); |
| 682 |
var selLen = document.selection.createRange().text.length; |
| 683 |
sel.moveStart('character', -input.value.length); |
| 684 |
result.start = sel.text.length - selLen; |
| 685 |
result.length = selLen; |
| 686 |
} |
| 687 |
return result; |
| 688 |
}; |
| 689 |
|
| 690 |
var transferStyles = function ($from, $to, properties) { |
| 691 |
var i, n, styles = {}; |
| 692 |
if (properties) { |
| 693 |
for (i = 0, n = properties.length; i < n; i++) { |
| 694 |
styles[properties[i]] = $from.css(properties[i]); |
| 695 |
} |
| 696 |
} else { |
| 697 |
styles = $from.css(); |
| 698 |
} |
| 699 |
$to.css(styles); |
| 700 |
}; |
| 701 |
|
| 702 |
var measureString = function (str, $parent) { |
| 703 |
if (!str) { |
| 704 |
return 0; |
| 705 |
} |
| 706 |
|
| 707 |
if (!Selectize.$testInput) { |
| 708 |
Selectize.$testInput = $('<span />').css({ |
| 709 |
position: 'absolute', |
| 710 |
width: 'auto', |
| 711 |
padding: 0, |
| 712 |
whiteSpace: 'pre' |
| 713 |
}); |
| 714 |
|
| 715 |
$('<div />').css({ |
| 716 |
position: 'absolute', |
| 717 |
width: 0, |
| 718 |
height: 0, |
| 719 |
overflow: 'hidden' |
| 720 |
}).attr({ |
| 721 |
'aria-hidden': true |
| 722 |
}).append(Selectize.$testInput).appendTo('body'); |
| 723 |
} |
| 724 |
|
| 725 |
Selectize.$testInput.text(str); |
| 726 |
|
| 727 |
transferStyles($parent, Selectize.$testInput, [ |
| 728 |
'letterSpacing', |
| 729 |
'fontSize', |
| 730 |
'fontFamily', |
| 731 |
'fontWeight', |
| 732 |
'textTransform' |
| 733 |
]); |
| 734 |
|
| 735 |
return Selectize.$testInput.width(); |
| 736 |
}; |
| 737 |
|
| 738 |
var autoGrow = function ($input) { |
| 739 |
var currentWidth = null; |
| 740 |
|
| 741 |
var update = function (e, options) { |
| 742 |
var value, keyCode, printable, width; |
| 743 |
var placeholder, placeholderWidth; |
| 744 |
var shift, character, selection; |
| 745 |
e = e || window.event || {}; |
| 746 |
options = options || {}; |
| 747 |
|
| 748 |
if (e.metaKey || e.altKey) return; |
| 749 |
if (!options.force && $input.data('grow') === false) return; |
| 750 |
|
| 751 |
value = $input.val(); |
| 752 |
if (e.type && e.type.toLowerCase() === 'keydown') { |
| 753 |
keyCode = e.keyCode; |
| 754 |
printable = ( |
| 755 |
(keyCode >= 48 && keyCode <= 57) || |
| 756 |
(keyCode >= 65 && keyCode <= 90) || |
| 757 |
(keyCode >= 96 && keyCode <= 111) || |
| 758 |
(keyCode >= 186 && keyCode <= 222) || |
| 759 |
keyCode === 32 |
| 760 |
); |
| 761 |
|
| 762 |
if (keyCode === KEY_DELETE || keyCode === KEY_BACKSPACE) { |
| 763 |
selection = getInputSelection($input[0]); |
| 764 |
if (selection.length) { |
| 765 |
value = value.substring(0, selection.start) + value.substring(selection.start + selection.length); |
| 766 |
} else if (keyCode === KEY_BACKSPACE && selection.start) { |
| 767 |
value = value.substring(0, selection.start - 1) + value.substring(selection.start + 1); |
| 768 |
} else if (keyCode === KEY_DELETE && typeof selection.start !== 'undefined') { |
| 769 |
value = value.substring(0, selection.start) + value.substring(selection.start + 1); |
| 770 |
} |
| 771 |
} else if (printable) { |
| 772 |
shift = e.shiftKey; |
| 773 |
character = String.fromCharCode(e.keyCode); |
| 774 |
if (shift) character = character.toUpperCase(); |
| 775 |
else character = character.toLowerCase(); |
| 776 |
value += character; |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
var width = $input.attr('readonly') ? 0 : 4; |
| 781 |
placeholder = $input.attr('placeholder'); |
| 782 |
if (placeholder) { |
| 783 |
placeholderWidth = measureString(placeholder, $input) + width; |
| 784 |
} else { |
| 785 |
placeholderWidth = 0; |
| 786 |
} |
| 787 |
|
| 788 |
width = Math.max(measureString(value, $input), placeholderWidth) + width; |
| 789 |
if (width !== currentWidth) { |
| 790 |
currentWidth = width; |
| 791 |
$input.width(width); |
| 792 |
$input.triggerHandler('resize'); |
| 793 |
} |
| 794 |
}; |
| 795 |
|
| 796 |
$input.on('keydown keyup update blur', update); |
| 797 |
update(); |
| 798 |
}; |
| 799 |
|
| 800 |
var domToString = function (d) { |
| 801 |
var tmp = document.createElement('div'); |
| 802 |
|
| 803 |
tmp.appendChild(d.cloneNode(true)); |
| 804 |
|
| 805 |
return tmp.innerHTML; |
| 806 |
}; |
| 807 |
|
| 808 |
var logError = function (message, options) { |
| 809 |
if (!options) options = {}; |
| 810 |
var component = "Selectize"; |
| 811 |
|
| 812 |
console.error(component + ": " + message) |
| 813 |
|
| 814 |
if (options.explanation) { |
| 815 |
if (console.group) console.group(); |
| 816 |
console.error(options.explanation); |
| 817 |
if (console.group) console.groupEnd(); |
| 818 |
} |
| 819 |
}; |
| 820 |
|
| 821 |
var isJSON = function (data) { |
| 822 |
try { |
| 823 |
JSON.parse(data); |
| 824 |
} catch (e) { |
| 825 |
return false; |
| 826 |
} |
| 827 |
return true; |
| 828 |
}; |
| 829 |
|
| 830 |
function uaDetect(platform, re) { |
| 831 |
if (navigator.userAgentData) { |
| 832 |
return platform === navigator.userAgentData.platform; |
| 833 |
} |
| 834 |
|
| 835 |
return re.test(navigator.userAgent); |
| 836 |
} |
| 837 |
|
| 838 |
var Selectize = function($input, settings) { |
| 839 |
var key, i, n, dir, input, self = this; |
| 840 |
input = $input[0]; |
| 841 |
input.selectize = self; |
| 842 |
|
| 843 |
var computedStyle = window.getComputedStyle && window.getComputedStyle(input, null); |
| 844 |
dir = computedStyle ? computedStyle.getPropertyValue('direction') : input.currentStyle && input.currentStyle.direction; |
| 845 |
dir = dir || $input.parents('[dir]:first').attr('dir') || ''; |
| 846 |
|
| 847 |
self.settings = {}; |
| 848 |
|
| 849 |
$.extend(self, { |
| 850 |
order : 0, |
| 851 |
settings : settings, |
| 852 |
$input : $input, |
| 853 |
tabIndex : $input.attr('tabindex') || '', |
| 854 |
tagType : input.tagName.toLowerCase() === 'select' ? TAG_SELECT : TAG_INPUT, |
| 855 |
rtl : /rtl/i.test(dir), |
| 856 |
|
| 857 |
eventNS : '.selectize' + (++Selectize.count), |
| 858 |
highlightedValue : null, |
| 859 |
isBlurring : false, |
| 860 |
isOpen : false, |
| 861 |
isDisabled : false, |
| 862 |
isRequired : $input.is('[required]'), |
| 863 |
isInvalid : false, |
| 864 |
isLocked : false, |
| 865 |
isFocused : false, |
| 866 |
isInputHidden : false, |
| 867 |
isSetup : false, |
| 868 |
isShiftDown : false, |
| 869 |
isCmdDown : false, |
| 870 |
isCtrlDown : false, |
| 871 |
ignoreFocus : false, |
| 872 |
ignoreBlur : false, |
| 873 |
ignoreHover : false, |
| 874 |
hasOptions : false, |
| 875 |
currentResults : null, |
| 876 |
lastValue : '', |
| 877 |
lastValidValue : '', |
| 878 |
lastOpenTarget : false, |
| 879 |
caretPos : 0, |
| 880 |
loading : 0, |
| 881 |
loadedSearches : {}, |
| 882 |
isDropdownClosing: false, |
| 883 |
|
| 884 |
$activeOption : null, |
| 885 |
$activeItems : [], |
| 886 |
|
| 887 |
optgroups : {}, |
| 888 |
options : {}, |
| 889 |
userOptions : {}, |
| 890 |
items : [], |
| 891 |
renderCache : {}, |
| 892 |
onSearchChange : settings.loadThrottle === null ? self.onSearchChange : debounce(self.onSearchChange, settings.loadThrottle) |
| 893 |
}); |
| 894 |
|
| 895 |
self.sifter = new Sifter(this.options, {diacritics: settings.diacritics}); |
| 896 |
|
| 897 |
if (self.settings.options) { |
| 898 |
for (i = 0, n = self.settings.options.length; i < n; i++) { |
| 899 |
self.registerOption(self.settings.options[i]); |
| 900 |
} |
| 901 |
delete self.settings.options; |
| 902 |
} |
| 903 |
|
| 904 |
if (self.settings.optgroups) { |
| 905 |
for (i = 0, n = self.settings.optgroups.length; i < n; i++) { |
| 906 |
self.registerOptionGroup(self.settings.optgroups[i]); |
| 907 |
} |
| 908 |
delete self.settings.optgroups; |
| 909 |
} |
| 910 |
|
| 911 |
self.settings.mode = self.settings.mode || (self.settings.maxItems === 1 ? 'single' : 'multi'); |
| 912 |
if (typeof self.settings.hideSelected !== 'boolean') { |
| 913 |
self.settings.hideSelected = self.settings.mode === 'multi'; |
| 914 |
} |
| 915 |
|
| 916 |
self.initializePlugins(self.settings.plugins); |
| 917 |
self.setupCallbacks(); |
| 918 |
self.setupTemplates(); |
| 919 |
self.setup(); |
| 920 |
}; |
| 921 |
|
| 922 |
MicroEvent.mixin(Selectize); |
| 923 |
MicroPlugin.mixin(Selectize); |
| 924 |
|
| 925 |
$.extend(Selectize.prototype, { |
| 926 |
|
| 927 |
setup: function() { |
| 928 |
var self = this; |
| 929 |
var settings = self.settings; |
| 930 |
var eventNS = self.eventNS; |
| 931 |
var $window = $(window); |
| 932 |
var $document = $(document); |
| 933 |
var $input = self.$input; |
| 934 |
|
| 935 |
var $wrapper; |
| 936 |
var $control; |
| 937 |
var $control_input; |
| 938 |
var $dropdown; |
| 939 |
var $dropdown_content; |
| 940 |
var $dropdown_parent; |
| 941 |
var inputMode; |
| 942 |
var timeout_blur; |
| 943 |
var timeout_focus; |
| 944 |
var classes; |
| 945 |
var classes_plugins; |
| 946 |
var inputId; |
| 947 |
var noArrowClass; |
| 948 |
|
| 949 |
inputMode = self.settings.mode; |
| 950 |
classes = $input.attr('class') || ''; |
| 951 |
noArrowClass = settings.showArrow ? '' : ' no-arrow'; |
| 952 |
|
| 953 |
$wrapper = $('<div>').addClass(settings.wrapperClass).addClass(classes + ' selectize-control').addClass(inputMode); |
| 954 |
$control = $('<div>').addClass(settings.inputClass + noArrowClass + ' selectize-input items').appendTo($wrapper); |
| 955 |
$control_input = $('<input type="text" autocomplete="new-password" autofill="no" />').appendTo($control).attr('tabindex', $input.is(':disabled') ? '-1' : self.tabIndex); |
| 956 |
$dropdown_parent = $(settings.dropdownParent || $wrapper); |
| 957 |
$dropdown = $('<div>').addClass(settings.dropdownClass).addClass(inputMode + ' selectize-dropdown').hide().appendTo($dropdown_parent); |
| 958 |
$dropdown_content = $('<div>').addClass(settings.dropdownContentClass + ' selectize-dropdown-content').attr('tabindex', '-1').appendTo($dropdown); |
| 959 |
|
| 960 |
if(inputId = $input.attr('id')) { |
| 961 |
$control_input.attr('id', inputId + '-selectized'); |
| 962 |
$("label[for='"+inputId+"']").attr('for', inputId + '-selectized'); |
| 963 |
} |
| 964 |
|
| 965 |
if(self.settings.copyClassesToDropdown) { |
| 966 |
$dropdown.addClass(classes); |
| 967 |
} |
| 968 |
|
| 969 |
$wrapper.css({ |
| 970 |
width: $input[0].style.width |
| 971 |
}); |
| 972 |
|
| 973 |
if (self.plugins.names.length) { |
| 974 |
classes_plugins = 'plugin-' + self.plugins.names.join(' plugin-'); |
| 975 |
$wrapper.addClass(classes_plugins); |
| 976 |
$dropdown.addClass(classes_plugins); |
| 977 |
} |
| 978 |
|
| 979 |
if ((settings.maxItems === null || settings.maxItems > 1) && self.tagType === TAG_SELECT) { |
| 980 |
$input.attr('multiple', 'multiple'); |
| 981 |
} |
| 982 |
|
| 983 |
if (self.settings.placeholder) { |
| 984 |
$control_input.attr('placeholder', settings.placeholder); |
| 985 |
} |
| 986 |
|
| 987 |
if (!self.settings.search) { |
| 988 |
$control_input.attr('readonly', true); |
| 989 |
$control_input.attr('inputmode', 'none'); |
| 990 |
$control.css('cursor', 'pointer'); |
| 991 |
} |
| 992 |
|
| 993 |
if (!self.settings.splitOn && self.settings.delimiter) { |
| 994 |
var delimiterEscaped = self.settings.delimiter.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); |
| 995 |
self.settings.splitOn = new RegExp('\\s*' + delimiterEscaped + '+\\s*'); |
| 996 |
} |
| 997 |
|
| 998 |
if ($input.attr('autocorrect')) { |
| 999 |
$control_input.attr('autocorrect', $input.attr('autocorrect')); |
| 1000 |
} |
| 1001 |
|
| 1002 |
if ($input.attr('autocapitalize')) { |
| 1003 |
$control_input.attr('autocapitalize', $input.attr('autocapitalize')); |
| 1004 |
} |
| 1005 |
if ($input.is('input')) { |
| 1006 |
$control_input[0].type = $input[0].type; |
| 1007 |
} |
| 1008 |
|
| 1009 |
self.$wrapper = $wrapper; |
| 1010 |
self.$control = $control; |
| 1011 |
self.$control_input = $control_input; |
| 1012 |
self.$dropdown = $dropdown; |
| 1013 |
self.$dropdown_content = $dropdown_content; |
| 1014 |
|
| 1015 |
$dropdown.on('mouseenter mousedown mouseup click', '[data-disabled]>[data-selectable]', function(e) { e.stopImmediatePropagation(); }); |
| 1016 |
$dropdown.on('mouseenter', '[data-selectable]', function() { return self.onOptionHover.apply(self, arguments); }); |
| 1017 |
$dropdown.on('mouseup click', '[data-selectable]', function() { return self.onOptionSelect.apply(self, arguments); }); |
| 1018 |
watchChildEvent($control, 'mouseup', '*:not(input)', function() { return self.onItemSelect.apply(self, arguments); }); |
| 1019 |
autoGrow($control_input); |
| 1020 |
|
| 1021 |
$control.on({ |
| 1022 |
mousedown : function() { return self.onMouseDown.apply(self, arguments); }, |
| 1023 |
click : function() { return self.onClick.apply(self, arguments); } |
| 1024 |
}); |
| 1025 |
|
| 1026 |
$control_input.on({ |
| 1027 |
mousedown : function(e) { |
| 1028 |
if (self.$control_input.val() !== '' || self.settings.openOnFocus) { |
| 1029 |
e.stopPropagation(); |
| 1030 |
} |
| 1031 |
}, |
| 1032 |
keydown : function() { return self.onKeyDown.apply(self, arguments); }, |
| 1033 |
keypress : function() { return self.onKeyPress.apply(self, arguments); }, |
| 1034 |
input : function() { return self.onInput.apply(self, arguments); }, |
| 1035 |
resize : function() { self.positionDropdown.apply(self, []); }, |
| 1036 |
blur : function() { return self.onBlur.apply(self, arguments); }, |
| 1037 |
focus : function() { return self.onFocus.apply(self, arguments); }, |
| 1038 |
paste : function() { return self.onPaste.apply(self, arguments); } |
| 1039 |
}); |
| 1040 |
|
| 1041 |
$document.on('keydown' + eventNS, function(e) { |
| 1042 |
self.isCmdDown = e[IS_MAC ? 'metaKey' : 'ctrlKey']; |
| 1043 |
self.isCtrlDown = e[IS_MAC ? 'altKey' : 'ctrlKey']; |
| 1044 |
self.isShiftDown = e.shiftKey; |
| 1045 |
}); |
| 1046 |
|
| 1047 |
$document.on('keyup' + eventNS, function(e) { |
| 1048 |
if (e.keyCode === KEY_CTRL) self.isCtrlDown = false; |
| 1049 |
if (e.keyCode === KEY_SHIFT) self.isShiftDown = false; |
| 1050 |
if (e.keyCode === KEY_CMD) self.isCmdDown = false; |
| 1051 |
}); |
| 1052 |
|
| 1053 |
$document.on('mousedown' + eventNS, function(e) { |
| 1054 |
if (self.isFocused) { |
| 1055 |
if ( |
| 1056 |
e.target === self.$dropdown[0] || |
| 1057 |
self.$dropdown.has(e.target).length) |
| 1058 |
{ |
| 1059 |
return false; |
| 1060 |
} |
| 1061 |
if (e.target !== self.$control[0]) { |
| 1062 |
self.blur(e.target); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
}); |
| 1066 |
|
| 1067 |
$window.on(['scroll' + eventNS, 'resize' + eventNS].join(' '), function() { |
| 1068 |
if (self.isOpen) { |
| 1069 |
self.positionDropdown.apply(self, arguments); |
| 1070 |
} |
| 1071 |
}); |
| 1072 |
$window.on('mousemove' + eventNS, function() { |
| 1073 |
self.ignoreHover = self.settings.ignoreHover; |
| 1074 |
}); |
| 1075 |
|
| 1076 |
var inputPlaceholder = $('<div></div>'); |
| 1077 |
var inputChildren = $input.children().detach(); |
| 1078 |
|
| 1079 |
$input.replaceWith(inputPlaceholder); |
| 1080 |
inputPlaceholder.replaceWith($input); |
| 1081 |
|
| 1082 |
this.revertSettings = { |
| 1083 |
$children : inputChildren, |
| 1084 |
tabindex : $input.attr('tabindex') |
| 1085 |
}; |
| 1086 |
|
| 1087 |
$input.attr('tabindex', -1).hide().after(self.$wrapper); |
| 1088 |
|
| 1089 |
if (Array.isArray(settings.items)) { |
| 1090 |
self.lastValidValue = settings.items; |
| 1091 |
self.setValue(settings.items); |
| 1092 |
delete settings.items; |
| 1093 |
} |
| 1094 |
|
| 1095 |
if (SUPPORTS_VALIDITY_API) { |
| 1096 |
$input.on('invalid' + eventNS, function(e) { |
| 1097 |
e.preventDefault(); |
| 1098 |
self.isInvalid = true; |
| 1099 |
self.refreshState(); |
| 1100 |
}); |
| 1101 |
} |
| 1102 |
|
| 1103 |
self.updateOriginalInput(); |
| 1104 |
self.refreshItems(); |
| 1105 |
self.refreshState(); |
| 1106 |
self.updatePlaceholder(); |
| 1107 |
self.isSetup = true; |
| 1108 |
|
| 1109 |
if ($input.is(':disabled')) { |
| 1110 |
self.disable(); |
| 1111 |
} |
| 1112 |
|
| 1113 |
self.on('change', this.onChange); |
| 1114 |
|
| 1115 |
$input.data('selectize', self); |
| 1116 |
$input.addClass('selectized'); |
| 1117 |
self.trigger('initialize'); |
| 1118 |
|
| 1119 |
if (settings.preload === true) { |
| 1120 |
self.onSearchChange(''); |
| 1121 |
} |
| 1122 |
|
| 1123 |
}, |
| 1124 |
|
| 1125 |
setupTemplates: function() { |
| 1126 |
var self = this; |
| 1127 |
var field_label = self.settings.labelField; |
| 1128 |
var field_value = self.settings.valueField; |
| 1129 |
var field_optgroup = self.settings.optgroupLabelField; |
| 1130 |
|
| 1131 |
var templates = { |
| 1132 |
'optgroup': function(data) { |
| 1133 |
return '<div class="optgroup">' + data.html + '</div>'; |
| 1134 |
}, |
| 1135 |
'optgroup_header': function(data, escape) { |
| 1136 |
return '<div class="optgroup-header">' + escape(data[field_optgroup]) + '</div>'; |
| 1137 |
}, |
| 1138 |
'option': function(data, escape) { |
| 1139 |
var classes = data.classes ? ' ' + data.classes : ''; |
| 1140 |
classes += data[field_value] === '' ? ' selectize-dropdown-emptyoptionlabel' : ''; |
| 1141 |
|
| 1142 |
var styles = data.styles ? ' style="' + data.styles + '"': ''; |
| 1143 |
return '<div' + styles + ' class="option' + classes + '">' + escape(data[field_label]) + '</div>'; |
| 1144 |
}, |
| 1145 |
'item': function(data, escape) { |
| 1146 |
return '<div class="item">' + escape(data[field_label]) + '</div>'; |
| 1147 |
}, |
| 1148 |
'option_create': function(data, escape) { |
| 1149 |
return '<div class="create">Add <strong>' + escape(data.input) + '</strong>…</div>'; |
| 1150 |
} |
| 1151 |
}; |
| 1152 |
|
| 1153 |
self.settings.render = $.extend({}, templates, self.settings.render); |
| 1154 |
}, |
| 1155 |
|
| 1156 |
setupCallbacks: function() { |
| 1157 |
var key, fn, callbacks = { |
| 1158 |
'initialize' : 'onInitialize', |
| 1159 |
'change' : 'onChange', |
| 1160 |
'item_add' : 'onItemAdd', |
| 1161 |
'item_remove' : 'onItemRemove', |
| 1162 |
'clear' : 'onClear', |
| 1163 |
'option_add' : 'onOptionAdd', |
| 1164 |
'option_remove' : 'onOptionRemove', |
| 1165 |
'option_clear' : 'onOptionClear', |
| 1166 |
'optgroup_add' : 'onOptionGroupAdd', |
| 1167 |
'optgroup_remove' : 'onOptionGroupRemove', |
| 1168 |
'optgroup_clear' : 'onOptionGroupClear', |
| 1169 |
'dropdown_open' : 'onDropdownOpen', |
| 1170 |
'dropdown_close' : 'onDropdownClose', |
| 1171 |
'type' : 'onType', |
| 1172 |
'load' : 'onLoad', |
| 1173 |
'focus' : 'onFocus', |
| 1174 |
'blur' : 'onBlur', |
| 1175 |
'dropdown_item_activate' : 'onDropdownItemActivate', |
| 1176 |
'dropdown_item_deactivate' : 'onDropdownItemDeactivate' |
| 1177 |
}; |
| 1178 |
|
| 1179 |
for (key in callbacks) { |
| 1180 |
if (callbacks.hasOwnProperty(key)) { |
| 1181 |
fn = this.settings[callbacks[key]]; |
| 1182 |
if (fn) this.on(key, fn); |
| 1183 |
} |
| 1184 |
} |
| 1185 |
}, |
| 1186 |
|
| 1187 |
onClick: function(e) { |
| 1188 |
var self = this; |
| 1189 |
|
| 1190 |
if (self.isDropdownClosing) { |
| 1191 |
return; |
| 1192 |
} |
| 1193 |
|
| 1194 |
if (!self.isFocused || !self.isOpen) { |
| 1195 |
self.focus(); |
| 1196 |
e.preventDefault(); |
| 1197 |
} |
| 1198 |
}, |
| 1199 |
|
| 1200 |
onMouseDown: function(e) { |
| 1201 |
var self = this; |
| 1202 |
var defaultPrevented = e.isDefaultPrevented(); |
| 1203 |
var $target = $(e.target); |
| 1204 |
|
| 1205 |
if (e.button && e.button === 2) { |
| 1206 |
return; |
| 1207 |
} |
| 1208 |
|
| 1209 |
if (!self.isFocused) { |
| 1210 |
if (!defaultPrevented) { |
| 1211 |
window.setTimeout(function () { |
| 1212 |
if (!self.isOpen) { |
| 1213 |
self.focus(); |
| 1214 |
} |
| 1215 |
}, 0); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
if ($target !== self.$control_input[0] || self.$control_input.val() === '') { |
| 1219 |
if (self.settings.mode === 'single') { |
| 1220 |
self.isOpen ? self.close() : self.open(); |
| 1221 |
} else { |
| 1222 |
if (!defaultPrevented) { |
| 1223 |
self.setActiveItem(null); |
| 1224 |
} |
| 1225 |
if (!self.settings.openOnFocus) { |
| 1226 |
if (self.isOpen && $target === self.lastOpenTarget) { |
| 1227 |
self.close(); |
| 1228 |
self.lastOpenTarget = false; |
| 1229 |
} else if (!self.isOpen) { |
| 1230 |
self.refreshOptions(); |
| 1231 |
self.open(); |
| 1232 |
self.lastOpenTarget = $target; |
| 1233 |
} else { |
| 1234 |
self.lastOpenTarget = $target; |
| 1235 |
} |
| 1236 |
} |
| 1237 |
} |
| 1238 |
return false; |
| 1239 |
} |
| 1240 |
}, |
| 1241 |
|
| 1242 |
onChange: function() { |
| 1243 |
var self = this; |
| 1244 |
if (self.getValue() !== "") { |
| 1245 |
self.lastValidValue = self.getValue(); |
| 1246 |
} |
| 1247 |
this.$input.trigger('input'); |
| 1248 |
this.$input.trigger('change'); |
| 1249 |
}, |
| 1250 |
|
| 1251 |
onPaste: function(e) { |
| 1252 |
var self = this; |
| 1253 |
|
| 1254 |
if (self.isFull() || self.isInputHidden || self.isLocked) { |
| 1255 |
e.preventDefault(); |
| 1256 |
return; |
| 1257 |
} |
| 1258 |
|
| 1259 |
if (self.settings.splitOn) { |
| 1260 |
|
| 1261 |
setTimeout(function() { |
| 1262 |
var pastedText = self.$control_input.val(); |
| 1263 |
if(!pastedText.match(self.settings.splitOn)){ return } |
| 1264 |
|
| 1265 |
var splitInput = pastedText |
| 1266 |
.trim() |
| 1267 |
.split(self.settings.splitOn); |
| 1268 |
for (var i = 0, n = splitInput.length; i < n; i++) { |
| 1269 |
self.createItem(splitInput[i]); |
| 1270 |
} |
| 1271 |
}, 0); |
| 1272 |
} |
| 1273 |
}, |
| 1274 |
|
| 1275 |
onKeyPress: function(e) { |
| 1276 |
if (this.isLocked) return e && e.preventDefault(); |
| 1277 |
var character = String.fromCharCode(e.keyCode || e.which); |
| 1278 |
if (this.settings.create && this.settings.mode === 'multi' && character === this.settings.delimiter) { |
| 1279 |
this.createItem(); |
| 1280 |
e.preventDefault(); |
| 1281 |
return false; |
| 1282 |
} |
| 1283 |
}, |
| 1284 |
|
| 1285 |
onKeyDown: function(e) { |
| 1286 |
var isInput = e.target === this.$control_input[0]; |
| 1287 |
var self = this; |
| 1288 |
|
| 1289 |
if (self.isLocked) { |
| 1290 |
if (e.keyCode !== KEY_TAB) { |
| 1291 |
e.preventDefault(); |
| 1292 |
} |
| 1293 |
return; |
| 1294 |
} |
| 1295 |
|
| 1296 |
switch (e.keyCode) { |
| 1297 |
case KEY_A: |
| 1298 |
if (self.isCmdDown) { |
| 1299 |
self.selectAll(); |
| 1300 |
return; |
| 1301 |
} |
| 1302 |
break; |
| 1303 |
case KEY_ESC: |
| 1304 |
if (self.isOpen) { |
| 1305 |
e.preventDefault(); |
| 1306 |
e.stopPropagation(); |
| 1307 |
self.close(); |
| 1308 |
} |
| 1309 |
return; |
| 1310 |
case KEY_N: |
| 1311 |
if (!e.ctrlKey || e.altKey) break; |
| 1312 |
case KEY_DOWN: |
| 1313 |
if (!self.isOpen && self.hasOptions) { |
| 1314 |
self.open(); |
| 1315 |
} else if (self.$activeOption) { |
| 1316 |
self.ignoreHover = true; |
| 1317 |
var $next = self.getAdjacentOption(self.$activeOption, 1); |
| 1318 |
if ($next.length) self.setActiveOption($next, true, true); |
| 1319 |
} |
| 1320 |
e.preventDefault(); |
| 1321 |
return; |
| 1322 |
case KEY_P: |
| 1323 |
if (!e.ctrlKey || e.altKey) break; |
| 1324 |
case KEY_UP: |
| 1325 |
if (self.$activeOption) { |
| 1326 |
self.ignoreHover = true; |
| 1327 |
var $prev = self.getAdjacentOption(self.$activeOption, -1); |
| 1328 |
if ($prev.length) self.setActiveOption($prev, true, true); |
| 1329 |
} |
| 1330 |
e.preventDefault(); |
| 1331 |
return; |
| 1332 |
case KEY_RETURN: |
| 1333 |
if (self.isOpen && self.$activeOption) { |
| 1334 |
self.onOptionSelect({currentTarget: self.$activeOption}); |
| 1335 |
e.preventDefault(); |
| 1336 |
} |
| 1337 |
return; |
| 1338 |
case KEY_LEFT: |
| 1339 |
self.advanceSelection(-1, e); |
| 1340 |
return; |
| 1341 |
case KEY_RIGHT: |
| 1342 |
self.advanceSelection(1, e); |
| 1343 |
return; |
| 1344 |
case KEY_TAB: |
| 1345 |
if (self.settings.selectOnTab && self.isOpen && self.$activeOption) { |
| 1346 |
self.onOptionSelect({currentTarget: self.$activeOption}); |
| 1347 |
|
| 1348 |
if (!self.isFull()) { |
| 1349 |
e.preventDefault(); |
| 1350 |
} |
| 1351 |
} |
| 1352 |
if (self.settings.create && self.createItem() && self.settings.showAddOptionOnCreate) { |
| 1353 |
e.preventDefault(); |
| 1354 |
} |
| 1355 |
return; |
| 1356 |
case KEY_BACKSPACE: |
| 1357 |
case KEY_DELETE: |
| 1358 |
self.deleteSelection(e); |
| 1359 |
return; |
| 1360 |
} |
| 1361 |
|
| 1362 |
if ((self.isFull() || self.isInputHidden) && !(IS_MAC ? e.metaKey : e.ctrlKey)) { |
| 1363 |
e.preventDefault(); |
| 1364 |
return; |
| 1365 |
} |
| 1366 |
}, |
| 1367 |
|
| 1368 |
onInput: function(e) { |
| 1369 |
var self = this; |
| 1370 |
|
| 1371 |
var value = self.$control_input.val() || ''; |
| 1372 |
if (self.lastValue !== value) { |
| 1373 |
self.lastValue = value; |
| 1374 |
self.onSearchChange(value); |
| 1375 |
self.refreshOptions(); |
| 1376 |
self.trigger('type', value); |
| 1377 |
} |
| 1378 |
}, |
| 1379 |
|
| 1380 |
onSearchChange: function(value) { |
| 1381 |
var self = this; |
| 1382 |
var fn = self.settings.load; |
| 1383 |
if (!fn) return; |
| 1384 |
if (self.loadedSearches.hasOwnProperty(value)) return; |
| 1385 |
self.loadedSearches[value] = true; |
| 1386 |
self.load(function(callback) { |
| 1387 |
fn.apply(self, [value, callback]); |
| 1388 |
}); |
| 1389 |
}, |
| 1390 |
|
| 1391 |
onFocus: function(e) { |
| 1392 |
var self = this; |
| 1393 |
var wasFocused = self.isFocused; |
| 1394 |
|
| 1395 |
if (self.isDisabled) { |
| 1396 |
self.blur(); |
| 1397 |
e && e.preventDefault(); |
| 1398 |
return false; |
| 1399 |
} |
| 1400 |
|
| 1401 |
if (self.ignoreFocus) return; |
| 1402 |
self.isFocused = true; |
| 1403 |
if (self.settings.preload === 'focus') self.onSearchChange(''); |
| 1404 |
|
| 1405 |
if (!wasFocused) self.trigger('focus'); |
| 1406 |
|
| 1407 |
if (!self.$activeItems.length) { |
| 1408 |
self.showInput(); |
| 1409 |
self.setActiveItem(null); |
| 1410 |
self.refreshOptions(!!self.settings.openOnFocus); |
| 1411 |
} |
| 1412 |
|
| 1413 |
self.refreshState(); |
| 1414 |
}, |
| 1415 |
|
| 1416 |
onBlur: function(e, dest) { |
| 1417 |
var self = this; |
| 1418 |
|
| 1419 |
if (!self.isFocused) return; |
| 1420 |
self.isFocused = false; |
| 1421 |
|
| 1422 |
if (self.ignoreFocus) { |
| 1423 |
return; |
| 1424 |
} |
| 1425 |
|
| 1426 |
var deactivate = function() { |
| 1427 |
self.close(); |
| 1428 |
self.setTextboxValue(''); |
| 1429 |
self.setActiveItem(null); |
| 1430 |
self.setActiveOption(null); |
| 1431 |
self.setCaret(self.items.length); |
| 1432 |
self.refreshState(); |
| 1433 |
|
| 1434 |
dest && dest.focus && dest.focus(); |
| 1435 |
|
| 1436 |
self.isBlurring = false; |
| 1437 |
self.ignoreFocus = false; |
| 1438 |
self.trigger('blur'); |
| 1439 |
}; |
| 1440 |
|
| 1441 |
self.isBlurring = true; |
| 1442 |
self.ignoreFocus = true; |
| 1443 |
if (self.settings.create && self.settings.createOnBlur) { |
| 1444 |
self.createItem(null, false, deactivate); |
| 1445 |
} else { |
| 1446 |
deactivate(); |
| 1447 |
} |
| 1448 |
}, |
| 1449 |
|
| 1450 |
onOptionHover: function(e) { |
| 1451 |
if (this.ignoreHover) return; |
| 1452 |
this.setActiveOption(e.currentTarget, false); |
| 1453 |
}, |
| 1454 |
|
| 1455 |
onOptionSelect: function(e) { |
| 1456 |
var value, $target, $option, self = this; |
| 1457 |
|
| 1458 |
if (e.preventDefault) { |
| 1459 |
e.preventDefault(); |
| 1460 |
e.stopPropagation(); |
| 1461 |
} |
| 1462 |
|
| 1463 |
if (e.button && e.button === 2) { |
| 1464 |
return; |
| 1465 |
} |
| 1466 |
|
| 1467 |
$target = $(e.currentTarget); |
| 1468 |
if ($target.hasClass('create')) { |
| 1469 |
self.createItem(null, function() { |
| 1470 |
if (self.settings.closeAfterSelect) { |
| 1471 |
self.close(); |
| 1472 |
} |
| 1473 |
}); |
| 1474 |
} else { |
| 1475 |
value = $target.attr('data-value'); |
| 1476 |
if (typeof value !== 'undefined') { |
| 1477 |
self.lastQuery = null; |
| 1478 |
self.setTextboxValue(''); |
| 1479 |
self.addItem(value); |
| 1480 |
if (self.settings.closeAfterSelect) { |
| 1481 |
self.close(); |
| 1482 |
} else if (!self.settings.hideSelected && e.type && /mouse/.test(e.type)) { |
| 1483 |
self.setActiveOption(self.getOption(value)); |
| 1484 |
} |
| 1485 |
} |
| 1486 |
} |
| 1487 |
}, |
| 1488 |
|
| 1489 |
onItemSelect: function(e) { |
| 1490 |
var self = this; |
| 1491 |
|
| 1492 |
if (self.isLocked) return; |
| 1493 |
if (self.settings.mode === 'multi') { |
| 1494 |
e.preventDefault(); |
| 1495 |
self.setActiveItem(e.currentTarget, e); |
| 1496 |
} |
| 1497 |
}, |
| 1498 |
|
| 1499 |
load: function(fn) { |
| 1500 |
var self = this; |
| 1501 |
var $wrapper = self.$wrapper.addClass(self.settings.loadingClass); |
| 1502 |
|
| 1503 |
self.loading++; |
| 1504 |
fn.apply(self, [function(results) { |
| 1505 |
self.loading = Math.max(self.loading - 1, 0); |
| 1506 |
if (results && results.length) { |
| 1507 |
self.addOption(results); |
| 1508 |
self.refreshOptions(self.isFocused && !self.isInputHidden); |
| 1509 |
} |
| 1510 |
if (!self.loading) { |
| 1511 |
$wrapper.removeClass(self.settings.loadingClass); |
| 1512 |
} |
| 1513 |
self.trigger('load', results); |
| 1514 |
}]); |
| 1515 |
}, |
| 1516 |
|
| 1517 |
getTextboxValue: function() { |
| 1518 |
var $input = this.$control_input; |
| 1519 |
return $input.val(); |
| 1520 |
}, |
| 1521 |
|
| 1522 |
setTextboxValue: function(value) { |
| 1523 |
var $input = this.$control_input; |
| 1524 |
var changed = $input.val() !== value; |
| 1525 |
if (changed) { |
| 1526 |
$input.val(value).triggerHandler('update'); |
| 1527 |
this.lastValue = value; |
| 1528 |
} |
| 1529 |
}, |
| 1530 |
|
| 1531 |
getValue: function() { |
| 1532 |
if (this.tagType === TAG_SELECT && this.$input.attr('multiple')) { |
| 1533 |
return this.items; |
| 1534 |
} else { |
| 1535 |
return this.items.join(this.settings.delimiter); |
| 1536 |
} |
| 1537 |
}, |
| 1538 |
|
| 1539 |
setValue: function(value, silent) { |
| 1540 |
const items = Array.isArray(value) ? value : [value]; |
| 1541 |
if (items.join('') === this.items.join('')) { |
| 1542 |
return; |
| 1543 |
} |
| 1544 |
|
| 1545 |
var events = silent ? [] : ['change']; |
| 1546 |
|
| 1547 |
debounce_events(this, events, function() { |
| 1548 |
this.clear(silent); |
| 1549 |
this.addItems(value, silent); |
| 1550 |
}); |
| 1551 |
}, |
| 1552 |
|
| 1553 |
setMaxItems: function(value){ |
| 1554 |
if(value === 0) value = null; |
| 1555 |
this.settings.maxItems = value; |
| 1556 |
this.settings.mode = this.settings.mode || (this.settings.maxItems === 1 ? 'single' : 'multi'); |
| 1557 |
this.refreshState(); |
| 1558 |
}, |
| 1559 |
|
| 1560 |
setActiveItem: function($item, e) { |
| 1561 |
var self = this; |
| 1562 |
var eventName; |
| 1563 |
var i, idx, begin, end, item, swap; |
| 1564 |
var $last; |
| 1565 |
|
| 1566 |
if (self.settings.mode === 'single') return; |
| 1567 |
$item = $($item); |
| 1568 |
|
| 1569 |
if (!$item.length) { |
| 1570 |
$(self.$activeItems).removeClass('active'); |
| 1571 |
self.$activeItems = []; |
| 1572 |
if (self.isFocused) { |
| 1573 |
self.showInput(); |
| 1574 |
} |
| 1575 |
return; |
| 1576 |
} |
| 1577 |
|
| 1578 |
eventName = e && e.type.toLowerCase(); |
| 1579 |
|
| 1580 |
if (eventName === 'mousedown' && self.isShiftDown && self.$activeItems.length) { |
| 1581 |
$last = self.$control.children('.active:last'); |
| 1582 |
begin = Array.prototype.indexOf.apply(self.$control[0].childNodes, [$last[0]]); |
| 1583 |
end = Array.prototype.indexOf.apply(self.$control[0].childNodes, [$item[0]]); |
| 1584 |
if (begin > end) { |
| 1585 |
swap = begin; |
| 1586 |
begin = end; |
| 1587 |
end = swap; |
| 1588 |
} |
| 1589 |
for (i = begin; i <= end; i++) { |
| 1590 |
item = self.$control[0].childNodes[i]; |
| 1591 |
if (self.$activeItems.indexOf(item) === -1) { |
| 1592 |
$(item).addClass('active'); |
| 1593 |
self.$activeItems.push(item); |
| 1594 |
} |
| 1595 |
} |
| 1596 |
e.preventDefault(); |
| 1597 |
} else if ((eventName === 'mousedown' && self.isCtrlDown) || (eventName === 'keydown' && this.isShiftDown)) { |
| 1598 |
if ($item.hasClass('active')) { |
| 1599 |
idx = self.$activeItems.indexOf($item[0]); |
| 1600 |
self.$activeItems.splice(idx, 1); |
| 1601 |
$item.removeClass('active'); |
| 1602 |
} else { |
| 1603 |
self.$activeItems.push($item.addClass('active')[0]); |
| 1604 |
} |
| 1605 |
} else { |
| 1606 |
$(self.$activeItems).removeClass('active'); |
| 1607 |
self.$activeItems = [$item.addClass('active')[0]]; |
| 1608 |
} |
| 1609 |
|
| 1610 |
self.hideInput(); |
| 1611 |
if (!this.isFocused) { |
| 1612 |
self.focus(); |
| 1613 |
} |
| 1614 |
}, |
| 1615 |
|
| 1616 |
setActiveOption: function($option, scroll, animate) { |
| 1617 |
var height_menu, height_item, y; |
| 1618 |
var scroll_top, scroll_bottom; |
| 1619 |
var self = this; |
| 1620 |
|
| 1621 |
if (self.$activeOption) { |
| 1622 |
self.$activeOption.removeClass('active'); |
| 1623 |
self.trigger('dropdown_item_deactivate', self.$activeOption.attr('data-value')); |
| 1624 |
} |
| 1625 |
self.$activeOption = null; |
| 1626 |
|
| 1627 |
$option = $($option); |
| 1628 |
if (!$option.length) return; |
| 1629 |
|
| 1630 |
self.$activeOption = $option.addClass('active'); |
| 1631 |
if (self.isOpen) self.trigger('dropdown_item_activate', self.$activeOption.attr('data-value')); |
| 1632 |
|
| 1633 |
if (scroll || !isset(scroll)) { |
| 1634 |
|
| 1635 |
height_menu = self.$dropdown_content.height(); |
| 1636 |
height_item = self.$activeOption.outerHeight(true); |
| 1637 |
scroll = self.$dropdown_content.scrollTop() || 0; |
| 1638 |
y = self.$activeOption.offset().top - self.$dropdown_content.offset().top + scroll; |
| 1639 |
scroll_top = y; |
| 1640 |
scroll_bottom = y - height_menu + height_item; |
| 1641 |
|
| 1642 |
if (y + height_item > height_menu + scroll) { |
| 1643 |
self.$dropdown_content.stop().animate({scrollTop: scroll_bottom}, animate ? self.settings.scrollDuration : 0); |
| 1644 |
} else if (y < scroll) { |
| 1645 |
self.$dropdown_content.stop().animate({scrollTop: scroll_top}, animate ? self.settings.scrollDuration : 0); |
| 1646 |
} |
| 1647 |
|
| 1648 |
} |
| 1649 |
}, |
| 1650 |
|
| 1651 |
selectAll: function() { |
| 1652 |
var self = this; |
| 1653 |
if (self.settings.mode === 'single') return; |
| 1654 |
|
| 1655 |
self.$activeItems = Array.prototype.slice.apply(self.$control.children(':not(input)').addClass('active')); |
| 1656 |
if (self.$activeItems.length) { |
| 1657 |
self.hideInput(); |
| 1658 |
self.close(); |
| 1659 |
} |
| 1660 |
self.focus(); |
| 1661 |
}, |
| 1662 |
|
| 1663 |
hideInput: function() { |
| 1664 |
var self = this; |
| 1665 |
|
| 1666 |
self.setTextboxValue(''); |
| 1667 |
self.$control_input.css({opacity: 0, position: 'absolute', left: self.rtl ? 10000 : 0}); |
| 1668 |
self.isInputHidden = true; |
| 1669 |
}, |
| 1670 |
|
| 1671 |
showInput: function() { |
| 1672 |
this.$control_input.css({opacity: 1, position: 'relative', left: 0}); |
| 1673 |
this.isInputHidden = false; |
| 1674 |
}, |
| 1675 |
|
| 1676 |
focus: function() { |
| 1677 |
var self = this; |
| 1678 |
if (self.isDisabled) return self; |
| 1679 |
|
| 1680 |
self.ignoreFocus = true; |
| 1681 |
self.$control_input[0].focus(); |
| 1682 |
window.setTimeout(function() { |
| 1683 |
self.ignoreFocus = false; |
| 1684 |
self.onFocus(); |
| 1685 |
}, 0); |
| 1686 |
return self; |
| 1687 |
}, |
| 1688 |
|
| 1689 |
blur: function(dest) { |
| 1690 |
this.$control_input[0].blur(); |
| 1691 |
this.onBlur(null, dest); |
| 1692 |
return this; |
| 1693 |
}, |
| 1694 |
|
| 1695 |
getScoreFunction: function(query) { |
| 1696 |
return this.sifter.getScoreFunction(query, this.getSearchOptions()); |
| 1697 |
}, |
| 1698 |
|
| 1699 |
getSearchOptions: function() { |
| 1700 |
var settings = this.settings; |
| 1701 |
var sort = settings.sortField; |
| 1702 |
if (typeof sort === 'string') { |
| 1703 |
sort = [{field: sort}]; |
| 1704 |
} |
| 1705 |
|
| 1706 |
return { |
| 1707 |
fields : settings.searchField, |
| 1708 |
conjunction : settings.searchConjunction, |
| 1709 |
sort : sort, |
| 1710 |
nesting : settings.nesting, |
| 1711 |
filter : settings.filter, |
| 1712 |
respect_word_boundaries : settings.respect_word_boundaries |
| 1713 |
}; |
| 1714 |
}, |
| 1715 |
|
| 1716 |
search: function(query) { |
| 1717 |
var i, value, score, result, calculateScore; |
| 1718 |
var self = this; |
| 1719 |
var settings = self.settings; |
| 1720 |
var options = this.getSearchOptions(); |
| 1721 |
|
| 1722 |
if (settings.score) { |
| 1723 |
calculateScore = self.settings.score.apply(this, [query]); |
| 1724 |
if (typeof calculateScore !== 'function') { |
| 1725 |
throw new Error('Selectize "score" setting must be a function that returns a function'); |
| 1726 |
} |
| 1727 |
} |
| 1728 |
|
| 1729 |
if (query !== self.lastQuery) { |
| 1730 |
if (settings.normalize) query = query.normalize("NFD").replace(/[\u0300-\u036f]/g, ""); |
| 1731 |
self.lastQuery = query; |
| 1732 |
result = self.sifter.search(query, $.extend(options, {score: calculateScore})); |
| 1733 |
self.currentResults = result; |
| 1734 |
} else { |
| 1735 |
result = $.extend(true, {}, self.currentResults); |
| 1736 |
} |
| 1737 |
|
| 1738 |
if (settings.hideSelected) { |
| 1739 |
for (i = result.items.length - 1; i >= 0; i--) { |
| 1740 |
if (self.items.indexOf(hash_key(result.items[i].id)) !== -1) { |
| 1741 |
result.items.splice(i, 1); |
| 1742 |
} |
| 1743 |
} |
| 1744 |
} |
| 1745 |
|
| 1746 |
return result; |
| 1747 |
}, |
| 1748 |
|
| 1749 |
refreshOptions: function(triggerDropdown) { |
| 1750 |
var i, j, k, n, groups, groups_order, option, option_html, optgroup, optgroups, html, html_children, has_create_option; |
| 1751 |
var $active, $active_before, $create; |
| 1752 |
|
| 1753 |
if (typeof triggerDropdown === 'undefined') { |
| 1754 |
triggerDropdown = true; |
| 1755 |
} |
| 1756 |
|
| 1757 |
var self = this; |
| 1758 |
var query = (self.$control_input.val()).trim(); |
| 1759 |
var results = self.search(query); |
| 1760 |
var $dropdown_content = self.$dropdown_content; |
| 1761 |
var active_before = self.$activeOption && hash_key(self.$activeOption.attr('data-value')); |
| 1762 |
|
| 1763 |
n = results.items.length; |
| 1764 |
if (typeof self.settings.maxOptions === 'number') { |
| 1765 |
n = Math.min(n, self.settings.maxOptions); |
| 1766 |
} |
| 1767 |
|
| 1768 |
groups = {}; |
| 1769 |
groups_order = []; |
| 1770 |
|
| 1771 |
for (i = 0; i < n; i++) { |
| 1772 |
option = self.options[results.items[i].id]; |
| 1773 |
option_html = self.render('option', option); |
| 1774 |
optgroup = option[self.settings.optgroupField] || ''; |
| 1775 |
optgroups = Array.isArray(optgroup) ? optgroup : [optgroup]; |
| 1776 |
|
| 1777 |
for (j = 0, k = optgroups && optgroups.length; j < k; j++) { |
| 1778 |
optgroup = optgroups[j]; |
| 1779 |
if (!self.optgroups.hasOwnProperty(optgroup) && typeof self.settings.optionGroupRegister === 'function') { |
| 1780 |
var regGroup; |
| 1781 |
if (regGroup = self.settings.optionGroupRegister.apply(self, [optgroup])) { |
| 1782 |
self.registerOptionGroup(regGroup); |
| 1783 |
} |
| 1784 |
} |
| 1785 |
if (!self.optgroups.hasOwnProperty(optgroup)) { |
| 1786 |
optgroup = ''; |
| 1787 |
} |
| 1788 |
if (!groups.hasOwnProperty(optgroup)) { |
| 1789 |
groups[optgroup] = document.createDocumentFragment(); |
| 1790 |
groups_order.push(optgroup); |
| 1791 |
} |
| 1792 |
groups[optgroup].appendChild(option_html); |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
if (this.settings.lockOptgroupOrder) { |
| 1797 |
groups_order.sort(function(a, b) { |
| 1798 |
var a_order = self.optgroups[a] && self.optgroups[a].$order || 0; |
| 1799 |
var b_order = self.optgroups[b] && self.optgroups[b].$order || 0; |
| 1800 |
return a_order - b_order; |
| 1801 |
}); |
| 1802 |
} |
| 1803 |
|
| 1804 |
html = document.createDocumentFragment(); |
| 1805 |
for (i = 0, n = groups_order.length; i < n; i++) { |
| 1806 |
optgroup = groups_order[i]; |
| 1807 |
if (self.optgroups.hasOwnProperty(optgroup) && groups[optgroup].childNodes.length) { |
| 1808 |
html_children = document.createDocumentFragment(); |
| 1809 |
html_children.appendChild(self.render('optgroup_header', self.optgroups[optgroup])); |
| 1810 |
html_children.appendChild(groups[optgroup]); |
| 1811 |
|
| 1812 |
html.appendChild(self.render('optgroup', $.extend({}, self.optgroups[optgroup], { |
| 1813 |
html: domToString(html_children), |
| 1814 |
dom: html_children |
| 1815 |
}))); |
| 1816 |
} else { |
| 1817 |
html.appendChild(groups[optgroup]); |
| 1818 |
} |
| 1819 |
} |
| 1820 |
|
| 1821 |
$dropdown_content.html(html); |
| 1822 |
|
| 1823 |
if (self.settings.highlight) { |
| 1824 |
$dropdown_content.removeHighlight(); |
| 1825 |
if (results.query.length && results.tokens.length) { |
| 1826 |
for (i = 0, n = results.tokens.length; i < n; i++) { |
| 1827 |
highlight($dropdown_content, results.tokens[i].regex); |
| 1828 |
} |
| 1829 |
} |
| 1830 |
} |
| 1831 |
|
| 1832 |
if (!self.settings.hideSelected) { |
| 1833 |
self.$dropdown.find('.selected').removeClass('selected'); |
| 1834 |
|
| 1835 |
for (i = 0, n = self.items.length; i < n; i++) { |
| 1836 |
self.getOption(self.items[i]).addClass('selected'); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
if (self.settings.dropdownSize.sizeType !== 'auto' && self.isOpen) { |
| 1841 |
self.setupDropdownHeight(); |
| 1842 |
} |
| 1843 |
|
| 1844 |
self.positionDropdown(); |
| 1845 |
|
| 1846 |
has_create_option = self.canCreate(query); |
| 1847 |
if (has_create_option) { |
| 1848 |
if(self.settings.showAddOptionOnCreate) { |
| 1849 |
$dropdown_content.prepend(self.render('option_create', {input: query})); |
| 1850 |
$create = $($dropdown_content[0].childNodes[0]); |
| 1851 |
} |
| 1852 |
} |
| 1853 |
|
| 1854 |
self.hasOptions = results.items.length > 0 || ( has_create_option && self.settings.showAddOptionOnCreate ) || self.settings.setFirstOptionActive; |
| 1855 |
|
| 1856 |
if (self.hasOptions) { |
| 1857 |
if (results.items.length > 0) { |
| 1858 |
$active_before = active_before && self.getOption(active_before); |
| 1859 |
if (results.query !== "" && self.settings.setFirstOptionActive) { |
| 1860 |
$active = $dropdown_content.find('[data-selectable]:first') |
| 1861 |
} else if (results.query !== "" && $active_before && $active_before.length) { |
| 1862 |
$active = $active_before; |
| 1863 |
} else if (self.settings.mode === 'single' && self.items.length) { |
| 1864 |
$active = self.getOption(self.items[0]); |
| 1865 |
} |
| 1866 |
if (!$active || !$active.length) { |
| 1867 |
if ($create && !self.settings.addPrecedence) { |
| 1868 |
$active = self.getAdjacentOption($create, 1); |
| 1869 |
} else { |
| 1870 |
$active = $dropdown_content.find('[data-selectable]:first'); |
| 1871 |
} |
| 1872 |
} |
| 1873 |
} else { |
| 1874 |
$active = $create; |
| 1875 |
} |
| 1876 |
self.setActiveOption($active); |
| 1877 |
if (triggerDropdown && !self.isOpen) { self.open(); } |
| 1878 |
} else { |
| 1879 |
self.setActiveOption(null); |
| 1880 |
if (triggerDropdown && self.isOpen) { self.close(); } |
| 1881 |
} |
| 1882 |
}, |
| 1883 |
|
| 1884 |
addOption: function(data) { |
| 1885 |
var i, n, value, self = this; |
| 1886 |
|
| 1887 |
if (Array.isArray(data)) { |
| 1888 |
for (i = 0, n = data.length; i < n; i++) { |
| 1889 |
self.addOption(data[i]); |
| 1890 |
} |
| 1891 |
return; |
| 1892 |
} |
| 1893 |
|
| 1894 |
if (value = self.registerOption(data)) { |
| 1895 |
self.userOptions[value] = true; |
| 1896 |
self.lastQuery = null; |
| 1897 |
self.trigger('option_add', value, data); |
| 1898 |
} |
| 1899 |
}, |
| 1900 |
|
| 1901 |
registerOption: function(data) { |
| 1902 |
var key = hash_key(data[this.settings.valueField]); |
| 1903 |
if (typeof key === 'undefined' || key === null || this.options.hasOwnProperty(key)) return false; |
| 1904 |
data.$order = data.$order || ++this.order; |
| 1905 |
this.options[key] = data; |
| 1906 |
return key; |
| 1907 |
}, |
| 1908 |
|
| 1909 |
registerOptionGroup: function(data) { |
| 1910 |
var key = hash_key(data[this.settings.optgroupValueField]); |
| 1911 |
if (!key) return false; |
| 1912 |
|
| 1913 |
data.$order = data.$order || ++this.order; |
| 1914 |
this.optgroups[key] = data; |
| 1915 |
return key; |
| 1916 |
}, |
| 1917 |
|
| 1918 |
addOptionGroup: function(id, data) { |
| 1919 |
data[this.settings.optgroupValueField] = id; |
| 1920 |
if (id = this.registerOptionGroup(data)) { |
| 1921 |
this.trigger('optgroup_add', id, data); |
| 1922 |
} |
| 1923 |
}, |
| 1924 |
|
| 1925 |
removeOptionGroup: function(id) { |
| 1926 |
if (this.optgroups.hasOwnProperty(id)) { |
| 1927 |
delete this.optgroups[id]; |
| 1928 |
this.renderCache = {}; |
| 1929 |
this.trigger('optgroup_remove', id); |
| 1930 |
} |
| 1931 |
}, |
| 1932 |
|
| 1933 |
clearOptionGroups: function() { |
| 1934 |
this.optgroups = {}; |
| 1935 |
this.renderCache = {}; |
| 1936 |
this.trigger('optgroup_clear'); |
| 1937 |
}, |
| 1938 |
|
| 1939 |
updateOption: function(value, data) { |
| 1940 |
var self = this; |
| 1941 |
var $item, $item_new; |
| 1942 |
var value_new, index_item, cache_items, cache_options, order_old; |
| 1943 |
|
| 1944 |
value = hash_key(value); |
| 1945 |
value_new = hash_key(data[self.settings.valueField]); |
| 1946 |
|
| 1947 |
if (value === null) return; |
| 1948 |
if (!self.options.hasOwnProperty(value)) return; |
| 1949 |
if (typeof value_new !== 'string') throw new Error('Value must be set in option data'); |
| 1950 |
|
| 1951 |
order_old = self.options[value].$order; |
| 1952 |
|
| 1953 |
if (value_new !== value) { |
| 1954 |
delete self.options[value]; |
| 1955 |
index_item = self.items.indexOf(value); |
| 1956 |
if (index_item !== -1) { |
| 1957 |
self.items.splice(index_item, 1, value_new); |
| 1958 |
} |
| 1959 |
} |
| 1960 |
data.$order = data.$order || order_old; |
| 1961 |
self.options[value_new] = data; |
| 1962 |
|
| 1963 |
cache_items = self.renderCache['item']; |
| 1964 |
cache_options = self.renderCache['option']; |
| 1965 |
|
| 1966 |
if (cache_items) { |
| 1967 |
delete cache_items[value]; |
| 1968 |
delete cache_items[value_new]; |
| 1969 |
} |
| 1970 |
if (cache_options) { |
| 1971 |
delete cache_options[value]; |
| 1972 |
delete cache_options[value_new]; |
| 1973 |
} |
| 1974 |
|
| 1975 |
if (self.items.indexOf(value_new) !== -1) { |
| 1976 |
$item = self.getItem(value); |
| 1977 |
$item_new = $(self.render('item', data)); |
| 1978 |
if ($item.hasClass('active')) $item_new.addClass('active'); |
| 1979 |
$item.replaceWith($item_new); |
| 1980 |
} |
| 1981 |
|
| 1982 |
self.lastQuery = null; |
| 1983 |
|
| 1984 |
if (self.isOpen) { |
| 1985 |
self.refreshOptions(false); |
| 1986 |
} |
| 1987 |
}, |
| 1988 |
|
| 1989 |
removeOption: function(value, silent) { |
| 1990 |
var self = this; |
| 1991 |
value = hash_key(value); |
| 1992 |
|
| 1993 |
var cache_items = self.renderCache['item']; |
| 1994 |
var cache_options = self.renderCache['option']; |
| 1995 |
if (cache_items) delete cache_items[value]; |
| 1996 |
if (cache_options) delete cache_options[value]; |
| 1997 |
|
| 1998 |
delete self.userOptions[value]; |
| 1999 |
delete self.options[value]; |
| 2000 |
self.lastQuery = null; |
| 2001 |
self.trigger('option_remove', value); |
| 2002 |
self.removeItem(value, silent); |
| 2003 |
}, |
| 2004 |
|
| 2005 |
clearOptions: function(silent) { |
| 2006 |
var self = this; |
| 2007 |
|
| 2008 |
self.loadedSearches = {}; |
| 2009 |
self.userOptions = {}; |
| 2010 |
self.renderCache = {}; |
| 2011 |
var options = self.options; |
| 2012 |
$.each(self.options, function(key, value) { |
| 2013 |
if(self.items.indexOf(key) == -1) { |
| 2014 |
delete options[key]; |
| 2015 |
} |
| 2016 |
}); |
| 2017 |
self.options = self.sifter.items = options; |
| 2018 |
self.lastQuery = null; |
| 2019 |
self.trigger('option_clear'); |
| 2020 |
self.clear(silent); |
| 2021 |
}, |
| 2022 |
|
| 2023 |
getOption: function(value) { |
| 2024 |
return this.getElementWithValue(value, this.$dropdown_content.find('[data-selectable]')); |
| 2025 |
}, |
| 2026 |
|
| 2027 |
getFirstOption: function() { |
| 2028 |
var $options = this.$dropdown.find('[data-selectable]'); |
| 2029 |
return $options.length > 0 ? $options.eq(0) : $(); |
| 2030 |
}, |
| 2031 |
|
| 2032 |
getAdjacentOption: function($option, direction) { |
| 2033 |
var $options = this.$dropdown.find('[data-selectable]'); |
| 2034 |
var index = $options.index($option) + direction; |
| 2035 |
|
| 2036 |
return index >= 0 && index < $options.length ? $options.eq(index) : $(); |
| 2037 |
}, |
| 2038 |
|
| 2039 |
getElementWithValue: function(value, $els) { |
| 2040 |
value = hash_key(value); |
| 2041 |
|
| 2042 |
if (typeof value !== 'undefined' && value !== null) { |
| 2043 |
for (var i = 0, n = $els.length; i < n; i++) { |
| 2044 |
if ($els[i].getAttribute('data-value') === value) { |
| 2045 |
return $($els[i]); |
| 2046 |
} |
| 2047 |
} |
| 2048 |
} |
| 2049 |
|
| 2050 |
return $(); |
| 2051 |
}, |
| 2052 |
|
| 2053 |
getElementWithTextContent: function(textContent, ignoreCase ,$els) { |
| 2054 |
textContent = hash_key(textContent); |
| 2055 |
|
| 2056 |
if (typeof textContent !== 'undefined' && textContent !== null) { |
| 2057 |
for (var i = 0, n = $els.length; i < n; i++) { |
| 2058 |
var eleTextContent = $els[i].textContent |
| 2059 |
if (ignoreCase == true) { |
| 2060 |
eleTextContent = (eleTextContent !== null) ? eleTextContent.toLowerCase() : null; |
| 2061 |
textContent = textContent.toLowerCase(); |
| 2062 |
} |
| 2063 |
if (eleTextContent === textContent) { |
| 2064 |
return $($els[i]); |
| 2065 |
} |
| 2066 |
} |
| 2067 |
} |
| 2068 |
|
| 2069 |
return $(); |
| 2070 |
}, |
| 2071 |
|
| 2072 |
getItem: function(value) { |
| 2073 |
return this.getElementWithValue(value, this.$control.children()); |
| 2074 |
}, |
| 2075 |
|
| 2076 |
getFirstItemMatchedByTextContent: function(textContent, ignoreCase) { |
| 2077 |
ignoreCase = (ignoreCase !== null && ignoreCase === true) ? true : false; |
| 2078 |
return this.getElementWithTextContent(textContent, ignoreCase, this.$dropdown_content.find('[data-selectable]')); |
| 2079 |
}, |
| 2080 |
|
| 2081 |
addItems: function(values, silent) { |
| 2082 |
this.buffer = document.createDocumentFragment(); |
| 2083 |
|
| 2084 |
var childNodes = this.$control[0].childNodes; |
| 2085 |
for (var i = 0; i < childNodes.length; i++) { |
| 2086 |
this.buffer.appendChild(childNodes[i]); |
| 2087 |
} |
| 2088 |
|
| 2089 |
var items = Array.isArray(values) ? values : [values]; |
| 2090 |
for (var i = 0, n = items.length; i < n; i++) { |
| 2091 |
this.isPending = (i < n - 1); |
| 2092 |
this.addItem(items[i], silent); |
| 2093 |
} |
| 2094 |
|
| 2095 |
var control = this.$control[0]; |
| 2096 |
control.insertBefore(this.buffer, control.firstChild); |
| 2097 |
|
| 2098 |
this.buffer = null; |
| 2099 |
}, |
| 2100 |
|
| 2101 |
addItem: function(value, silent) { |
| 2102 |
var events = silent ? [] : ['change']; |
| 2103 |
|
| 2104 |
debounce_events(this, events, function() { |
| 2105 |
var $item, $option, $options; |
| 2106 |
var self = this; |
| 2107 |
var inputMode = self.settings.mode; |
| 2108 |
var i, active, value_next, wasFull; |
| 2109 |
value = hash_key(value); |
| 2110 |
|
| 2111 |
if (self.items.indexOf(value) !== -1) { |
| 2112 |
if (inputMode === 'single') self.close(); |
| 2113 |
return; |
| 2114 |
} |
| 2115 |
|
| 2116 |
if (!self.options.hasOwnProperty(value)) return; |
| 2117 |
if (inputMode === 'single') self.clear(silent); |
| 2118 |
if (inputMode === 'multi' && self.isFull()) return; |
| 2119 |
|
| 2120 |
$item = $(self.render('item', self.options[value])); |
| 2121 |
wasFull = self.isFull(); |
| 2122 |
self.items.splice(self.caretPos, 0, value); |
| 2123 |
self.insertAtCaret($item); |
| 2124 |
if (!self.isPending || (!wasFull && self.isFull())) { |
| 2125 |
self.refreshState(); |
| 2126 |
} |
| 2127 |
|
| 2128 |
if (self.isSetup) { |
| 2129 |
$options = self.$dropdown_content.find('[data-selectable]'); |
| 2130 |
|
| 2131 |
if (!self.isPending) { |
| 2132 |
$option = self.getOption(value); |
| 2133 |
value_next = self.getAdjacentOption($option, 1).attr('data-value'); |
| 2134 |
self.refreshOptions(self.isFocused && inputMode !== 'single'); |
| 2135 |
if (value_next) { |
| 2136 |
self.setActiveOption(self.getOption(value_next)); |
| 2137 |
} |
| 2138 |
} |
| 2139 |
|
| 2140 |
if (!$options.length || self.isFull()) { |
| 2141 |
self.close(); |
| 2142 |
} else if (!self.isPending) { |
| 2143 |
self.positionDropdown(); |
| 2144 |
} |
| 2145 |
|
| 2146 |
self.updatePlaceholder(); |
| 2147 |
self.trigger('item_add', value, $item); |
| 2148 |
|
| 2149 |
if (!self.isPending) { |
| 2150 |
self.updateOriginalInput({silent: silent}); |
| 2151 |
} |
| 2152 |
} |
| 2153 |
}); |
| 2154 |
}, |
| 2155 |
|
| 2156 |
removeItem: function(value, silent) { |
| 2157 |
var self = this; |
| 2158 |
var $item, i, idx; |
| 2159 |
|
| 2160 |
$item = (value instanceof $) ? value : self.getItem(value); |
| 2161 |
value = hash_key($item.attr('data-value')); |
| 2162 |
i = self.items.indexOf(value); |
| 2163 |
|
| 2164 |
if (i !== -1) { |
| 2165 |
self.trigger('item_before_remove', value, $item); |
| 2166 |
$item.remove(); |
| 2167 |
if ($item.hasClass('active')) { |
| 2168 |
$item.removeClass('active'); |
| 2169 |
idx = self.$activeItems.indexOf($item[0]); |
| 2170 |
self.$activeItems.splice(idx, 1); |
| 2171 |
$item.removeClass('active'); |
| 2172 |
} |
| 2173 |
|
| 2174 |
self.items.splice(i, 1); |
| 2175 |
self.lastQuery = null; |
| 2176 |
if (!self.settings.persist && self.userOptions.hasOwnProperty(value)) { |
| 2177 |
self.removeOption(value, silent); |
| 2178 |
} |
| 2179 |
|
| 2180 |
if (i < self.caretPos) { |
| 2181 |
self.setCaret(self.caretPos - 1); |
| 2182 |
} |
| 2183 |
|
| 2184 |
self.refreshState(); |
| 2185 |
self.updatePlaceholder(); |
| 2186 |
self.updateOriginalInput({silent: silent}); |
| 2187 |
self.positionDropdown(); |
| 2188 |
self.trigger('item_remove', value, $item); |
| 2189 |
} |
| 2190 |
}, |
| 2191 |
|
| 2192 |
createItem: function(input, triggerDropdown) { |
| 2193 |
var self = this; |
| 2194 |
var caret = self.caretPos; |
| 2195 |
input = input || (self.$control_input.val() || '').trim(); |
| 2196 |
|
| 2197 |
var callback = arguments[arguments.length - 1]; |
| 2198 |
if (typeof callback !== 'function') callback = function() {}; |
| 2199 |
|
| 2200 |
if (typeof triggerDropdown !== 'boolean') { |
| 2201 |
triggerDropdown = true; |
| 2202 |
} |
| 2203 |
|
| 2204 |
if (!self.canCreate(input)) { |
| 2205 |
callback(); |
| 2206 |
return false; |
| 2207 |
} |
| 2208 |
|
| 2209 |
self.lock(); |
| 2210 |
|
| 2211 |
var setup = (typeof self.settings.create === 'function') ? this.settings.create : function(input) { |
| 2212 |
var data = {}; |
| 2213 |
data[self.settings.labelField] = input; |
| 2214 |
var key = input; |
| 2215 |
if ( self.settings.formatValueToKey && typeof self.settings.formatValueToKey === 'function' ) { |
| 2216 |
key = self.settings.formatValueToKey.apply(this, [key]); |
| 2217 |
if (key === null || typeof key === 'undefined' || typeof key === 'object' || typeof key === 'function') { |
| 2218 |
throw new Error('Selectize "formatValueToKey" setting must be a function that returns a value other than object or function.'); |
| 2219 |
} |
| 2220 |
} |
| 2221 |
data[self.settings.valueField] = key; |
| 2222 |
return data; |
| 2223 |
}; |
| 2224 |
|
| 2225 |
var create = once(function(data) { |
| 2226 |
self.unlock(); |
| 2227 |
|
| 2228 |
if (!data || typeof data !== 'object') return callback(); |
| 2229 |
var value = hash_key(data[self.settings.valueField]); |
| 2230 |
if (typeof value !== 'string') return callback(); |
| 2231 |
|
| 2232 |
self.setTextboxValue(''); |
| 2233 |
self.addOption(data); |
| 2234 |
self.setCaret(caret); |
| 2235 |
self.addItem(value); |
| 2236 |
self.refreshOptions(triggerDropdown && self.settings.mode !== 'single'); |
| 2237 |
callback(data); |
| 2238 |
}); |
| 2239 |
|
| 2240 |
var output = setup.apply(this, [input, create]); |
| 2241 |
if (typeof output !== 'undefined') { |
| 2242 |
create(output); |
| 2243 |
} |
| 2244 |
|
| 2245 |
return true; |
| 2246 |
}, |
| 2247 |
|
| 2248 |
refreshItems: function(silent) { |
| 2249 |
this.lastQuery = null; |
| 2250 |
|
| 2251 |
if (this.isSetup) { |
| 2252 |
this.addItem(this.items, silent); |
| 2253 |
} |
| 2254 |
|
| 2255 |
this.refreshState(); |
| 2256 |
this.updateOriginalInput({silent: silent}); |
| 2257 |
}, |
| 2258 |
|
| 2259 |
refreshState: function() { |
| 2260 |
this.refreshValidityState(); |
| 2261 |
this.refreshClasses(); |
| 2262 |
}, |
| 2263 |
|
| 2264 |
refreshValidityState: function() { |
| 2265 |
if (!this.isRequired) return false; |
| 2266 |
|
| 2267 |
var invalid = !this.items.length; |
| 2268 |
|
| 2269 |
this.isInvalid = invalid; |
| 2270 |
this.$control_input.prop('required', invalid); |
| 2271 |
this.$input.prop('required', !invalid); |
| 2272 |
}, |
| 2273 |
|
| 2274 |
refreshClasses: function() { |
| 2275 |
var self = this; |
| 2276 |
var isFull = self.isFull(); |
| 2277 |
var isLocked = self.isLocked; |
| 2278 |
|
| 2279 |
self.$wrapper |
| 2280 |
.toggleClass('rtl', self.rtl); |
| 2281 |
|
| 2282 |
self.$control |
| 2283 |
.toggleClass('focus', self.isFocused) |
| 2284 |
.toggleClass('disabled', self.isDisabled) |
| 2285 |
.toggleClass('required', self.isRequired) |
| 2286 |
.toggleClass('invalid', self.isInvalid) |
| 2287 |
.toggleClass('locked', isLocked) |
| 2288 |
.toggleClass('full', isFull).toggleClass('not-full', !isFull) |
| 2289 |
.toggleClass('input-active', self.isFocused && !self.isInputHidden) |
| 2290 |
.toggleClass('dropdown-active', self.isOpen) |
| 2291 |
.toggleClass('has-options', !$.isEmptyObject(self.options)) |
| 2292 |
.toggleClass('has-items', self.items.length > 0); |
| 2293 |
|
| 2294 |
self.$control_input.data('grow', !isFull && !isLocked); |
| 2295 |
}, |
| 2296 |
|
| 2297 |
isFull: function() { |
| 2298 |
return this.settings.maxItems !== null && this.items.length >= this.settings.maxItems; |
| 2299 |
}, |
| 2300 |
|
| 2301 |
updateOriginalInput: function(opts) { |
| 2302 |
var i, n, existing, fresh, old, $options, label, value, values, self = this; |
| 2303 |
opts = opts || {}; |
| 2304 |
|
| 2305 |
if (self.tagType === TAG_SELECT) { |
| 2306 |
$options = self.$input.find('option'); |
| 2307 |
existing = []; |
| 2308 |
fresh = []; |
| 2309 |
old = []; |
| 2310 |
values = []; |
| 2311 |
|
| 2312 |
$options.get().forEach(function(option) { |
| 2313 |
existing.push(option.value); |
| 2314 |
}); |
| 2315 |
|
| 2316 |
self.items.forEach(function(item) { |
| 2317 |
label = self.options[item][self.settings.labelField] || ''; |
| 2318 |
|
| 2319 |
values.push(item); |
| 2320 |
|
| 2321 |
if (existing.indexOf(item) != -1) { |
| 2322 |
return; |
| 2323 |
} |
| 2324 |
|
| 2325 |
fresh.push('<option value="' + escape_html(item) + '" selected="selected">' + escape_html(label) + '</option>'); |
| 2326 |
}); |
| 2327 |
|
| 2328 |
old = existing.filter(function(value) { |
| 2329 |
return values.indexOf(value) < 0; |
| 2330 |
}).map(function(value) { |
| 2331 |
return 'option[value="' + escape_html(value) + '"]'; |
| 2332 |
}); |
| 2333 |
|
| 2334 |
if (existing.length - old.length + fresh.length === 0 && !self.$input.attr('multiple')) { |
| 2335 |
fresh.push('<option value="" selected="selected"></option>'); |
| 2336 |
} |
| 2337 |
|
| 2338 |
self.$input.find(old.join(', ')).remove(); |
| 2339 |
self.$input.append(fresh.join('')); |
| 2340 |
} else { |
| 2341 |
self.$input.val(self.getValue()); |
| 2342 |
self.$input.attr('value',self.$input.val()); |
| 2343 |
} |
| 2344 |
|
| 2345 |
if (self.isSetup) { |
| 2346 |
if (!opts.silent) { |
| 2347 |
self.trigger('change', self.$input.val()); |
| 2348 |
} |
| 2349 |
} |
| 2350 |
}, |
| 2351 |
|
| 2352 |
updatePlaceholder: function() { |
| 2353 |
if (!this.settings.placeholder) return; |
| 2354 |
var $input = this.$control_input; |
| 2355 |
|
| 2356 |
if (this.items.length) { |
| 2357 |
$input.removeAttr('placeholder'); |
| 2358 |
} else { |
| 2359 |
$input.attr('placeholder', this.settings.placeholder); |
| 2360 |
} |
| 2361 |
$input.triggerHandler('update', {force: true}); |
| 2362 |
}, |
| 2363 |
|
| 2364 |
open: function() { |
| 2365 |
var self = this; |
| 2366 |
|
| 2367 |
if ( |
| 2368 |
self.isLocked || |
| 2369 |
self.isOpen || |
| 2370 |
(self.settings.mode === "multi" && self.isFull()) |
| 2371 |
) |
| 2372 |
return; |
| 2373 |
self.focus(); |
| 2374 |
self.isOpen = true; |
| 2375 |
self.refreshState(); |
| 2376 |
self.$dropdown.css({ visibility: 'hidden', display: 'block' }); |
| 2377 |
self.setupDropdownHeight(); |
| 2378 |
self.positionDropdown(); |
| 2379 |
self.$dropdown.css({visibility: 'visible'}); |
| 2380 |
self.trigger('dropdown_open', self.$dropdown); |
| 2381 |
}, |
| 2382 |
|
| 2383 |
close: function() { |
| 2384 |
var self = this; |
| 2385 |
var trigger = self.isOpen; |
| 2386 |
|
| 2387 |
if (self.settings.mode === 'single' && self.items.length) { |
| 2388 |
self.hideInput(); |
| 2389 |
|
| 2390 |
if (self.isBlurring) { |
| 2391 |
self.$control_input[0].blur(); |
| 2392 |
} |
| 2393 |
} |
| 2394 |
|
| 2395 |
self.isOpen = false; |
| 2396 |
self.$dropdown.hide(); |
| 2397 |
self.setActiveOption(null); |
| 2398 |
self.refreshState(); |
| 2399 |
|
| 2400 |
if (trigger) self.trigger('dropdown_close', self.$dropdown); |
| 2401 |
}, |
| 2402 |
|
| 2403 |
positionDropdown: function() { |
| 2404 |
var $control = this.$control; |
| 2405 |
var offset = this.settings.dropdownParent === 'body' ? $control.offset() : $control.position(); |
| 2406 |
offset.top += $control.outerHeight(true); |
| 2407 |
var w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content'; |
| 2408 |
if (this.settings.minWidth && this.settings.minWidth > w) |
| 2409 |
{ |
| 2410 |
w = this.settings.minWidth; |
| 2411 |
} |
| 2412 |
|
| 2413 |
if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) { |
| 2414 |
w = '100%'; |
| 2415 |
} |
| 2416 |
|
| 2417 |
this.$dropdown.css({ |
| 2418 |
width : w, |
| 2419 |
minWidth : $control.outerWidth(true), |
| 2420 |
top : offset.top, |
| 2421 |
left : offset.left |
| 2422 |
}); |
| 2423 |
}, |
| 2424 |
|
| 2425 |
setupDropdownHeight: function () { |
| 2426 |
if (typeof this.settings.dropdownSize === 'object' && this.settings.dropdownSize.sizeType !== 'auto') { |
| 2427 |
var height = this.settings.dropdownSize.sizeValue; |
| 2428 |
|
| 2429 |
if (this.settings.dropdownSize.sizeType === 'numberItems') { |
| 2430 |
var $items = this.$dropdown_content.find('*').not('.optgroup, .highlight').not(this.settings.ignoreOnDropwdownHeight); |
| 2431 |
var totalHeight = 0; |
| 2432 |
var marginTop = 0; |
| 2433 |
var marginBottom = 0; |
| 2434 |
var separatorHeight = 0; |
| 2435 |
|
| 2436 |
|
| 2437 |
for (var i = 0; i < height; i++) { |
| 2438 |
var $item = $($items[i]); |
| 2439 |
|
| 2440 |
if ($item.length === 0) { |
| 2441 |
break; |
| 2442 |
} |
| 2443 |
|
| 2444 |
totalHeight += $item.outerHeight(true); |
| 2445 |
if (typeof $item.data('selectable') == 'undefined') { |
| 2446 |
if ($item.hasClass('optgroup-header')) { |
| 2447 |
var styles = window.getComputedStyle($item.parent()[0], ':before'); |
| 2448 |
|
| 2449 |
if (styles) { |
| 2450 |
marginTop = styles.marginTop ? Number(styles.marginTop.replace(/\W*(\w)\w*/g, '$1')) : 0; |
| 2451 |
marginBottom = styles.marginBottom ? Number(styles.marginBottom.replace(/\W*(\w)\w*/g, '$1')) : 0; |
| 2452 |
separatorHeight = styles.borderTopWidth ? Number(styles.borderTopWidth.replace(/\W*(\w)\w*/g, '$1')) : 0; |
| 2453 |
} |
| 2454 |
} |
| 2455 |
height++; |
| 2456 |
} |
| 2457 |
|
| 2458 |
} |
| 2459 |
|
| 2460 |
var paddingTop = this.$dropdown_content.css('padding-top') ? Number(this.$dropdown_content.css('padding-top').replace(/\W*(\w)\w*/g, '$1')) : 0; |
| 2461 |
var paddingBottom = this.$dropdown_content.css('padding-bottom') ? Number(this.$dropdown_content.css('padding-bottom').replace(/\W*(\w)\w*/g, '$1')) : 0; |
| 2462 |
|
| 2463 |
height = (totalHeight + paddingTop + paddingBottom + marginTop + marginBottom + separatorHeight) + 'px'; |
| 2464 |
} else if (this.settings.dropdownSize.sizeType !== 'fixedHeight') { |
| 2465 |
console.warn('Selectize.js - Value of "sizeType" must be "fixedHeight" or "numberItems'); |
| 2466 |
return; |
| 2467 |
} |
| 2468 |
|
| 2469 |
this.$dropdown_content.css({ height: height, maxHeight: 'none' }); |
| 2470 |
} |
| 2471 |
}, |
| 2472 |
|
| 2473 |
clear: function(silent) { |
| 2474 |
var self = this; |
| 2475 |
|
| 2476 |
if (!self.items.length) return; |
| 2477 |
self.$control.children(':not(input)').remove(); |
| 2478 |
self.items = []; |
| 2479 |
self.lastQuery = null; |
| 2480 |
self.setCaret(0); |
| 2481 |
self.setActiveItem(null); |
| 2482 |
self.updatePlaceholder(); |
| 2483 |
self.updateOriginalInput({silent: silent}); |
| 2484 |
self.refreshState(); |
| 2485 |
self.showInput(); |
| 2486 |
self.trigger('clear'); |
| 2487 |
}, |
| 2488 |
|
| 2489 |
insertAtCaret: function($el) { |
| 2490 |
var caret = Math.min(this.caretPos, this.items.length); |
| 2491 |
var el = $el[0]; |
| 2492 |
var target = this.buffer || this.$control[0]; |
| 2493 |
|
| 2494 |
if (caret === 0) { |
| 2495 |
target.insertBefore(el, target.firstChild); |
| 2496 |
} else { |
| 2497 |
target.insertBefore(el, target.childNodes[caret]); |
| 2498 |
} |
| 2499 |
|
| 2500 |
this.setCaret(caret + 1); |
| 2501 |
}, |
| 2502 |
|
| 2503 |
deleteSelection: function(e) { |
| 2504 |
var i, n, direction, selection, values, caret, option_select, $option_select, $tail; |
| 2505 |
var self = this; |
| 2506 |
|
| 2507 |
direction = (e && e.keyCode === KEY_BACKSPACE) ? -1 : 1; |
| 2508 |
selection = getInputSelection(self.$control_input[0]); |
| 2509 |
|
| 2510 |
if (self.$activeOption && !self.settings.hideSelected) { |
| 2511 |
if (typeof self.settings.deselectBehavior === 'string' && self.settings.deselectBehavior === 'top') { |
| 2512 |
option_select = self.getFirstOption().attr('data-value'); |
| 2513 |
} else { |
| 2514 |
option_select = self.getAdjacentOption(self.$activeOption, -1).attr('data-value'); |
| 2515 |
} |
| 2516 |
} |
| 2517 |
|
| 2518 |
values = []; |
| 2519 |
|
| 2520 |
if (self.$activeItems.length) { |
| 2521 |
$tail = self.$control.children('.active:' + (direction > 0 ? 'last' : 'first')); |
| 2522 |
caret = self.$control.children(':not(input)').index($tail); |
| 2523 |
if (direction > 0) { caret++; } |
| 2524 |
|
| 2525 |
for (i = 0, n = self.$activeItems.length; i < n; i++) { |
| 2526 |
values.push($(self.$activeItems[i]).attr('data-value')); |
| 2527 |
} |
| 2528 |
if (e) { |
| 2529 |
e.preventDefault(); |
| 2530 |
e.stopPropagation(); |
| 2531 |
} |
| 2532 |
} else if ((self.isFocused || self.settings.mode === 'single') && self.items.length) { |
| 2533 |
if (direction < 0 && selection.start === 0 && selection.length === 0) { |
| 2534 |
values.push(self.items[self.caretPos - 1]); |
| 2535 |
} else if (direction > 0 && selection.start === self.$control_input.val().length) { |
| 2536 |
values.push(self.items[self.caretPos]); |
| 2537 |
} |
| 2538 |
} |
| 2539 |
|
| 2540 |
if (!values.length || (typeof self.settings.onDelete === 'function' && self.settings.onDelete.apply(self, [values]) === false)) { |
| 2541 |
return false; |
| 2542 |
} |
| 2543 |
|
| 2544 |
if (typeof caret !== 'undefined') { |
| 2545 |
self.setCaret(caret); |
| 2546 |
} |
| 2547 |
while (values.length) { |
| 2548 |
self.removeItem(values.pop()); |
| 2549 |
} |
| 2550 |
|
| 2551 |
self.showInput(); |
| 2552 |
self.positionDropdown(); |
| 2553 |
self.refreshOptions(true); |
| 2554 |
|
| 2555 |
if (option_select) { |
| 2556 |
$option_select = self.getOption(option_select); |
| 2557 |
if ($option_select.length) { |
| 2558 |
self.setActiveOption($option_select); |
| 2559 |
} |
| 2560 |
} |
| 2561 |
|
| 2562 |
return true; |
| 2563 |
}, |
| 2564 |
|
| 2565 |
advanceSelection: function(direction, e) { |
| 2566 |
var tail, selection, idx, valueLength, cursorAtEdge, $tail; |
| 2567 |
var self = this; |
| 2568 |
|
| 2569 |
if (direction === 0) return; |
| 2570 |
if (self.rtl) direction *= -1; |
| 2571 |
|
| 2572 |
tail = direction > 0 ? 'last' : 'first'; |
| 2573 |
selection = getInputSelection(self.$control_input[0]); |
| 2574 |
|
| 2575 |
if (self.isFocused && !self.isInputHidden) { |
| 2576 |
valueLength = self.$control_input.val().length; |
| 2577 |
cursorAtEdge = direction < 0 |
| 2578 |
? selection.start === 0 && selection.length === 0 |
| 2579 |
: selection.start === valueLength; |
| 2580 |
|
| 2581 |
if (cursorAtEdge && !valueLength) { |
| 2582 |
self.advanceCaret(direction, e); |
| 2583 |
} |
| 2584 |
} else { |
| 2585 |
$tail = self.$control.children('.active:' + tail); |
| 2586 |
if ($tail.length) { |
| 2587 |
idx = self.$control.children(':not(input)').index($tail); |
| 2588 |
self.setActiveItem(null); |
| 2589 |
self.setCaret(direction > 0 ? idx + 1 : idx); |
| 2590 |
} |
| 2591 |
} |
| 2592 |
}, |
| 2593 |
|
| 2594 |
advanceCaret: function(direction, e) { |
| 2595 |
var self = this, fn, $adj; |
| 2596 |
|
| 2597 |
if (direction === 0) return; |
| 2598 |
|
| 2599 |
fn = direction > 0 ? 'next' : 'prev'; |
| 2600 |
if (self.isShiftDown) { |
| 2601 |
$adj = self.$control_input[fn](); |
| 2602 |
if ($adj.length) { |
| 2603 |
self.hideInput(); |
| 2604 |
self.setActiveItem($adj); |
| 2605 |
e && e.preventDefault(); |
| 2606 |
} |
| 2607 |
} else { |
| 2608 |
self.setCaret(self.caretPos + direction); |
| 2609 |
} |
| 2610 |
}, |
| 2611 |
|
| 2612 |
setCaret: function(i) { |
| 2613 |
var self = this; |
| 2614 |
|
| 2615 |
if (self.settings.mode === 'single') { |
| 2616 |
i = self.items.length; |
| 2617 |
} else { |
| 2618 |
i = Math.max(0, Math.min(self.items.length, i)); |
| 2619 |
} |
| 2620 |
|
| 2621 |
if(!self.isPending) { |
| 2622 |
var j, n, fn, $children, $child; |
| 2623 |
$children = self.$control.children(':not(input)'); |
| 2624 |
for (j = 0, n = $children.length; j < n; j++) { |
| 2625 |
$child = $($children[j]).detach(); |
| 2626 |
if (j < i) { |
| 2627 |
self.$control_input.before($child); |
| 2628 |
} else { |
| 2629 |
self.$control.append($child); |
| 2630 |
} |
| 2631 |
} |
| 2632 |
} |
| 2633 |
|
| 2634 |
self.caretPos = i; |
| 2635 |
}, |
| 2636 |
|
| 2637 |
lock: function() { |
| 2638 |
this.close(); |
| 2639 |
this.isLocked = true; |
| 2640 |
this.refreshState(); |
| 2641 |
}, |
| 2642 |
|
| 2643 |
unlock: function() { |
| 2644 |
this.isLocked = false; |
| 2645 |
this.refreshState(); |
| 2646 |
}, |
| 2647 |
|
| 2648 |
disable: function() { |
| 2649 |
var self = this; |
| 2650 |
self.$input.prop('disabled', true); |
| 2651 |
self.$control_input.prop('disabled', true).prop('tabindex', -1); |
| 2652 |
self.isDisabled = true; |
| 2653 |
self.lock(); |
| 2654 |
}, |
| 2655 |
|
| 2656 |
enable: function() { |
| 2657 |
var self = this; |
| 2658 |
self.$input.prop('disabled', false); |
| 2659 |
self.$control_input.prop('disabled', false).prop('tabindex', self.tabIndex); |
| 2660 |
self.isDisabled = false; |
| 2661 |
self.unlock(); |
| 2662 |
}, |
| 2663 |
|
| 2664 |
destroy: function() { |
| 2665 |
var self = this; |
| 2666 |
var eventNS = self.eventNS; |
| 2667 |
var revertSettings = self.revertSettings; |
| 2668 |
|
| 2669 |
self.trigger('destroy'); |
| 2670 |
self.off(); |
| 2671 |
self.$wrapper.remove(); |
| 2672 |
self.$dropdown.remove(); |
| 2673 |
|
| 2674 |
self.$input |
| 2675 |
.html('') |
| 2676 |
.append(revertSettings.$children) |
| 2677 |
.removeAttr('tabindex') |
| 2678 |
.removeClass('selectized') |
| 2679 |
.attr({tabindex: revertSettings.tabindex}) |
| 2680 |
.show(); |
| 2681 |
|
| 2682 |
self.$control_input.removeData('grow'); |
| 2683 |
self.$input.removeData('selectize'); |
| 2684 |
|
| 2685 |
if (--Selectize.count == 0 && Selectize.$testInput) { |
| 2686 |
Selectize.$testInput.remove(); |
| 2687 |
Selectize.$testInput = undefined; |
| 2688 |
} |
| 2689 |
|
| 2690 |
$(window).off(eventNS); |
| 2691 |
$(document).off(eventNS); |
| 2692 |
$(document.body).off(eventNS); |
| 2693 |
|
| 2694 |
delete self.$input[0].selectize; |
| 2695 |
}, |
| 2696 |
|
| 2697 |
render: function(templateName, data) { |
| 2698 |
var value, id, label; |
| 2699 |
var html = ''; |
| 2700 |
var cache = false; |
| 2701 |
var self = this; |
| 2702 |
var regex_tag = /^[\t \r\n]*<([a-z][a-z0-9\-_]*(?:\:[a-z][a-z0-9\-_]*)?)/i; |
| 2703 |
|
| 2704 |
if (templateName === 'option' || templateName === 'item') { |
| 2705 |
value = hash_key(data[self.settings.valueField]); |
| 2706 |
cache = !!value; |
| 2707 |
} |
| 2708 |
|
| 2709 |
if (cache) { |
| 2710 |
if (!isset(self.renderCache[templateName])) { |
| 2711 |
self.renderCache[templateName] = {}; |
| 2712 |
} |
| 2713 |
if (self.renderCache[templateName].hasOwnProperty(value)) { |
| 2714 |
return self.renderCache[templateName][value]; |
| 2715 |
} |
| 2716 |
} |
| 2717 |
|
| 2718 |
html = $(self.settings.render[templateName].apply(this, [data, escape_html])); |
| 2719 |
|
| 2720 |
if (templateName === 'option' || templateName === 'option_create') { |
| 2721 |
if (!data[self.settings.disabledField]) { |
| 2722 |
html.attr('data-selectable', ''); |
| 2723 |
} |
| 2724 |
} |
| 2725 |
else if (templateName === 'optgroup') { |
| 2726 |
id = data[self.settings.optgroupValueField] || ''; |
| 2727 |
html.attr('data-group', id); |
| 2728 |
if(data[self.settings.disabledField]) { |
| 2729 |
html.attr('data-disabled', ''); |
| 2730 |
} |
| 2731 |
} |
| 2732 |
if (templateName === 'option' || templateName === 'item') { |
| 2733 |
html.attr('data-value', value || ''); |
| 2734 |
} |
| 2735 |
|
| 2736 |
if (cache) { |
| 2737 |
self.renderCache[templateName][value] = html[0]; |
| 2738 |
} |
| 2739 |
|
| 2740 |
return html[0]; |
| 2741 |
}, |
| 2742 |
|
| 2743 |
clearCache: function(templateName) { |
| 2744 |
var self = this; |
| 2745 |
if (typeof templateName === 'undefined') { |
| 2746 |
self.renderCache = {}; |
| 2747 |
} else { |
| 2748 |
delete self.renderCache[templateName]; |
| 2749 |
} |
| 2750 |
}, |
| 2751 |
|
| 2752 |
canCreate: function(input) { |
| 2753 |
var self = this; |
| 2754 |
if (!self.settings.create) return false; |
| 2755 |
var filter = self.settings.createFilter; |
| 2756 |
return input.length |
| 2757 |
&& (typeof filter !== 'function' || filter.apply(self, [input])) |
| 2758 |
&& (typeof filter !== 'string' || new RegExp(filter).test(input)) |
| 2759 |
&& (!(filter instanceof RegExp) || filter.test(input)); |
| 2760 |
} |
| 2761 |
}); |
| 2762 |
|
| 2763 |
Selectize.count = 0; |
| 2764 |
Selectize.defaults = { |
| 2765 |
options: [], |
| 2766 |
optgroups: [], |
| 2767 |
|
| 2768 |
plugins: [], |
| 2769 |
delimiter: ',', |
| 2770 |
splitOn: null, |
| 2771 |
persist: true, |
| 2772 |
diacritics: true, |
| 2773 |
create: false, |
| 2774 |
showAddOptionOnCreate: true, |
| 2775 |
createOnBlur: false, |
| 2776 |
createFilter: null, |
| 2777 |
highlight: true, |
| 2778 |
openOnFocus: true, |
| 2779 |
maxOptions: 1000, |
| 2780 |
maxItems: null, |
| 2781 |
hideSelected: null, |
| 2782 |
addPrecedence: false, |
| 2783 |
selectOnTab: true, |
| 2784 |
preload: false, |
| 2785 |
allowEmptyOption: false, |
| 2786 |
showEmptyOptionInDropdown: false, |
| 2787 |
emptyOptionLabel: '--', |
| 2788 |
setFirstOptionActive: false, |
| 2789 |
closeAfterSelect: false, |
| 2790 |
closeDropdownThreshold: 250, |
| 2791 |
|
| 2792 |
scrollDuration: 60, |
| 2793 |
deselectBehavior: 'previous', |
| 2794 |
loadThrottle: 300, |
| 2795 |
loadingClass: 'loading', |
| 2796 |
|
| 2797 |
dataAttr: 'data-data', |
| 2798 |
optgroupField: 'optgroup', |
| 2799 |
valueField: 'value', |
| 2800 |
labelField: 'text', |
| 2801 |
disabledField: 'disabled', |
| 2802 |
optgroupLabelField: 'label', |
| 2803 |
optgroupValueField: 'value', |
| 2804 |
lockOptgroupOrder: false, |
| 2805 |
|
| 2806 |
sortField: '$order', |
| 2807 |
searchField: ['text'], |
| 2808 |
searchConjunction: 'and', |
| 2809 |
respect_word_boundaries: false, |
| 2810 |
normalize: true, |
| 2811 |
|
| 2812 |
mode: null, |
| 2813 |
wrapperClass: '', |
| 2814 |
inputClass: '', |
| 2815 |
dropdownClass: '', |
| 2816 |
dropdownContentClass: '', |
| 2817 |
|
| 2818 |
dropdownParent: null, |
| 2819 |
|
| 2820 |
copyClassesToDropdown: true, |
| 2821 |
dropdownSize: { |
| 2822 |
sizeType: 'auto', |
| 2823 |
sizeValue: 'auto', |
| 2824 |
}, |
| 2825 |
|
| 2826 |
ignoreOnDropwdownHeight: 'img, i', |
| 2827 |
search: true, |
| 2828 |
showArrow: true, |
| 2829 |
|
| 2830 |
render: { |
| 2831 |
} |
| 2832 |
}; |
| 2833 |
|
| 2834 |
$.fn.selectize = function (settings_user) { |
| 2835 |
var defaults = $.fn.selectize.defaults; |
| 2836 |
var settings = $.extend({}, defaults, settings_user); |
| 2837 |
var attr_data = settings.dataAttr; |
| 2838 |
var field_label = settings.labelField; |
| 2839 |
var field_value = settings.valueField; |
| 2840 |
var field_disabled = settings.disabledField; |
| 2841 |
var field_optgroup = settings.optgroupField; |
| 2842 |
var field_optgroup_label = settings.optgroupLabelField; |
| 2843 |
var field_optgroup_value = settings.optgroupValueField; |
| 2844 |
|
| 2845 |
var init_textbox = function ($input, settings_element) { |
| 2846 |
var i, n, values, option; |
| 2847 |
|
| 2848 |
var data_raw = $input.attr(attr_data); |
| 2849 |
|
| 2850 |
if (!data_raw) { |
| 2851 |
var value = ($input.val() || '').trim(); |
| 2852 |
if (!settings.allowEmptyOption && !value.length) return; |
| 2853 |
values = value.split(settings.delimiter); |
| 2854 |
for (i = 0, n = values.length; i < n; i++) { |
| 2855 |
option = {}; |
| 2856 |
option[field_label] = values[i]; |
| 2857 |
option[field_value] = values[i]; |
| 2858 |
settings_element.options.push(option); |
| 2859 |
} |
| 2860 |
settings_element.items = values; |
| 2861 |
} else { |
| 2862 |
settings_element.options = JSON.parse(data_raw); |
| 2863 |
for (i = 0, n = settings_element.options.length; i < n; i++) { |
| 2864 |
settings_element.items.push(settings_element.options[i][field_value]); |
| 2865 |
} |
| 2866 |
} |
| 2867 |
}; |
| 2868 |
|
| 2869 |
var init_select = function ($input, settings_element) { |
| 2870 |
var i, n, tagName, $children, order = 0; |
| 2871 |
var options = settings_element.options; |
| 2872 |
var optionsMap = {}; |
| 2873 |
|
| 2874 |
var readData = function ($el) { |
| 2875 |
var data = attr_data && $el.attr(attr_data); |
| 2876 |
var allData = $el.data(); |
| 2877 |
var obj = {}; |
| 2878 |
|
| 2879 |
if (typeof data === 'string' && data.length) { |
| 2880 |
if (isJSON(data)) { |
| 2881 |
Object.assign(obj, JSON.parse(data)) |
| 2882 |
} else { |
| 2883 |
obj[data] = data; |
| 2884 |
} |
| 2885 |
} |
| 2886 |
|
| 2887 |
|
| 2888 |
Object.assign(obj, allData); |
| 2889 |
|
| 2890 |
return obj || null; |
| 2891 |
}; |
| 2892 |
|
| 2893 |
var addOption = function ($option, group) { |
| 2894 |
$option = $($option); |
| 2895 |
|
| 2896 |
var value = hash_key($option.val()); |
| 2897 |
if (!value && !settings.allowEmptyOption) return; |
| 2898 |
|
| 2899 |
if (optionsMap.hasOwnProperty(value)) { |
| 2900 |
if (group) { |
| 2901 |
var arr = optionsMap[value][field_optgroup]; |
| 2902 |
if (!arr) { |
| 2903 |
optionsMap[value][field_optgroup] = group; |
| 2904 |
} else if (!Array.isArray(arr)) { |
| 2905 |
optionsMap[value][field_optgroup] = [arr, group]; |
| 2906 |
} else { |
| 2907 |
arr.push(group); |
| 2908 |
} |
| 2909 |
} |
| 2910 |
return; |
| 2911 |
} |
| 2912 |
|
| 2913 |
var option = readData($option) || {}; |
| 2914 |
option[field_value] = option[field_value] || value; |
| 2915 |
option[field_label] = option[field_label] || $option.text() || option[field_value]; |
| 2916 |
option[field_disabled] = option[field_disabled] || $option.prop('disabled'); |
| 2917 |
option[field_optgroup] = option[field_optgroup] || group; |
| 2918 |
option.styles = $option.attr('style') || ''; |
| 2919 |
option.classes = $option.attr('class') || ''; |
| 2920 |
|
| 2921 |
optionsMap[value] = option; |
| 2922 |
options.push(option); |
| 2923 |
|
| 2924 |
if ($option.is(':selected')) { |
| 2925 |
settings_element.items.push(value); |
| 2926 |
} |
| 2927 |
}; |
| 2928 |
|
| 2929 |
var addGroup = function ($optgroup) { |
| 2930 |
var i, n, id, optgroup, $options; |
| 2931 |
|
| 2932 |
$optgroup = $($optgroup); |
| 2933 |
id = $optgroup.attr('label'); |
| 2934 |
|
| 2935 |
if (id) { |
| 2936 |
optgroup = readData($optgroup) || {}; |
| 2937 |
optgroup[field_optgroup_label] = id; |
| 2938 |
optgroup[field_optgroup_value] = id; |
| 2939 |
optgroup[field_disabled] = $optgroup.prop('disabled'); |
| 2940 |
settings_element.optgroups.push(optgroup); |
| 2941 |
} |
| 2942 |
|
| 2943 |
$options = $('option', $optgroup); |
| 2944 |
for (i = 0, n = $options.length; i < n; i++) { |
| 2945 |
addOption($options[i], id); |
| 2946 |
} |
| 2947 |
}; |
| 2948 |
|
| 2949 |
settings_element.maxItems = $input.attr('multiple') ? null : 1; |
| 2950 |
|
| 2951 |
$children = $input.children(); |
| 2952 |
for (i = 0, n = $children.length; i < n; i++) { |
| 2953 |
tagName = $children[i].tagName.toLowerCase(); |
| 2954 |
if (tagName === 'optgroup') { |
| 2955 |
addGroup($children[i]); |
| 2956 |
} else if (tagName === 'option') { |
| 2957 |
addOption($children[i]); |
| 2958 |
} |
| 2959 |
} |
| 2960 |
}; |
| 2961 |
|
| 2962 |
return this.each(function () { |
| 2963 |
if (this.selectize) return; |
| 2964 |
|
| 2965 |
var instance; |
| 2966 |
var $input = $(this); |
| 2967 |
var tag_name = this.tagName.toLowerCase(); |
| 2968 |
var placeholder = $input.attr('placeholder') || $input.attr('data-placeholder'); |
| 2969 |
if (!placeholder && !settings.allowEmptyOption) { |
| 2970 |
placeholder = $input.children('option[value=""]').text(); |
| 2971 |
} |
| 2972 |
if (settings.allowEmptyOption && settings.showEmptyOptionInDropdown && !$input.children('option[value=""]').length) { |
| 2973 |
var input_html = $input.html(); |
| 2974 |
var label = escape_html(settings.emptyOptionLabel || '--'); |
| 2975 |
$input.html('<option value="">' + label + '</option>' + input_html); |
| 2976 |
} |
| 2977 |
|
| 2978 |
var settings_element = { |
| 2979 |
'placeholder': placeholder, |
| 2980 |
'options': [], |
| 2981 |
'optgroups': [], |
| 2982 |
'items': [] |
| 2983 |
}; |
| 2984 |
|
| 2985 |
if (tag_name === 'select') { |
| 2986 |
init_select($input, settings_element); |
| 2987 |
} else { |
| 2988 |
init_textbox($input, settings_element); |
| 2989 |
} |
| 2990 |
|
| 2991 |
instance = new Selectize($input, $.extend(true, {}, defaults, settings_element, settings_user)); |
| 2992 |
instance.settings_user = settings_user; |
| 2993 |
}); |
| 2994 |
}; |
| 2995 |
|
| 2996 |
$.fn.selectize.defaults = Selectize.defaults; |
| 2997 |
$.fn.selectize.support = { |
| 2998 |
validity: SUPPORTS_VALIDITY_API |
| 2999 |
}; |
| 3000 |
|
| 3001 |
Selectize.define("auto_position", function () { |
| 3002 |
var self = this; |
| 3003 |
|
| 3004 |
const POSITION = { |
| 3005 |
top: 'top', |
| 3006 |
bottom: 'bottom', |
| 3007 |
}; |
| 3008 |
|
| 3009 |
self.positionDropdown = (function () { |
| 3010 |
return function () { |
| 3011 |
const $control = this.$control; |
| 3012 |
const offset = this.settings.dropdownParent === 'body' ? $control.offset() : $control.position(); |
| 3013 |
offset.top += $control.outerHeight(true); |
| 3014 |
|
| 3015 |
const dropdownHeight = this.$dropdown.prop('scrollHeight') + 5; |
| 3016 |
const controlPosTop = this.$control.get(0).getBoundingClientRect().top; |
| 3017 |
const wrapperHeight = this.$wrapper.height(); |
| 3018 |
const controlPosBottom = self.$control.get(0).getBoundingClientRect().bottom |
| 3019 |
const position = |
| 3020 |
controlPosTop + dropdownHeight + wrapperHeight > window.innerHeight && |
| 3021 |
controlPosBottom - dropdownHeight - wrapperHeight >= 0 ? |
| 3022 |
POSITION.top : |
| 3023 |
POSITION.bottom; |
| 3024 |
let w = this.$wrapper[0].style.width !== 'fit-content' ? this.settings.dropdownParent === 'body' ? 'max-content' : '100%' : 'max-content'; |
| 3025 |
const styles = { |
| 3026 |
width: w, |
| 3027 |
minWidth : $control.outerWidth(true), |
| 3028 |
left: offset.left |
| 3029 |
}; |
| 3030 |
|
| 3031 |
if (position === POSITION.top) { |
| 3032 |
const styleToAdd = { bottom: offset.top, top: 'unset' }; |
| 3033 |
|
| 3034 |
if (this.settings.dropdownParent === 'body') { |
| 3035 |
styleToAdd.top = offset.top - this.$dropdown.outerHeight(true) - $control.outerHeight(true); |
| 3036 |
styleToAdd.bottom = 'unset'; |
| 3037 |
} |
| 3038 |
Object.assign(styles, styleToAdd); |
| 3039 |
this.$dropdown.addClass('selectize-position-top'); |
| 3040 |
this.$control.addClass('selectize-position-top'); |
| 3041 |
} else { |
| 3042 |
Object.assign(styles, { top: offset.top, bottom: 'unset' }); |
| 3043 |
this.$dropdown.removeClass('selectize-position-top'); |
| 3044 |
this.$control.removeClass('selectize-position-top'); |
| 3045 |
} |
| 3046 |
|
| 3047 |
if (this.settings.dropdownParent !== 'body' && w === 'max-content' && $control.outerWidth(true) >= this.$dropdown.outerWidth(true)) { |
| 3048 |
w = '100%'; |
| 3049 |
} |
| 3050 |
|
| 3051 |
this.$dropdown.css(styles); |
| 3052 |
}; |
| 3053 |
}()); |
| 3054 |
}); |
| 3055 |
|
| 3056 |
Selectize.define('auto_select_on_type', function(options) { |
| 3057 |
var self = this; |
| 3058 |
|
| 3059 |
self.onBlur = (function() { |
| 3060 |
var originalBlur = self.onBlur; |
| 3061 |
return function(e) { |
| 3062 |
var $matchedItem = self.getFirstItemMatchedByTextContent(self.lastValue, true); |
| 3063 |
if (typeof $matchedItem.attr('data-value') !== 'undefined' && self.getValue() !== $matchedItem.attr('data-value')) |
| 3064 |
{ |
| 3065 |
self.setValue($matchedItem.attr('data-value')); |
| 3066 |
} |
| 3067 |
return originalBlur.apply(this, arguments); |
| 3068 |
} |
| 3069 |
}()); |
| 3070 |
}); |
| 3071 |
|
| 3072 |
Selectize.define("autofill_disable", function (options) { |
| 3073 |
var self = this; |
| 3074 |
|
| 3075 |
self.setup = (function () { |
| 3076 |
var original = self.setup; |
| 3077 |
return function () { |
| 3078 |
original.apply(self, arguments); |
| 3079 |
|
| 3080 |
self.$control_input.attr({ name: nanoid(21), autocomplete: nanoid(21) }); |
| 3081 |
}; |
| 3082 |
})(); |
| 3083 |
}); |
| 3084 |
|
| 3085 |
Selectize.define("clear_button", function (options) { |
| 3086 |
var self = this; |
| 3087 |
|
| 3088 |
options = $.extend( |
| 3089 |
{ |
| 3090 |
title: "Clear", |
| 3091 |
className: "clear", |
| 3092 |
label: "×", |
| 3093 |
html: function (data) { |
| 3094 |
return ( |
| 3095 |
'<a class="' + data.className + '" title="' + data.title + '"> ' + data.label + '</a>' |
| 3096 |
); |
| 3097 |
}, |
| 3098 |
}, |
| 3099 |
options |
| 3100 |
); |
| 3101 |
|
| 3102 |
self.setup = (function () { |
| 3103 |
var original = self.setup; |
| 3104 |
return function () { |
| 3105 |
original.apply(self, arguments); |
| 3106 |
self.$button_clear = $(options.html(options)); |
| 3107 |
|
| 3108 |
if (self.settings.mode === "single") self.$wrapper.addClass("single"); |
| 3109 |
|
| 3110 |
self.$wrapper.append(self.$button_clear); |
| 3111 |
|
| 3112 |
if (self.getValue() === "" || self.getValue().length === 0) { |
| 3113 |
self.$wrapper.find("." + options.className).css("display", "none"); |
| 3114 |
} |
| 3115 |
|
| 3116 |
self.on("change", function () { |
| 3117 |
if (self.getValue() === "" || self.getValue().length === 0) { |
| 3118 |
self.$wrapper.find("." + options.className).css("display", "none"); |
| 3119 |
} else { |
| 3120 |
self.$wrapper.find("." + options.className).css("display", ""); |
| 3121 |
} |
| 3122 |
}); |
| 3123 |
|
| 3124 |
self.$wrapper.on("click", "." + options.className, function (e) { |
| 3125 |
e.preventDefault(); |
| 3126 |
e.stopImmediatePropagation(); |
| 3127 |
e.stopPropagation(); |
| 3128 |
|
| 3129 |
if (self.isLocked) return; |
| 3130 |
|
| 3131 |
self.clear(); |
| 3132 |
self.$wrapper.find("." + options.className).css("display", "none"); |
| 3133 |
}); |
| 3134 |
}; |
| 3135 |
})(); |
| 3136 |
}); |
| 3137 |
|
| 3138 |
Selectize.define('drag_drop', function(options) { |
| 3139 |
if (!$.fn.sortable) throw new Error('The "drag_drop" plugin requires jQuery UI "sortable".'); |
| 3140 |
if (this.settings.mode !== 'multi') return; |
| 3141 |
var self = this; |
| 3142 |
|
| 3143 |
self.lock = (function() { |
| 3144 |
var original = self.lock; |
| 3145 |
return function() { |
| 3146 |
var sortable = self.$control.data('sortable'); |
| 3147 |
if (sortable) sortable.disable(); |
| 3148 |
return original.apply(self, arguments); |
| 3149 |
}; |
| 3150 |
})(); |
| 3151 |
|
| 3152 |
self.unlock = (function() { |
| 3153 |
var original = self.unlock; |
| 3154 |
return function() { |
| 3155 |
var sortable = self.$control.data('sortable'); |
| 3156 |
if (sortable) sortable.enable(); |
| 3157 |
return original.apply(self, arguments); |
| 3158 |
}; |
| 3159 |
})(); |
| 3160 |
|
| 3161 |
self.setup = (function() { |
| 3162 |
var original = self.setup; |
| 3163 |
return function() { |
| 3164 |
original.apply(this, arguments); |
| 3165 |
|
| 3166 |
var $control = self.$control.sortable({ |
| 3167 |
items: '[data-value]', |
| 3168 |
forcePlaceholderSize: true, |
| 3169 |
disabled: self.isLocked, |
| 3170 |
start: function(e, ui) { |
| 3171 |
ui.placeholder.css('width', ui.helper.css('width')); |
| 3172 |
$control.addClass('dragging'); |
| 3173 |
}, |
| 3174 |
stop: function() { |
| 3175 |
$control.removeClass('dragging'); |
| 3176 |
var active = self.$activeItems ? self.$activeItems.slice() : null; |
| 3177 |
var values = []; |
| 3178 |
$control.children('[data-value]').each(function() { |
| 3179 |
values.push($(this).attr('data-value')); |
| 3180 |
}); |
| 3181 |
self.isFocused = false; |
| 3182 |
self.setValue(values); |
| 3183 |
self.isFocused = true; |
| 3184 |
self.setActiveItem(active); |
| 3185 |
self.positionDropdown(); |
| 3186 |
} |
| 3187 |
}); |
| 3188 |
}; |
| 3189 |
})(); |
| 3190 |
|
| 3191 |
}); |
| 3192 |
|
| 3193 |
Selectize.define('dropdown_header', function(options) { |
| 3194 |
var self = this; |
| 3195 |
|
| 3196 |
options = $.extend({ |
| 3197 |
title : 'Untitled', |
| 3198 |
headerClass : 'selectize-dropdown-header', |
| 3199 |
titleRowClass : 'selectize-dropdown-header-title', |
| 3200 |
labelClass : 'selectize-dropdown-header-label', |
| 3201 |
closeClass : 'selectize-dropdown-header-close', |
| 3202 |
|
| 3203 |
html: function(data) { |
| 3204 |
return ( |
| 3205 |
'<div class="' + data.headerClass + '">' + |
| 3206 |
'<div class="' + data.titleRowClass + '">' + |
| 3207 |
'<span class="' + data.labelClass + '">' + data.title + '</span>' + |
| 3208 |
'<a href="javascript:void(0)" class="' + data.closeClass + '">×</a>' + |
| 3209 |
'</div>' + |
| 3210 |
'</div>' |
| 3211 |
); |
| 3212 |
} |
| 3213 |
}, options); |
| 3214 |
|
| 3215 |
self.setup = (function() { |
| 3216 |
var original = self.setup; |
| 3217 |
return function() { |
| 3218 |
original.apply(self, arguments); |
| 3219 |
self.$dropdown_header = $(options.html(options)); |
| 3220 |
self.$dropdown.prepend(self.$dropdown_header); |
| 3221 |
self.$dropdown_header.find('.' + options.closeClass).on('click', function () { |
| 3222 |
self.close(); |
| 3223 |
}); |
| 3224 |
}; |
| 3225 |
})(); |
| 3226 |
|
| 3227 |
}); |
| 3228 |
|
| 3229 |
Selectize.define('optgroup_columns', function(options) { |
| 3230 |
var self = this; |
| 3231 |
|
| 3232 |
options = $.extend({ |
| 3233 |
equalizeWidth : true, |
| 3234 |
equalizeHeight : true |
| 3235 |
}, options); |
| 3236 |
|
| 3237 |
this.getAdjacentOption = function($option, direction) { |
| 3238 |
var $options = $option.closest('[data-group]').find('[data-selectable]'); |
| 3239 |
var index = $options.index($option) + direction; |
| 3240 |
|
| 3241 |
return index >= 0 && index < $options.length ? $options.eq(index) : $(); |
| 3242 |
}; |
| 3243 |
|
| 3244 |
this.onKeyDown = (function() { |
| 3245 |
var original = self.onKeyDown; |
| 3246 |
return function(e) { |
| 3247 |
var index, $option, $options, $optgroup; |
| 3248 |
|
| 3249 |
if (this.isOpen && (e.keyCode === KEY_LEFT || e.keyCode === KEY_RIGHT)) { |
| 3250 |
self.ignoreHover = true; |
| 3251 |
$optgroup = this.$activeOption.closest('[data-group]'); |
| 3252 |
index = $optgroup.find('[data-selectable]').index(this.$activeOption); |
| 3253 |
|
| 3254 |
if(e.keyCode === KEY_LEFT) { |
| 3255 |
$optgroup = $optgroup.prev('[data-group]'); |
| 3256 |
} else { |
| 3257 |
$optgroup = $optgroup.next('[data-group]'); |
| 3258 |
} |
| 3259 |
|
| 3260 |
$options = $optgroup.find('[data-selectable]'); |
| 3261 |
$option = $options.eq(Math.min($options.length - 1, index)); |
| 3262 |
if ($option.length) { |
| 3263 |
this.setActiveOption($option); |
| 3264 |
} |
| 3265 |
return; |
| 3266 |
} |
| 3267 |
|
| 3268 |
return original.apply(this, arguments); |
| 3269 |
}; |
| 3270 |
})(); |
| 3271 |
|
| 3272 |
var getScrollbarWidth = function() { |
| 3273 |
var div; |
| 3274 |
var width = getScrollbarWidth.width; |
| 3275 |
var doc = document; |
| 3276 |
|
| 3277 |
if (typeof width === 'undefined') { |
| 3278 |
div = doc.createElement('div'); |
| 3279 |
div.innerHTML = '<div style="width:50px;height:50px;position:absolute;left:-50px;top:-50px;overflow:auto;"><div style="width:1px;height:100px;"></div></div>'; |
| 3280 |
div = div.firstChild; |
| 3281 |
doc.body.appendChild(div); |
| 3282 |
width = getScrollbarWidth.width = div.offsetWidth - div.clientWidth; |
| 3283 |
doc.body.removeChild(div); |
| 3284 |
} |
| 3285 |
return width; |
| 3286 |
}; |
| 3287 |
|
| 3288 |
var equalizeSizes = function() { |
| 3289 |
var i, n, height_max, width, width_last, width_parent, $optgroups; |
| 3290 |
|
| 3291 |
$optgroups = $('[data-group]', self.$dropdown_content); |
| 3292 |
n = $optgroups.length; |
| 3293 |
if (!n || !self.$dropdown_content.width()) return; |
| 3294 |
|
| 3295 |
if (options.equalizeHeight) { |
| 3296 |
height_max = 0; |
| 3297 |
for (i = 0; i < n; i++) { |
| 3298 |
height_max = Math.max(height_max, $optgroups.eq(i).height()); |
| 3299 |
} |
| 3300 |
$optgroups.css({height: height_max}); |
| 3301 |
} |
| 3302 |
|
| 3303 |
if (options.equalizeWidth) { |
| 3304 |
width_parent = self.$dropdown_content.innerWidth() - getScrollbarWidth(); |
| 3305 |
width = Math.round(width_parent / n); |
| 3306 |
$optgroups.css({width: width}); |
| 3307 |
if (n > 1) { |
| 3308 |
width_last = width_parent - width * (n - 1); |
| 3309 |
$optgroups.eq(n - 1).css({width: width_last}); |
| 3310 |
} |
| 3311 |
} |
| 3312 |
}; |
| 3313 |
|
| 3314 |
if (options.equalizeHeight || options.equalizeWidth) { |
| 3315 |
hook.after(this, 'positionDropdown', equalizeSizes); |
| 3316 |
hook.after(this, 'refreshOptions', equalizeSizes); |
| 3317 |
} |
| 3318 |
|
| 3319 |
|
| 3320 |
}); |
| 3321 |
|
| 3322 |
Selectize.define('read-only', function(options){ |
| 3323 |
var self = this; |
| 3324 |
this.setup = (function() { |
| 3325 |
var original = self.setup; |
| 3326 |
return function() { |
| 3327 |
original.apply(this, arguments); |
| 3328 |
if(this.$dropdown.hasClass("read-only"))this.$control_input.attr('readonly', 'readonly'); |
| 3329 |
}; |
| 3330 |
})(); |
| 3331 |
this.readonly = (function() { |
| 3332 |
return function(state) { |
| 3333 |
if(state){ |
| 3334 |
this.$control_input.attr('readonly', 'readonly'); |
| 3335 |
this.$dropdown.addClass("read-only") |
| 3336 |
} |
| 3337 |
else{ |
| 3338 |
this.$control_input.removeAttr('readonly'); |
| 3339 |
this.$dropdown.removeClass("read-only") |
| 3340 |
} |
| 3341 |
}; |
| 3342 |
})(); |
| 3343 |
}); |
| 3344 |
|
| 3345 |
Selectize.define('remove_button', function (options) { |
| 3346 |
if (this.settings.mode === 'single') return; |
| 3347 |
|
| 3348 |
options = $.extend({ |
| 3349 |
label : '×', |
| 3350 |
title : 'Remove', |
| 3351 |
className : 'remove', |
| 3352 |
append : true |
| 3353 |
}, options); |
| 3354 |
|
| 3355 |
var multiClose = function(thisRef, options) { |
| 3356 |
|
| 3357 |
var self = thisRef; |
| 3358 |
var html = '<a href="javascript:void(0)" class="' + options.className + '" tabindex="-1" title="' + escape_html(options.title) + '">' + options.label + '</a>'; |
| 3359 |
|
| 3360 |
var append = function(html_container, html_element) { |
| 3361 |
var pos = html_container.search(/(<\/[^>]+>\s*)$/); |
| 3362 |
return html_container.substring(0, pos) + html_element + html_container.substring(pos); |
| 3363 |
}; |
| 3364 |
|
| 3365 |
thisRef.setup = (function() { |
| 3366 |
var original = self.setup; |
| 3367 |
return function() { |
| 3368 |
if (options.append) { |
| 3369 |
var render_item = self.settings.render.item; |
| 3370 |
self.settings.render.item = function(data) { |
| 3371 |
return append(render_item.apply(thisRef, arguments), html); |
| 3372 |
}; |
| 3373 |
} |
| 3374 |
|
| 3375 |
original.apply(thisRef, arguments); |
| 3376 |
|
| 3377 |
thisRef.$control.on('click', '.' + options.className, function(e) { |
| 3378 |
e.preventDefault(); |
| 3379 |
if (self.isLocked) return; |
| 3380 |
|
| 3381 |
var $item = $(e.currentTarget).parent(); |
| 3382 |
self.setActiveItem($item); |
| 3383 |
if (self.deleteSelection()) { |
| 3384 |
self.setCaret(self.items.length); |
| 3385 |
} |
| 3386 |
return false; |
| 3387 |
}); |
| 3388 |
|
| 3389 |
}; |
| 3390 |
})(); |
| 3391 |
}; |
| 3392 |
|
| 3393 |
multiClose(this, options); |
| 3394 |
}); |
| 3395 |
|
| 3396 |
Selectize.define('restore_on_backspace', function(options) { |
| 3397 |
var self = this; |
| 3398 |
|
| 3399 |
options.text = options.text || function(option) { |
| 3400 |
return option[this.settings.labelField]; |
| 3401 |
}; |
| 3402 |
|
| 3403 |
this.onKeyDown = (function() { |
| 3404 |
var original = self.onKeyDown; |
| 3405 |
return function(e) { |
| 3406 |
var index, option; |
| 3407 |
if (e.keyCode === KEY_BACKSPACE && this.$control_input.val() === '' && !this.$activeItems.length) { |
| 3408 |
index = this.caretPos - 1; |
| 3409 |
if (index >= 0 && index < this.items.length) { |
| 3410 |
option = this.options[this.items[index]]; |
| 3411 |
if (this.deleteSelection(e)) { |
| 3412 |
this.setTextboxValue(options.text.apply(this, [option])); |
| 3413 |
this.refreshOptions(true); |
| 3414 |
} |
| 3415 |
e.preventDefault(); |
| 3416 |
return; |
| 3417 |
} |
| 3418 |
} |
| 3419 |
return original.apply(this, arguments); |
| 3420 |
}; |
| 3421 |
})(); |
| 3422 |
}); |
| 3423 |
|
| 3424 |
Selectize.define('select_on_focus', function(options) { |
| 3425 |
var self = this; |
| 3426 |
|
| 3427 |
self.on('focus', function() { |
| 3428 |
var originalFocus = self.onFocus; |
| 3429 |
return function(e) { |
| 3430 |
var value = self.getItem(self.getValue()).text(); |
| 3431 |
self.clear(); |
| 3432 |
self.setTextboxValue(value); |
| 3433 |
self.$control_input.select(); |
| 3434 |
setTimeout( function () { |
| 3435 |
if (self.settings.selectOnTab) { |
| 3436 |
self.setActiveOption(self.getFirstItemMatchedByTextContent(value)); |
| 3437 |
} |
| 3438 |
self.settings.score = null; |
| 3439 |
},0); |
| 3440 |
return originalFocus.apply(this, arguments); |
| 3441 |
}; |
| 3442 |
}()); |
| 3443 |
|
| 3444 |
self.onBlur = (function() { |
| 3445 |
var originalBlur = self.onBlur; |
| 3446 |
return function(e) { |
| 3447 |
if (self.getValue() === "" && self.lastValidValue !== self.getValue()) { |
| 3448 |
self.setValue(self.lastValidValue); |
| 3449 |
} |
| 3450 |
setTimeout( function () { |
| 3451 |
self.settings.score = function() { |
| 3452 |
return function() { |
| 3453 |
return 1; |
| 3454 |
}; |
| 3455 |
}; |
| 3456 |
}, 0 ); |
| 3457 |
return originalBlur.apply(this, arguments); |
| 3458 |
} |
| 3459 |
}()); |
| 3460 |
self.settings.score = function() { |
| 3461 |
return function() { return 1; }; |
| 3462 |
}; |
| 3463 |
|
| 3464 |
}); |
| 3465 |
|
| 3466 |
Selectize.define('tag_limit', function (options) { |
| 3467 |
const self = this |
| 3468 |
options.tagLimit = options.tagLimit |
| 3469 |
this.onBlur = (function (e) { |
| 3470 |
const original = self.onBlur |
| 3471 |
|
| 3472 |
return function (e) { |
| 3473 |
original.apply(this, e); |
| 3474 |
if (!e) |
| 3475 |
return |
| 3476 |
const $control = this.$control |
| 3477 |
const $items = $control.find('.item') |
| 3478 |
const limit = options.tagLimit |
| 3479 |
if (limit === undefined || $items.length <= limit) |
| 3480 |
return |
| 3481 |
|
| 3482 |
$items.toArray().forEach(function(item, index) { |
| 3483 |
if (index < limit) |
| 3484 |
return |
| 3485 |
$(item).hide() |
| 3486 |
}); |
| 3487 |
|
| 3488 |
$control.append('<span><b>' + ($items.length - limit) + '</b></span>') |
| 3489 |
}; |
| 3490 |
})() |
| 3491 |
|
| 3492 |
this.onFocus = (function (e) { |
| 3493 |
const original = self.onFocus |
| 3494 |
|
| 3495 |
return function (e) { |
| 3496 |
original.apply(this, e); |
| 3497 |
if (!e) |
| 3498 |
return |
| 3499 |
const $control = this.$control |
| 3500 |
const $items = $control.find('.item') |
| 3501 |
$items.show() |
| 3502 |
$control.find('span').remove() |
| 3503 |
|
| 3504 |
}; |
| 3505 |
})() |
| 3506 |
}); |
| 3507 |
|
| 3508 |
return Selectize; |
| 3509 |
})); |
| 3510 |
|