-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from viivue/4.0.2
release 4.0.2
- Loading branch information
Showing
3 changed files
with
57 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {isJSON} from "@/utils"; | ||
|
||
/** | ||
* 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 = ['autoShow']; // convert these props to float | ||
const wrapper = context.selectTag; | ||
|
||
// options from attribute | ||
let dataAttribute = wrapper.getAttribute(context.atts.init); | ||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters