Sha256: 6453feedd59be9f1d9952058dc07ca1b38fec7a681144b53110e2c2a00a9e9b1

Contents?: true

Size: 1.62 KB

Versions: 23

Compression:

Stored size: 1.62 KB

Contents

import { Application } from "@hotwired/stimulus"

const application = Application.start()
const { controllerAttribute } = application.schema
const registeredControllers = {}

function autoloadControllersWithin(element) {
  queryControllerNamesWithin(element).forEach(loadController)
}

function queryControllerNamesWithin(element) {
  return Array.from(element.querySelectorAll(`[${controllerAttribute}]`)).map(extractControllerNamesFrom).flat()
}

function extractControllerNamesFrom(element) {
  return element.getAttribute(controllerAttribute).split(/\s+/).filter(content => content.length)
}

function loadController(name) {
  import(controllerFilename(name))
    .then(module => registerController(name, module))
    .catch(error => console.log(`Failed to autoload controller: ${name}`, error))
}

function controllerFilename(name) {
  return `controllers/${name.replace(/--/g, "/").replace(/-/g, "_")}_controller`
}

function registerController(name, module) {
  if (name in registeredControllers) return

  application.register(name, module.default)
  registeredControllers[name] = true
}


new MutationObserver((mutationsList) => {
  for (const { attributeName, target, type } of mutationsList) {
    switch (type) {
      case "attributes": {
        if (attributeName == controllerAttribute && target.getAttribute(controllerAttribute)) {
          extractControllerNamesFrom(target).forEach(loadController)
        }
      }
      case "childList": {
        autoloadControllersWithin(target)
      }
    }
  }
}).observe(document, { attributeFilter: [controllerAttribute], subtree: true, childList: true })

autoloadControllersWithin(document)

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
stimulus-rails-0.6.2 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.6.1 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.6.0 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.5.4 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.5.3 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.5.2 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.5.1 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.5.0 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.4.2 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.4.1 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.4.0 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.11 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.10 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.9 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.8 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.7 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.6 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.5 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.4 app/assets/javascripts/stimulus-autoloader.js
stimulus-rails-0.3.3 app/assets/javascripts/stimulus-autoloader.js