1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/mixins/inject_in_head_mixin.js
2015-06-01 18:59:47 +02:00

58 lines
1.3 KiB
JavaScript

let mapAttr = {
link: 'href',
source: 'src'
}
let mapExt = {
js: 'source',
css: 'link'
}
let InjectInHeadMixin = {
/**
* Provide functions to inject `<script>` and `<link>` in `<head>`.
* Useful when you have to load a huge external library and
* you don't want to embed everything inside the build file.
*/
isPresent(tag, src) {
let attr = mapAttr[tag];
let query = `head > ${tag}[${attr}="${src}"]`;
return document.querySelector(query);
},
injectTag(tag, src){
console.log(this.foobar);
if (InjectInHeadMixin.isPresent(tag, src))
return;
let attr = mapAttr[tag];
let element = document.createElement(tag);
document.head.appendChild(element);
element[attr] = src;
},
injectStylesheet(src) {
this.injectTag('link', src);
},
injectScript(src) {
this.injectTag('source', src);
},
inject(src) {
//debugger;
let ext = src.split('.').pop();
try {
let tag = mapAttr(src);
} catch (e) {
throw new Error(`Cannot inject ${src} in the DOM, cannot guess the tag name from extension ${ext}. Valid extensions are "js" and "css".`);
}
InjectInHeadMixin.injectTag(tag, src);
}
};
export default InjectInHeadMixin;