Skip to content

Commit

Permalink
Add jQueryUI to libs plus add support for new option in libs JSON config
Browse files Browse the repository at this point in the history
  • Loading branch information
padolsey committed Feb 28, 2012
1 parent 080f0c8 commit cd9419c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 23 deletions.
2 changes: 1 addition & 1 deletion VERSION
@@ -1 +1 @@
1.1.1
1.1.2
59 changes: 46 additions & 13 deletions app/jsapi/jsapi.sourcehandler.js
Expand Up @@ -37,29 +37,45 @@ function SourceHandler(requestData, libData) {

this.libData = libData;

this.loader = new SourceHandler.Loader(requestData, libData);
this.loader = new SourceHandler.Loader(
requestData.lib + '.' + requestData.ver,
libData.url.replace('{VERSION}', requestData.ver),
requestData.refresh
);

this.loader
this.requires = libData.requires && libData.requires[requestData.ver];

.on('success', function(source) {
if (this.requires) {

this.source = source;
this.env = new SourceHandler.Environment(this.nullify + this.source);
// In the case that this lib requires another file we want
// to request the required file and then we can continue with
// `setupEnvironment` ...

this.env.on('ready', function(){
this.resolver = new SourceHandler.Resolver(this.env, this.source, this.libData);
this.emit('ready');
}.bind(this));
this.loader
.on('success', function(source) {

this.env.init();
log('Loaded lib URL: now loading requirement: ', this.requires);

}.bind(this))
// Load required script:
this.requiredLoader = new SourceHandler.Loader(
this.requires.match(/\/([^\/]+)$/)[1],
this.requires,
requestData.refresh
).on('success', function(rSource) {
this.setupEnvironment(source, rSource);
}.bind(this)).on('failure', function() {
this.loader.emit('failure');
}.bind(this)).get();
}.bind(this));

} else {
this.loader.on('success', this.setupEnvironment.bind(this));
}

this.loader
.on('failure', function(){
log('Failure on SourceHandler.Loader', arguments);
}.bind(this))

.get();

}
Expand All @@ -71,6 +87,23 @@ SourceHandler.prototype = new events.EventEmitter;
// EG: @@##__jQuery.fn.data#this.data__##@@
SourceHandler.LINKIFY_MARKER = ['@@##__', '__##@@'];

SourceHandler.prototype.setupEnvironment = function(source, preRequiredSource) {
this.source = source;

preRequiredSource = preRequiredSource || ''; // e.g. jQuery for jQuery UI (requirement)

this.env = new SourceHandler.Environment(
this.nullify + ';' + preRequiredSource + ';' + this.source
);

this.env.on('ready', function(){
this.resolver = new SourceHandler.Resolver(this.env, this.source, this.libData);
this.emit('ready');
}.bind(this));

this.env.init();
};

SourceHandler.prototype.findSingleMethod = function(method) {

var resolved = this.resolver.resolve(method),
Expand Down
15 changes: 6 additions & 9 deletions app/jsapi/jsapi.sourcehandler.loader.js
Expand Up @@ -8,14 +8,13 @@ var events = require('events'),
* SourceHandler.Loader
* Takes care of loading source files
*/
module.exports = Loader = function SHLoader(requestData, libraryData) {
module.exports = Loader = function SHLoader(filename, uri, refresh) {

events.EventEmitter.call(this);

this.requestData = requestData;
this.libraryData = libraryData;

this.filename = './_libs/' + requestData.lib + '.' + requestData.ver + '.js';
this.uri = uri;
this.filename = './_libs/' + filename + '.js';
this.refresh = !!refresh;

};

Expand All @@ -25,7 +24,7 @@ Loader.prototype.get = function() {

fs.stat(this.filename, function(err, stats) {

if (this.requestData.refresh || err && err.code === 'ENOENT') {
if (this.refresh || err && err.code === 'ENOENT') {
this.getRemoteSource();
} else if(err) {
this.emit('failure', {error: 'Error on file retrieval, ' + err.code});
Expand All @@ -40,9 +39,7 @@ Loader.prototype.get = function() {
Loader.prototype.getRemoteSource = function() {

var me = this,
libURL = url.parse(
this.libraryData.url.replace('{VERSION}', this.requestData.ver)
),
libURL = url.parse(this.uri),
filestream = fs.createWriteStream(this.filename, {
encoding: 'utf8'
});
Expand Down
15 changes: 15 additions & 0 deletions app/libs.json
Expand Up @@ -29,6 +29,21 @@
]
},

"jqui": "jqueryui",
"$ui": "jqueryui",
"jqueryui": {
"name": "jQuery UI",
"url": "http://ajax.googleapis.com/ajax/libs/jqueryui/{VERSION}/jquery-ui.js",
"dom_env": true,
"default_version": "1.8.18",
"versions": ["1", "1.8", "1.8.18"],
"get_real_version": "UI (\\d+\\.\\d+(?:\\.\\d+)?(?:pre)?)",
"look_in": ["jQuery.ui", "jQuery.widget", "jQuery.fn"],
"requires": {
"1.8": "http://code.jquery.com/jquery-1.7.js"
}
},

"mootools": {
"name": "MooTools",
"url": "http://ajax.googleapis.com/ajax/libs/mootools/{VERSION}/mootools.js",
Expand Down
1 change: 1 addition & 0 deletions index.html
Expand Up @@ -13,6 +13,7 @@ <h2>Start exploring:</h2>

<ul id="explore">
<li><a href="/jquery/">jQuery</a></li>
<li><a href="/jqueryui/">jQuery UI</a></li>
<li><a href="/mootools/">MooTools</a></li>
<li><a href="/dojo/">Dojo</a></li>
<li><a href="/underscore/">Underscore</a></li>
Expand Down
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -36,3 +36,4 @@ It works by loading the library you specify into an instance of [jsdom](https://
* `1.0.0` - Gotta start somewhere
* `1.1.0` - Add `package.json` to manage dependencies. Linkifies `this.methodName` calls with new *LINK_MARKER* syntax (incl. name of item, e.g. `this.css`, and the full name, e.g. *jQuery.fn.css*). Sorts methods in sidebar by similarity to current method name (done with [similarity.js](https://github.com/jamespadolsey/similarity.js)).
* `1.1.1` - Fixed ?expand option so it doesn't go beyond the end of the source file. Fixed issue where names present in global scope, even if not functions, take precedence in resolver, e.g. window.outerHeight when querying `/jquery/outerHeight`. Fixed this by including a `typeof v == 'function'` check in the embedded resolver function. Also fixed error being thrown when a method can't be found.
* `1.1.2` - Added jQueryUI to libs and added a `require` option in the JSON config (currently only used with jQuery UI). Also generalised SHLoader.

0 comments on commit cd9419c

Please sign in to comment.