UNPKG

ilib

Version:

iLib is a cross-engine library of internationalization (i18n) classes written in pure JS

1 lines 3.66 kB
var Path=require("./Path.js"),ilib=require("./ilib.js"),Loader=function(){this.protocol="file://",this.includePath=[]};Loader.prototype=new ilib.Loader(),Loader.prototype.parent=ilib.Loader,(Loader.prototype.constructor=Loader).prototype._loadFile=function(pathname,sync,cb){},Loader.prototype._exists=function(dir,file){var fullpath=Path.normalize(Path.join(dir,file));"http://"!==this.protocol?this._loadFile(fullpath,!0)&&this.includePath.push(dir):(this.includePath.push(dir),this._loadFile(fullpath,!1,ilib.bind(this,function(text){text||(this.includePath=this.includePath.slice(-1))})))},Loader.prototype._loadFileAlongIncludePath=function(includePath,pathname){for(var i=0;i<includePath.length;i++){var manifest=this.manifest[includePath[i]];if(!manifest||-1<Loader.indexOf(manifest,pathname)){var filepath=Path.join(includePath[i],pathname),text=this._loadFile(filepath,!0);if(text)return text}}},Loader.prototype.loadFiles=function(paths,sync,params,callback){var includePath=params&&params.base?[params.base].concat(this.includePath):this.includePath;if(paths){if(sync){var ret=[];this._loadManifests(!0);for(var i=0;i<paths.length;i++){var text=this._loadFileAlongIncludePath(includePath,Path.normalize(paths[i]));ret.push("string"==typeof text?JSON.parse(text):text)}return"function"==typeof callback&&callback(ret),ret}this._loadManifests(!1,ilib.bind(this,function(){this._loadFilesAsync(includePath,paths,[],callback)}))}},Loader.prototype._loadFilesAsyncAlongIncludePath=function(includes,filename,cb){if(0<includes.length){var root=includes[0];includes=includes.slice(1);var manifest=this.manifest[root];if(!manifest||-1<Loader.indexOf(manifest,filename)){var filepath=Path.join(root,filename);this._loadFile(filepath,!1,ilib.bind(this,function(t){t?cb(t):this._loadFilesAsyncAlongIncludePath(includes,filename,cb)}))}else this._loadFilesAsyncAlongIncludePath(includes,filename,cb)}else cb()},Loader.prototype._loadFilesAsync=function(includePath,paths,results,callback){if(0<paths.length){var filename=paths[0];paths=paths.slice(1),this._loadFilesAsyncAlongIncludePath(includePath,filename,ilib.bind(this,function(json){results.push("string"==typeof json?JSON.parse(json):json),this._loadFilesAsync(includePath,paths,results,callback)}))}else"function"==typeof callback&&callback(results)},Loader.prototype._loadManifestFile=function(i,sync,cb){if(i<this.includePath.length){var filepath=Path.join(this.includePath[i],"ilibmanifest.json");this._loadFile(filepath,sync,ilib.bind(this,function(text){text&&(this.manifest[this.includePath[i]]=("string"==typeof text?JSON.parse(text):text).files),this._loadManifestFile(i+1,sync,cb)}))}else"function"==typeof cb&&cb()},Loader.prototype._loadManifests=function(sync,cb){this.manifest?"function"==typeof cb&&cb():(this.manifest={},"boolean"!=typeof sync&&(sync=!0),this._loadManifestFile(0,sync,cb))},Loader.prototype.listAvailableFiles=function(sync,cb){return this._loadManifests(sync,ilib.bind(this,function(){"function"==typeof cb&&cb(this.manifest)})),this.manifest},Loader.indexOf=function(array,obj){if(!array||!obj)return-1;if("function"==typeof array.indexOf)return array.indexOf(obj);for(var i=0;i<array.length;i++)if(array[i]===obj)return i;return-1},Loader.prototype.checkAvailability=function(file){for(var dir in this.manifest)if(-1!==Loader.indexOf(this.manifest[dir],file))return!0;return!1},Loader.prototype.isAvailable=function(file,sync,cb){if("boolean"!=typeof sync&&(sync=!0),sync)return this._loadManifests(sync),this.checkAvailability(file);this._loadManifests(!1,ilib.bind(this,function(){"function"==typeof cb&&cb(this.checkAvailability(file))}))},module.exports=Loader;