comparison static-files/wiki.js @ 25:0aecc756ea18

The web app now commits to the server, though it doesn't handle any edge cases whatsoever and is therefore extremely fragile.
author Atul Varma <varmaa@toolness.com>
date Thu, 12 Feb 2009 20:06:17 -0800
parents b094768c285b
children a42400d52a1e
comparison
equal deleted inserted replaced
24:b094768c285b 25:0aecc756ea18
36 36
37 var App = { 37 var App = {
38 PART_SEPARATOR: "\n\n", 38 PART_SEPARATOR: "\n\n",
39 creole: null, 39 creole: null,
40 editingElement: null, 40 editingElement: null,
41 latestRemoteChangeset: null,
41 localChanges: [], 42 localChanges: [],
42 eventHandlers: {} 43 eventHandlers: {}
43 }; 44 };
44 45
45 App.eventHandlers.editPart = function editPart(aEvt) { 46 App.eventHandlers.editPart = function editPart(aEvt) {
58 ); 59 );
59 60
60 return partsMarkup.join(App.PART_SEPARATOR); 61 return partsMarkup.join(App.PART_SEPARATOR);
61 }; 62 };
62 63
63 App.saveChanges = function saveChanges(doFadeIn) { 64 App.saveChanges = function saveChanges(isUserChange) {
64 var markup = App.getMarkup($(window.document)); 65 var markup = App.getMarkup($(window.document));
65 var changeset = {date: new Date(), 66 var changeset = {date: new Date(),
66 content: markup}; 67 content: markup,
68 parentRemoteChangeset: App.latestRemoteChangeset};
67 App.localChanges.push(changeset); 69 App.localChanges.push(changeset);
68 var changesetElem = $('<div class="changeset selected"></div>'); 70 var changesetElem = $('<div class="changeset selected"></div>');
69 function setText() { 71 function setText() {
70 changesetElem.text(jQuery.timeago(changeset.date)); 72 changesetElem.text(jQuery.timeago(changeset.date));
71 } 73 }
74 changesetElem.mousedown( 76 changesetElem.mousedown(
75 function(aEvt) { 77 function(aEvt) {
76 78
77 aEvt.preventDefault(); 79 aEvt.preventDefault();
78 if (aEvt.shiftKey) { 80 if (aEvt.shiftKey) {
79 $('#local-changes').find('.changeset').removeClass('diffed'); 81 $('#recent-changes').find('.changeset').removeClass('diffed');
80 var newerVersion = App.getMarkup($("#content")); 82 var newerVersion = App.getMarkup($("#content"));
81 var diff = $('<div class="diff"></div>'); 83 var diff = $('<div class="diff"></div>');
82 var markupDiv = $('<div class="creole-markup"></div>'); 84 var markupDiv = $('<div class="creole-markup"></div>');
83 markupDiv.text(newerVersion); 85 markupDiv.text(newerVersion);
84 diff.html(diffString(changeset.content, newerVersion)); 86 diff.html(diffString(changeset.content, newerVersion));
85 $("#content").empty(); 87 $("#content").empty();
86 $("#content").append(diff); 88 $("#content").append(diff);
87 $("#content").append(markupDiv); 89 $("#content").append(markupDiv);
88 $(this).addClass('diffed'); 90 $(this).addClass('diffed');
89 } else { 91 } else {
90 $('#local-changes').find('.changeset').removeClass('selected'); 92 $('#recent-changes').find('.changeset').removeClass('selected');
91 $('#local-changes').find('.changeset').removeClass('diffed'); 93 $('#recent-changes').find('.changeset').removeClass('diffed');
92 $(this).addClass('selected'); 94 $(this).addClass('selected');
93 $("#content").empty(); 95 $("#content").empty();
94 $("#content").append(App.createParts(changeset.content)); 96 $("#content").append(App.createParts(changeset.content));
95 } 97 }
96 } 98 }
97 ); 99 );
98 $('#local-changes').find('.changeset').removeClass('selected'); 100 $('#recent-changes').find('.changeset').removeClass('selected');
99 $('#local-changes').find('.changeset').removeClass('diffed'); 101 $('#recent-changes').find('.changeset').removeClass('diffed');
100 $('#local-changes').find('h1').after(changesetElem); 102 $('#recent-changes').find('h1').after(changesetElem);
101 if (doFadeIn) 103 if (isUserChange) {
102 $('#local-changes').fadeIn(); 104 $('#recent-changes').fadeIn();
105 var maybeNewChangeset = App.latestRemoteChangeset + 1;
106 var jsonData = JSON.stringify({date: changeset.date.toString(),
107 content: changeset.content});
108 jQuery.ajax({url: '/' + maybeNewChangeset,
109 type: 'PUT',
110 success: function() {
111 // TODO: Because we're running asynchronously,
112 // weird things can happen here, e.g. if the user
113 // makes another edit while the XHR is being
114 // processed.
115 App.latestRemoteChangeset = maybeNewChangeset;
116 },
117 error: function(xhr, textStatus, errorThrown) {
118 // TODO: Do something here.
119 },
120 dataType: 'text',
121 processData: false,
122 contentType: 'application/json',
123 data: jsonData});
124 }
103 }; 125 };
104 126
105 App.enterEditMode = function enterEditMode(parts, level, cursorPos) { 127 App.enterEditMode = function enterEditMode(parts, level, cursorPos) {
106 if (App.editingElement) { 128 if (App.editingElement) {
107 $(App.editingElement).blur(); 129 $(App.editingElement).blur();
119 function exitEditMode() { 141 function exitEditMode() {
120 var markup = $(this).attr("value"); 142 var markup = $(this).attr("value");
121 $(this).replaceWith(App.createParts(markup)); 143 $(this).replaceWith(App.createParts(markup));
122 if (markup != originalMarkup) { 144 if (markup != originalMarkup) {
123 App.saveChanges(true); 145 App.saveChanges(true);
124 $('#login').fadeIn();
125 } 146 }
126 } 147 }
127 148
128 function editSiblings(aEvt) { 149 function editSiblings(aEvt) {
129 if (!aEvt.shiftKey || level == 0) 150 if (!aEvt.shiftKey || level == 0)
191 }, 212 },
192 linkFormat: '' 213 linkFormat: ''
193 }); 214 });
194 215
195 function onStatus(status) { 216 function onStatus(status) {
217 App.latestRemoteChangeset = status.max_changeset;
196 jQuery.get("/" + status.max_changeset, 218 jQuery.get("/" + status.max_changeset,
197 {}, 219 {},
198 function(obj) { 220 function(obj) {
199 var text = obj.content; 221 var text = obj.content;
200 $("#content").append(App.createParts(text)); 222 $("#content").append(App.createParts(text));