ilib
Version:
iLib is a cross-engine library of internationalization (i18n) classes written in pure JS
1 lines • 3.26 kB
JavaScript
var Path=require("./Path.js");var ilib=require("./ilib.js");var Loader=function(){this.protocol="file://";this.includePath=[]};Loader.prototype=new ilib.Loader;Loader.prototype.parent=ilib.Loader;Loader.prototype.constructor=Loader;Loader.prototype._loadFile=function(i,t,e){};Loader.prototype._exists=function(i,t){var e=Path.normalize(Path.join(i,t));if(this.protocol!=="http://"){var n=this._loadFile(e,true);if(n){this.includePath.push(i)}}else{this.includePath.push(i);this._loadFile(e,false,ilib.bind(this,function(i){if(!i){this.includePath=this.includePath.slice(-1)}}))}};Loader.prototype._loadFileAlongIncludePath=function(i,t){for(var e=0;e<i.length;e++){var n=this.manifest[i[e]];if(!n||Loader.indexOf(n,t)>-1){var o=Path.join(i[e],t);var a=this._loadFile(o,true);if(a){return a}}}return undefined};Loader.prototype.loadFiles=function(i,t,e,n){var o=e&&e.base?[e.base].concat(this.includePath):this.includePath;if(!i){return}if(e&&e.returnOne){var a;var s=[];while((a=i.pop())!==undefined){s.push(a)}i=s}if(t){var l=[];this._loadManifests(true);for(var r=0;r<i.length;r++){var f=this._loadFileAlongIncludePath(o,Path.normalize(i[r]));l.push(f?JSON.parse(f):undefined);if(e&&e.returnOne&&f){break}}if(typeof n==="function"){n(l)}return l}this._loadManifests(false,ilib.bind(this,function(){this.results=[];this._loadFilesAsync(o,i,n)}))};Loader.prototype._loadFilesAsyncAlongIncludePath=function(i,t,e){var n=undefined;if(i.length>0){var o=i[0];i=i.slice(1);var a=this.manifest[o];if(!a||Loader.indexOf(a,t)>-1){var s=Path.join(o,t);this._loadFile(s,false,ilib.bind(this,function(n){if(n){e(n)}else{this._loadFilesAsyncAlongIncludePath(i,t,e)}}))}else{this._loadFilesAsyncAlongIncludePath(i,t,e)}}else{e()}};Loader.prototype._loadFilesAsync=function(i,t,e){if(t.length>0){var n=t[0];t=t.slice(1);this._loadFilesAsyncAlongIncludePath(i,n,ilib.bind(this,function(n){this.results.push(n?JSON.parse(n):undefined);this._loadFilesAsync(i,t,e)}))}else{if(typeof e==="function"){e(this.results)}}};Loader.prototype._loadManifestFile=function(i,t,e){if(i<this.includePath.length){var n=Path.join(this.includePath[i],"ilibmanifest.json");var o=this._loadFile(n,t,ilib.bind(this,function(n){if(n){this.manifest[this.includePath[i]]=JSON.parse(n).files}this._loadManifestFile(i+1,t,e)}))}else{if(typeof e==="function"){e()}}};Loader.prototype._loadManifests=function(i,t){if(!this.manifest){this.manifest={};if(typeof i!=="boolean"){i=true}this._loadManifestFile(0,i,t)}else{if(typeof t==="function"){t()}}};Loader.prototype.listAvailableFiles=function(i,t){this._loadManifests(i,ilib.bind(this,function(){if(typeof t==="function"){t(this.manifest)}}));return this.manifest};Loader.indexOf=function(i,t){if(!i||!t){return-1}if(typeof i.indexOf==="function"){return i.indexOf(t)}else{for(var e=0;e<i.length;e++){if(i[e]===t){return e}}return-1}};Loader.prototype.checkAvailability=function(i){for(var t in this.manifest){if(Loader.indexOf(this.manifest[t],i)!==-1){return true}}return false};Loader.prototype.isAvailable=function(i,t,e){if(typeof t!=="boolean"){t=true}if(t){this._loadManifests(t);return this.checkAvailability(i)}this._loadManifests(false,ilib.bind(this,function(){if(typeof e==="function"){e(this.checkAvailability(i))}}))};module.exports=Loader;