-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfloating-panel-ui-creation.js
More file actions
719 lines (620 loc) · 30 KB
/
floating-panel-ui-creation.js
File metadata and controls
719 lines (620 loc) · 30 KB
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
// Version: 1.1
// floating-panel-ui-creation.js
// Documentation:
// This file handles fetching and creating the floating panel from an HTML template.
// It also contains basic behavior methods for making the panel draggable, positioning it,
// and creating the toggle button that summons it.
//
// Methods included:
// - createFloatingPanel(): Fetches floating-panel-files/floating-panel.html and injects it into the page.
// - createProfileSwitcher(): Builds the profile dropdown in the panel footer.
// - makeDraggable(): Enables drag functionality on an element via a handle.
// - positionPanelAtCursor(): Positions the panel relative to the mouse cursor.
// - positionPanelBottomRight(): Positions the panel to the lower-right corner safely.
// - createPanelToggleButton(): Creates the toggle button for summoning the floating panel.
//
// Dependencies:
// - floating-panel.js provides the namespace (window.MaxExtensionFloatingPanel).
// - utils.js for logging via logConCgp.
//
'use strict';
/**
* Creates the floating panel element by fetching an HTML template and appending it.
*/
window.MaxExtensionFloatingPanel.createFloatingPanel = async function () {
// Check if the panel element exists and is still attached to the document.
if (this.panelElement && document.body.contains(this.panelElement)) {
return this.panelElement;
}
// If the panel element reference exists but is not in the DOM, it's been detached.
if (this.panelElement) {
this.panelElement = null; // Reset reference to allow re-creation.
logConCgp('[floating-panel] Panel element was detached from the DOM. It will be recreated.');
}
try {
const response = await fetch(chrome.runtime.getURL('floating-panel-files/floating-panel.html'));
if (!response.ok) {
throw new Error(`Failed to fetch floating-panel-files/floating-panel.html: ${response.statusText}`);
}
const html = await response.text();
const tempDiv = document.createElement('div');
tempDiv.innerHTML = html;
const panel = tempDiv.firstElementChild;
document.body.appendChild(panel);
// Get references to all elements
this.panelElement = panel;
const panelHeader = document.getElementById('max-extension-floating-panel-header');
const closeButton = document.getElementById('max-extension-panel-close-btn');
const collapseButton = document.getElementById('max-extension-panel-collapse-btn');
const transparencyButton = document.getElementById('max-extension-panel-transparency-btn');
const transparencyPopover = document.getElementById('max-extension-transparency-popover');
const transparencySlider = document.getElementById('max-extension-transparency-slider');
const transparencyValue = document.getElementById('max-extension-transparency-value');
const collapseFooterButton = document.getElementById('max-extension-panel-collapse-footer-btn');
const profileSwitcherContainer = document.getElementById('max-extension-profile-switcher');
// Ensure settings object exists before trying to access its properties.
// This prevents errors on initial load if settings haven't been initialized yet.
if (!this.currentPanelSettings) {
this.currentPanelSettings = { ...this.defaultPanelSettings };
}
// Apply initial dynamic styles that can't be in the CSS file
this.updatePanelFromSettings();
// Attach event listeners
collapseButton.addEventListener('click', () => {
if (typeof this.toggleHeaderCollapse === 'function') {
this.toggleHeaderCollapse();
}
});
if (collapseFooterButton) {
collapseFooterButton.addEventListener('click', () => {
if (typeof this.toggleFooterCollapse === 'function') {
this.toggleFooterCollapse();
}
});
}
closeButton.addEventListener('click', () => {
// If the user closes the panel, don’t auto-reopen it for this tab.
try {
window.__OCP_userDisabledFallback = true;
} catch (_) {}
this.togglePanel();
});
// --- Transparency controls ---
const clampPercent = (p) => Math.min(100, Math.max(0, Math.round(p)));
const clampOpacity = (o) => Math.min(1, Math.max(0, o));
const updateTransparencyLabel = (p) => {
if (transparencyValue) transparencyValue.textContent = `${p}%`;
};
this._rememberPopoverOrigin = (popover) => {
if (!popover) return;
const parent = popover.parentElement;
if (!parent || parent === document.body) {
return;
}
if (!popover.__ocpOriginalParent || popover.__ocpOriginalParent.parent !== parent) {
popover.__ocpOriginalParent = {
parent,
nextSibling: popover.nextSibling
};
}
};
this.restorePopoverToOriginalParent = (popover) => {
if (!popover) return;
popover.style.position = '';
popover.style.top = '';
popover.style.left = '';
popover.style.right = '';
popover.style.bottom = '';
popover.style.zIndex = '';
if (!popover.__ocpOriginalParent) {
return;
}
const { parent, nextSibling } = popover.__ocpOriginalParent;
if (!parent || !document.contains(parent)) {
delete popover.__ocpOriginalParent;
return;
}
if (nextSibling && nextSibling.parentNode === parent) {
parent.insertBefore(popover, nextSibling);
} else {
parent.appendChild(popover);
}
};
this.positionFloatingPopover = (popover, anchor, options = {}) => {
if (!popover || !anchor) return;
const {
offsetY = 6,
offsetX = 0,
align = 'right',
viewportMargin = 8
} = options;
this._rememberPopoverOrigin(popover);
if (popover.parentElement !== document.body) {
document.body.appendChild(popover);
}
popover.style.position = 'fixed';
popover.style.right = 'auto';
popover.style.bottom = 'auto';
popover.style.zIndex = '2147483647';
// Force reflow so measurements are accurate after moving to body.
void popover.offsetWidth;
const anchorRect = anchor.getBoundingClientRect();
const popRect = popover.getBoundingClientRect();
const clampNumber = (value, min, max) => Math.max(min, Math.min(max, value));
const viewportWidth = window.innerWidth || document.documentElement.clientWidth;
const viewportHeight = window.innerHeight || document.documentElement.clientHeight;
let left;
if (align === 'left') {
left = anchorRect.left + offsetX;
} else if (align === 'center') {
left = anchorRect.left + (anchorRect.width / 2) - (popRect.width / 2) + offsetX;
} else {
left = anchorRect.right - popRect.width + offsetX;
}
let top = anchorRect.bottom + offsetY;
const maxLeft = viewportWidth - popRect.width - viewportMargin;
left = clampNumber(left, viewportMargin, Math.max(viewportMargin, maxLeft));
if (top + popRect.height + viewportMargin > viewportHeight) {
top = anchorRect.top - popRect.height - offsetY;
}
const maxTop = viewportHeight - popRect.height - viewportMargin;
top = clampNumber(top, viewportMargin, Math.max(viewportMargin, maxTop));
popover.style.left = `${Math.round(left)}px`;
popover.style.top = `${Math.round(top)}px`;
};
const applyTransparencyPercent = (percent) => {
const clampedPercent = clampPercent(percent);
if (transparencySlider && String(transparencySlider.value) !== String(clampedPercent)) {
transparencySlider.value = clampedPercent;
}
updateTransparencyLabel(clampedPercent);
const newOpacity = clampOpacity(1 - (clampedPercent / 100));
if (!this.currentPanelSettings) this.currentPanelSettings = { ...this.defaultPanelSettings };
this.currentPanelSettings.opacity = newOpacity;
this.updatePanelFromSettings();
this.debouncedSavePanelSettings();
};
const getCurrentTransparencyPercent = () => {
let currentOpacity = this.currentPanelSettings?.opacity;
if (typeof currentOpacity !== 'number' || Number.isNaN(currentOpacity)) {
currentOpacity = this.defaultPanelSettings.opacity;
}
currentOpacity = clampOpacity(currentOpacity);
const percent = Math.round((1 - currentOpacity) * 100);
return clampPercent(percent);
};
if (transparencyButton && transparencyPopover && transparencySlider) {
// Initialize slider position from current settings
const initialPercent = getCurrentTransparencyPercent();
transparencySlider.value = initialPercent;
updateTransparencyLabel(initialPercent);
const openTransparencyPopover = () => {
const currentPercent = getCurrentTransparencyPercent();
transparencySlider.value = currentPercent;
updateTransparencyLabel(currentPercent);
transparencyPopover.style.display = 'block';
if (typeof this.positionFloatingPopover === 'function') {
this.positionFloatingPopover(transparencyPopover, transparencyButton, { offsetY: 6, align: 'right' });
}
};
const closeTransparencyPopover = () => {
transparencyPopover.style.display = 'none';
if (typeof this.restorePopoverToOriginalParent === 'function') {
this.restorePopoverToOriginalParent(transparencyPopover);
}
};
this.closeTransparencyPopover = closeTransparencyPopover;
// Toggle popover
transparencyButton.addEventListener('click', (e) => {
e.stopPropagation();
const isVisible = transparencyPopover.style.display === 'block';
if (isVisible) {
closeTransparencyPopover();
} else {
openTransparencyPopover();
}
});
// Close on outside click
const outsideClickHandler = (e) => {
if (!transparencyPopover || transparencyPopover.style.display !== 'block') return;
const withinPopover = transparencyPopover.contains(e.target);
const onButton = transparencyButton.contains(e.target);
if (!withinPopover && !onButton) {
closeTransparencyPopover();
}
};
document.addEventListener('mousedown', outsideClickHandler, true);
// Close on ESC
const escHandler = (e) => {
if (e.key === 'Escape' && transparencyPopover.style.display === 'block') {
closeTransparencyPopover();
}
};
document.addEventListener('keydown', escHandler, true);
// Slider input -> live preview + save (debounced)
transparencySlider.addEventListener('input', (e) => {
const val = Number(e.target.value);
applyTransparencyPercent(val);
});
// Prevent drag interference while interacting with slider
transparencySlider.addEventListener('mousedown', (event) => event.stopPropagation());
transparencySlider.addEventListener('click', (event) => event.stopPropagation());
}
this.makeDraggable(panel, panelHeader);
this.makeDraggable(panel, profileSwitcherContainer);
// Resize listener
panel.addEventListener('mouseup', () => {
if (this.currentPanelSettings && (panel.style.width !== `${this.currentPanelSettings.width}px` ||
panel.style.height !== `${this.currentPanelSettings.height}px`)) {
this.currentPanelSettings.width = parseInt(panel.style.width);
this.currentPanelSettings.height = parseInt(panel.style.height);
this.debouncedSavePanelSettings();
}
});
// Initialize the queue section with its logic
this.initializeQueueSection();
// Initially hide the panel
panel.style.display = 'none';
logConCgp('[floating-panel] Floating panel created from HTML template.');
return panel;
} catch (error) {
logConCgp('[floating-panel] Error creating floating panel from template:', error);
return null;
}
};
/**
* Creates the profile switcher UI inside the panel footer.
*/
window.MaxExtensionFloatingPanel.createProfileSwitcher = function () {
const switcherContainer = document.getElementById('max-extension-profile-switcher');
if (!switcherContainer) return;
// Clear existing content
switcherContainer.innerHTML = '';
// Create a container for profile elements (label + dropdown)
const profileContainer = document.createElement('div');
profileContainer.className = 'profile-elements-container';
profileContainer.style.cssText = `
display: flex;
align-items: center;
gap: 8px;
`;
// Create profile label
const profileLabel = document.createElement('div');
profileLabel.textContent = 'Profile:';
// Create profile selector dropdown
const profileSelector = document.createElement('select');
profileSelector.id = 'max-extension-profile-selector';
// Prevent dragging when interacting with the dropdown
profileSelector.addEventListener('mousedown', (event) => {
event.stopPropagation();
});
profileSelector.addEventListener('click', (event) => {
event.stopPropagation();
});
// Populate the dropdown with available profiles
this.availableProfiles.forEach(profileName => {
const option = document.createElement('option');
option.value = profileName;
option.textContent = profileName;
if (profileName === this.currentProfileName) {
option.selected = true;
}
profileSelector.appendChild(option);
});
// Add change event listener to the profile selector
profileSelector.addEventListener('change', (event) => {
const selectedProfileName = event.target.value;
this.switchToProfile(selectedProfileName);
});
// Append label and selector to the profile container
profileContainer.appendChild(profileLabel);
profileContainer.appendChild(profileSelector);
// Create a container for the queue toggle (will be moved here when space allows)
const queueToggleContainer = document.createElement('div');
queueToggleContainer.id = 'max-extension-queue-toggle-footer';
queueToggleContainer.className = 'queue-toggle-footer-container';
queueToggleContainer.style.cssText = `
display: none;
margin-right: 16px;
`;
// Append queue toggle and profile containers to the switcher (they will appear on the left)
switcherContainer.appendChild(queueToggleContainer);
switcherContainer.appendChild(profileContainer);
// Initialize responsive queue toggle positioning
this.initializeResponsiveQueueToggle();
};
/**
* Makes an element draggable using a given handle element.
*/
window.MaxExtensionFloatingPanel.makeDraggable = function (element, handle) {
let offsetX = 0;
let offsetY = 0;
const startDrag = (e) => {
e.preventDefault();
offsetX = e.clientX - element.getBoundingClientRect().left;
offsetY = e.clientY - element.getBoundingClientRect().top;
document.addEventListener('mousemove', dragElement);
document.addEventListener('mouseup', stopDrag);
};
const dragElement = (e) => {
e.preventDefault();
// Calculate the new position
let newLeft = e.clientX - offsetX;
let newTop = e.clientY - offsetY;
// Get viewport dimensions
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Get panel dimensions
const panelWidth = element.offsetWidth;
const panelHeight = element.offsetHeight;
// Constrain the horizontal position (left)
newLeft = Math.max(0, newLeft);
newLeft = Math.min(newLeft, viewportWidth - panelWidth);
// Constrain the vertical position (top)
newTop = Math.max(0, newTop);
newTop = Math.min(newTop, viewportHeight - panelHeight);
// Apply the constrained position
element.style.left = newLeft + 'px';
element.style.top = newTop + 'px';
};
const stopDrag = () => {
document.removeEventListener('mousemove', dragElement);
document.removeEventListener('mouseup', stopDrag);
this.currentPanelSettings.posX = parseInt(element.style.left);
this.currentPanelSettings.posY = parseInt(element.style.top);
this.debouncedSavePanelSettings();
};
handle.addEventListener('mousedown', startDrag);
};
/**
* Positions the floating panel at the mouse cursor's position.
*/
window.MaxExtensionFloatingPanel.positionPanelAtCursor = function (event) {
if (!this.panelElement) return;
const cursorX = event.clientX;
const cursorY = event.clientY;
this.panelElement.style.left = cursorX + 'px';
this.panelElement.style.top = (cursorY - this.currentPanelSettings.height) + 'px';
this.currentPanelSettings.posX = parseInt(this.panelElement.style.left);
this.currentPanelSettings.posY = parseInt(this.panelElement.style.top);
};
/**
* Creates a toggle button for the floating panel.
*/
/**
* Positions the floating panel in the bottom-right corner of the viewport.
* Performs a secondary adjustment in the next animation frame to account for
* late layout shifts (e.g. scrollbars).
*/
window.MaxExtensionFloatingPanel.positionPanelBottomRight = function () {
if (!this.panelElement) return;
const margin = 20;
const panelWidth = this.panelElement.offsetWidth || this.currentPanelSettings.width || 300;
const panelHeight = this.panelElement.offsetHeight || this.currentPanelSettings.height || 400;
let newLeft = Math.max(window.innerWidth - panelWidth - margin, 0);
let newTop = Math.max(window.innerHeight - panelHeight - margin, 0);
this.panelElement.style.left = `${newLeft}px`;
this.panelElement.style.top = `${newTop}px`;
this.currentPanelSettings.posX = parseInt(newLeft);
this.currentPanelSettings.posY = parseInt(newTop);
this.debouncedSavePanelSettings?.();
requestAnimationFrame(() => {
const adjustedLeft = Math.max(window.innerWidth - this.panelElement.offsetWidth - margin, 0);
const adjustedTop = Math.max(window.innerHeight - this.panelElement.offsetHeight - margin, 0);
this.panelElement.style.left = `${adjustedLeft}px`;
this.panelElement.style.top = `${adjustedTop}px`;
this.currentPanelSettings.posX = parseInt(adjustedLeft);
this.currentPanelSettings.posY = parseInt(adjustedTop);
this.debouncedSavePanelSettings?.();
});
};
/**
* Positions the floating panel in the TOP-right corner of the viewport.
*/
window.MaxExtensionFloatingPanel.positionPanelTopRight = function () {
if (!this.panelElement) return;
const margin = 20;
const panelWidth = this.panelElement.offsetWidth || this.currentPanelSettings.width || 300;
// top-right = x near right edge, y near top
let newLeft = Math.max(window.innerWidth - panelWidth - margin, 0);
let newTop = margin;
this.panelElement.style.left = `${newLeft}px`;
this.panelElement.style.top = `${newTop}px`;
this.currentPanelSettings.posX = parseInt(newLeft);
this.currentPanelSettings.posY = parseInt(newTop);
this.debouncedSavePanelSettings?.();
// second pass after layout settles
requestAnimationFrame(() => {
const adjustedLeft = Math.max(window.innerWidth - this.panelElement.offsetWidth - margin, 0);
const adjustedTop = margin;
this.panelElement.style.left = `${adjustedLeft}px`;
this.panelElement.style.top = `${adjustedTop}px`;
this.currentPanelSettings.posX = parseInt(adjustedLeft);
this.currentPanelSettings.posY = parseInt(adjustedTop);
this.debouncedSavePanelSettings?.();
});
};
window.MaxExtensionFloatingPanel.createPanelToggleButton = function () {
const toggleButton = document.createElement('button');
toggleButton.type = 'button'; // Prevent form submission!
toggleButton.innerHTML = '🔼';
toggleButton.style.cssText = `
background-color: transparent;
border: none;
cursor: pointer;
padding: 1px;
font-size: 20px;
margin-right: 5px;
margin-bottom: 5px;
`;
toggleButton.title = 'Toggle floating button panel';
toggleButton.addEventListener('click', async (event) => {
await this.togglePanel(event);
});
return toggleButton;
};
/**
* Ensures the floating panel stays fully within the current viewport.
* Applies a single clamping adjustment and saves the corrected position.
* This mirrors the drag-time bounds logic and is intended for a one-time
* correction right after spawn or settings load.
*/
window.MaxExtensionFloatingPanel.ensurePanelWithinViewport = function () {
if (!this.panelElement) return;
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Prefer actual rendered size; fall back to settings if not measured yet
const panelWidth = this.panelElement.offsetWidth || this.currentPanelSettings?.width || 300;
const panelHeight = this.panelElement.offsetHeight || this.currentPanelSettings?.height || 400;
// Read the intended position (style wins; then settings)
let left = parseInt(this.panelElement.style.left, 10);
if (Number.isNaN(left)) left = parseInt(this.currentPanelSettings?.posX, 10) || 0;
let top = parseInt(this.panelElement.style.top, 10);
if (Number.isNaN(top)) top = parseInt(this.currentPanelSettings?.posY, 10) || 0;
const maxLeft = Math.max(0, viewportWidth - panelWidth);
const maxTop = Math.max(0, viewportHeight - panelHeight);
const clampedLeft = Math.min(Math.max(0, left), maxLeft);
const clampedTop = Math.min(Math.max(0, top), maxTop);
if (clampedLeft !== left || clampedTop !== top) {
this.panelElement.style.left = clampedLeft + 'px';
this.panelElement.style.top = clampedTop + 'px';
if (this.currentPanelSettings) {
this.currentPanelSettings.posX = clampedLeft;
this.currentPanelSettings.posY = clampedTop;
this.debouncedSavePanelSettings?.();
}
try { logConCgp('[floating-panel] Adjusted panel inside viewport bounds after spawn/settings load.'); } catch (_) {}
}
};
/**
* Initializes responsive positioning for the queue toggle based on available space.
*/
window.MaxExtensionFloatingPanel.initializeResponsiveQueueToggle = function () {
// This will be called after the queue section is initialized
// We'll add a resize observer to monitor panel width changes
if (!this.panelElement) return;
const checkSpaceAndMoveToggle = () => {
const queueToggleOriginal = document.getElementById('max-extension-queue-toggle-placeholder');
const queueToggleFooter = document.getElementById('max-extension-queue-toggle-footer');
const profileSwitcher = document.getElementById('max-extension-profile-switcher');
const queueSection = document.getElementById('max-extension-queue-section');
const controlsContainer = queueSection?.querySelector('.controls-container');
const expandableSection = queueSection?.querySelector('.expandable-queue-controls');
const tosWarning = document.getElementById('max-extension-queue-tos-warning');
if (!queueToggleOriginal || !queueToggleFooter || !profileSwitcher || !queueSection || !controlsContainer) return;
const panelWidth = this.panelElement.offsetWidth;
const hideQueueToggle = Boolean(window.globalMaxExtensionConfig?.queueHideActivationToggle);
if (hideQueueToggle) {
queueToggleOriginal.style.display = 'none';
queueToggleFooter.style.display = 'none';
queueSection.style.display = 'none';
if (expandableSection) expandableSection.style.display = 'none';
if (this.queueDisplayArea) this.queueDisplayArea.style.display = 'none';
this.queueToggleForcedToFooter = false;
return;
}
const minWidthForFooterPlacement = 350;
const toggle = this.queueModeToggle;
const isToggleInOriginal = toggle && toggle.parentElement === queueToggleOriginal;
const toggleLabel = toggle?.querySelector('label');
const footer = document.getElementById('max-extension-profile-switcher');
let forcedFooter = this.queueToggleForcedToFooter === true;
let labelOverflowing = false;
if (isToggleInOriginal && toggleLabel) {
labelOverflowing = (toggleLabel.scrollWidth - toggleLabel.clientWidth) > 1;
if (labelOverflowing) {
forcedFooter = true;
this.queueToggleForcedToFooter = true;
}
}
if (panelWidth >= minWidthForFooterPlacement && !labelOverflowing) {
forcedFooter = false;
this.queueToggleForcedToFooter = false;
}
const shouldMoveToFooter = panelWidth >= minWidthForFooterPlacement || forcedFooter;
const footerCollapsed = !!footer && footer.classList.contains('collapsed');
const queueEnabled = !!window.globalMaxExtensionConfig?.enableQueueMode;
const footerAllowsDisplay = (!footerCollapsed || queueEnabled);
const shouldDisplayFooterToggle = shouldMoveToFooter && footerAllowsDisplay;
// Determine if TOS warning is currently visible using computed style (works regardless of inline or stylesheet rules).
const tosVisible = !!tosWarning && window.getComputedStyle(tosWarning).display !== 'none';
if (shouldMoveToFooter) {
if (toggle && toggle.parentElement === queueToggleOriginal) {
queueToggleFooter.appendChild(toggle);
queueToggleOriginal.style.display = 'none';
}
queueToggleFooter.style.display = shouldDisplayFooterToggle ? 'flex' : 'none';
if (tosVisible) {
queueSection.style.display = 'flex';
if (expandableSection) expandableSection.style.display = 'none';
} else {
const isQueueEnabled = window.globalMaxExtensionConfig?.enableQueueMode || false;
if (isQueueEnabled) {
queueSection.style.display = 'flex';
if (expandableSection) expandableSection.style.display = 'contents';
} else {
queueSection.style.display = 'none';
}
}
} else {
if (toggle && toggle.parentElement === queueToggleFooter) {
queueToggleFooter.style.display = 'none';
queueToggleOriginal.style.display = 'block';
queueToggleOriginal.appendChild(toggle);
this.queueToggleForcedToFooter = false;
if (toggleLabel && (toggleLabel.scrollWidth - toggleLabel.clientWidth) > 1) {
this.queueToggleForcedToFooter = true;
queueToggleFooter.appendChild(toggle);
queueToggleFooter.style.display = footerAllowsDisplay ? 'flex' : 'none';
queueToggleOriginal.style.display = 'none';
}
}
// At narrow widths, keep the queue section hidden if the footer is
// collapsed and the queue is not enabled (unless a TOS warning must show).
if (tosVisible) {
queueSection.style.display = 'flex';
} else {
const showNarrow = !footerCollapsed || queueEnabled;
queueSection.style.display = showNarrow ? 'flex' : 'none';
}
}
};
this.updateQueueTogglePlacement = checkSpaceAndMoveToggle;
// Initial check
setTimeout(checkSpaceAndMoveToggle, 100);
// Monitor panel resize
if (window.ResizeObserver) {
const resizeObserver = new ResizeObserver(checkSpaceAndMoveToggle);
resizeObserver.observe(this.panelElement);
this.queueToggleResizeObserver = resizeObserver;
}
// Also check on window resize as fallback
window.addEventListener('resize', checkSpaceAndMoveToggle);
};
/**
* Updates the visibility of the queue section based on toggle placement and state.
* This function now respects the TOS warning visibility: if the warning is showing,
* the queue section must remain visible to display it.
*/
window.MaxExtensionFloatingPanel.updateQueueSectionVisibility = function (isToggleInFooter) {
const queueSection = document.getElementById('max-extension-queue-section');
const tosWarning = document.getElementById('max-extension-queue-tos-warning');
if (!queueSection) return;
if (Boolean(window.globalMaxExtensionConfig?.queueHideActivationToggle)) {
queueSection.style.display = 'none';
return;
}
const tosVisible = !!tosWarning && window.getComputedStyle(tosWarning).display !== 'none';
if (tosVisible) {
// Force visible to keep the warning accessible
queueSection.style.display = 'flex';
return;
}
if (isToggleInFooter) {
// Hide the entire queue section when toggle is in footer and no TOS warning is shown
queueSection.style.display = 'none';
} else {
// Show the queue section when toggle is back in original position
queueSection.style.display = 'flex';
}
};