UNPKG

ccs-frontend

Version:
154 lines (147 loc) 6.08 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.CCSFrontend = {})); })(this, (function (exports) { 'use strict'; /* * This variable is automatically overwritten during builds and releases. * It doesn't need to be updated manually. */ /** * CCS Frontend release version * * {@link https://github.com/Crown-Commercial-Service/ccs-frontend-project/releases} */ const version = '1.4.1'; class Header { static moduleName = 'ccs-header'; $menuButton; $menu; constructor($header) { this.$menuButton = $header.find('.ccs-js-header-toggle'); this.$menu = $header.find(`#${this.$menuButton.attr('aria-controls')}`); } init() { this.syncState(this.$menu.hasClass('ccs-header__navigation-lists--open')); this.setEventListeners(); this.$menuButton.removeAttr('hidden'); } syncState(isVisible) { this.$menuButton.toggleClass('ccs-header__menu-button--open', isVisible); this.$menuButton.attr('aria-expanded', String(isVisible)); } setEventListeners() { this.$menuButton.on('click', () => { this.$menu.toggleClass('ccs-header__navigation-lists--open'); this.syncState(this.$menu.hasClass('ccs-header__navigation-lists--open')); }); } } class PasswordStrengthTest { $passwordStrengthTest; constructor($passwordStrengthTest) { this.$passwordStrengthTest = $passwordStrengthTest; } init() { this.$passwordStrengthTest.addClass('ccs-password-strength-tests__wrong'); } testPasswordInput(passwordInputText) { const testPassed = this.test.test(passwordInputText); this.$passwordStrengthTest.toggleClass('ccs-password-strength-tests__wrong', !testPassed); this.$passwordStrengthTest.toggleClass('ccs-password-strength-tests__correct', testPassed); } } class PasswordStrengthTestLength extends PasswordStrengthTest { test; constructor($passwordStrengthTest, value) { super($passwordStrengthTest); this.test = new RegExp(`^.{${value},}`); } } class PasswordStrengthTestSymbol extends PasswordStrengthTest { test; constructor($passwordStrengthTest, value) { super($passwordStrengthTest); this.test = new RegExp(`^(?=.*?[${value}])`); } } class PasswordStrengthTestNumber extends PasswordStrengthTest { test; constructor($passwordStrengthTest) { super($passwordStrengthTest); this.test = new RegExp('^(?=.*[0-9])'); } } class PasswordStrengthTestUppercase extends PasswordStrengthTest { test; constructor($passwordStrengthTest) { super($passwordStrengthTest); this.test = new RegExp('^(?=.*[A-Z])'); } } class PasswordStrengthTestLowercase extends PasswordStrengthTest { test; constructor($passwordStrengthTest) { super($passwordStrengthTest); this.test = new RegExp('^(?=.*[a-z])'); } } const passwordStrengthTestFactory = ($passwordStrengthTest) => { const testType = $passwordStrengthTest.data('testType'); const value = $passwordStrengthTest.data('testValue'); switch (testType) { case 'length': return new PasswordStrengthTestLength($passwordStrengthTest, value); case 'symbol': return new PasswordStrengthTestSymbol($passwordStrengthTest, value); case 'number': return new PasswordStrengthTestNumber($passwordStrengthTest); case 'uppercase': return new PasswordStrengthTestUppercase($passwordStrengthTest); case 'lowercase': return new PasswordStrengthTestLowercase($passwordStrengthTest); } }; class PasswordStrength { static moduleName = 'ccs-password-strength'; $passwordStrength; $passwordInput; passwordStrengthTests; constructor($passwordStrength) { this.$passwordStrength = $passwordStrength; this.$passwordInput = $(`#${$passwordStrength.data('target')}`); this.passwordStrengthTests = this.$passwordStrength.find('.ccs-password-strength-test').get().map((passwordStrengthTest) => passwordStrengthTestFactory($(passwordStrengthTest))); } init() { if (this.$passwordInput.length) { this.$passwordStrength.addClass('ccs-password-strength-tests'); this.passwordStrengthTests.forEach((passwordStrengthTest) => passwordStrengthTest.init()); this.$passwordInput.on('keyup', () => { this.testPasswordInput(); }); } } testPasswordInput() { const passwordInputText = String(this.$passwordInput.val()); this.passwordStrengthTests.forEach((passwordStrengthTest) => passwordStrengthTest.testPasswordInput(passwordInputText)); } } const createAll = (Component) => { $(`[data-module="${Component.moduleName}"]`).each((_index, componentElement) => { new Component($(componentElement)).init(); }); }; const initAll = () => { const components = [ Header, PasswordStrength, ]; components.forEach((Component) => { createAll(Component); }); }; exports.Header = Header; exports.PasswordStrength = PasswordStrength; exports.createAll = createAll; exports.initAll = initAll; exports.version = version; })); //# sourceMappingURL=all.bundle.js.map