1class NASelect { 2 constructor(sub) { 3 this.DOMElement = sub.DOMElement 4 this.PlaceHolder = sub.custom.placeholder ? sub.custom.placeholder : (this.DOMElement.dataset.placeholder ? this.DOMElement.dataset.placeholder : polyglot.t('core.select_value')); 5 this.TemplateSelection = sub.custom.templateSelection ? sub.custom.templateSelection : (this.DOMElement.dataset.formatselection ? this.DOMElement.dataset.formatselection : null); 6 this.TemplateResult = sub.custom.templateResult ? sub.custom.templateResult : (this.DOMElement.dataset.formatresult ? this.DOMElement.dataset.formatresult : null); 7 this.Parent = sub.custom.parent ? sub.custom.parent : (this.DOMElement.parentElement ? this.DOMElement.parentElement : null); 8 this.Multiple = sub.custom.multiple || this.DOMElement.dataset.multiple == 'true' || this.DOMElement.multiple ? true : false; 9 this.Width = sub.custom.width ? sub.custom.width : (this.DOMElement.style.width ? this.DOMElement.style.width : 'resolve'); 10 this.Translations = sub.custom.translations ? sub.custom.translations : {}; 11 this.MaximumSelectionSize = sub.custom.maximumSelectionSize ? sub.custom.maximumSelectionSize : (this.DOMElement.dataset.maximumSelectionSize ? this.DOMElement.dataset.maximumSelectionSize : 9999); 12 this.FormatSelectionTooBig = sub.custom.formatSelectionTooBig ? sub.custom.formatSelectionTooBig : (this.DOMElement.dataset.formatSelectionTooBig ? this.DOMElement.dataset.formatSelectionTooBig : ''); 13 this.Ajax = null; 14 this.Code = null; 15 this.Limit = null; 16 this.AjaxURL = null; 17 this.LoadMorePadding = null; 18 this.ProcessID = null; 19 this.ProcessValue = null; 20 this.Separator = sub.custom.separator ? sub.custom.separator : (this.DOMElement.dataset.separator ? this.DOMElement.dataset.allowclear : ';'); 21 this.AllowClear = sub.custom.allowClear === false || this.DOMElement.dataset.allowclear === 'false' ? false : true; 22 this.SetCustomConfig(sub); 23 this.Build(); 24 } 25 26 SetCustomConfig(data) { 27 } 28 29 SetConfig() { 30 const self = this; 31 return { 32 language : this.GetTranslations(), 33 placeholder : this.PlaceHolder, 34 allowClear : this.AllowClear, 35 width : this.Width, 36 ajax : this.Ajax, 37 dropdownParent : this.Parent, 38 loadMorePadding : 200, 39 multiple : this.Multiple, 40 closeOnSelect : true, 41 separator : this.Separator, 42 maximumSelectionSize : this.MaximumSelectionSize, 43 formatSelectionTooBig : function (x) { return self.FormatSelectionTooBig; }, 44 formatSearching : function () { return 'Recherche...'; }, 45 templateSelection : function (x) { 46 if (self.TemplateSelection == undefined) { 47 let parser = new DOMParser(); 48 let document = parser.parseFromString(x.text, 'text/html'); 49 var htmlObject = document.createElement('div'); 50 htmlObject.innerHTML = document.body.innerHTML; 51 return htmlObject; 52 } 53 return window[self.TemplateSelection](x); 54 }, 55 templateResult : function (x) { 56 if (x.element != undefined && x.element.className != undefined && x.element.className != "") { 57 console.log(x); 58 let wrapper = document.createElement('span'); 59 wrapper.classList = x.element.className; 60 wrapper.innerHTML = x.text; 61 return wrapper; 62 } 63 if (self.TemplateResult != undefined) { 64 return window[self.TemplateResult](x); 65 } 66 let parser = new DOMParser(); 67 let doc = parser.parseFromString(x.text, 'text/html'); 68 var htmlObject = doc.createElement('div'); 69 htmlObject.innerHTML = doc.body.innerHTML;
70 return htmlObject; 71 }, 72 } 73 } 74 75 GetTranslations() { 76 const self = this; 77 return { 78 errorLoading : function () { return self.Translations.errorLoading ? self.Translations.errorLoading : polyglot.t("select2.errorLoading") }, 79 inputTooLong : function () { return self.Translations.inputTooLong ? self.Translations.inputTooLong : polyglot.t("select2.inputTooLong") }, 80 inputTooShort : function () { return self.Translations.inputTooShort ? self.Translations.inputTooShort : polyglot.t("select2.inputTooShort") }, 81 loadingMore : function () { return self.Translations.loadingMore ? self.Translations.loadingMore : polyglot.t("select2.loadingMore") }, 82 maximumSelected : function () { return self.Translations.maximumSelected ? self.Translations.maximumSelected : polyglot.t("select2.maximumSelected") }, 83 noResults : function () { return self.Translations.noResults ? self.Translations.noResults : polyglot.t("select2.noResults") }, 84 searching : function () { return self.Translations.searching ? self.Translations.searching : polyglot.t("select2.searching") } 85 }; 86 } 87 88 Build() { 89 if (typeof $(this.DOMElement).select2 !== 'function') 90 return; 91 $(this.DOMElement).select2(this.SetConfig()) 92 this.DOMElement.classList.add('autocomplete-initialized-new'); 93 } 94} 95 96class NASelectSimpleSearch extends NASelect { 97 constructor(data) { 98 super(data); 99 } 100} 101 102class NASelectAutocomplete extends NASelect { 103 constructor(data) { 104 super(data); 105 } 106 107 SetCustomConfig(data) { 108 this.Code = data.custom.code ? data.custom.code : (this.DOMElement.dataset.code ? this.DOMElement.dataset.code : null); 109 this.Limit = data.custom.limit ? data.custom.limit : (this.DOMElement.dataset.limit ? this.DOMElement.dataset.limit : 20); 110 this.AjaxURL = (data.custom.url_root ? data.custom.url_root : '') + (data.ajaxURL ? data.ajaxURL : (this.DOMElement.dataset.url ? this.DOMElement.dataset.url : "/global/module/admin/ressources/ajax/ajax_stdAutocomplete.php")); 111 this.ProcessID = data.custom.processID ? data.custom.processID : (this.DOMElement.dataset.processId ? this.DOMElement.dataset.processId : null); 112 this.ProcessValue = data.custom.processValue ? data.custom.processvalue : (this.DOMElement.dataset.processValue ? this.DOMElement.dataset.processValue : null); 113 this.Ajax = this.SetAjax(); 114 } 115 116 SetAjax() { 117 const self = this; 118 return { 119 delay : 500, 120 type : 'POST', 121 url : this.AjaxURL, 122 quietMillis : 750, 123 data : function (params) { 124 return { 125 query : params.term, 126 limit : self.Limit, 127 page : params.page, 128 criterion : self.Code 129 }; 130 }, 131 results : function (data, page) { 132 let more = data.fullLoaded ? false : (page * self.Limit) < data.total; 133 return {results: data.results, more: more}; 134 }, 135 processResults: (self.ProcessID && self.ProcessValue ? function (data) { 136 let data_; 137 138 try { 139 data_ = JSON.parse(data); 140 } catch (e) { 141 data_ = data; 142 } 143 144 let results = []; 145 data_.forEach(element => { 146 results.push({ 147 'id': element[self.ProcessID], 148 'text': element[self.ProcessValue] 149 }); 150 }); 151 return { 152 results: results 153 }; 154 } : function (data) { return data; }) 155 }; 156 } 157} 158 159const _NASelect2 = new class { 160 InitSimpleSearch(selector = document, classe = 'na-select2', data = {}) { 161 if (selector !== document && typeof selector === 'object' && selector[0]) { 162 selector = selector[0] 163 } 164 let selects = selector.getElementsByClassName(classe); 165 for (let index = 0; index < selects.length; index++) { 166 if (!selects[index].classList.contains('autocomplete-initialized-new')) { 167 new NASelectSimpleSearch({DOMElement: selects[index], custom: data}) 168 } 169 } 170 } 171 172 InitAutoComplete(selector = document, classe = 'na-select2-autocomplete', data = {}) { 173 if (selector !== document && typeof selector === 'object' && selector[0]) { 174 selector = selector[0] 175 } 176 let autocomplete = selector.getElementsByClassName(classe); 177 for (let index = 0; index < autocomplete.length; index++) { 178 if (!autocomplete[index].classList.contains('autocomplete-initialized-new')) { 179 new NASelectAutocomplete({DOMElement: autocomplete[index], custom: data}) 180 } 181 } 182 } 183} 184 185document.addEventListener('DOMContentLoaded', function () { 186 _NASelect2.InitSimpleSearch(); 187 _NASelect2.InitAutoComplete(); 188}, false);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.