oddmuse-cache.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. var wiki = (function() {
  2. var pages;
  3. var log = function(msg) {
  4. $("#status").text(msg);
  5. }
  6. var log_html = function(msg) {
  7. $("#status").html(msg);
  8. }
  9. var log_node = function(msg) {
  10. $("#status").empty();
  11. $("#status").append(msg);
  12. }
  13. var get_pages = function(data) {
  14. pages = data.split("\n");
  15. var count = 1;
  16. pages.pop(); // after the last newline there is nothing
  17. $("#total").text(pages.length);
  18. var get_page = function(i, id) {
  19. if (id != "") {
  20. var store_page = function(data) {
  21. window.localStorage.setItem(id, data);
  22. $("#page").text(count++);
  23. }
  24. $.get("cgi-bin/wiki.pl",
  25. {action: "browse", id: id},
  26. store_page);
  27. }
  28. }
  29. $.each(pages, get_page);
  30. window.localStorage.setItem(" pages", pages.join(" "));
  31. }
  32. var download = function() {
  33. log("Getting list of pages...");
  34. $.get("cgi-bin/wiki.pl",
  35. {action: "index", raw: "1"},
  36. get_pages);
  37. log_html('<p><a href="javascript:wiki.list()">List</a> the pages in local storage.');
  38. }
  39. var initialize = function() {
  40. pages = window.localStorage.getItem(" pages").split(" ");
  41. if (pages) {
  42. log_html('<p>Found pages in local storage. <a href="javascript:wiki.list()">List</a> the pages in local storage. <a href="javascript:wiki.download()">Download</a> a fresh copy.');
  43. } else {
  44. download();
  45. }
  46. };
  47. var list = function() {
  48. var ul = document.createElement('ul');
  49. $.each(pages, function(i, id) {
  50. var li = document.createElement('li');
  51. var a = document.createElement('a');
  52. $(a).attr({href: "javascript:wiki.browse('" + id + "')"});
  53. $(a).text(id);
  54. $(li).append(a);
  55. $(ul).append(li);
  56. });
  57. log_node(ul);
  58. }
  59. var browse = function(id) {
  60. var re = /http:\/\/localhost\/cgi-bin\/wiki.pl\/([^\/?]+)/;
  61. $('*').html(window.localStorage.getItem(id));
  62. $('a[href^="http://localhost/cgi-bin/wiki.pl"]').each(function(i, a) {
  63. var match = re.exec($(a).attr('href'));
  64. if (match) {
  65. var id = unescape(match[1]);
  66. if (pages.indexOf(id) >= 0) {
  67. $(a).attr('href', "javascript:wiki.browse('" + id + "')");
  68. }
  69. }
  70. });
  71. }
  72. return {
  73. initialize: initialize,
  74. download: download,
  75. list: list,
  76. browse: browse,
  77. };
  78. }());
  79. $(document).ready(function(evt) {
  80. wiki.initialize();
  81. });