Skip to content

Commit

Permalink
Add getOptionsFromAttribute() (#46)
Browse files Browse the repository at this point in the history
* feat: add getOptionsFromAttrs and remove old getOptions

* feat: change this.options position

* fix: move this.options before init

* fix: replace this.originalOptions for options
  • Loading branch information
vandangnhathung authored Aug 4, 2023
1 parent 07feeee commit 8f06351
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 61 deletions.
21 changes: 13 additions & 8 deletions src/_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand All @@ -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();

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
53 changes: 0 additions & 53 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8f06351

Please sign in to comment.