"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SGraphSelectContainer = void 0; const lib_1 = require("@persagy-web/base/lib"); const SGraphLayoutType_1 = require("./enums/SGraphLayoutType"); const SOrderSetType_1 = require("./enums/SOrderSetType"); class SGraphSelectContainer extends lib_1.SObject { constructor() { super(); this.itemList = []; this.radix = 0.001; } get count() { return this.itemList.length; } addItem(item) { for (let i = 0; i < this.itemList.length; i++) { if (this.itemList[i] == item) { return; } } item.selected = true; this.itemList.push(item); this.$emit("listChange", this.itemList); } setItem(item) { this.itemList.forEach((t) => { t.selected = false; }); this.itemList.length = 0; item.selected = true; this.itemList.push(item); this.$emit("listChange", this.itemList); } clear() { this.itemList.forEach((t) => { t.selected = false; }); this.itemList.length = 0; this.$emit("listChange", this.itemList); } toggleItem(item) { for (let i = 0; i < this.itemList.length; i++) { if (this.itemList[i] == item) { this.itemList[i].selected = false; this.itemList.splice(i, 1); this.$emit("listChange", this.itemList, item); return; } } if (this.itemList.length) { if (item.parent != this.itemList[0].parent) { return; } } item.selected = true; this.itemList.push(item); this.$emit("listChange", this.itemList); } layout(type) { if (this.itemList.length < 2) { return; } switch (type) { case SGraphLayoutType_1.SGraphLayoutType.Left: this.alignLeft(); break; case SGraphLayoutType_1.SGraphLayoutType.Bottom: this.alignBottom(); break; case SGraphLayoutType_1.SGraphLayoutType.Center: this.alignCenter(); break; case SGraphLayoutType_1.SGraphLayoutType.Horizontal: this.alignHorizontal(); break; case SGraphLayoutType_1.SGraphLayoutType.Middle: this.alignMiddle(); break; case SGraphLayoutType_1.SGraphLayoutType.Right: this.alignRight(); break; case SGraphLayoutType_1.SGraphLayoutType.Top: this.alignTop(); break; case SGraphLayoutType_1.SGraphLayoutType.Vertical: this.alignVertical(); break; default: console.log("对齐类型不存在"); break; } } setOrder(type) { if (this.itemList.length < 1) { return; } switch (type) { case SOrderSetType_1.SOrderSetType.Top: this.setTop(); break; case SOrderSetType_1.SOrderSetType.Bottom: this.setBottom(); break; case SOrderSetType_1.SOrderSetType.After: this.setAfter(); break; case SOrderSetType_1.SOrderSetType.Before: this.setBefore(); break; default: console.log("图层操作类型不存在"); break; } } alignLeft() { let standardItem = this.itemList[0]; let p = standardItem.mapToScene(standardItem.boundingRect().x, standardItem.boundingRect().y); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.x = (p1.x - curItem.boundingRect().x) * curItem.inverseScale + curItem.x; } } } alignTop() { let standardItem = this.itemList[0]; let p = standardItem.mapToScene(standardItem.boundingRect().x, standardItem.boundingRect().y); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.y = (p1.y - curItem.boundingRect().y) * curItem.inverseScale + curItem.y; } } } alignRight() { let standardItem = this.itemList[0]; let p = standardItem.mapToScene(standardItem.boundingRect().right, standardItem.boundingRect().bottom); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.x = (p1.x - curItem.boundingRect().right) * curItem.inverseScale + curItem.x; } } } alignBottom() { let standardItem = this.itemList[0]; let p = standardItem.mapToScene(standardItem.boundingRect().right, standardItem.boundingRect().bottom); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.y = (p1.y - curItem.boundingRect().bottom) * curItem.inverseScale + curItem.y; } } } alignCenter() { let standardItem = this.itemList[0]; let center = standardItem.boundingRect().center(); let p = standardItem.mapToScene(center.x, center.y); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.x = (p1.x - curItem.boundingRect().center().x) * curItem.inverseScale + curItem.x; } } } alignMiddle() { let standardItem = this.itemList[0]; let center = standardItem.boundingRect().center(); let p = standardItem.mapToScene(center.x, center.y); for (let i = 1; i < this.itemList.length; i++) { let curItem = this.itemList[i]; if (curItem.moveable) { let p1 = curItem.mapFromScene(p.x, p.y); curItem.y = (p1.y - curItem.boundingRect().center().y) * curItem.inverseScale + curItem.y; } } } alignVertical() { if (this.itemList.length < 3) { return; } for (let i = 0; i < this.itemList.length - 1; i++) { for (let j = 0; j < this.itemList.length - 1 - i; j++) { let curItemCenter = this.itemList[j].boundingRect().center(); let nextItemCenter = this.itemList[j + 1] .boundingRect() .center(); let curCenterY = this.itemList[j].mapToScene(curItemCenter.x, curItemCenter.y).y; let nextCenterY = this.itemList[j + 1].mapToScene(nextItemCenter.x, nextItemCenter.y).y; if (curCenterY > nextCenterY) { let temp = this.itemList[j]; this.itemList[j] = this.itemList[j + 1]; this.itemList[j + 1] = temp; } } } let top = this.itemList[0]; let bottom = this.itemList[this.itemList.length - 1]; let topCenter = top.boundingRect().center(); let bottomCenter = bottom.boundingRect().center(); let leftToRight = bottom.mapToScene(bottomCenter.x, bottomCenter.y).y - top.mapToScene(topCenter.x, topCenter.y).y; let average = leftToRight / (this.itemList.length - 1); for (let k = 1; k < this.itemList.length - 1; k++) { let curItem = this.itemList[k]; if (curItem.moveable) { let p1 = top.mapToScene(top.boundingRect().center().x, top.boundingRect().center().y); let p2 = curItem.mapFromScene(p1.x, p1.y + average * k); curItem.y = (p2.y - curItem.boundingRect().center().y) * curItem.inverseScale + curItem.y; } } } alignHorizontal() { if (this.itemList.length < 3) { return; } for (let i = 0; i < this.itemList.length - 1; i++) { for (let j = 0; j < this.itemList.length - 1 - i; j++) { let curItemCenter = this.itemList[j].boundingRect().center(); let nextItemCenter = this.itemList[j + 1] .boundingRect() .center(); let curCenterX = this.itemList[j].mapToScene(curItemCenter.x, curItemCenter.y).x; let nextCenterX = this.itemList[j + 1].mapToScene(nextItemCenter.x, nextItemCenter.y).x; if (curCenterX > nextCenterX) { let temp = this.itemList[j]; this.itemList[j] = this.itemList[j + 1]; this.itemList[j + 1] = temp; } } } let left = this.itemList[0]; let right = this.itemList[this.itemList.length - 1]; let leftCenter = left.boundingRect().center(); let rightCenter = right.boundingRect().center(); let leftToRight = right.mapToScene(rightCenter.x, rightCenter.y).x - left.mapToScene(leftCenter.x, leftCenter.y).x; let average = leftToRight / (this.itemList.length - 1); for (let k = 1; k < this.itemList.length - 1; k++) { let curItem = this.itemList[k]; if (curItem.moveable) { let p1 = left.mapToScene(left.boundingRect().center().x, left.boundingRect().center().y); let p2 = curItem.mapFromScene(p1.x + average * k, p1.y); curItem.x = (p2.x - curItem.boundingRect().center().x) * curItem.inverseScale + curItem.x; } } } setTop() { const arr = this.sortItemList(this.itemList, true); arr.forEach(it => { let max = it.zOrder; if (it.parent) { it.parent.children.forEach(item => { if (Number(max.toFixed()) == Number(item.zOrder.toFixed())) { if (item.zOrder > max) { max = item.zOrder; } } }); it.zOrder = max + this.radix; } }); } setBottom() { const arr = this.sortItemList(this.itemList, false); arr.forEach(it => { let min = it.zOrder; if (it.parent) { it.parent.children.forEach(item => { if (Number(min.toFixed()) == Number(item.zOrder.toFixed())) { if (item.zOrder < min) { min = item.zOrder; } } }); it.zOrder = min - this.radix; } }); } setBefore() { const arr = this.sortItemList(this.itemList, false); arr.forEach(it => { if (it.parent) { const min = it.zOrder; let temp = it.parent.children .map(child => { if (Number(child.zOrder.toFixed()) == Number(min.toFixed())) { return child.zOrder; } }) .filter(c => c); temp = [...new Set(temp)]; const index = temp.indexOf(it.zOrder); if (index <= 1) { it.zOrder = temp[0] - this.radix; } else if (index > 1) { it.zOrder = (temp[index - 1] + temp[index - 2]) / 2; } } }); } setAfter() { const arr = this.sortItemList(this.itemList, false); arr.forEach(it => { if (it.parent) { const max = it.zOrder; let temp = it.parent.children .map(child => { if (Number(child.zOrder.toFixed()) == Number(max.toFixed())) { return child.zOrder; } }) .filter(c => c); temp = [...new Set(temp)].reverse(); const index = temp.indexOf(it.zOrder); if (index <= 1) { it.zOrder = temp[0] + this.radix; } else if (index > 1) { it.zOrder = (temp[index - 1] + temp[index - 2]) / 2; } } }); } sortItemList(arr, flag = true) { const list = arr.map(t => t); list.sort(this.compare("zOrder", flag)); return list; } compare(prop, flag) { const f = flag ? 1 : -1; return (a, b) => { a = a[prop]; b = b[prop]; if (a < b) { return f * -1; } if (a > b) { return f * 1; } return 0; }; } } exports.SGraphSelectContainer = SGraphSelectContainer;