@lemonadejs/modal 5.8.1 → 5.8.3

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.
Files changed (2) hide show
  1. package/dist/index.js +82 -67
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -7,8 +7,8 @@ if (!lemonade && typeof (require) === 'function') {
7
7
 
8
8
  ;(function (global, factory) {
9
9
  typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
10
- typeof define === 'function' && define.amd ? define(factory) :
11
- global.Modal = factory();
10
+ typeof define === 'function' && define.amd ? define(factory) :
11
+ global.Modal = factory();
12
12
  }(this, (function () {
13
13
 
14
14
  class CustomEvents extends Event {
@@ -22,7 +22,7 @@ if (!lemonade && typeof (require) === 'function') {
22
22
  if (props) {
23
23
  for (const key in props) {
24
24
  // Avoid assigning if property already exists anywhere on `this`
25
- if (! (key in this)) {
25
+ if (!(key in this)) {
26
26
  this[key] = props[key];
27
27
  }
28
28
  }
@@ -31,7 +31,7 @@ if (!lemonade && typeof (require) === 'function') {
31
31
  }
32
32
 
33
33
  // Dispatcher
34
- const Dispatch = function(method, type, options) {
34
+ const Dispatch = function (method, type, options) {
35
35
  // Try calling the method directly if provided
36
36
  if (typeof method === 'function') {
37
37
  let a = Object.values(options);
@@ -58,7 +58,7 @@ if (!lemonade && typeof (require) === 'function') {
58
58
  * Send the modal to the front
59
59
  * @param container
60
60
  */
61
- const sendToFront = function(container) {
61
+ const sendToFront = function (container) {
62
62
  let highestXIndex = defaultZIndex;
63
63
  for (let i = 0; i < modals.length; i++) {
64
64
  const zIndex = parseInt(modals[i].el.style.zIndex);
@@ -73,12 +73,12 @@ if (!lemonade && typeof (require) === 'function') {
73
73
  * Send modal to the back
74
74
  * @param container
75
75
  */
76
- const sendToBack = function(container) {
76
+ const sendToBack = function (container) {
77
77
  container.style.zIndex = defaultZIndex;
78
78
  }
79
79
 
80
80
  // Get the coordinates of the action
81
- const getCoords = function(e) {
81
+ const getCoords = function (e) {
82
82
  let x;
83
83
  let y;
84
84
 
@@ -90,11 +90,11 @@ if (!lemonade && typeof (require) === 'function') {
90
90
  y = e.clientY;
91
91
  }
92
92
 
93
- return [x,y];
93
+ return [x, y];
94
94
  }
95
95
 
96
96
  // Get the button status
97
- const getButton = function(e) {
97
+ const getButton = function (e) {
98
98
  e = e || window.event;
99
99
  if (e.buttons) {
100
100
  return e.buttons;
@@ -106,12 +106,12 @@ if (!lemonade && typeof (require) === 'function') {
106
106
  }
107
107
 
108
108
  // Finalize any potential action
109
- const mouseUp = function(e) {
109
+ const mouseUp = function (e) {
110
110
  // Finalize all actions
111
- if (typeof(controls.action) === 'function') {
111
+ if (typeof (controls.action) === 'function') {
112
112
  controls.action();
113
113
  }
114
- setTimeout(function() {
114
+ setTimeout(function () {
115
115
  // Remove cursor
116
116
  if (controls.e) {
117
117
  controls.e.style.cursor = '';
@@ -126,12 +126,12 @@ if (!lemonade && typeof (require) === 'function') {
126
126
  }, 0)
127
127
  }
128
128
 
129
- const mouseMove = function(e) {
130
- if (! getButton(e)) {
129
+ const mouseMove = function (e) {
130
+ if (!getButton(e)) {
131
131
  return false;
132
132
  }
133
133
  // Get mouse coordinates
134
- let [x,y] = getCoords(e);
134
+ let [x, y] = getCoords(e);
135
135
 
136
136
  // Move modal
137
137
  if (controls.type === 'move') {
@@ -167,7 +167,7 @@ if (!lemonade && typeof (require) === 'function') {
167
167
  if (e.shiftKey) {
168
168
  height = controls.h + (x - controls.x) * (controls.h / controls.w);
169
169
  }
170
- } else if (controls.d === 'w-resize' || controls.d === 'nw-resize'|| controls.d === 'sw-resize') {
170
+ } else if (controls.d === 'w-resize' || controls.d === 'nw-resize' || controls.d === 'sw-resize') {
171
171
  left = controls.l + (x - controls.x);
172
172
  // Do not move further
173
173
  if (left >= controls.l) {
@@ -182,7 +182,7 @@ if (!lemonade && typeof (require) === 'function') {
182
182
  }
183
183
 
184
184
  if (controls.d === 's-resize' || controls.d === 'se-resize' || controls.d === 'sw-resize') {
185
- if (! height) {
185
+ if (!height) {
186
186
  height = controls.h + (y - controls.y);
187
187
  }
188
188
  } else if (controls.d === 'n-resize' || controls.d === 'ne-resize' || controls.d === 'nw-resize') {
@@ -210,16 +210,16 @@ if (!lemonade && typeof (require) === 'function') {
210
210
  }
211
211
  }
212
212
 
213
- if (typeof(document) !== "undefined") {
213
+ if (typeof (document) !== "undefined") {
214
214
  document.addEventListener('mouseup', mouseUp);
215
215
  document.addEventListener('mousemove', mouseMove);
216
216
  }
217
217
 
218
- const isTrue = function(e) {
218
+ const isTrue = function (e) {
219
219
  return e === true || e === 1 || e === 'true';
220
220
  }
221
221
 
222
- const refreshMinimized = function() {
222
+ const refreshMinimized = function () {
223
223
  let items = minimizedModals;
224
224
  let numOfItems = items.length;
225
225
  let width = 10;
@@ -239,7 +239,7 @@ if (!lemonade && typeof (require) === 'function') {
239
239
  }
240
240
  }
241
241
 
242
- const delayAction = function(self, action) {
242
+ const delayAction = function (self, action) {
243
243
  // Make sure to remove the transformation before minimize to preserve the animation
244
244
  if (self.el.style.marginLeft || self.el.style.marginTop) {
245
245
  // Make sure no animation during this process
@@ -247,18 +247,18 @@ if (!lemonade && typeof (require) === 'function') {
247
247
  // Remove adjustment
248
248
  removeMargin(self);
249
249
  // Make sure to continue with minimize
250
- setTimeout(function() {
250
+ setTimeout(function () {
251
251
  // Remove class
252
252
  self.el.classList.remove('action');
253
253
  // Call action
254
254
  action(self);
255
- },0)
255
+ }, 0)
256
256
 
257
257
  return true;
258
258
  }
259
259
  }
260
260
 
261
- const setMini = function(self) {
261
+ const setMini = function (self) {
262
262
  if (delayAction(self, setMini)) {
263
263
  return;
264
264
  }
@@ -269,10 +269,10 @@ if (!lemonade && typeof (require) === 'function') {
269
269
  self.el.top = self.el.offsetTop;
270
270
  self.el.left = self.el.offsetLeft;
271
271
 
272
- if (! self.el.style.top) {
272
+ if (!self.el.style.top) {
273
273
  self.el.style.top = self.el.top + 'px';
274
274
  }
275
- if (! self.el.style.left) {
275
+ if (!self.el.style.left) {
276
276
  self.el.style.left = self.el.left + 'px';
277
277
  }
278
278
 
@@ -280,13 +280,13 @@ if (!lemonade && typeof (require) === 'function') {
280
280
  self.el.translateX = 0;
281
281
 
282
282
  // Refresh positions
283
- setTimeout(function() {
283
+ setTimeout(function () {
284
284
  refreshMinimized();
285
285
  self.minimized = true;
286
- },10)
286
+ }, 10)
287
287
  }
288
288
 
289
- const removeMini = function(self) {
289
+ const removeMini = function (self) {
290
290
  minimizedModals.splice(minimizedModals.indexOf(self), 1);
291
291
  self.minimized = false;
292
292
  self.el.style.top = self.el.top + 'px';
@@ -306,7 +306,7 @@ if (!lemonade && typeof (require) === 'function') {
306
306
  }, 400);
307
307
  }
308
308
 
309
- const removeMargin = function(self) {
309
+ const removeMargin = function (self) {
310
310
  if (self.el.style.marginLeft) {
311
311
  let y = self.el.offsetLeft;
312
312
  self.el.style.marginLeft = '';
@@ -320,8 +320,8 @@ if (!lemonade && typeof (require) === 'function') {
320
320
  }
321
321
  }
322
322
 
323
- const adjustHorizontal = function(self) {
324
- if (! isTrue(self['auto-adjust'])) {
323
+ const adjustHorizontal = function (self) {
324
+ if (!isTrue(self['auto-adjust'])) {
325
325
  return false;
326
326
  }
327
327
 
@@ -363,8 +363,8 @@ if (!lemonade && typeof (require) === 'function') {
363
363
  }
364
364
  }
365
365
 
366
- const adjustVertical = function(self) {
367
- if (! isTrue(self['auto-adjust'])) {
366
+ const adjustVertical = function (self) {
367
+ if (!isTrue(self['auto-adjust'])) {
368
368
  return false;
369
369
  }
370
370
 
@@ -409,7 +409,7 @@ if (!lemonade && typeof (require) === 'function') {
409
409
  }
410
410
  }
411
411
 
412
- const removeElements = function(root) {
412
+ const removeElements = function (root) {
413
413
  // Keep the DOM elements
414
414
  let elements = [];
415
415
  if (root) {
@@ -421,7 +421,7 @@ if (!lemonade && typeof (require) === 'function') {
421
421
  return elements;
422
422
  }
423
423
 
424
- const appendElements = function(root, elements) {
424
+ const appendElements = function (root, elements) {
425
425
  if (elements && elements.length) {
426
426
  while (elements[0]) {
427
427
  root.appendChild(elements.shift());
@@ -429,7 +429,7 @@ if (!lemonade && typeof (require) === 'function') {
429
429
  }
430
430
  }
431
431
 
432
- const Modal = function (template, { onchange, onload, track }) {
432
+ const Modal = function (template, {onchange, onload, track}) {
433
433
  let self = this;
434
434
  let backdrop = null;
435
435
  let elements = null;
@@ -444,7 +444,7 @@ if (!lemonade && typeof (require) === 'function') {
444
444
  }
445
445
 
446
446
  // Make sure keep the state as boolean
447
- self.closed = !! self.closed;
447
+ self.closed = !!self.closed;
448
448
 
449
449
  // Keep all modals references
450
450
  modals.push(self);
@@ -458,9 +458,9 @@ if (!lemonade && typeof (require) === 'function') {
458
458
 
459
459
  let ignoreEvents = false;
460
460
 
461
- const click = function(e) {
461
+ const click = function (e) {
462
462
  if (e.target.classList.contains('lm-modal-close')) {
463
- self.close({ origin: 'button' });
463
+ self.close({origin: 'button'});
464
464
  }
465
465
 
466
466
  if (e.target.classList.contains('lm-modal-minimize')) {
@@ -473,13 +473,13 @@ if (!lemonade && typeof (require) === 'function') {
473
473
  }
474
474
  }
475
475
 
476
- const mousemove = function(e) {
476
+ const mousemove = function (e) {
477
477
  if (getButton(e)) {
478
478
  return;
479
479
  }
480
480
 
481
481
  // Get mouse coordinates
482
- let [x,y] = getCoords(e);
482
+ let [x, y] = getCoords(e);
483
483
  // Root element of the component
484
484
  let item = self.el;
485
485
  // Get the position and dimensions
@@ -545,10 +545,10 @@ if (!lemonade && typeof (require) === 'function') {
545
545
  }
546
546
  }
547
547
 
548
- const mousedown = function(e) {
549
- if (! self.minimized) {
548
+ const mousedown = function (e) {
549
+ if (!self.minimized) {
550
550
  // Get mouse coordinates
551
- let [x,y] = getCoords(e);
551
+ let [x, y] = getCoords(e);
552
552
  controls.x = x;
553
553
  controls.y = y;
554
554
  // Root element of the component
@@ -563,10 +563,10 @@ if (!lemonade && typeof (require) === 'function') {
563
563
  // If is not minimized
564
564
  if (controls.type === 'resize') {
565
565
  // Make sure the width and height is defined for the modal
566
- if (! item.style.width) {
566
+ if (!item.style.width) {
567
567
  item.style.width = controls.w + 'px';
568
568
  }
569
- if (! item.style.height) {
569
+ if (!item.style.height) {
570
570
  item.style.height = controls.h + 'px';
571
571
  }
572
572
  // This will be the callback when finalize the resize
@@ -602,15 +602,15 @@ if (!lemonade && typeof (require) === 'function') {
602
602
  }
603
603
  }
604
604
 
605
- self.back = function() {
605
+ self.back = function () {
606
606
  sendToBack(self.el);
607
607
  }
608
608
 
609
- self.front = function() {
609
+ self.front = function () {
610
610
  sendToFront(self.el);
611
611
  }
612
612
 
613
- self.open = function() {
613
+ self.open = function () {
614
614
  if (self.closed === true) {
615
615
  self.closed = false;
616
616
  // Close event
@@ -620,7 +620,7 @@ if (!lemonade && typeof (require) === 'function') {
620
620
  }
621
621
  }
622
622
 
623
- self.close = function(options) {
623
+ self.close = function (options) {
624
624
  if (self.closed === false) {
625
625
  self.closed = true;
626
626
  // Close event
@@ -631,17 +631,32 @@ if (!lemonade && typeof (require) === 'function') {
631
631
  }
632
632
  }
633
633
 
634
- self.isClosed = function() {
634
+ self.isClosed = function () {
635
635
  return self.closed;
636
636
  }
637
637
 
638
- if (! template || typeof(template) !== 'string') {
638
+ /**
639
+ * Recalculate the auto-adjust position when the modal dimensions change,
640
+ * since the margin compensation depends on the current size of the modal.
641
+ * NOTE: cannot be named refresh, since lemonade registers its own refresh on self
642
+ */
643
+ self.refreshPosition = function () {
644
+ if (self.closed === false) {
645
+ // Auto adjust
646
+ queueMicrotask(() => {
647
+ adjustHorizontal(self);
648
+ adjustVertical(self);
649
+ });
650
+ }
651
+ }
652
+
653
+ if (!template || typeof (template) !== 'string') {
639
654
  template = '';
640
655
  }
641
656
 
642
657
  // Custom Root Configuration
643
658
  self.settings = {
644
- getRoot: function() {
659
+ getRoot: function () {
645
660
  return self.root;
646
661
  }
647
662
  }
@@ -699,7 +714,7 @@ if (!lemonade && typeof (require) === 'function') {
699
714
  backdrop = document.createElement('div');
700
715
  backdrop.classList.add('lm-modal-backdrop');
701
716
  backdrop.addEventListener('click', () => {
702
- self.close({ origin: 'backdrop' });
717
+ self.close({origin: 'backdrop'});
703
718
  });
704
719
 
705
720
  if (self.closed === false) {
@@ -709,18 +724,18 @@ if (!lemonade && typeof (require) === 'function') {
709
724
 
710
725
  // Import content from DOM
711
726
  if (self.content) {
712
- if (typeof(self.content) === 'string') {
727
+ if (typeof (self.content) === 'string') {
713
728
  template = self.content;
714
- } else if (typeof(self.content) === 'object' && self.content.tagName) {
729
+ } else if (typeof (self.content) === 'object' && self.content.tagName) {
715
730
  self.root.appendChild(self.content);
716
731
  }
717
732
  }
718
733
 
719
734
  // Focus out of the component
720
- self.el.addEventListener('focusout', function(e) {
721
- if (! self.el.contains(e.relatedTarget)) {
735
+ self.el.addEventListener('focusout', function (e) {
736
+ if (!self.el.contains(e.relatedTarget)) {
722
737
  if (isTrue(self['auto-close'])) {
723
- self.close({ origin: 'focusout' });
738
+ self.close({origin: 'focusout'});
724
739
  }
725
740
  // Remove focus
726
741
  self.el.classList.remove('lm-modal-focus');
@@ -728,7 +743,7 @@ if (!lemonade && typeof (require) === 'function') {
728
743
  });
729
744
 
730
745
  // Focus out of the component
731
- self.el.addEventListener('focusin', function(e) {
746
+ self.el.addEventListener('focusin', function (e) {
732
747
  self.el.classList.add('lm-modal-focus');
733
748
  });
734
749
 
@@ -736,7 +751,7 @@ if (!lemonade && typeof (require) === 'function') {
736
751
  self.el.addEventListener('keydown', (e) => {
737
752
  if (e.key === 'Escape') {
738
753
  if (self.closed === false) {
739
- self.close({ origin: 'escape' });
754
+ self.close({origin: 'escape'});
740
755
  e.preventDefault();
741
756
  e.stopImmediatePropagation();
742
757
  }
@@ -753,7 +768,7 @@ if (!lemonade && typeof (require) === 'function') {
753
768
  .then(response => response.clone().body)
754
769
  .then(body => {
755
770
  let reader = body.getReader();
756
- reader.read().then(({ done, value }) => {
771
+ reader.read().then(({done, value}) => {
757
772
  // Add HTML to the modal
758
773
  self.root.innerHTML = new TextDecoder().decode(value.buffer);
759
774
  // Call onload event
@@ -824,10 +839,10 @@ if (!lemonade && typeof (require) === 'function') {
824
839
  self.left = '';
825
840
  }
826
841
  } else {
827
- if (! self.top) {
842
+ if (!self.top) {
828
843
  self.top = (window.innerHeight - self.el.offsetHeight) / 2;
829
844
  }
830
- if (! self.left) {
845
+ if (!self.left) {
831
846
  self.left = (window.innerWidth - self.el.offsetWidth) / 2;
832
847
  }
833
848
  }
@@ -846,7 +861,7 @@ if (!lemonade && typeof (require) === 'function') {
846
861
  }
847
862
 
848
863
  const Component = function (root, options) {
849
- if (typeof(root) === 'object') {
864
+ if (typeof (root) === 'object') {
850
865
  // Remove elements from the DOM
851
866
  let elements = removeElements(root);
852
867
  // Create the modal
@@ -861,7 +876,7 @@ if (!lemonade && typeof (require) === 'function') {
861
876
  }
862
877
 
863
878
  // Create LemonadeJS Component
864
- lemonade.setComponents({ Modal: Modal });
879
+ lemonade.setComponents({Modal: Modal});
865
880
  // Create Web Component
866
881
  lemonade.createWebComponent('modal', Modal)
867
882
 
package/package.json CHANGED
@@ -17,5 +17,5 @@
17
17
  },
18
18
  "main": "dist/index.js",
19
19
  "types": "dist/index.d.ts",
20
- "version": "5.8.1"
20
+ "version": "5.8.3"
21
21
  }