salix/front/core/components/drop-down/drop-down.js

487 lines
12 KiB
JavaScript
Raw Normal View History

2018-10-18 07:24:20 +00:00
import './style.scss';
2018-02-10 15:18:01 +00:00
import ngModule from '../../module';
2018-03-09 13:15:30 +00:00
import Component from '../../lib/component';
2018-10-18 07:24:20 +00:00
import ArrayModel from '../array-model/array-model';
import CrudModel from '../crud-model/crud-model';
2018-12-27 11:54:16 +00:00
import {mergeWhere} from 'vn-loopback/util/filter';
2018-10-18 07:24:20 +00:00
/**
* @event select Thrown when model item is selected
* @event change Thrown when model data is ready
*/
2018-03-09 13:15:30 +00:00
export default class DropDown extends Component {
2018-10-18 07:24:20 +00:00
constructor($element, $scope, $transclude, $timeout, $translate, $http, $q, $filter) {
2018-03-09 13:15:30 +00:00
super($element, $scope);
this.$transclude = $transclude;
2017-09-14 11:40:55 +00:00
this.$timeout = $timeout;
this.$translate = $translate;
2018-10-18 07:24:20 +00:00
this.$http = $http;
this.$q = $q;
this.$filter = $filter;
2017-09-14 11:40:55 +00:00
2018-03-09 13:15:30 +00:00
this.valueField = 'id';
2018-10-18 18:48:21 +00:00
this.showField = 'name';
2018-03-09 13:15:30 +00:00
this._search = undefined;
2017-09-14 11:40:55 +00:00
this._activeOption = -1;
2018-03-09 13:15:30 +00:00
this.showLoadMore = true;
this.showFilter = true;
this.docKeyDownHandler = e => this.onDocKeyDown(e);
}
$postLink() {
super.$postLink();
2019-09-30 09:30:54 +00:00
this.$.list.addEventListener('scroll', e => this.onScroll(e));
}
2018-03-09 13:15:30 +00:00
get shown() {
2018-10-18 07:24:20 +00:00
return this.$.popover && this.$.popover.shown;
}
2018-03-09 13:15:30 +00:00
set shown(value) {
this.$.popover.shown = value;
}
2017-09-14 11:40:55 +00:00
get search() {
return this._search;
}
2017-09-14 11:40:55 +00:00
set search(value) {
2018-10-18 07:24:20 +00:00
let oldValue = this._search;
2018-03-09 13:15:30 +00:00
this._search = value;
2018-10-18 07:24:20 +00:00
if (!this.shown) return;
value = value == '' || value == null ? null : value;
oldValue = oldValue == '' || oldValue == null ? null : oldValue;
if (value === oldValue && this.modelData != null) return;
2018-03-09 13:15:30 +00:00
if (value != null)
this._activeOption = 0;
2018-03-09 13:15:30 +00:00
this.$timeout.cancel(this.searchTimeout);
2018-10-18 07:24:20 +00:00
if (this.model) {
this.model.clear();
if (!this.data) {
this.searchTimeout = this.$timeout(() => {
this.refreshModel();
this.searchTimeout = null;
}, 350);
} else
2018-10-18 07:24:20 +00:00
this.refreshModel();
}
2018-03-09 13:15:30 +00:00
this.buildList();
}
get statusText() {
2018-10-18 07:24:20 +00:00
let model = this.model;
let data = this.modelData;
2018-03-09 13:15:30 +00:00
2018-10-18 07:24:20 +00:00
if (!model)
return 'No data';
2018-03-09 13:15:30 +00:00
if (model.isLoading || this.searchTimeout)
2018-10-18 07:24:20 +00:00
return 'Loading...';
if (data == null)
return 'No data';
if (model.moreRows)
return 'Load More';
if (data.length === 0)
return 'No results found';
return null;
2017-09-14 11:40:55 +00:00
}
2017-09-14 11:40:55 +00:00
get activeOption() {
return this._activeOption;
}
2018-03-09 13:15:30 +00:00
/**
* Shows the drop-down. If a parent is specified it is shown in a visible
* relative position to it.
*
2018-10-18 07:24:20 +00:00
* @param {HTMLElement} parent The parent element to show drop down relative to
2018-03-09 13:15:30 +00:00
* @param {String} search The initial search term or %null
*/
2018-10-18 07:24:20 +00:00
show(parent, search) {
this._activeOption = -1;
2018-10-18 07:24:20 +00:00
this.$.popover.show(parent || this.parent);
2019-10-09 22:47:29 +00:00
this.search = search;
2019-01-21 14:21:24 +00:00
this.buildList();
}
2018-03-09 13:15:30 +00:00
/**
* Hides the drop-down.
*/
hide() {
this.$.popover.hide();
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
/**
* Activates an option and scrolls the drop-down to that option.
2018-03-09 13:15:30 +00:00
*
* @param {Number} option The option index
*/
moveToOption(option) {
this.activateOption(option);
2019-09-30 09:30:54 +00:00
let list = this.$.list;
2018-03-09 13:15:30 +00:00
let li = this.activeLi;
if (!li) return;
let liRect = li.getBoundingClientRect();
let listRect = list.getBoundingClientRect();
if (liRect.bottom > listRect.bottom)
list.scrollTop += liRect.bottom - listRect.bottom;
else if (liRect.top < listRect.top)
list.scrollTop -= listRect.top - liRect.top;
}
2018-03-09 13:15:30 +00:00
/**
* Activates an option.
2018-03-09 13:15:30 +00:00
*
* @param {Number} option The option index
*/
activateOption(option) {
this._activeOption = option;
if (this.activeLi)
this.activeLi.className = '';
2018-10-18 07:24:20 +00:00
let data = this.modelData;
2018-03-09 13:15:30 +00:00
if (option >= 0 && data && option < data.length) {
2019-09-30 09:30:54 +00:00
this.activeLi = this.$.ul.children[option];
2018-03-09 13:15:30 +00:00
this.activeLi.className = 'active';
}
}
2017-11-22 12:10:33 +00:00
2018-03-09 13:15:30 +00:00
/**
* Selects an option.
*
* @param {Number} option The option index
*/
selectOption(option) {
2018-10-18 07:24:20 +00:00
let data = this.modelData;
let item = option != -1 && data ? data[option] : null;
if (item) {
let value = item[this.valueField];
2017-11-22 12:10:33 +00:00
2018-03-09 13:15:30 +00:00
if (this.multiple) {
if (!Array.isArray(this.selection)) {
this.selection = [];
this.field = [];
}
this.selection.push(item);
this.field.push(value);
} else {
this.selection = item;
this.field = value;
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
this.emit('select', {item});
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
if (!this.multiple)
this.$.popover.hide();
2017-11-22 12:10:33 +00:00
}
onOpen() {
this.document.addEventListener('keydown', this.docKeyDownHandler);
2019-09-30 09:30:54 +00:00
this.$.list.scrollTop = 0;
this.$.input.focus();
this.emit('open');
2017-09-20 09:50:53 +00:00
}
2018-03-09 13:15:30 +00:00
onClose() {
this.document.removeEventListener('keydown', this.docKeyDownHandler);
this.emit('close');
2017-09-20 11:52:53 +00:00
}
2018-03-09 13:15:30 +00:00
onClearClick() {
this.search = null;
}
2018-03-09 13:15:30 +00:00
onScroll() {
2019-09-30 09:30:54 +00:00
let list = this.$.list;
2018-03-09 13:15:30 +00:00
let shouldLoad =
list.scrollTop + list.clientHeight >= (list.scrollHeight - 40)
2018-10-18 07:24:20 +00:00
&& !this.model.isLoading;
2018-03-09 13:15:30 +00:00
if (shouldLoad)
2018-10-18 07:24:20 +00:00
this.model.loadMore();
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
onLoadMoreClick(event) {
2019-01-21 14:21:24 +00:00
if (event.defaultPrevented) return;
2018-03-09 13:15:30 +00:00
event.preventDefault();
2018-10-18 07:24:20 +00:00
this.model.loadMore();
2018-03-09 13:15:30 +00:00
}
onContainerClick(event) {
if (event.defaultPrevented) return;
2019-09-30 09:30:54 +00:00
let index = getPosition(this.$.ul, event);
2019-01-21 14:21:24 +00:00
if (index != -1) {
event.preventDefault();
this.selectOption(index);
}
2018-03-09 13:15:30 +00:00
}
onDocKeyDown(event) {
if (event.defaultPrevented) return;
2018-10-18 07:24:20 +00:00
let data = this.modelData;
2018-03-09 13:15:30 +00:00
let option = this.activeOption;
let nOpts = data ? data.length - 1 : 0;
switch (event.keyCode) {
case 9: // Tab
this.selectOption(option);
return;
2018-03-09 13:15:30 +00:00
case 13: // Enter
this.selectOption(option);
break;
case 38: // Up
this.moveToOption(option <= 0 ? nOpts : option - 1);
break;
case 40: // Down
this.moveToOption(option >= nOpts ? 0 : option + 1);
break;
case 35: // End
this.moveToOption(nOpts);
break;
case 36: // Start
this.moveToOption(0);
break;
default:
return;
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
event.preventDefault();
this.$.$applyAsync();
}
buildList() {
2018-10-18 07:24:20 +00:00
if (!this.shown) return;
this.destroyList();
2018-03-09 13:15:30 +00:00
let hasTemplate = this.$transclude && this.$transclude.isSlotFilled('tplItem');
2018-03-09 13:15:30 +00:00
let fragment = this.document.createDocumentFragment();
2018-10-18 07:24:20 +00:00
let data = this.modelData;
2018-03-09 13:15:30 +00:00
if (data) {
for (let i = 0; i < data.length; i++) {
let option = data[i];
option.orgShowField = option[this.showField];
if (this.translateFields) {
option = Object.assign({}, option);
for (let field of this.translateFields)
option[field] = this.$translate.instant(option[field]);
}
2018-03-09 13:15:30 +00:00
let li = this.document.createElement('li');
2019-04-26 06:52:43 +00:00
li.setAttribute('name', option.orgShowField);
2018-03-09 13:15:30 +00:00
fragment.appendChild(li);
if (this.multiple) {
let check = this.document.createElement('input');
check.type = 'checkbox';
li.appendChild(check);
if (this.field && this.field.indexOf(option[this.valueField]) != -1)
2018-03-09 13:15:30 +00:00
check.checked = true;
}
if (hasTemplate) {
this.$transclude((clone, scope) => {
Object.assign(scope, option);
2018-03-09 13:15:30 +00:00
li.appendChild(clone[0]);
this.scopes[i] = scope;
}, null, 'tplItem');
} else {
let text = this.document.createTextNode(option[this.showField]);
2018-03-09 13:15:30 +00:00
li.appendChild(text);
}
}
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
2019-09-30 09:30:54 +00:00
this.$.ul.appendChild(fragment);
2018-03-09 13:15:30 +00:00
this.activateOption(this._activeOption);
2018-03-13 12:21:53 +00:00
this.$.$applyAsync(() => this.$.popover.relocate());
2017-11-22 12:10:33 +00:00
}
2018-03-09 13:15:30 +00:00
destroyList() {
2019-09-30 09:30:54 +00:00
this.$.ul.innerHTML = '';
if (this.scopes) {
2018-03-09 13:15:30 +00:00
for (let scope of this.scopes)
scope.$destroy();
}
this.scopes = [];
2017-09-14 11:40:55 +00:00
}
2018-03-09 13:15:30 +00:00
2018-10-18 07:24:20 +00:00
getFields() {
let fields = [];
fields.push(this.valueField);
fields.push(this.showField);
if (this.fields) {
2018-10-18 07:24:20 +00:00
for (let field of this.fields)
fields.push(field);
}
2018-10-18 07:24:20 +00:00
return fields;
}
2017-09-14 11:40:55 +00:00
$onDestroy() {
this.destroyList();
2017-09-14 11:40:55 +00:00
}
2018-10-18 07:24:20 +00:00
// Model related code
onDataChange() {
if (this.model.orgData)
this.emit('dataReady');
this.buildList();
}
get modelData() {
return this._model ? this._model.data : null;
}
get model() {
return this._model;
}
set model(value) {
this.linkEvents({_model: value}, {dataChange: this.onDataChange});
this.onDataChange();
}
get url() {
return this._url;
}
set url(value) {
this._url = value;
if (value) {
this.model = new CrudModel(this.$q, this.$http);
this.model.autoLoad = false;
this.model.url = value;
2018-10-22 06:23:10 +00:00
this.model.$onInit();
2018-10-18 07:24:20 +00:00
}
}
get data() {
return this._data;
}
set data(value) {
this._data = value;
if (value) {
this.model = new ArrayModel(this.$q, this.$filter);
this.model.autoLoad = false;
this.model.orgData = value;
2018-10-22 06:23:10 +00:00
this.model.$onInit();
2018-10-18 07:24:20 +00:00
}
}
refreshModel() {
let model = this.model;
let order;
if (this.order)
order = this.order;
else if (this.showField)
order = `${this.showField} ASC`;
let filter = {
order,
limit: this.limit || 8
};
if (model instanceof CrudModel) {
let searchExpr = this._search == null
? null
: this.searchFunction({$search: this._search});
Object.assign(filter, {
fields: this.getFields(),
include: this.include,
where: mergeWhere(this.where, searchExpr)
});
} else if (model instanceof ArrayModel) {
if (this._search != null)
filter.where = this.searchFunction({$search: this._search});
}
return this.model.applyFilter(filter);
}
searchFunction(scope) {
if (this.model instanceof CrudModel)
return {[this.showField]: {like: `%${scope.$search}%`}};
if (this.model instanceof ArrayModel)
return {[this.showField]: scope.$search};
}
}
2018-10-18 07:24:20 +00:00
DropDown.$inject = ['$element', '$scope', '$transclude', '$timeout', '$translate', '$http', '$q', '$filter'];
2018-03-09 13:15:30 +00:00
/**
* Gets the position of an event element relative to a parent.
*
* @param {HTMLElement} parent The parent element
* @param {Event} event The event object
* @return {Number} The element index
*/
function getPosition(parent, event) {
let target = event.target;
let children = parent.children;
if (target === parent)
return -1;
while (target.parentNode !== parent)
target = target.parentNode;
for (let i = 0; i < children.length; i++) {
2018-03-09 13:15:30 +00:00
if (children[i] === target)
return i;
}
2018-03-09 13:15:30 +00:00
return -1;
}
2018-02-10 15:18:01 +00:00
ngModule.component('vnDropDown', {
template: require('./drop-down.html'),
controller: DropDown,
bindings: {
2018-03-09 13:15:30 +00:00
field: '=?',
selection: '=?',
search: '<?',
showFilter: '<?',
2017-09-20 11:52:53 +00:00
parent: '<?',
multiple: '<?',
onSelect: '&?',
2018-10-18 07:24:20 +00:00
translateFields: '<?',
data: '<?',
url: '@?',
fields: '<?',
include: '<?',
where: '<?',
order: '@?',
limit: '<?',
searchFunction: '&?'
2017-09-20 09:50:53 +00:00
},
transclude: {
2018-03-09 13:15:30 +00:00
tplItem: '?tplItem'
2017-06-21 11:16:37 +00:00
}
});