From ee8fb109a0a05da12d60edaa2d520fff4cbf7e76 Mon Sep 17 00:00:00 2001 From: Paul Robert Lloyd Date: Sun, 14 Aug 2022 13:04:31 +0100 Subject: [PATCH] Refactor JS export --- x-govuk/all.js | 35 +++++++++++++++++++++- x-govuk/components/all.js | 61 --------------------------------------- 2 files changed, 34 insertions(+), 62 deletions(-) delete mode 100644 x-govuk/components/all.js diff --git a/x-govuk/all.js b/x-govuk/all.js index 72d914a..01b87fe 100644 --- a/x-govuk/all.js +++ b/x-govuk/all.js @@ -1 +1,34 @@ -module.exports = require('./components/all.js') +import Autocomplete from './components/autocomplete/autocomplete.js' +import Edge from './components/edge/edge.js' +import WarnOnUnsavedChanges from './components/warn-on-unsaved-changes/warn-on-unsaved-changes.js' + +function initAll (options) { + // Set the options to an empty object by default if no options are passed. + options = typeof options !== 'undefined' ? options : {} + + // Allow user to initialise components in only certain sections of the page + // Defaults to the entire document if nothing is set. + const scope = typeof options.scope !== 'undefined' ? options.scope : document + + const $autocompletes = scope.querySelectorAll('[data-module="autocomplete"]') + $autocompletes.forEach(function ($autocomplete) { + new Autocomplete($autocomplete).init() + }) + + const $edges = scope.querySelectorAll('[data-module="edge"]') + $edges.forEach(function ($edge) { + new Edge($edge).init() + }) + + const $forms = scope.querySelectorAll('[data-module="warn-on-unsaved-changes"]') + $forms.forEach(function ($form) { + new WarnOnUnsavedChanges($form).init() + }) +} + +export { + initAll, + Autocomplete, + Edge, + WarnOnUnsavedChanges +} diff --git a/x-govuk/components/all.js b/x-govuk/components/all.js deleted file mode 100644 index e21854d..0000000 --- a/x-govuk/components/all.js +++ /dev/null @@ -1,61 +0,0 @@ -import Autocomplete from './autocomplete/autocomplete.js' -import Edge from './edge/edge.js' -import WarnOnUnsavedChanges from './warn-on-unsaved-changes/warn-on-unsaved-changes.js' - -/** - * Get module name. - * - * @example - * _getModuleName(selectable-table) // SelectableTable - * - * @access private - * @param {string} string - Original value - * @returns {string} data Updated data - */ -const _getModuleName = (string) => { - // Convert string to camel case - string = string.replace(/-([a-z])/g, (g) => g.charAt(1).toUpperCase()) - - // Capitalise first letter - string = string.charAt(0).toUpperCase() + string.slice(1) - - return string -} - -/** - * Find and initiate component modules. - * - * @example - * [data-module="foo-bar"] initiates GOVUKPrototypeComponents.FooBar() - */ -export default (function () { - const GOVUKPrototypeComponents = window.GOVUKPrototypeComponents || {} - - // Initiate all component modules - GOVUKPrototypeComponents.initAll = function (container) { - container = container || document.documentElement - const modules = container.querySelectorAll('[data-module]') - - modules.forEach((module, i) => { - const element = modules[i] - const name = _getModuleName(element.dataset.module) - const started = element.dataset.moduleStarted - - if (typeof GOVUKPrototypeComponents[name] === 'function' && !started) { - module = new GOVUKPrototypeComponents[name](element) - module.init() - element.dataset.moduleStarted = true - } - }) - } - - // Add component modules to GOVUKPrototypeComponents object - GOVUKPrototypeComponents.Autocomplete = Autocomplete - GOVUKPrototypeComponents.Edge = Edge - GOVUKPrototypeComponents.WarnOnUnsavedChanges = WarnOnUnsavedChanges - - // Add GOVUKPrototypeComponents to window global - window.GOVUKPrototypeComponents = GOVUKPrototypeComponents - - return GOVUKPrototypeComponents -})()