]> jfr.im git - irc/unrealircd/unrealircd-webpanel.git/blame - js/bs-modal.js
Logs: you can now enter an IP address or nickid in the search field.
[irc/unrealircd/unrealircd-webpanel.git] / js / bs-modal.js
CommitLineData
905f0895
VP
1/**
2 * Generate a bootstrap modal
3 * Mandatory:
4 * @param {string} title - The modal title
5 * @param {string} body - HTML for the body
6 * @param {string} footer - HTML for the footer
7 *
8 * Optional:
9 * @param {string} size - the bootstrap size category for modals (sm, lg, xl)
10 * @param {boolean} static - whether or not to make the backdrop static, forcing the user to respond to the dialog
11 * @param {boolean} show - whether or not to automatically show the modal
12 * @returns {string} returns the ID
13 */
14
15function bsModal(title, body, footer, size = null, static = false, show = false)
16{
17 /* generate a random number between 1000 and 90000 to use as an id */
18 const min = 1000;
19 const max = 90000;
20 id = Date.now().toString(36); // base36 unique id
21 ourSize = "";
22 if (size)
23 ourSize += "modal-" + size;
24
25 const m1 = document.createElement("div");
26 const m2 = m1.cloneNode();
27 const m3 = m1.cloneNode();
28 const mHeader = m1.cloneNode();
29 const mBody = m1.cloneNode();
30 const mFooter = m1.cloneNode();
31
32 m1.classList.add("modal", "fade");
33 m1.id = id;
34 m1.role = "dialog";
35 m1.ariaHidden = "true";
36
37 m2.classList.add("modal-dialog", "modal-dialog-centered");
38 if (ourSize.length)
39 m2.classList.add(ourSize);
40
41 m2.role = "document";
42 m2.id = id + "-2";
43
44 m3.classList.add("modal-content");
45 m3.id = id + "-3";
46
47 mHeader.classList.add("modal-header");
48 mHeader.id = id + "-header";
49 mHeader.innerHTML =`<h5 class="modal-title" id="` + id + `"-title">` + title + `</h5>
50 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
51 <span aria-hidden="true">&times;</span></button>`;
52
53 mBody.classList.add("modal-body");
54 mBody.id = id + "-body";
55 mBody.innerHTML = body;
56
57 mFooter.classList.add("modal-footer");
58 mFooter.id = id + "-footer";
59 mFooter.innerHTML = footer;
60
61 m1.appendChild(m2);
62 m2.appendChild(m3);
63 m3.appendChild(mHeader);
64 m3.appendChild(mBody);
65 m3.appendChild(mFooter);
66
67 document.body.append(m1);
68
69 if (static)
70 $('#' + m1.id).modal({ backdrop: "static" });
71
72 if (show)
73 $('#' + m1.id).modal('show');
74}