midaswiki.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. function displayContents(rawMarkdown, section) {
  2. var pageContents = marked(rawMarkdown);
  3. document.getElementById(section).innerHTML = pageContents;
  4. }
  5. function attachEditor(markdownFile) {
  6. document.getElementById("midaswiki-editor").style.display = "none";
  7. document.getElementById("content").style.display = "block";
  8. //determine if history page or talk page
  9. var location = window.location.hash;
  10. var pageType = "";
  11. if(location != "" && typeof location != typeof undefined) {
  12. pageType = location.split('#')[1].split("-")[0];
  13. location = location.split("#")[1].split("-")[1];
  14. }
  15. var header = document.getElementById("controls");
  16. header.innerHTML = "";
  17. var controlContainer = document.createElement('ul');
  18. controlContainer.className += "tabs"; /* causes a jQuery error, need to investigate */
  19. if(pageType != "history" && pageType != "edit") {
  20. var historyTab = document.createElement("li");
  21. historyTab.className += "tab";
  22. var historyLink = document.createElement("a");
  23. historyLink.innerHTML = "History";
  24. historyLink.href = "#history-"+markdownFile;
  25. historyTab.appendChild(historyLink);
  26. //controlContainer.appendChild(historyTab);
  27. }
  28. if(pageType != "history" && pageType != "talk" && pageType != "edit") {
  29. var talkTab = document.createElement("li");
  30. talkTab.className += "tab";
  31. var talkLink = document.createElement("a");
  32. talkLink.innerHTML = "Talk";
  33. talkLink.href = "#talk-"+markdownFile;
  34. talkTab.appendChild(talkLink);
  35. controlContainer.appendChild(talkTab);
  36. }
  37. if(pageType != "edit" && pageType != "history") {
  38. var editTab = document.createElement("li");
  39. editTab.className += "tab";
  40. var editLink = document.createElement("a");
  41. editLink.innerHTML = "Edit";
  42. editLink.href = "#edit-"+markdownFile;
  43. editTab.appendChild(editLink);
  44. controlContainer.appendChild(editTab);
  45. }
  46. header.appendChild(controlContainer);
  47. }
  48. function displayEditor(raw, markdownFile) {
  49. document.getElementById("midaswiki-editor").style.display = "block";
  50. document.getElementById("content").style.display = "none";
  51. var editfield = document.getElementById("midaswiki-edit");
  52. editfield.value = raw;
  53. }
  54. function pageReload() {
  55. var location = window.location.hash.toLowerCase().replace("../", "");
  56. if(location == "" || typeof location == typeof undefined) {
  57. location = "#/index.md";
  58. }
  59. location = location.split('#')[1];
  60. var pageType = location.split("-")[0];
  61. var markdownFile = location.split("-")[1];
  62. document.getElementById("midaswiki-file").value = markdownFile;
  63. var noCacheHeaders = {
  64. "Pragma": "no-cache",
  65. "Cache-Control": "no-store, no-cache, must-revalidate, post-check=0, pre-check=0",
  66. "Expires": 0,
  67. "Last-Modified": new Date(0)//, // January 1, 1970
  68. //"If-Modified-Since": new Date(0)
  69. };
  70. var promise = ajaxGetRaw("/markdown/header.md", noCacheHeaders);
  71. promise.success = function(response) {
  72. displayContents(response.trim(), "header");
  73. };
  74. promise.failure = function(response) {
  75. console.error("Error retrieving header");
  76. };
  77. //console.log(pageType);
  78. if(pageType == "edit") {
  79. var promise = ajaxGetRaw("/markdown"+ markdownFile, noCacheHeaders);
  80. promise.success = function(response) {
  81. displayEditor(response.trim(), markdownFile);
  82. };
  83. promise.failure = function(response) {
  84. console.error("Error building editor for " + markdownFile);
  85. displayEditor(markdownFile, markdownFile);
  86. };
  87. } else {
  88. //attachEditor(location);
  89. var promise = ajaxGetRaw("/markdown"+ location, noCacheHeaders);
  90. promise.success = function(response) {
  91. displayContents(response.trim(), "content");
  92. };
  93. promise.failure = function(response) {
  94. console.error("Error retrieving " + location);
  95. displayContents("Page not found: " + location, "content");
  96. };
  97. }
  98. var promise = ajaxGetRaw("/markdown/footer.md", noCacheHeaders);
  99. promise.success = function(response) {
  100. displayContents(response.trim(), "footer");
  101. };
  102. promise.failure = function(response) {
  103. console.error("Error retrieving footer");
  104. };
  105. }
  106. document.addEventListener("DOMContentLoaded", function(event) {
  107. pageReload();
  108. });
  109. window.addEventListener("hashchange", function(event) {
  110. pageReload();
  111. });
  112. document.getElementById("midaswiki-save").addEventListener("click", function(event) {
  113. var editfield = document.getElementById("midaswiki-edit");
  114. var markdownFile = document.getElementById("midaswiki-file").value;
  115. var data = {"markdown": editfield.value, "filename": markdownFile};
  116. var savePromise = ajaxPost("/api/", data);
  117. savePromise.success = function(response) {
  118. window.location.hash = "#" + markdownFile;
  119. };
  120. });
  121. document.getElementById("midaswiki-cancel").addEventListener("click", function(event) {
  122. var markdownFile = document.getElementById("midaswiki-file").value;
  123. window.location.hash = "#" + markdownFile;
  124. });