-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevmode.js
More file actions
127 lines (104 loc) · 3.27 KB
/
devmode.js
File metadata and controls
127 lines (104 loc) · 3.27 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
/**
* Total CMS Development Mode Toggle
*
* Manages the development mode toggle switch with real-time countdown
* and automatic API calls to enable/disable development mode.
*/
export default class DevModeToggle {
constructor(toggle, options = {}) {
this.toggle = toggle;
this.api = new TotalCMS({
url: this.toggle.dataset.api || "",
});
this.endpoint = '/cache/devmode';
this.countdownElement = document.getElementById('devmode-countdown');
this.helpElement = this.toggle.closest('.form-field')?.querySelector('.help');
this.countdownInterval = null;
this.remainingSeconds = parseInt(options.remainingSeconds) || 0;
this.init();
}
init() {
this.toggle.addEventListener('change', this.onToggleChange.bind(this));
// Start countdown if dev mode is active
if (this.remainingSeconds > 0) {
this.startCountdown();
}
}
onToggleChange(event) {
const enable = event.target.checked;
const method = enable ? 'POST' : 'DELETE';
this.makeApiCall(method)
.then(data => this.handleApiSuccess(data))
.catch(error => this.handleApiError(error, enable));
}
async makeApiCall(method) {
if (method === 'POST') {
return await this.api.postAPI(this.endpoint);
} else {
return await this.api.deleteAPI(this.endpoint);
}
}
handleApiSuccess(data) {
if (!data.success) {
throw new Error(data.message || 'Unknown error');
}
// Update UI based on new state
if (data.devmode.enabled) {
this.remainingSeconds = data.devmode.remaining_seconds;
this.updateHelpText(`<strong>Development mode is active.</strong> Remaining time: <span id="devmode-countdown">${data.devmode.remaining_formatted}</span>`);
this.countdownElement = document.getElementById('devmode-countdown');
this.startCountdown();
} else {
this.remainingSeconds = 0;
this.updateHelpText('Development mode is disabled. Caching is active.');
this.stopCountdown();
}
console.log(data.message);
}
handleApiError(error, originalState) {
// Revert toggle state on error
this.toggle.checked = !originalState;
console.error('Error toggling dev mode:', error);
this.updateHelpText('Failed to toggle dev mode. A network or timeout error occurred. Please try again.');
}
updateHelpText(html) {
if (this.helpElement) {
this.helpElement.innerHTML = html;
}
}
startCountdown() {
this.stopCountdown(); // Clear any existing interval
if (this.remainingSeconds <= 0) {
return;
}
this.countdownInterval = setInterval(() => {
this.updateCountdown();
}, 1000);
}
stopCountdown() {
if (this.countdownInterval) {
clearInterval(this.countdownInterval);
this.countdownInterval = null;
}
}
updateCountdown() {
if (this.remainingSeconds <= 0) {
this.stopCountdown();
// Refresh the page when dev mode expires
window.location.reload();
return;
}
const hours = Math.floor(this.remainingSeconds / 3600);
const minutes = Math.floor((this.remainingSeconds % 3600) / 60);
const seconds = this.remainingSeconds % 60;
const formatted = `${hours}:${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`;
if (this.countdownElement) {
this.countdownElement.textContent = formatted;
}
this.remainingSeconds--;
}
destroy() {
this.stopCountdown();
this.toggle.removeEventListener('change', this.onToggleChange);
}
}