| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class DomainList { |
| 22 |
constructor() { |
| 23 |
this._page = null; |
| 24 |
this._table = null; |
| 25 |
this._scroll = null; |
| 26 |
this._element = document.getElementById("main-block"); |
| 27 |
this._sort = { column: "fqdn", direction: "ascent" }; |
| 28 |
} |
| 29 |
|
| 30 |
display() { |
| 31 |
this._make_page_container(); |
| 32 |
this._make_scroll_container(); |
| 33 |
this._make_table(); |
| 34 |
this._scroll.append(this._table.element()); |
| 35 |
this._page.append(this._scroll); |
| 36 |
this._element.append(this._page); |
| 37 |
this._table.focus(); |
| 38 |
} |
| 39 |
|
| 40 |
update() { |
| 41 |
this._fetch_list(); |
| 42 |
} |
| 43 |
|
| 44 |
title() { |
| 45 |
return "Domain List"; |
| 46 |
} |
| 47 |
|
| 48 |
_fetch_list() { |
| 49 |
this._table.display_status("wait"); |
| 50 |
let that = this; |
| 51 |
|
| 52 |
return window.fetch("domains.php", { |
| 53 |
method: "GET", |
| 54 |
cache: "no-store", |
| 55 |
headers: HTTP_HEADERS, |
| 56 |
credentials: "same-origin" |
| 57 |
}).then(function(resp) { |
| 58 |
if (!resp.ok) |
| 59 |
throw new Error("Failed to fetch the domain list"); |
| 60 |
return resp.json(); |
| 61 |
}).then(function(data) { |
| 62 |
that._table.display_status(null); |
| 63 |
Common.checkResult(data); |
| 64 |
let d = { more: data.more }; |
| 65 |
d.rows = data.domains.map(function(it) { |
| 66 |
return that._make_row_data(it); |
| 67 |
}); |
| 68 |
if (User.level === "admin" || User.level === "manager") { |
| 69 |
d.rows.push(new NewDomainRow(4)); |
| 70 |
} |
| 71 |
let fr = new DomainFrame(d, that._table.last_row_index() + 1); |
| 72 |
that._table.clear(); |
| 73 |
that._table.add_frame(fr); |
| 74 |
if (that._sort.column) { |
| 75 |
that._table.sort(that._sort.column, that._sort.direction); |
| 76 |
} |
| 77 |
that._table.focus(); |
| 78 |
}).catch(function(err) { |
| 79 |
Common.displayError(err); |
| 80 |
that._table.display_status("error"); |
| 81 |
}); |
| 82 |
} |
| 83 |
|
| 84 |
_make_page_container() { |
| 85 |
this._page = document.createElement("div"); |
| 86 |
this._page.classList.add("page-container"); |
| 87 |
} |
| 88 |
|
| 89 |
_make_scroll_container() { |
| 90 |
this._scroll = document.createElement("div"); |
| 91 |
this._scroll.classList.add("table-wrapper"); |
| 92 |
} |
| 93 |
|
| 94 |
_make_table() { |
| 95 |
this._table = new ITable({ |
| 96 |
class: "main-table domains", |
| 97 |
onclick: function(row) { |
| 98 |
let data = row.userdata(); |
| 99 |
if (data) { |
| 100 |
this._display_edit_dialog(data); |
| 101 |
} |
| 102 |
}.bind(this), |
| 103 |
onsort: function(col) { |
| 104 |
let dir = col.sorted() && "toggle" || "ascent"; |
| 105 |
this._table.set_sorted(col.name(), dir); |
| 106 |
this._table.sort(col.name(), col.sorted()); |
| 107 |
this._sort.column = col.name(); |
| 108 |
this._sort.direction = col.sorted(); |
| 109 |
this._table.focus(); |
| 110 |
}.bind(this), |
| 111 |
onfocus: function(el) { |
| 112 |
scroll_to_element(el, this._scroll); |
| 113 |
}.bind(this) |
| 114 |
}); |
| 115 |
[ |
| 116 |
{ content: "", sortable: true, name: "status", class: "cell-status" }, |
| 117 |
{ content: "FQDN", sortable: true, name: "fqdn" }, |
| 118 |
{ content: "Updated", sortable: true, name: "date" }, |
| 119 |
{ content: "Description", class: "descr" } |
| 120 |
].forEach(function(col) { |
| 121 |
let c = this._table.add_column(col); |
| 122 |
if (c.name() === this._sort.column) { |
| 123 |
c.sort(this._sort.direction); |
| 124 |
} |
| 125 |
}, this); |
| 126 |
} |
| 127 |
|
| 128 |
_make_row_data(d) { |
| 129 |
let rd = { cells: [], userdata: d.fqdn }; |
| 130 |
rd.cells.push(new DomainStatusCell(d.active)); |
| 131 |
rd.cells.push({ content: d.fqdn, class: "fqdn" }); |
| 132 |
rd.cells.push(new DomainTimeCell(new Date(d.updated_time))); |
| 133 |
rd.cells.push({ content: d.description || "", class: "descr" }); |
| 134 |
return rd; |
| 135 |
} |
| 136 |
|
| 137 |
_display_edit_dialog(fqdn) { |
| 138 |
let dlg_par = {}; |
| 139 |
if (fqdn === "*new") { |
| 140 |
dlg_par["new"] = true; |
| 141 |
dlg_par.disable = User.level !== "admin"; |
| 142 |
} |
| 143 |
else { |
| 144 |
dlg_par.fqdn = fqdn; |
| 145 |
dlg_par.disable = User.level === "user"; |
| 146 |
} |
| 147 |
dlg_par.level = User.level; |
| 148 |
|
| 149 |
let dlg = new DomainEditDialog(dlg_par); |
| 150 |
this._element.appendChild(dlg.element()); |
| 151 |
let that = this; |
| 152 |
dlg.show().then(function(d) { |
| 153 |
if (d) { |
| 154 |
that.update(); |
| 155 |
} |
| 156 |
}).finally(function() { |
| 157 |
dlg.element().remove(); |
| 158 |
that._table.focus(); |
| 159 |
}); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
class DomainStatusCell extends ITableCell { |
| 164 |
constructor(is_active, props) { |
| 165 |
props = props || {}; |
| 166 |
let ca = (props.class || "").split(" "); |
| 167 |
ca.push(is_active && "state-green" || "state-gray"); |
| 168 |
props.class = ca.filter(function(s) { return s.length > 0; }).join(" "); |
| 169 |
super(is_active, props); |
| 170 |
} |
| 171 |
|
| 172 |
value(target) { |
| 173 |
if (target === "dom") { |
| 174 |
let div = document.createElement("div"); |
| 175 |
div.setAttribute("class", "state-background status-indicator"); |
| 176 |
if (!this._title) { |
| 177 |
div.setAttribute("title", this._content && "active" || "inactive"); |
| 178 |
} |
| 179 |
return div; |
| 180 |
} |
| 181 |
return this._content; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
class DomainTimeCell extends ITableCell { |
| 186 |
value(target) { |
| 187 |
if (target === "dom") { |
| 188 |
return this._content && this._content.toUIString() || ""; |
| 189 |
} |
| 190 |
if (target === "sort") { |
| 191 |
return this._content && this._content.valueOf() || ""; |
| 192 |
} |
| 193 |
super.value(target); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
class NewDomainRow extends ITableRow { |
| 198 |
constructor(col_cnt) { |
| 199 |
super({ |
| 200 |
userdata: "*new", |
| 201 |
cells: [] |
| 202 |
}); |
| 203 |
this._col_cnt = col_cnt; |
| 204 |
} |
| 205 |
|
| 206 |
element() { |
| 207 |
if (!this._element) { |
| 208 |
super.element(); |
| 209 |
this._element.classList.add("colspanned", "virtual-item"); |
| 210 |
for (let i = 0; i < this._col_cnt; ++i) { |
| 211 |
let cell = document.createElement("div"); |
| 212 |
cell.setAttribute("class", "table-cell"); |
| 213 |
cell.appendChild(document.createTextNode(!i && "New domain" || "\u00A0")); |
| 214 |
this._element.appendChild(cell); |
| 215 |
} |
| 216 |
} |
| 217 |
return this._element; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
class DomainFrame extends ITableFrame { |
| 222 |
sort(col_idx, direction) { |
| 223 |
this._sort_dir = (direction === "ascent" && 1) || (direction === "descent" && 2) || 0; |
| 224 |
super.sort(col_idx, direction); |
| 225 |
} |
| 226 |
|
| 227 |
_compare_cells(c1, c2) { |
| 228 |
if (!c1) { |
| 229 |
return this._sort_dir === 2; |
| 230 |
} |
| 231 |
if (!c2) { |
| 232 |
return this._sort_dir === 1; |
| 233 |
} |
| 234 |
return super._compare_cells(c1, c2); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
class DomainEditDialog extends VerticalDialog { |
| 239 |
constructor(params) { |
| 240 |
let tl = null; |
| 241 |
let ba = []; |
| 242 |
if (params.level !== "user") ba.push("save"); |
| 243 |
if (params["new"]) { |
| 244 |
tl = "New domain"; |
| 245 |
} |
| 246 |
else { |
| 247 |
tl = "Domain settings"; |
| 248 |
if (params.level !== "user") ba.push("delete"); |
| 249 |
} |
| 250 |
ba.push("close"); |
| 251 |
super({ title: tl, buttons: ba }); |
| 252 |
this._data = params || {}; |
| 253 |
this._note_el = null; |
| 254 |
this._fqdn_el = null; |
| 255 |
this._actv_el = null; |
| 256 |
this._desc_el = null; |
| 257 |
this._c_tm_el = null; |
| 258 |
this._u_tm_el = null; |
| 259 |
this._fetched = false; |
| 260 |
} |
| 261 |
|
| 262 |
_gen_content() { |
| 263 |
let dis = this._data.disable; |
| 264 |
|
| 265 |
this._note_el = this._content.appendChild(document.createElement("div")); |
| 266 |
this._note_el.classList.add("hidden", "warn-block"); |
| 267 |
|
| 268 |
let fq = document.createElement("input"); |
| 269 |
fq.setAttribute("type", "text"); |
| 270 |
if (!this._data["new"]) { |
| 271 |
fq.setAttribute("value", this._data.fqdn); |
| 272 |
fq.disabled = true; |
| 273 |
} |
| 274 |
fq.required = true; |
| 275 |
this._insert_input_row("FQDN", fq); |
| 276 |
this._fqdn_el = fq; |
| 277 |
|
| 278 |
{ |
| 279 |
let en = document.createElement("select"); |
| 280 |
let op1 = document.createElement("option"); |
| 281 |
op1.setAttribute("value", "yes"); |
| 282 |
op1.appendChild(document.createTextNode("Yes")); |
| 283 |
en.appendChild(op1); |
| 284 |
let op2 = document.createElement("option"); |
| 285 |
op2.setAttribute("value", "no"); |
| 286 |
op2.appendChild(document.createTextNode("No")); |
| 287 |
en.appendChild(op2); |
| 288 |
en.required = true; |
| 289 |
en.disabled = dis; |
| 290 |
this._insert_input_row("Active", en); |
| 291 |
this._actv_el = en; |
| 292 |
} |
| 293 |
|
| 294 |
let tx = document.createElement("textarea"); |
| 295 |
tx.classList.add("description") |
| 296 |
tx.disabled = dis; |
| 297 |
this._insert_input_row("Description", tx); |
| 298 |
this._desc_el = tx; |
| 299 |
|
| 300 |
let ct = document.createElement("input"); |
| 301 |
ct.setAttribute("type", "text"); |
| 302 |
ct.disabled = true; |
| 303 |
ct.setAttribute("value","n/a"); |
| 304 |
this._insert_input_row("Created", ct); |
| 305 |
this._c_tm_el = ct; |
| 306 |
|
| 307 |
let ut = document.createElement("input"); |
| 308 |
ut.setAttribute("type", "text"); |
| 309 |
ut.setAttribute("value","n/a"); |
| 310 |
ut.disabled = true; |
| 311 |
this._insert_input_row("Updated", ut); |
| 312 |
this._u_tm_el = ut; |
| 313 |
|
| 314 |
this._inputs.addEventListener("input", function(event) { |
| 315 |
if (this._fetched || this._data["new"]) { |
| 316 |
this._buttons[1].disabled = ( |
| 317 |
this._fqdn_el.value.trim() === "" || ( |
| 318 |
this._actv_el.dataset.server === this._actv_el.value && |
| 319 |
this._desc_el.defaultValue === this._desc_el.value && |
| 320 |
this._fqdn_el.defaultValue === this._fqdn_el.value) |
| 321 |
); |
| 322 |
} |
| 323 |
}.bind(this)); |
| 324 |
|
| 325 |
if (!this._fetched) this._fetch_data(); |
| 326 |
} |
| 327 |
|
| 328 |
_fetch_data() { |
| 329 |
this._enable_ui(false); |
| 330 |
this.display_status("wait", "Getting data..."); |
| 331 |
|
| 332 |
const url = new URL("domains.php", document.location); |
| 333 |
url.searchParams.set("domain", this._data.fqdn || ""); |
| 334 |
window.fetch(url, { |
| 335 |
method: "GET", |
| 336 |
cache: "no-store", |
| 337 |
headers: HTTP_HEADERS, |
| 338 |
credentials: "same-origin" |
| 339 |
}).then(resp => { |
| 340 |
if (!resp.ok) throw new Error("Failed to fetch the domain data"); |
| 341 |
return resp.json(); |
| 342 |
}).then(data => { |
| 343 |
this._fetched = true; |
| 344 |
Common.checkResult(data); |
| 345 |
if (data.created_time) data.created_time = new Date(data.created_time); |
| 346 |
if (data.updated_time) data.updated_time = new Date(data.updated_time); |
| 347 |
this._update_ui(data); |
| 348 |
this._enable_ui(true); |
| 349 |
}).catch(err => { |
| 350 |
Common.displayError(err); |
| 351 |
this.display_status("error", err.message); |
| 352 |
}).finally(() => { |
| 353 |
this.display_status("wait", null); |
| 354 |
}); |
| 355 |
} |
| 356 |
|
| 357 |
_update_ui(data) { |
| 358 |
if (data.verification === "dns") { |
| 359 |
this._note_el.classList.remove("hidden"); |
| 360 |
let lm = document.createElement("a"); |
| 361 |
lm.href = ""; |
| 362 |
lm.textContent = "Learn more"; |
| 363 |
this._note_el.append("Domain verification is required! ", lm); |
| 364 |
lm.addEventListener("click", event => { |
| 365 |
event.preventDefault(); |
| 366 |
this._display_verification_dialog(data.verification_data, lm); |
| 367 |
}); |
| 368 |
} |
| 369 |
if (this._data["new"]) return; |
| 370 |
|
| 371 |
let val = ""; |
| 372 |
for (let i = 0; i < this._actv_el.options.length; ++i) { |
| 373 |
let op = this._actv_el.options[i]; |
| 374 |
if (data.active === (op.value === "yes")) { |
| 375 |
op.setAttribute("selected", "selected"); |
| 376 |
val = op.value; |
| 377 |
} |
| 378 |
else { |
| 379 |
op.removeAttribute("selected"); |
| 380 |
} |
| 381 |
} |
| 382 |
this._actv_el.value = val; |
| 383 |
this._actv_el.dataset.server = val; |
| 384 |
this._desc_el.appendChild(document.createTextNode(data.description || "")); |
| 385 |
this._c_tm_el.setAttribute("value", data.created_time && data.created_time.toUIString() || "n/a"); |
| 386 |
this._u_tm_el.setAttribute("value", data.updated_time && data.updated_time.toUIString() || "n/a"); |
| 387 |
} |
| 388 |
|
| 389 |
_add_button(container, text, type) { |
| 390 |
let btn = null; |
| 391 |
if (type === "save") { |
| 392 |
text = "Save"; |
| 393 |
btn = document.createElement("button"); |
| 394 |
btn.disabled = true; |
| 395 |
btn.addEventListener("click", this._save.bind(this)); |
| 396 |
} |
| 397 |
else if (type === "delete") { |
| 398 |
text = "Delete"; |
| 399 |
btn = document.createElement("button"); |
| 400 |
btn.addEventListener("click", () => this._confirm_delete(false)); |
| 401 |
} |
| 402 |
else { |
| 403 |
super._add_button(container, text, type); |
| 404 |
return; |
| 405 |
} |
| 406 |
btn.setAttribute("type", "button"); |
| 407 |
btn.appendChild(document.createTextNode(text)); |
| 408 |
container.appendChild(btn); |
| 409 |
this._buttons.push(btn); |
| 410 |
} |
| 411 |
|
| 412 |
_enable_ui(en) { |
| 413 |
let dis = this._data.disable; |
| 414 |
this._fqdn_el.disabled = !en || !this._data["new"]; |
| 415 |
this._actv_el.disabled = !en || dis; |
| 416 |
this._desc_el.disabled = !en || dis; |
| 417 |
for (let i = 2; i < this._buttons.length - 1; ++i) { |
| 418 |
this._buttons[i].disabled = !en; |
| 419 |
} |
| 420 |
this.focus(); |
| 421 |
} |
| 422 |
|
| 423 |
_save() { |
| 424 |
this._enable_ui(false); |
| 425 |
this.display_status("wait", "Sending data to the server..."); |
| 426 |
|
| 427 |
const body = {}; |
| 428 |
body.fqdn = this._data["new"] && this._fqdn_el.value || this._data.fqdn; |
| 429 |
body.action = this._data["new"] && "add" || "update"; |
| 430 |
body.active = this._actv_el.value === "yes"; |
| 431 |
body.description = this._desc_el.value; |
| 432 |
|
| 433 |
window.fetch("domains.php", { |
| 434 |
method: "POST", |
| 435 |
cache: "no-store", |
| 436 |
headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST), |
| 437 |
credentials: "same-origin", |
| 438 |
body: JSON.stringify(body) |
| 439 |
}).then(resp => { |
| 440 |
if (!resp.ok) |
| 441 |
throw new Error("Failed to " + (body.new && "add" || "update") + " the domain data"); |
| 442 |
return resp.json(); |
| 443 |
}).then(data => { |
| 444 |
Common.checkResult(data); |
| 445 |
this._result = body; |
| 446 |
this.hide(); |
| 447 |
Notification.add({ |
| 448 |
text: "The domain " + body.fqdn + " was " + (body.action === "add" && "added" || "updated") |
| 449 |
}); |
| 450 |
}).catch(err => { |
| 451 |
Common.displayError(err); |
| 452 |
this.display_status("error", err.message); |
| 453 |
}).finally(() => { |
| 454 |
this.display_status("wait", null); |
| 455 |
this._enable_ui(true); |
| 456 |
}); |
| 457 |
} |
| 458 |
|
| 459 |
_confirm_delete(with_reports) { |
| 460 |
let msg; |
| 461 |
if (with_reports) { |
| 462 |
msg = "There are incoming reports for the domain. Do you want to delete the domain with its reports?"; |
| 463 |
} else { |
| 464 |
msg = "Are sure you want to delete this domain?"; |
| 465 |
} |
| 466 |
if (confirm(msg)) { |
| 467 |
this._delete(with_reports); |
| 468 |
return true; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
_delete(with_reports) { |
| 473 |
this._enable_ui(false); |
| 474 |
this.display_status("wait", "Sending a request to the server..."); |
| 475 |
|
| 476 |
const body = { |
| 477 |
fqdn: this._data.fqdn, |
| 478 |
force: with_reports, |
| 479 |
action: "delete" |
| 480 |
}; |
| 481 |
|
| 482 |
window.fetch("domains.php", { |
| 483 |
method: "POST", |
| 484 |
cache: "no-store", |
| 485 |
headers: Object.assign(HTTP_HEADERS, HTTP_HEADERS_POST), |
| 486 |
credentials: "same-origin", |
| 487 |
body: JSON.stringify(body) |
| 488 |
}).then(resp => { |
| 489 |
if (!resp.ok) throw new Error("Failed to delete the domain"); |
| 490 |
return resp.json(); |
| 491 |
}).then(data => { |
| 492 |
Common.checkResult(data); |
| 493 |
this._result = data; |
| 494 |
this.hide(); |
| 495 |
Notification.add({ text: "The domain " + body.fqdn + " was removed" }); |
| 496 |
}).catch(err => { |
| 497 |
if (err.error_code === -10 && this._confirm_delete(true)) return; |
| 498 |
Common.displayError(err); |
| 499 |
this.display_status("error", err.message); |
| 500 |
}).finally(() => { |
| 501 |
this.display_status("wait", null); |
| 502 |
this._enable_ui(true); |
| 503 |
}); |
| 504 |
} |
| 505 |
|
| 506 |
_display_verification_dialog(data, source_el) { |
| 507 |
const dlg = new VerificationDialog(data); |
| 508 |
this._element.classList.add("hidden"); |
| 509 |
document.getElementById("main-block").append(dlg.element()); |
| 510 |
dlg.show().finally(() => { |
| 511 |
dlg.element().remove(); |
| 512 |
this._element.classList.remove("hidden"); |
| 513 |
source_el.focus(); |
| 514 |
}); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
class VerificationDialog extends ModalDialog { |
| 519 |
constructor(data) { |
| 520 |
super({ title: "Domain verification information", buttons: [ "close" ] }); |
| 521 |
this._data = data; |
| 522 |
} |
| 523 |
|
| 524 |
_gen_content() { |
| 525 |
this._element.children[0].classList.add("verification"); |
| 526 |
this._content.classList.add("vertical-content"); |
| 527 |
const t1 = document.createElement("div"); |
| 528 |
t1.textContent = "To ensure that you are the owner of the domain, add a TXT record to the DNS settings at your domain registrar with the following content:"; |
| 529 |
const t2 = document.createElement("textarea"); |
| 530 |
t2.textContent = this._data; |
| 531 |
t2.readOnly = true; |
| 532 |
const t3 = document.createElement("div"); |
| 533 |
t3.textContent = "Once the domain has been successfully added, the TXT record can be deleted."; |
| 534 |
const t4 = document.createElement("div"); |
| 535 |
t4.textContent = "Important! Some registrars may require additional time to publish your verification code. If the tool can't find your new TXT record, wait an hour before you try again." |
| 536 |
this._content.append(t1, t2, t3, t4); |
| 537 |
} |
| 538 |
} |