Skip to content

Commit

Permalink
Move fixed elements functions into alchemy_admin package
Browse files Browse the repository at this point in the history
Move createTab and removeTab functions into alchemy_admin - package. These two functions are still available in the Alchemy.FixedElements - namespace until we move all calls out of these js.erb - files. The functions are also migrated to vanilla Javascript.
  • Loading branch information
sascha-karnatz committed Apr 30, 2024
1 parent 840d7ff commit 1e59d50
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 38 deletions.
1 change: 0 additions & 1 deletion app/assets/javascripts/alchemy/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@
//= require handlebars
//= require alchemy/templates
//= require alchemy/alchemy.dialog
//= require alchemy/alchemy.fixed_elements
//= require alchemy/alchemy.image_overlay
36 changes: 0 additions & 36 deletions app/assets/javascripts/alchemy/alchemy.fixed_elements.js

This file was deleted.

2 changes: 2 additions & 0 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Rails from "@rails/ujs"
import GUI from "alchemy_admin/gui"
import { translate } from "alchemy_admin/i18n"
import Dirty from "alchemy_admin/dirty"
import * as FixedElements from "alchemy_admin/fixed_elements"
import { growl } from "alchemy_admin/growler"
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link"
import ImageLoader from "alchemy_admin/image_loader"
Expand Down Expand Up @@ -39,6 +40,7 @@ Object.assign(Alchemy, {
...Dirty,
GUI,
t: translate, // Global utility method for translating a given string
FixedElements,
growl,
ImageLoader: ImageLoader.init,
ImageCropper,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { removeTab } from "alchemy_admin/fixed_elements"
import { growl } from "alchemy_admin/growler"
import { reloadPreview } from "alchemy_admin/components/preview_window"
import { confirmToDeleteDialog } from "alchemy_admin/confirm_dialog"
Expand All @@ -20,7 +21,7 @@ export class DeleteElementButton extends HTMLElement {
const elementEditor = this.closest("alchemy-element-editor")
elementEditor.addEventListener("transitionend", () => {
if (elementEditor.fixed) {
Alchemy.FixedElements.removeTab(elementEditor.elementId)
removeTab(elementEditor.elementId)
}
elementEditor.remove()
})
Expand Down
24 changes: 24 additions & 0 deletions app/javascript/alchemy_admin/fixed_elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Creates a fixed element tab.
export function createTab(element_id, label) {
const fixed_elements = document.getElementById("fixed-elements")
const panel_name = `fixed-element-${element_id}`

const tab = `<sl-tab slot="nav" panel="${panel_name}">${label}</sl-tab>`
const panel = `<sl-tab-panel name="${panel_name}" style="--padding: 0" />`

fixed_elements.innerHTML += tab + panel

window.requestAnimationFrame(function () {
fixed_elements.show(panel_name)
})
}

export function removeTab(element_id) {
const fixed_elements = document.getElementById("fixed-elements")
const panel_name = `fixed-element-${element_id}`

fixed_elements.querySelector(`sl-tab[panel="${panel_name}"]`).remove()
fixed_elements.querySelector(`sl-tab-panel[name="${panel_name}"]`).remove()

fixed_elements.show("main-content-elements")
}

0 comments on commit 1e59d50

Please sign in to comment.