@cocreate/resize 1.5.11 → 1.5.13

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.
package/src/index.js CHANGED
@@ -1,456 +1,456 @@
1
- // "use strict";
2
- import observer from "@cocreate/observer";
3
- import "./index.css";
4
-
5
- const EVENTS = ['mousemove touchmove', 'mousedown touchstart', 'mouseup touchend'];
6
- const DIRECTIONS = ['left', 'right', 'top', 'bottom'];
7
-
8
- const styleEl = document.createElement("style");
9
- styleEl.setAttribute('component', 'resize');
10
- document.head.appendChild(styleEl);
11
- const styleSheet = styleEl.sheet;
12
-
13
- const coCreateResize = {
14
- selector: '', //'.resize',
15
- resizers: [],
16
- resizeWidgets: [],
17
-
18
- init: function(handleObj) {
19
- for (var handleKey in handleObj)
20
- if (handleObj.hasOwnProperty(handleKey) && handleKey == 'selector')
21
- this.selector = handleObj[handleKey];
22
-
23
- this.resizers = document.querySelectorAll(this.selector);
24
- var _this = this;
25
- this.resizers.forEach(function(resize, idx) {
26
- let resizeWidget = new CoCreateResize(resize, handleObj);
27
- _this.resizeWidgets[idx] = resizeWidget;
28
- });
29
- },
30
-
31
- initElement: function(target) {
32
- let resizeWidget = new CoCreateResize(target, {
33
- dragLeft: "[resize='left']",
34
- dragRight: "[resize='right']",
35
- dragTop: "[resize='top']",
36
- dragBottom: "[resize='bottom']"
37
- });
38
- this.resizeWidgets[0] = resizeWidget;
39
- }
40
- };
41
-
42
- function CoCreateResize(resizer, options) {
43
- this.resizeWidget = resizer;
44
- this.cornerSize = 10;
45
- this.init(options);
46
- }
47
-
48
- CoCreateResize.prototype = {
49
- init: function(handleObj) {
50
- if (this.resizeWidget) {
51
- this.initDrags = [];
52
- this.checkCorners = [];
53
- this.doDrags = [];
54
- this.Drags = [];
55
-
56
- //this is for grid
57
- this.isGrid = this.detectGrid();
58
- this.gridWidth = 0;
59
- this.gridHeight = 0;
60
- this.limitSpan = 0;
61
- this.widthSpan = 0;
62
- this.heightSpan = 0;
63
- this.originClassAttribute = this.resizeWidget.getAttribute('class');
64
-
65
- DIRECTIONS.map(d => {
66
- this.Drags[d] = this.resizeWidget.querySelector(handleObj['drag' + d.charAt(0).toUpperCase() + d.slice(1)]);
67
- });
68
-
69
- this.bindListeners();
70
- this.initResize();
71
-
72
- //this is just for grid system
73
- this.getGridProperty();
74
- window.addEventListener('resize', this.checkGridColumns.bind(this));
75
- }
76
- },
77
-
78
- getGridProperty: function() {
79
- if (!this.resizeWidget.parentNode) return;
80
- let compStyles = window.getComputedStyle(this.resizeWidget.parentNode);
81
- let gridColumns = compStyles.gridTemplateColumns;
82
- let height = compStyles.gridAutoRows;
83
-
84
- if (gridColumns !== 'none') {
85
- let width = gridColumns.split(' ');
86
- this.limitSpan = width.length;
87
- this.gridColumnGap = Number.parseInt(compStyles.gridColumnGap);
88
- this.gridRowGap = Number.parseInt(compStyles.gridRowGap);
89
- this.gridWidth = Number.parseFloat(width[0]);
90
- this.gridHeight = Number.parseInt(height);
91
- }
92
- },
93
-
94
- checkGridColumns: function() {
95
- if (typeof this.resizeWidget !== 'undefined' && this.resizeWidget.parentNode)
96
- {
97
- let compStylesOfParent = window.getComputedStyle(this.resizeWidget.parentNode);
98
- let spans = 0;
99
- let gridColumns = compStylesOfParent.gridTemplateColumns.split(' ');
100
- let temp = gridColumns[0];
101
- gridColumns.map(v => {
102
- if (v == temp) spans ++;
103
- });
104
- this.limitSpan = spans;
105
- if (this.resizeWidget.style['grid-column-end'])
106
- {
107
- let curClassAttributes = this.resizeWidget.getAttribute('class').split(' ');
108
- let prevColumnState = curClassAttributes[curClassAttributes.length - 1].split('_');
109
- let prevSpan = Number.parseInt(prevColumnState[1]);
110
- let curColumnState = this.resizeWidget.style['grid-column-end'].split(' ');
111
- let curSpan = Number.parseInt(curColumnState[1]);
112
-
113
- let span = 0;
114
- if (this.limitSpan >= prevSpan) span = prevSpan;
115
- else if (prevSpan > this.limitSpan) span = this.limitSpan;
116
- else span = Math.min(curSpan, this.limitSpan);
117
-
118
- this.resizeWidget.style['grid-column-end'] = 'span ' + span;
119
- }
120
- }
121
- },
122
-
123
- initResize: function() {
124
- DIRECTIONS.map(d => {
125
- if (this.Drags[d])
126
- this.addListenerMulti(this.Drags[d], EVENTS[0], this.checkCorners[d]);
127
- });
128
- },
129
-
130
- checkDragImplementation: function(e, from, to, offset, fcur, scur) {
131
- if (e.touches)
132
- e = e.touches[0];
133
- let other;
134
- if (to == 'top')
135
- other = 'bottom'
136
- else if (to == 'bottom')
137
- other = 'top'
138
- else if (to == 'left')
139
- other = 'right'
140
- else if (to == 'right')
141
- other = 'left'
142
- this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[from]);
143
- this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[to]);
144
- this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[other]);
145
- this.addListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[from]);
146
-
147
- if (offset < this.cornerSize && this.Drags[to]) {
148
- this.Drags[from].style.cursor = fcur;
149
- this.addListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[to]);
150
- }
151
- else {
152
- this.Drags[from].style.cursor = scur;
153
- }
154
- },
155
-
156
- initDrag: function(e, idx) {
157
- e.preventDefault();
158
-
159
- if (!this.resizeWidget.hasAttribute('resizing')){
160
- this.resizeWidget.setAttribute('resizing', '')
161
- }
162
-
163
- let selector = document.defaultView.getComputedStyle(this.resizeWidget);
164
- styleSheet.insertRule('iframe {pointer-events: none}', 0)
165
-
166
- if (idx == 'top' || idx == 'bottom') {
167
- this.startTop = parseInt(selector.top, 10);
168
- this.startHeight = parseInt(selector.height, 10);
169
-
170
- if (e.touches)
171
- this.startY = e.touches[0].clientY;
172
- else
173
- this.startY = e.clientY;
174
- }
175
- else {
176
- this.startLeft = parseInt(selector.left, 10);
177
- this.startWidth = parseInt(selector.width, 10);
178
-
179
- if (e.touches)
180
- this.startX = e.touches[0].clientX;
181
- else
182
- this.startX = e.clientX;
183
- }
184
-
185
- this.addListenerMulti(document.documentElement, EVENTS[0], this.doDrags[idx]);
186
- this.addListenerMulti(document.documentElement, EVENTS[2], this.stopDrag);
187
- },
188
-
189
- initTopDrag: function(e) {
190
- this.initDrag(e, 'top');
191
- },
192
- initBottomDrag: function(e) {
193
- this.initDrag(e, 'bottom');
194
- },
195
- initLeftDrag: function(e) {
196
- this.initDrag(e, 'left');
197
-
198
- },
199
- initRightDrag: function(e) {
200
- this.initDrag(e, 'right');
201
- },
202
-
203
- //this is just for grid system
204
- setRowEnd: function(height) {
205
- if (this.gridHeight) {
206
- this.heightSpan = Math.ceil((height - (this.heightSpan - 1) * this.gridRowGap) / this.gridHeight );
207
- this.resizeWidget.style['grid-row-end'] = 'span ' + this.heightSpan;
208
- this.collaborate({rowEnd: this.heightSpan})
209
- }
210
- },
211
-
212
- doTopDrag: function(e) {
213
- let top, height;
214
-
215
- if (e.touches)
216
- e = e.touches[0];
217
- top = this.startTop + e.clientY - this.startY;
218
- height = this.startHeight - e.clientY + this.startY;
219
-
220
- if (height < 10)
221
- return;
222
- if (!this.isGrid) {
223
- this.resizeWidget.style.top = top + 'px';
224
- this.collaborate({top, height})
225
- }
226
- this.resizeWidget.style.height = height + 'px';
227
- this.setRowEnd(height);
228
- },
229
-
230
-
231
- doBottomDrag: function(e) {
232
- let height;
233
-
234
- if (e.touches)
235
- e = e.touches[0];
236
-
237
- height = this.startHeight + e.clientY - this.startY;
238
-
239
- if (height < 10)
240
- return;
241
- if (!this.isGrid)
242
- this.collaborate({height})
243
-
244
- this.resizeWidget.style.height = height + 'px';
245
- this.setRowEnd(height);
246
- },
247
-
248
- //this is just for grid system
249
- setColumnEnd: function(width) {
250
- if (this.gridWidth) {
251
- let spans = Math.ceil((width - (this.widthSpan - 1) * this.gridColumnGap) / this.gridWidth);
252
- this.widthSpan = (spans > this.limitSpan) ? this.limitSpan : spans;
253
- this.resizeWidget.style['grid-column-end'] = 'span ' + this.widthSpan;
254
- this.collaborate({columnEnd: this.widthSpan})
255
- }
256
- },
257
-
258
- doLeftDrag: function(e) {
259
- let left, width;
260
- if (e.touches)
261
- e = e.touches[0];
262
- left = this.startLeft + e.clientX - this.startX;
263
- width = this.startWidth - e.clientX + this.startX;
264
-
265
- if (width < 10)
266
- return;
267
- if (!this.isGrid) {
268
- this.resizeWidget.style.left = left + 'px';
269
- this.collaborate({left, width})
270
- }
271
-
272
- this.resizeWidget.style.width = width + 'px';
273
- this.setColumnEnd(width);
274
- },
275
-
276
-
277
- doRightDrag: function(e) {
278
- let width;
279
- if (e.touches)
280
- e = e.touches[0];
281
- width = this.startWidth + e.clientX - this.startX;
282
- if (width < 10)
283
- return;
284
-
285
- if (!this.isGrid)
286
- this.collaborate({width})
287
-
288
- this.resizeWidget.style.width = width + 'px';
289
- this.setColumnEnd(width);
290
- },
291
-
292
- detectGrid: function() {
293
- let isGrid = false;
294
- if (this.resizeWidget.parentNode) {
295
- let compStyles = window.getComputedStyle(this.resizeWidget.parentNode);
296
- isGrid = (compStyles.display === 'grid');
297
- }
298
- this.isGrid = isGrid;
299
- return isGrid;
300
- },
301
-
302
- stopDrag: function(e) {
303
- styleSheet.deleteRule(0)
304
- if (this.resizeWidget.hasAttribute('resizing')){
305
- this.resizeWidget.removeAttribute('resizing', '')
306
- }
307
-
308
- //this is just for grid system
309
- if (this.detectGrid()) {
310
- this.resizeWidget.style.width = null;
311
- this.resizeWidget.style.height = null;
312
- this.resizeWidget.style.left = null;
313
- this.resizeWidget.style.top = null;
314
-
315
- if (this.widthSpan) this.resizeWidget.setAttribute('class', this.originClassAttribute + ' grid-column-end:span_' + this.widthSpan);
316
- if (this.heightSpan) this.resizeWidget.setAttribute('class', this.originClassAttribute + ' grid-row-end:span_' + this.heightSpan);
317
- }
318
-
319
- DIRECTIONS.map(d => { this.removeListenerMulti(document.documentElement, EVENTS[0], this.doDrags[d]) });
320
- this.removeListenerMulti(document.documentElement, EVENTS[2], this.stopDrag);
321
- },
322
- checkLeftCorners: function(e) {
323
- let offset = e.clientY - this.getDistance(this.Drags['left'], true) + document.documentElement.scrollTop;
324
- if (offset > 10) {
325
- offset = this.getDistance(this.Drags['left'], true) + this.Drags['left'].clientHeight - e.clientY - document.documentElement.scrollTop;
326
- this.checkDragImplementation(e, 'left', 'bottom', offset, 'ne-resize', 'e-resize');
327
- } else {
328
- this.checkDragImplementation(e, 'left', 'top', offset, 'se-resize', 'e-resize');
329
- }
330
- },
331
- checkTopCorners: function(e) {
332
- let offset = e.clientX - this.getDistance(this.Drags['top'], false) + document.documentElement.scrollTop;
333
- if (offset > 10) {
334
- offset = this.getDistance(this.Drags['top'], false) + this.Drags['top'].clientWidth - e.clientX - document.documentElement.scrollLeft;
335
- this.checkDragImplementation(e, 'top', 'right', offset, 'ne-resize', 's-resize');
336
- } else {
337
- this.checkDragImplementation(e, 'top', 'left', offset, 'se-resize', 's-resize');
338
- }
339
- },
340
- checkBottomCorners: function(e) {
341
- let offset = e.clientX - this.getDistance(this.Drags['bottom'], false) + document.documentElement.scrollTop;
342
- if (offset > 10) {
343
- offset = this.getDistance(this.Drags['bottom'], false) + this.Drags['bottom'].clientWidth - e.clientX - document.documentElement.scrollLeft;
344
- this.checkDragImplementation(e, 'bottom', 'right', offset, 'se-resize', 's-resize');
345
- } else {
346
- this.checkDragImplementation(e, 'bottom', 'left', offset, 'ne-resize', 's-resize');
347
- }
348
- },
349
- checkRightCorners: function(e) {
350
- let offset = e.clientY - this.getDistance(this.Drags['right'], true) + document.documentElement.scrollTop;
351
- if (offset > 10) {
352
- offset = this.getDistance(this.Drags['right'], true) + this.Drags['right'].clientHeight - e.clientY - document.documentElement.scrollTop;
353
- this.checkDragImplementation(e, 'right', 'bottom', offset, 'se-resize', 'e-resize');
354
- } else {
355
- this.checkDragImplementation(e, 'right', 'top', offset, 'ne-resize', 'e-resize');
356
- }
357
- },
358
-
359
- bindListeners: function() {
360
- this.doDrags['left'] = this.doLeftDrag.bind(this);
361
- this.doDrags['top'] = this.doTopDrag.bind(this);
362
- this.doDrags['right'] = this.doRightDrag.bind(this);
363
- this.doDrags['bottom'] = this.doBottomDrag.bind(this);
364
-
365
- this.stopDrag = this.stopDrag.bind(this);
366
-
367
- this.checkCorners['left'] = this.checkLeftCorners.bind(this);
368
- this.checkCorners['top'] = this.checkTopCorners.bind(this);
369
- this.checkCorners['bottom'] = this.checkBottomCorners.bind(this);
370
- this.checkCorners['right'] = this.checkRightCorners.bind(this);
371
-
372
- this.initDrags['top'] = this.initTopDrag.bind(this);
373
- this.initDrags['left'] = this.initLeftDrag.bind(this);
374
- this.initDrags['bottom'] = this.initBottomDrag.bind(this);
375
- this.initDrags['right'] = this.initRightDrag.bind(this);
376
- },
377
-
378
- getDistance: function(elem, flag) {
379
- var location = 0;
380
- if (!elem) return;
381
- if (elem.offsetParent) {
382
- do {
383
- location += (flag) ? elem.offsetTop : elem.offsetLeft;
384
- elem = elem.offsetParent;
385
- } while (elem);
386
- }
387
- return location >= 0 ? location : 0;
388
- },
389
-
390
- // Bind multiiple events to a listener
391
- addListenerMulti: function(element, eventNames, listener) {
392
- var events = eventNames.split(' ');
393
- for (var i = 0, iLen = events.length; i < iLen; i++) {
394
- element.addEventListener(events[i], listener, { passive: false });
395
- }
396
- },
397
-
398
- // Remove multiiple events from a listener
399
- removeListenerMulti: function(element, eventNames, listener) {
400
- var events = eventNames.split(' ');
401
- for (var i = 0, iLen = events.length; i < iLen; i++) {
402
- element.removeEventListener(events[i], listener, { passive: false });
403
- }
404
- },
405
-
406
- collaborate: function({left, top, width, height, rowEnd, columnEnd}) {
407
- if (window.CoCreate && window.CoCreate.text) {
408
- let domTextEditor = this.resizeWidget.closest('[contenteditable]');
409
- if (domTextEditor) {
410
- if (left)
411
- CoCreate.text.setStyle({
412
- domTextEditor, target: this.resizeWidget, property: 'left', value: left + 'px'
413
- })
414
- if (top)
415
- CoCreate.text.setStyle({
416
- domTextEditor, target: this.resizeWidget, property: 'top', value: top + 'px'
417
- })
418
- if (width)
419
- CoCreate.text.setStyle({
420
- domTextEditor, target: this.resizeWidget, property: 'width', value: width + 'px'
421
- })
422
- if (height)
423
- CoCreate.text.setStyle({
424
- domTextEditor, target: this.resizeWidget, property: 'height', value: height + 'px'
425
- })
426
- if (rowEnd)
427
- CoCreate.text.setStyle({
428
- domTextEditor, target: this.resizeWidget, property: 'grid-row-end', value: 'span ' + rowEnd
429
- })
430
- if (columnEnd)
431
- CoCreate.text.setStyle({
432
- domTextEditor, target: this.resizeWidget, property: 'grid-column-end', value: 'span ' + columnEnd
433
- })
434
- }
435
- }
436
- }
437
- };
438
-
439
- observer.init({
440
- name: 'CoCreateResize',
441
- observe: ['addedNodes'],
442
- selector: "[resizable]:not([resizable='false'])",
443
- callback: function(mutation) {
444
- coCreateResize.initElement(mutation.target);
445
-
446
- }
447
- });
448
-
449
- coCreateResize.init({
450
- selector: "[resizable]:not([resizable='false'])",
451
- dragLeft: "[resize='left']",
452
- dragRight: "[resize='right']",
453
- dragTop: "[resize='top']",
454
- dragBottom: "[resize='bottom']",
455
- });
456
- export default coCreateResize;
1
+ // "use strict";
2
+ import observer from "@cocreate/observer";
3
+ import "./index.css";
4
+
5
+ const EVENTS = ['mousemove touchmove', 'mousedown touchstart', 'mouseup touchend'];
6
+ const DIRECTIONS = ['left', 'right', 'top', 'bottom'];
7
+
8
+ const styleEl = document.createElement("style");
9
+ styleEl.setAttribute('component', 'resize');
10
+ document.head.appendChild(styleEl);
11
+ const styleSheet = styleEl.sheet;
12
+
13
+ const coCreateResize = {
14
+ selector: '', //'.resize',
15
+ resizers: [],
16
+ resizeWidgets: [],
17
+
18
+ init: function(handleObj) {
19
+ for (var handleKey in handleObj)
20
+ if (handleObj.hasOwnProperty(handleKey) && handleKey == 'selector')
21
+ this.selector = handleObj[handleKey];
22
+
23
+ this.resizers = document.querySelectorAll(this.selector);
24
+ var _this = this;
25
+ this.resizers.forEach(function(resize, idx) {
26
+ let resizeWidget = new CoCreateResize(resize, handleObj);
27
+ _this.resizeWidgets[idx] = resizeWidget;
28
+ });
29
+ },
30
+
31
+ initElement: function(target) {
32
+ let resizeWidget = new CoCreateResize(target, {
33
+ dragLeft: "[resize='left']",
34
+ dragRight: "[resize='right']",
35
+ dragTop: "[resize='top']",
36
+ dragBottom: "[resize='bottom']"
37
+ });
38
+ this.resizeWidgets[0] = resizeWidget;
39
+ }
40
+ };
41
+
42
+ function CoCreateResize(resizer, options) {
43
+ this.resizeWidget = resizer;
44
+ this.cornerSize = 10;
45
+ this.init(options);
46
+ }
47
+
48
+ CoCreateResize.prototype = {
49
+ init: function(handleObj) {
50
+ if (this.resizeWidget) {
51
+ this.initDrags = [];
52
+ this.checkCorners = [];
53
+ this.doDrags = [];
54
+ this.Drags = [];
55
+
56
+ //this is for grid
57
+ this.isGrid = this.detectGrid();
58
+ this.gridWidth = 0;
59
+ this.gridHeight = 0;
60
+ this.limitSpan = 0;
61
+ this.widthSpan = 0;
62
+ this.heightSpan = 0;
63
+ this.originClassAttribute = this.resizeWidget.getAttribute('class');
64
+
65
+ DIRECTIONS.map(d => {
66
+ this.Drags[d] = this.resizeWidget.querySelector(handleObj['drag' + d.charAt(0).toUpperCase() + d.slice(1)]);
67
+ });
68
+
69
+ this.bindListeners();
70
+ this.initResize();
71
+
72
+ //this is just for grid system
73
+ this.getGridProperty();
74
+ window.addEventListener('resize', this.checkGridColumns.bind(this));
75
+ }
76
+ },
77
+
78
+ getGridProperty: function() {
79
+ if (!this.resizeWidget.parentNode) return;
80
+ let compStyles = window.getComputedStyle(this.resizeWidget.parentNode);
81
+ let gridColumns = compStyles.gridTemplateColumns;
82
+ let height = compStyles.gridAutoRows;
83
+
84
+ if (gridColumns !== 'none') {
85
+ let width = gridColumns.split(' ');
86
+ this.limitSpan = width.length;
87
+ this.gridColumnGap = Number.parseInt(compStyles.gridColumnGap);
88
+ this.gridRowGap = Number.parseInt(compStyles.gridRowGap);
89
+ this.gridWidth = Number.parseFloat(width[0]);
90
+ this.gridHeight = Number.parseInt(height);
91
+ }
92
+ },
93
+
94
+ checkGridColumns: function() {
95
+ if (typeof this.resizeWidget !== 'undefined' && this.resizeWidget.parentNode)
96
+ {
97
+ let compStylesOfParent = window.getComputedStyle(this.resizeWidget.parentNode);
98
+ let spans = 0;
99
+ let gridColumns = compStylesOfParent.gridTemplateColumns.split(' ');
100
+ let temp = gridColumns[0];
101
+ gridColumns.map(v => {
102
+ if (v == temp) spans ++;
103
+ });
104
+ this.limitSpan = spans;
105
+ if (this.resizeWidget.style['grid-column-end'])
106
+ {
107
+ let curClassAttributes = this.resizeWidget.getAttribute('class').split(' ');
108
+ let prevColumnState = curClassAttributes[curClassAttributes.length - 1].split('_');
109
+ let prevSpan = Number.parseInt(prevColumnState[1]);
110
+ let curColumnState = this.resizeWidget.style['grid-column-end'].split(' ');
111
+ let curSpan = Number.parseInt(curColumnState[1]);
112
+
113
+ let span = 0;
114
+ if (this.limitSpan >= prevSpan) span = prevSpan;
115
+ else if (prevSpan > this.limitSpan) span = this.limitSpan;
116
+ else span = Math.min(curSpan, this.limitSpan);
117
+
118
+ this.resizeWidget.style['grid-column-end'] = 'span ' + span;
119
+ }
120
+ }
121
+ },
122
+
123
+ initResize: function() {
124
+ DIRECTIONS.map(d => {
125
+ if (this.Drags[d])
126
+ this.addListenerMulti(this.Drags[d], EVENTS[0], this.checkCorners[d]);
127
+ });
128
+ },
129
+
130
+ checkDragImplementation: function(e, from, to, offset, fcur, scur) {
131
+ if (e.touches)
132
+ e = e.touches[0];
133
+ let other;
134
+ if (to == 'top')
135
+ other = 'bottom'
136
+ else if (to == 'bottom')
137
+ other = 'top'
138
+ else if (to == 'left')
139
+ other = 'right'
140
+ else if (to == 'right')
141
+ other = 'left'
142
+ this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[from]);
143
+ this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[to]);
144
+ this.removeListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[other]);
145
+ this.addListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[from]);
146
+
147
+ if (offset < this.cornerSize && this.Drags[to]) {
148
+ this.Drags[from].style.cursor = fcur;
149
+ this.addListenerMulti(this.Drags[from], EVENTS[1], this.initDrags[to]);
150
+ }
151
+ else {
152
+ this.Drags[from].style.cursor = scur;
153
+ }
154
+ },
155
+
156
+ initDrag: function(e, idx) {
157
+ e.preventDefault();
158
+
159
+ if (!this.resizeWidget.hasAttribute('resizing')){
160
+ this.resizeWidget.setAttribute('resizing', '')
161
+ }
162
+
163
+ let selector = document.defaultView.getComputedStyle(this.resizeWidget);
164
+ styleSheet.insertRule('iframe {pointer-events: none}', 0)
165
+
166
+ if (idx == 'top' || idx == 'bottom') {
167
+ this.startTop = parseInt(selector.top, 10);
168
+ this.startHeight = parseInt(selector.height, 10);
169
+
170
+ if (e.touches)
171
+ this.startY = e.touches[0].clientY;
172
+ else
173
+ this.startY = e.clientY;
174
+ }
175
+ else {
176
+ this.startLeft = parseInt(selector.left, 10);
177
+ this.startWidth = parseInt(selector.width, 10);
178
+
179
+ if (e.touches)
180
+ this.startX = e.touches[0].clientX;
181
+ else
182
+ this.startX = e.clientX;
183
+ }
184
+
185
+ this.addListenerMulti(document.documentElement, EVENTS[0], this.doDrags[idx]);
186
+ this.addListenerMulti(document.documentElement, EVENTS[2], this.stopDrag);
187
+ },
188
+
189
+ initTopDrag: function(e) {
190
+ this.initDrag(e, 'top');
191
+ },
192
+ initBottomDrag: function(e) {
193
+ this.initDrag(e, 'bottom');
194
+ },
195
+ initLeftDrag: function(e) {
196
+ this.initDrag(e, 'left');
197
+
198
+ },
199
+ initRightDrag: function(e) {
200
+ this.initDrag(e, 'right');
201
+ },
202
+
203
+ //this is just for grid system
204
+ setRowEnd: function(height) {
205
+ if (this.gridHeight) {
206
+ this.heightSpan = Math.ceil((height - (this.heightSpan - 1) * this.gridRowGap) / this.gridHeight );
207
+ this.resizeWidget.style['grid-row-end'] = 'span ' + this.heightSpan;
208
+ this.collaborate({rowEnd: this.heightSpan})
209
+ }
210
+ },
211
+
212
+ doTopDrag: function(e) {
213
+ let top, height;
214
+
215
+ if (e.touches)
216
+ e = e.touches[0];
217
+ top = this.startTop + e.clientY - this.startY;
218
+ height = this.startHeight - e.clientY + this.startY;
219
+
220
+ if (height < 10)
221
+ return;
222
+ if (!this.isGrid) {
223
+ this.resizeWidget.style.top = top + 'px';
224
+ this.collaborate({top, height})
225
+ }
226
+ this.resizeWidget.style.height = height + 'px';
227
+ this.setRowEnd(height);
228
+ },
229
+
230
+
231
+ doBottomDrag: function(e) {
232
+ let height;
233
+
234
+ if (e.touches)
235
+ e = e.touches[0];
236
+
237
+ height = this.startHeight + e.clientY - this.startY;
238
+
239
+ if (height < 10)
240
+ return;
241
+ if (!this.isGrid)
242
+ this.collaborate({height})
243
+
244
+ this.resizeWidget.style.height = height + 'px';
245
+ this.setRowEnd(height);
246
+ },
247
+
248
+ //this is just for grid system
249
+ setColumnEnd: function(width) {
250
+ if (this.gridWidth) {
251
+ let spans = Math.ceil((width - (this.widthSpan - 1) * this.gridColumnGap) / this.gridWidth);
252
+ this.widthSpan = (spans > this.limitSpan) ? this.limitSpan : spans;
253
+ this.resizeWidget.style['grid-column-end'] = 'span ' + this.widthSpan;
254
+ this.collaborate({columnEnd: this.widthSpan})
255
+ }
256
+ },
257
+
258
+ doLeftDrag: function(e) {
259
+ let left, width;
260
+ if (e.touches)
261
+ e = e.touches[0];
262
+ left = this.startLeft + e.clientX - this.startX;
263
+ width = this.startWidth - e.clientX + this.startX;
264
+
265
+ if (width < 10)
266
+ return;
267
+ if (!this.isGrid) {
268
+ this.resizeWidget.style.left = left + 'px';
269
+ this.collaborate({left, width})
270
+ }
271
+
272
+ this.resizeWidget.style.width = width + 'px';
273
+ this.setColumnEnd(width);
274
+ },
275
+
276
+
277
+ doRightDrag: function(e) {
278
+ let width;
279
+ if (e.touches)
280
+ e = e.touches[0];
281
+ width = this.startWidth + e.clientX - this.startX;
282
+ if (width < 10)
283
+ return;
284
+
285
+ if (!this.isGrid)
286
+ this.collaborate({width})
287
+
288
+ this.resizeWidget.style.width = width + 'px';
289
+ this.setColumnEnd(width);
290
+ },
291
+
292
+ detectGrid: function() {
293
+ let isGrid = false;
294
+ if (this.resizeWidget.parentNode) {
295
+ let compStyles = window.getComputedStyle(this.resizeWidget.parentNode);
296
+ isGrid = (compStyles.display === 'grid');
297
+ }
298
+ this.isGrid = isGrid;
299
+ return isGrid;
300
+ },
301
+
302
+ stopDrag: function(e) {
303
+ styleSheet.deleteRule(0)
304
+ if (this.resizeWidget.hasAttribute('resizing')){
305
+ this.resizeWidget.removeAttribute('resizing', '')
306
+ }
307
+
308
+ //this is just for grid system
309
+ if (this.detectGrid()) {
310
+ this.resizeWidget.style.width = null;
311
+ this.resizeWidget.style.height = null;
312
+ this.resizeWidget.style.left = null;
313
+ this.resizeWidget.style.top = null;
314
+
315
+ if (this.widthSpan) this.resizeWidget.setAttribute('class', this.originClassAttribute + ' grid-column-end:span_' + this.widthSpan);
316
+ if (this.heightSpan) this.resizeWidget.setAttribute('class', this.originClassAttribute + ' grid-row-end:span_' + this.heightSpan);
317
+ }
318
+
319
+ DIRECTIONS.map(d => { this.removeListenerMulti(document.documentElement, EVENTS[0], this.doDrags[d]) });
320
+ this.removeListenerMulti(document.documentElement, EVENTS[2], this.stopDrag);
321
+ },
322
+ checkLeftCorners: function(e) {
323
+ let offset = e.clientY - this.getDistance(this.Drags['left'], true) + document.documentElement.scrollTop;
324
+ if (offset > 10) {
325
+ offset = this.getDistance(this.Drags['left'], true) + this.Drags['left'].clientHeight - e.clientY - document.documentElement.scrollTop;
326
+ this.checkDragImplementation(e, 'left', 'bottom', offset, 'ne-resize', 'e-resize');
327
+ } else {
328
+ this.checkDragImplementation(e, 'left', 'top', offset, 'se-resize', 'e-resize');
329
+ }
330
+ },
331
+ checkTopCorners: function(e) {
332
+ let offset = e.clientX - this.getDistance(this.Drags['top'], false) + document.documentElement.scrollTop;
333
+ if (offset > 10) {
334
+ offset = this.getDistance(this.Drags['top'], false) + this.Drags['top'].clientWidth - e.clientX - document.documentElement.scrollLeft;
335
+ this.checkDragImplementation(e, 'top', 'right', offset, 'ne-resize', 's-resize');
336
+ } else {
337
+ this.checkDragImplementation(e, 'top', 'left', offset, 'se-resize', 's-resize');
338
+ }
339
+ },
340
+ checkBottomCorners: function(e) {
341
+ let offset = e.clientX - this.getDistance(this.Drags['bottom'], false) + document.documentElement.scrollTop;
342
+ if (offset > 10) {
343
+ offset = this.getDistance(this.Drags['bottom'], false) + this.Drags['bottom'].clientWidth - e.clientX - document.documentElement.scrollLeft;
344
+ this.checkDragImplementation(e, 'bottom', 'right', offset, 'se-resize', 's-resize');
345
+ } else {
346
+ this.checkDragImplementation(e, 'bottom', 'left', offset, 'ne-resize', 's-resize');
347
+ }
348
+ },
349
+ checkRightCorners: function(e) {
350
+ let offset = e.clientY - this.getDistance(this.Drags['right'], true) + document.documentElement.scrollTop;
351
+ if (offset > 10) {
352
+ offset = this.getDistance(this.Drags['right'], true) + this.Drags['right'].clientHeight - e.clientY - document.documentElement.scrollTop;
353
+ this.checkDragImplementation(e, 'right', 'bottom', offset, 'se-resize', 'e-resize');
354
+ } else {
355
+ this.checkDragImplementation(e, 'right', 'top', offset, 'ne-resize', 'e-resize');
356
+ }
357
+ },
358
+
359
+ bindListeners: function() {
360
+ this.doDrags['left'] = this.doLeftDrag.bind(this);
361
+ this.doDrags['top'] = this.doTopDrag.bind(this);
362
+ this.doDrags['right'] = this.doRightDrag.bind(this);
363
+ this.doDrags['bottom'] = this.doBottomDrag.bind(this);
364
+
365
+ this.stopDrag = this.stopDrag.bind(this);
366
+
367
+ this.checkCorners['left'] = this.checkLeftCorners.bind(this);
368
+ this.checkCorners['top'] = this.checkTopCorners.bind(this);
369
+ this.checkCorners['bottom'] = this.checkBottomCorners.bind(this);
370
+ this.checkCorners['right'] = this.checkRightCorners.bind(this);
371
+
372
+ this.initDrags['top'] = this.initTopDrag.bind(this);
373
+ this.initDrags['left'] = this.initLeftDrag.bind(this);
374
+ this.initDrags['bottom'] = this.initBottomDrag.bind(this);
375
+ this.initDrags['right'] = this.initRightDrag.bind(this);
376
+ },
377
+
378
+ getDistance: function(elem, flag) {
379
+ var location = 0;
380
+ if (!elem) return;
381
+ if (elem.offsetParent) {
382
+ do {
383
+ location += (flag) ? elem.offsetTop : elem.offsetLeft;
384
+ elem = elem.offsetParent;
385
+ } while (elem);
386
+ }
387
+ return location >= 0 ? location : 0;
388
+ },
389
+
390
+ // Bind multiiple events to a listener
391
+ addListenerMulti: function(element, eventNames, listener) {
392
+ var events = eventNames.split(' ');
393
+ for (var i = 0, iLen = events.length; i < iLen; i++) {
394
+ element.addEventListener(events[i], listener, { passive: false });
395
+ }
396
+ },
397
+
398
+ // Remove multiiple events from a listener
399
+ removeListenerMulti: function(element, eventNames, listener) {
400
+ var events = eventNames.split(' ');
401
+ for (var i = 0, iLen = events.length; i < iLen; i++) {
402
+ element.removeEventListener(events[i], listener, { passive: false });
403
+ }
404
+ },
405
+
406
+ collaborate: function({left, top, width, height, rowEnd, columnEnd}) {
407
+ if (window.CoCreate && window.CoCreate.text) {
408
+ let domTextEditor = this.resizeWidget.closest('[contenteditable]');
409
+ if (domTextEditor) {
410
+ if (left)
411
+ CoCreate.text.setStyle({
412
+ domTextEditor, target: this.resizeWidget, property: 'left', value: left + 'px'
413
+ })
414
+ if (top)
415
+ CoCreate.text.setStyle({
416
+ domTextEditor, target: this.resizeWidget, property: 'top', value: top + 'px'
417
+ })
418
+ if (width)
419
+ CoCreate.text.setStyle({
420
+ domTextEditor, target: this.resizeWidget, property: 'width', value: width + 'px'
421
+ })
422
+ if (height)
423
+ CoCreate.text.setStyle({
424
+ domTextEditor, target: this.resizeWidget, property: 'height', value: height + 'px'
425
+ })
426
+ if (rowEnd)
427
+ CoCreate.text.setStyle({
428
+ domTextEditor, target: this.resizeWidget, property: 'grid-row-end', value: 'span ' + rowEnd
429
+ })
430
+ if (columnEnd)
431
+ CoCreate.text.setStyle({
432
+ domTextEditor, target: this.resizeWidget, property: 'grid-column-end', value: 'span ' + columnEnd
433
+ })
434
+ }
435
+ }
436
+ }
437
+ };
438
+
439
+ observer.init({
440
+ name: 'CoCreateResize',
441
+ observe: ['addedNodes'],
442
+ selector: "[resizable]:not([resizable='false'])",
443
+ callback: function(mutation) {
444
+ coCreateResize.initElement(mutation.target);
445
+
446
+ }
447
+ });
448
+
449
+ coCreateResize.init({
450
+ selector: "[resizable]:not([resizable='false'])",
451
+ dragLeft: "[resize='left']",
452
+ dragRight: "[resize='right']",
453
+ dragTop: "[resize='top']",
454
+ dragBottom: "[resize='bottom']",
455
+ });
456
+ export default coCreateResize;