Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate datepicker into a web component #2554

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/alchemy/admin/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -37,7 +37,6 @@ Object.assign(Alchemy, {
ImageCropper,
Initializer,
IngredientAnchorLink,
Datepicker,
Sitemap,
Tinymce,
PagePublicationFields
Expand Down
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -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" })
1 change: 0 additions & 1 deletion app/javascript/alchemy_admin/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down