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