-
-
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.
Rename Anchor Select into Dom Id Select
It was previously a dom id - select and technically it is search for dom nodes with ids. It also adds an enabled and disabled state on the dom id - select. Also separate both dom id selects into slightly different components and add an enable and disable mechanic to alchemy-select.
- Loading branch information
1 parent
5bc6769
commit 92b3d9c
Showing
10 changed files
with
164 additions
and
115 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
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.
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,69 @@ | ||
import { get } from "alchemy_admin/utils/ajax" | ||
import { translate } from "alchemy_admin/i18n" | ||
|
||
class DomIdSelect extends HTMLElement { | ||
dataItem(hash) { | ||
return { | ||
id: `#${hash}`, | ||
text: `#${hash}` | ||
} | ||
} | ||
|
||
get selectElement() { | ||
return this.querySelector('select[is="alchemy-select"]') | ||
} | ||
} | ||
|
||
class DomIdApiSelect extends DomIdSelect { | ||
#pageId = undefined | ||
|
||
connectedCallback() { | ||
this.page = this.getAttribute("page") | ||
} | ||
|
||
async #fetchDomIds() { | ||
const result = await get(Alchemy.routes.api_ingredients_path, { | ||
page_id: this.#pageId | ||
}) | ||
const options = result.data.ingredients | ||
.filter((ingredient) => ingredient.data?.dom_id) | ||
.map((ingredient) => this.dataItem(ingredient.data.dom_id)) | ||
const prompt = | ||
options.length > 0 ? translate("None") : translate("No anchors found") | ||
|
||
this.selectElement.setOptions(options, prompt) | ||
this.selectElement.enable() | ||
} | ||
|
||
#reset() { | ||
// wait a tick to initialize the alchemy-select | ||
requestAnimationFrame(() => { | ||
this.selectElement.disable() | ||
this.selectElement.setOptions([], translate("Select a page first")) | ||
}) | ||
} | ||
|
||
set page(pageId) { | ||
this.#pageId = pageId | ||
pageId ? this.#fetchDomIds() : this.#reset() | ||
} | ||
} | ||
|
||
class DomIdPreviewSelect extends DomIdSelect { | ||
connectedCallback() { | ||
// wait a tick to let the browser initialize the inner select component | ||
requestAnimationFrame(() => { | ||
const frame = document.getElementById("alchemy_preview_window") | ||
const elements = frame.contentDocument?.querySelectorAll("[id]") || [] | ||
if (elements.length > 0) { | ||
const options = Array.from(elements).map((element) => { | ||
return this.dataItem(element.id) | ||
}) | ||
this.selectElement.setOptions(options, translate("None")) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
customElements.define("alchemy-dom-id-api-select", DomIdApiSelect) | ||
customElements.define("alchemy-dom-id-preview-select", DomIdPreviewSelect) |
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
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
Oops, something went wrong.