I'm using a Sass framework based on modular CSS principles, which outputs all my variables and configuration for each Sass "module" into JSON format in the DOM, which I have successfully retrieved in my JS. My JSON is in the following format:
{
"billboard":{
"selector-type":"flex",
"extend-settings":true,
"name":"billboard",
"full-screen":false,
"overlay":true,
"bg-color":"#232627",
"bg-image":"../../images/billboard-1.jpg",
"overlay-color":"#9B58B5",
"overlay-opacity":0.6,
"color":"#ffffff",
"text-align":"center",
"min-height":"500px",
"wrapper-width":"940px"
},
"navigation":{
"selector-type":"flex",
"extend-settings":true,
"name":"navigation",
"item-color":"#ffffff",
"item-bg":"transparent",
"item-border":[
"1px",
"solid",
"transparent"
],
"item-hover-color":null,
"item-hover-bg":"transparent",
"item-hover-border":[
"1px",
"solid",
"#ffffff"
]
"header-dark":{
"default":false,
"item-color":"#6f777b",
"item-border":[
"1px",
"solid",
"#ffffff"
],
"item-bg-color":"transparent",
"item-hover-color":null,
"item-hover-border":[
"1px",
"solid",
"#ffffff"
],
"item-hover-bg-color":"#9B58B5"
},
"style":false,
"no-icons":true,
"link-color":"#ffffff"
}
}
Each first level set will always be a Sass "module" (e.g., "header"), which will then contain the module's configuration, which can be of any depth.
I'd like to write a function to retrieve the value of the key I specify. Since not all keys are unique, and the only guaranteed uniqueness is in the module name, my requirements are to be able to select the value by specifying the parents.
Here's what I have so far, where "stylesConfig" is my array of JSON data:
function module(module, option) {
var mValue;
$.each(stylesConfig, function(id, param) {
if(id == module) {
$.each(param, function(key, value) {
if (key == option) {
mValue = value;
return false;
}
});
}
});
return mValue;
}
This works perfectly using:
module("navigation", "selector-type");
Which would return "flex" using the above JSON example. The problem is that it doesn't work recursively, so doesn't work on nested options. I would like to be able to expand upon what I already have to be able to do something like:
module("navigation", ["header-dark", "item-color"]);
To return "#6f777b".
Coming from a design background, I frankly have no idea what I'm doing. My programming capabilities are limited to what I have learned using Sass.