forked from Viewtiful/fs-dialog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs-anchored-dialog.html
850 lines (779 loc) · 33.7 KB
/
fs-anchored-dialog.html
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
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
<link rel="import" href="../fs-globals/fs-globals.html">
<link rel="import" href="./fs-dialog-base.html">
<script src="./fs-dialog-positioning-obj.js"></script>
<script>
(function () {
const fsAnchoredDialogStyles = document.createElement('template');
fsAnchoredDialogStyles.setAttribute('style', 'display: none;');
fsAnchoredDialogStyles.innerHTML = `
<style data-fs-anchored-dialog>
fs-anchored-dialog {
position: absolute;
top: 0;
}
fs-anchored-dialog > .fs-dialog-pointer {
border: 16px solid transparent;
content: '';
height: 0;
width: 0;
overflow: visible;
position: absolute;
}
fs-anchored-dialog[pointer-direction="up"] > .fs-dialog-pointer {
left: calc(50% - 16px);
top: -16px;
border-top-width: 0;
border-bottom-color: var(--fs-pointer-up-color, #fff);
}
fs-anchored-dialog[pointer-direction="down"] > .fs-dialog-pointer {
left: calc(50% - 16px);
bottom: -16px;
border-bottom-width: 0;
border-top-color: var(--fs-pointer-down-color, #fff);
}
fs-anchored-dialog[pointer-direction="left"] > .fs-dialog-pointer {
left: -16px;
top: calc(50% - 16px);
border-left-width: 0;
border-right-color: var(--fs-pointer-left-color, #fff);
}
fs-anchored-dialog[pointer-direction="right"] > .fs-dialog-pointer {
right: -16px;
top: calc(50% - 16px);
border-right-width: 0;
border-left-color: var(--fs-pointer-right-color, #fff);
}
fs-anchored-dialog[no-pointer] > .fs-dialog-pointer {
visibility: hidden;
display: none;
}
@media screen and (max-width: 480px) {
fs-anchored-dialog:not([no-fullscreen-mobile]) > .fs-dialog-pointer {
visibility: hidden;
display: none;
}
}
</style>
`;
const fsAnchoredDialogTemplate = document.createElement('template');
fsAnchoredDialogTemplate.setAttribute('style', 'display: none;');
fsAnchoredDialogTemplate.innerHTML = `
<div class="fs-dialog-pointer"></div>
`;
var positioningObj = FS.dialog.service.positioningObj;
const LEFT = 'left';
const RIGHT = 'right';
const UP = 'up';
const DOWN = 'down';
const CENTER = 'center';
const BOTTOM = 'bottom';
const TOP = 'top';
const POINTER_DIRECTION = 'pointer-direction';
const POINTER_BASE = 32;
const POINTER_ALTITUDE = 16;
const REAL_POINTER_ALTITUDE = 16;
const PADDING = 15;
var attrTypeMap = {
'persistent': 'boolean',
'no-pointer': 'boolean',
'attach-direction': 'string',
'alignment': 'string',
'forced-pointer-direction': 'string',
'preferred-pointer-direction': 'space separated array'
};
var attrNameMap = {
'persistent': 'persistent',
'no-pointer': 'noPointer',
'attach-direction': 'attachDirection',
'alignment': 'alignment',
'forced-pointer-direction': 'forcedPointerDirection',
'preferred-pointer-direction': 'preferredPointerDirection'
};
var attrList = [
'persistent',
'no-pointer',
'attach-direction',
'alignment',
'forced-pointer-direction',
'preferred-pointer-direction'
];
/**
* `<fs-anchored-dialog>` is a dialog that is anchored to an element on the page.
*
* <body>
* <button onclick="dialog.open({
* focusBackElement: this,
* attachToElement: this
* });">Open Dialog</button>
* <fs-anchored-dialog
* forced-pointer-direction="right"
* no-transition
* no-fullscreen-mobile
* persistent>
* <header>
* [Header Text]
* </header>
* <div class="fs-dialog__body">
* [Dialog Body Content]
* </div>
* <footer>
* [Buttons or Footer Text]
* </footer>
* </fs-anchored-dialog>
* </body>
*
* @demo demo/index.html
*/
class FSAnchoredDialog extends FS.dialog.baseDialogComponent {
// only used for docs.
static get properties () {
return {
/**
* Whether or not the dialog is opened. Do not set or unset this yourself. Instead, use the open and close functions.
* @type {Boolean} [opened=false]
*/
'opened': {
type: Boolean,
value: false
},
/**
* Only to be used with the 'no-pointer' attribute, it specifies the alignment to the anchor element.
* Default: 'left'. Valid values: 'top', 'right', 'bottom', 'left', 'center'.
* @type {String} [alignment=left]
*/
'alignment': {
type: String,
value: LEFT
},
/**
* Forces the pointer to be a certain direction.
* Default: ''. Valid values: 'up', 'down', 'left', 'right'.
* @type {String} [forced-pointer-direction='left']
*/
'forced-pointer-direction': {
type: String,
value: ''
},
/**
* Specifies the preferred order to put the pointer direction if it fits in the current view.
* Default: 'left right up down'. Valid values: space separated left, right, up, and down
* @type {String} [preferred-pointer-direction='up right down left']
*/
'preferred-pointer-direction': {
type: String,
value: 'left right up down'
},
/**
* Whether or not the pointer should be hidden.
* @type {Boolean} [no-pointer=false]
*/
'no-pointer': {
type: Boolean,
value: false
},
/**
* Only to be used with the 'no-pointer' attribute, the side of the element to which the dialog will be attached.
* Default: 'bottom'. Valid values: 'top', 'right', 'bottom', 'left'.
* @type {String} [attach-direction=bottom]
*/
'attach-direction': {
type: String,
value: LEFT
},
/**
* Whether or not to close the dialog on loss of focus (click away).
* @type {Boolean} [persisent=false]
*/
'persistent': {
type: Boolean,
value: false
},
/**
* If you add this attribute to any clickable element within the dialog, it will fire the 'fs-dialog-dismiss' event and close the dialog on click.
* @type {Boolean} [data-dialog-dismiss=false]
*/
'data-dialog-dismiss': {
type: Boolean,
value: false
},
/**
* If you add this attribute to any clickable element within the dialog, it will fire the 'fs-dialog-confirm' event on click.
* @type {Boolean} [data-dialog-confirm=false]
*/
'data-dialog-confirm': {
type: Boolean,
value: false
},
/**
* The open modal transition animation for mobile.
* Default: ''. Valid values: 'from-right', 'from-bottom'.
* @type {String} [transition='from-bottom']
*/
'transition': {
type: String,
value: ''
},
/**
* Whether or not the modal should take up the entire screen below 480px.
* Default: false.
* @type {Boolean} [no-fullscreen-mobile=true]
*/
'no-fullscreen-mobile': {
type: Boolean,
value: false
},
/**
* Whether or not the modal should take up the entire screen regardless of screen size.
* Default: false.
* @type {Boolean} [keep-fullscreen=true]
*/
'keep-fullscreen': {
type: Boolean,
value: false
},
/**
* Whether or not to disable transition animation.
* Default: false.
* @type {Boolean} [no-transition=false]
*/
'no-transition': {
type: Boolean,
value: false
},
/**
* Whether or not to show the close button x in the upper right corner
* Default: false.
* @type {Boolean} [no-close-button]
*/
'no-close-button': {
type: Boolean,
value: false
}
};
}
/**
* Open the dialog.
* @param {object} config - Object containing the following properties (attachToElement and attachToCoordinates are mutually exclusive, but one is required):
* focusBackElement - The element to return focus back after the dialog closes.
* attachToElement - The element to which the dialog will be attached.
* attachToCoordinates - Object containing 'x' and 'y' properties representing the coordinates to attach the dialog to the element.
*/
open (config) {
super.open(config);
}
/**
* Close the dialog. If focusBackElement exists, focus is returned to that element.
*/
close () {
super.close();
}
static get observedAttributes () {
return FS.dialog.baseDialogComponent.observedAttributes.concat(attrList);
}
/**
* Set our attributes to be properties, too, and keep them up-to-date.
* @param {string} attr - The name of the attribute that changed.
* @param {string} oldValue - The previous value.
* @param {string} newValue - The new value.
*/
attributeChangedCallback (attr, oldValue, newValue) {
var handledByParent = super.attributeChangedCallback(attr, oldValue, newValue);
if (!handledByParent) {
attrList.some(function (attributeName) {
if (attr === attributeName) {
if (attrTypeMap[attributeName] === 'boolean') {
if (newValue !== undefined && newValue !== null) {
this[attrNameMap[attributeName]] = true;
} else {
this[attrNameMap[attributeName]] = false;
}
} else {
if (newValue !== undefined && newValue !== null) {
this[attrNameMap[attributeName]] = newValue;
} else {
this[attrNameMap[attributeName]] = null;
}
}
return true;
}
}.bind(this));
}
}
/**
* Calls attachedCallback
*/
connectedCallback () {
this.attachedCallback();
}
/**
* Initialize the dialog, add events, and accessibility features. Should only change
* the DOM on attached callback otherwise it breaks Polymer bindings.
*/
attachedCallback () {
this.appendStyles(fsAnchoredDialogStyles, 'style[data-fs-anchored-dialog]', this);
// initialize the attributes that were set before the component got upgraded.
// This may not be needed anymore after webcomponents are native everywhere
var that = this;
attrList.forEach(function (attr) {
that.attributeChangedCallback(attr, null, that.getAttribute(attr));
});
var clone = document.importNode(fsAnchoredDialogTemplate.content, true);
this.appendChild(clone);
if (this.persistent) {
this.removeAttribute('dismiss-on-blur');
} else {
this.setAttribute('dismiss-on-blur', '');
}
this._resizeHandler = resizeHandler.bind(this);
this._resizeThrottler = resizeThrottler.bind(this);
this._openAnchoredFunction = openAnchoredFunction.bind(this);
this._closeAnchoredFunction = closeAnchoredFunction.bind(this);
this._getPointerlessDialogRect = getPointerlessDialogRect.bind(this);
this._positionDialogWithinDom = positionDialogWithinDom.bind(this);
this._getRectsWithForcedDirection = getRectsWithForcedDirection.bind(this);
this._getNewPointerRect = getNewPointerRect.bind(this);
this._getBestFittingDialogRect = getBestFittingDialogRect.bind(this);
this._adjustDialogRectForTranslates = adjustDialogRectForTranslates.bind(this);
this._scaleRect = scaleRect.bind(this);
super.attachedCallback(openAnchoredFunction, closeAnchoredFunction);
var resizeTimeout;
function resizeThrottler () {
// ignore resize events as long as an resizeHandler execution is in the queue
if (!resizeTimeout) {
resizeTimeout = setTimeout(function () {
resizeTimeout = null;
this._resizeHandler();
// The resizeHandler will execute at a rate of 15fps
}.bind(this), 66);
}
}
function resizeHandler (event) {
var pointerElement = this.querySelector('.fs-dialog-pointer');
var dialogElement = this;
var sourceElement = this.attachToElement;
var pointerDirection = this.getAttribute(POINTER_DIRECTION);
// we put this in a setTimout so that the dom can update before we do our calculations
setTimeout(function () {
// Avoid situations where the resize function fires after the attachToElement has already been unset
if (!dialogElement.opened) {
return;
}
if (dialogElement.noPointer) {
dialogElement._positionDialogWithinDom(dialogElement, pointerElement, dialogElement._getPointerlessDialogRect({attachToElement: sourceElement}), {left: 0, top: 0}, '');
} else {
var newRects = dialogElement._getRectsWithForcedDirection(sourceElement, dialogElement, pointerElement, pointerDirection, true);
dialogElement._positionDialogWithinDom(dialogElement, pointerElement, newRects.dialogRect, newRects.pointerRect, pointerDirection);
}
}, 0);
}
function openAnchoredFunction (positionObj) {
window.addEventListener('resize', this._resizeThrottler);
positionObj = positionObj || {};
if (this.persistent) {
this.removeAttribute('dismiss-on-blur');
} else {
this.setAttribute('dismiss-on-blur', '');
}
// convert coordinates to an element
if (positionObj.attachToCoordinates && typeof (positionObj.attachToCoordinates.x) === 'number' && typeof (positionObj.attachToCoordinates.y) === 'number') {
var coordinateElement = this.coordinateElement;
if (!coordinateElement) {
coordinateElement = document.createElement('div');
document.body.appendChild(coordinateElement);
this.coordinateElement = coordinateElement;
}
coordinateElement.style.position = 'absolute';
coordinateElement.style.left = positionObj.attachToCoordinates.x + 'px';
coordinateElement.style.top = positionObj.attachToCoordinates.y + 'px';
positionObj.attachToElement = coordinateElement;
}
positionObj.attachToElement = positionObj.attachToElement || document.activeElement;
this.attachToElement = positionObj.attachToElement;
// we get the last one in the array because that should be the one that belongs to this dialog in case there are nested popovers
var pointerElement = this.querySelectorAll('.fs-dialog-pointer')[this.querySelectorAll('.fs-dialog-pointer').length - 1];
// reset styles on elements
pointerElement.style.top = '';
pointerElement.style.left = '';
this.style.top = '';
this.style.left = '';
var newRects;
if (this.noPointer) {
this._positionDialogWithinDom(this, pointerElement, this._getPointerlessDialogRect(positionObj), {left: 0, top: 0}, '');
} else if (this.forcedPointerDirection) {
newRects = this._getRectsWithForcedDirection(positionObj.attachToElement, this, pointerElement, this.forcedPointerDirection);
this._positionDialogWithinDom(this, pointerElement, newRects.dialogRect, newRects.pointerRect, this.forcedPointerDirection);
} else {
// auto-position
newRects = this._getBestFittingDialogRect(positionObj, pointerElement);
this._positionDialogWithinDom(this, pointerElement, newRects.dialogRect, newRects.pointerRect, newRects.direction);
}
}
function closeAnchoredFunction () {
window.removeEventListener('resize', this._resizeThrottler);
this.attachToElement = null;
// remove the positioning on the dialog so that dialogs don't cause extra space to be taken up on the page when they are not showing
setTimeout(() => {
if (this.opened) return;
this.style.top = '';
this.style.left = '';
}, 300);
}
function getPointerlessDialogRect (positionObj) {
var dialogElementOffsetParent = this.offsetParent || document.body;
var srcRect = positioningObj.fsLocalToGlobal(positionObj.attachToElement);
var dialogWidth = this.getBoundingClientRect().width;
var dialogHeight = this.getBoundingClientRect().height;
var topOffset = 0;
var dialogTop, dialogLeft, dialogRect;
var leftOffset = 0;
var scale = this.getBoundingClientRect().width / this.offsetWidth;
var bodyRect;
// there's some weird stuff with positioning if the body is the dialog's offset parent and its not statically positioned
if (dialogElementOffsetParent === document.body && window.getComputedStyle(document.body).position !== 'static') {
bodyRect = document.body.getBoundingClientRect();
leftOffset = bodyRect.left + window.pageXOffset;
} else if (dialogElementOffsetParent === document.body) {
bodyRect = document.body.getBoundingClientRect();
topOffset = bodyRect.top + window.pageYOffset;
}
switch (this.attachDirection) {
case BOTTOM:
case TOP:
default:
switch (this.alignment) {
case CENTER:
dialogLeft = srcRect.left - dialogWidth / 2 + srcRect.width / 2 - leftOffset;
break;
case RIGHT:
dialogLeft = srcRect.left + srcRect.width - dialogWidth - leftOffset;
break;
case LEFT:
default:
dialogLeft = srcRect.left - leftOffset;
break;
}
break;
case LEFT:
case RIGHT:
switch (this.alignment) {
case CENTER:
dialogTop = srcRect.top - dialogHeight / 2 + srcRect.height / 2 + topOffset;
break;
case TOP:
default:
dialogTop = srcRect.top + topOffset;
break;
case BOTTOM:
dialogTop = srcRect.bottom + topOffset - dialogHeight;
break;
}
break;
}
switch (this.attachDirection) {
case BOTTOM:
default:
dialogTop = srcRect.bottom + topOffset;
break;
case TOP:
dialogTop = srcRect.top + topOffset - dialogHeight;
break;
case LEFT:
dialogLeft = srcRect.left - dialogWidth - leftOffset;
break;
case RIGHT:
dialogLeft = srcRect.right - leftOffset;
break;
}
dialogRect = new positioningObj.FSRect(dialogLeft, dialogTop, dialogWidth, dialogHeight);
dialogRect = positioningObj.fsGlobalToLocal(dialogElementOffsetParent, dialogRect);
dialogRect = this._scaleRect(dialogRect, scale);
return dialogRect;
}
function getBestFittingDialogRect (positionObj, pointerElement) {
var prefOrder = this.preferredPointerDirection ? this.preferredPointerDirection.split(' ') : [LEFT, RIGHT, UP, DOWN];
var best = 0;
var pos = prefOrder[0];
var containingRect = positioningObj.fsGetWindowRect();
var newRects;
var leftRects = this._getRectsWithForcedDirection(positionObj.attachToElement, this, pointerElement, LEFT);
var rightRects = this._getRectsWithForcedDirection(positionObj.attachToElement, this, pointerElement, RIGHT);
var upRects = this._getRectsWithForcedDirection(positionObj.attachToElement, this, pointerElement, UP);
var downRects = this._getRectsWithForcedDirection(positionObj.attachToElement, this, pointerElement, DOWN);
var scores = {};
containingRect = adjustContainingRectForTransform(containingRect, this);
var leftDialogScoringRect = this._adjustDialogRectForTranslates(leftRects.dialogRect, this);
var rightDialogScoringRect = this._adjustDialogRectForTranslates(rightRects.dialogRect, this);
var upDialogScoringRect = this._adjustDialogRectForTranslates(upRects.dialogRect, this);
var downDialogScoringRect = this._adjustDialogRectForTranslates(downRects.dialogRect, this);
// The following two blocks of code are for debugging purposes and should never be running in production
// drawRect(this, 'red', containingRect)
// drawRect(this, 'blue', adjustDialogRectForTranslates(leftRects.dialogRect, this))
// drawRect(this, 'green', adjustDialogRectForTranslates(upRects.dialogRect, this))
// drawRect(this, 'orange', adjustDialogRectForTranslates(downRects.dialogRect, this))
// drawRect(this, 'purple', adjustDialogRectForTranslates(rightRects.dialogRect, this))
// function drawRect(dialog, color, rect) {
// var newDiv = dialog.offsetParent.appendChild(document.createElement('div'));
// newDiv.style.border = '2px solid ' + color;
// newDiv.style.position = 'absolute';
// newDiv.style.width = rect.width + 'px';
// newDiv.style.height = rect.height + 'px';
// newDiv.style.left = rect.left + 'px';
// newDiv.style.top = rect.top + 'px';
// }
scores.left = positioningObj.fsCalculatePercentageVisible(leftDialogScoringRect, containingRect);
scores.right = positioningObj.fsCalculatePercentageVisible(rightDialogScoringRect, containingRect);
scores.up = positioningObj.fsCalculatePercentageVisible(upDialogScoringRect, containingRect);
scores.down = positioningObj.fsCalculatePercentageVisible(downDialogScoringRect, containingRect);
prefOrder.every(function (tempPos) {
if (best < scores[tempPos]) {
pos = tempPos;
best = scores[tempPos];
}
return true;
});
switch (pos) {
case LEFT:
newRects = leftRects;
break;
case RIGHT:
newRects = rightRects;
break;
case UP:
newRects = upRects;
break;
case DOWN:
newRects = downRects;
break;
}
newRects.direction = pos;
return newRects;
}
function getRectsWithForcedDirection (sourceElement, dialogElement, pointerElement, direction, doNotAdjustForWindow) {
var scale = dialogElement.getBoundingClientRect().width / dialogElement.offsetWidth;
var realPointerAltitude = REAL_POINTER_ALTITUDE * scale;
var windowRect = positioningObj.fsGetWindowRect();
var globalSrcRect = positioningObj.fsLocalToGlobal(sourceElement);
var dialogElementOffsetParent = dialogElement.offsetParent || document.body;
var srcRect = positioningObj.fsLocalToLocal(sourceElement, dialogElementOffsetParent);
var pointerRect = positioningObj.fsLocalToLocal(pointerElement, dialogElementOffsetParent);
var dialogRect = positioningObj.fsLocalToLocal(dialogElement, dialogElementOffsetParent);
var dialogHeight = dialogElement.getBoundingClientRect().height;
var dialogWidth = dialogElement.getBoundingClientRect().width;
// there's some weird stuff with positioning if the body is the dialog's offset parent
if (dialogElementOffsetParent === document.body) {
var bodyPosition = window.getComputedStyle(document.body).position;
var bodyRect = document.body.getBoundingClientRect();
var leftOffset = bodyRect.left + window.pageXOffset;
if (bodyPosition === 'static') {
// sometimes the margins will mess up the getBoundingClientRect function on the
// body, but if it's statically positioned, then we can just use the global rect
srcRect = globalSrcRect;
dialogRect = positioningObj.fsLocalToGlobal(dialogElement);
} else {
// the left for some reason doesn't always get calculated correctly if the body
// is not positioned statically - this gives us the relative position
srcRect.left = srcRect.left - leftOffset;
dialogRect.left = dialogRect.left - leftOffset;
}
}
var srcHorizontalCenter = srcRect.left + (srcRect.width / 2);
var srcVerticalCenter = srcRect.top + (srcRect.height / 2);
var dialogTop = srcVerticalCenter - (dialogHeight / 2);
var dialogLeft = srcHorizontalCenter - (dialogWidth / 2);
var globalSrcHorizontalCenter = globalSrcRect.left + (globalSrcRect.width / 2);
var globalSrcVerticalCenter = globalSrcRect.top + (globalSrcRect.height / 2);
var globalDialogTop = globalSrcVerticalCenter - (dialogHeight / 2);
var globalDialogBottom = globalSrcVerticalCenter + (dialogHeight / 2);
var globalDialogRight = globalSrcHorizontalCenter + (dialogWidth / 2);
var globalDialogLeft = globalSrcHorizontalCenter - (dialogWidth / 2);
var xOffset = this.xOffset = calculateXOffset.bind(this)(doNotAdjustForWindow);
var yOffset = this.yOffset = calculateYOffset.bind(this)(doNotAdjustForWindow);
return positionDialog(xOffset, yOffset, pointerRect, srcRect, direction);
function calculateXOffset (doNotAdjustForWindow) {
var xOffset = 0;
switch (direction) {
case UP:
case DOWN:
if (doNotAdjustForWindow) {
return (dialogElement.offsetWidth - POINTER_BASE) / 2 - parseFloat(pointerElement.style.left.split('px')[0]);
}
if (globalDialogRight > windowRect.right) {
xOffset = -(globalDialogRight - windowRect.right);
} else if (globalDialogLeft < windowRect.left) {
xOffset = Math.abs(globalDialogLeft) + windowRect.left;
}
break;
case LEFT:
xOffset += realPointerAltitude;
break;
case RIGHT:
xOffset -= realPointerAltitude;
xOffset -= srcRect.width;
xOffset -= dialogWidth;
break;
}
return xOffset;
}
function calculateYOffset (doNotAdjustForWindow) {
var yOffset = 0;
switch (direction) {
case UP:
yOffset += realPointerAltitude;
break;
case DOWN:
yOffset -= realPointerAltitude;
yOffset -= globalSrcRect.height;
yOffset -= dialogHeight;
break;
case LEFT:
case RIGHT:
if (doNotAdjustForWindow) {
return (dialogElement.offsetHeight - POINTER_BASE) / 2 - parseFloat(pointerElement.style.top.split('px')[0]);
}
if (globalDialogBottom > windowRect.bottom) {
yOffset = -(globalDialogBottom - windowRect.bottom);
// if we've pushed the dialog off the top of the screen now, correct for it - this will only happen if the
// height of the dialog has been over-ridden and the height of the dialog is taller than the window height
if (globalDialogTop + yOffset < windowRect.top) {
yOffset = windowRect.top - globalDialogTop;
}
} else if (globalDialogTop < windowRect.top) {
yOffset = windowRect.top - globalDialogTop;
}
break;
}
return yOffset;
}
// TODO: rename
function positionDialog (xOffset, yOffset, pointerRect, srcRect, direction) {
var newDialogRectTop;
var newDialogRectLeft;
var newDialogRect;
var newPointerRect = getNewPointerRect(pointerRect, srcRect, direction, scale);
var padding = PADDING * scale;
switch (direction) {
case UP:
case DOWN:
newDialogRectTop = srcRect.top + srcRect.height + yOffset;
newDialogRectLeft = dialogLeft + xOffset;
newDialogRect = new positioningObj.FSRect(newDialogRectLeft, newDialogRectTop, dialogWidth, dialogHeight);
if (newDialogRect.left > newPointerRect.left - padding) {
newDialogRect.left = newPointerRect.left - padding;
} else if (newDialogRect.right < newPointerRect.right + padding) {
newDialogRect.left = newDialogRect.left + (newPointerRect.right + padding - newDialogRect.right + padding);
}
break;
case LEFT:
case RIGHT:
newDialogRectLeft = srcRect.left + srcRect.width + xOffset;
newDialogRectTop = dialogTop + yOffset;
newDialogRect = new positioningObj.FSRect(newDialogRectLeft, newDialogRectTop, dialogWidth, dialogHeight);
if (newDialogRect.top > newPointerRect.top - padding) {
newDialogRect.top = newPointerRect.top - padding;
} else if (newDialogRect.bottom < newPointerRect.bottom + padding) {
newDialogRect.top = newDialogRect.top + (newPointerRect.bottom + padding - newDialogRect.bottom);
}
break;
}
return {
dialogRect: dialogElement._scaleRect(newDialogRect, scale),
pointerRect: dialogElement._scaleRect(newPointerRect, scale)
};
}
}
function getNewPointerRect (pointerRect, srcRect, direction, scale) {
var newPointerRectTop = pointerRect.top;
var newPointerRectLeft = pointerRect.left;
var newPointerRect;
var pointerWidth;
var pointerHeight;
switch (direction) {
case UP:
case DOWN:
newPointerRectLeft = srcRect.left + ((srcRect.width - POINTER_BASE * scale) / 2);
pointerHeight = POINTER_ALTITUDE * scale;
pointerWidth = POINTER_BASE * scale;
break;
case LEFT:
case RIGHT:
newPointerRectTop = srcRect.top + ((srcRect.height - POINTER_BASE * scale) / 2);
pointerHeight = POINTER_BASE * scale;
pointerWidth = POINTER_ALTITUDE * scale;
break;
}
newPointerRect = new positioningObj.FSRect(newPointerRectLeft, newPointerRectTop, pointerWidth, pointerHeight);
return newPointerRect;
}
function positionDialogWithinDom (dialogElement, pointerElement, newDialogRect, newPointerRect, direction) {
// set the pointer-direction attribute
dialogElement.setAttribute(POINTER_DIRECTION, direction);
// position in the dom the dialog
dialogElement.style.left = newDialogRect.left + 'px';
dialogElement.style.top = newDialogRect.top + 'px';
// position in the dom the pointer
if (direction === LEFT || direction === RIGHT) {
// the pointer element isn't relative to the dialog (its offset parent), it is realtive to the
// dialog's offset parent. So, we have to subtract ou the dialog's top and left to get the top
// and left of the pointer relative to the dialog.
pointerElement.style.top = (newPointerRect.top - newDialogRect.top) + 'px';
} else {
pointerElement.style.left = (newPointerRect.left - newDialogRect.left) + 'px';
}
}
function adjustContainingRectForTransform (containingRect, dialogElement) {
var scale = dialogElement.getBoundingClientRect().width / dialogElement.offsetWidth;
var translates = getOffsetParentTranslates(dialogElement);
if (scale === 1 && translates.x === 0 && translates.y === 0) return containingRect;
var left = (containingRect.left - translates.x) / scale;
var top = (containingRect.top - translates.y) / scale;
var width = containingRect.width / scale;
var height = containingRect.height / scale;
return new positioningObj.FSRect(left, top, width, height);
}
function getOffsetParentTranslates (element) {
// Unfortunately, there's really no programmatic way to figure out what the translate values are
// because of scrolling. This function gets any translates explicitly set on the offset parent of the dialog
var translates = {x: 0, y: 0};
var transform = window.getComputedStyle(element.offsetParent || document.body).transform;
if (transform.includes('matrix')) {
translates.x = transform.split(', ')[4];
translates.y = transform.split(', ')[5].split(')')[0];
}
return translates;
}
function scaleRect (rect, scale) {
var left = rect.left / scale;
var top = rect.top / scale;
var width = rect.width * scale;
var height = rect.height * scale;
return new positioningObj.FSRect(left, top, width, height);
}
function adjustDialogRectForTranslates (rect, dialogElement) {
var width = dialogElement.offsetWidth;
var height = dialogElement.offsetHeight;
return new positioningObj.FSRect(rect.left, rect.top, width, height);
}
}
/**
* Calls detachedCallback.
*/
disconnectedCallback () {
this.detachedCallback();
}
/**
* If dummy element was created to position the dialog at attachToCoordinates, remove the element.
*/
detachedCallback () {
if (this.coordinateElement) {
document.body.removeChild(this.coordinateElement);
this.coordinateElement = null;
}
}
}
if ('customElements' in window) {
customElements.define('fs-anchored-dialog', FSAnchoredDialog);
} else {
document.registerElement('fs-anchored-dialog', {prototype: Object.create(FSAnchoredDialog.prototype)});
}
})();
</script>