-
Notifications
You must be signed in to change notification settings - Fork 46
/
popups.js
2178 lines (1762 loc) · 78.5 KB
/
popups.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
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
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Popup/floating footnotes to avoid readers needing to scroll to the end of
the page to see any footnotes; see
http://ignorethecode.net/blog/2010/04/20/footnotes/ for details.
Original author: Lukas Mathis (2010-04-20)
License: public domain ("And some people have asked me about a license for
this piece of code. I think it’s far too short to get its own license, so
I’m relinquishing any copyright claims. Consider the code to be public
domain. No attribution is necessary.")
*/
Popups = {
/**********/
/* Config.
*/
popupContainerID: "popup-container",
popupContainerParentSelector: "html",
popupContainerZIndex: "10000",
popupBreathingRoomX: 12.0,
popupBreathingRoomY: 8.0,
popupBreathingRoomYTight: -4.0,
popupTriggerDelay: 750,
popupFadeoutDelay: 100,
popupFadeoutDuration: 250,
/******************/
/* Implementation.
*/
// Used in: Popups.containingDocumentForTarget
rootDocument: document,
popupFadeTimer: false,
popupDespawnTimer: false,
popupSpawnTimer: false,
popupContainer: null,
popupBeingDragged: null,
popupBeingResized: null,
hoverEventsActive: true,
cleanup: () => {
GWLog("Popups.cleanup", "popups.js", 1);
// Remove popups container.
Popups.popupContainer?.remove();
Popups.popupContainer = null;
// Remove Escape key event listener.
document.removeEventListener("keyup", Popups.keyUp);
// Remove mousemove listener.
window.removeEventListener("mousemove", Popups.windowMouseMove);
// Remove scroll listener.
removeScrollListener("updatePopupsEventStateScrollListener");
// Remove popup-spawn event handler.
GW.notificationCenter.removeHandlerForEvent("Popups.popupDidSpawn", Popups.addDisableHoverEventsOnScrollListenerOnPopupSpawned);
// Fire event.
GW.notificationCenter.fireEvent("Popups.cleanupDidComplete");
},
setup: () => {
GWLog("Popups.setup", "popups.js", 1);
// Run cleanup.
Popups.cleanup();
// Inject popups container.
let popupContainerParent = document.querySelector(Popups.popupContainerParentSelector);
if (popupContainerParent == null) {
GWLog("Popup container parent element not found. Exiting.", "popups.js", 1);
return;
}
Popups.popupContainer = popupContainerParent.appendChild(newElement("DIV", {
id: Popups.popupContainerID,
class: "popup-container",
style: `z-index: ${Popups.popupContainerZIndex};`
}));
// Add window resize listener, to reposition pinned popups.
addWindowResizeListener(Popups.repositionPopupsOnWindowResize = (event) => {
Popups.allSpawnedPopups().forEach(popup => {
Popups.setPopupViewportRect(popup, popup.viewportRect, { clampPositionToScreen: true });
});
}, {
name: "repositionPopupsOnWindowResizeListener",
defer: true
});
// Add Escape key event listener.
document.addEventListener("keyup", Popups.keyUp);
// Add mousemove listener, to enable hover on mouse move.
window.addEventListener("mousemove", Popups.windowMouseMove = (event) => {
if ( Popups.popupBeingDragged == null
&& Popups.popupBeingResized == null)
Popups.hoverEventsActive = true;
});
// Add scroll listener, to disable hover on scroll.
addScrollListener(Popups.disableHoverEventsOnScroll = (event) => {
Popups.hoverEventsActive = false;
}, {
name: "disablePopupHoverEventsOnScrollListener"
});
/* Add event handler to add scroll listener to spawned popups, to
disable hover events when scrolling within a popup.
*/
GW.notificationCenter.addHandlerForEvent("Popups.popupDidSpawn", Popups.addDisableHoverEventsOnScrollListenerOnPopupSpawned = (info) => {
addScrollListener(Popups.disableHoverEventsOnScroll, {
name: "disablePopupHoverEventsOnScrollInPopupListener",
target: info.popup.scrollView
});
});
// Enable default popup tiling control keys (aswdqexzfrcv).
Popups.setPopupTilingControlKeys();
// Fire event.
GW.notificationCenter.fireEvent("Popups.setupDidComplete");
},
// Called by: extracts.js
addTarget: (target, prepareFunction) => {
GWLog("Popups.addTarget", "popups.js", 1);
// Bind mouseenter/mouseleave/mousedown events.
target.addEventListener("mouseenter", Popups.targetMouseEnter);
target.addEventListener("mouseleave", Popups.targetMouseLeave);
target.addEventListener("mousedown", Popups.targetMouseDown);
// Set prepare function.
target.preparePopup = prepareFunction;
// Mark target as spawning a popup.
target.classList.toggle("spawns-popup", true);
},
// Called by: extracts.js
removeTarget: (target) => {
GWLog("Popups.removeTarget", "popups.js", 1);
// Unbind existing mouseenter/mouseleave/mousedown events, if any.
target.removeEventListener("mouseenter", Popups.targetMouseEnter);
target.removeEventListener("mouseleave", Popups.targetMouseLeave);
target.removeEventListener("mousedown", Popups.targetMouseDown);
// Clear timers for target.
Popups.clearPopupTimers(target);
// Remove spawned popup for target, if any.
if (target.popup)
Popups.despawnPopup(target.popup);
// Unset popup prepare function.
target.preparePopup = null;
// Un-mark target as spawning a popup.
target.classList.toggle("spawns-popup", false);
},
/*******************/
/* General helpers.
*/
popupContainerIsVisible: () => {
return (Popups.popupContainer.style.visibility != "hidden");
},
// Called by: extracts-options.js
hidePopupContainer: () => {
GWLog("Popups.hidePopupContainer", "popups.js", 3);
if (Popups.popupContainer) {
Popups.popupContainer.style.visibility = "hidden";
Popups.allSpawnedPopups().forEach(popup => {
Popups.addClassesToPopFrame(popup, "hidden");
});
} else {
GW.notificationCenter.addHandlerForEvent("Popups.setDidComplete", (info) => {
Popups.hidePopupContainer();
});
}
},
// Called by: extracts-options.js
unhidePopupContainer: () => {
GWLog("Popups.unhidePopupContainer", "popups.js", 3);
if (Popups.popupContainer) {
Popups.popupContainer.style.visibility = "";
Popups.allSpawnedPopups().forEach(popup => {
Popups.removeClassesFromPopFrame(popup, "hidden");
});
} else {
GW.notificationCenter.addHandlerForEvent("Popups.setDidComplete", (info) => {
Popups.unhidePopupContainer();
});
}
},
updatePageScrollState: () => {
GWLog("Popups.updatePageScrollState", "popups.js", 2);
if (Popups.allSpawnedPopups().findIndex(popup => Popups.popupIsMaximized(popup)) == -1)
togglePageScrolling(true);
else
togglePageScrolling(false);
},
containingDocumentForTarget: (target) => {
return (Popups.containingPopFrame(target)?.document ?? Popups.rootDocument);
},
allSpawnedPopFrames: () => {
return Popups.allSpawnedPopups();
},
// Called by: extracts.js
allSpawnedPopups: () => {
if (Popups.popupContainer == null)
return [ ];
return Array.from(Popups.popupContainer.children).filter(popup => (popup.classList.contains("fading") == false));
},
// Called by: extracts.js
containingPopFrame: (element) => {
let shadowBody = element.closest(".shadow-body");
if (shadowBody)
return shadowBody.popup;
return element.closest(".popup");
},
addClassesToPopFrame: (popup, ...args) => {
popup.classList.add(...args);
popup.body.classList.add(...args);
},
removeClassesFromPopFrame: (popup, ...args) => {
popup.classList.remove(...args);
popup.body.classList.remove(...args);
},
/****************************************/
/* Visibility of elements within popups.
*/
/* Returns true if the given element is currently visible.
*/
// Called by: extracts-content.js
isVisible: (element) => {
let containingPopup = Popups.containingPopFrame(element);
return (containingPopup ? isWithinRect(element, containingPopup.getBoundingClientRect()) : isOnScreen(element));
},
// Called by: extracts.js
scrollElementIntoViewInPopFrame: (element, alwaysRevealTopEdge = false) => {
let popup = Popups.containingPopFrame(element);
let elementRect = element.getBoundingClientRect();
let popupBodyRect = popup.body.getBoundingClientRect();
let popupScrollViewRect = popup.scrollView.getBoundingClientRect();
let bottomBound = alwaysRevealTopEdge ? elementRect.top : elementRect.bottom;
if ( popup.scrollView.scrollTop >= elementRect.top - popupBodyRect.top
&& popup.scrollView.scrollTop + popupScrollViewRect.height <= bottomBound - popupBodyRect.top)
return;
popup.scrollView.scrollTop = elementRect.top - popupBodyRect.top;
},
/*******************************/
/* Popup spawning & despawning.
*/
newPopup: (target) => {
GWLog("Popups.newPopup", "popups.js", 2);
// Create popup, scroll view, content view, shadow root, shadow body.
let popup = newElement("DIV", { class: "popup popframe" }, { spawningTarget: target });
popup.scrollView = popup.appendChild(newElement("DIV", { class: "popframe-scroll-view" }));
popup.contentView = popup.scrollView.appendChild(newElement("DIV", { class: "popframe-content-view" }));
popup.document = popup.contentView.attachShadow({ mode: "open" });
popup.document.body = popup.body = popup.shadowBody = popup.document.appendChild(newElement("DIV", {
class: "popframe-body popup-body shadow-body"
}));
// Set reverse references.
popup.document.popup = popup.body.popup = popup.contentView.popup = popup.scrollView.popup = popup;
// Inject style reset.
popup.document.insertBefore(newElement("STYLE", null, { innerHTML: `.shadow-body { all: initial; }` }), popup.body);
// Default empty title bar.
popup.titleBarContents = [ ];
// Loading spinner and “loading failed” message views.
popup.loadingSpinnerView = popup.appendChild(newElement("DIV", { class: "popframe-loading-spinner-view" }));
popup.loadingFailedMessageView = popup.appendChild(newElement("DIV", { class: "popframe-loading-failed-message-view" }));
return popup;
},
// Called by: extracts.js
// Called by: extracts-content.js
setPopFrameContent: (popup, content) => {
if (content) {
popup.body.replaceChildren(content);
return true;
} else {
return false;
}
},
// Called by: extracts.js
// Called by: extracts-annotations.js
spawnPopup: (target, spawnPoint) => {
GWLog("Popups.spawnPopup", "popups.js", 2);
// Prevent spawn attempts before setup complete.
if (Popups.popupContainer == null)
return;
// Set wait cursor.
Popups.setWaitCursorForTarget(target);
// Despawn existing popup, if any.
if (target.popup)
Popups.despawnPopup(target.popup);
// Create the new popup.
let popup = Popups.newPopup(target);
// Prepare the newly created popup for spawning.
if (popup = target.preparePopup(popup)) {
// Attach popup to target.
Popups.attachPopupToTarget(popup, target);
} else {
// Reset cursor to normal.
Popups.clearWaitCursorForTarget(target);
// Preparation failed, so do nothing.
return;
}
/* Once this popup is spawned, despawn all non-pinned popups not in
this popup’s stack.
*/
GW.notificationCenter.addHandlerForEvent("Popups.popupDidSpawn", (info) => {
Popups.allSpawnedPopups().forEach(spawnedPopup => {
if ( Popups.popupIsPinned(spawnedPopup) == false
&& target.popup.popupStack.indexOf(spawnedPopup) == -1)
Popups.despawnPopup(spawnedPopup);
});
}, {
once: true,
condition: (info) => (info.popup == popup)
});
// If title bar contents are provided, add a title bar (if needed).
if ( popup.titleBar == null
&& popup.titleBarContents.length > 0)
Popups.addTitleBarToPopup(popup);
if (popup.parentElement == Popups.popupContainer) {
// If the popup is an existing popup, just bring it to the front.
Popups.bringPopupToFront(popup);
} else {
// Otherwise, inject the popup into the page.
Popups.injectPopup(popup);
}
// Default spawn location (in case popup was spawned programmatically).
if (spawnPoint == null) {
let targetRect = target.getBoundingClientRect();
spawnPoint = {
x: targetRect.x,
y: targetRect.y
};
}
// Position the popup appropriately with respect to the target.
Popups.positionPopup(popup, { spawnPoint: spawnPoint });
// Fire notification event.
GW.notificationCenter.fireEvent("Popups.popupDidSpawn", { popup: popup });
requestAnimationFrame(() => {
// Reset cursor to normal.
Popups.clearWaitCursorForTarget(target);
});
return popup;
},
injectPopup: (popup) => {
GWLog("Popups.injectPopup", "popups.js", 2);
// Add popup to a popup stack.
if (popup.popupStack == null) {
let parentPopup = Popups.containingPopFrame(popup.spawningTarget);
popup.popupStack = parentPopup ? parentPopup.popupStack : [ ];
} else {
popup.popupStack.remove(popup);
}
popup.popupStack.push(popup);
// Inject popup into page.
Popups.popupContainer.appendChild(popup);
// Bring popup to front.
Popups.bringPopupToFront(popup);
// Cache border width.
popup.borderWidth = parseFloat(getComputedStyle(popup).borderLeftWidth);
// Add event listeners.
popup.addEventListener("click", Popups.popupClicked);
popup.addEventListener("mouseenter", Popups.popupMouseEnter);
popup.addEventListener("mouseleave", Popups.popupMouseLeave);
popup.addEventListener("mouseout", Popups.popupMouseOut);
popup.addEventListener("mousedown", Popups.popupMouseDown);
// We define the mousemove listener here in order to capture `popup`.
popup.addEventListener("mousemove", Popups.popupMouseMove = (event) => {
GWLog("Popups.popupMouseMove", "popups.js", 3);
if ( event.target == popup
&& Popups.popupBeingDragged == null
&& Popups.popupIsResizeable(popup)) {
// Mouse position is relative to the popup’s coordinate system.
let edgeOrCorner = Popups.edgeOrCorner(popup, {
x: event.clientX - popup.viewportRect.left,
y: event.clientY - popup.viewportRect.top
});
// Set cursor.
document.documentElement.style.cursor = Popups.cursorForPopupBorder(edgeOrCorner);
}
});
},
// Called by: Popups.spawnPopup
// Called by: extracts.js
attachPopupToTarget: (popup, target) => {
GWLog("Popups.attachPopupToTarget", "popups.js", 2);
target = target ?? popup.spawningTarget;
// Clear timers.
Popups.clearPopupTimers(target);
target.classList.add("popup-open");
target.popup = popup;
target.popFrame = popup;
popup.spawningTarget = target;
},
// Called by: Popups.spawnPopup
// Called by: Popups.despawnPopup
// Called by: Popups.pinPopup
// Called by: extracts.js
detachPopupFromTarget: (popup, target) => {
GWLog("Popups.detachPopupFromTarget", "popups.js", 2);
target = target ?? popup.spawningTarget;
// Clear timers.
Popups.clearPopupTimers(target);
// Reset cursor to normal.
Popups.clearWaitCursorForTarget(target);
target.classList.remove("popup-open");
target.popup = null;
target.popFrame = null;
},
despawnPopup: (popup) => {
GWLog("Popups.despawnPopup", "popups.js", 2);
if (popup.isDespawned)
return;
GW.notificationCenter.fireEvent("Popups.popupWillDespawn", { popup: popup });
// Detach popup from its spawning target.
Popups.detachPopupFromTarget(popup);
// Remove popup from the page.
popup.remove();
// Remove popup from its popup stack.
popup.popupStack.remove(popup);
popup.popupStack = null;
// Mark popup as despawned.
popup.isDespawned = true;
// Update z-indexes of all popups.
Popups.updatePopupsZOrder();
// Enable/disable main document scrolling.
Popups.updatePageScrollState();
document.activeElement.blur();
},
// Called by: extracts.js
popFrameStateLoading: (popup) => {
return popin.classList.contains("loading");
},
// Called by: extracts.js
popFrameStateLoadingFailed: (popup) => {
return popup.classList.contains("loading-failed");
},
// Called by: extracts.js
setPopFrameStateLoading: (popup) => {
Popups.removeClassesFromPopFrame(popup, "loading-failed");
Popups.addClassesToPopFrame(popup, "loading");
},
// Called by: extracts.js
setPopFrameStateLoadingFailed: (popup) => {
Popups.removeClassesFromPopFrame(popup, "loading");
Popups.addClassesToPopFrame(popup, "loading-failed");
},
// Called by: extracts.js
clearPopFrameState: (popup) => {
Popups.removeClassesFromPopFrame(popup, "loading", "loading-failed");
},
getPopupAncestorStack: (popup) => {
let indexOfPopup = popup.popupStack.indexOf(popup);
if (indexOfPopup != -1) {
return popup.popupStack.slice(0, indexOfPopup + 1);
} else {
let parentPopup = Popups.containingPopFrame(popup.spawningTarget);
return ((parentPopup && parentPopup.popupStack)
? Popups.getPopupAncestorStack(parentPopup)
: [ ]);
}
},
isSpawned: (popup) => {
return ( popup != null
&& popup.parentElement != null
&& popup.classList.contains("fading") == false);
},
/********************/
/* Popup collapsing.
*/
popupIsCollapsed: (popup) => {
return popup.classList.contains("collapsed");
},
collapseOrUncollapsePopup: (popup) => {
GWLog("Popups.collapseOrUncollapsePopup", "popups.js", 2);
if (Popups.popupIsCollapsed(popup)) {
Popups.uncollapsePopup(popup);
} else {
Popups.collapsePopup(popup);
}
// Cache the viewport rect.
popup.viewportRect = popup.getBoundingClientRect();
},
collapsePopup: (popup) => {
GWLog("Popups.collapsePopup", "popups.js", 3);
// Update class.
Popups.addClassesToPopFrame(popup, "collapsed");
// Save and unset height, if need be.
if (popup.style.height) {
popup.dataset.previousHeight = popup.style.height;
popup.style.height = "";
}
// Pin popup.
Popups.pinPopup(popup);
// Clear timers.
Popups.clearPopupTimers(popup.spawningTarget);
// Update title bar buttons states (if any).
if (popup.titleBar)
popup.titleBar.updateState();
},
uncollapsePopup: (popup) => {
GWLog("Popups.uncollapsePopup", "popups.js", 3);
// Update class.
Popups.removeClassesFromPopFrame(popup, "collapsed");
// Restore height, if need be.
if (popup.dataset.previousHeight) {
if (Popups.popupIsPinned(popup) == true)
popup.style.height = popup.dataset.previousHeight;
// Delete saved height.
delete popup.dataset["previousHeight"];
}
// Clear timers.
Popups.clearPopupTimers(popup.spawningTarget);
// Update title bar buttons states (if any).
if (popup.titleBar)
popup.titleBar.updateState();
},
/********************************************************/
/* Popup pinning/unpinning, zooming/tiling, & restoring.
*/
/* Popup tiling control keys.
*/
popupTilingControlKeys: (localStorage.getItem("popup-tiling-control-keys") || ""),
setPopupTilingControlKeys: (keystring) => {
GWLog("Popups.setPopupTilingControlKeys", "popups.js", 1);
Popups.popupTilingControlKeys = keystring || "aswdqexzfrcv";
localStorage.setItem("popup-tiling-control-keys", Popups.popupTilingControlKeys);
},
popupIsResizeable: (popup) => {
return ( Popups.popupIsPinned(popup)
&& ( Popups.popupAllowsHorizontalResize(popup)
|| Popups.popupAllowsVerticalResize(popup)));
},
popupAllowsHorizontalResize: (popup) => {
return (popup.classList.contains("no-resize-width") == false);
},
popupAllowsVerticalResize: (popup) => {
return ( popup.classList.contains("no-resize-height") == false
&& Popups.popupIsCollapsed(popup) == false);
},
popupIsZoomed: (popup) => {
return popup.classList.contains("zoomed");
},
popupIsZoomedToPlace: (popup, place) => {
return ( popup.classList.contains("zoomed")
&& popup.classList.contains(place));
},
popupIsMaximized: (popup) => {
return (popup.classList.contains("zoomed") && popup.classList.contains("full"));
},
popupWasRestored: (popup) => {
return popup.classList.contains("restored");
},
popupIsPinned: (popup) => {
return popup.classList.contains("pinned");
},
popupWasUnpinned: (popup) => {
return popup.classList.contains("unpinned");
},
zoomPopup: (popup, place) => {
GWLog("Popups.zoomPopup", "popups.js", 2);
// If popup isn’t already zoomed, save position.
if (Popups.popupIsZoomed(popup) == false) {
popup.dataset.previousXPosition = popup.viewportRect.left;
popup.dataset.previousYPosition = popup.viewportRect.top;
}
// If the popup is collapsed, expand it.
if (Popups.popupIsCollapsed(popup))
Popups.uncollapsePopup(popup);
// Update classes.
Popups.removeClassesFromPopFrame(popup, "restored", ...(Popups.titleBarComponents.popupPlaces));
Popups.addClassesToPopFrame(popup, "zoomed", place);
// Viewport width must account for vertical scroll bar.
let viewportWidth = document.documentElement.offsetWidth;
let viewportHeight = window.innerHeight;
switch (place) {
case "top-left":
popup.zoomToX = 0.0;
popup.zoomToY = 0.0;
break;
case "top":
popup.zoomToX = 0.0;
popup.zoomToY = 0.0;
break;
case "top-right":
popup.zoomToX = viewportWidth / 2.0;
popup.zoomToY = 0.0;
break;
case "left":
popup.zoomToX = 0.0;
popup.zoomToY = 0.0;
break;
case "full":
popup.zoomToX = 0.0;
popup.zoomToY = 0.0;
break;
case "right":
popup.zoomToX = viewportWidth / 2.0;
popup.zoomToY = 0.0;
break;
case "bottom-left":
popup.zoomToX = 0.0;
popup.zoomToY = viewportHeight / 2.0;
break;
case "bottom":
popup.zoomToX = 0.0;
popup.zoomToY = viewportHeight / 2.0;
break;
case "bottom-right":
popup.zoomToX = viewportWidth / 2.0;
popup.zoomToY = viewportHeight / 2.0;
break;
}
// Update popup size.
popup.style.maxWidth = "unset";
popup.style.maxHeight = "unset";
switch (place) {
case "full":
popup.style.width = "100%";
popup.style.height = "100vh";
break;
case "left":
case "right":
popup.style.width = "50%";
popup.style.height = "100vh";
break;
case "top":
case "bottom":
popup.style.width = "100%";
popup.style.height = "50vh";
break;
case "top-left":
case "top-right":
case "bottom-left":
case "bottom-right":
popup.style.width = "50%";
popup.style.height = "50vh";
break;
}
// Pin popup. (This also updates the popup’s position.)
Popups.pinPopup(popup);
// Clear timers.
Popups.clearPopupTimers(popup.spawningTarget);
// Enable/disable main document scrolling.
Popups.updatePageScrollState();
// Update title bar buttons states (if any).
if (popup.titleBar)
popup.titleBar.updateState();
},
restorePopup: (popup) => {
GWLog("Popups.restorePopup", "popups.js", 2);
// Update classes.
Popups.removeClassesFromPopFrame(popup, "zoomed", "resized", ...(Popups.titleBarComponents.popupPlaces));
Popups.addClassesToPopFrame(popup, "restored");
// Update popup size.
popup.style.width = "";
popup.style.height = "";
popup.style.maxWidth = "";
popup.style.maxHeight = "";
// Update popup position.
Popups.positionPopup(popup);
// Clear timers.
Popups.clearPopupTimers(popup.spawningTarget);
// Enable/disable main document scrolling.
Popups.updatePageScrollState();
// Update title bar buttons states (if any).
if (popup.titleBar)
popup.titleBar.updateState();
},
pinOrUnpinPopup: (popup) => {
GWLog("Popups.pinOrUnpinPopup", "popups.js", 2);
if (Popups.popupIsPinned(popup) == true) {
Popups.unpinPopup(popup);
} else {
Popups.pinPopup(popup);
}
},
pinPopup: (popup) => {
GWLog("Popups.pinPopup", "popups.js", 2);
popup.swapClasses([ "pinned", "unpinned" ], 0);
Popups.positionPopup(popup);
popup.popupStack.remove(popup);
Popups.detachPopupFromTarget(popup);
popup.titleBar.updateState();
},
unpinPopup: (popup) => {
GWLog("Popups.unpinPopup", "popups.js", 2);
popup.swapClasses([ "pinned", "unpinned" ], 1);
Popups.positionPopup(popup);
popup.popupStack.push(popup);
Popups.attachPopupToTarget(popup);
popup.titleBar.updateState();
},
/******************/
/* Popup resizing.
*/
popupWasResized: (popup) => {
return popup.classList.contains("resized");
},
edgeOrCorner: (popup, relativeMousePos) => {
if (Popups.popupAllowsHorizontalResize(popup) == false) {
let cornerHandleSize = popup.borderWidth;
if (relativeMousePos.y < cornerHandleSize) {
return "edge-top";
} else if (relativeMousePos.y > popup.viewportRect.height - cornerHandleSize) {
return "edge-bottom";
} else {
return "";
}
} else if (Popups.popupAllowsVerticalResize(popup) == false) {
let cornerHandleSize = popup.borderWidth;
if (relativeMousePos.x < cornerHandleSize) {
return "edge-left";
} else if (relativeMousePos.x > popup.viewportRect.width - cornerHandleSize) {
return "edge-right";
} else {
return "";
}
} else {
// Make corner drag areas big enough to make a decent mouse target.
let cornerHandleSize = Math.min(20.0, (Math.min(popup.viewportRect.width, popup.viewportRect.height) / 3.0));
if ( relativeMousePos.x < cornerHandleSize
&& relativeMousePos.y < cornerHandleSize) {
return "corner-top-left";
} else if ( relativeMousePos.x > popup.viewportRect.width - cornerHandleSize
&& relativeMousePos.y > popup.viewportRect.height - cornerHandleSize) {
return "corner-bottom-right";
} else if ( relativeMousePos.x < cornerHandleSize
&& relativeMousePos.y > popup.viewportRect.height - cornerHandleSize) {
return "corner-bottom-left";
} else if ( relativeMousePos.x > popup.viewportRect.width - cornerHandleSize
&& relativeMousePos.y < cornerHandleSize) {
return "corner-top-right";
} else if (relativeMousePos.x < cornerHandleSize) {
return "edge-left";
} else if (relativeMousePos.x > popup.viewportRect.width - cornerHandleSize) {
return "edge-right";
} else if (relativeMousePos.y < cornerHandleSize) {
return "edge-top";
} else if (relativeMousePos.y > popup.viewportRect.height - cornerHandleSize) {
return "edge-bottom";
} else {
return "";
}
}
},
cursorForPopupBorder: (edgeOrCorner) => {
switch (edgeOrCorner) {
case "edge-top":
case "edge-bottom":
return "row-resize";
case "edge-left":
case "edge-right":
return "col-resize";
case "corner-top-left":
case "corner-bottom-right":
return "nwse-resize";
case "corner-top-right":
case "corner-bottom-left":
return "nesw-resize";
default:
return "";
}
},
/*******************/
/* Popup title bar.
*/
/* Add title bar to a popup which has a populated .titleBarContents.
*/
addTitleBarToPopup: (popup) => {
GWLog("Popups.addTitleBarToPopup", "popups.js", 2);
// Set class ‘has-title-bar’ on the popup.
popup.classList.add("has-title-bar");
// Create and inject the title bar element.
popup.titleBar = newElement("DIV", {
class: "popframe-title-bar",
title: "Drag popup by title bar to reposition; [v]: double-click title bar to collapse (hold [⌥/Alt] to collapse all)"
});
popup.insertBefore(popup.titleBar, popup.firstElementChild);
// Add the provided title bar contents (buttons, title, etc.).
popup.titleBarContents.forEach(element => {
popup.titleBar.appendChild(element);
if (element.buttonAction)
element.addActivateEvent(element.buttonAction);
// Add popup-positioning submenu to zoom button.
if ( element.classList.contains("zoom-button")
&& element.submenuEnabled)
Popups.titleBarComponents.addSubmenuToButton(element, "zoom-button-submenu", Popups.titleBarComponents.popupZoomButtons());
});
// Add state-updating function.
popup.titleBar.updateState = () => {
popup.titleBar.querySelectorAll("button").forEach(button => {
if (button.updateState)
button.updateState();
});
};
// Add event listeners for dragging the popup by the title bar.
popup.titleBar.addEventListener("mousedown", Popups.popupTitleBarMouseDown);
popup.titleBar.addEventListener("mouseup", Popups.popupTitleBarMouseUp);
// Add double-click event listener for collapsing/uncollapsing the popup.
popup.titleBar.addEventListener("dblclick", Popups.popupTitleBarDoubleClicked);
},
/* Elements and methods related to popup title bars.
*/
titleBarComponents: {
// The standard positions for a popup to zoom to.
popupPlaces: [ "top-left", "top", "top-right", "left", "full", "right", "bottom-left", "bottom", "bottom-right" ],
getButtonIcon: (buttonType) => {
let icon = Popups.titleBarComponents.buttonIcons[buttonType];
return icon.startsWith("<") ? icon : GW.svg(icon);
},
/* Icons for various popup title bar buttons.
(Values are keys for GW.svg().)
*/
buttonIcons: {
"close": "times-square-regular",
"zoom": "arrows-maximize-solid",
"restore": "compress-solid",
"pin": "thumbtack-regular",
"unpin": "thumbtack-solid",
"options": "gear-solid",
"zoom-top-left": "expand-arrows-up-left",
"zoom-top": "expand-arrows-up",
"zoom-top-right": "expand-arrows-up-right",
"zoom-left": "expand-arrows-left",
"zoom-full": "arrows-maximize-solid",
"zoom-right": "expand-arrows-right",
"zoom-bottom-left": "expand-arrows-down-left",
"zoom-bottom": "expand-arrows-down",
"zoom-bottom-right": "expand-arrows-down-right"
},
// Tooltip text for various popup title bar icons.
buttonTitles: {