PageRenderTime 65ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/simile-ajax-api.js

http://showslow.googlecode.com/
JavaScript | 282 lines | 226 code | 28 blank | 28 comment | 76 complexity | 3e503c7c2574d1e6b371ae40e3f6e198 MD5 | raw file
  1. /*==================================================
  2. * Simile Ajax API
  3. *==================================================
  4. */
  5. if (typeof SimileAjax == "undefined") {
  6. var isCompiled = ("SimileAjax_isCompiled" in window) && window.SimileAjax_isCompiled;
  7. var SimileAjax = {
  8. loaded: false,
  9. loadingScriptsCount: 0,
  10. error: null,
  11. params: { bundle:"true" }
  12. };
  13. SimileAjax.Platform = new Object();
  14. /*
  15. HACK: We need these 2 things here because we cannot simply append
  16. a <script> element containing code that accesses SimileAjax.Platform
  17. to initialize it because IE executes that <script> code first
  18. before it loads ajax.js and platform.js.
  19. */
  20. var getHead = function(doc) {
  21. return doc.getElementsByTagName("head")[0];
  22. };
  23. SimileAjax.findScript = function(doc, substring) {
  24. var scripts=doc.documentElement.getElementsByTagName("script");
  25. for (s=0; s<scripts.length;s++) {
  26. var url = scripts[s].src;
  27. var i = url.indexOf(substring);
  28. if (i >= 0) {
  29. return url;
  30. }
  31. }
  32. var heads = doc.documentElement.getElementsByTagName("head");
  33. for (var h = 0; h < heads.length; h++) {
  34. var node = heads[h].firstChild;
  35. while (node != null) {
  36. if (node.nodeType == 1 && node.tagName.toLowerCase() == "script") {
  37. var url = node.src;
  38. var i = url.indexOf(substring);
  39. if (i >= 0) {
  40. return url;
  41. }
  42. }
  43. node = node.nextSibling;
  44. }
  45. }
  46. return null;
  47. };
  48. SimileAjax.includeJavascriptFile = function(doc, url, onerror, charset, callback) {
  49. onerror = onerror || "";
  50. if (doc.body == null) {
  51. try {
  52. var q = "'" + onerror.replace( /'/g, '&apos' ) + "'"; // "
  53. doc.write("<script src='" + url + "' onerror="+ q +
  54. (charset ? " charset='"+ charset +"'" : "") +
  55. " type='text/javascript'>"+ onerror + "</script>");
  56. return;
  57. } catch (e) {
  58. // fall through
  59. }
  60. }
  61. var calledBack = false;
  62. var callbackOnce = function() {
  63. if (callback && !calledBack) {
  64. calledBack=true;
  65. callback();
  66. }
  67. }
  68. var script = doc.createElement("script");
  69. if (onerror) {
  70. try { script.innerHTML = onerror; } catch(e) {};
  71. script.onerror = function () {onerror(); callbackOnce()};
  72. }
  73. else {
  74. script.onerror = callbackOnce;
  75. }
  76. if (charset) {
  77. script.setAttribute("charset", charset);
  78. }
  79. script.type = "text/javascript";
  80. script.language = "JavaScript";
  81. script.src = url;
  82. if (callback)
  83. script.onload = script.onreadystatechange = function() {
  84. if (!this.readyState || this.readyState == "loaded" ||
  85. this.readyState == "complete")
  86. callbackOnce();
  87. }
  88. return getHead(doc).appendChild(script);
  89. };
  90. function includeJavascriptList(doc, urlPrefix, filenames, included, index, callback) {
  91. if (!included[index]) { // avoid duplicate callback
  92. included[index] = true;
  93. if (index<filenames.length) {
  94. var nextCall=function(){
  95. includeJavascriptList(doc, urlPrefix, filenames,
  96. included, index+1, callback);
  97. }
  98. SimileAjax.
  99. includeJavascriptFile(doc,
  100. urlPrefix + filenames[index], null, null,
  101. nextCall);
  102. }
  103. else if (callback != null) callback();
  104. }
  105. }
  106. SimileAjax.includeJavascriptFiles = function(doc, urlPrefix, filenames) {
  107. if (doc.body == null) {
  108. for (var i = 0; i < filenames.length; i++) {
  109. SimileAjax.includeJavascriptFile(doc, urlPrefix + filenames[i]);
  110. }
  111. SimileAjax.loadingScriptsCount += filenames.length;
  112. SimileAjax.includeJavascriptFile(doc, SimileAjax.urlPrefix + "scripts/signal.js?" + filenames.length);
  113. }
  114. else {
  115. var included = new Array();
  116. for (var i = 0; i < filenames.length; i++)
  117. included[i] = false;
  118. if (typeof window.SimileAjax_onLoad == "string")
  119. f = eval(window.SimileAjax_onLoad);
  120. else if (typeof window.SimileAjax_onLoad == "function")
  121. f = window.SimileAjax_onLoad;
  122. window.SimileAjax_onLoad = null;
  123. includeJavascriptList(doc, urlPrefix, filenames, included, 0, f);
  124. }
  125. };
  126. SimileAjax.includeCssFile = function(doc, url) {
  127. if (doc.body == null) {
  128. try {
  129. doc.write("<link rel='stylesheet' href='" + url + "' type='text/css'/>");
  130. return;
  131. } catch (e) {
  132. // fall through
  133. }
  134. }
  135. var link = doc.createElement("link");
  136. link.setAttribute("rel", "stylesheet");
  137. link.setAttribute("type", "text/css");
  138. link.setAttribute("href", url);
  139. getHead(doc).appendChild(link);
  140. };
  141. SimileAjax.includeCssFiles = function(doc, urlPrefix, filenames) {
  142. for (var i = 0; i < filenames.length; i++) {
  143. SimileAjax.includeCssFile(doc, urlPrefix + filenames[i]);
  144. }
  145. };
  146. /**
  147. * Append into urls each string in suffixes after prefixing it with urlPrefix.
  148. * @param {Array} urls
  149. * @param {String} urlPrefix
  150. * @param {Array} suffixes
  151. */
  152. SimileAjax.prefixURLs = function(urls, urlPrefix, suffixes) {
  153. for (var i = 0; i < suffixes.length; i++) {
  154. urls.push(urlPrefix + suffixes[i]);
  155. }
  156. };
  157. /**
  158. * Parse out the query parameters from a URL
  159. * @param {String} url the url to parse, or location.href if undefined
  160. * @param {Object} to optional object to extend with the parameters
  161. * @param {Object} types optional object mapping keys to value types
  162. * (String, Number, Boolean or Array, String by default)
  163. * @return a key/value Object whose keys are the query parameter names
  164. * @type Object
  165. */
  166. SimileAjax.parseURLParameters = function(url, to, types) {
  167. to = to || {};
  168. types = types || {};
  169. if (typeof url == "undefined") {
  170. url = location.href;
  171. }
  172. var q = url.indexOf("?");
  173. if (q < 0) {
  174. return to;
  175. }
  176. url = (url+"#").slice(q+1, url.indexOf("#")); // toss the URL fragment
  177. var params = url.split("&"), param, parsed = {};
  178. var decode = window.decodeURIComponent || unescape;
  179. for (var i = 0; param = params[i]; i++) {
  180. var eq = param.indexOf("=");
  181. var name = decode(param.slice(0,eq));
  182. var old = parsed[name];
  183. var replacement = decode(param.slice(eq+1));
  184. if (typeof old == "undefined") {
  185. old = [];
  186. } else if (!(old instanceof Array)) {
  187. old = [old];
  188. }
  189. parsed[name] = old.concat(replacement);
  190. }
  191. for (var i in parsed) {
  192. if (!parsed.hasOwnProperty(i)) continue;
  193. var type = types[i] || String;
  194. var data = parsed[i];
  195. if (!(data instanceof Array)) {
  196. data = [data];
  197. }
  198. if (type === Boolean && data[0] == "false") {
  199. to[i] = false; // because Boolean("false") === true
  200. } else {
  201. to[i] = type.apply(this, data);
  202. }
  203. }
  204. return to;
  205. };
  206. if (!isCompiled) {
  207. (function() {
  208. var javascriptFiles = [
  209. "platform.js",
  210. "debug.js",
  211. "xmlhttp.js",
  212. "json.js",
  213. "dom.js",
  214. "graphics.js",
  215. "date-time.js",
  216. "string.js",
  217. "html.js",
  218. "data-structure.js",
  219. "units.js",
  220. "ajax.js",
  221. "history.js",
  222. "window-manager.js",
  223. "remoteLog.js"
  224. ];
  225. var cssFiles = [
  226. "graphics.css"
  227. ];
  228. if (!("jQuery" in window) && !("$" in window)) {
  229. // javascriptFiles.unshift("jquery-1.4.2.min.js");
  230. SimileAjax.includeJavascriptFile(document, "http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js");
  231. }
  232. if (typeof SimileAjax_urlPrefix == "string") {
  233. SimileAjax.urlPrefix = SimileAjax_urlPrefix;
  234. } else {
  235. var url = SimileAjax.findScript(document, "simile-ajax-api.js");
  236. if (url == null) {
  237. SimileAjax.error = new Error("Failed to derive URL prefix for Simile Ajax API code files");
  238. return;
  239. }
  240. SimileAjax.urlPrefix = url.substr(0, url.indexOf("simile-ajax-api.js"));
  241. SimileAjax.parseURLParameters(url, SimileAjax.params, { bundle: Boolean });
  242. }
  243. if (!isCompiled) {
  244. if (SimileAjax.params.bundle) {
  245. SimileAjax.includeJavascriptFiles(document, SimileAjax.urlPrefix, [ "simile-ajax-bundle.js" ]);
  246. } else {
  247. SimileAjax.includeJavascriptFiles(document, SimileAjax.urlPrefix + "scripts/", javascriptFiles);
  248. }
  249. SimileAjax.includeCssFiles(document, SimileAjax.urlPrefix + "styles/", cssFiles);
  250. }
  251. SimileAjax.loaded = true;
  252. })();
  253. }
  254. }