PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/static/admin.js

https://github.com/samalba/CirruxCache
JavaScript | 331 lines | 307 code | 20 blank | 4 comment | 49 complexity | 9a1ba8c44967a23cf8ddd598cc2a541c MD5 | raw file
  1. /*
  2. * This file is part of the CirruxCache project
  3. * http://code.google.com/p/cirruxcache/
  4. */
  5. $(document).ready(function() {
  6. $("#flush > span").hide();
  7. var t = $("#tabs");
  8. var tabCallback = [null, fetchStore, fetchStats];
  9. t.tabs({ show: function(event, ui) {
  10. var cb = tabCallback[t.tabs("option", "selected")];
  11. if (cb)
  12. cb();
  13. $("div > span:first-child").hide();
  14. }});
  15. $("#flush > input[type=button]").bind("click", flush);
  16. $("#store > fieldset > form").bind("change", newStore);
  17. $("#store > fieldset > input[type=text]").bind("blur", checkStorePath);
  18. $("#stats > input[type=button]").bind("click", function() { fetchStats(true) });
  19. $("#config > fieldset > input").bind("click", configNewFile);
  20. $("#config > fieldset > form").bind("change", configLoadFile);
  21. $("#config > div > input[type=button]:first-child").bind("click", configSave);
  22. $("#config > div > input[type=button] + input[type=button]").bind("click", configClose);
  23. $("#config > div > fieldset:eq(0) > input[type=button]").bind("click", configMappingAdd);
  24. $("#configMapping > div > select").bind("mousedown", configMappingSelect);
  25. $("#config > div > fieldset:eq(1) > input[type=button]").bind("click", configServicesAdd);
  26. });
  27. var showMessage = function(target, message) {
  28. var t = $(target);
  29. t.html(message);
  30. t.slideDown("fast");
  31. setTimeout(function() { t.slideUp("fast") }, 3000);
  32. };
  33. var flush = function() {
  34. var data = $("#flush > textarea")[0].value.split("\n");
  35. var message = "";
  36. var j = 0;
  37. for (var i = 0; i < data.length; ++i) {
  38. var req = jQuery.trim(data[i]);
  39. $.ajax({type: "DELETE",
  40. url: req,
  41. dataType: "text",
  42. complete: function(XMLHttpRequest, textStatus) {
  43. message += ++j + ": " + textStatus + "<br />";
  44. if (i == data.length)
  45. showMessage("#flush > span", message);
  46. }});
  47. }
  48. };
  49. var fetchStore = function(force) {
  50. var target = $("#store > ul");
  51. if (!force && target.html())
  52. return;
  53. $.ajax({
  54. url: document.location.pathname + "store",
  55. dataType: "text",
  56. success: function(data, textStatus, XMLHttpRequest) {
  57. if (!data)
  58. data = " ";
  59. target.html(data);
  60. }
  61. });
  62. };
  63. var checkStorePath = function() {
  64. var url = $("#store > fieldset > input[type=text]")[0];
  65. if (url.value[url.value.length - 1] == "/" || url.value[0] != "/") {
  66. alert("Bad path syntax");
  67. url.focus();
  68. return false;
  69. }
  70. return true;
  71. };
  72. var newStore = function() {
  73. if (!checkStorePath())
  74. return false;
  75. var url = $("#store > fieldset > input[type=text]")[0].value;
  76. url += "/new";
  77. $.ajax({
  78. type: "GET",
  79. url: url,
  80. dataType: "text",
  81. success: addStore,
  82. error: function(XMLHttpRequest, textStatus, errorThrown) {
  83. showMessage("#store > span", "Error. Is billing mode enabled on this AppEngine account?");
  84. }
  85. });
  86. };
  87. var addStore = function(data, textStatus, XMLHttpRequest) {
  88. var form = $("#store > fieldset > form");
  89. form.ajaxSubmit({
  90. url: data,
  91. dataType: "text",
  92. success: function() {
  93. fetchStore(true);
  94. form[0].reset();
  95. showMessage("#store > span", "Upload successful");
  96. }
  97. });
  98. };
  99. var delStore = function(url) {
  100. var c = confirm("Remove this file?");
  101. if (!c)
  102. return;
  103. $.ajax({
  104. type: "GET",
  105. url: url + "/delete",
  106. dataType: "text",
  107. complete: function() { fetchStore(true); }
  108. });
  109. };
  110. var fetchStats = function(force) {
  111. var target = $("#stats > ul");
  112. if (!force && target.html())
  113. return;
  114. $.ajax({
  115. url: document.location.pathname + "stats",
  116. dataType: "text",
  117. success: function(data, textStatus, XMLHttpRequest) {
  118. if (!data)
  119. data = " ";
  120. target.html(data);
  121. }
  122. });
  123. };
  124. var configNewFile = function() {
  125. $("#config > fieldset").slideUp("fast");
  126. $("#config > div").slideDown("fast");
  127. };
  128. var configLoadFile = function() {
  129. var form = $("#config > fieldset > form");
  130. form.ajaxSubmit({
  131. url: document.location.pathname + "configload",
  132. dataType: "script",
  133. success: function(data, textStatus, XMLHttpRequest) {
  134. try {
  135. configLoad(data);
  136. configNewFile();
  137. }
  138. catch (e) {
  139. showMessage("#config > span", data);
  140. }
  141. }
  142. });
  143. };
  144. var configClose = function() {
  145. if (!confirm("All this configuration will be lost!"))
  146. return;
  147. document.location.href = document.location.pathname + "#config";
  148. document.location.reload();
  149. };
  150. var configMappingAdd = function(name, type) {
  151. fromEvent = (this.type != undefined);
  152. if (fromEvent || $("#configMapping > div:last-child > select").val() != "") {
  153. $("#configMapping").append("<div>" + $("#configMapping > div:first-child").html() + "</div>");
  154. $("#configMapping > div:last-child > select").bind("mousedown", configMappingSelect);
  155. $("#configMapping > div:last-child > input[type=button]").bind("click", function() {
  156. var div = this.parentNode;
  157. div.parentNode.removeChild(div);
  158. });
  159. }
  160. if (fromEvent)
  161. return;
  162. var select = $("#configMapping > div:last-child > select");
  163. configMappingSelect(select);
  164. $("#configMapping > div:last-child > input[type=text]").val(name);
  165. select.val(type);
  166. };
  167. var configMappingSelect = function(target) {
  168. if (this.type != undefined) {
  169. target = $(this);
  170. }
  171. var html = "<option value=\"\">&lt;Service Name&gt;</option>";
  172. var legend = $("#configServices > div > fieldset > legend");
  173. for (var i = 0; i < legend.length; ++i) {
  174. var v = legend[i].innerHTML;
  175. if (v == "")
  176. continue;
  177. var value = v.substr(0, v.indexOf(" ("));
  178. html += "<option value=\"" + value + "\">" + value + "</option>";
  179. }
  180. target.html(html);
  181. };
  182. var configServicesAdd = function(title, type) {
  183. if (this.type != undefined) {
  184. type = $("#config > div > fieldset:eq(1) > select").val();
  185. title = $("#config > div > fieldset:eq(1) > input[type=text]").val();
  186. }
  187. if (type == "" || title == "") {
  188. alert("Select a type AND set a service name.");
  189. return;
  190. }
  191. $("#configServices").append("<div>" + $("#configServices > div:first-child").html() + "</div>");
  192. $("#configServices > div:last-child > fieldset > legend").text(title + " (" + type + ")");
  193. $("#configServices > div:last-child > input[type=button]").bind("click", function() {
  194. var div = this.parentNode;
  195. div.parentNode.removeChild(div);
  196. });
  197. $("#configServices > div:last-child > fieldset > input[type=button]").bind("click", configServicesVarAdd);
  198. configServicesVarBind($("#configServices > div:last-child > fieldset > div"));
  199. };
  200. var configServicesVarAdd = function(type, value) {
  201. fromEvent = (this.type != undefined);
  202. var target = $(this);
  203. if (!fromEvent)
  204. target = $("#configServices > div:last-child > fieldset > input[type=button]");
  205. if (fromEvent || target.parent().find("div:last-child > select").val() != "") {
  206. var html = target.next().html();
  207. target.parent().append("<div>" + html + "</div>");
  208. target.parent().find("div:last-child > input[type=button]").bind("click", function() {
  209. var div = this.parentNode;
  210. div.parentNode.removeChild(div);
  211. });
  212. configServicesVarBind(target.parent().find("div:last-child"));
  213. }
  214. if (!fromEvent) {
  215. var select = target.parent().find("div:last-child > select");
  216. select.html(select.html() + "<option value=\"" + type + "\">" + type + "</option>");
  217. select.val(type);
  218. target.parent().find("div:last-child > input[type=text]").val(value);
  219. if ($("#configServices").children().length <= 1)
  220. return;
  221. }
  222. };
  223. var parseServiceType = function(legend) {
  224. var service = legend.text();
  225. var i = service.indexOf(" (") + 2;
  226. return service.substr(i, service.length - i - 1);
  227. };
  228. var configServicesVarBind = function(div) {
  229. var select = div.find("select");
  230. var type = parseServiceType(div.parent().find("legend"));
  231. select.bind("mousedown", function() {
  232. $.ajax({
  233. url: document.location.pathname + "configvars?" + type,
  234. dataType: "text",
  235. async: false,
  236. success: function(data, textStatus, XMLHttpRequest) {
  237. var a = eval(data);
  238. select.html("<option value=\"\">&lt;Variable&gt;</option>");
  239. for (var i in a) {
  240. select.append("<option value=\"" + a[i] + "\">" + a[i] + "</option>");
  241. }
  242. }
  243. });
  244. });
  245. var getConfigHelp = function(tooltip) {
  246. var type = div.find("select").val();
  247. var service = parseServiceType(div.parent().find("legend"));
  248. if (type == "") {
  249. $(tooltip).html("No help available");
  250. return;
  251. }
  252. $.ajax({
  253. url: document.location.pathname + "confighelp?" + service + "_" + type,
  254. dataType: "text",
  255. async: false,
  256. success: function(data, textStatus, XMLHttpRequest) {
  257. $(tooltip).html(data);
  258. }
  259. });
  260. };
  261. div.find("p").wTooltip({callBefore: getConfigHelp, content: true});
  262. };
  263. var configSave = function() {
  264. var config = "[[\n";
  265. $("#configServices > div").each(function(index, element) {
  266. element = $(element);
  267. var legend = element.find("fieldset > legend");
  268. var t = legend.text();
  269. var name = t.substr(0, t.indexOf(" ("));
  270. if (name) {
  271. var type = parseServiceType(legend);
  272. config += '"' + name + '", "' + type + '",\n[\n';
  273. element.find("fieldset > div").each(function(index, element) {
  274. element = $(element);
  275. name = element.find("input[type=text]").val();
  276. if (name)
  277. type = element.find("select").val();
  278. config += '"' + type + '", "' + name + '",\n';
  279. });
  280. config += '],\n';
  281. }
  282. });
  283. config += "],\n[";
  284. $("#configMapping > div").each(function(index, element) {
  285. element = $(element);
  286. var name = element.find("input[type=text]").val();
  287. var type = element.find("select").val();
  288. if (type)
  289. config += '"' + name + '", "' + type + '",\n';
  290. });
  291. config += "]]";
  292. var input = '<input type="hidden" name="configFile" value="'+ encodeURIComponent(config) +'" />';
  293. var url = document.location.pathname + "configsave";
  294. var markup = '<form action="' + url + '" method="POST" target="_blank">' + input + '</form>';
  295. jQuery(markup).appendTo('body').submit().remove();
  296. };
  297. var configLoad = function(content) {
  298. content = eval(content);
  299. var services = content[0];
  300. var mapping = content[1];
  301. for (var i = 0; i < services.length; i += 3) {
  302. configServicesAdd(services[i], services[i + 1]);
  303. var vars = services[i + 2];
  304. for (var j = 0; j < vars.length; j += 2) {
  305. configServicesVarAdd(vars[j], vars[j + 1]);
  306. }
  307. }
  308. for (var i = 0; i < mapping.length; i += 2) {
  309. configMappingAdd(mapping[i], mapping[i + 1]);
  310. }
  311. };