wiki.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. function readFile(name) {
  2. var r
  3. new Ajax.Request("projects/" + name + ".txt", {
  4. method: "get",
  5. asynchronous: false,
  6. onSuccess: function(transport) { r = transport.responseText },
  7. onFailure: function(transport) { r = "" },
  8. onException: function(x) { console.log(x) }
  9. })
  10. return r
  11. }
  12. function writeFile(name, text) {
  13. var ok = true
  14. new Ajax.Request("projects/" + name + ".txt", {
  15. method: "put",
  16. asynchronous: false,
  17. postBody: text,
  18. onFailure: function() { ok = false }
  19. })
  20. if (!ok)
  21. throw "unable to write file '" + name + "'"
  22. }
  23. function projectIsDirty() { return $('workspaceForm').source.value != $('workspaceForm').source.origValue }
  24. dirtyAreYouSureMessage = "The changes you have made to this project will be lost unless you press 'cancel' " +
  25. "and save your work. Proceed?"
  26. window.onbeforeunload = function() { if (projectIsDirty()) return dirtyAreYouSureMessage }
  27. function loadProject() {
  28. if (arguments.length > 0) {
  29. if (arguments[0] == "" || "#" + arguments[0] == document.location.hash)
  30. return
  31. document.location.hash = hashChangedHandler.oldHash = "#" + arguments[0]
  32. }
  33. if (projectIsDirty() && !confirm(dirtyAreYouSureMessage))
  34. return
  35. var projName = document.location.hash.substring(1),
  36. projData = readFile(projName)
  37. $('workspaceForm').source.value = projData
  38. $('workspaceForm').source.origValue = projData
  39. $('title').innerHTML = "<font color=#000088>" + projName.replace(/_/g, " ") + "</font>" + titleRest
  40. }
  41. function saveProject() {
  42. try {
  43. var projName = document.location.hash.substring(1),
  44. projData = $('workspaceForm').source.value
  45. // the following is an ugly hack to fix a bug in prototype.js
  46. if (projData == "")
  47. projData = " "
  48. writeFile(projName, projData)
  49. $('workspaceForm').source.origValue = projData
  50. alert("Project '" + projName + "' saved")
  51. }
  52. catch (e) {
  53. alert("Error: " + e + "\n" +
  54. "Please save your work locally (by cutting and pasting),\n" +
  55. "and let Alex know about this problem.")
  56. throw e
  57. }
  58. }
  59. hashChangedHandler = function() {
  60. if (document.location.hash == hashChangedHandler.oldHash)
  61. return
  62. hashChangedHandler.oldHash = document.location.hash
  63. loadProject()
  64. }
  65. hashChangedHandler.oldHash = document.location.hash
  66. hashChangedHandler.intervalId = setInterval(hashChangedHandler, 1000)