salix/client/core/src/icon-menu/icon-menu.js

133 lines
3.3 KiB
JavaScript
Raw Normal View History

import {module} from '../module';
import './style.scss';
export default class IconMenu {
constructor($element, $http, $timeout) {
this.$element = $element;
this.$http = $http;
this.$timeout = $timeout;
this._showDropDown = false;
2017-06-29 12:28:22 +00:00
this.finding = false;
this.findMore = false;
2017-09-14 11:40:55 +00:00
this.element = $element[0];
}
get showDropDown() {
return this._showDropDown;
}
set showDropDown(value) {
this._showDropDown = value;
}
findItems(search) {
if (!this.url)
return this.items ? this.items : [];
2017-06-29 12:28:22 +00:00
if (search && !this.finding) {
let filter = {where: {name: {regexp: search}}};
let json = JSON.stringify(filter);
2017-06-29 12:28:22 +00:00
this.finding = true;
this.$http.get(`${this.url}?filter=${json}`).then(
json => {
this.items = json.data;
2017-06-29 12:28:22 +00:00
this.finding = false;
},
() => {
this.finding = false;
}
);
2017-06-29 12:28:22 +00:00
} else if (!search && !this.finding) {
2017-09-14 11:40:55 +00:00
this.maxRow = 10;
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;
2017-11-23 13:09:38 +00:00
this.mouseFocus = false;
this.focused = false;
this.$element.bind('mouseover', e => {
this.$timeout(() => {
2017-11-23 13:09:38 +00:00
this.mouseFocus = true;
this.showDropDown = this.focused;
});
});
this.$element.bind('mouseout', () => {
this.$timeout(() => {
2017-11-23 13:09:38 +00:00
this.mouseFocus = false;
this.showDropDown = this.focused;
});
});
2017-09-14 11:40:55 +00:00
this.$element.bind('focusin', e => {
this.$timeout(() => {
2017-11-23 13:09:38 +00:00
this.focused = true;
2017-09-14 11:40:55 +00:00
this.showDropDown = true;
});
});
this.$element.bind('focusout', e => {
this.$timeout(() => {
2017-11-23 13:09:38 +00:00
this.focused = false;
this.showDropDown = this.mouseFocus;
2017-09-14 11:40:55 +00:00
});
});
}
2017-09-14 11:40:55 +00:00
$onDestroy() {
this.$element.unbind('mouseover');
this.$element.unbind('mouseout');
2017-09-14 11:40:55 +00:00
this.$element.unbind('focusin');
this.$element.unbind('focusout');
}
}
IconMenu.$inject = ['$element', '$http', '$timeout'];
module.component('vnIconMenu', {
template: require('./icon-menu.html'),
bindings: {
url: '@?',
items: '<?',
icon: '@',
maxRow: '@?',
selected: '='
},
controller: IconMenu
});