@yanqirenshi/d3.wireframe 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,84 +1,77 @@
1
- import ElementFounder from './ElementFounder';
2
- import ElementLabel from './ElementLabel';
3
- import ElementIcon from './ElementIcon';
4
-
5
- import STROKE from './DataStroke';
6
-
7
- export default class ElementButton extends ElementFounder {
8
- constructor (params) {
9
- super('button', params);
10
-
11
- this.label = new ElementLabel({idm:this.idm});
12
- this.icon = new ElementIcon({idm:this.idm});
13
- }
14
- /* **************************************************************** *
15
- * Data
16
- * **************************************************************** */
17
- templateElement () {
18
- return this.makeTemplateElement({
19
- round:10,
20
- stroke: { color: '#afafb0', width: 1 },
21
- labels: [],
22
- children: [],
23
- icon: null,
24
- });
25
- }
26
- makeElement (core, parent) {
27
- let element = this.templateElement();
28
-
29
- this.initElement(element, core, parent);
30
-
31
- if (core.round)
32
- element.round = core.round;
33
-
34
- this.fittingRectangle(element, core);
35
- this.fittingPosition(element, core);
36
- this.fittingBackground(element, core);
37
-
38
- if (core.stroke===null)
39
- element.stroke = null;
40
- else
41
- this.fittingStroke(element, core);
42
-
43
- element.labels = this.label.makeElements (core.labels);
44
- element.icons = this.icon.makeElements(core.icons, element);
45
-
46
-
47
- return element;
48
- }
49
- mergeCore (core, element) {
50
- element._core = core;
51
- core._element = element;
52
- }
53
- /* **************************************************************** *
54
- * Draw
55
- * **************************************************************** */
56
- drawAdd (place, data, projector) {
57
- const g = place.selectAll(`g.${this.name}`)
58
- .data([data], (d) => { return d._id; })
59
- .enter()
60
- .append('g')
61
- .attr('class', this.name)
62
- .attr("transform", d => this.groupTransform(d, projector));
63
-
64
- this.drawBase(g, projector);
65
-
66
- this.icon.draw(g, data, projector);
67
- this.label.draw(g, data, projector);
68
- }
69
- drawUpdate (place, data) {
70
- place.selectAll(`g.${this.name}`)
71
- .data([data], (d) => { return d._id; });
72
- }
73
- drawDelete (place, data) {
74
- place.selectAll(`g.${this.name}`)
75
- .data([data], (d) => { return d._id; })
76
- .exit()
77
- .remove();
78
- }
79
- draw (place, data, projector) {
80
- // this.drawDelete(place, data);
81
- this.drawUpdate(place, data);
82
- this.drawAdd(place, data, projector);
83
- }
84
- }
1
+ import ElementFounder from './ElementFounder';
2
+ import ElementLabel from './ElementLabel';
3
+ import ElementIcon from './ElementIcon';
4
+
5
+ import { BUTTON_DEFAULTS, STROKE_DEFAULTS, roundOf } from '@yanqirenshi/model.wireframe';
6
+
7
+ export default class ElementButton extends ElementFounder {
8
+ constructor (params) {
9
+ super('button', params);
10
+
11
+ this.label = new ElementLabel({idm:this.idm});
12
+ this.icon = new ElementIcon({idm:this.idm});
13
+ }
14
+ /* **************************************************************** *
15
+ * Data
16
+ * **************************************************************** */
17
+ templateElement () {
18
+ return this.makeTemplateElement({
19
+ round: BUTTON_DEFAULTS.round,
20
+ stroke: { ...STROKE_DEFAULTS },
21
+ labels: [],
22
+ children: [],
23
+ icon: null,
24
+ });
25
+ }
26
+ makeElement (core, parent) {
27
+ let element = this.templateElement();
28
+
29
+ this.initElement(element, core, parent);
30
+
31
+ // 既定(角丸 10・線)は model.wireframe が持つ(#195)
32
+ element.round = roundOf(core);
33
+
34
+ this.fittingRectangle(element, core);
35
+ this.fittingPosition(element, core);
36
+ this.fittingBackground(element, core);
37
+
38
+ this.fittingStroke(element, core); // null なら線なし、書いていなければ既定の線
39
+
40
+ element.labels = this.label.makeElements (core.labels);
41
+ element.icons = this.icon.makeElements(core.icons, element);
42
+
43
+
44
+ return element;
45
+ }
46
+ /* **************************************************************** *
47
+ * Draw
48
+ * **************************************************************** */
49
+ drawAdd (place, data, projector) {
50
+ const g = place.selectAll(`g.${this.name}`)
51
+ .data([data], (d) => { return d._id; })
52
+ .enter()
53
+ .append('g')
54
+ .attr('class', this.name)
55
+ .attr("transform", d => this.groupTransform(d, projector));
56
+
57
+ this.drawBase(g, projector);
58
+
59
+ this.icon.draw(g, data, projector);
60
+ this.label.draw(g, data, projector);
61
+ }
62
+ drawUpdate (place, data) {
63
+ place.selectAll(`g.${this.name}`)
64
+ .data([data], (d) => { return d._id; });
65
+ }
66
+ drawDelete (place, data) {
67
+ place.selectAll(`g.${this.name}`)
68
+ .data([data], (d) => { return d._id; })
69
+ .exit()
70
+ .remove();
71
+ }
72
+ draw (place, data, projector) {
73
+ // this.drawDelete(place, data);
74
+ this.drawUpdate(place, data);
75
+ this.drawAdd(place, data, projector);
76
+ }
77
+ }
@@ -1,124 +1,130 @@
1
- import POSITION from './DataPosition';
2
- import RECTANGLE from './DataRectangle';
3
- import BACKGROUND from './DataBackground';
4
- import STROKE from './DataStroke';
5
- import FONT from './DataFont';
6
-
7
- export default class ElementFounder {
8
- constructor (name, params) {
9
- this.name = name;
10
-
11
- this.idm = params.idm;
12
- }
13
- /* **************************************************************** *
14
- * Data
15
- * **************************************************************** */
16
- makeTemplateElement (attributes) {
17
- return {
18
- ...{
19
- element_type: this.name,
20
- visible: true,
21
- _id: null,
22
- _core: null,
23
- },
24
- ...attributes
25
- };
26
- }
27
- makeCrossReference (element, core) {
28
- element._core = core;
29
- core._element = element;
30
- }
31
- initElementID (element, core) {
32
- element._id = core.id || this.idm.nextID();
33
- }
34
- initElementVisible (element, core) {
35
- if (core.visible===true || core.visible===false)
36
- element.visible = core.visible;
37
- }
38
- initElementParent (element, parent) {
39
- if (parent)
40
- element._parent = parent;
41
- }
42
- initElement (element, core, parent) {
43
- this.makeCrossReference(element, core);
44
- this.initElementID(element, core);
45
- this.initElementVisible(element, core);
46
- this.initElementParent(element, parent);
47
- }
48
- fittingRectangle (element, core) {
49
- element.rectangle = RECTANGLE.build(core.rectangle);
50
- }
51
- fittingPosition (element, core) {
52
- element.position = POSITION.build(core.position);
53
- }
54
- fittingBackground (element, core) {
55
- element.background = BACKGROUND.build(core.background);
56
- }
57
- fittingStroke (element, core) {
58
- element.stroke = STROKE.build(core.stroke || element.stroke);
59
- }
60
- fittingFont (element, core) {
61
- element.font = FONT.build(core.font);
62
- }
63
- merge (core, element) {}
64
- /* **************************************************************** *
65
- * Draw
66
- * **************************************************************** */
67
- /**
68
- * グループの transform。回転ビュー(projector あり)では絶対座標を
69
- * 射影して描くため translate は使わない。
70
- */
71
- groupTransform (d, projector) {
72
- if (projector)
73
- return null;
74
-
75
- return `translate(${d.position.x}, ${d.position.y})`;
76
- }
77
- /**
78
- * 要素の下地(base)を描く。通常はグループ局所座標の rect、
79
- * 回転ビューでは絶対座標の4頂点を射影した polygon(角丸 round は
80
- * 回転ビューでは省略される)。fill/stroke は共通で適用し、
81
- * 追加の属性は返り値の selection に呼び出し側で付ける。
82
- */
83
- drawBase (g, projector) {
84
- let base = null;
85
-
86
- if (projector) {
87
- base = g.append('polygon')
88
- .attr('class', 'base')
89
- .attr('points', d => projector.rectPoints(
90
- d.position.x, d.position.y, d.rectangle.w, d.rectangle.h,
91
- projector.depthZ(d._depth)));
92
- } else {
93
- base = g.append('rect')
94
- .attr('class', 'base')
95
- .attr('width', d => d.rectangle.w)
96
- .attr('height', d => d.rectangle.h)
97
- .attr('rx', d => d.round || 0)
98
- .attr('ry', d => d.round || 0);
99
- }
100
-
101
- return base
102
- .attr('fill', d => d.background.color)
103
- .attr('stroke', d => this.stroke(d))
104
- .attr('stroke-width', d => this.strokeWidth(d));
105
- }
106
- stroke (d) {
107
- if (!d.stroke)
108
- return null;
109
-
110
- if (!d.stroke)
111
- return '#333';
112
-
113
- return d.stroke.color || '#333';
114
- }
115
- strokeWidth (d) {
116
- if (!d.stroke)
117
- return null;
118
-
119
- if (!d.stroke)
120
- return 1;
121
-
122
- return d.stroke.width || 1;
123
- }
124
- }
1
+ import POSITION from './DataPosition';
2
+ import RECTANGLE from './DataRectangle';
3
+ import BACKGROUND from './DataBackground';
4
+ import FONT from './DataFont';
5
+
6
+ import { elementStrokeOf, visibleOf } from '@yanqirenshi/model.wireframe';
7
+
8
+ export default class ElementFounder {
9
+ constructor (name, params) {
10
+ this.name = name;
11
+
12
+ this.idm = params.idm;
13
+ }
14
+ /* **************************************************************** *
15
+ * Data
16
+ * **************************************************************** */
17
+ makeTemplateElement (attributes) {
18
+ return {
19
+ ...{
20
+ element_type: this.name,
21
+ visible: true,
22
+ _id: null,
23
+ _core: null,
24
+ },
25
+ ...attributes
26
+ };
27
+ }
28
+ /**
29
+ * 要素から入力を引けるようにする(element._core)。
30
+ * 入力の側には書かない(0.2.1 までは core._element を書いていたが、読む所が無く、
31
+ * 入力を書き換えて JSON にできなくしていたので、0.3.0 でやめた。#195)。
32
+ */
33
+ makeCrossReference (element, core) {
34
+ element._core = core;
35
+ }
36
+ initElementID (element, core) {
37
+ element._id = core.id || this.idm.nextID();
38
+ }
39
+ initElementVisible (element, core) {
40
+ // false と書いたときだけ描かない(model.wireframe の visibleOf)
41
+ element.visible = visibleOf(core);
42
+ }
43
+ initElementParent (element, parent) {
44
+ if (parent)
45
+ element._parent = parent;
46
+ }
47
+ initElement (element, core, parent) {
48
+ this.makeCrossReference(element, core);
49
+ this.initElementID(element, core);
50
+ this.initElementVisible(element, core);
51
+ this.initElementParent(element, parent);
52
+ }
53
+ fittingRectangle (element, core) {
54
+ element.rectangle = RECTANGLE.build(core.rectangle);
55
+ }
56
+ fittingPosition (element, core) {
57
+ element.position = POSITION.build(core.position);
58
+ }
59
+ fittingBackground (element, core) {
60
+ element.background = BACKGROUND.build(core.background);
61
+ }
62
+ fittingStroke (element, core) {
63
+ // 種類ごとの線(button は書いていなければ既定の線・null で線なし。ほかは書いていなければ線なし)。
64
+ // 判定は model.wireframe の elementStrokeOf(#195)
65
+ element.stroke = elementStrokeOf(core);
66
+ }
67
+ fittingFont (element, core) {
68
+ element.font = FONT.build(core.font);
69
+ }
70
+ /* **************************************************************** *
71
+ * Draw
72
+ * **************************************************************** */
73
+ /**
74
+ * グループの transform。回転ビュー(projector あり)では絶対座標を
75
+ * 射影して描くため translate は使わない。
76
+ */
77
+ groupTransform (d, projector) {
78
+ if (projector)
79
+ return null;
80
+
81
+ return `translate(${d.position.x}, ${d.position.y})`;
82
+ }
83
+ /**
84
+ * 要素の下地(base)を描く。通常はグループ局所座標の rect、
85
+ * 回転ビューでは絶対座標の4頂点を射影した polygon(角丸 round は
86
+ * 回転ビューでは省略される)。fill/stroke は共通で適用し、
87
+ * 追加の属性は返り値の selection に呼び出し側で付ける。
88
+ */
89
+ drawBase (g, projector) {
90
+ let base = null;
91
+
92
+ if (projector) {
93
+ base = g.append('polygon')
94
+ .attr('class', 'base')
95
+ .attr('points', d => projector.rectPoints(
96
+ d.position.x, d.position.y, d.rectangle.w, d.rectangle.h,
97
+ projector.depthZ(d._depth)));
98
+ } else {
99
+ base = g.append('rect')
100
+ .attr('class', 'base')
101
+ .attr('width', d => d.rectangle.w)
102
+ .attr('height', d => d.rectangle.h)
103
+ .attr('rx', d => d.round || 0)
104
+ .attr('ry', d => d.round || 0);
105
+ }
106
+
107
+ return base
108
+ .attr('fill', d => d.background.color)
109
+ .attr('stroke', d => this.stroke(d))
110
+ .attr('stroke-width', d => this.strokeWidth(d));
111
+ }
112
+ stroke (d) {
113
+ if (!d.stroke)
114
+ return null;
115
+
116
+ if (!d.stroke)
117
+ return '#333';
118
+
119
+ return d.stroke.color || '#333';
120
+ }
121
+ strokeWidth (d) {
122
+ if (!d.stroke)
123
+ return null;
124
+
125
+ if (!d.stroke)
126
+ return 1;
127
+
128
+ return d.stroke.width || 1;
129
+ }
130
+ }
@@ -1,66 +1,68 @@
1
- import ElementFounder from './ElementFounder';
2
-
3
- export default class ElementLabel extends ElementFounder {
4
- constructor (params) {
5
- super('label', params);
6
- }
7
- /* **************************************************************** *
8
- * Data
9
- * **************************************************************** */
10
- templateElement () {
11
- return this.makeTemplateElement({
12
- text: '????????',
13
- position: null,
14
- font: null,
15
- });
16
- }
17
- makeElement (core, parent) {
18
- let element = this.templateElement();
19
-
20
- this.initElement(element, core, parent);
21
-
22
- if (core.text)
23
- element.text = core.text;
24
-
25
- this.fittingPosition(element, core);
26
- this.fittingFont(element, core);
27
-
28
- return element;
29
- }
30
- makeElements (labels) {
31
- if (!labels)
32
- return [];
33
-
34
- return labels.map((d) => this.makeElement(d));
35
- }
36
- /* **************************************************************** *
37
- * Draw
38
- * **************************************************************** */
39
- /**
40
- * ラベルを描く。回転ビュー(projector あり)では、親要素の絶対位置 +
41
- * ラベル相対位置を射影した点に置き、フォントサイズは距離スケール(k)で
42
- * 近似する(SVG の文字は遠近で歪ませられないため、位置と大きさのみ)。
43
- */
44
- draw (place, data, projector) {
45
- const project = (d) => projector.point(
46
- data.position.x + d.position.x,
47
- data.position.y + d.position.y,
48
- projector.depthZ(data._depth));
49
-
50
- place.selectAll('label')
51
- .data(data.labels, d => d._id)
52
- .enter()
53
- .append("text")
54
- .attr("x", d => projector ? project(d).x : d.position.x)
55
- .attr("y", d => projector ? project(d).y : d.position.y)
56
- .style("font-size", d => {
57
- if (!projector)
58
- return `${d.font.size}px`;
59
-
60
- return `${d.font.size * project(d).k}px`;
61
- })
62
- .style("fill", d => d.font.color)
63
- .style("font-weight", d => d.font.weight)
64
- .text(d => d.text);
65
- }
66
- }
1
+ import ElementFounder from './ElementFounder';
2
+
3
+ import { LABEL_DEFAULTS } from '@yanqirenshi/model.wireframe';
4
+
5
+ export default class ElementLabel extends ElementFounder {
6
+ constructor (params) {
7
+ super('label', params);
8
+ }
9
+ /* **************************************************************** *
10
+ * Data
11
+ * **************************************************************** */
12
+ templateElement () {
13
+ return this.makeTemplateElement({
14
+ text: LABEL_DEFAULTS.text, // model.wireframe(#195)
15
+ position: null,
16
+ font: null,
17
+ });
18
+ }
19
+ makeElement (core, parent) {
20
+ let element = this.templateElement();
21
+
22
+ this.initElement(element, core, parent);
23
+
24
+ if (core.text)
25
+ element.text = core.text;
26
+
27
+ this.fittingPosition(element, core);
28
+ this.fittingFont(element, core);
29
+
30
+ return element;
31
+ }
32
+ makeElements (labels) {
33
+ if (!labels)
34
+ return [];
35
+
36
+ return labels.map((d) => this.makeElement(d));
37
+ }
38
+ /* **************************************************************** *
39
+ * Draw
40
+ * **************************************************************** */
41
+ /**
42
+ * ラベルを描く。回転ビュー(projector あり)では、親要素の絶対位置 +
43
+ * ラベル相対位置を射影した点に置き、フォントサイズは距離スケール(k)で
44
+ * 近似する(SVG の文字は遠近で歪ませられないため、位置と大きさのみ)。
45
+ */
46
+ draw (place, data, projector) {
47
+ const project = (d) => projector.point(
48
+ data.position.x + d.position.x,
49
+ data.position.y + d.position.y,
50
+ projector.depthZ(data._depth));
51
+
52
+ place.selectAll('label')
53
+ .data(data.labels, d => d._id)
54
+ .enter()
55
+ .append("text")
56
+ .attr("x", d => projector ? project(d).x : d.position.x)
57
+ .attr("y", d => projector ? project(d).y : d.position.y)
58
+ .style("font-size", d => {
59
+ if (!projector)
60
+ return `${d.font.size}px`;
61
+
62
+ return `${d.font.size * project(d).k}px`;
63
+ })
64
+ .style("fill", d => d.font.color)
65
+ .style("font-weight", d => d.font.weight)
66
+ .text(d => d.text);
67
+ }
68
+ }