-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdir-interactable.js
418 lines (353 loc) · 13.2 KB
/
dir-interactable.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
(function(ng) {
ng.app.
directive("dirInteractable", ["$document", "$timeout",
function($document, $timeout) {
return {
template: [
'<div id="{{settings.id}}" class="i-control" ng-class="{\'i-control-default\': vm.default, \'i-control-selected\': vm.selected, \'i-control-move-enabled\': vm.move_enabled}">',
'\t<div ng-class="{\'shown touchBox\': vm.selected, \'i-resizable-handle i-resizable-handle-nw\': vm.resize_enabled, \'small-content\': vm.is_small_content}"></div>',
'\t<div ng-class="{\'shown touchBox\': vm.selected, \'i-resizable-handle i-resizable-handle-ne\': vm.resize_enabled, \'small-content\': vm.is_small_content}"></div>',
'\t<div ng-class="{\'shown touchBox\': vm.selected, \'i-resizable-handle i-resizable-handle-se\': vm.resize_enabled, \'small-content\': vm.is_small_content}"></div>',
'\t<div ng-class="{\'shown touchBox\': vm.selected, \'i-resizable-handle i-resizable-handle-sw\': vm.resize_enabled, \'small-content\': vm.is_small_content}"></div>',
'\t<div ng-class="{\'i-rotatable-handle\': vm.rotate_enabled, \'shown\': vm.selected}"></div>',
'\t<div class="i-content" ng-transclude></div>',
'</div>'
].join("\n"),
scope: {
"_options": "=dirInteractable"
},
restrict: "A",
replace: true,
transclude: true,
link: function(scope, elem/*, attrs*/) {
var defaults = {
constrain_ar: false,
enabled: true,
move_enabled: true,
resize_enabled: true,
rotate_enabled: true,
id: ""+(new Date).getTime(),
width: 100,
height: 100,
x: 100,
y: 100,
angle: 0
};
scope.settings = ng.extend(defaults, scope._options);
/* initialize control */
var control = {
id: scope.settings.id,
width: scope.settings.width,
height: scope.settings.height,
x: scope.settings.x,
y: scope.settings.y,
angle: scope.settings.angle,
aspect_ratio: scope.settings.width / scope.settings.height,
selected: false,
offset: {
x: 0,
y: 0
}
};
/* scope linked properties */
scope.vm = {};
Object.defineProperty(scope.vm, "default", {
get: function() {
return !control.selected;
}
});
Object.defineProperty(scope.vm, "selected", {
get: function() {
return control.selected && scope.settings.enabled;
}
});
Object.defineProperty(scope.vm, "move_enabled", {
get: function() {
return control.selected && scope.settings.move_enabled;
}
});
Object.defineProperty(scope.vm, "resize_enabled", {
get: function() {
return scope.settings.resize_enabled;
}
});
Object.defineProperty(scope.vm, "rotate_enabled", {
get: function() {
return scope.settings.rotate_enabled;
}
});
Object.defineProperty(scope.vm, "is_small_content", {
get: function() {
return control.width < 26 || control.height < 26;
}
});
/* set up initial state, disable browser dragging images */
elem.
css({"position": "absolute"}).
add(elem.find("img")).
attr("draggable", "false").
attr("unselectable", "on").
addClass("unselectable").
on("dragstart", function() { return false; } );
/* timeout to give elements a cycle to render */
$timeout(function() {
set_position();
set_rotation(control.angle);
}, 100);
/* if enabled, set up events */
if (scope.settings.enabled) {
elem.
off("mousedown.{0} touchstart.{0}".format(control.id)).
on("mousedown.{0} touchstart.{0}".format(control.id), mousedown);
$document.
off("mouseup.{0} touchend.{0}".format(control.id)).
on("mouseup.{0} touchend.{0}".format(control.id), function(e) {
$document.off("mousemove.{0} touchmove.{0}".format(control.id));
}).
off("mousedown.{0} touchstart.{0}".format(control.id)).
on("mousedown.{0} touchstart.{0}".format(control.id), function(e) {
var selected = e.target.id === control.id || ng.element(e.target).closest(".i-control").attr("id") === control.id;
if (!selected && control.selected) {
scope.$root.$apply_safe(function() {
control.selected = false;
});
}
});
}
/* utility functions */
var set_rotation = function(angle) {
elem.css({"transform": "rotate({0}rad)".format(angle)});
};
var set_position = function() {
// have to round or Chrome (osX) will show remnants when dragging...
if (control.width < 10) { // width/height must be > 10
control.width = 10;
}
if (control.height < 10) {
control.height = 10;
}
elem.css({
"width": Math.round(control.width),
"height": Math.round(control.height),
"left": Math.round(control.x),
"top": Math.round(control.y)
});
};
var get_rotation_offset = function() {
if (control.angle) {
set_rotation(0);
}
var offset = elem.offset();
if (control.angle) {
set_rotation(control.angle);
}
return offset;
};
var to_degrees = function(radians) {
return (radians * (180 / Math.PI)) % 360;
};
var to_radians = function(degrees) {
return (degrees % 360) * (Math.PI / 180);
};
var get_element_center = function() {
var offset = get_rotation_offset();
return {
x: offset.left + control.width / 2,
y: offset.top + control.height / 2
}
};
var get_mouse_delta = function(e) {
return {
x: e.pageX - control.offset.x,
y: e.pageY - control.offset.y
};
};
var calculate_delta = function(e) {
var delta = get_mouse_delta(e);
set_offset(e);
return get_rotated_delta(delta);
};
var get_rotated_delta = function(delta) {
var dWidth = {x: delta.x * Math.cos(control.angle), y: -delta.x * Math.sin(control.angle)};
var dHeight = {x: delta.y * Math.sin(control.angle), y: delta.y * Math.cos(control.angle)};
return {
x: dWidth.x + dHeight.x,
y: dWidth.y + dHeight.y
};
};
var set_offset = function(e) {
control.offset = {
x: e.pageX,
y: e.pageY
};
};
var check_snap = function() {
var deg = to_degrees(control.angle);
var mod90 = Math.abs(deg) % 90;
if (mod90 > 85 || mod90 < 5) {
control.angle = to_radians(Math.round(deg / 90) * 90);
}
};
var get_event_location = function(e) {
return typeof e.pageX != "undefined" ? e : e.originalEvent.changedTouches[0];
};
/* event handlers */
function mousedown(e) {
if (!control.selected) {
scope.$root.$apply_safe(function() {
control.selected = true;
});
}
else {
var classes = e.target.className;
var loc = get_event_location(e);
if (classes.match(/i\-rotatable\-handle/gi)) {
// rotate
var center = get_element_center();
var start_from_center = {
x: loc.pageX - center.x,
y: loc.pageY - center.y
};
var start_angle = Math.atan2(start_from_center.y, start_from_center.x);
var element_start_angle = control.angle;
$document.
off("mousemove.{0} touchmove.{0}".format(control.id)).
on("mousemove.{0} touchmove.{0}".format(control.id), function(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var center = get_element_center();
var from_center = {
x: loc.pageX - center.x,
y: loc.pageY - center.y
};
var angle = Math.atan2(from_center.y, from_center.x);
control.angle = angle - start_angle + element_start_angle;
// snap to 90's from within 5 degrees
check_snap();
set_rotation(control.angle);
});
}
else if (classes.match(/i\-resizable\-handle\-(..)/gi)) {
var handle = RegExp.$1.toLowerCase();
// resize
control.offset = {
x: loc.pageX,
y: loc.pageY
};
switch (handle) {
case "ne":
$document.
off("mousemove.{0} touchmove.{0}".format(control.id)).
on("mousemove.{0} touchmove.{0}".format(control.id), ne_resize);
break;
case "se":
$document.
off("mousemove.{0} touchmove.{0}".format(control.id)).
on("mousemove.{0} touchmove.{0}".format(control.id), se_resize);
break;
case "sw":
$document.
off("mousemove.{0} touchmove.{0}".format(control.id)).
on("mousemove.{0} touchmove.{0}".format(control.id), sw_resize);
break;
case "nw":
$document.
off("mousemove.{0} touchmove.{0}".format(control.id)).
on("mousemove.{0} touchmove.{0}".format(control.id), nw_resize);
break;
default:
console.error("Invalid Handle: {0}".format(handle));
break;
}
}
else {
// drag
control.offset = {
x: loc.pageX,
y: loc.pageY
};
if (scope.settings.move_enabled) {
$document
.off("mousemove.{0} touchmove.{0}".format(control.id))
.on("mousemove.{0} touchmove.{0}".format(control.id), drag);
}
}
}
};
function ne_resize(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var delta = calculate_delta(loc);
if (scope.settings.constrain_ar) {
delta.x = -delta.y * control.aspect_ratio;
}
control.width = control.width + delta.x;
control.height = control.height - delta.y;
control.x = control.x + delta.y * Math.sin(-control.angle) - (0.5 * delta.y * Math.sin(-control.angle)) - delta.x * 0.5 * (1 - Math.cos(control.angle));
control.y = control.y + delta.y * Math.cos(-control.angle) + (0.5 * delta.y * (1 - Math.cos(-control.angle))) + (0.5 * delta.x * Math.sin(control.angle));
set_position();
};
function nw_resize(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var delta = calculate_delta(loc);
if (scope.settings.constrain_ar) {
delta.x = delta.y * control.aspect_ratio;
}
control.width = control.width - delta.x;
control.height = control.height - delta.y;
control.x = control.x + delta.x * Math.cos(control.angle) + delta.y * Math.sin(-control.angle) + (0.5 * delta.x * (1-Math.cos(control.angle))) - (0.5 * delta.y * Math.sin(-control.angle));
control.y = control.y + delta.y * Math.cos(-control.angle) + delta.x * Math.sin(control.angle) + (0.5 * delta.y * (1-Math.cos(-control.angle))) - (0.5 * delta.x * Math.sin(control.angle));
set_position();
};
function se_resize(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var delta = calculate_delta(loc);
if (scope.settings.constrain_ar) {
delta.x = delta.y * control.aspect_ratio;
}
control.width = control.width + delta.x;
control.height = control.height + delta.y;
control.x = control.x + 0.5 * delta.y * Math.sin(-control.angle) - (0.5 * delta.x * (1 - Math.cos(control.angle)));
control.y = control.y + 0.5 * delta.x * Math.sin(control.angle) - (0.5 * delta.y * (1 - Math.cos(-control.angle)));
set_position();
};
function sw_resize(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var delta = calculate_delta(loc);
if (scope.settings.constrain_ar) {
delta.x = -delta.y * control.aspect_ratio;
}
control.width = control.width - delta.x;
control.height = control.height + delta.y;
control.x = control.x + delta.x * Math.cos(control.angle) + delta.y * 0.5 * Math.sin(-control.angle) + (0.5 * delta.x * (1 - Math.cos(control.angle)));
control.y = control.y + delta.x * Math.sin(control.angle) - (0.5 * delta.y * (1 - Math.cos(-control.angle))) - 0.5 * delta.x * Math.sin(control.angle);
set_position();
};
function drag(e) {
e.preventDefault(); // prevent scrolling on touch devices
var loc = get_event_location(e);
var delta = get_mouse_delta(loc);
set_offset(loc);
control.x += delta.x;
control.y += delta.y;
set_position();
};
scope.$on("$destroy", function() {
// remove handlers
// -- one or more space-separated event types
elem.
off("mousedown.{0} mouseup.{0}".format(control.id));
elem.
off("touchstart.{0}".format(control.id));
$document.
off("mousedown.{0} mouseup.{0} click.{0}".format(control.id));
$document.
off("touchstart.{0} touchend.{0} touchmove.{0}".format(control.id));
});
}
}
}]);
})(window.angular);