2018-11-12 10:31:58 +00:00
|
|
|
import ngModule from '../../module';
|
|
|
|
import Component from '../../lib/component';
|
|
|
|
import './style.scss';
|
|
|
|
|
2019-10-02 07:54:52 +00:00
|
|
|
import './childs';
|
|
|
|
import './content';
|
|
|
|
|
2018-11-12 10:31:58 +00:00
|
|
|
/**
|
2019-04-29 09:49:43 +00:00
|
|
|
* Treeview
|
2018-11-12 10:31:58 +00:00
|
|
|
*/
|
|
|
|
export default class Treeview extends Component {
|
2019-10-01 13:09:55 +00:00
|
|
|
constructor($element, $scope, $transclude) {
|
2018-11-12 10:31:58 +00:00
|
|
|
super($element, $scope);
|
2019-10-01 13:09:55 +00:00
|
|
|
this.$transclude = $transclude;
|
2019-10-08 05:22:38 +00:00
|
|
|
this.readOnly = true;
|
2018-11-12 10:31:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
get data() {
|
|
|
|
return this._data;
|
2018-11-12 10:31:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
set data(value) {
|
|
|
|
this._data = value;
|
|
|
|
|
|
|
|
const sons = value.length;
|
|
|
|
const rootElement = [{
|
|
|
|
childs: value,
|
|
|
|
active: true,
|
|
|
|
sons: sons
|
|
|
|
}];
|
|
|
|
|
|
|
|
this.setParent(rootElement[0], value);
|
|
|
|
|
|
|
|
this.items = rootElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
fetch() {
|
|
|
|
return this.fetchFunc().then(res =>
|
|
|
|
this.data = res
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
setParent(parent, childs) {
|
|
|
|
childs.forEach(child => {
|
|
|
|
child.parent = parent;
|
|
|
|
|
|
|
|
if (child.childs)
|
|
|
|
this.setParent(parent, child.childs);
|
2019-01-21 10:45:53 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-10-04 05:41:24 +00:00
|
|
|
onToggle(item) {
|
|
|
|
if (item.active)
|
|
|
|
this.fold(item);
|
|
|
|
else
|
|
|
|
this.unfold(item);
|
2019-01-21 10:45:53 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 05:41:24 +00:00
|
|
|
fold(item) {
|
|
|
|
item.childs = undefined;
|
|
|
|
item.active = false;
|
2019-03-12 14:04:09 +00:00
|
|
|
}
|
|
|
|
|
2019-10-04 05:41:24 +00:00
|
|
|
unfold(item) {
|
2019-10-08 05:22:38 +00:00
|
|
|
return this.fetchFunc({$item: item}).then(newData => {
|
|
|
|
this.setParent(item, newData);
|
2019-10-04 05:41:24 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
const childs = item.childs;
|
|
|
|
if (childs) {
|
2019-10-04 05:41:24 +00:00
|
|
|
childs.forEach(child => {
|
|
|
|
let index = newData.findIndex(newChild => {
|
|
|
|
return newChild.id == child.id;
|
2019-02-18 07:37:26 +00:00
|
|
|
});
|
2019-10-04 05:41:24 +00:00
|
|
|
newData[index] = child;
|
|
|
|
});
|
|
|
|
}
|
2019-02-18 07:37:26 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
if (this.sortFunc) {
|
|
|
|
item.childs = newData.sort((a, b) =>
|
|
|
|
this.sortFunc({$a: a, $b: b})
|
|
|
|
);
|
|
|
|
}
|
2019-10-04 05:41:24 +00:00
|
|
|
}).then(() => item.active = true);
|
2018-11-12 10:31:58 +00:00
|
|
|
}
|
2019-04-23 06:29:23 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
onRemove(item) {
|
|
|
|
if (this.removeFunc)
|
|
|
|
this.removeFunc({$item: item});
|
2019-04-23 06:29:23 +00:00
|
|
|
}
|
2019-10-02 12:12:17 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
remove(item) {
|
|
|
|
const parent = item.parent;
|
|
|
|
let childs = parent.childs;
|
2019-10-02 12:12:17 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
if (!childs) childs = [];
|
2019-10-02 12:12:17 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
let index = childs.indexOf(item);
|
|
|
|
childs.splice(index, 1);
|
|
|
|
if (parent) parent.sons--;
|
2019-10-02 12:12:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
onCreate(parent) {
|
|
|
|
if (this.createFunc)
|
|
|
|
this.createFunc({$parent: parent});
|
2019-10-02 12:12:17 +00:00
|
|
|
}
|
2019-10-04 05:41:24 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
create(item) {
|
|
|
|
const parent = item.parent;
|
|
|
|
let childs = parent.childs;
|
|
|
|
if (!childs) childs = [];
|
2019-10-04 05:41:24 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
childs.push(item);
|
2019-10-04 05:41:24 +00:00
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
if (this.sortFunc) {
|
|
|
|
childs = childs.sort((a, b) =>
|
|
|
|
this.sortFunc({$a: a, $b: b})
|
|
|
|
);
|
2019-10-04 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2019-10-08 05:22:38 +00:00
|
|
|
if (parent) parent.sons++;
|
|
|
|
}
|
|
|
|
|
|
|
|
onDrop(item, dragged, dropped) {
|
|
|
|
this.emit('drop', {item, dragged, dropped});
|
|
|
|
}
|
2018-11-12 10:31:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-01 13:09:55 +00:00
|
|
|
Treeview.$inject = ['$element', '$scope', '$transclude'];
|
2018-11-12 10:31:58 +00:00
|
|
|
|
|
|
|
ngModule.component('vnTreeview', {
|
|
|
|
template: require('./index.html'),
|
|
|
|
controller: Treeview,
|
|
|
|
bindings: {
|
2019-10-08 05:22:38 +00:00
|
|
|
rootLabel: '@',
|
|
|
|
data: '<?',
|
2019-04-29 09:49:43 +00:00
|
|
|
draggable: '<?',
|
|
|
|
droppable: '<?',
|
2019-10-08 05:22:38 +00:00
|
|
|
fetchFunc: '&',
|
2019-10-02 12:12:17 +00:00
|
|
|
removeFunc: '&?',
|
2019-10-08 05:22:38 +00:00
|
|
|
createFunc: '&?',
|
|
|
|
sortFunc: '&?',
|
|
|
|
readOnly: '<?'
|
2019-10-01 13:09:55 +00:00
|
|
|
},
|
2019-10-02 07:54:52 +00:00
|
|
|
transclude: true
|
2018-11-12 10:31:58 +00:00
|
|
|
});
|