Skip to content

Commit

Permalink
Add a new dialog class to replace the old one
Browse files Browse the repository at this point in the history
Add a new dialog class with the same signature and the same properties as the old Dialog implementation. The new one is using the shoelace web components and is working with promises to allow the calling method to react to the user input in the dialog.
  • Loading branch information
sascha-karnatz committed May 7, 2024
1 parent e45b528 commit 37caa29
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 49 deletions.
7 changes: 6 additions & 1 deletion app/assets/javascripts/alchemy/alchemy.dialog.js.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Dialog windows
#
# @deprecated use dialog.js instead
class window.Alchemy.Dialog

DEFAULTS:
Expand Down Expand Up @@ -229,6 +229,11 @@ window.Alchemy.closeCurrentDialog = (callback) ->
dialog.options.closed = callback
dialog.close()

# this is a intermediate solution to use the same method also with the new dialog
new_dialog = document.querySelector("sl-dialog")
if new_dialog
new_dialog.dialogClassInstance.onSubmitSuccess(callback)

# Utility function to open a new Dialog
window.Alchemy.openDialog = (url, options) ->
if !url
Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/alchemy/shoelace.scss
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ sl-tooltip {
sl-dialog {
&::part(panel) {
background-color: var(--color-grey_light);
min-height: var(--dialog-min-height);
--body-spacing: var(--spacing-4) var(--spacing-3);
--footer-spacing: var(--spacing-4) var(--spacing-3);
}
Expand Down
27 changes: 7 additions & 20 deletions app/javascript/alchemy_admin/components/dialog_link.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
export const DEFAULTS = {
header_height: 36,
size: "400x300",
padding: true,
title: "",
modal: true,
overflow: "visible",
ready: () => {},
closed: () => {}
}
import { Dialog } from "alchemy_admin/dialog"

export class DialogLink extends HTMLAnchorElement {
connectedCallback() {
Expand All @@ -20,21 +11,17 @@ export class DialogLink extends HTMLAnchorElement {
}

openDialog() {
this.dialog = new Alchemy.Dialog(
this.getAttribute("href"),
this.dialogOptions
)
this.dialog.open()
this.dialog = new Dialog(this.getAttribute("href"), this.dialogOptions)
this.dialog.open().catch(() => {
// do nothing on reject
// this only prevents an uncaught warning
})
}

get dialogOptions() {
const options = this.dataset.dialogOptions
return this.dataset.dialogOptions
? JSON.parse(this.dataset.dialogOptions)
: {}
return {
...DEFAULTS,
...options
}
}

get disabled() {
Expand Down
149 changes: 149 additions & 0 deletions app/javascript/alchemy_admin/dialog.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import { createHtmlElement } from "alchemy_admin/utils/dom_helpers"

export class Dialog {
#dialogComponent
#isOpen = false
#onReject
#onResolve

/**
* @param {string} url
* @param {object} options
*/
constructor(url, options = {}) {
this.url = url
this.options = { title: "", size: "300x400", padding: true, ...options }
}

/**
* load the content of given url and than open the dialog
* @returns {Promise<unknown>}
*/
open() {
this.#build()
// Show the dialog with the spinner after a small delay.
// in most cases the content of the dialog is already available and the spinner is not flashing
setTimeout(() => this.#openDialog, 300)

this.#loadContent().then((content) => {
// create the dialog markup and show the dialog
this.#dialogComponent.innerHTML = content
this.#select2Handling()
this.#openDialog()

// bind the current class instance to the DOM - element
// this should be an intermediate solution
// the main goal, is to close the dialog with the turbo:submit-end - event
this.#dialogComponent.dialogClassInstance = this

// the dialog is closing with the overlay, esc - key, or close - button
// the reject - callback will be fired, because the user decided to close the
// dialog without saving anything
this.#dialogComponent.addEventListener("sl-request-close", () => {
this.#removeDialog()
this.#onReject()
})
})

return new Promise((resolve, reject) => {
this.#onResolve = resolve
this.#onReject = reject
})
}

/**
* hide and remove dialog
* the open - promise will be resolved
* @param {function|undefined} callback
*/
onSubmitSuccess(callback = undefined) {
this.#dialogComponent.hide().then(() => {
this.#removeDialog()
this.#onResolve()

// add possibility to provide a callback
// this is a intermediate solution until the old Alchemy dialog is gone
if (callback) {
callback()
}
})
}

/**
* load content of the given url
* @returns {Promise<string>}
*/
async #loadContent() {
const response = await fetch(this.url, {
headers: { "X-Requested-With": "XMLHttpRequest" }
})
return await response.text()
}

/**
* create and append the dialog container to the DOM
*/
#build() {
this.#dialogComponent = createHtmlElement(`
<sl-dialog label="${this.title}" style="${this.styles}">
<alchemy-spinner size="medium"></alchemy-spinner>
</sl-dialog>
`)
document.body.append(this.#dialogComponent)
}

/**
* opens the dialog
* the dialog is only opening once
*/
#openDialog() {
if (!this.#isOpen) {
this.#dialogComponent.show()
this.#isOpen = true
}
}

/**
* remove the dialog from dom
*/
#removeDialog() {
this.#dialogComponent.addEventListener("sl-after-hide", () => {
this.#dialogComponent.remove()
this.#isOpen = false
})
}

/**
* activate and deactivate the focus trap of the sl-dialog, if a select2 - component is opening
*/
#select2Handling() {
$(this.#dialogComponent)
.on("select2-open", (evt) => {
this.#dialogComponent.modal.activateExternal()
})
.on("select2-close", (evt) => {
this.#dialogComponent.modal.deactivateExternal()
})
}

/**
* provide the custom properties for the dialog settings
* @returns {string}
*/
get styles() {
const sizes = this.options.size.split("x")
let styles = `--width: ${sizes[0]}px; --dialog-min-height: ${sizes[1]}px;`
if (!this.options.padding) {
styles += " --body-spacing: 0;"
}
return styles
}

/**
* get the title of the dialog
* @returns {string}
*/
get title() {
return this.options.title
}
}
12 changes: 6 additions & 6 deletions spec/features/admin/edit_elements_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@
expect(button).to have_content "Add Slide"
button.click

expect(page).to have_css(".alchemy-dialog")
within ".alchemy-dialog" do
expect(page).to have_css("sl-dialog")
within "sl-dialog" do
expect(page).to have_select("Element")
expect(page).to have_css("[panel='paste_element_tab']")
end
Expand All @@ -103,8 +103,8 @@

scenario "the add button now opens add element form with the clipboard tab" do
find("a.add-nestable-element-button").click
expect(page).to have_css(".alchemy-dialog")
within ".alchemy-dialog" do
expect(page).to have_css("sl-dialog")
within "sl-dialog" do
expect(page).to have_select("Element")
expect(page).to have_css("[panel='paste_element_tab']")
end
Expand All @@ -126,8 +126,8 @@
button = page.find(".add-nestable-element-button")
expect(button).to have_content "New element"
button.click
expect(page).to have_css(".alchemy-dialog")
within ".alchemy-dialog" do
expect(page).to have_css("sl-dialog")
within "sl-dialog" do
expect(page).to have_select("Element")
select2("Text", from: "Element")
click_button("Add")
Expand Down
16 changes: 8 additions & 8 deletions spec/features/admin/page_editing_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@
visit alchemy.edit_admin_page_path(a_page)
expect(page).to have_link_with_tooltip("New element")
click_link_with_tooltip("New element")
expect(page).to have_selector(".alchemy-dialog-body .simple_form")
within ".alchemy-dialog-body .simple_form" do
expect(page).to have_selector("sl-dialog .simple_form")
within "sl-dialog .simple_form" do
select2("Article", from: "Element")
click_button("Add")
end
expect(page).to_not have_selector(".alchemy-dialog-body")
expect(page).to_not have_selector("sl-dialog")
expect(page).to have_selector('.element-editor[data-element-name="article"]')
end
end
Expand Down Expand Up @@ -221,16 +221,16 @@
within ".sitemap_page[name='#{a_page.name}']" do
click_icon("settings-3")
end
expect(page).to have_selector(".alchemy-dialog-overlay.open")
expect(page).to have_selector("sl-dialog[open]")
end

context "when updating the name" do
it "saves the name" do
within(".alchemy-dialog.modal") do
within("sl-dialog") do
find("input#page_name").set("name with some %!x^)'([@!{}]|/?:# characters")
find(".edit_page .submit button").click
end
expect(page).to_not have_selector(".alchemy-dialog-overlay.open")
expect(page).to_not have_selector("sl-dialog[open]")
expect(page).to have_selector("#sitemap a.sitemap_pagename_link", text: "name with some %!x^)'([@!{}]|/?:# characters")
end
end
Expand All @@ -239,12 +239,12 @@
let!(:new_parent) { create(:alchemy_page) }

it "can change page parent" do
within(".alchemy-dialog.modal") do
within(".simple_form:first-child") do
expect(page).to have_css("#s2id_page_parent_id")
select2_search(new_parent.name, from: "Parent")
find(".edit_page .submit button").click
end
expect(page).to_not have_selector(".alchemy-dialog-overlay.open")
expect(page).to_not have_selector("sl-dialog[open]")
expect(page).to have_selector("#sitemap .sitemap_url", text: "/#{new_parent.urlname}/#{a_page.urlname}")
end
end
Expand Down
18 changes: 4 additions & 14 deletions spec/javascript/alchemy_admin/components/dialog_link.spec.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
import "alchemy_admin/components/dialog_link"
import { DEFAULTS } from "alchemy_admin/components/dialog_link"
import { renderComponent } from "./component.helper"

class Dialog {
open() {}
}

beforeEach(() => {
global.Alchemy = {
Dialog: Dialog
}
})
import { Dialog } from "alchemy_admin/dialog"

describe("alchemy-dialog-link", () => {
it("opens a dialog on click", () => {
const html = `
<a type="submit" is="alchemy-dialog-link">Open Dialog</a>
`
const openSpy = jest.spyOn(Dialog.prototype, "open")
Dialog.prototype.open = jest.fn(() => Promise.resolve())
const dialogLink = renderComponent("alchemy-dialog-link", html)
const click = new Event("click", { bubbles: true })

dialogLink.dispatchEvent(click)
expect(openSpy).toHaveBeenCalled()
expect(Dialog.prototype.open).toHaveBeenCalled()
})

it("has default dialogOptions", () => {
Expand All @@ -31,7 +21,7 @@ describe("alchemy-dialog-link", () => {
`
const dialogLink = renderComponent("alchemy-dialog-link", html)

expect(dialogLink.dialogOptions).toEqual(DEFAULTS)
expect(dialogLink.dialogOptions).toEqual({})
})

it("parses dialogOptions from dataset", () => {
Expand Down

0 comments on commit 37caa29

Please sign in to comment.