diff --git a/client/core/src/autocomplete-v2/autocomplete.html b/client/core/src/autocomplete-v2/autocomplete.html new file mode 100644 index 000000000..f322a37ce --- /dev/null +++ b/client/core/src/autocomplete-v2/autocomplete.html @@ -0,0 +1,16 @@ + + + + + + + + \ No newline at end of file diff --git a/client/core/src/autocomplete-v2/autocomplete.js b/client/core/src/autocomplete-v2/autocomplete.js new file mode 100644 index 000000000..9f86e433d --- /dev/null +++ b/client/core/src/autocomplete-v2/autocomplete.js @@ -0,0 +1,136 @@ +import {module} from '../module'; +import './style.scss'; + +class Autocomplete { + constructor($element, $scope, $http, $timeout) { + this.$element = $element; + this.$scope = $scope; + this.$http = $http; + this.$timeout = $timeout; + + this._showDropDown = false; + this.finding = false; + this.findMore = false; + this._value = null; + this._field = null; + this.maxRow = 10; + this.showField = this.showField || 'name'; + this.items = this.data || null; + } + + get showDropDown() { + return this._showDropDown; + } + set showDropDown(value) { + this._showDropDown = value; + } + + get displayValue() { + return this._value; + } + + set displayValue(value) { + this._value = (value === undefined || value === '') ? null : value; + if (value === null) { + this.field = null; + } + } + + get field() { + return this._field; + } + set field(value) { + this._field = value; + if (value && value.hasOwnProperty(this.showField)) + this.displayValue = value[this.showField]; + } + + findItems(search) { + if (!this.url) + return this.items ? this.items : []; + + if (search && !this.finding) { + let filter = {where: {name: {regexp: search}}}; + let json = JSON.stringify(filter); + this.finding = true; + this.$http.get(`${this.url}?filter=${json}`).then( + json => { + this.items = json.data; + this.finding = false; + }, + () => { + this.finding = false; + } + ); + } else if (!search && !this.finding) { + this.items = []; + this.getItems(); + } + } + getItems() { + let filter = {}; + + if (this.maxRow) { + if (this.items) { + filter.skip = this.items.length; + } + filter.limit = this.maxRow; + filter.order = 'name ASC'; + } + + let json = JSON.stringify(filter); + + this.$http.get(`${this.url}?filter=${json}`).then( + json => { + if (json.data.length) + json.data.forEach( + el => { + this.items.push(el); + } + ); + else + this.maxRow = false; + } + ); + } + $onInit() { + if (!this.items && this.url) { + this.items = []; + this.getItems(); + } + + this.findMore = this.url && this.maxRow; + + this.$element.bind('mouseover', e => { + this.$timeout(() => { + this.showDropDown = true; + }); + }); + + this.$element.bind('mouseout', () => { + this.$timeout(() => { + this.showDropDown = false; + }); + }); + } + +} + +Autocomplete.$inject = ['$element', '$scope', '$http', '$timeout']; + +module.component('vnAutocomplete', { + template: require('./autocomplete.html'), + controller: Autocomplete, + bindings: { + url: '@?', + showField: '@?', + valueField: '@?', + selectFields: '@?', + initialData: '