summaryrefslogtreecommitdiff
path: root/WebInterface/src/js/modules/ui/components/modal/modal.js
diff options
context:
space:
mode:
authorDennis Kobert <d-kobert@web.de>2019-06-12 21:51:39 +0200
committerDennis Kobert <d-kobert@web.de>2019-06-12 21:51:39 +0200
commit9e9c1c822a64c0a65033b7eed07ea661a385cecc (patch)
treeee699d4e93bb4204f5f4e04cd14f6d77365b81b4 /WebInterface/src/js/modules/ui/components/modal/modal.js
parent304437b834e8c87687f68333ae67a13ee1377a73 (diff)
parent3a3d0fc3d4733f8908e23a03f860d76340479ec4 (diff)
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'WebInterface/src/js/modules/ui/components/modal/modal.js')
-rw-r--r--WebInterface/src/js/modules/ui/components/modal/modal.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/WebInterface/src/js/modules/ui/components/modal/modal.js b/WebInterface/src/js/modules/ui/components/modal/modal.js
new file mode 100644
index 0000000..c4c5119
--- /dev/null
+++ b/WebInterface/src/js/modules/ui/components/modal/modal.js
@@ -0,0 +1,66 @@
+/**
+ * Parent class to create Modals on the screen
+ * Contains no content, as that is implemented by child classes
+ */
+export default class Modal {
+ /**
+ * Creates a new modal with a title and empty content
+ * @param {string} titleString Title to show at the top of the modal
+ */
+ constructor(titleString) {
+ let modalBackground = document.createElement('div');
+ let modal = document.createElement('div');
+ let title = document.createElement('h1');
+ let body = document.createElement('div');
+
+ modalBackground.className = 'modal-container';
+ modal.className = 'modal';
+ title.className = 'modal-title';
+ body.className = 'modal-body';
+ title.textContent = titleString;
+
+ modal.appendChild(title);
+ modal.appendChild(body);
+ modalBackground.appendChild(modal);
+ document.body.appendChild(modalBackground);
+
+ this.bg = modalBackground;
+ this.modal = modal;
+ this.title = title;
+ this.body = body;
+
+ this.registerEvents();
+ }
+
+ /**
+ * Register event to close if clicked outside of modal
+ * Clicking on the modal itself should not close it though
+ */
+ registerEvents() {
+ this.modal.addEventListener('click', (e) => {
+ e.stopPropagation();
+ });
+
+ this.bg.addEventListener('click', () => {
+ this.close();
+ });
+ }
+
+ /**
+ * Fades modal out and removes it from the flow of the document
+ */
+ close() {
+ this.bg.classList.add('hidden');
+ this.bg.addEventListener('transitionend', () => {
+ document.body.removeChild(this.bg);
+ });
+ }
+
+ /**
+ * Puts text in the body
+ * @param {string} text Text to put into the body
+ */
+ setBodyText(text) {
+ this.body.textContent = text;
+ }
+}