1 /*
  2  * WebLoader.js - loader implementation for web apps.
  3  *
  4  * Copyright © 2015-2016, JEDLSoft
  5  *
  6  * Licensed under the Apache License, Version 2.0 (the "License");
  7  * you may not use this file except in compliance with the License.
  8  * You may obtain a copy of the License at
  9  *
 10  *     http://www.apache.org/licenses/LICENSE-2.0
 11  *
 12  * Unless required by applicable law or agreed to in writing, software
 13  * distributed under the License is distributed on an "AS IS" BASIS,
 14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  *
 16  * See the License for the specific language governing permissions and
 17  * limitations under the License.
 18  */
 19 
 20 var Path = require("./Path.js");
 21 var ilib = require("./ilib.js");
 22 var Loader = require("./Loader.js");
 23 
 24 var Locale = require("./Locale.js");
 25 
 26 /**
 27  * @class
 28  * An implementation of the Loader class to load locale web pages.
 29  *
 30  * @constructor
 31  * @private
 32  */
 33 var WebLoader = function(ilib, sync, onLoad) {
 34     //console.log("new WebLoader instance\n");
 35 
 36     this.parent.call(this, ilib);
 37 
 38     this._loadFile = (navigator.userAgent.indexOf(" .NET") > -1) ? this._ieLoadFile : this._regularLoadFile;
 39 
 40     // for use from within a check-out of ilib
 41     var base, root, pos, colon;
 42 
 43     var scripts = document.getElementsByTagName("script");
 44 
 45     pos = window.location.href.lastIndexOf("html");
 46     this.root = window.location.href.substring(0, pos);
 47     pos = this.root.lastIndexOf("/");
 48     this.root = this.root.substring(0, pos);
 49 
 50     colon = this.root.indexOf('://');
 51     this.root = Path.normalize(Path.join(this.root.substring(colon+3)));
 52 
 53     for (var i = 0; i < scripts.length; i++) {
 54         var source = scripts[i].src;
 55         if (source && (pos = source.search(/\/ilib-[a-z\-]*\.js$/)) !== -1) {
 56             colon = source.indexOf('://');
 57             this.protocol = source.substring(0,colon+3);
 58             base = Path.join(source.substring(colon+3, pos-1), "..");
 59             break;
 60         }
 61     }
 62 
 63     this.base = Path.normalize(Path.join(base || this.root, "data"));
 64 
 65     //console.log("WebLoader.constructor: this.base is " + this.base);
 66     //console.log("WebLoader.constructor: this.root is " + this.root);
 67 
 68     this.includePath.push(Path.join(this.root, "resources"));     // always check the application's resources dir first
 69 
 70     // then a standard locale dir of a built version of ilib
 71     this._exists(Path.join(base, "locale"), "localeinfo.json");
 72 
 73     // then try the standard install directories
 74     this._exists("/usr/share/javascript/ilib/locale", "localeinfo.json");
 75 
 76     // if all else fails, try a check-out dir of ilib
 77     // this._exists(Path.join(this.base, "locale"), "localeinfo.json");
 78 };
 79 
 80 WebLoader.prototype = new Loader();
 81 WebLoader.prototype.parent = Loader;
 82 WebLoader.prototype.constructor = WebLoader;
 83 
 84 WebLoader.prototype.name = "WebLoader";
 85 WebLoader.prototype._ieLoadFile = function(pathname, sync, cb) {
 86     // special case for IE because it has a @#$%ed up XMLHttpRequest implementation
 87     var req = new ActiveXObject("MSXML2.XMLHTTP");
 88     var text = undefined;
 89 
 90     req.open("GET", this.protocol + pathname, !sync);
 91 
 92     if (!sync) {
 93         req.onreadystatechange = function() {
 94             if (req.readyState === 4) {
 95                 text = req.responseText;
 96                 if (typeof(cb) === 'function') {
 97                     cb(text);
 98                 }
 99             }
100         };
101     }
102 
103     try {
104         req.send();
105 
106         text = req.responseText;
107     } catch (e) {
108         text = undefined;
109     }
110     if (sync) {
111         if (typeof(cb) === 'function') {
112             cb(text);
113         }
114     }
115 
116     return text;
117 };
118 WebLoader.prototype._regularLoadFile = function (pathname, sync, cb) {
119     // use normal web techniques
120     var req = new XMLHttpRequest();
121     var text = undefined;
122 
123     //req.open("GET", "file:" + Path.resolve(file), false);
124     if (pathname.substring(0, this.protocol.length) !== this.protocol) {
125         pathname = this.protocol + pathname;
126     }
127     req.open("GET", pathname, !sync);
128     //req.responseType = "text";
129     req.onload = function(e) {
130         text = req.response;
131         if (typeof(cb) === 'function') {
132             cb((req.status === 0 || req.status === 200) ? text : undefined);
133         }
134     };
135     req.onerror = function(err) {
136         // file is not there or could not be loaded
137         text = undefined;
138         if (typeof(cb) === 'function') {
139             cb(undefined);
140         }
141     };
142 
143     //console.log("url is " + JSON.stringify(req._url, undefined, 4));
144     try {
145         req.send();
146     } catch (e) {
147         // could not load the file
148         text = undefined;
149         if (typeof(cb) === 'function') {
150             cb(undefined);
151         }
152     }
153 
154     return text;
155 };
156 
157 WebLoader.prototype.getProperPath = function(params) {
158     var loc, language, script, region;
159     var isExist, returnPath, dir;
160     var baseResDir = "resources";
161 
162     if (!params.name) {
163         return undefined;
164     }
165 
166     if (params) {
167         if (params.locale){
168             this.locale = (typeof(params.locale) === 'string') ? new Locale(params.locale) : params.locale;
169         }
170 
171         if (params.name) {
172             this.name = params.name;
173         }
174 
175         if (params.baseResDir) {
176             baseResDir = params.baseResDir;
177         }
178 
179         loc = this.locale || new Locale();
180 
181         language = loc.getLanguage();
182         script = loc.getScript();
183         region = loc.getRegion();
184 
185         this.listAvailableFiles();
186 
187         if (language) {
188             dir = language +"/" + this.name;
189             isExist = this.checkAvailability(dir);
190             if (isExist) {
191                 returnPath = dir;
192             }
193         }
194 
195         if (script) {
196             dir = language +"/" + script + "/" + this.name;
197             isExist = this.checkAvailability(dir);
198             if (isExist) {
199                 returnPath = dir;
200             }
201         }
202 
203         if (region) {
204             if (script) {
205                 dir = language +"/" + script + "/"  + region + "/" + this.name;
206             } else {
207                 dir = language +"/" + region + "/" + this.name;
208             }
209 
210             isExist = this.checkAvailability(dir);
211             if (isExist) {
212                 returnPath = dir;
213             }
214         }
215 
216         if (!returnPath) {
217             return this.name;
218         }
219 
220     } else {
221         return undefined;
222     }
223     return baseResDir + "/" + returnPath;
224 }
225 
226 module.exports = WebLoader;
227