-
Notifications
You must be signed in to change notification settings - Fork 13
/
EcoIndexDialog.js
233 lines (210 loc) · 6.29 KB
/
EcoIndexDialog.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
import ApiService from "../services/ApiService";
import A11yDialog from "a11y-dialog";
import { replaceKeyIn } from "../helpers/stringUtils";
/**
* Simple collapse content component
*/
class EcoIndexDialog {
static #a11yDialog = null;
static ERROR_MESSAGES = {
404: `
{{- i18n "Error404" | markdownify -}}
`,
422: `
{{- i18n "Error422" | markdownify -}}
`,
429: `
{{- i18n "Error429" | markdownify -}}
`,
500: `
{{- i18n "Error500" | markdownify -}}
`,
502: `
{{- i18n "Error502" | markdownify -}}
`,
504: `
{{- i18n "Error504" | markdownify -}}
`,
520: `
{{- i18n "Error520" | markdownify -}}
`,
521: `
{{- i18n "Error521" | markdownify -}}
`,
};
/**
* Open the modal dialog for a pending analysis
* with a loading spinner (abort possible)
*
* @param {string} url URL being analysed
*
* @returns {boolean} true if success, otherwise true
*/
static openPendingAnalysis(url) {
let title = `
{{- (i18n "AnalysisInProgressFor") -}}`;
title = replaceKeyIn(title, "URL", url);
EcoIndexDialog.#openLoadingRequest(title);
}
/**
* Open the modal dialog for an analysis retrieval
* with a loading spinner (abort possible)
*/
static openAnalysisRetrieval() {
const title = `
{{- (i18n "AnalysisRetrieval") -}}`;
EcoIndexDialog.#openLoadingRequest(title);
}
/**
* Open the modal dialog and display error information
*
* @param {string} errorCode Error code, e.g. 429
* @param {Object} details Details object given by request error response,
* e.g. {daily_limit_per_host, host, message}
*/
static openErrorMessage(errorCode, details) {
// Title
const title = `
{{- (i18n "AnalysisErrorTitle") -}}`;
// Body (message)
let errorMessage = errorCode
? EcoIndexDialog.ERROR_MESSAGES[errorCode]
: `{{- (i18n "AnalysisErrorDefaultMessage") -}}`;
// Replace variables given in details object
if (details instanceof Object) {
for (const [key, value] of Object.entries(details)) {
errorMessage = replaceKeyIn(errorMessage, key, value);
}
}
EcoIndexDialog.#createAndShowDialog(title, {
body: `<p>{{- (i18n "AnalysisErrorIntro") | safeHTML -}}</p><p>${errorMessage}</p>`,
});
}
/**
* Open the modal dialog to share results with
* a text and a "copy to clipboard" button
*
* @param {string} url Results URL to be shared
*/
static openResultSharing(url) {
// Title
const title = `
{{- (i18n "ShareTheResult") -}}`;
// Body (message)
const body = `
<div class="with-sidebar-l --right-side --p75">
<div>
<input id="url-to-copy" type="text" readonly size="40" value="${url}"">
<div><button id="copy-to-clipboard">{{- i18n "CopyURL" -}}</button></div>
</div>
</div>
`;
EcoIndexDialog.#createAndShowDialog(title, { body });
}
/**
* Close the modal dialog
*
* @returns {boolean} true if success, otherwise true
*/
static close() {
const a11yDialog = EcoIndexDialog.#a11yDialog;
if (!a11yDialog) {
console.error("EcoIndexDialog not initialized.");
return false;
}
a11yDialog.hide();
return true;
}
/**
* Opens the modal dialog to display a "loading in progress" message
* with a spinner animation
* @param title Title to be displayed on the modal
*/
static #openLoadingRequest(title) {
EcoIndexDialog.#createAndShowDialog(title, {
actions: [
{
cb: function (e) {
ApiService.abortAnalysis();
EcoIndexDialog.close();
},
label: "{{- i18n `Cancel` -}}",
},
],
hasSpinner: true,
keepOpen: true,
});
}
/**
* Builds HTML content for a modal dialog and attaches it to the DOM
*
* @param {string} title - Modal dialog title (mandatory)
* @param {Object} options - The options for the modal dialog
* @param {string[]} [options.actions=[{cb, label}]] - An array of action objects
* @param {function} [options.actions[].cb=close] - The action callback function
* @param {string} [options.actions[].label="{{- i18n `Close` -}}"] - The action label
* @param {string} [options.body=""] - The modal dialog body content (HTML)
* @param {boolean} [options.hasSpinner=false] - Tells if the modal dialog needs a spinner
* @param {boolean} [options.keepOpen=false] - Tells if the modal dialog needs to be kept open
* until a button is explicitly activated.
* If set to true, [ESC] key and clic on the
* overlay panel won't close the modal dialog.
*/
static #createAndShowDialog(
title,
{
actions = [{ cb: EcoIndexDialog.close, label: "{{- i18n `Close` -}}" }],
body = "",
hasSpinner = false,
keepOpen = false,
} = {}
) {
const role = keepOpen ? "alertdialog" : "dialog";
const overlayAttr = keepOpen ? "" : "data-a11y-dialog-hide";
const modalContainer = document.querySelector("#dialog-here");
const dialogEl = document.querySelector("#dialog");
const modal = document.createElement("div");
modal.classList.add("dialog-container");
modal.id = "dialog";
modal.setAttribute("aria-labelledby", "dialog-title");
modal.setAttribute("role", role);
modal.innerHTML = `
<div class="dialog-overlay" ${overlayAttr}></div>
<div class="dialog-content stack-l --s3" role="document">
<h1 id="dialog-title" class="cover-l__middle text-align:center" tabindex="0">${title}</h1>
<div id="dialog-body">
${body}
</div>
${hasSpinner ? '<div id="dialog-spinner" class="page-loading-spinner"></div>' : ""}
<div class="dialog-buttons"></div>
</div>
`;
if (dialogEl) {
dialogEl.replaceWith(modal);
} else {
modalContainer.appendChild(modal);
}
const a11yDialog = (EcoIndexDialog.#a11yDialog = new A11yDialog(modal));
// buttons
const buttonsEl = modal.querySelector(".dialog-buttons");
actions.forEach(function (action) {
const btn = document.createElement("button");
btn.name = "action";
btn.innerHTML = action.label;
buttonsEl.appendChild(btn);
btn.addEventListener("click", action.cb);
});
// Prevent scroll behind the modal
const html = document.documentElement;
// prettier-ignore
a11yDialog
.on("show", () => (html.style.overflowY = "hidden"))
.on("hide", function(e) {
html.style.overflowY = "";
const dialogEl = document.querySelector("#dialog");
dialogEl.remove();
});
a11yDialog.show();
}
}
export default EcoIndexDialog;