From 8161c57767cb16e0ca1004368d1f36b492f03cdb Mon Sep 17 00:00:00 2001 From: Vladimir Kharlampidi Date: Wed, 7 Dec 2022 15:05:47 +0300 Subject: [PATCH] feat(element): support for comples parameters via attrs in a form of `autoplay-delay` --- src/element/get-params.js | 50 +++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/element/get-params.js b/src/element/get-params.js index 302471cbd..445e9b704 100644 --- a/src/element/get-params.js +++ b/src/element/get-params.js @@ -12,6 +12,31 @@ const formatValue = (val) => { return val; }; +const modulesParamsList = [ + 'a11y', + 'autoplay', + 'controller', + 'cards-effect', + 'coverflow-effect', + 'creative-effect', + 'cube-effect', + 'fade-effect', + 'flip-effect', + 'free-mode', + 'grid', + 'hash-navigation', + 'history', + 'keyboard', + 'mousewheel', + 'navigation', + 'pagination', + 'parallax', + 'scrollbar', + 'thumbs', + 'virtual', + 'zoom', +]; + function getParams(element) { const params = {}; const passedParams = {}; @@ -29,10 +54,27 @@ function getParams(element) { // Attributes [...element.attributes].forEach((attr) => { - const name = attrToProp(attr.name); - if (!allowedParams.includes(name)) return; - const value = formatValue(attr.value); - passedParams[name] = value; + const moduleParam = modulesParamsList.filter( + (mParam) => attr.name.indexOf(`${mParam}-`) === 0, + )[0]; + if (moduleParam) { + const parentObjName = attrToProp(moduleParam); + const subObjName = attrToProp(attr.name.split(`${moduleParam}-`)[1]); + if (!passedParams[parentObjName]) passedParams[parentObjName] = {}; + if (passedParams[parentObjName] === true) { + passedParams[parentObjName] = { enabled: true }; + } + passedParams[parentObjName][subObjName] = formatValue(attr.value); + } else { + const name = attrToProp(attr.name); + if (!allowedParams.includes(name)) return; + const value = formatValue(attr.value); + if (passedParams[name] && modulesParamsList.includes(attr.name)) { + passedParams[name].enabled = value; + } else { + passedParams[name] = value; + } + } }); extend(params, passedParams);