diff --git a/src/_index.js b/src/_index.js index f7e82ac..7a37ec4 100644 --- a/src/_index.js +++ b/src/_index.js @@ -6,14 +6,14 @@ import { getToggleState, getIndexById, getElements, - removeActiveClass, addActiveClass, getIdByIndex, log, getOptions + removeActiveClass, addActiveClass, getIdByIndex, log } from "./helpers"; import {debounce} from "./utils"; import {initSetup, onLoad, onResize} from "./methods"; import {isLive, validBreakpoints} from "./responsive"; import {scrollIntoView} from "./animation"; import {CLASSES, ATTRS, DEFAULTS} from './configs'; -import {EventsManager} from "@phucbm/os-util"; +import {EventsManager, getOptionsFromAttribute} from "@phucbm/os-util"; export class EasyTabAccordion{ constructor(options){ @@ -24,6 +24,17 @@ export class EasyTabAccordion{ // save options this.originalOptions = options; + + // get options init by data attribute (JSON format) + this.options + = getOptionsFromAttribute({ + target: this.wrapper, + defaultOptions: {...DEFAULTS, ...options}, + attributeName: ATTRS.container, + numericValues: ['duration', 'activeSection'], + dev: DEFAULTS.dev + }); + // init this.init(); @@ -42,9 +53,6 @@ export class EasyTabAccordion{ }; init(){ - // setup - this.options = {...DEFAULTS, ...this.originalOptions}; - if(!this.options.el){ log(this, 'warn', 'ETA Error, target not found!'); return; @@ -75,9 +83,6 @@ export class EasyTabAccordion{ const animationValue = this.wrapper.getAttribute(ATTRS.animation); this.options.animation = animationValue !== null ? animationValue : this.options.animation; - // get options init by data attribute (JSON format) - this.options = getOptions(this); - // assign id to wrapper this.wrapper.setAttribute(ATTRS.container, this.id); diff --git a/src/helpers.js b/src/helpers.js index dae4654..948bd19 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -191,59 +191,6 @@ export function log(context, status, ...message){ } } - -/** - * Get JSON options - * ID priority: data-attribute > selector#id > unique id - * @version 0.0.1 - * @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; - - // options from attribute - let dataAttribute = wrapper.getAttribute(ATTRS.container); - let options = {}; - - // data attribute doesn't exist or not JSON format -> string - const attributeIsNotJSON = !dataAttribute || !isJSON(dataAttribute); - - // data attribute is not json format or string - if(attributeIsNotJSON){ - options = {...defaultOptions}; - - // data attribute exist => string - if(dataAttribute) options.id = dataAttribute; - else options.id = ''; - }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 - const id = options.id || wrapper.id || defaultOptions.id; - context.id = id; - options.id = id; - - options = {...defaultOptions, ...options}; - - return options; -} - - /** * Is JSON string * https://stackoverflow.com/a/32278428/6453822