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';
|
2017-06-13 11:08:06 +00:00
|
|
|
|
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;
|
2018-06-19 07:06:04 +00:00
|
|
|
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;
|
|
|
|
|
2018-11-20 15:36:37 +00:00
|
|
|
this.docKeyDownHandler = e => this.onDocKeyDown(e);
|
2018-03-12 11:31:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$postLink() {
|
2018-10-22 11:29:52 +00:00
|
|
|
super.$postLink();
|
2019-09-30 09:30:54 +00:00
|
|
|
this.$.list.addEventListener('scroll', e => this.onScroll(e));
|
2017-12-12 12:38:23 +00:00
|
|
|
}
|
2017-12-12 12:59:31 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
get shown() {
|
2018-10-18 07:24:20 +00:00
|
|
|
return this.$.popover && this.$.popover.shown;
|
2017-09-21 11:10:30 +00:00
|
|
|
}
|
2017-10-04 06:47:16 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
set shown(value) {
|
2018-03-12 11:31:50 +00:00
|
|
|
this.$.popover.shown = value;
|
2017-10-04 06:47:16 +00:00
|
|
|
}
|
|
|
|
|
2017-09-14 11:40:55 +00:00
|
|
|
get search() {
|
|
|
|
return this._search;
|
|
|
|
}
|
2017-10-04 06:47:16 +00:00
|
|
|
|
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
|
|
|
|
2018-03-17 12:17:48 +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();
|
2018-11-20 15:36:37 +00:00
|
|
|
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-10-04 06:47:16 +00:00
|
|
|
|
2017-09-14 11:40:55 +00:00
|
|
|
get activeOption() {
|
|
|
|
return this._activeOption;
|
|
|
|
}
|
2017-10-04 06:47:16 +00:00
|
|
|
|
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) {
|
2018-03-12 11:31:50 +00:00
|
|
|
this._activeOption = -1;
|
2018-03-17 12:17:48 +00:00
|
|
|
this.search = search;
|
2018-10-18 07:24:20 +00:00
|
|
|
this.$.popover.show(parent || this.parent);
|
2019-01-21 14:21:24 +00:00
|
|
|
this.buildList();
|
2017-06-15 05:45:01 +00:00
|
|
|
}
|
2017-06-29 11:56:52 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
/**
|
|
|
|
* Hides the drop-down.
|
|
|
|
*/
|
|
|
|
hide() {
|
2018-03-12 11:31:50 +00:00
|
|
|
this.$.popover.hide();
|
2017-11-22 12:10:33 +00:00
|
|
|
}
|
2017-12-12 12:59:31 +00:00
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
/**
|
2018-03-17 12:17:48 +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;
|
2017-12-12 12:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 13:15:30 +00:00
|
|
|
/**
|
2018-03-17 12:17:48 +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 = '';
|
2017-11-23 13:07:55 +00:00
|
|
|
|
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-23 13:07:55 +00:00
|
|
|
}
|
|
|
|
}
|
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;
|
2018-03-17 12:17:48 +00:00
|
|
|
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
|
|
|
|
2019-06-13 11:08:11 +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)
|
2018-03-12 11:31:50 +00:00
|
|
|
this.$.popover.hide();
|
2017-11-22 12:10:33 +00:00
|
|
|
}
|
|
|
|
|
2018-03-12 11:31:50 +00:00
|
|
|
onOpen() {
|
|
|
|
this.document.addEventListener('keydown', this.docKeyDownHandler);
|
2019-09-30 09:30:54 +00:00
|
|
|
this.$.list.scrollTop = 0;
|
|
|
|
this.$.input.focus();
|
2017-09-20 09:50:53 +00:00
|
|
|
}
|
2018-03-09 13:15:30 +00:00
|
|
|
|
2018-03-12 11:31:50 +00:00
|
|
|
onClose() {
|
|
|
|
this.document.removeEventListener('keydown', this.docKeyDownHandler);
|
2017-09-20 11:52:53 +00:00
|
|
|
}
|
2018-03-09 13:15:30 +00:00
|
|
|
|
|
|
|
onClearClick() {
|
|
|
|
this.search = null;
|
2017-10-03 07:16:02 +00:00
|
|
|
}
|
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) {
|
2018-03-17 12:17:48 +00:00
|
|
|
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;
|
2018-03-12 11:31:50 +00:00
|
|
|
this.destroyList();
|
2018-03-09 13:15:30 +00:00
|
|
|
|
2018-06-19 07:06:04 +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++) {
|
2018-06-19 07:06:04 +00:00
|
|
|
let option = data[i];
|
2019-04-26 10:41:30 +00:00
|
|
|
option.orgShowField = option[this.showField];
|
2018-06-19 07:06:04 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2019-04-26 10:41:30 +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);
|
|
|
|
|
2018-06-19 07:06:04 +00:00
|
|
|
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) => {
|
2018-06-19 07:06:04 +00:00
|
|
|
Object.assign(scope, option);
|
2018-03-09 13:15:30 +00:00
|
|
|
li.appendChild(clone[0]);
|
|
|
|
this.scopes[i] = scope;
|
|
|
|
}, null, 'tplItem');
|
|
|
|
} else {
|
2018-06-19 07:06:04 +00:00
|
|
|
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
|
|
|
|
2018-03-12 11:31:50 +00:00
|
|
|
destroyList() {
|
2019-09-30 09:30:54 +00:00
|
|
|
this.$.ul.innerHTML = '';
|
2018-03-12 11:31:50 +00:00
|
|
|
|
2018-10-31 10:58:10 +00:00
|
|
|
if (this.scopes) {
|
2018-03-09 13:15:30 +00:00
|
|
|
for (let scope of this.scopes)
|
|
|
|
scope.$destroy();
|
2018-10-31 10:58:10 +00:00
|
|
|
}
|
2018-03-12 11:31:50 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-10-31 10:58:10 +00:00
|
|
|
if (this.fields) {
|
2018-10-18 07:24:20 +00:00
|
|
|
for (let field of this.fields)
|
|
|
|
fields.push(field);
|
2018-10-31 10:58:10 +00:00
|
|
|
}
|
2018-10-18 07:24:20 +00:00
|
|
|
|
|
|
|
return fields;
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:40:55 +00:00
|
|
|
$onDestroy() {
|
2018-03-12 11:31:50 +00:00
|
|
|
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};
|
|
|
|
}
|
2017-06-15 05:45:01 +00:00
|
|
|
}
|
2018-10-18 07:24:20 +00:00
|
|
|
DropDown.$inject = ['$element', '$scope', '$transclude', '$timeout', '$translate', '$http', '$q', '$filter'];
|
2017-10-04 06:47:16 +00:00
|
|
|
|
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;
|
|
|
|
|
2018-10-31 10:58:10 +00:00
|
|
|
for (let i = 0; i < children.length; i++) {
|
2018-03-09 13:15:30 +00:00
|
|
|
if (children[i] === target)
|
|
|
|
return i;
|
2018-10-31 10:58:10 +00:00
|
|
|
}
|
2018-03-09 13:15:30 +00:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2017-06-13 11:08:06 +00:00
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.component('vnDropDown', {
|
2017-06-13 11:08:06 +00:00
|
|
|
template: require('./drop-down.html'),
|
2017-06-15 05:45:01 +00:00
|
|
|
controller: DropDown,
|
2017-06-13 11:08:06 +00:00
|
|
|
bindings: {
|
2018-03-09 13:15:30 +00:00
|
|
|
field: '=?',
|
|
|
|
selection: '=?',
|
|
|
|
search: '<?',
|
2018-06-19 07:06:04 +00:00
|
|
|
showFilter: '<?',
|
2017-09-20 11:52:53 +00:00
|
|
|
parent: '<?',
|
2018-06-19 07:06:04 +00:00
|
|
|
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
|
|
|
}
|
2017-06-13 11:08:06 +00:00
|
|
|
});
|