Created
July 12, 2019 07:43
-
-
Save SPavelV/6d7927f0ea3d8038f4c6799b059e55ac to your computer and use it in GitHub Desktop.
Create instance SearchInputNative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| export default class SearchInputNative { | |
| constructor({ | |
| inputsElementsArr, | |
| valueAll = 'all', //значение value для отображения всех варинтов | |
| callback = ()=>console.log('---callback is not define:'), | |
| filterUniqueItems,// убираем дубликаты аптек с разными статусами при отображении всех элементов | |
| data | |
| }) { | |
| this.inputsElementsArr = inputsElementsArr; | |
| this.valueAll = valueAll; | |
| this.callback = callback; | |
| this.filterUniqueItems = filterUniqueItems; | |
| this.data = data; | |
| } | |
| addHandlers(data, callback) { | |
| this.inputsElementsArr.forEach( (element, i) => { | |
| if(element.nodeName === 'SELECT') { | |
| element.addEventListener('change', (evt) => this.handlerInput(evt,data,callback)); | |
| } else { | |
| element.addEventListener('input', (evt) => this.handlerInput(evt,data,callback)); | |
| } | |
| }) | |
| } | |
| search(data, callback, self, inputValue, key) { | |
| //поиск в data | |
| const result = []; | |
| for(let i = 0, len = data.length; i < len; i++) { | |
| // console.log('---element',i,callback(i,data[i])); | |
| if(callback(i,data[i],self,inputValue,key) === true) { | |
| result.push(data[i]); | |
| } | |
| } | |
| this.dataAfterSearch = result; | |
| // console.log('---result:',result); | |
| return result; | |
| } | |
| compareInput(index, dataItem, self, inputValue,key){ | |
| //сравнение введенного значения со свойством | |
| const needString = inputValue; | |
| let isSearch = false; | |
| for(let i = 0, len = key.length; i < len; i++) { | |
| //если поиск по нескольким свойствам проходим по каждому | |
| const pathToObjectProp = key[i].split('.'); | |
| //достаем значение первого ключа | |
| let propEmployee = dataItem[pathToObjectProp[0]]; | |
| //если свойсвто вложенно prop1.prop2 | |
| if(pathToObjectProp.length > 1) { | |
| propEmployee = self.getProp(dataItem,key[i]); | |
| } | |
| if(Array.isArray(propEmployee)) { | |
| //если свойстве лежит массив объектов, возвращаем массив занчений | |
| for(let i = 0, len = propEmployee.length; i < len; i++) { | |
| if(propEmployee[i].match(new RegExp(needString, 'i'))!== null) return true; | |
| } | |
| } | |
| isSearch = String(propEmployee).match(new RegExp(needString, 'i'))!== null; | |
| if(isSearch) return isSearch; | |
| } | |
| } | |
| getProp(object, propPath) { | |
| //достаем значение вложенного свойства | |
| let propArr = propPath.split('.'); | |
| for (let i = 0, n = propArr.length; i < n; ++i) { | |
| let prop = propArr[i]; | |
| if (prop in object) { | |
| object = object[prop]; | |
| //если в свойстве массив, возвращаем массив значений | |
| if (Array.isArray(object)) { | |
| const arr = object.map((element,i) => { | |
| let value; | |
| for(let key in element) { | |
| value = element[key]; | |
| } | |
| return value; | |
| }); | |
| return arr; | |
| } | |
| } else { | |
| return; | |
| } | |
| } | |
| return object; | |
| } | |
| handlerInput(evt,data,callback) { | |
| const self = this; | |
| let resultSearch = [...data]; | |
| this.inputsElementsArr.forEach((element,i)=> { | |
| if(element.nodeName === 'SELECT' && element.value === this.valueAll) { | |
| if(this.filterUniqueItems) resultSearch = this.filterUniqueItems(resultSearch); | |
| } | |
| else if(element.value.length > 0){ | |
| resultSearch = self.search(resultSearch, self.compareInput, self, element.value, element.dataset.searchKeys.split(',')); | |
| } | |
| }); | |
| callback(resultSearch); | |
| } | |
| init() { | |
| this.addHandlers(this.data, this.callback); | |
| } | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment