source: gs3-extensions/tabletop-dl/trunk/winbox-interact.js@ 37572

Last change on this file since 37572 was 37572, checked in by davidb, 14 months ago

Now with css theme selector in control bar

File size: 9.8 KB
Line 
1
2function dragRestrictionRect(x,y,interactable_elem)
3{
4 // This function effectively does the same as restrictEdges { outer: 'parent', ... }
5 // but ....
6 // Upgraded to cope with the more complicated case where the interactable has
7 // been rotated and scaled. Without the extra checks, the 'outer' div that is
8 // used for a rotated and scale interactable has is the one for the original element
9 // prior to any scale or rotation applied to it (which is applied to .scale-element,
10 // which is a child of the outer element)
11
12
13 let elem = interactable_elem.element;
14 let parent_elem = elem.parentElement;
15 //console.log("parent elem:");
16 //console.log(parent_elem);
17
18 let parent_rect = parent_elem.getBoundingClientRect();
19 let restrict_to_rect = {
20 left: parent_rect.left,
21 right: parent_rect.right,
22 top: parent_rect.top,
23 bottom: parent_rect.bottom
24 };
25
26
27 let delta_xl = x - restrict_to_rect.left;
28 let delta_xr = restrict_to_rect.right - x;
29 let delta_yt = y - restrict_to_rect.top;
30 let delta_yb = restrict_to_rect.bottom - y;
31
32 let dragged_rect;
33
34 let $dragged_elem = $(elem).find('.scale-element');
35 if ($dragged_elem.length>0) {
36 dragged_rect = $dragged_elem[0].getBoundingClientRect();
37 }
38 else {
39 dragged_rect = elem.getBoundingClientRect();
40 }
41
42 //console.log(dragged_rect);
43 //console.log("restrict to:");
44 //console.log(restrict_to_rect);
45
46 // test bounds of dragged_rect, to restrict_to_rect
47 // factor in any overshoots in the encoded rect returned
48
49 if ((dragged_rect.left >= restrict_to_rect.left) || (dragged_rect.right <= restrict_to_rect.right)) {
50 // want to avoid situation where for dragged_rect both left and right are
51 // outside of the restrict_to_rect
52
53 if (dragged_rect.left < restrict_to_rect.left) {
54 // encode how much the overshoot it
55 restrict_to_rect.left += (restrict_to_rect.left - dragged_rect.left);
56
57 // Now factor in the offset that comes from the supplied (x,y);
58 restrict_to_rect.left += (x - parent_rect.left);
59 restrict_to_rect.right += (x - parent_rect.left);
60
61 }
62 if (dragged_rect.right > restrict_to_rect.right) {
63 // encode how much the overshoot it
64 restrict_to_rect.right -= (dragged_rect.right - restrict_to_rect.right);
65
66 // Now factor in the offset that comes from the supplied (x,y);
67 restrict_to_rect.left -= (parent_rect.right - x) + 1;
68 restrict_to_rect.right -= (parent_rect.right - x) + 1;
69
70 }
71 }
72 if ((dragged_rect.top >= restrict_to_rect.top) || (dragged_rect.bottom <= restrict_to_rect.bottom)) {
73 // want to avoid situation where for dragged_rect both left and right are
74 // outside of the restrict_to_rect
75
76 if (dragged_rect.top < restrict_to_rect.top) {
77 // encode how much the overshoot it
78 restrict_to_rect.top += (restrict_to_rect.top - dragged_rect.top);
79
80 // Now factor in the offset that comes from the supplied (x,y);
81 restrict_to_rect.top += (y - parent_rect.top);
82 restrict_to_rect.bottom += (y - parent_rect.top);
83 }
84 if (dragged_rect.bottom > restrict_to_rect.bottom) {
85 // encode how much the overshoot it
86 restrict_to_rect.bottom -= (dragged_rect.bottom - restrict_to_rect.bottom);
87
88 // Now factor in the offset that comes from the supplied (x,y);
89 restrict_to_rect.top -= (parent_rect.bottom - y) + 1;
90 restrict_to_rect.bottom -= (parent_rect.bottom - y) + 1;
91 }
92 }
93
94 return restrict_to_rect;
95}
96
97
98function dragMoveListener(event)
99{
100 var target = event.target
101
102 // keep the dragged position in the data-x/data-y attributes
103 var x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx
104 var y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy
105
106 // translate the element
107 target.style.transform = 'translate(' + x + 'px, ' + y + 'px)'
108
109 // update the posiion attributes
110 target.setAttribute('data-x', x)
111 target.setAttribute('data-y', y)
112}
113
114function getCoreDraggableOptions()
115{
116 let core_draggable_options = {
117
118 //listeners: { move: dragMoveListener },
119
120 /*
121 onstart: function(event) {
122 let target = event.target;
123 },
124
125 */
126
127 onmove: dragMoveListener,
128
129 /*
130 onend: function(event) {
131 let target = event.target;
132 },
133 */
134
135
136 modifiers: [
137 interact.modifiers.restrict({
138 restriction: dragRestrictionRect,
139 endOnly: true
140 }),
141 ],
142
143 inertia: {
144 resistance: 6,
145 //minSpeed: 20,
146 //endSpeed: 10,
147 smoothEndDuration: 500,
148 allowResume: true
149 }
150 };
151
152 return core_draggable_options;
153}
154
155function getCoreResizableOptions()
156{
157 let core_resizable_options = {
158
159 preserveAspectRatio: false,
160 edges: { left: true, right: true, bottom: true, top: false },
161
162 listeners: {
163
164 moveNEWER_UNUSED: function (event) {
165 let { x, y } = event.target.dataset
166
167 x = (parseFloat(x) || 0) + event.deltaRect.left
168 y = (parseFloat(y) || 0) + event.deltaRect.top
169
170 Object.assign(event.target.style, {
171 width: `${event.rect.width}px`,
172 height: `${event.rect.height}px`,
173 transform: `translate(${x}px, ${y}px)`
174 })
175
176 Object.assign(event.target.dataset, { x, y })
177 },
178
179 move (event) {
180 var target = event.target
181 var x = (parseFloat(target.getAttribute('data-x')) || 0)
182 var y = (parseFloat(target.getAttribute('data-y')) || 0)
183 //var x = (parseFloat(target.getAttribute('data-x')) || target.style.clientLeft)
184 //var y = (parseFloat(target.getAttribute('data-y')) || target.style.clientTop)
185
186 // update the element's style
187 target.style.width = event.rect.width + 'px'
188 target.style.height = event.rect.height + 'px'
189
190 // translate when resizing from top or left edges
191 x += event.deltaRect.left
192 y += event.deltaRect.top
193
194 target.style.transform = 'translate(' + x + 'px,' + y + 'px)'
195
196 //var target_new_x_org = target.clientLeft + event.deltaRect.left;
197 //var target_new_y_org = target.clientTop + event.deltaRect.top;
198
199 //target.style.left = target_new_x_org + 'px';
200 //target.style.top = target_new_y_org + 'px';
201
202 //target.style.left = x + 'px';
203 //target.style.top = y + 'px';
204
205 target.setAttribute('data-x', x)
206 target.setAttribute('data-y', y)
207
208 }
209 },
210
211 modifiers: [
212 // keep the edges inside the parent
213 interact.modifiers.restrictEdges({
214 outer: 'parent',
215 endOnly: true
216 }),
217
218 // minimum size
219 interact.modifiers.restrictSize({
220 min: { width: 100, height: 50 }
221 })
222 ],
223
224 inertia: false
225 };
226
227 return core_resizable_options;
228
229}
230
231
232//function getCoreGesturableOptions(elem,angleScale)
233function getCoreGesturableOptions(elem)
234{
235 //let elem_angle_scale = {
236 let angleScale = {
237 angle: 0,
238 scale: 1
239 };
240
241 let core_gesturable_options = {
242 listeners: {
243 start (event) {
244 angleScale.angle -= event.angle;
245 },
246 move (event) {
247
248 let currentAngle = angleScale.angle + event.angle;
249 let currentScale = angleScale.scale * event.scale;
250
251 let $scale_elem = $(elem).find('.scale-element');
252
253 $scale_elem.css(
254 'transform',
255 'rotate(' + currentAngle + 'deg)' + 'scale(' + currentScale + ')'
256 );
257
258
259 dragMoveListener(event);
260 },
261 end (event) {
262 angleScale.angle = angleScale.angle + event.angle;
263 angleScale.scale = angleScale.scale * event.scale;
264 }
265 }
266 };
267
268 return core_gesturable_options;
269}
270
271
272// For a slightly different way to attach event listeners see
273// https://codepen.io/bxchang04/pen/XWmWbZQ
274
275/*
276$(document).ready(function() {
277
278 let draggable_options = getCoreDraggableOptions();
279 let resizable_options = getCoreResizableOptions();
280
281 draggable_options.autoScroll =true;
282
283 interact('.resize-drag')
284 .draggable(draggable_options)
285 .resizable(resizable_options)
286
287 var $drag_and_zoom_elems = $('.drag-and-zoom');
288
289 $drag_and_zoom_elems.each(function(index) {
290
291 let this_elem = this;
292
293
294 //let this_angle_scale = {
295 // angle: 0,
296 // scale: 1
297 //}
298
299
300 let gdraggable_options = getCoreDraggableOptions();
301 let gesturable_options = getCoreGesturableOptions(this_elem);//this_angle_scale);
302
303 interact(this_elem)
304 .gesturable(gesturable_options)
305 .draggable(gdraggable_options);
306
307 });
308});
309*/
310
311
312$(document).ready(function() {
313
314 let doc_url = "http://localhost:8383/greenstone3/library/collection/gs2mgdemo/document/HASHe3aae2159b0dda6553cee4";
315
316 const gs_primary_bg_color = $('#gs-css-root-primary').css('background-color');
317
318 $.ajax({
319 url: doc_url,
320 data: {
321 "excerptid": "gs_content",
322 "ed": "1"
323 }
324 })
325 .done(function(html_result) {
326
327
328 let win2 = new WinBox({
329 id: "my-winbox2",
330 title: 'Document: Drag, Scale and Rotate',
331 //class: [ "no-min", "no-max", "no-full", "no-resize", "no-move" ],
332 class: [ "no-min", "no-max", "no-full" ],
333 x: 1000,
334 y: 80,
335 width: '900px',
336 height: '600px',
337 border: "0.3em",
338 html: html_result
339 });
340
341 // user-select: none;
342 // touch-action: none;
343
344 let $win2 = $('#my-winbox2');
345 let $scale_element_div = $('<div>')
346 .attr("class","scale-element")
347 .css("pointer-events","all")
348 .css("position","relative")
349 .css("width","100%")
350 .css("height","100%")
351 .css("background-color", gs_primary_bg_color);
352
353 $scale_element_div.append($win2.children()).clone();
354
355 $win2.empty().append($scale_element_div);
356
357 $('#my-winbox2 .wb-body').addClass("winbox-info-window");
358
359
360 let this_elem = document.getElementById('my-winbox2');
361
362 /*
363 let this_angle_scale = {
364 angle: 0,
365 scale: 1
366 };
367 */
368
369 let gdraggable_options = getCoreDraggableOptions();
370 let gesturable_options = getCoreGesturableOptions(this_elem/*,this_angle_scale*/);
371
372 var my_winbox2 = interact('#my-winbox2')
373 .gesturable(gesturable_options)
374 .draggable(gdraggable_options);
375
376 $('#my-winbox2').css("pointer-events","none");
377
378 });
379});
Note: See TracBrowser for help on using the repository browser.