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

155 lines
4.0 KiB
JavaScript
Raw Normal View History

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
*
* @property {String} position The relative position to the parent
*/
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-02 07:54:52 +00:00
this.items = [];
2018-11-12 10:31:58 +00:00
}
2019-01-21 10:45:53 +00:00
$onInit() {
this.refresh();
2018-11-12 10:31:58 +00:00
}
2019-01-21 10:45:53 +00:00
refresh() {
this.model.refresh().then(() => {
2019-10-02 07:54:52 +00:00
this.items = this.model.data;
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) {
return 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;
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-04 05:41:24 +00:00
item.childs = newData.sort((a, b) => {
if (b.selected !== a.selected) {
if (a.selected == null)
return 1;
if (b.selected == null)
return -1;
return b.selected - a.selected;
}
2019-02-20 11:31:08 +00:00
2019-10-04 05:41:24 +00:00
return a.name.localeCompare(b.name);
2019-01-21 10:45:53 +00:00
});
2019-10-04 05:41:24 +00:00
}).then(() => item.active = true);
2018-11-12 10:31:58 +00:00
}
onDrop(item, dragged, dropped) {
this.emit('drop', {item, dragged, dropped});
}
2019-10-02 12:12:17 +00:00
onRemove(item, items, parent) {
if (!this.removeFunc) return;
2019-10-04 05:41:24 +00:00
let res = this.removeFunc({$item: item, $parent: parent});
2019-10-02 12:12:17 +00:00
function remove() {
let index = items.indexOf(item);
items.splice(index, 1);
if (parent) parent.sons--;
}
if (res instanceof Object && res.then)
res.then(remove);
else if (res)
remove();
}
2019-10-04 05:41:24 +00:00
onCreate(parent, childs) {
if (!this.createFunc) return;
let res = this.createFunc({$parent: parent, $childs: childs});
2019-10-02 12:12:17 +00:00
2019-10-04 05:41:24 +00:00
function create() {
2019-10-02 12:12:17 +00:00
if (!childs) childs = [];
childs.push(res);
2019-10-04 05:41:24 +00:00
if (parent) parent.sons++;
2019-10-02 12:12:17 +00:00
}
2019-10-04 05:41:24 +00:00
if (res instanceof Object && res.then) {
if (parent && !parent.active)
this.unfold(parent).then(() => res.then(create));
else res.then(create);
} else if (res) {
if (parent && !parent.active)
this.unfold(parent).then(() => create());
else create();
} else if (parent && !parent.active)
this.unfold(parent);
2019-10-02 12:12:17 +00:00
}
2019-10-04 05:41:24 +00:00
/* onCreate(parent, childs) {
if (!this.createFunc) return;
let res = this.createFunc({$parent: parent});
function create() {
if (!childs) childs = [];
childs.push(res);
if (parent) parent.sons++;
}
if (res instanceof Object && res.then) {
if (!parent.active)
this.unfold(parent).then(() => res.then(create));
else res.then(create);
} else if (res) {
if (!parent.active)
this.unfold(parent).then(() => create());
else create();
}
} */
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-03-12 14:04:09 +00:00
model: '<',
icons: '<?',
disabled: '<?',
selectable: '<?',
editable: '<?',
2019-04-29 09:49:43 +00:00
draggable: '<?',
droppable: '<?',
2019-10-02 12:12:17 +00:00
aclRole: '@?',
removeFunc: '&?',
2019-10-04 05:41:24 +00:00
createFunc: '&?'
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
});