From 9600b0bd444207f93cfbc1b9086b5dbce8a726ad Mon Sep 17 00:00:00 2001 From: vupham Date: Tue, 3 Jan 2023 18:12:27 +0700 Subject: [PATCH 01/13] :zap: getOptions > refactor code for reuse purpose --- src/helpers.js | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 1dad461..eb027a7 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -195,34 +195,39 @@ export function log(context, status, ...message){ * @returns void */ export function getOptions(context){ + const numeric = ['duration', 'activeSection']; // convert these props to float + const wrapper = context.wrapper; + // options from attribute - let string = context.wrapper.getAttribute(context._attr.container); + let dataAttribute = wrapper.getAttribute(context._attr.container); let options = {}; - if(!isJSON(string)){ - context.id = context.options.id; + // data attribute doesn't exist or not JSON format -> get default ID + if(!dataAttribute || !isJSON(dataAttribute)){ + context.id = dataAttribute || context.options.id; return; } // option priority: attribute > js object > default - options = {...JSON.parse(string)}; + options = JSON.parse(dataAttribute); - // convert boolean string to real boolean for(const [key, value] of Object.entries(options)){ + // convert boolean string to real boolean if(value === "false") options[key] = false; - if(value === "true") options[key] = true; - if(!isNaN(value)) options[key] = parseInt(value); - } + else if(value === "true") options[key] = true; + else options[key] = value; - // get ID - if(options['id'] && !isEmptyString(options['id'])){ - context.id = options['id']; - }else{ - context.id = context.options.id; + // convert string to float + if(numeric.includes(key) && typeof value === 'string' && value.length > 0){ + options[key] = parseFloat(value); + } } - // replace default options context.options = {...context.options, ...options}; + context.id = options.id || context.options.id; + + // remove json + wrapper.removeAttribute(context._attr.container); } From caf4173d8fbdab1e3651f32b293b58d802fe5a11 Mon Sep 17 00:00:00 2001 From: vupham Date: Tue, 3 Jan 2023 18:19:57 +0700 Subject: [PATCH 02/13] :zap: getOptions > set version --- src/helpers.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/helpers.js b/src/helpers.js index eb027a7..d4877f6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -192,6 +192,7 @@ export function log(context, status, ...message){ /** * Get JSON options + * @version 0.0.1 * @returns void */ export function getOptions(context){ From 6fbf1567a933692ba8700af6fe176889eba8f6bb Mon Sep 17 00:00:00 2001 From: vupham Date: Wed, 4 Jan 2023 12:13:22 +0700 Subject: [PATCH 03/13] :zap: getOptions > enhance code structure for multiple ESLibs --- src/_index.js | 2 +- src/helpers.js | 28 +++++++++++++++++++--------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/_index.js b/src/_index.js index 22fef7a..720e3ba 100644 --- a/src/_index.js +++ b/src/_index.js @@ -121,7 +121,7 @@ export class EasyTabAccordion{ this.options.animation = animationValue !== null ? animationValue : this.options.animation; // get options init by data attribute (JSON format) - getOptions(this); + this.options = getOptions(this, this.options); // assign id to wrapper this.wrapper.setAttribute(this._attr.container, this.id); diff --git a/src/helpers.js b/src/helpers.js index d4877f6..819acd3 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -192,10 +192,11 @@ export function log(context, status, ...message){ /** * Get JSON options + * ID priority: data-attribute > selector#id > unique id * @version 0.0.1 - * @returns void + * @returns {object} */ -export function getOptions(context){ +export function getOptions(context, defaultOptions){ const numeric = ['duration', 'activeSection']; // convert these props to float const wrapper = context.wrapper; @@ -205,8 +206,12 @@ export function getOptions(context){ // data attribute doesn't exist or not JSON format -> get default ID if(!dataAttribute || !isJSON(dataAttribute)){ - context.id = dataAttribute || context.options.id; - return; + // reassign id + const id = dataAttribute || wrapper.id || defaultOptions.id; + context.id = id; + defaultOptions.id = id; + + return defaultOptions; } // option priority: attribute > js object > default @@ -224,19 +229,24 @@ export function getOptions(context){ } } - context.options = {...context.options, ...options}; - context.id = options.id || context.options.id; + options = {...defaultOptions, ...options}; + + // reassign id + const id = options.id || wrapper.id || defaultOptions.id; + context.id = id; + options.id = id; // remove json wrapper.removeAttribute(context._attr.container); + + return options; } /** - * Is JSON string - * https://stackoverflow.com/a/32278428/6453822 + * Get ID * @param string - * @returns {any|boolean} + * @returns void */ function isJSON(string){ try{ From fdf4b4de71f965c966515cf0a3197cce6db8af09 Mon Sep 17 00:00:00 2001 From: vupham Date: Wed, 4 Jan 2023 12:26:52 +0700 Subject: [PATCH 04/13] :zap: getOptions > change the order of id priority --- src/helpers.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 819acd3..439f9f2 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -204,7 +204,7 @@ export function getOptions(context, defaultOptions){ let dataAttribute = wrapper.getAttribute(context._attr.container); let options = {}; - // data attribute doesn't exist or not JSON format -> get default ID + // data attribute doesn't exist or not JSON format -> string if(!dataAttribute || !isJSON(dataAttribute)){ // reassign id const id = dataAttribute || wrapper.id || defaultOptions.id; @@ -214,7 +214,6 @@ export function getOptions(context, defaultOptions){ return defaultOptions; } - // option priority: attribute > js object > default options = JSON.parse(dataAttribute); for(const [key, value] of Object.entries(options)){ @@ -229,13 +228,13 @@ export function getOptions(context, defaultOptions){ } } - options = {...defaultOptions, ...options}; - // reassign id const id = options.id || wrapper.id || defaultOptions.id; context.id = id; options.id = id; + options = {...defaultOptions, ...options}; + // remove json wrapper.removeAttribute(context._attr.container); From 108b146827ebf38ff9d739e415799a5d45e39d6a Mon Sep 17 00:00:00 2001 From: vupham Date: Wed, 4 Jan 2023 13:51:08 +0700 Subject: [PATCH 05/13] :zap: getOptions > refactor code --- src/helpers.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 439f9f2..d883e81 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -220,12 +220,9 @@ export function getOptions(context, defaultOptions){ // convert boolean string to real boolean if(value === "false") options[key] = false; else if(value === "true") options[key] = true; - else options[key] = value; - // convert string to float - if(numeric.includes(key) && typeof value === 'string' && value.length > 0){ - options[key] = parseFloat(value); - } + else if(numeric.includes(key) && typeof value === 'string' && value.length > 0) options[key] = parseFloat(value); + else options[key] = value; } // reassign id From 0ee4d2140ea2193986b1b51957f9749ff372c188 Mon Sep 17 00:00:00 2001 From: vupham Date: Thu, 5 Jan 2023 16:14:23 +0700 Subject: [PATCH 06/13] :zap: getOptions > rename description --- src/helpers.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index d883e81..65481a6 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -240,9 +240,10 @@ export function getOptions(context, defaultOptions){ /** - * Get ID + * Is JSON string + * https://stackoverflow.com/a/32278428/6453822 * @param string - * @returns void + * @returns {any|boolean} */ function isJSON(string){ try{ From 58a2afd2cf84954600840249393fef87a7793256 Mon Sep 17 00:00:00 2001 From: vupham Date: Thu, 5 Jan 2023 16:19:38 +0700 Subject: [PATCH 07/13] :zap: getOptions > remove "remove json function" --- src/helpers.js | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 65481a6..3442498 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -232,9 +232,6 @@ export function getOptions(context, defaultOptions){ options = {...defaultOptions, ...options}; - // remove json - wrapper.removeAttribute(context._attr.container); - return options; } From 0edc01397e6f56deaa3e70b34c63cdb7b9998cbf Mon Sep 17 00:00:00 2001 From: vupham Date: Thu, 5 Jan 2023 16:48:25 +0700 Subject: [PATCH 08/13] :zap: getOptions > enhance code structure (remove duplicate code) --- src/helpers.js | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 3442498..daeb202 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -205,24 +205,25 @@ export function getOptions(context, defaultOptions){ let options = {}; // data attribute doesn't exist or not JSON format -> string - if(!dataAttribute || !isJSON(dataAttribute)){ - // reassign id - const id = dataAttribute || wrapper.id || defaultOptions.id; - context.id = id; - defaultOptions.id = id; + const attributeIsNotJSON = !dataAttribute || !isJSON(dataAttribute); - return defaultOptions; - } - - options = JSON.parse(dataAttribute); + // data attribute is not json format or string + if(attributeIsNotJSON){ + options = defaultOptions; - for(const [key, value] of Object.entries(options)){ - // convert boolean string to real boolean - if(value === "false") options[key] = false; - else if(value === "true") options[key] = true; - // convert string to float - else if(numeric.includes(key) && typeof value === 'string' && value.length > 0) options[key] = parseFloat(value); - else options[key] = value; + // data attribute exist => string + if(dataAttribute) options.id = dataAttribute; + }else{ + options = JSON.parse(dataAttribute); + + for(const [key, value] of Object.entries(options)){ + // convert boolean string to real boolean + if(value === "false") options[key] = false; + else if(value === "true") options[key] = true; + // convert string to float + else if(numeric.includes(key) && typeof value === 'string' && value.length > 0) options[key] = parseFloat(value); + else options[key] = value; + } } // reassign id From 8cc245e6c57c1db6050dfad87365bd80ce49a2f2 Mon Sep 17 00:00:00 2001 From: vupham Date: Thu, 5 Jan 2023 16:50:26 +0700 Subject: [PATCH 09/13] :zap: getOptions > refactor get default options parameter --- src/_index.js | 2 +- src/helpers.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/_index.js b/src/_index.js index 720e3ba..340303c 100644 --- a/src/_index.js +++ b/src/_index.js @@ -121,7 +121,7 @@ export class EasyTabAccordion{ this.options.animation = animationValue !== null ? animationValue : this.options.animation; // get options init by data attribute (JSON format) - this.options = getOptions(this, this.options); + this.options = getOptions(this); // assign id to wrapper this.wrapper.setAttribute(this._attr.container, this.id); diff --git a/src/helpers.js b/src/helpers.js index daeb202..5fad200 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -197,6 +197,10 @@ export function log(context, status, ...message){ * @returns {object} */ export function getOptions(context, defaultOptions){ + if(!defaultOptions){ + defaultOptions = context.options || context.config || {}; + } + const numeric = ['duration', 'activeSection']; // convert these props to float const wrapper = context.wrapper; From 284a348da72ae352594fad32c94a6b5b4d1ae137 Mon Sep 17 00:00:00 2001 From: phucbm Date: Fri, 6 Jan 2023 16:59:14 +0700 Subject: [PATCH 10/13] :zap: v2.1.3 --- CHANGELOG.md | 4 ++++ README.md | 17 +++++------------ dev/index.html | 2 +- dev/script.js | 2 +- dist/easy-tab-accordion.min.js | 4 ++-- dist/easy-tab-accordion.module.js | 4 ++-- package.json | 3 +-- 7 files changed, 16 insertions(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 65747a6..b2af3a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +### [2.1.2] - 2023-01-06 + +- Improve performances + ### [2.1.2] - 2022-12-21 - Slide down function > fix a minor bug diff --git a/README.md b/README.md index 9db4758..5887bd0 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ import "@viivue/easy-tab-accordion"; ```html - + ``` ## Initialize @@ -226,21 +226,14 @@ eta.update(); ## Deployment -Start dev server - ```shell +# Start dev server npm run dev -``` -Distribute production files (set new version in `package.json` first) - -```shell +# Distribute production files (set new version in `package.json` first) npm run prod -``` - -Build dev site (for Netlify only) -```shell +# Build dev site (for Netlify only) npm run build ``` @@ -248,4 +241,4 @@ npm run build [MIT License](https://github.com/viivue/easy-tab-accordion/blob/main/LICENSE) -Copyright (c) 2022 ViiVue \ No newline at end of file +Copyright (c) 2023 ViiVue \ No newline at end of file diff --git a/dev/index.html b/dev/index.html index a72e153..3000619 100644 --- a/dev/index.html +++ b/dev/index.html @@ -217,7 +217,7 @@

Accordion initialize with JSON format