diff --git a/spec/dummy_app/app/assets/javascripts/rails_admin/custom/ui.js b/spec/dummy_app/app/assets/javascripts/rails_admin/custom/ui.js new file mode 100644 index 0000000000..fe0ab0e7e8 --- /dev/null +++ b/spec/dummy_app/app/assets/javascripts/rails_admin/custom/ui.js @@ -0,0 +1,17 @@ +window.domReadyTriggered = []; + +document.addEventListener("rails_admin:dom_ready", function () { + window.domReadyTriggered.push("plainjs/colon"); +}); + +document.addEventListener("rails_admin.dom_ready", function () { + window.domReadyTriggered.push("plainjs/dot"); +}); + +$(document).on("rails_admin:dom_ready", function () { + window.domReadyTriggered.push("jquery/colon"); +}); + +$(document).on("rails_admin.dom_ready", function () { + window.domReadyTriggered.push("jquery/dot"); +}); diff --git a/spec/dummy_app/app/javascript/rails_admin.js b/spec/dummy_app/app/javascript/rails_admin.js index c45b2a786d..0c8cc48d1d 100644 --- a/spec/dummy_app/app/javascript/rails_admin.js +++ b/spec/dummy_app/app/javascript/rails_admin.js @@ -4,3 +4,21 @@ import "trix"; import "@rails/actiontext"; import * as ActiveStorage from "@rails/activestorage"; ActiveStorage.start(); + +window.domReadyTriggered = []; + +document.addEventListener("rails_admin:dom_ready", function () { + window.domReadyTriggered.push("plainjs/colon"); +}); + +document.addEventListener("rails_admin.dom_ready", function () { + window.domReadyTriggered.push("plainjs/dot"); +}); + +$(document).on("rails_admin:dom_ready", function () { + window.domReadyTriggered.push("jquery/colon"); +}); + +$(document).on("rails_admin.dom_ready", function () { + window.domReadyTriggered.push("jquery/dot"); +}); diff --git a/spec/integration/rails_admin_spec.rb b/spec/integration/rails_admin_spec.rb index bb6ccc37f9..f9fd9645e1 100644 --- a/spec/integration/rails_admin_spec.rb +++ b/spec/integration/rails_admin_spec.rb @@ -197,7 +197,7 @@ is_expected.to have_content 'Details for Player' end - it 'triggers rails_admin.dom_ready right after a validation error' do + it 'triggers rails_admin:dom_ready right after a validation error' do visit edit_path(model_name: 'player', id: player.id) fill_in 'player[name]', with: 'on steroids' find_button('Save').trigger 'click' @@ -206,6 +206,13 @@ end end + describe 'dom_ready events', js: true do + it 'trigger properly' do + visit dashboard_path + expect(evaluate_script('domReadyTriggered')).to match_array %w[plainjs/colon plainjs/dot jquery/colon jquery/dot] + end + end + context 'with invalid model name' do it "redirects to dashboard and inform the user the model wasn't found" do visit '/admin/whatever' diff --git a/src/rails_admin/nested-form-hooks.js b/src/rails_admin/nested-form-hooks.js index 9f627c9f37..64791a3ae4 100644 --- a/src/rails_admin/nested-form-hooks.js +++ b/src/rails_admin/nested-form-hooks.js @@ -37,8 +37,9 @@ import * as bootstrap from "bootstrap"; toggler = controls.find(".toggler"); nav.append(new_tab); - const event = new CustomEvent("rails_admin.dom_ready", { detail: field }); - document.dispatchEvent(event); + document.dispatchEvent( + new CustomEvent("rails_admin:dom_ready", { detail: field }) + ); new_tab.children("a").each(function (index, element) { bootstrap.Tab.getOrCreateInstance(element).show(); diff --git a/src/rails_admin/remote-form.js b/src/rails_admin/remote-form.js index 15b3fe45cd..d462763381 100644 --- a/src/rails_admin/remote-form.js +++ b/src/rails_admin/remote-form.js @@ -101,8 +101,9 @@ import * as bootstrap from "bootstrap"; }) .html(saveButtonText); - const event = new CustomEvent("rails_admin.dom_ready", { detail: form }); - document.dispatchEvent(event); + document.dispatchEvent( + new CustomEvent("rails_admin:dom_ready", { detail: form }) + ); form.bind("ajax:complete", function (event) { var data = event.detail[0]; diff --git a/src/rails_admin/sidescroll.js b/src/rails_admin/sidescroll.js index d8a3f6a116..7a478cab42 100644 --- a/src/rails_admin/sidescroll.js +++ b/src/rails_admin/sidescroll.js @@ -1,7 +1,7 @@ "use strict"; { - document.addEventListener("rails_admin.dom_ready", () => { + document.addEventListener("rails_admin:dom_ready", () => { const scroller = document.getElementById("sidescroll"); if (!scroller) { return; diff --git a/src/rails_admin/ui.js b/src/rails_admin/ui.js index 3fcb4030cd..eb7bdb0fe7 100644 --- a/src/rails_admin/ui.js +++ b/src/rails_admin/ui.js @@ -82,13 +82,12 @@ import I18n from "./i18n"; function triggerDomReady() { I18n.init($("html").attr("lang"), $("#admin-js").data("i18nOptions")); - const event = new CustomEvent("rails_admin.dom_ready"); - document.dispatchEvent(event); + document.dispatchEvent(new CustomEvent("rails_admin:dom_ready")); } $(document).ready(triggerDomReady); document.addEventListener("turbo:render", triggerDomReady); - document.addEventListener("rails_admin.dom_ready", function () { + document.addEventListener("rails_admin:dom_ready", function (event) { $(".nav.nav-pills li.active").removeClass("active"); $( '.nav.nav-pills li[data-model="' + $(".page-header").data("model") + '"]' @@ -129,6 +128,15 @@ import I18n from "./i18n"; $("a[data-method]").on("click", function (event) { window.Turbo.session.drive = false; }); + + // Trigger with the old event name for compatibility with existing user codes + document.dispatchEvent( + new CustomEvent("rails_admin.dom_ready", { detail: event.detail }) + ); + $(document).trigger( + "rails_admin.dom_ready", + event.detail ? [event.detail] : null + ); }); $(document).on("click", ".bulk-link", function (event) { diff --git a/src/rails_admin/widgets.js b/src/rails_admin/widgets.js index aabd4833d4..5e7b78b20d 100644 --- a/src/rails_admin/widgets.js +++ b/src/rails_admin/widgets.js @@ -5,7 +5,7 @@ import flatpickr from "flatpickr"; import I18n from "./i18n"; (function ($) { - document.addEventListener("rails_admin.dom_ready", function (event) { + document.addEventListener("rails_admin:dom_ready", function (event) { var $editors, array, config_options,