Skip to content

Commit 54b5515

Browse files
author
Pete Romano
committed
Adding plugins
1 parent d74ceca commit 54b5515

File tree

14 files changed

+1176
-0
lines changed

14 files changed

+1176
-0
lines changed

async.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/** @license
2+
* RequireJS plugin for async dependency load like JSONP and Google Maps
3+
* Author: Miller Medeiros
4+
* Version: 0.1.1 (2011/11/17)
5+
* Released under the MIT license
6+
*/
7+
define(function(){
8+
9+
var DEFAULT_PARAM_NAME = 'callback',
10+
_uid = 0;
11+
12+
function injectScript(src){
13+
var s, t;
14+
s = document.createElement('script'); s.type = 'text/javascript'; s.async = true; s.src = src;
15+
t = document.getElementsByTagName('script')[0]; t.parentNode.insertBefore(s,t);
16+
}
17+
18+
function formatUrl(name, id){
19+
var paramRegex = /!(.+)/,
20+
url = name.replace(paramRegex, ''),
21+
param = (paramRegex.test(name))? name.replace(/.+!/, '') : DEFAULT_PARAM_NAME;
22+
url += (url.indexOf('?') < 0)? '?' : '&';
23+
return url + param +'='+ id;
24+
}
25+
26+
function uid() {
27+
_uid += 1;
28+
return '__async_req_'+ _uid +'__';
29+
}
30+
31+
return{
32+
load : function(name, req, onLoad, config){
33+
if(config.isBuild){
34+
onLoad(null); //avoid errors on the optimizer
35+
}else{
36+
var id = uid();
37+
window[id] = onLoad; //create a global variable that stores onLoad so callback function can define new module after async load
38+
injectScript(formatUrl(name, id));
39+
}
40+
}
41+
};
42+
});

css.js

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
/*
2+
* css! loader plugin
3+
* Allows for loading stylesheets with the 'css!' syntax.
4+
*
5+
* External stylesheets supported.
6+
*
7+
* '!' suffix skips load checking
8+
*
9+
*/
10+
define(['./normalize'], function(normalize) {
11+
if (typeof window == 'undefined')
12+
return { load: function(n, r, load){ load() } };
13+
14+
var head = document.getElementsByTagName('head')[0];
15+
16+
17+
/* XHR code - copied from RequireJS text plugin */
18+
var progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
19+
var get = function(url, callback, errback) {
20+
21+
var xhr, i, progId;
22+
if (typeof XMLHttpRequest !== 'undefined')
23+
xhr = new XMLHttpRequest();
24+
else if (typeof ActiveXObject !== 'undefined')
25+
for (i = 0; i < 3; i += 1) {
26+
progId = progIds[i];
27+
try {
28+
xhr = new ActiveXObject(progId);
29+
}
30+
catch (e) {}
31+
32+
if (xhr) {
33+
progIds = [progId]; // so faster next time
34+
break;
35+
}
36+
}
37+
38+
xhr.open('GET', url, requirejs.inlineRequire ? false : true);
39+
40+
xhr.onreadystatechange = function (evt) {
41+
var status, err;
42+
//Do not explicitly handle errors, those should be
43+
//visible via console output in the browser.
44+
if (xhr.readyState === 4) {
45+
status = xhr.status;
46+
if (status > 399 && status < 600) {
47+
//An http 4xx or 5xx error. Signal an error.
48+
err = new Error(url + ' HTTP status: ' + status);
49+
err.xhr = xhr;
50+
errback(err);
51+
}
52+
else
53+
callback(xhr.responseText);
54+
}
55+
};
56+
57+
xhr.send(null);
58+
}
59+
60+
//main api object
61+
var cssAPI = {};
62+
63+
cssAPI.pluginBuilder = './css-builder';
64+
65+
//<style> tag creation
66+
var stylesheet = document.createElement('style');
67+
stylesheet.type = 'text/css';
68+
head.appendChild(stylesheet);
69+
70+
if (stylesheet.styleSheet)
71+
cssAPI.inject = function(css) {
72+
stylesheet.styleSheet.cssText += css;
73+
}
74+
else
75+
cssAPI.inject = function(css) {
76+
stylesheet.appendChild(document.createTextNode(css));
77+
}
78+
79+
cssAPI.inspect = function() {
80+
if (stylesheet.styleSheet)
81+
return stylesheet.styleSheet.cssText;
82+
else if (stylesheet.innerHTML)
83+
return stylesheet.innerHTML;
84+
}
85+
86+
var instantCallbacks = {};
87+
cssAPI.normalize = function(name, normalize) {
88+
var instantCallback;
89+
if (name.substr(name.length - 1, 1) == '!')
90+
instantCallback = true;
91+
if (instantCallback)
92+
name = name.substr(0, name.length - 1);
93+
if (name.substr(name.length - 4, 4) == '.css')
94+
name = name.substr(0, name.length - 4);
95+
96+
name = normalize(name);
97+
98+
if (instantCallback)
99+
instantCallbacks[name] = instantCallback;
100+
101+
return name;
102+
}
103+
104+
cssAPI.load = function(cssId, req, load, config, parse) {
105+
var instantCallback = instantCallbacks[cssId];
106+
if (instantCallback)
107+
delete instantCallbacks[cssId];
108+
109+
var fileUrl = cssId;
110+
111+
if (fileUrl.substr(fileUrl.length - 4, 4) != '.css' && !parse)
112+
fileUrl += '.css';
113+
114+
fileUrl = req.toUrl(fileUrl);
115+
116+
//external url -> add as a <link> tag to load. onload support not reliable so not provided
117+
if (fileUrl.substr(0, 7) == 'http://' || fileUrl.substr(0, 8) == 'https://') {
118+
if (parse)
119+
throw 'Cannot preprocess external css.';
120+
var link = document.createElement('link');
121+
link.type = 'text/css';
122+
link.rel = 'stylesheet';
123+
link.href = fileUrl;
124+
head.appendChild(link);
125+
126+
//only instant callback due to onload not being reliable
127+
load(cssAPI);
128+
}
129+
//internal url -> download and inject into <style> tag
130+
else {
131+
get(fileUrl, function(css) {
132+
if (parse)
133+
css = parse(css);
134+
135+
var pathname = window.location.pathname.split('/');
136+
pathname.pop();
137+
pathname = pathname.join('/') + '/';
138+
139+
//make file url absolute
140+
if (fileUrl.substr(0, 1) != '/')
141+
fileUrl = '/' + normalize.convertURIBase(fileUrl, pathname, '/');
142+
143+
css = normalize(css, fileUrl, pathname);
144+
145+
cssAPI.inject(css);
146+
147+
if (!instantCallback)
148+
load(cssAPI);
149+
});
150+
if (instantCallback)
151+
load(cssAPI);
152+
}
153+
}
154+
155+
return cssAPI;
156+
});

depend.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/** @license
2+
* Plugin to load JS files that have dependencies but aren't wrapped into
3+
* `define` calls.
4+
* Author: Miller Medeiros
5+
* Version: 0.1.0 (2011/12/13)
6+
* Released under the MIT license
7+
*/
8+
define(function () {
9+
10+
var rParts = /^(.*)\[([^\]]*)\]$/;
11+
12+
return {
13+
14+
//example: depend!bar[jquery,lib/foo]
15+
load : function(name, req, onLoad, config){
16+
var parts = rParts.exec(name);
17+
18+
req(parts[2].split(','), function(){
19+
req([parts[1]], function(mod){
20+
onLoad(mod);
21+
});
22+
});
23+
}
24+
25+
};
26+
27+
});

font.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/** @license
2+
* RequireJS plugin for loading web fonts using the WebFont Loader
3+
* Author: Miller Medeiros
4+
* Version: 0.2.0 (2011/12/06)
5+
* Released under the MIT license
6+
*/
7+
define(['propertyParser'], function (propertyParser) {
8+
9+
var rParts = /^([^,]+),([^\|]+)\|?/;
10+
11+
function parseName(name) {
12+
var data = {},
13+
vendors = name.split('|'),
14+
n = vendors.length,
15+
match;
16+
17+
while (n--) {
18+
match = rParts.exec(vendors[n]);
19+
data[ match[1] ] = propertyParser.parseProperties(match[2]);
20+
}
21+
return data;
22+
}
23+
24+
// API
25+
return {
26+
27+
//example: font!google,families:[Tangerine,Cantarell,Yanone Kaffeesatz:700]
28+
load : function(name, req, onLoad, config){
29+
if (config.isBuild) {
30+
onLoad(null); //avoid errors on the optimizer
31+
} else {
32+
var data = parseName(name);
33+
data.active = onLoad;
34+
data.inactive = function(){
35+
onLoad(false);
36+
};
37+
req([(document.location.protocol === 'https:'? 'https' : 'http') +'://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js'], function(){
38+
WebFont.load(data);
39+
});
40+
}
41+
}
42+
43+
};
44+
45+
});

goog.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/** @license
2+
* RequireJS plugin for loading Google Ajax API modules thru `google.load`
3+
* Author: Miller Medeiros
4+
* Version: 0.2.0 (2011/12/06)
5+
* Released under the MIT license
6+
*/
7+
define(['async', 'propertyParser'], function (async, propertyParser) {
8+
9+
var rParts = /^([^,]+)(?:,([^,]+))?(?:,(.+))?/;
10+
11+
function parseName(name){
12+
var match = rParts.exec(name),
13+
data = {
14+
moduleName : match[1],
15+
version : match[2] || '1'
16+
};
17+
data.settings = propertyParser.parseProperties(match[3]);
18+
return data;
19+
}
20+
21+
return {
22+
load : function(name, req, onLoad, config){
23+
if (config.isBuild) {
24+
onLoad(null); //avoid errors on the optimizer
25+
} else {
26+
var data = parseName(name),
27+
settings = data.settings;
28+
29+
settings.callback = onLoad;
30+
31+
req(['async!'+ (document.location.protocol === 'https:'? 'https' : 'http') +'://www.google.com/jsapi'], function(){
32+
google.load(data.moduleName, data.version, settings);
33+
});
34+
}
35+
}
36+
};
37+
38+
});

0 commit comments

Comments
 (0)