|
| 1 | +import { Application } from '@hotwired/stimulus'; |
| 2 | +import { isApplicationDebug, eagerControllers, lazyControllers } from './controllers.js'; |
| 3 | + |
| 4 | +const controllerAttribute = 'data-controller'; |
| 5 | +const loadControllers = (application, eagerControllers, lazyControllers) => { |
| 6 | + for (const name in eagerControllers) { |
| 7 | + registerController(name, eagerControllers[name], application); |
| 8 | + } |
| 9 | + const lazyControllerHandler = new StimulusLazyControllerHandler(application, lazyControllers); |
| 10 | + lazyControllerHandler.start(); |
| 11 | +}; |
| 12 | +const startStimulusApp = () => { |
| 13 | + const application = Application.start(); |
| 14 | + application.debug = isApplicationDebug; |
| 15 | + loadControllers(application, eagerControllers, lazyControllers); |
| 16 | + return application; |
| 17 | +}; |
| 18 | +class StimulusLazyControllerHandler { |
| 19 | + constructor(application, lazyControllers) { |
| 20 | + this.application = application; |
| 21 | + this.lazyControllers = lazyControllers; |
| 22 | + } |
| 23 | + start() { |
| 24 | + this.lazyLoadExistingControllers(document.documentElement); |
| 25 | + this.lazyLoadNewControllers(document.documentElement); |
| 26 | + } |
| 27 | + lazyLoadExistingControllers(element) { |
| 28 | + this.queryControllerNamesWithin(element).forEach((controllerName) => this.loadLazyController(controllerName)); |
| 29 | + } |
| 30 | + async loadLazyController(name) { |
| 31 | + if (canRegisterController(name, this.application)) { |
| 32 | + if (this.lazyControllers[name] === undefined) { |
| 33 | + return; |
| 34 | + } |
| 35 | + const controllerModule = await this.lazyControllers[name](); |
| 36 | + registerController(name, controllerModule.default, this.application); |
| 37 | + } |
| 38 | + } |
| 39 | + lazyLoadNewControllers(element) { |
| 40 | + new MutationObserver((mutationsList) => { |
| 41 | + for (const { attributeName, target, type } of mutationsList) { |
| 42 | + switch (type) { |
| 43 | + case 'attributes': { |
| 44 | + if (attributeName === controllerAttribute && |
| 45 | + target.getAttribute(controllerAttribute)) { |
| 46 | + extractControllerNamesFrom(target).forEach((controllerName) => this.loadLazyController(controllerName)); |
| 47 | + } |
| 48 | + break; |
| 49 | + } |
| 50 | + case 'childList': { |
| 51 | + this.lazyLoadExistingControllers(target); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + }).observe(element, { |
| 56 | + attributeFilter: [controllerAttribute], |
| 57 | + subtree: true, |
| 58 | + childList: true, |
| 59 | + }); |
| 60 | + } |
| 61 | + queryControllerNamesWithin(element) { |
| 62 | + return Array.from(element.querySelectorAll(`[${controllerAttribute}]`)) |
| 63 | + .map(extractControllerNamesFrom) |
| 64 | + .flat(); |
| 65 | + } |
| 66 | +} |
| 67 | +function registerController(name, controller, application) { |
| 68 | + if (canRegisterController(name, application)) { |
| 69 | + application.register(name, controller); |
| 70 | + } |
| 71 | +} |
| 72 | +function extractControllerNamesFrom(element) { |
| 73 | + const controllerNameValue = element.getAttribute(controllerAttribute); |
| 74 | + if (!controllerNameValue) { |
| 75 | + return []; |
| 76 | + } |
| 77 | + return controllerNameValue.split(/\s+/).filter((content) => content.length); |
| 78 | +} |
| 79 | +function canRegisterController(name, application) { |
| 80 | + return !application.router.modulesByIdentifier.has(name); |
| 81 | +} |
| 82 | + |
| 83 | +export { loadControllers, startStimulusApp }; |
0 commit comments