File size: 9,374 Bytes
9bd422a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * FullscreenManager - Quản Lý Chế Độ Toàn Màn Hình
 * Cung cấp nút "Fullscreen" để mở rộng card đồ thị chiếm toàn bộ viewport.
 * Sử dụng Fullscreen API gốc của trình duyệt.
 * Requirements: 26.1, 26.2, 26.3, 26.4, 26.5, 26.6
 */

const FullscreenManager = (function () {
    class FullscreenManager {
        /**
         * @param {string} graphCardSelector - CSS selector cho card chứa đồ thị
         */
        constructor(graphCardSelector) {
            /** @type {string} */
            this._cardSelector = graphCardSelector;

            /** @type {HTMLElement|null} */
            this._cardElement = null;

            /** @type {HTMLButtonElement|null} */
            this._btn = null;

            /** @type {Function|null} */
            this._onFullscreenChangeBound = null;

            /** @type {boolean} */
            this._initialized = false;
        }

        // ─── Public API ─────────────────────────────────────────────────────

        /**
         * Khởi tạo: tìm card element, tạo nút fullscreen, gắn event listeners.
         */
        init() {
            if (this._initialized) return;

            this._cardElement = this._resolveCardElement();
            if (!this._cardElement) {
                console.warn('[FullscreenManager] Card element not found for selector:', this._cardSelector);
                return;
            }

            this._createButton();
            this._bindEvents();
            this._initialized = true;
            console.log('[FullscreenManager] Initialized');
        }

        /**
         * Vào chế độ toàn màn hình cho card đồ thị.
         */
        enterFullscreen() {
            if (!this._cardElement) return;
            if (this.isFullscreen()) return;

            const el = this._cardElement;
            if (el.requestFullscreen) {
                el.requestFullscreen();
            } else if (el.webkitRequestFullscreen) {
                el.webkitRequestFullscreen();
            } else if (el.msRequestFullscreen) {
                el.msRequestFullscreen();
            }
        }

        /**
         * Thoát chế độ toàn màn hình.
         */
        exitFullscreen() {
            if (!this.isFullscreen()) return;

            if (document.exitFullscreen) {
                document.exitFullscreen();
            } else if (document.webkitExitFullscreen) {
                document.webkitExitFullscreen();
            } else if (document.msExitFullscreen) {
                document.msExitFullscreen();
            }
        }

        /**
         * Toggle giữa fullscreen và normal.
         */
        toggle() {
            if (this.isFullscreen()) {
                this.exitFullscreen();
            } else {
                this.enterFullscreen();
            }
        }

        /**
         * Kiểm tra trạng thái fullscreen hiện tại.
         * @returns {boolean}
         */
        isFullscreen() {
            return !!(
                document.fullscreenElement ||
                document.webkitFullscreenElement ||
                document.msFullscreenElement
            );
        }

        /**
         * Hủy và dọn dẹp event listeners, DOM.
         */
        destroy() {
            if (this._onFullscreenChangeBound) {
                document.removeEventListener('fullscreenchange', this._onFullscreenChangeBound);
                document.removeEventListener('webkitfullscreenchange', this._onFullscreenChangeBound);
                this._onFullscreenChangeBound = null;
            }

            if (this._btn && this._btn.parentNode) {
                this._btn.parentNode.removeChild(this._btn);
                this._btn = null;
            }

            if (this._cardElement) {
                this._cardElement.classList.remove('graph-card-fullscreen');
                this._cardElement = null;
            }

            this._initialized = false;
        }

        // ─── Private ────────────────────────────────────────────────────────

        /**
         * Resolve the card element from the selector.
         * The selector targets the card that is the parent of #graphContainer.
         * @returns {HTMLElement|null}
         */
        _resolveCardElement() {
            if (this._cardSelector) {
                // Try direct selector first
                const el = document.querySelector(this._cardSelector);
                if (el) return el;
            }

            // Fallback: find the .card ancestor of #graphContainer
            const graphContainer = document.getElementById('graphContainer');
            if (graphContainer) {
                return graphContainer.closest('.card');
            }

            return null;
        }

        /**
         * Create the fullscreen toggle button and insert it into #graphExportContainer.
         */
        _createButton() {
            const exportContainer = document.getElementById('graphExportContainer');
            if (!exportContainer) {
                console.warn('[FullscreenManager] #graphExportContainer not found');
                return;
            }

            const btn = document.createElement('button');
            btn.className = 'btn btn-outline-secondary btn-sm ms-2 fullscreen-btn';
            btn.id = 'fullscreenBtn';
            btn.title = 'Toggle Fullscreen';
            btn.innerHTML = '<i class="fas fa-expand me-1"></i>Fullscreen';

            exportContainer.appendChild(btn);
            this._btn = btn;
        }

        /**
         * Bind click and fullscreenchange events.
         */
        _bindEvents() {
            // Button click
            if (this._btn) {
                this._btn.addEventListener('click', () => {
                    this.toggle();
                });
            }

            // Fullscreen change event
            this._onFullscreenChangeBound = this._onFullscreenChange.bind(this);
            document.addEventListener('fullscreenchange', this._onFullscreenChangeBound);
            document.addEventListener('webkitfullscreenchange', this._onFullscreenChangeBound);
        }

        /**
         * Handle fullscreenchange event: update button icon/text, apply styles, resize graph.
         */
        _onFullscreenChange() {
            const isFs = this.isFullscreen();

            // Update button icon and text
            this._updateButton(isFs);

            // Toggle fullscreen CSS class on the card
            if (this._cardElement) {
                if (isFs) {
                    this._cardElement.classList.add('graph-card-fullscreen');
                    this._cardElement.style.background = '#fff';
                } else {
                    this._cardElement.classList.remove('graph-card-fullscreen');
                    this._cardElement.style.background = '';
                }
            }

            // Adjust #graphContainer height
            this._adjustGraphContainerHeight(isFs);

            // Resize the Cytoscape graph after a short delay to let the DOM settle
            setTimeout(() => {
                this._resizeGraph();
            }, 100);
        }

        /**
         * Update button icon and text based on fullscreen state.
         * @param {boolean} isFs
         */
        _updateButton(isFs) {
            if (!this._btn) return;

            if (isFs) {
                this._btn.innerHTML = '<i class="fas fa-compress me-1"></i>Exit Fullscreen';
                this._btn.title = 'Exit Fullscreen';
            } else {
                this._btn.innerHTML = '<i class="fas fa-expand me-1"></i>Fullscreen';
                this._btn.title = 'Toggle Fullscreen';
            }
        }

        /**
         * Adjust the #graphContainer height for fullscreen mode.
         * @param {boolean} isFs
         */
        _adjustGraphContainerHeight(isFs) {
            const graphContainer = document.getElementById('graphContainer');
            if (!graphContainer) return;

            if (isFs) {
                // In fullscreen, expand to fill viewport minus card header (~60px)
                graphContainer.style.height = 'calc(100vh - 60px)';
            } else {
                // Restore original height
                graphContainer.style.height = '';
            }
        }

        /**
         * Call cy.resize() and cy.fit() on the Cytoscape instance to adjust to new dimensions.
         */
        _resizeGraph() {
            try {
                if (window._onnxApp && typeof window._onnxApp.getGraphVisualizer === 'function') {
                    const visualizer = window._onnxApp.getGraphVisualizer();
                    if (visualizer && visualizer._cy) {
                        visualizer._cy.resize();
                        visualizer._cy.fit();
                    }
                }
            } catch (err) {
                console.warn('[FullscreenManager] Could not resize graph:', err);
            }
        }
    }

    return FullscreenManager;
})();

// Export as global for browser usage
window.FullscreenManager = FullscreenManager;