-
Notifications
You must be signed in to change notification settings - Fork 827
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 #120 from primer/svg_node_api
Refactoring nodejs api and making more options available
- Loading branch information
Showing
7 changed files
with
281 additions
and
54 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
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 |
---|---|---|
@@ -1,4 +1,62 @@ | ||
module.exports = { | ||
keywords: require('./lib/keywords'), | ||
svg: require('./build/svg.json') | ||
} | ||
var data = require('./build/data.json') | ||
|
||
Object.keys(data).forEach(function(key) { | ||
|
||
// Returns a string representation of html attributes | ||
var htmlAttributes = function(icon, options) { | ||
var attributes = [] | ||
var attrObj = Object.assign({}, data[key].options, options) | ||
|
||
// If the user passed in options | ||
if (options) { | ||
|
||
// If any of the width or height is passed in | ||
if(options["width"] || options["height"]) { | ||
attrObj["width"] = options["width"] ? options["width"] : (parseInt(options["height"]) * data[key].options["width"] / data[key].options["height"]) | ||
attrObj["height"] = options["height"] ? options["height"] : (parseInt(options["width"]) * data[key].options["height"] / data[key].options["width"]) | ||
} | ||
|
||
// If the user passed in class | ||
if (options["class"]) { | ||
attrObj["class"] = "octicon octicon-" + key + " " + options["class"] | ||
attrObj["class"].trim() | ||
} | ||
|
||
// If the user passed in aria-label | ||
if (options["aria-label"]) { | ||
attrObj["aria-label"] = options["aria-label"] | ||
attrObj["role"] = "img" | ||
|
||
// Un-hide the icon | ||
delete attrObj["aria-hidden"] | ||
} | ||
} | ||
|
||
Object.keys(attrObj).forEach(function(option) { | ||
attributes.push(option + "=\"" + attrObj[option] + "\"") | ||
}) | ||
|
||
return attributes.join(" ").trim() | ||
} | ||
|
||
// Set the symbol for easy access | ||
data[key].symbol = key | ||
|
||
// Set all the default options | ||
data[key].options = { | ||
"version": "1.1", | ||
"width": data[key].width, | ||
"height": data[key].height, | ||
"viewBox": "0 0 " + data[key].width + " " + data[key].height, | ||
"class": "octicon octicon-" + key, | ||
"aria-hidden": "true" | ||
} | ||
|
||
// Function to return an SVG object | ||
data[key].toSVG = function(options) { | ||
return "<svg " + htmlAttributes(data[key], options) + ">" + data[key].path + "</svg>" | ||
} | ||
}) | ||
|
||
// Import data into exports | ||
module.exports = data |
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
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
Oops, something went wrong.