diff --git a/app/javascript/retrospring/common.ts b/app/javascript/retrospring/common.ts index 712edf3e..811e9a8a 100644 --- a/app/javascript/retrospring/common.ts +++ b/app/javascript/retrospring/common.ts @@ -1,10 +1,12 @@ import '@hotwired/turbo-rails'; -import initialize from './initializers'; +import initializeBootstrap from './initializers/bootstrap'; +import initializeStimulus from './initializers/stimulus'; export default function start(): void { try { - initialize(); + initializeBootstrap(); + initializeStimulus(); } catch (e) { // initialization errors } -} \ No newline at end of file +} diff --git a/app/javascript/retrospring/initializers/index.ts b/app/javascript/retrospring/initializers/index.ts deleted file mode 100644 index 3e7768c0..00000000 --- a/app/javascript/retrospring/initializers/index.ts +++ /dev/null @@ -1,20 +0,0 @@ -/** - * Using some JavaScript trickery with require.context - * and default exports, we're basically rebuilding the - * Rails concept of "initializers" in JavaScript. - * - * Every file in this folder exports a default function - * which this index script is loading and executing, so - * we don't have to specify several single import - * statements and can dynamically extend this with as - * many initializers as we see fit. - */ -export default function initialize(): void { - const files = require.context('.', false, /\.ts$/); - - files.keys().forEach((key) => { - if (key === './index.ts') return; - if (key.startsWith('./_')) return; - files(key).default(); - }); -} \ No newline at end of file diff --git a/app/javascript/retrospring/initializers/stimulus.ts b/app/javascript/retrospring/initializers/stimulus.ts new file mode 100644 index 00000000..fc65f20e --- /dev/null +++ b/app/javascript/retrospring/initializers/stimulus.ts @@ -0,0 +1,18 @@ +import { Application } from "@hotwired/stimulus"; +import AnnouncementController from "retrospring/controllers/announcement_controller"; +import AutofocusController from "retrospring/controllers/autofocus_controller"; +import CharacterCountController from "retrospring/controllers/character_count_controller"; + +/** + * This module sets up Stimulus and our controllers + * + * TODO: Temporary solution until I implement stimulus-rails and move + * controllers to app/javascript/controllers where an automated + * index can be generated + */ +export default function (): void { + window['Stimulus'] = Application.start(); + window['Stimulus'].register('announcement', AnnouncementController); + window['Stimulus'].register('autofocus', AutofocusController); + window['Stimulus'].register('character-count', CharacterCountController); +}