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

86 lines
2.0 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) => b.isIncluded - a.isIncluded);
});
}
item.active = !item.active;
}
}
Treeview.$inject = ['$element', '$scope'];
ngModule.component('vnTreeview', {
template: require('./index.html'),
controller: Treeview,
bindings: {
model: '<'
}
});