salix/front/core/components/treeview/index.js

93 lines
2.2 KiB
JavaScript

import ngModule from '../../module';
import Component from '../../lib/component';
import './style.scss';
/**
* A simple tooltip.
*
* @property {String} position The relative position to the parent
*/
export default class Treeview extends Component {
constructor($element, $scope) {
super($element, $scope);
this.data = [];
}
$onInit() {
this.refresh();
}
refresh() {
this.model.refresh().then(() => {
this.data = this.model.data;
});
}
/* hasCheckedChilds(node) {
if (!node.childs) return false;
const childs = node.childs;
for (let i = 0; i < childs.length; i++) {
if (childs[i].selected || this.hasCheckedChilds(childs[i]))
return true;
}
return false;
}
hasCheckedParents(node) {
if (!node.parent) return false;
const parent = node.parent;
if (parent.selected || this.hasCheckedParents(parent))
return true;
return false;
} */
onSelection(item, value) {
this.emit('selection', {item, value});
}
onToggle(item) {
if (item.active)
item.childs = undefined;
else {
this.model.applyFilter({}, {parentFk: item.id}).then(() => {
const newData = this.model.data;
if (item.childs) {
let childs = item.childs;
childs.forEach(child => {
let index = newData.findIndex(newChild => {
return newChild.id == child.id;
});
newData[index] = child;
});
}
item.childs = newData.sort((a, b) => {
let priority = (b.isIncluded - a.isIncluded) - 1;
if (b.name > a.name)
priority++;
return priority;
});
});
}
item.active = !item.active;
}
}
Treeview.$inject = ['$element', '$scope'];
ngModule.component('vnTreeview', {
template: require('./index.html'),
controller: Treeview,
bindings: {
model: '<'
}
});