-
Notifications
You must be signed in to change notification settings - Fork 0
/
moduler.js
97 lines (84 loc) · 2.65 KB
/
moduler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
(function( window, document ) {
var Moduler = {
config : {
cssPath : 'css/',
jsPath : 'js/',
media : 'screen'
},
init : function( options ) {
this.cssElements = document.querySelectorAll('[data-moduler-css]')
this.jsElements = document.querySelectorAll('[data-moduler-js]')
if ( this.cssElements.length > 0 ) {
this.iterateCSS( this.cssElements, this.config.media, this.config.cssPath );
}
if ( this.jsElements.length > 0 ) {
this.iterateJS( this.jsElements, this.config.jsPath );
}
},
iterateCSS : function( elements, media, path ) {
for (var i = 0; i < elements.length; ++i) {
var name = elements[i].getAttribute('data-moduler-css');
var fullpath = path + name;
this.loadCSS( fullpath, media );
}
},
iterateJS : function( elements, path ) {
for (var i = 0; i < elements.length; ++i) {
var name = elements[i].getAttribute('data-moduler-js');
var fullpath = path + name;
this.loadJS( fullpath );
}
},
loadCSS : function( href, media, callback ) {
"use strict";
var ss = document.createElement( "link" );
var ref = document.querySelector('script');
var sheets = document.styleSheets;
ss.rel = "stylesheet";
ss.href = href;
// temporarily, set media to something non-matching to ensure it'll fetch without blocking render
ss.media = "only x";
// DEPRECATED
if( callback ) {
ss.onload = callback;
}
// inject link
ref.parentNode.insertBefore( ss, ref );
// This function sets the link's media back to `all` so that the stylesheet applies once it loads
// It is designed to poll until document.styleSheets includes the new sheet.
ss.onloadcssdefined = function( cb ){
var defined;
for( var i = 0; i < sheets.length; i++ ){
if( sheets[ i ].href && sheets[ i ].href.indexOf( href ) > -1 ){
defined = true;
}
}
if( defined ){
cb();
} else {
setTimeout(function() {
ss.onloadcssdefined( cb );
});
}
};
ss.onloadcssdefined(function() {
ss.media = media || "all";
});
return ss;
},
loadJS : function( src, callback ) {
"use strict";
var script = document.createElement( "script" );
script.src = src;
script.async = true;
document.body.appendChild( script );
if (callback && typeof(callback) === "function") {
script.onload = callback;
}
return script;
}
}
document.addEventListener("DOMContentLoaded", function(event) {
Moduler.init();
});
})(window, document)