<% app_name = Rails.application.class.to_s.split('::').first %> class SeedTray constructor: () -> loadEvent = undefined if window.Turbolinks loadEvent = if Turbolinks.EVENTS then 'page:change' else 'turbolinks:load'; else loadEvent = 'DOMContentLoaded' document.addEventListener loadEvent, => @render() @root = <%= app_name %> # Dynamically delegate render to controller#action specific render methods render: => # Run the code that should run on every page. @delegate_sitewide_render() # Run the page specific render code @delegate_render() delegate_render: => suppress_console = <%= SeedTray.configuration.suppress_console %> controller = document.body.getAttribute("data-controller") action = document.body.getAttribute("data-action") controllerLc = controller.toLowerCase(); actionLc = action.toLowerCase(); if @root[controller] && @render_defined(@root[controller]) @root[controller].render() @trigger_event "seedtray:#{controllerLc}:render" else unless suppress_console console.info "Skipped #{@root.name}.#{controller}.render()." if @root[controller] && @root[controller][action] && @render_defined(@root[controller][action]) @root[controller][action].render() @trigger_event "seedtray:#{controllerLc}:#{actionLc}:render" unless suppress_console console.info "Executed #{@root.name}.#{controller}.#{action}.render()." else unless suppress_console console.info "Skipped #{@root.name}.#{controller}.#{action}.render()." delegate_sitewide_render: => @site_wide_render() @trigger_event 'seedtray:render' render_defined: (object) -> object.render != undefined trigger_event: (evt) -> document.dispatchEvent new Event(evt); site_wide_render: -> <%= app_name %>.delegator = new SeedTray()