|
1 // The following identifier turns the IE conditional pre-compilation on. |
|
2 /*@cc_on @*/ |
|
3 |
|
4 /** |
|
5 * @namespace Parent namespace for the api.ovi.com project. |
|
6 */ |
|
7 if (typeof ovi == 'undefined') { |
|
8 ovi = {}; |
|
9 }; |
|
10 |
|
11 (function (ovi) { |
|
12 // ovi = function() { |
|
13 var that = {}; |
|
14 |
|
15 ovi.description = { |
|
16 proxy_url: window.location.protocol + "//" + window.location.host + "/ovi-api", |
|
17 sourceLocation: "/js", |
|
18 urlSeperator: "/", |
|
19 libFileExtension: ".js", |
|
20 minified: false, |
|
21 sign_method : "method-delegate-oauth", |
|
22 env_path : "/alpha", |
|
23 dispatch_path : "/api", |
|
24 proxy_prefix : "", //useful during dev, so same consumer key can be reused for multiple instances of ovi-api |
|
25 request_timeout : 30, // Network request timeout lapse (in seconds). |
|
26 caller_id : "", //random id used for caller authentication |
|
27 env_device : true // Do not change anything in this line, even the spacing. Else the project won't build properly for SPB. |
|
28 }; |
|
29 // that.description = description; |
|
30 |
|
31 //Ignore console calls, in case it is not imported |
|
32 ovi.console = {init:function(){},log:function(){},error:function(){},debug:function(){},info:function(){}}; |
|
33 |
|
34 //Ignore config calls, in case config.js is not imported |
|
35 ovi.config = {setenv:function(){},setServiceEndPoint:function(){}}; |
|
36 |
|
37 var _loaded = {}; //Mapping from script source URL to status (0 - not loaded, 1 - loaded) |
|
38 var _libMappings = {}; //Mapping from script source URL to libName |
|
39 var _pending_callbacks = []; //Used to delay callbacks, until all dependencies are loaded |
|
40 |
|
41 /** |
|
42 * Method to initialize source URL using location of path from where oviapi.js was loaded |
|
43 * |
|
44 */ |
|
45 var _initSourceURL = function() { |
|
46 var scripts = document.getElementsByTagName('script'); |
|
47 if (!scripts) return; // TODO Throw an exception instead. |
|
48 |
|
49 var srcFilePattern = new RegExp("/js/ovi/(oviapi|oviapi-min|oviapi_(.+)|oviapi_(.+)-min).js$", "gi"); |
|
50 for(var index = 0; index < scripts.length; index++) { |
|
51 |
|
52 var ssrc = scripts.item(index).src; |
|
53 if (typeof ssrc !== 'undefined') { |
|
54 ssrc = ssrc.replace(/^\s*|\s*$/g,''); // To remove any white spaces. |
|
55 |
|
56 var startIndex = ssrc.search(srcFilePattern); |
|
57 if (startIndex > 0) { |
|
58 |
|
59 // To determine whether the minified version of ovi libraries are used. |
|
60 if (ssrc.indexOf("min") > 0) { |
|
61 ovi.description.minified = true; |
|
62 } |
|
63 |
|
64 // If only relative src can be accessed, then use window.location ro create sourceURL |
|
65 if (ssrc.substr(0,6) !== 'http:/' && ssrc.substr(0,7) !== 'https:/') { |
|
66 var rootdir = window.location.pathname.split('/'); |
|
67 if (rootdir.length > 1) |
|
68 ovi.description.proxy_url = window.location.protocol + "//" + window.location.host + "/" + rootdir[1]; |
|
69 break; |
|
70 } else { |
|
71 ovi.description.proxy_url = ssrc.substring(0, startIndex); |
|
72 break; |
|
73 } |
|
74 } |
|
75 } |
|
76 } |
|
77 }; |
|
78 _initSourceURL(); |
|
79 |
|
80 /** |
|
81 * Get an array of the loaded libs |
|
82 * @param {Integer} Optional param to specify the status of the loaded library. |
|
83 * @return Array of libNames |
|
84 */ |
|
85 var _getLoadedLibNames = function(status){ |
|
86 var loadedLibs = []; |
|
87 if (typeof status === 'undefined') status = -1; |
|
88 for (var item in _loaded){ |
|
89 if ((status === -1) || (_loaded[item] === status)){ |
|
90 loadedLibs.push(_libMappings[item]); |
|
91 } |
|
92 } |
|
93 return loadedLibs; |
|
94 }; |
|
95 |
|
96 /** |
|
97 * Get an object with loaded lib name as key and load status as value (1 - loaded, 0 - not loaded) |
|
98 * @param {Integer} Optional param to specify the status of the loaded library. |
|
99 * @return Object with libNames as key and load status as value |
|
100 */ |
|
101 var _getLoadedLibNamesAsObject = function(status){ |
|
102 var loadedLibs = {}; |
|
103 if (typeof status === 'undefined') status = -1; |
|
104 for (var item in _loaded){ |
|
105 if ((status === -1) || (_loaded[item] === status)){ |
|
106 loadedLibs[_libMappings[item]] = _loaded[item]; |
|
107 } |
|
108 } |
|
109 return loadedLibs; |
|
110 }; |
|
111 |
|
112 /** |
|
113 * Test if script was already loaded |
|
114 * @param {String} scriptSrcURL source URL of script |
|
115 * @return {Boolean} |
|
116 */ |
|
117 var _isExistingScript = function(scriptSrcURL){ |
|
118 for (var item in _loaded){ |
|
119 if (scriptSrcURL === item){ |
|
120 return true; |
|
121 } |
|
122 } |
|
123 return false; |
|
124 }; |
|
125 |
|
126 /** |
|
127 * Method to verify if a library object has already been loaded. In case of packaged/minified |
|
128 * version of ovi libraries all the libraries would reside in one single JS file. |
|
129 * |
|
130 * @param {String} libraryName Name of the library object to check. |
|
131 * @return {Boolean} Based on whether the check is true/false. |
|
132 */ |
|
133 var _isExistingLib = function(libraryName) { |
|
134 for (var lib in ovi.libName) { |
|
135 if (libraryName == lib) { |
|
136 return true; |
|
137 } |
|
138 } |
|
139 return false; |
|
140 } |
|
141 |
|
142 var _determineScriptSrc = function(libName, options){ |
|
143 var fileName = libName; |
|
144 var scriptSrcURL = ovi.description.proxy_url; |
|
145 |
|
146 // This helps while using this on a widget. |
|
147 if (options != undefined && options.sourceURL != undefined) { |
|
148 scriptSrcURL = options.sourceURL; |
|
149 } |
|
150 |
|
151 var fileExt = ""; |
|
152 // If not using slash path, then use ".js" file extension and replace dot with slash. |
|
153 if(fileName.indexOf('/') == -1){ |
|
154 fileExt = ovi.description.libFileExtension; |
|
155 fileName = fileName.replace(/\./g,'/'); |
|
156 } |
|
157 |
|
158 var rootDir = ""; |
|
159 //If ovi package, then use source location as root dir |
|
160 if (fileName.substr(0,3) === "ovi") |
|
161 rootDir = ovi.description.sourceLocation; |
|
162 |
|
163 scriptSrcURL = scriptSrcURL + rootDir + "/" + fileName |
|
164 + ((ovi.description.minified) ? "-min" : "") |
|
165 + fileExt; |
|
166 return scriptSrcURL; |
|
167 }; |
|
168 |
|
169 var _loadScript = function(scriptSrcURL, srcLib, options){ |
|
170 |
|
171 function _loadedcallback(){ |
|
172 |
|
173 //console.log(new Date().getTime()+" Loaded script:"+scriptSrcURL+" "+_getLoadedLibNames(1)); |
|
174 //console.log("not loaded:"+_getLoadedLibNames(0)); |
|
175 |
|
176 //Mark script as loaded |
|
177 _loaded[scriptSrcURL] = 1; |
|
178 |
|
179 //If callback exists, then add to pending callbacks |
|
180 if (options && options.callback != null) |
|
181 _pending_callbacks.push(options.callback); |
|
182 |
|
183 //If 1 or more items are pending, then don't call callbacks |
|
184 var done = true; |
|
185 for (var item in _loaded){ |
|
186 if (_loaded[item] === 0){ |
|
187 done = false; |
|
188 break; |
|
189 } |
|
190 } |
|
191 |
|
192 //If all libs are loaded, then call callbacks for all includes, if any |
|
193 if (done == true){ |
|
194 //console.log(new Date().getTime()+" DONE AT LAST\n"); |
|
195 //Call all pending callbacks |
|
196 for (var index = 0; index < _pending_callbacks.length; index++){ |
|
197 try{ |
|
198 _pending_callbacks[index].call(); |
|
199 }catch(err){ |
|
200 //console.log("failed to exec callback"); |
|
201 } |
|
202 } |
|
203 _pending_callbacks = []; //Reset pending callbacks list |
|
204 ovi.onReady(_getLoadedLibNamesAsObject(1)); //call onReady |
|
205 } |
|
206 } |
|
207 |
|
208 // If the library is already loaded (throug packaged/minified source file) then skip the download process. |
|
209 if (!_isExistingLib(srcLib)) { |
|
210 var scriptTag = document.createElement('script'); |
|
211 scriptTag.type = 'text/javascript'; |
|
212 scriptTag.src = scriptSrcURL; |
|
213 |
|
214 if(window.opera){ |
|
215 scriptTag.onload = function(){ |
|
216 scriptTag.onload = null; |
|
217 _loadedcallback(); |
|
218 }; |
|
219 } |
|
220 //From Nicholas C. Zakas (http://www.nczonline.net/blog/2009/06/23/loading-javascript-without-blocking/) |
|
221 else if (scriptTag.readyState){ //IE |
|
222 scriptTag.onreadystatechange = function(){ |
|
223 if (scriptTag.readyState == "loaded" || scriptTag.readyState == "complete"){ |
|
224 scriptTag.onreadystatechange = null; |
|
225 _loadedcallback(); |
|
226 } |
|
227 }; |
|
228 } else { //Others |
|
229 scriptTag.onload = function(){ |
|
230 _loadedcallback(); |
|
231 }; |
|
232 } |
|
233 |
|
234 var documentHead = document.getElementsByTagName("head")[0]; |
|
235 var newScriptTag = documentHead.appendChild(scriptTag); |
|
236 if (newScriptTag) return true; |
|
237 } else { |
|
238 _loadedcallback(); |
|
239 return true; |
|
240 } |
|
241 return false; |
|
242 }; |
|
243 |
|
244 /** |
|
245 * ovi.include - Method to dynamically include JS libraries from "api.ovi.com".<br/><br/> |
|
246 * The ovi.onReady(libs) function is triggered once the library and its |
|
247 * dependencies are loaded. See documentation of ovi.onReady. |
|
248 * |
|
249 * @param {String,Array} |
|
250 * libNames Names of one or more libraries to be included, specified as an |
|
251 * array of strings or as a comma separated string. |
|
252 * Library name can be specified in dot notation or as a relative path. <span |
|
253 * style="color:blue">e.g., "ovi.api.people", |
|
254 * "ovi/api/people.1.0.js".</span> External libraries must start |
|
255 * with "lib" <span style="color:blue">e.g., "lib.jquery", |
|
256 * "lib/jquery.json.js".</span> |
|
257 * @param {Object} |
|
258 * options Name/Value object to hold other optional parameters. |
|
259 * @param {Function} |
|
260 * options.callback Function to be called after a library has |
|
261 * been loaded. |
|
262 * |
|
263 * @example |
|
264 * ovi.include("ovi.api.maps"); |
|
265 * ovi.include("ovi.api.people",{callback:function(){callMeBack()}}); |
|
266 * ovi.include("ovi.api.people, ovi.api.share", {callback:function(){callMeBack()}}); |
|
267 * |
|
268 * |
|
269 * |
|
270 * @function |
|
271 * @methodOf ovi |
|
272 */ |
|
273 ovi.include = function(libNames, options) { |
|
274 var importStatus = true; |
|
275 try { |
|
276 if (libNames != undefined) { |
|
277 if (typeof libNames === 'string') |
|
278 libNames = libNames.split(','); |
|
279 |
|
280 // Determine which scripts (and also libraries) need to be loaded |
|
281 var toLoadScript = []; |
|
282 for (var index = 0; index < libNames.length; index++){ |
|
283 var libName = libNames[index].replace(/^\s*|\s*$/g,''); // Remove any white spaces. |
|
284 |
|
285 // Determine script source URL from libName |
|
286 var scriptSrcURL = _determineScriptSrc(libName, options); |
|
287 if (!_isExistingScript(scriptSrcURL)) { |
|
288 _loaded[scriptSrcURL] = 0; //Mark script as pending |
|
289 _libMappings[scriptSrcURL] = libName; |
|
290 toLoadScript.push(scriptSrcURL); |
|
291 |
|
292 } else { |
|
293 if (options && options.callback) |
|
294 toLoadScript.push(scriptSrcURL); |
|
295 } |
|
296 //console.log("o================o"); |
|
297 } |
|
298 |
|
299 var lstatus; |
|
300 //Load scripts that need to be loaded. Pass optional callback only for last script (since callback should be called only once). |
|
301 for (var index = 0; index < toLoadScript.length; index ++){ |
|
302 if (index == (toLoadScript.length-1)) // if last lib, then pass optional callback |
|
303 lstatus = _loadScript(toLoadScript[index], _libMappings[toLoadScript[index]], options); |
|
304 else |
|
305 lstatus = _loadScript(toLoadScript[index], _libMappings[toLoadScript[index]]); |
|
306 if (!lstatus) importStatus = false; |
|
307 } |
|
308 } |
|
309 } catch (exception) { |
|
310 importStatus = false; |
|
311 } |
|
312 return importStatus; |
|
313 }; |
|
314 // that.include = include; |
|
315 |
|
316 /** |
|
317 * ovi.onReady - Callback function triggered after calling ovi.include, |
|
318 * once the library and its dependencies are loaded. Developers can override the default |
|
319 * implementation, with their own ovi.onReady function. If successive calls to ovi.include are made, |
|
320 * ovi.onReady may be triggered only once. |
|
321 * |
|
322 * @param {Object} |
|
323 * libs - library name as key and load status as value (0 - not |
|
324 * loaded, 1 - loaded). Developer must check if the libraries |
|
325 * they need are loaded before using them using the ovi.testIfLoaded function. |
|
326 * |
|
327 * @function |
|
328 * @methodOf ovi |
|
329 */ |
|
330 ovi.onReady = function(libs) { |
|
331 //default, do nothing |
|
332 //console.log("onReady:"); |
|
333 //console.log(libs); |
|
334 }; |
|
335 // that.onReady = onReady; |
|
336 |
|
337 /** |
|
338 * ovi.testIfLoaded - Utility function to test if the specified lib names are marked as loaded in the specified loaded libs object. |
|
339 * Used to test the status of libs in ovi.onReady. |
|
340 * |
|
341 * @param {String,Array} |
|
342 * libNames Names of one or more libraries to be included, specified as an |
|
343 * array of strings or as a comma separated string. |
|
344 * |
|
345 * @param {Object} |
|
346 * loadedLibs A mapping of lib name string to load status (0 - not loaded, 1 - loaded) |
|
347 * |
|
348 * @return {Boolean} |
|
349 * |
|
350 * @function |
|
351 * @methodOf ovi |
|
352 */ |
|
353 ovi.testIfLoaded = function(libNames, loadedLibs){ |
|
354 var reqdlibs = libNames; |
|
355 if (typeof libNames === 'string') |
|
356 reqdlibs = libNames.split(','); |
|
357 for (var index = 0; index < reqdlibs.length; index ++){ |
|
358 var libName = reqdlibs[index].replace(/\s/g,''); |
|
359 if ((libName in loadedLibs) && (loadedLibs[libName] == 1)) |
|
360 continue; |
|
361 else |
|
362 return false; |
|
363 } |
|
364 return true; |
|
365 }; |
|
366 // that.testIfLoaded = testIfLoaded; |
|
367 |
|
368 /** |
|
369 * ovi.setCallerId - Sets caller id required to authenticate API caller with Ovi API backend. |
|
370 * |
|
371 * @param {String} |
|
372 * cid - Caller id string |
|
373 * |
|
374 * @function |
|
375 * @methodOf ovi |
|
376 */ |
|
377 ovi.setCallerId = function(cid){ |
|
378 ovi.description.caller_id = cid; |
|
379 }; |
|
380 |
|
381 // return that; |
|
382 // }(); |
|
383 })(ovi); |
|
384 |
|
385 |
|
386 /** |
|
387 * @property {Object} services Object that holds configurable parameters of all the libraries. |
|
388 * @memberOf ovi |
|
389 */ |
|
390 ovi.services = {}; |
|
391 ovi.services["import"] = ovi.description; |
|
392 |
|
393 /** |
|
394 * @property {Object} libName Object that holds the identified name of all the OVI API libraries. |
|
395 * @memberOf ovi |
|
396 */ |
|
397 ovi.libName = {}; |
|
398 ovi.libName["ovi.oviapi"] = ovi; |
|
399 |
|
400 ovi.include('ovi.config'); |
|
401 |
|
402 /* |
|
403 @@todo: |
|
404 |
|
405 - if one include inside of another fails to load, then failure is silent |
|
406 - file imports will fail silently if the path is wrong |
|
407 - Safari reinclude doesn't work (seems that DOM removeChild doesn't really remove the script) |
|
408 */ |