This repository has been archived by the owner on Dec 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 419
/
piece.js
270 lines (227 loc) · 7.72 KB
/
piece.js
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
cube.piece = function(board) {
var piece = {},
size = [8, 3],
position = [0, 0],
padding = 4;
var event = d3.dispatch(
"position",
"size",
"move",
"edit",
"serialize",
"deserialize"
);
var div = document.createElement("div"),
selection = d3.select(div),
transition = selection;
var selection = d3.select(div)
.attr("class", "piece");
if (mode == "edit") {
selection
.attr("tabindex", 1)
.on("keydown.piece", keydown)
.on("mousedown.piece", mousedrag)
.selectAll(".resize")
.data(["n", "e", "s", "w", "nw", "ne", "se", "sw"])
.enter().append("div")
.attr("class", function(d) { return "resize " + d; })
.on("mousedown.piece", mouseresize);
d3.select(window)
.on("keydown.piece", cube_piece_keydown)
.on("mousemove.piece", cube_piece_mousemove)
.on("mouseup.piece", cube_piece_mouseup);
}
board
.on("padding.piece", resize)
.on("squareSize.piece", resize);
event.on("position.piece", resize);
event.on("size.piece", resize);
event.on("deserialize.piece", deserialize);
function resize() {
var squareSize = board.squareSize(),
boardPadding = board.padding() | 0;
piece.transition()
.style("left", (padding + boardPadding + position[0] * squareSize) + "px")
.style("top", (padding + boardPadding + (1.5 + position[1]) * squareSize) + "px")
.style("width", size[0] * squareSize - 2 * padding + 1 + "px")
.style("height", size[1] * squareSize - 2 * padding + 1 + "px");
}
function deserialize(x) {
piece
.size(x.size)
.position(x.position);
}
function keydown() {
if (d3.event.target !== this) return d3.event.stopPropagation();
if (cube_piece_dragPiece) return;
if (d3.event.keyCode === 8) {
board.remove(piece);
d3.event.preventDefault();
}
}
piece.node = function() {
return div;
};
piece.on = function(type, listener) {
event.on(type, listener);
return piece;
};
piece.size = function(x) {
if (!arguments.length) return size;
event.size.call(piece, size = x);
return piece;
};
piece.innerSize = function() {
var squareSize = board.squareSize();
return [
size[0] * squareSize - 2 * padding,
size[1] * squareSize - 2 * padding
];
};
piece.position = function(x) {
if (!arguments.length) return position;
event.position.call(piece, position = x);
return piece;
};
piece.toJSON = function() {
var x = {id: piece.id, size: size, position: position};
event.serialize.call(piece, x);
return x;
};
piece.fromJSON = function(x) {
event.deserialize.call(piece, x);
return piece;
};
piece.edit = function() {
event.edit.call(piece);
return piece;
};
function mousedrag() {
if (/^(INPUT|TEXTAREA|SELECT)$/.test(d3.event.target.tagName)) return;
if (d3.event.target === this && d3.event.altKey) {
cube_piece_dragPiece = piece.copy().fromJSON(piece.toJSON());
cube_piece_dragPiece.node().focus();
} else {
d3.select(this).transition(); // cancel transition, if any
this.parentNode.appendChild(this).focus();
cube_piece_dragPiece = piece;
}
cube_piece_dragOrigin = [d3.event.pageX, d3.event.pageY];
cube_piece_dragPosition = position.slice();
cube_piece_dragSize = size.slice();
cube_piece_dragBoard = board;
cube_piece_mousemove();
}
function mouseresize(d) {
cube_piece_dragResize = d;
}
piece.transition = function(x) {
if (!arguments.length) return transition;
if (x == null) {
transition = selection;
} else {
transition = x.select(function() { return div; });
d3.timer(function() {
event.move.call(piece);
return transition = selection;
});
}
return piece;
};
piece.focus = function() {
selection.classed("active", true);
return piece;
};
piece.blur = function() {
selection.classed("active", false);
return piece;
};
resize();
return piece;
};
cube.piece.type = {};
var cube_piece_dragPiece,
cube_piece_dragBoard,
cube_piece_dragOrigin,
cube_piece_dragPosition,
cube_piece_dragSize,
cube_piece_dragResize;
function cube_piece_mousePosition() {
var squareSize = cube_piece_dragBoard.squareSize();
return cube_piece_dragResize ? [
cube_piece_dragPosition[0] + /w$/.test(cube_piece_dragResize) * Math.min(cube_piece_dragSize[0] - 5, (d3.event.pageX - cube_piece_dragOrigin[0]) / squareSize),
cube_piece_dragPosition[1] + /^n/.test(cube_piece_dragResize) * Math.min(cube_piece_dragSize[1] - 3, (d3.event.pageY - cube_piece_dragOrigin[1]) / squareSize)
] : [
cube_piece_dragPosition[0] + (d3.event.pageX - cube_piece_dragOrigin[0]) / squareSize,
cube_piece_dragPosition[1] + (d3.event.pageY - cube_piece_dragOrigin[1]) / squareSize
];
}
function cube_piece_mouseSize() {
var squareSize = cube_piece_dragBoard.squareSize();
return cube_piece_dragResize ? [
Math.max(5, cube_piece_dragSize[0] + (/e$/.test(cube_piece_dragResize) ? 1 : /w$/.test(cube_piece_dragResize) ? -1 : 0) * (d3.event.pageX - cube_piece_dragOrigin[0]) / squareSize),
Math.max(3, cube_piece_dragSize[1] + (/^s/.test(cube_piece_dragResize) ? 1 : /^n/.test(cube_piece_dragResize) ? -1 : 0) * (d3.event.pageY - cube_piece_dragOrigin[1]) / squareSize)
] : cube_piece_dragSize;
}
function cube_piece_clamp(position, size) {
var boardSize = cube_piece_dragBoard.size();
if (cube_piece_dragResize) {
if (/e$/.test(cube_piece_dragResize)) {
size[0] = Math.max(0, Math.min(boardSize[0] - position[0], Math.round(size[0])));
} else if (/w$/.test(cube_piece_dragResize)) {
size[0] = Math.round(size[0] + position[0] - (position[0] = Math.max(0, Math.min(boardSize[0], Math.round(position[0])))));
}
if (/^s/.test(cube_piece_dragResize)) {
size[1] = Math.max(0, Math.min(boardSize[1] - position[1], Math.round(size[1])));
} else if (/^n/.test(cube_piece_dragResize)) {
size[1] = Math.round(size[1] + position[1] - (position[1] = Math.max(0, Math.min(boardSize[1], Math.round(position[1])))));
}
} else {
position[0] = Math.max(0, Math.min(boardSize[0] - size[0], Math.round(position[0])));
position[1] = Math.max(0, Math.min(boardSize[1] - size[1], Math.round(position[1])));
}
}
function cube_piece_mousemove() {
if (cube_piece_dragPiece) {
var position = cube_piece_mousePosition(),
size = cube_piece_mouseSize();
cube_piece_dragPiece
.position(position.slice())
.size(size.slice());
cube_piece_clamp(position, size);
var x0 = position[0],
y0 = position[1],
x1 = x0 + size[0],
y1 = y0 + size[1];
d3.select(cube_piece_dragBoard.node()).selectAll(".squares rect")
.classed("shadow", function(d, i) { return d.x >= x0 && d.x < x1 && d.y >= y0 && d.y < y1; });
d3.event.preventDefault();
}
}
function cube_piece_mouseup() {
if (cube_piece_dragPiece) {
var position = cube_piece_mousePosition(),
size = cube_piece_mouseSize();
cube_piece_clamp(position, size);
d3.select(cube_piece_dragBoard.node()).selectAll(".squares rect")
.classed("shadow", false);
cube_piece_dragPiece
.transition(d3.transition().ease("elastic").duration(500))
.position(position)
.size(size);
cube_piece_dragPiece =
cube_piece_dragBoard =
cube_piece_dragEvent =
cube_piece_dragOrigin =
cube_piece_dragPosition =
cube_piece_dragSize =
cube_piece_dragResize = null;
d3.event.preventDefault();
}
}
// Disable delete as the back key, since we use it to delete pieces.
function cube_piece_keydown() {
if (d3.event.keyCode == 8) {
d3.event.preventDefault();
}
}