-
Notifications
You must be signed in to change notification settings - Fork 18
/
d-diagram-layers.ts
140 lines (127 loc) · 3.45 KB
/
d-diagram-layers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* Copyright (C) 2019 Toshiba Corporation
* SPDX-License-Identifier: Apache-2.0
*/
import { DBaseStateSet } from "./d-base-state-set";
import { EShape } from "./shape/e-shape";
import { EShapeContainer } from "./shape/e-shape-container";
import { EShapeEmbeddedLayer } from "./shape/variant/e-shape-embedded-layer";
export interface DDiagramLayersShape {
parent: EShapeContainer | EShape | null;
}
export interface DDiagramLayersShapeContainer extends EShapeContainer {
state: DBaseStateSet;
}
export type DDiagramLayersLayer = DDiagramLayersShapeContainer | EShape;
export class DDiagramLayers {
static toLayers(shape: DDiagramLayersShape, indices: number[]): DDiagramLayersLayer[] {
const result: DDiagramLayersLayer[] = [];
const layer = this.toLayer(shape);
if (layer) {
const container = layer.parent;
if (container) {
const children = container.children as DDiagramLayersLayer[];
const childrenLength = children.length;
for (let i = 0, imax = indices.length; i < imax; ++i) {
const index = indices[i];
if (0 <= index && index < childrenLength) {
result.push(children[index]);
}
}
}
}
return result;
}
static toLayer(shape: DDiagramLayersShape): DDiagramLayersLayer | null | undefined {
let parent = shape.parent;
while (parent != null) {
if (parent instanceof EShapeContainer) {
return parent as DDiagramLayersShapeContainer;
}
if (parent instanceof EShapeEmbeddedLayer) {
return parent;
}
parent = parent.parent;
}
return null;
}
static show(target: DDiagramLayersLayer): boolean {
if (!target.visible) {
target.visible = true;
return true;
}
return false;
}
static showAll(targets: DDiagramLayersLayer[]): boolean {
let isChanged = false;
for (let i = 0, imax = targets.length; i < imax; ++i) {
const layer = targets[i];
if (!layer.visible) {
layer.visible = true;
isChanged = true;
}
}
return isChanged;
}
static hide(target: DDiagramLayersLayer): boolean {
if (target.visible) {
target.visible = false;
return true;
}
return false;
}
static hideAll(targets: DDiagramLayersLayer[]): boolean {
let isChanged = false;
for (let i = 0, imax = targets.length; i < imax; ++i) {
const layer = targets[i];
if (layer.visible) {
layer.visible = false;
isChanged = true;
}
}
return isChanged;
}
static bringToFront(target: DDiagramLayersLayer): boolean {
const parent = target.parent;
if (parent) {
return this.doBringToFront(target, parent.children, 0);
}
return false;
}
static bringAllToFront(targets: DDiagramLayersLayer[]): boolean {
const targetsLength = targets.length;
if (0 < targetsLength) {
const parent = targets[0].parent;
if (parent) {
let isChanged = false;
const layers = parent.children;
for (let i = 0; i < targetsLength; ++i) {
const layer = targets[targetsLength - 1 - i];
if (this.doBringToFront(layer, layers, i)) {
isChanged = true;
}
}
return isChanged;
}
}
return false;
}
protected static doBringToFront(target: unknown, layers: unknown[], offset: number): boolean {
const layersLength = layers.length;
const ito = layersLength - 1 - offset;
for (let i = ito; 0 <= i; --i) {
const child = layers[i];
if (child === target) {
if (i !== ito) {
for (let j = i + 1; j <= ito; ++j) {
layers[j - 1] = layers[j];
}
layers[ito] = child;
return true;
}
return false;
}
}
return false;
}
}