Skip to content

Commit

Permalink
Locally initialize SortableElements
Browse files Browse the repository at this point in the history
We have two places where we need to initialize SortableJS
for elements.

1) The ElementsWindow and its top level elements
2) The nested elements inside of each element-editor

Instead of using the global Alchemy object to initialize
them we can import the file locally and init it where it needs
to be. This makes it possible to remove another global
call from the `create.js.erb` file.
  • Loading branch information
tvdeyen committed Apr 3, 2024
1 parent 20ff6ed commit 1e5766c
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 9 deletions.
2 changes: 0 additions & 2 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { LinkDialog } from "alchemy_admin/link_dialog"
import pictureSelector from "alchemy_admin/picture_selector"
import pleaseWaitOverlay from "alchemy_admin/please_wait_overlay"
import Sitemap from "alchemy_admin/sitemap"
import SortableElements from "alchemy_admin/sortable_elements"
import Spinner from "alchemy_admin/spinner"
import PagePublicationFields from "alchemy_admin/page_publication_fields"
import { reloadPreview } from "alchemy_admin/components/preview_window"
Expand Down Expand Up @@ -64,7 +63,6 @@ Object.assign(Alchemy, {
pictureSelector,
pleaseWaitOverlay,
Sitemap,
SortableElements,
Spinner,
PagePublicationFields,
reloadPreview
Expand Down
2 changes: 2 additions & 0 deletions app/javascript/alchemy_admin/components/element_editor.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ImageLoader from "alchemy_admin/image_loader"
import fileEditors from "alchemy_admin/file_editors"
import pictureEditors from "alchemy_admin/picture_editors"
import SortableElements from "alchemy_admin/sortable_elements"
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link"
import { post } from "alchemy_admin/utils/ajax"
import { createHtmlElement } from "alchemy_admin/utils/dom_helpers"
Expand Down Expand Up @@ -43,6 +44,7 @@ export class ElementEditor extends HTMLElement {
`#${this.id} .ingredient-editor.file, #${this.id} .ingredient-editor.audio, #${this.id} .ingredient-editor.video`
)
pictureEditors(`#${this.id} .ingredient-editor.picture`)
SortableElements(`#${this.id} .nested-elements`)
}

handleEvent(event) {
Expand Down
4 changes: 3 additions & 1 deletion app/javascript/alchemy_admin/components/elements_window.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import SortableElements from "alchemy_admin/sortable_elements"

class ElementsWindow extends HTMLElement {
#visible = true

Expand All @@ -16,7 +18,7 @@ class ElementsWindow extends HTMLElement {
.querySelector(window.location.hash)
?.trigger("FocusElementEditor.Alchemy")
}
Alchemy.SortableElements()
SortableElements()
}

collapseAllElements() {
Expand Down
4 changes: 0 additions & 4 deletions app/javascript/alchemy_admin/sortable_elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,4 @@ export default function SortableElements(selector) {
})

sortable_areas.forEach((element) => createSortable(element))

document.querySelectorAll(".nested-elements").forEach((nestedElement) => {
createSortable(nestedElement)
})
}
2 changes: 0 additions & 2 deletions app/views/alchemy/admin/elements/create.js.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
$element_area.append(element_html);
<%- end -%>

Alchemy.SortableElements('[data-element-id="<%= @element.id %>"] .nested-elements');

Alchemy.growl('<%= Alchemy.t(:successfully_added_element) %>');
Alchemy.closeCurrentDialog();

Expand Down

0 comments on commit 1e5766c

Please sign in to comment.