123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436 |
- define([
- 'jquery',
- './results',
- './selection/single',
- './selection/multiple',
- './selection/placeholder',
- './selection/allowClear',
- './selection/search',
- './selection/selectionCss',
- './selection/eventRelay',
- './utils',
- './translation',
- './diacritics',
- './data/select',
- './data/array',
- './data/ajax',
- './data/tags',
- './data/tokenizer',
- './data/minimumInputLength',
- './data/maximumInputLength',
- './data/maximumSelectionLength',
- './dropdown',
- './dropdown/search',
- './dropdown/hidePlaceholder',
- './dropdown/infiniteScroll',
- './dropdown/attachBody',
- './dropdown/minimumResultsForSearch',
- './dropdown/selectOnClose',
- './dropdown/closeOnSelect',
- './dropdown/dropdownCss',
- './i18n/en'
- ], function ($,
- ResultsList,
- SingleSelection, MultipleSelection, Placeholder, AllowClear,
- SelectionSearch, SelectionCSS, EventRelay,
- Utils, Translation, DIACRITICS,
- SelectData, ArrayData, AjaxData, Tags, Tokenizer,
- MinimumInputLength, MaximumInputLength, MaximumSelectionLength,
- Dropdown, DropdownSearch, HidePlaceholder, InfiniteScroll,
- AttachBody, MinimumResultsForSearch, SelectOnClose, CloseOnSelect,
- DropdownCSS,
- EnglishTranslation) {
- function Defaults () {
- this.reset();
- }
- Defaults.prototype.apply = function (options) {
- options = $.extend(true, {}, this.defaults, options);
- if (options.dataAdapter == null) {
- if (options.ajax != null) {
- options.dataAdapter = AjaxData;
- } else if (options.data != null) {
- options.dataAdapter = ArrayData;
- } else {
- options.dataAdapter = SelectData;
- }
- if (options.minimumInputLength > 0) {
- options.dataAdapter = Utils.Decorate(
- options.dataAdapter,
- MinimumInputLength
- );
- }
- if (options.maximumInputLength > 0) {
- options.dataAdapter = Utils.Decorate(
- options.dataAdapter,
- MaximumInputLength
- );
- }
- if (options.maximumSelectionLength > 0) {
- options.dataAdapter = Utils.Decorate(
- options.dataAdapter,
- MaximumSelectionLength
- );
- }
- if (options.tags) {
- options.dataAdapter = Utils.Decorate(options.dataAdapter, Tags);
- }
- if (options.tokenSeparators != null || options.tokenizer != null) {
- options.dataAdapter = Utils.Decorate(
- options.dataAdapter,
- Tokenizer
- );
- }
- }
- if (options.resultsAdapter == null) {
- options.resultsAdapter = ResultsList;
- if (options.ajax != null) {
- options.resultsAdapter = Utils.Decorate(
- options.resultsAdapter,
- InfiniteScroll
- );
- }
- if (options.placeholder != null) {
- options.resultsAdapter = Utils.Decorate(
- options.resultsAdapter,
- HidePlaceholder
- );
- }
- if (options.selectOnClose) {
- options.resultsAdapter = Utils.Decorate(
- options.resultsAdapter,
- SelectOnClose
- );
- }
- }
- if (options.dropdownAdapter == null) {
- if (options.multiple) {
- options.dropdownAdapter = Dropdown;
- } else {
- var SearchableDropdown = Utils.Decorate(Dropdown, DropdownSearch);
- options.dropdownAdapter = SearchableDropdown;
- }
- if (options.minimumResultsForSearch !== 0) {
- options.dropdownAdapter = Utils.Decorate(
- options.dropdownAdapter,
- MinimumResultsForSearch
- );
- }
- if (options.closeOnSelect) {
- options.dropdownAdapter = Utils.Decorate(
- options.dropdownAdapter,
- CloseOnSelect
- );
- }
- if (
- options.dropdownCssClass != null ||
- options.dropdownCss != null ||
- options.adaptDropdownCssClass != null
- ) {
- options.dropdownAdapter = Utils.Decorate(
- options.dropdownAdapter,
- DropdownCSS
- );
- }
- options.dropdownAdapter = Utils.Decorate(
- options.dropdownAdapter,
- AttachBody
- );
- }
- if (options.selectionAdapter == null) {
- if (options.multiple) {
- options.selectionAdapter = MultipleSelection;
- } else {
- options.selectionAdapter = SingleSelection;
- }
- // Add the placeholder mixin if a placeholder was specified
- if (options.placeholder != null) {
- options.selectionAdapter = Utils.Decorate(
- options.selectionAdapter,
- Placeholder
- );
- }
- if (options.allowClear) {
- options.selectionAdapter = Utils.Decorate(
- options.selectionAdapter,
- AllowClear
- );
- }
- if (options.multiple) {
- options.selectionAdapter = Utils.Decorate(
- options.selectionAdapter,
- SelectionSearch
- );
- }
- if (
- options.containerCssClass != null ||
- options.containerCss != null ||
- options.adaptContainerCssClass != null
- ) {
- options.selectionAdapter = Utils.Decorate(
- options.selectionAdapter,
- SelectionCSS
- );
- }
- options.selectionAdapter = Utils.Decorate(
- options.selectionAdapter,
- EventRelay
- );
- }
- // If the defaults were not previously applied from an element, it is
- // possible for the language option to have not been resolved
- options.language = this._resolveLanguage(options.language);
- // Always fall back to English since it will always be complete
- options.language.push('en');
- var uniqueLanguages = [];
- for (var l = 0; l < options.language.length; l++) {
- var language = options.language[l];
- if (uniqueLanguages.indexOf(language) === -1) {
- uniqueLanguages.push(language);
- }
- }
- options.language = uniqueLanguages;
- options.translations = this._processTranslations(
- options.language,
- options.debug
- );
- return options;
- };
- Defaults.prototype.reset = function () {
- function stripDiacritics (text) {
- // Used 'uni range + named function' from http://jsperf.com/diacritics/18
- function match(a) {
- return DIACRITICS[a] || a;
- }
- return text.replace(/[^\u0000-\u007E]/g, match);
- }
- function matcher (params, data) {
- // Always return the object if there is nothing to compare
- if (params.term == null || params.term.trim() === '') {
- return data;
- }
- // Do a recursive check for options with children
- if (data.children && data.children.length > 0) {
- // Clone the data object if there are children
- // This is required as we modify the object to remove any non-matches
- var match = $.extend(true, {}, data);
- // Check each child of the option
- for (var c = data.children.length - 1; c >= 0; c--) {
- var child = data.children[c];
- var matches = matcher(params, child);
- // If there wasn't a match, remove the object in the array
- if (matches == null) {
- match.children.splice(c, 1);
- }
- }
- // If any children matched, return the new object
- if (match.children.length > 0) {
- return match;
- }
- // If there were no matching children, check just the plain object
- return matcher(params, match);
- }
- var original = stripDiacritics(data.text).toUpperCase();
- var term = stripDiacritics(params.term).toUpperCase();
- // Check if the text contains the term
- if (original.indexOf(term) > -1) {
- return data;
- }
- // If it doesn't contain the term, don't return anything
- return null;
- }
- this.defaults = {
- amdLanguageBase: './i18n/',
- closeOnSelect: true,
- debug: false,
- dropdownAutoWidth: false,
- escapeMarkup: Utils.escapeMarkup,
- language: {},
- matcher: matcher,
- minimumInputLength: 0,
- maximumInputLength: 0,
- maximumSelectionLength: 0,
- minimumResultsForSearch: 0,
- selectOnClose: false,
- scrollAfterSelect: false,
- sorter: function (data) {
- return data;
- },
- templateResult: function (result) {
- return result.text;
- },
- templateSelection: function (selection) {
- return selection.text;
- },
- theme: 'default',
- width: 'resolve'
- };
- };
- Defaults.prototype.applyFromElement = function (options, $element) {
- var optionLanguage = options.language;
- var defaultLanguage = this.defaults.language;
- var elementLanguage = $element.prop('lang');
- var parentLanguage = $element.closest('[lang]').prop('lang');
- var languages = Array.prototype.concat.call(
- this._resolveLanguage(elementLanguage),
- this._resolveLanguage(optionLanguage),
- this._resolveLanguage(defaultLanguage),
- this._resolveLanguage(parentLanguage)
- );
- options.language = languages;
- return options;
- };
- Defaults.prototype._resolveLanguage = function (language) {
- if (!language) {
- return [];
- }
- if ($.isEmptyObject(language)) {
- return [];
- }
- if ($.isPlainObject(language)) {
- return [language];
- }
- var languages;
- if (!Array.isArray(language)) {
- languages = [language];
- } else {
- languages = language;
- }
- var resolvedLanguages = [];
- for (var l = 0; l < languages.length; l++) {
- resolvedLanguages.push(languages[l]);
- if (typeof languages[l] === 'string' && languages[l].indexOf('-') > 0) {
- // Extract the region information if it is included
- var languageParts = languages[l].split('-');
- var baseLanguage = languageParts[0];
- resolvedLanguages.push(baseLanguage);
- }
- }
- return resolvedLanguages;
- };
- Defaults.prototype._processTranslations = function (languages, debug) {
- var translations = new Translation();
- for (var l = 0; l < languages.length; l++) {
- var languageData = new Translation();
- var language = languages[l];
- if (typeof language === 'string') {
- try {
- // Try to load it with the original name
- languageData = Translation.loadPath(language);
- } catch (e) {
- try {
- // If we couldn't load it, check if it wasn't the full path
- language = this.defaults.amdLanguageBase + language;
- languageData = Translation.loadPath(language);
- } catch (ex) {
- // The translation could not be loaded at all. Sometimes this is
- // because of a configuration problem, other times this can be
- // because of how Select2 helps load all possible translation files
- if (debug && window.console && console.warn) {
- console.warn(
- 'Select2: The language file for "' + language + '" could ' +
- 'not be automatically loaded. A fallback will be used instead.'
- );
- }
- }
- }
- } else if ($.isPlainObject(language)) {
- languageData = new Translation(language);
- } else {
- languageData = language;
- }
- translations.extend(languageData);
- }
- return translations;
- };
- Defaults.prototype.set = function (key, value) {
- var camelKey = $.camelCase(key);
- var data = {};
- data[camelKey] = value;
- var convertedData = Utils._convertData(data);
- $.extend(true, this.defaults, convertedData);
- };
- var defaults = new Defaults();
- return defaults;
- });
|