forked from kriszyp/xstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss.js
61 lines (60 loc) · 2.04 KB
/
css.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
define(["require"], function(moduleRequire){
"use strict";
/*
* AMD css! plugin
* This plugin will load and wait for css files. This could be handy when
* loading css files as part of a layer or as a way to apply a run-time theme. This
* module checks to see if the CSS is already loaded before incurring the cost
* of loading the full CSS loader codebase
*/
function testElementStyle(tag, id, property){
// test an element's style
var docElement = document.documentElement;
var testDiv = docElement.insertBefore(document.createElement(tag), docElement.firstChild);
testDiv.id = id;
var styleValue = (testDiv.currentStyle || getComputedStyle(testDiv, null))[property];
docElement.removeChild(testDiv);
return styleValue;
}
return {
load: function(resourceDef, require, callback, config) {
var url = require.toUrl(resourceDef);
var cachedCss = require.cache['url:' + url];
if(cachedCss){
// we have CSS cached inline in the build
if(cachedCss.xCss){
var parser = cachedCss.parser;
var xCss =cachedCss.xCss;
cachedCss = cachedCss.cssText;
}
moduleRequire(['./util/createStyleSheet'],function(createStyleSheet){
createStyleSheet(cachedCss);
});
if(xCss){
//require([parsed], callback);
}
return checkForParser();
}
function checkForParser(){
var parser = testElementStyle('x-parse', null, 'content');
if(parser && parser != 'none'){
// TODO: wait for parser to load
require([eval(parser)], callback);
}else{
callback();
}
}
// if there is an id test available, see if the referenced rule is already loaded,
// and if so we can completely avoid any dynamic CSS loading. If it is
// not present, we need to use the dynamic CSS loader.
var displayStyle = testElementStyle('div', resourceDef.replace(/\//g,'-').replace(/\..*/,'') + "-loaded", 'display');
if(displayStyle == "none"){
return checkForParser();
}
// use dynamic loader
moduleRequire(["./load-css"], function(load){
load(url, checkForParser);
});
}
};
});