@yanqirenshi/d3.wireframe 0.2.1 → 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,61 +1,61 @@
1
- import ElementFounder from './ElementFounder';
2
-
3
- export default class ElementModal extends ElementFounder {
4
- constructor (params) {
5
- super('modal', params);
6
- }
7
- /* **************************************************************** *
8
- * Data
9
- * **************************************************************** */
10
- templateElement () {
11
- return this.makeTemplateElement({
12
- position: null,
13
- rectangle: null,
14
- children: [],
15
- });
16
- }
17
- makeElement (core, parent) {
18
- let element = this.templateElement();
19
-
20
- this.initElement(element, core, parent);
21
-
22
- this.fittingRectangle(element, core);
23
- this.fittingPosition(element, core);
24
- this.fittingBackground(element, core);
25
-
26
- return element;
27
- }
28
- /* **************************************************************** *
29
- * Draw
30
- * **************************************************************** */
31
- drawAdd (place, data, projector) {
32
- const g = place.selectAll(`g.${this.name}`)
33
- .data([data], (d) => { return d._id; })
34
- .enter()
35
- .append('g')
36
- .attr('class', this.name)
37
- .attr("transform", d => this.groupTransform(d, projector));
38
-
39
- // modal は stroke 無し + 半透明の面。共通の drawBase(rect/polygon の
40
- // 切替)を使い、追加の属性だけここで上書きする。
41
- this.drawBase(g, projector)
42
- .attr('stroke', null)
43
- .attr('stroke-width', null)
44
- .attr('fill-opacity', d => d.background.opacity);
45
- }
46
- drawUpdate (place, data) {
47
- place.selectAll(`g.${this.name}`)
48
- .data([data], (d) => { return d._id; });
49
- }
50
- drawDelete (place, data) {
51
- place.selectAll(`g.${this.name}`)
52
- .data([data], (d) => { return d._id; })
53
- .exit()
54
- .remove();
55
- }
56
- draw (place, data, projector) {
57
- // this.drawDelete(place, data);
58
- this.drawUpdate(place, data);
59
- this.drawAdd(place, data, projector);
60
- }
61
- }
1
+ import ElementFounder from './ElementFounder';
2
+
3
+ export default class ElementModal extends ElementFounder {
4
+ constructor (params) {
5
+ super('modal', params);
6
+ }
7
+ /* **************************************************************** *
8
+ * Data
9
+ * **************************************************************** */
10
+ templateElement () {
11
+ return this.makeTemplateElement({
12
+ position: null,
13
+ rectangle: null,
14
+ children: [],
15
+ });
16
+ }
17
+ makeElement (core, parent) {
18
+ let element = this.templateElement();
19
+
20
+ this.initElement(element, core, parent);
21
+
22
+ this.fittingRectangle(element, core);
23
+ this.fittingPosition(element, core);
24
+ this.fittingBackground(element, core);
25
+
26
+ return element;
27
+ }
28
+ /* **************************************************************** *
29
+ * Draw
30
+ * **************************************************************** */
31
+ drawAdd (place, data, projector) {
32
+ const g = place.selectAll(`g.${this.name}`)
33
+ .data([data], (d) => { return d._id; })
34
+ .enter()
35
+ .append('g')
36
+ .attr('class', this.name)
37
+ .attr("transform", d => this.groupTransform(d, projector));
38
+
39
+ // modal は stroke 無し + 半透明の面。共通の drawBase(rect/polygon の
40
+ // 切替)を使い、追加の属性だけここで上書きする。
41
+ this.drawBase(g, projector)
42
+ .attr('stroke', null)
43
+ .attr('stroke-width', null)
44
+ .attr('fill-opacity', d => d.background.opacity);
45
+ }
46
+ drawUpdate (place, data) {
47
+ place.selectAll(`g.${this.name}`)
48
+ .data([data], (d) => { return d._id; });
49
+ }
50
+ drawDelete (place, data) {
51
+ place.selectAll(`g.${this.name}`)
52
+ .data([data], (d) => { return d._id; })
53
+ .exit()
54
+ .remove();
55
+ }
56
+ draw (place, data, projector) {
57
+ // this.drawDelete(place, data);
58
+ this.drawUpdate(place, data);
59
+ this.drawAdd(place, data, projector);
60
+ }
61
+ }
package/src/Rectum.js CHANGED
@@ -5,8 +5,36 @@ import Projector from './Projector';
5
5
 
6
6
  import { DrawerHierarchy } from './Drawer.js';
7
7
 
8
+ /** 要素のグループ(foreground レイヤの直下に、親 → 子の順で平らに並ぶ) */
9
+ const ELEMENT_GROUPS = 'g.frame, g.image, g.button, g.modal, g.dialog';
10
+
8
11
  /**
9
- * データクラス。ワイヤーフレーム要素の構築・マージ・保持を行い、
12
+ * click / contextmenu を、コールバックへ知らせる(#195)。
13
+ * d3.sitemap / d3.deployment / d3.unchi / d3.dfd の notify と同じ作法。
14
+ *
15
+ * - コールバックがあるときだけ呼ばれる(呼ぶ側が、無ければ on() を付けない)。
16
+ * 渡していなければ何もしないので、ブラウザ標準のメニューが出て、イベントは外側へ届く
17
+ * - contextmenu: 左ボタンを押したまま(ドラッグ中)の右クリックは知らせない。
18
+ * それ以外は、ブラウザ標準のメニューを止め、外側へ伝えない。最後の引数に event を渡す
19
+ * - click: 外側へ伝えず、(core, element) で呼ぶ
20
+ */
21
+ const notify = (callback, event, core, datum) => {
22
+ if (event.type === 'contextmenu') {
23
+ if (event.buttons & 1)
24
+ return;
25
+
26
+ event.preventDefault();
27
+ event.stopPropagation();
28
+ callback(core, datum, event);
29
+ return;
30
+ }
31
+
32
+ event.stopPropagation();
33
+ callback(core, datum);
34
+ };
35
+
36
+ /**
37
+ * データクラス。ワイヤーフレーム要素の構築・保持を行い、
10
38
  * 描画は Element(各 Element クラス)へ委譲する。
11
39
  *
12
40
  * 旧 D3Wireframe(assh0le 0.0.3 の Asshole クラス継承)を
@@ -22,6 +50,30 @@ export default class Rectum extends Colon {
22
50
  // 回転ビュー(任意)。{ y, x, perspective, origin } を渡すと
23
51
  // レイアウト平面を透視投影して描く(Projector 参照)。
24
52
  this._rotation = (params && params.rotation) || null;
53
+
54
+ // コールバック(任意。0.3.0 から)。描画は行わず通知のみ
55
+ this._callbacks = this.initCallbacks(params);
56
+
57
+ // Ctrl + ドラッグで回転させた直後のクリックを、要素のクリックとして知らせないための印
58
+ this._rotate_moved = false;
59
+ }
60
+ /**
61
+ * コールバック(すべて任意。0.3.0 から)。
62
+ * element.click(core, element) … 要素のクリック
63
+ * element.contextmenu(core, element, event) … 要素の右クリック
64
+ * core は入力のレコードそのもの、element は描画用に作った要素(位置は親の位置を足した絶対位置)。
65
+ * 入れ子の要素は、いちばん内側(上に描かれている)の要素だけが知らせる。
66
+ */
67
+ initCallbacks (params) {
68
+ const callbacks = (params && params.callbacks) || {};
69
+ const element = callbacks.element || {};
70
+
71
+ return {
72
+ element: {
73
+ click: element.click || null,
74
+ contextmenu: element.contextmenu || null,
75
+ },
76
+ };
25
77
  }
26
78
  /* **************************************************************** *
27
79
  * Rotation (回転ビュー)
@@ -64,7 +116,10 @@ export default class Rectum extends Colon {
64
116
  lift: base.lift,
65
117
  };
66
118
 
119
+ this._rotate_moved = false;
120
+
67
121
  const move = (e) => {
122
+ this._rotate_moved = true;
68
123
  this.rotation({
69
124
  ...rest,
70
125
  y: this.clampAngle(start.y + (e.clientX - start.mx) * DEG_PER_PX),
@@ -74,6 +129,11 @@ export default class Rectum extends Colon {
74
129
  const up = () => {
75
130
  window.removeEventListener('mousemove', move);
76
131
  window.removeEventListener('mouseup', up);
132
+
133
+ // 離したあとに届く click は、回転の操作の一部なので要素のクリックとして知らせない。
134
+ // click は mouseup の直後に届くので、タイマーで印を消す
135
+ // (mousedown で消す形にしないのは、d3.zoom が Ctrl なしの mousedown の伝播を止めるため)
136
+ setTimeout(() => { this._rotate_moved = false; }, 0);
77
137
  };
78
138
 
79
139
  window.addEventListener('mousemove', move);
@@ -122,58 +182,35 @@ export default class Rectum extends Colon {
122
182
 
123
183
  this._rotation = v;
124
184
 
125
- const place = this.layer('foreground');
126
- if (place) {
127
- place.selectAll('*').remove();
185
+ // draw() が描いてあるものを消してから描き直す(0.3.0 から)
186
+ if (this.layer('foreground'))
128
187
  this.draw();
129
- }
130
188
 
131
189
  return this;
132
190
  }
133
191
  /* **************************************************************** *
134
192
  * Make Elements
135
193
  * **************************************************************** */
136
- firstObject (list) {
137
- if (list.length===0)
138
- return null;
139
- return list[0];
140
- }
141
- merge (source, target) {
142
- const source_sorted = source.sort((a, b) => { return a.id<b.id ? -1 : 1; });
143
- const target_sorted = target.sort((a, b) => {
144
- return a._core.id < b._core.id ? -1 : 1;
145
- });
146
-
147
- let out = [];
148
- while (source_sorted.length>0 || target_sorted.length>0) {
149
- let src = this.firstObject(source_sorted);
150
- let trg = this.firstObject(target_sorted);
151
- if (trg===null) {
152
- // source にしか無いケース
153
- if (src.visible!==false)
154
- out.push(this.element.makeElement(src));
155
- source_sorted.shift();
156
- } else if (src===null) {
157
- // target にしか無いやつは削除する。
158
- target_sorted.shift();
159
- } else if (src.id === trg._core.id) {
160
- out.push(this.element.mergeCore(src, trg));
161
- source_sorted.shift();
162
- target_sorted.shift();
163
- } else if (src.id < trg._core.id) {
164
- // source にしか無いケース
165
- out.push(this.element.makeElement(src));
166
- source_sorted.shift();
167
- } else {
168
- // target にしか無いやつは削除する。
169
- target_sorted.shift();
170
- }
171
- }
172
-
173
- return out;
194
+ /**
195
+ * 根の要素を作る。data() のたびに、入力から作り直す(0.3.0 から。#195)。
196
+ *
197
+ * 0.2.1 までは、前の要素と突き合わせて(merge)いたが、入力の配列を並べ替えたうえで空にし、
198
+ * 同じ id(か id の無い)要素を 2 回目に渡すと TypeError で落ちていた。
199
+ *
200
+ * - 入力の配列は書き換えない(並べ替えは複製に対して行う)
201
+ * - 描く順番は、0.2.1 の 1 回目と同じ id の昇順(入力の順ではない)。比べ方も同じ
202
+ * - visible: false の根の要素は作らない(子は Element.makeElement が除く)
203
+ * - 自動の番号(id の無い要素・ラベル・アイコン)は、毎回 1 から振り直す
204
+ */
205
+ sortRoots (list) {
206
+ return [...(list || [])].sort((a, b) => { return a.id<b.id ? -1 : 1; });
174
207
  }
175
208
  makeElements (list) {
176
- let new_list = this.merge(list, this._elements.list);
209
+ this.element = new Element();
210
+
211
+ const new_list = this.sortRoots(list)
212
+ .filter(src => src.visible!==false)
213
+ .map(src => this.element.makeElement(src));
177
214
 
178
215
  let pool = { ht: {}, list: [] };
179
216
 
@@ -211,6 +248,11 @@ export default class Rectum extends Colon {
211
248
 
212
249
  return out;
213
250
  }
251
+ /**
252
+ * 描く。描いてあるものを消してから、要素を描き直す(0.3.0 から。#195)。
253
+ * 各要素の描画は enter だけで、消す・更新する処理を持たないので、
254
+ * data() で要素を作り直しても、描いてあるものを消さないと図が変わらない(rotation() と同じやり方)。
255
+ */
214
256
  draw () {
215
257
  const data = this.flatten(this._elements.list);
216
258
  const place = this.layer('foreground');
@@ -218,7 +260,36 @@ export default class Rectum extends Colon {
218
260
  if (!place)
219
261
  return;
220
262
 
263
+ place.selectAll('*').remove();
264
+
221
265
  this.element.draw(place, data, this.projector());
266
+
267
+ this.attachElementEvents(place);
268
+ }
269
+ /**
270
+ * 要素のクリック・右クリックを知らせる(callbacks.element。0.3.0 から)。受けるのは要素のグループ(g.frame など)で、
271
+ * ラベルとアイコンは、その中にある。入れ子の要素は DOM では入れ子にならず、子が親より後(上)に描かれるので、
272
+ * いちばん内側の要素が受ける。コールバックがあるときだけ、カーソルを pointer にする。
273
+ * Ctrl + ドラッグで回転させた直後のクリックは知らせない。
274
+ */
275
+ attachElementEvents (place) {
276
+ const { click, contextmenu } = this._callbacks.element;
277
+
278
+ if (!click && !contextmenu)
279
+ return;
280
+
281
+ const groups = place.selectAll(ELEMENT_GROUPS).style('cursor', 'pointer');
282
+
283
+ if (click)
284
+ groups.on('click', (event, d) => {
285
+ if (this._rotate_moved)
286
+ return;
287
+
288
+ notify(click, event, d._core, d);
289
+ });
290
+
291
+ if (contextmenu)
292
+ groups.on('contextmenu', (event, d) => notify(contextmenu, event, d._core, d));
222
293
  }
223
294
  /* **************************************************************** *
224
295
  * Data