diff --git a/app/helpers/alchemy/admin/base_helper.rb b/app/helpers/alchemy/admin/base_helper.rb index 6e22212e5b..4e69fa8348 100644 --- a/app/helpers/alchemy/admin/base_helper.rb +++ b/app/helpers/alchemy/admin/base_helper.rb @@ -323,7 +323,7 @@ def alchemy_datepicker(object, method, html_options = {}) value = date&.iso8601 text_field object.class.name.demodulize.underscore.to_sym, - method.to_sym, {:type => "text", :class => type, "data-datepicker-type" => type, :value => value}.merge(html_options) + method.to_sym, {:type => "text", :class => type, :is => "alchemy-datepicker", "data-datepicker-type" => type, :value => value}.merge(html_options) end # Render a hint icon with tooltip for given object. diff --git a/app/javascript/alchemy_admin.js b/app/javascript/alchemy_admin.js index c22a36422d..05cbc4da72 100644 --- a/app/javascript/alchemy_admin.js +++ b/app/javascript/alchemy_admin.js @@ -11,13 +11,13 @@ import pictureEditors from "alchemy_admin/picture_editors" import ImageLoader from "alchemy_admin/image_loader" import ImageCropper from "alchemy_admin/image_cropper" import Initializer from "alchemy_admin/initializer" -import Datepicker from "alchemy_admin/datepicker" import Sitemap from "alchemy_admin/sitemap" import Tinymce from "alchemy_admin/tinymce" import PagePublicationFields from "alchemy_admin/page_publication_fields" // Web Components import "alchemy_admin/components/char_counter" +import "alchemy_admin/components/datepicker" // Global Alchemy object if (typeof window.Alchemy === "undefined") { @@ -37,7 +37,6 @@ Object.assign(Alchemy, { ImageCropper, Initializer, IngredientAnchorLink, - Datepicker, Sitemap, Tinymce, PagePublicationFields diff --git a/app/javascript/alchemy_admin/datepicker.js b/app/javascript/alchemy_admin/components/datepicker.js similarity index 58% rename from app/javascript/alchemy_admin/datepicker.js rename to app/javascript/alchemy_admin/components/datepicker.js index 6ad0c2b08e..289e5bcc7c 100644 --- a/app/javascript/alchemy_admin/datepicker.js +++ b/app/javascript/alchemy_admin/components/datepicker.js @@ -1,17 +1,10 @@ import flatpickr from "flatpickr" -export default function Datepicker(scope = document) { - if (scope === "") { - scope = document - } else if (scope instanceof String) { - scope = document.querySelectorAll(scope) - } - - const datepickerInputs = scope.querySelectorAll("input[data-datepicker-type]") +class Datepicker extends HTMLInputElement { + constructor() { + super() - // Initializes the datepickers - datepickerInputs.forEach((input) => { - const type = input.dataset.datepickerType + const type = this.dataset.datepickerType const options = { // alchemy_i18n supports `zh_CN` etc., but flatpickr only has two-letter codes (`zh`) locale: Alchemy.locale.slice(0, 2), @@ -26,6 +19,9 @@ export default function Datepicker(scope = document) { Alchemy.setElementDirty(instance.element.closest(".element-editor")) } } - flatpickr(input, options) - }) + + flatpickr(this, options) + } } + +customElements.define("alchemy-datepicker", Datepicker, { extends: "input" }) diff --git a/app/javascript/alchemy_admin/gui.js b/app/javascript/alchemy_admin/gui.js index fc7772088e..708e4e5baf 100644 --- a/app/javascript/alchemy_admin/gui.js +++ b/app/javascript/alchemy_admin/gui.js @@ -2,7 +2,6 @@ import TagsAutocomplete from "alchemy_admin/tags_autocomplete" function init(scope) { Alchemy.SelectBox(scope) - Alchemy.Datepicker(scope && scope.selector) Alchemy.Tooltips(scope) Alchemy.Buttons.observe(scope) if (!scope) {