Created
April 9, 2015 12:24
-
-
Save GianlucaGuarini/b6a328775996bfbcf4a5 to your computer and use it in GitHub Desktop.
Some useful helpers
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
export default { | |
/** | |
* Similar it handles any kind of ajax request | |
* @param { string } url url to call | |
* @param { string } type ajax request type | |
* @return { promise } | |
*/ | |
ajax(url, type = 'get') { | |
return new Promise((resolve, reject) => { | |
var request = new XMLHttpRequest() | |
request.open(type, url, true) | |
request.onload = function() { | |
if (request.status >= 200 && request.status < 400) { | |
resolve(request.responseText) | |
} else { | |
reject(request) | |
} | |
} | |
request.onerror = function() { | |
reject() | |
} | |
request.send() | |
}) | |
}, | |
/** | |
* Parses SVG fragment and converts it into a fragment | |
* method borrowed from snapsvg | |
* @param { string } svg | |
**/ | |
parseSVG(svg) { | |
var f = document.createDocumentFragment(), | |
full = true, | |
div = document.createElement('div') | |
svg = String(svg) | |
if (!svg.match(/^\s*<\s*svg(?:\s|>)/)) { | |
svg = '<svg>' + svg + '</svg>' | |
full = false | |
} | |
div.innerHTML = svg | |
svg = div.getElementsByTagName('svg')[0] | |
if (svg) { | |
if (full) { | |
f = svg | |
} else { | |
while (svg.firstChild) { | |
f.appendChild(svg.firstChild) | |
} | |
} | |
} | |
return f | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment