treeview refactor & zone #1140
gitea/salix/dev This commit looks good Details

This commit is contained in:
Joan Sanchez 2019-02-18 08:37:26 +01:00
parent 56bc860fa0
commit 3b0d003322
11 changed files with 190 additions and 218 deletions

View File

@ -1,5 +1,5 @@
<md-checkbox <md-checkbox
aria-label="Checkbox 1" aria-label="{{::$ctrl.label}}"
md-indeterminate="$ctrl.isIntermediate" md-indeterminate="$ctrl.isIntermediate"
ng-disabled="$ctrl.disabled" ng-disabled="$ctrl.disabled"
ng-checked="$ctrl.isChecked" ng-checked="$ctrl.isChecked"

View File

@ -2,15 +2,16 @@
vn-check { vn-check {
position: relative; position: relative;
& > .mdl-checkbox {
width: initial;
}
& > i { & > i {
padding-left: 5px; padding-left: 5px;
position: absolute; position: absolute;
bottom: 3px;
color: $color-font-secondary; color: $color-font-secondary;
font-size: 20px !important font-size: 20px !important;
cursor: help
}
md-checkbox.md-checked .md-icon {
background-color: $color-main;
} }
} }

View File

@ -1,24 +1,24 @@
<ul ng-if="$ctrl.items"> <ul ng-if="$ctrl.items">
<li ng-repeat="item in $ctrl.items" ng-class="{'selected' : item.selected && !item.excluded, 'included': item.included}"> <li ng-repeat="item in $ctrl.items"
ng-class="{
'expanded': item.active,
'collapsed': !item.active,
'included': item.isIncluded == 1,
'excluded': item.isIncluded == 0
}">
<vn-horizontal> <vn-horizontal>
<vn-auto class="actions"> <vn-auto class="actions">
<vn-icon icon="keyboard_arrow_up" ng-if="item.childs.length" <vn-icon icon="keyboard_arrow_down"
ng-click="$ctrl.toggle(item, $event)"> ng-show="item.sons > 0"
</vn-icon>
<vn-icon icon="keyboard_arrow_down" ng-if="item.sons > 0 && !item.childs"
ng-click="$ctrl.toggle(item, $event)" > ng-click="$ctrl.toggle(item, $event)" >
</vn-icon> </vn-icon>
</vn-auto> </vn-auto>
<vn-one class="description"> <div class="description">
<vn-horizontal> <vn-check vn-auto field="item.isIncluded"
<vn-check vn-auto field="item.selected && !item.excluded" on-change="$ctrl.select(item, value)" triple-state="true">
on-change="$ctrl.select(item)">
</vn-check> </vn-check>
<vn-one ng-dblclick="$ctrl.toggle(item)" class="text unselectable">
{{::item.name}} {{::item.name}}
</vn-one> </div>
</vn-horizontal>
</vn-one>
</vn-horizontal> </vn-horizontal>
<vn-treeview-child items="item.childs"></vn-treeview-child> <vn-treeview-child items="item.childs"></vn-treeview-child>
</li> </li>

View File

@ -10,8 +10,8 @@ class Controller extends Component {
this.treeview.onToggle(item); this.treeview.onToggle(item);
} }
select(item) { select(item, value) {
this.treeview.onSelection(item); this.treeview.onSelection(item, value);
} }
} }

View File

@ -20,72 +20,10 @@ export default class Treeview extends Component {
refresh() { refresh() {
this.model.refresh().then(() => { this.model.refresh().then(() => {
this.data = this.model.data; this.data = this.model.data;
console.log(this.data);
this.repaintAll();
}); });
} }
repaintAll() { /* hasCheckedChilds(node) {
let oldData = this.data;
oldData.forEach(node => {
this.repaintAsc(node);
this.repaintDesc(node);
});
}
repaintNode(node) {
this.repaintAsc(node);
this.repaintDesc(node);
}
repaintAsc(node) {
if (!node.parent) return;
const parent = node.parent;
if ((node.selected || node.included) && !parent.selected) {
parent.included = true;
parent.hasCheckedChilds = true;
} else if (!this.hasCheckedChilds(parent) && !this.hasCheckedParents(node))
parent.included = false;
// FIXME - Propagate hasCheckedCHilds
if (!node.selected && this.hasCheckedParents(node)) {
node.included = true;
parent.hasCheckedChilds = false;
}
if (!this.hasCheckedChilds(node))
node.hasCheckedChilds = false;
this.repaintAsc(parent);
}
repaintDesc(node) {
/* if (node.hasCheckedChilds)
node.included = false; */
if (!node.selected && this.hasCheckedChilds(node)) {
node.hasCheckedChilds = true;
node.included = true;
} else if (!node.selected && node.childs && !this.hasCheckedChilds(node))
node.hasCheckedChilds = false;
const childs = node.childs || [];
for (let i = 0; i < childs.length; i++) {
childs[i].included = false;
if (((node.selected || node.included && this.hasCheckedParents(childs[i])) && !childs[i].selected) && !node.excluded)
childs[i].included = true;
this.repaintDesc(childs[i]);
}
if (!node.selected && node.hasCheckedChilds)
node.included = true;
}
hasCheckedChilds(node) {
if (!node.childs) return false; if (!node.childs) return false;
const childs = node.childs; const childs = node.childs;
@ -105,44 +43,34 @@ export default class Treeview extends Component {
return true; return true;
return false; return false;
} } */
onSelection(item) { onSelection(item, value) {
let canBeExcluded = this.hasCheckedChilds(item) || this.hasCheckedParents(item); this.emit('selection', {item, value});
if (!item.selected && item.included && canBeExcluded)
item.excluded = true;
else
item.excluded = false;
item.selected = !item.selected;
if (item.selected && item.included)
item.included = false;
if (this.hasCheckedChilds(item))
item.hasCheckedChilds = true;
else if (this.childs)
item.hasCheckedChilds = false;
this.emit('selection', {item});
} }
onToggle(item) { onToggle(item) {
if (item.childs && item.childs.length == 0) if (item.active)
return;
if (item.childs)
item.childs = undefined; item.childs = undefined;
else { else {
this.model.applyFilter({}, {parentFk: item.id}).then(() => { this.model.applyFilter({}, {parentFk: item.id}).then(() => {
item.childs = this.model.data; const newData = this.model.data;
item.childs.forEach(child => {
child.parent = item; if (item.childs) {
let childs = item.childs;
childs.forEach(child => {
let index = newData.findIndex(newChild => {
return newChild.id == child.id;
}); });
this.repaintNode(item); newData[index] = child;
}); });
} }
item.childs = newData.sort((a, b) => b.isIncluded - a.isIncluded);
});
}
item.active = !item.active;
} }
} }

View File

@ -2,19 +2,21 @@
vn-treeview { vn-treeview {
ul { ul {
margin: 0; line-height: 24px;
padding: 0; padding: 0;
margin: 0;
li { li {
list-style: none; list-style: none;
cursor: pointer; cursor: pointer;
.actions { .actions {
padding: 0.5em; min-width: 24px;
} }
.description { .description {
padding: 0.5em padding-left: 5px
} }
} }
@ -22,27 +24,41 @@ vn-treeview {
padding: 0 1.8em; padding: 0 1.8em;
} }
li > vn-horizontal {
padding: 5px
}
li > vn-horizontal:hover { li > vn-horizontal:hover {
background-color: $color-hover-cd background-color: $color-hover-cd
} }
li.selected > vn-horizontal > .description .text, li.expanded .actions > vn-icon[icon="keyboard_arrow_down"] {
li.included > vn-horizontal > .description .text { transition: all 0.2s;
font-weight: bold; transform: rotate(180deg);
color: $color-main; }
li.collapsed .actions > vn-icon[icon="keyboard_arrow_down"] {
transition: all 0.2s;
transform: rotate(0deg);
} }
li.included { li.included {
& > vn-horizontal > .description > vn-horizontal > vn-check { & > vn-horizontal > .description {
.mdl-checkbox .mdl-checkbox__box-outline, { color: $color-notice;
border: 2px solid $color-main-medium; font-weight: bold;
& > vn-check .md-icon {
background-color: $color-notice
} }
fieldset[disabled] .mdl-checkbox .mdl-checkbox__box-outline, .mdl-checkbox.is-disabled .mdl-checkbox__box-outline {
border: 2px solid rgba(0,0,0,.26);
}
.mdl-checkbox .mdl-checkbox__tick-outline {
background: $color-main-medium;
} }
}
li.excluded {
& > vn-horizontal > .description {
color: $color-alert;
font-weight: bold;
} }
} }
} }

View File

@ -6,12 +6,6 @@ module.exports = Self => {
description: 'Returns the first shipped and landed possible for params', description: 'Returns the first shipped and landed possible for params',
accessType: '', accessType: '',
accepts: [{ accepts: [{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
http: {source: 'query'}
},
{
arg: 'zoneFk', arg: 'zoneFk',
type: 'Number', type: 'Number',
required: true, required: true,
@ -21,6 +15,12 @@ module.exports = Self => {
type: 'Number', type: 'Number',
default: 1, default: 1,
required: false, required: false,
},
{
arg: 'filter',
type: 'Object',
description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string',
http: {source: 'query'}
}], }],
returns: { returns: {
type: ['object'], type: ['object'],
@ -32,64 +32,106 @@ module.exports = Self => {
} }
}); });
Self.getLeaves = async(filter, zoneFk, parentFk = 1) => { Self.getLeaves = async(zoneFk, parentFk = 1, filter) => {
let conn = Self.dataSource.connector; let conn = Self.dataSource.connector;
let stmts = []; let stmts = [];
let geo = await Self.findById(parentFk);
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tmp.checkedChilds`);
stmts.push(new ParameterizedSQL( stmts.push(new ParameterizedSQL(
`SELECT lft, rgt, depth + 1 INTO @lft, @rgt, @depth `CREATE TEMPORARY TABLE tmp.checkedChilds (
FROM zoneGeo WHERE id = ?`, [parentFk])); id INT,
name VARCHAR(100),
lft INT,
rgt INT,
depth BIGINT(22),
sons DECIMAL(10, 0),
isIncluded TINYINT
) ENGINE = MEMORY`));
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tChilds`); if (parentFk === 1) {
stmts.push(new ParameterizedSQL(
let stmt = new ParameterizedSQL( `INSERT INTO tmp.checkedChilds
`CREATE TEMPORARY TABLE tChilds SELECT
ENGINE = MEMORY zg.id,
SELECT id, lft, rgt zg.name,
FROM zoneGeo pt`); zg.lft,
stmt.merge(conn.makeSuffix(filter)); zg.rgt,
zg.depth,
if (!filter.where) { zg.sons,
stmt.merge(`WHERE pt.lft > @lft AND pt.rgt < @rgt zi.isIncluded
AND pt.depth = @depth`); FROM zoneGeo zg
JOIN zoneIncluded zi ON zi.geoFk = zg.id
AND zoneFk = ?`, [zoneFk]));
} }
stmts.push(stmt); let stmt = new ParameterizedSQL(
`SELECT * FROM (
SELECT
zg.id,
zg.name,
zg.lft,
zg.rgt,
zg.depth,
zg.sons,
IF(ch.id = zg.id, isIncluded, null) isIncluded
FROM zoneGeo zg
JOIN tmp.checkedChilds ch ON zg.lft <= ch.lft AND zg.rgt >= ch.rgt
UNION ALL
SELECT
child.id,
child.name,
child.lft,
child.rgt,
child.depth,
child.sons,
zi.isIncluded
FROM zoneGeo parent
JOIN zoneGeo child ON child.lft > parent.lft
AND child.rgt < parent.rgt
AND child.depth = parent.depth + 1
LEFT JOIN zoneIncluded zi ON zi.geoFk = child.id
AND zoneFk = ?
WHERE parent.id = ?) AS nst`, [zoneFk, parentFk]);
stmts.push(`DROP TEMPORARY TABLE IF EXISTS tZones`); // Get nodes from depth greather than Origin
stmts.push(new ParameterizedSQL( stmt.merge(conn.makeSuffix(filter));
`CREATE TEMPORARY TABLE tZones stmt.merge('GROUP BY nst.id');
(INDEX (id))
ENGINE = MEMORY
SELECT t.id
FROM tChilds t
JOIN zoneGeo zt
ON zt.lft > t.lft AND zt.rgt < t.rgt
JOIN zoneIncluded zi
ON zi.geoFk = zt.id AND zi.zoneFk = ?
GROUP BY t.id`, [zoneFk]));
const resultIndex = stmts.push(new ParameterizedSQL( const resultIndex = stmts.push(stmt) - 1;
`SELECT
pt.id,
pt.name,
pt.lft,
pt.rgt,
pt.depth,
pt.sons,
ti.id IS NOT NULL hasCheckedChilds,
zi.geoFk IS NOT NULL AS selected,
zi.isIncluded AS excluded
FROM zoneGeo pt
LEFT JOIN vn.zoneIncluded zi
ON zi.geoFk = pt.id AND zi.zoneFk = ?
JOIN tChilds c ON c.id = pt.id
LEFT JOIN tZones ti ON ti.id = pt.id
ORDER BY selected DESC, name`, [zoneFk])) - 1;
const sql = ParameterizedSQL.join(stmts, ';'); const sql = ParameterizedSQL.join(stmts, ';');
const result = await Self.rawStmt(sql); const result = await Self.rawStmt(sql);
const nodes = result[resultIndex];
return result[resultIndex]; // Get parent nodes
const parentNodes = nodes.filter(element => {
return element.depth === geo.depth + 1;
});
const sortedLeaves = parentNodes.sort((a, b) => b.isIncluded - a.isIncluded);
const leaves = Object.assign([], sortedLeaves);
nestLeaves(leaves);
function nestLeaves(elements) {
elements.forEach(element => {
element.childs = Object.assign([], getLeaves(element));
nestLeaves(element.childs);
});
}
function getLeaves(parent) {
let elements = nodes.filter(element => {
return element.lft > parent.lft && element.rgt < parent.rgt
&& element.depth === parent.depth + 1;
});
return elements;
}
return leaves;
}; };
}; };

View File

@ -8,8 +8,8 @@ module.exports = Self => {
required: true, required: true,
}, },
{ {
arg: 'geoFk', arg: 'item',
type: 'Number', type: 'Object',
required: true, required: true,
}], }],
returns: { returns: {
@ -22,31 +22,15 @@ module.exports = Self => {
} }
}); });
Self.toggleIsIncluded = async(zoneFk, geoFk) => { Self.toggleIsIncluded = async(zoneFk, item) => {
const models = Self.app.models; if (item.isIncluded === null)
const geo = await models.ZoneGeo.findById(geoFk); return Self.destroyAll({zoneFk, geoFk: item.id});
const isIncluded = await Self.findOne({ else {
where: {zoneFk, geoFk} return Self.upsert({
zoneFk: zoneFk,
geoFk: item.id,
isIncluded: item.isIncluded
}); });
}
const hasCheckedParents = await Self.rawSql(
`SELECT id
FROM vn.zoneGeo zt
JOIN vn.zoneIncluded zi ON zi.geoFk = zt.id
WHERE zt.lft < ? AND zt.rgt > ?`, [geo.lft, geo.rgt]
);
const hasCheckedChilds = await Self.rawSql(
`SELECT id
FROM vn.zoneGeo zt
JOIN vn.zoneIncluded zi ON zi.geoFk = zt.id
WHERE zt.lft > ? AND zt.rgt < ?`, [geo.lft, geo.rgt]
);
const isExcluded = hasCheckedParents.length || hasCheckedChilds.length;
if (isIncluded)
return Self.destroyAll({zoneFk, geoFk});
else
return Self.upsert({zoneFk, geoFk, isIncluded: isExcluded});
}; };
}; };

View File

@ -33,7 +33,7 @@ class Controller {
res.data.forEach(holiday => { res.data.forEach(holiday => {
events.push({ events.push({
date: holiday.dated, date: holiday.dated,
color: 'blue-circle', color: 'red',
title: holiday.description || holiday.name, title: holiday.description || holiday.name,
isRemovable: false isRemovable: false
}); });

View File

@ -2,7 +2,7 @@
vn-id="model" vn-id="model"
url="/agency/api/ZoneGeos/getLeaves" url="/agency/api/ZoneGeos/getLeaves"
filter="::$ctrl.filter" filter="::$ctrl.filter"
params="{zoneFk: $ctrl.$stateParams.id, parentFk: 1}"> params="{zoneFk: $ctrl.$stateParams.id}" auto-load="false">
</vn-crud-model> </vn-crud-model>
<div class="main-with-right-menu"> <div class="main-with-right-menu">
<vn-card compact pad-large> <vn-card compact pad-large>
@ -13,7 +13,7 @@
vn-focus> vn-focus>
</vn-searchbar> </vn-searchbar>
<vn-treeview vn-id="treeview" model="model" <vn-treeview vn-id="treeview" model="model"
on-selection="$ctrl.onSelection(item)"> on-selection="$ctrl.onSelection(item, value)">
</vn-treeview> </vn-treeview>
</vn-card> </vn-card>
<vn-side-menu side="right"> <vn-side-menu side="right">

View File

@ -22,11 +22,12 @@ class Controller {
} }
} }
onSelection(item) { onSelection(item, isIncluded) {
item.isIncluded = isIncluded;
const path = '/agency/api/ZoneIncludeds/toggleIsIncluded'; const path = '/agency/api/ZoneIncludeds/toggleIsIncluded';
const params = {geoFk: item.id, zoneFk: this.zone.id}; const params = {zoneFk: this.zone.id, item};
this.$http.post(path, params).then(() => { this.$http.post(path, params).then(() => {
this.$scope.treeview.repaintNode(item);
}); });
} }
} }