PageRenderTime 114ms CodeModel.GetById 60ms app.highlight 14ms RepoModel.GetById 36ms app.codeStats 0ms

/timeplot/timeplot-api.js

http://showslow.googlecode.com/
JavaScript | 188 lines | 143 code | 27 blank | 18 comment | 53 complexity | 7ae0ac71d5ddc211335ed929e6ea3315 MD5 | raw file
  1/*==================================================
  2 *  Simile Timeplot API
  3 *
  4 *  Include Timeplot in your HTML file as follows:
  5 *    <script src="http://api.simile-widgets.org/timeplot/1.1/timeplot-api.js" type="text/javascript"></script>
  6 *
  7 *==================================================*/
  8
  9(function() {
 10
 11    var local = false;
 12
 13    // obtain local mode from the document URL    
 14    if (document.location.search.length > 0) {
 15        var params = document.location.search.substr(1).split("&");
 16        for (var i = 0; i < params.length; i++) {
 17            if (params[i] == "local") {
 18                local = true;
 19            }
 20        }
 21    }
 22
 23    // obtain local mode from the script URL params attribute
 24    if (!local) {
 25        var heads = document.documentElement.getElementsByTagName("head");
 26        for (var h = 0; h < heads.length; h++) {
 27            var node = heads[h].firstChild;
 28            while (node != null) {
 29                if (node.nodeType == 1 && node.tagName.toLowerCase() == "script") {
 30                    var url = node.src;
 31                    if (url.indexOf("timeplot-api") >= 0) {
 32                        local = (url.indexOf("local") >= 0);
 33                    }
 34                }
 35                node = node.nextSibling;
 36            }
 37        }
 38    }
 39
 40    // Load Timeplot if it's not already loaded (after SimileAjax and Timeline)
 41    var loadTimeplot = function() {
 42
 43        if (typeof window.Timeplot != "undefined") {
 44            return;
 45        }
 46        
 47        window.Timeplot = {
 48            loaded:     false,
 49            params:     { bundle: true, autoCreate: true },
 50            importers:  {}
 51        };
 52    
 53        var javascriptFiles = [
 54            "timeplot.js",
 55            "plot.js",
 56            "sources.js",
 57            "geometry.js",
 58            "color.js",
 59            "math.js",
 60            "processor.js"
 61        ];
 62        var cssFiles = [
 63            "timeplot.css"
 64        ];
 65        
 66        var locales = [ "en" ];
 67
 68        var defaultClientLocales = ("language" in navigator ? navigator.language : navigator.browserLanguage).split(";");
 69        for (var l = 0; l < defaultClientLocales.length; l++) {
 70            var locale = defaultClientLocales[l];
 71            if (locale != "en") {
 72                var segments = locale.split("-");
 73                if (segments.length > 1 && segments[0] != "en") {
 74                    locales.push(segments[0]);
 75                }
 76                locales.push(locale);
 77            }
 78        }
 79
 80        var paramTypes = { bundle:Boolean, js:Array, css:Array, autoCreate:Boolean };
 81        if (typeof Timeplot_urlPrefix == "string") {
 82            Timeplot.urlPrefix = Timeplot_urlPrefix;
 83            if ("Timeplot_parameters" in window) {
 84                SimileAjax.parseURLParameters(Timeplot_parameters, Timeplot.params, paramTypes);
 85            }
 86        } else {
 87            var url = SimileAjax.findScript(document, "/timeplot-api.js");
 88            if (url == null) {
 89                Timeplot.error = new Error("Failed to derive URL prefix for Simile Timeplot API code files");
 90                return;
 91            }
 92            Timeplot.urlPrefix = url.substr(0, url.indexOf("timeplot-api.js"));
 93        
 94            SimileAjax.parseURLParameters(url, Timeplot.params, paramTypes);
 95        }
 96
 97        if (Timeplot.params.locale) { // ISO-639 language codes,
 98            // optional ISO-3166 country codes (2 characters)
 99            if (Timeplot.params.locale != "en") {
100                var segments = Timeplot.params.locale.split("-");
101                if (segments.length > 1 && segments[0] != "en") {
102                    locales.push(segments[0]);
103                }
104                locales.push(Timeplot.params.locale);
105            }
106        }
107
108        var timeplotURLPrefix = Timeplot.urlPrefix;
109
110        if (local && !("console" in window)) {
111            var firebug = [ timeplotURLPrefix + "lib/firebug/firebug.js" ];
112            SimileAjax.includeJavascriptFiles(document, "", firebug);
113        }
114        
115        var canvas = document.createElement("canvas");
116
117        if (!canvas.getContext) {
118            var excanvas = [ timeplotURLPrefix + "lib/excanvas.js" ];
119            SimileAjax.includeJavascriptFiles(document, "", excanvas);
120        }
121        
122        var scriptURLs = Timeplot.params.js || [];
123        var cssURLs = Timeplot.params.css || [];
124
125        // Core scripts and styles
126        if (Timeplot.params.bundle && !local) {
127            scriptURLs.push(timeplotURLPrefix + "timeplot-bundle.js");
128            cssURLs.push(timeplotURLPrefix + "timeplot-bundle.css");
129        } else {
130            SimileAjax.prefixURLs(scriptURLs, timeplotURLPrefix + "scripts/", javascriptFiles);
131            SimileAjax.prefixURLs(cssURLs, timeplotURLPrefix + "styles/", cssFiles);
132        }
133        
134        // Localization
135        //for (var i = 0; i < locales.length; i++) {
136        //    scriptURLs.push(Timeplot.urlPrefix + "locales/" + locales[i] + "/locale.js");
137        //};
138        
139        window.SimileAjax_onLoad = function() {
140            if (local && window.console.open) window.console.open();
141            if (Timeplot.params.callback) {
142                eval(Timeplot.params.callback + "()");
143            }
144        }
145        
146        SimileAjax.includeJavascriptFiles(document, "", scriptURLs);
147        SimileAjax.includeCssFiles(document, "", cssURLs);
148        Timeplot.loaded = true;
149    };
150
151    // Load Timeline if it's not already loaded (after SimileAjax and before Timeplot)
152    var loadTimeline = function() {
153        if (typeof Timeline != "undefined") {
154            loadTimeplot();
155        } else {
156            var timelineURL = "http://api.simile-widgets.org/timeline/2.3.1/timeline-api.js?bundle=true";
157            window.SimileAjax_onLoad = loadTimeplot;
158            SimileAjax.includeJavascriptFile(document, timelineURL);
159        }
160    };
161    
162    // Load SimileAjax if it's not already loaded
163    if (typeof SimileAjax == "undefined") {
164        window.SimileAjax_onLoad = loadTimeline;
165        
166        var url = "http://api.simile-widgets.org/ajax/2.2.1/simile-ajax-api.js?bundle=true";
167                
168        var createScriptElement = function() {
169            var script = document.createElement("script");
170            script.type = "text/javascript";
171            script.language = "JavaScript";
172            script.src = url;
173            document.getElementsByTagName("head")[0].appendChild(script);
174        }
175        
176        if (document.body == null) {
177            try {
178                document.write("<script src='" + url + "' type='text/javascript'></script>");
179            } catch (e) {
180                createScriptElement();
181            }
182        } else {
183            createScriptElement();
184        }
185    } else {
186        loadTimeline();
187    }
188})();