123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- import type { Definition } from '@hotwired/stimulus';
- import { Application, Controller } from '@hotwired/stimulus';
- type ControllerObjectDefinition = Record<string, () => void> & {
- STATIC?: {
- classes?: string[];
- targets?: string[];
- values: typeof Controller.values;
- };
- };
- class WagtailApplication extends Application {
-
- static Controller = Controller;
-
- static createController = (
- controllerDefinition: ControllerObjectDefinition = {},
- ): typeof Controller => {
- class NewController<X extends Element> extends Controller<X> {}
- const { STATIC = {}, ...controllerDefinitionWithoutStatic } =
- controllerDefinition;
-
- Object.entries(STATIC).forEach(([key, value]) => {
- NewController[key] = value;
- });
-
- Object.assign(NewController.prototype, controllerDefinitionWithoutStatic);
- return NewController;
- };
- }
- export const initStimulus = ({
- debug = process.env.NODE_ENV === 'development',
- definitions = [],
- element = document.documentElement,
- }: {
- debug?: boolean;
- definitions?: Definition[];
- element?: HTMLElement;
- } = {}): Application => {
- const application = WagtailApplication.start(element);
- application.debug = debug;
- application.load(definitions);
- return application;
- };
|