comparison lib/GeoTemCo/js/Build/Loader/DynaJsLoader.js @ 0:b57c7821382f

initial
author Dirk Wintergruen <dwinter@mpiwg-berlin.mpg.de>
date Thu, 28 May 2015 10:28:12 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:b57c7821382f
1 /*
2 * DynaJsLoader.js
3 *
4 * Copyright (c) 2012, Stefan Jänicke. All rights reserved.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301 USA
20 */
21
22 /**
23 * Dynamic Script Loader for GeoTemCo
24 * @author Stefan Jänicke (stjaenicke@informatik.uni-leipzig.de)
25 * @release 1.0
26 * @release date: 2012-07-27
27 * @version date: 2012-07-27
28 */
29 function DynaJsLoader() {
30
31 this.checkInterval = 20;
32 this.loadAttempts = 2000;
33
34 this.loadScripts = function(scripts, callback) {
35
36 if (scripts.length > 0) {
37 this.scriptStack = scripts;
38 this.scriptIndex = 0;
39 this.loadScript(callback);
40 }
41
42 };
43
44 this.loadScript = function(callback) {
45
46 var loader = this;
47 if (this.scriptIndex < this.scriptStack.length) {
48
49 var scriptEmbedded = false, scriptLoaded = false, iter = 0;
50 var scriptData = this.scriptStack[this.scriptIndex];
51 this.scriptIndex++;
52 var testFunction = scriptData.test;
53 var test = function() {
54 scriptEmbedded = true;
55 if (!testFunction || typeof (eval(testFunction)) === 'function') {
56 scriptLoaded = true;
57 } else {
58 setTimeout(function() {
59 test();
60 }), loader.checkInterval
61 }
62 }
63 var head = document.getElementsByTagName('head')[0];
64 var script = document.createElement('script');
65 script.type = 'text/javascript';
66 script.src = scriptData.url;
67
68 script.onload = test;
69 script.onreadystatechange = function() {
70 if (this.readyState == 'complete') {
71 test();
72 }
73 }
74
75 head.appendChild(script);
76
77 var checkStatus = function() {
78 if (scriptEmbedded && scriptLoaded) {
79 loader.loadScript(callback);
80 if ( typeof console != 'undefined') {
81 console.log(scriptData.url + " loaded in " + (iter * loader.checkInterval) + " ms");
82 }
83 } else {
84 iter++;
85 if (iter > loader.loadAttempts) {
86 if ( typeof console != 'undefined') {
87 console.log("MapTimeView not loaded: Not able to load " + scriptData.url + "!");
88 Publisher.Publish('StifReady', null);
89 }
90 return;
91 }
92 setTimeout(function() {
93 checkStatus();
94 }), loader.checkInterval
95 }
96 }
97 checkStatus();
98
99 } else if (callback) {
100 callback();
101 }
102
103 };
104
105 };