Skip to content

Commit

Permalink
convert Initializer from Coffeescript to Javascript
Browse files Browse the repository at this point in the history
Move the initializer also into the Javascript folder. It make sense to move the higher level Javascript first, because this will prevent loading raise conditions.
  • Loading branch information
sascha-karnatz committed Jul 4, 2023
1 parent c239df1 commit 822f9dd
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 57 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 @@ -32,7 +32,6 @@
//= require alchemy/alchemy.string_extension
//= require alchemy/alchemy.link_dialog
//= require alchemy/alchemy.list_filter
//= require alchemy/alchemy.initializer
//= require alchemy/alchemy.uploader
//= require alchemy/alchemy.preview_window
//= require alchemy/alchemy.spinner
Expand Down
56 changes: 0 additions & 56 deletions app/assets/javascripts/alchemy/alchemy.initializer.js.coffee

This file was deleted.

8 changes: 8 additions & 0 deletions app/javascript/alchemy_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link"
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"
Expand All @@ -26,9 +27,16 @@ Object.assign(Alchemy, {
pictureEditors,
ImageLoader: ImageLoader.init,
ImageCropper,
Initializer,
IngredientAnchorLink,
Datepicker,
Sitemap,
Tinymce,
PagePublicationFields
})

$(document).on("turbo:load", Initializer)

$(document).on("turbo:before-fetch-request", function () {
Alchemy.Tinymce.removeFrom($(".has_tinymce"))
})
58 changes: 58 additions & 0 deletions app/javascript/alchemy_admin/initializer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* add change listener to select to redirect the user after selecting another locale or site
* @param {string} selectId
* @param {string} parameterName
*/
function selectHandler(selectId, parameterName) {
$(`select#${selectId}`).on("change", function (e) {
let url = window.location.pathname
let delimiter = url.match(/\?/) ? "&" : "?"
Turbo.visit(`${url}${delimiter}${parameterName}=${$(this).val()}`, {})
})
}

function Initialize() {
// We obviously have javascript enabled.
$("html").removeClass("no-js")

// Initialize the GUI.
Alchemy.GUI.init()

// Fade all growl notifications.
if ($("#flash_notices").length > 0) {
Alchemy.Growler.fade()
}

// Add observer for please wait overlay.
$(".please_wait, .button_with_label form :submit")
.not("*[data-alchemy-confirm]")
.click(Alchemy.pleaseWaitOverlay)

// Hack for enabling tab focus for <a>'s styled as button.
$("a.button").attr({ tabindex: 0 })

// Locale select handler
selectHandler("change_locale", "admin_locale")

// Site select handler
selectHandler("change_site", "site_id")

// Submit forms of selects with `data-autosubmit="true"`
$('select[data-auto-submit="true"]').on("change", function () {
$(this.form).submit()
})

// Attaches the image loader on all images
Alchemy.ImageLoader("#main_content")

// Override the filter of keymaster.js so we can blur the fields on esc key.
key.filter = function (event) {
let tagName = (event.target || event.srcElement).tagName
return (
key.isPressed("esc") ||
!(tagName === "INPUT" || tagName === "SELECT" || tagName === "TEXTAREA")
)
}
}

export default Initialize

0 comments on commit 822f9dd

Please sign in to comment.