-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2818 from AlchemyCMS/delete-element-button
Add delete-element-button
- Loading branch information
Showing
11 changed files
with
202 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 0 additions & 85 deletions
85
app/assets/javascripts/alchemy/alchemy.confirm_dialog.js.coffee
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
app/javascript/alchemy_admin/components/element_editor/delete_element_button.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { growl } from "alchemy_admin/growler" | ||
import { reloadPreview } from "alchemy_admin/components/preview_window" | ||
import { confirmToDeleteDialog } from "alchemy_admin/confirm_dialog" | ||
|
||
export class DeleteElementButton extends HTMLElement { | ||
constructor() { | ||
super() | ||
this.addEventListener("click", this) | ||
} | ||
|
||
handleEvent() { | ||
confirmToDeleteDialog(this.url, { message: this.message }).then( | ||
(response) => { | ||
this.#removeElement(response) | ||
} | ||
) | ||
} | ||
|
||
#removeElement(response) { | ||
const elementEditor = this.closest("alchemy-element-editor") | ||
elementEditor.addEventListener("transitionend", () => { | ||
if (elementEditor.fixed) { | ||
Alchemy.FixedElements.removeTab(elementEditor.elementId) | ||
} | ||
elementEditor.remove() | ||
}) | ||
elementEditor.classList.add("dismiss") | ||
growl(response.message) | ||
reloadPreview() | ||
} | ||
|
||
get url() { | ||
return this.getAttribute("href") | ||
} | ||
|
||
get message() { | ||
return this.getAttribute("message") | ||
} | ||
} | ||
|
||
customElements.define("alchemy-delete-element-button", DeleteElementButton) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
import { growl } from "alchemy_admin/growler" | ||
import pleaseWaitOverlay from "alchemy_admin/please_wait_overlay" | ||
|
||
Alchemy.Dialog = window.Alchemy.Dialog || class Dialog {} | ||
|
||
class ConfirmDialog extends Alchemy.Dialog { | ||
constructor(message, options = {}) { | ||
const DEFAULTS = { | ||
size: "300x100", | ||
title: "Please confirm", | ||
ok_label: "Yes", | ||
cancel_label: "No", | ||
on_ok() {} | ||
} | ||
|
||
options = { ...DEFAULTS, ...options } | ||
|
||
super("", options) | ||
this.message = message | ||
this.options = options | ||
} | ||
|
||
load() { | ||
this.dialog_title.text(this.options.title) | ||
this.dialog_body.html(`<p>${this.message}</p>`) | ||
this.dialog_body.append(this.build_buttons()) | ||
this.bind_buttons() | ||
} | ||
|
||
build_buttons() { | ||
const $btn_container = $('<div class="alchemy-dialog-buttons" />') | ||
this.cancel_button = $( | ||
`<button class=\"cancel secondary\">${this.options.cancel_label}</button>` | ||
) | ||
this.ok_button = $( | ||
`<button class=\"confirm\">${this.options.ok_label}</button>` | ||
) | ||
$btn_container.append(this.cancel_button) | ||
$btn_container.append(this.ok_button) | ||
return $btn_container | ||
} | ||
|
||
bind_buttons() { | ||
this.cancel_button.trigger("focus") | ||
this.cancel_button.on("click", () => { | ||
this.close() | ||
return false | ||
}) | ||
this.ok_button.on("click", () => { | ||
this.close() | ||
this.options.on_ok() | ||
return false | ||
}) | ||
} | ||
} | ||
|
||
// Opens a confirm dialog | ||
// | ||
// Arguments: | ||
// | ||
// message - The message that will be displayed to the user (String) | ||
// | ||
// Options: | ||
// | ||
// title: '' - The title of the overlay window (String) | ||
// cancel_label: '' - The label of the cancel button (String) | ||
// ok_label: '' - The label of the ok button (String) | ||
// on_ok: null - The function to invoke on confirmation (Function) | ||
// | ||
export function openConfirmDialog(message, options = {}) { | ||
const dialog = new ConfirmDialog(message, options) | ||
dialog.open() | ||
return dialog | ||
} | ||
|
||
// Opens a confirm to delete dialog | ||
// | ||
// Arguments: | ||
// | ||
// url - The url to the server delete action. Uses DELETE as HTTP method. (String) | ||
// opts - An options object (Object) | ||
// | ||
// Options: | ||
// | ||
// title: '' - The title of the confirmation window (String) | ||
// message: '' - The message that will be displayed to the user (String) | ||
// ok_label: '' - The label for the ok button (String) | ||
// cancel_label: '' - The label for the cancel button (String) | ||
// | ||
export function confirmToDeleteDialog(url, opts = {}) { | ||
return new Promise((resolve, reject) => { | ||
const options = { | ||
on_ok() { | ||
pleaseWaitOverlay() | ||
$.ajax({ | ||
url, | ||
type: "DELETE", | ||
error(xhr, _status, error) { | ||
const type = xhr.status === 403 ? "warning" : "error" | ||
growl(xhr.responseText || error, type) | ||
reject(error) | ||
}, | ||
complete(response) { | ||
pleaseWaitOverlay(false) | ||
resolve(response) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
openConfirmDialog(opts.message, { ...options, ...opts }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.