ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 4.5 kB
JavaScript
var Path=require("./Path.js"),ilib=require("./ilib.js"),JSUtils=require("./JSUtils.js"),Loader=function(){this.protocol="file://",this.includePath=[],this.addPaths=[],this.jsutils=JSUtils};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 text,file=Path.normalize(Path.join(dir,file));"http://"!==this.protocol?(text=this._loadFile(file,!0))&&-1===this.includePath.indexOf(text)&&this.includePath.push(dir):-1===this.includePath.indexOf(dir)&&(this.includePath.push(dir),this._loadFile(file,!1,ilib.bind(this,function(text){text||(this.includePath=this.includePath.slice(-1))})))},Loader.prototype._loadFileAlongIncludePath=function(includePath,pathname){for(var textMerge={},i=0;i<includePath.length;i++){var manifest=this.manifest[includePath[i]];if(!manifest||-1<Loader.indexOf(manifest,pathname)){manifest=Path.join(includePath[i],pathname),manifest=this._loadFile(manifest,!0);if(manifest){if(void 0===this.isMultiPaths||!0!==this.isMultiPaths)return manifest;"string"==typeof manifest&&(manifest=JSON.parse(manifest)),textMerge=this.jsutils.merge(manifest,textMerge)}}}if(0<Object.keys(textMerge).length)return textMerge},Loader.prototype.loadFiles=function(paths,sync,params,callback,root){root=root||params&¶ms.base;var includePath=[];if(this.addPaths&&0<this.addPaths.length&&(includePath=includePath.concat(this.addPaths)),root&&includePath.push(root),includePath=includePath.concat(this.includePath),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){var root,manifest;0<includes.length?(root=includes[0],includes=includes.slice(1),!(manifest=this.manifest[root])||-1<Loader.indexOf(manifest,filename)?(manifest=Path.join(root,filename),this._loadFile(manifest,!1,ilib.bind(this,function(t){t?cb(t):this._loadFilesAsyncAlongIncludePath(includes,filename,cb)}))):this._loadFilesAsyncAlongIncludePath(includes,filename,cb)):cb()},Loader.prototype._loadFilesAsync=function(includePath,paths,results,callback){var filename;0<paths.length?(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)}))):"function"==typeof callback&&callback(results)},Loader.prototype._loadManifestFile=function(i,sync,cb){var filepath;i<this.includePath.length?(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)}))):"function"==typeof cb&&cb()},Loader.prototype._loadManifests=function(sync,cb){this.manifest?"function"==typeof cb&&cb():(this.manifest={},this._loadManifestFile(0,sync="boolean"!=typeof sync?!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.prototype.addPath=function(paths){paths&&(paths=ilib.isArray(paths)?paths:[paths],this.addPaths=this.addPaths.concat(paths),this.isMultiPaths=!0)},Loader.prototype.removePath=function(paths){paths&&(paths=ilib.isArray(paths)?paths:[paths]).forEach(ilib.bind(this,function(item){item=this.addPaths.indexOf(item);-1!==item&&this.addPaths.splice(item,1)}))},Loader.indexOf=function(array,obj){if(array&&obj){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(sync="boolean"!=typeof 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;