From 07e843f72bf3e8bc53b71b6df98b3b4dc0adfc86 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Stalder?=
Date: Wed, 22 Sep 2021 15:26:42 +0200
Subject: [PATCH] Refactor non-modal dialog widget
Remove jQuery, update markup, apply code style
refs #142
---
.../_examples/non-modal-dialog/example.css | 2 +-
.../_examples/non-modal-dialog/example.js | 125 +++++++++---------
.../_examples/non-modal-dialog/index.html | 14 +-
3 files changed, 76 insertions(+), 65 deletions(-)
diff --git a/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.css b/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.css
index 0d43c9d3..4152541c 100644
--- a/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.css
+++ b/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.css
@@ -37,4 +37,4 @@ svg#definition {
width: 16px;
height: 16px;
fill: #000;
-}
\ No newline at end of file
+}
diff --git a/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.js b/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.js
index a3605541..ac4937d5 100644
--- a/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.js
+++ b/pages/examples/widgets/dialog/_examples/non-modal-dialog/example.js
@@ -1,70 +1,73 @@
-;(function () {
- var AdgDialog
+class AdgDialog {
+ constructor (el) {
+ this.openButton = el
+ this.elementToOpenId = this.openButton.getAttribute('data-adg-dialog')
+ this.initContainer(this.elementToOpenId)
+ this.initOpenButton()
+ this.hide()
+ }
+
+ initOpenButton () {
+ this.openButton.setAttribute('aria-expanded', 'false')
- AdgDialog = class AdgDialog {
- constructor (el) {
- this.$openButton = $(el)
- this.initContainer(this.$openButton.attr('data-adg-dialog'))
- this.initOpenButton()
- }
+ const dialogHintElement = document.createElement('span')
+ dialogHintElement.classList.add('adg-visually-hidden')
+ dialogHintElement.innerText = ' (dialog)'
- initOpenButton () {
- this.$openButton.attr('aria-expanded', false)
- this.$openButton.append(
- ' (dialog)'
- )
- return this.$openButton.click(e => {
- if (this.$container.is(':visible')) {
- return this.hide()
- } else {
- return this.show()
- }
- })
- }
+ this.openButton.appendChild(dialogHintElement)
- initContainer (id) {
- this.$container = $(`#${id}`)
- this.$container.attr('data-adg-dialog-container', true)
- this.initCloseButton()
- return this.initConfirmButton()
- }
+ this.openButton.addEventListener('click', () => {
+ if (this.container.hidden) {
+ this.show()
+ } else {
+ this.hide()
+ }
+ })
+ }
- initConfirmButton () {
- this.$confirmButton = $(
- ''
- )
- this.$container.append(this.$confirmButton)
- return this.$confirmButton.click(() => {
- return this.hide()
- })
- }
+ initContainer (id) {
+ this.container = document.getElementById(id)
+ this.container.setAttribute('data-adg-dialog-container', '')
+ this.initCloseButton()
+ this.initConfirmButton()
+ }
- initCloseButton () {
- this.$closeButton = $(
- ''
- )
- this.$container.prepend(this.$closeButton)
- return this.$closeButton.click(() => {
- return this.hide()
- })
- }
+ initConfirmButton () {
+ this.confirmButton = document.createElement('button')
+ this.confirmButton.setAttribute('type', 'button')
+ this.confirmButton.innerHTML = 'Confirm (close)'
- show () {
- this.$container.attr('hidden', false)
- this.$openButton.attr('aria-expanded', true)
- return this.$closeButton.focus()
- }
+ this.confirmButton.addEventListener('click', () => this.hide())
- hide () {
- this.$container.attr('hidden', true)
- this.$openButton.attr('aria-expanded', false)
- return this.$openButton.focus()
- }
+ this.container.append(this.confirmButton)
}
- $(document).ready(function () {
- return $('[data-adg-dialog]').each(function () {
- return new AdgDialog(this)
- })
- })
-}.call(this))
+ initCloseButton () {
+ this.closeButton = document.createElement('button')
+ this.closeButton.setAttribute('type', 'button')
+ this.closeButton.classList.add('adg-dialog-icon')
+ this.closeButton.innerHTML = 'Close dialog'
+
+ this.closeButton.addEventListener('click', () => this.hide())
+
+ this.container.prepend(this.closeButton)
+ }
+
+ show () {
+ this.container.hidden = false
+ this.openButton.setAttribute('aria-expanded', 'true')
+ this.closeButton.focus()
+ }
+
+ hide () {
+ this.container.hidden = true
+ this.openButton.setAttribute('aria-expanded', 'false')
+ this.openButton.focus()
+ }
+}
+
+const dialogs = []
+
+document
+ .querySelectorAll('[data-adg-dialog]')
+ .forEach((element) => dialogs.push(new AdgDialog(element)))
diff --git a/pages/examples/widgets/dialog/_examples/non-modal-dialog/index.html b/pages/examples/widgets/dialog/_examples/non-modal-dialog/index.html
index c6b94df8..9977d3da 100644
--- a/pages/examples/widgets/dialog/_examples/non-modal-dialog/index.html
+++ b/pages/examples/widgets/dialog/_examples/non-modal-dialog/index.html
@@ -1,4 +1,12 @@
-
+
@@ -12,11 +20,11 @@
Some text after.
-
+
Here are the terms and conditions.
Please read them carefully...
-
\ No newline at end of file
+