blob: 2e4abd43ee39ab4aa52588bb1ece7d649af06610 [file] [log] [blame]
Alexander Afanasyev3c95c852013-03-01 18:58:50 -08001$.Class ("ChronoShare", { },
2 {
3 init: function (username, foldername) {
4 $("#folder-name").text (foldername);
5 $("#user-name").text (username);
Alexander Afanasyev46bd8062013-02-27 23:59:15 -08006
Alexander Afanasyev3c95c852013-03-01 18:58:50 -08007 this.username = new Name (username);
8 this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder");
Alexander Afanasyev46bd8062013-02-27 23:59:15 -08009
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080010 this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions");
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080011
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080012 this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file");
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080013
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080014 this.ndn = new NDN ({host:"127.0.0.1"});
Alexander Afanasyev1663a412013-03-02 13:52:00 -080015 this.ndn.verify = false; //disable content verification, works WAAAAY faster
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080016 },
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080017
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080018 run: function () {
19 console.log ("RUN page: " + PAGE);
20 $("#loader").fadeIn (500);
21 $("#error").addClass ("hidden");
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080022
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080023 cmd = {};
24 if (PAGE == "fileList") {
25 cmd = this.info_files (PARAMS.item);
26 }
27 else if (PAGE == "folderHistory") {
28 cmd = this.info_actions ("folder", PARAMS.item);
29 }
30 else if (PAGE == "fileHistory") {
31 cmd = this.info_actions ("file", PARAMS.item);
32 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080033
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080034 if (cmd.request && cmd.callback) {
35 console.log (cmd.request.to_uri ());
36 this.ndn.expressInterest (cmd.request, cmd.callback);
37 }
38 else {
39 $("#loader").fadeOut (500); // ("hidden");
40 $("#content").empty ();
41 if (cmd.error) {
42 $("#error").html (cmd.error);
43 }
44 else {
45 $("#error").html ("Unknown error with " + PAGE);
46 }
47 $("#error").removeClass ("hidden");
48 }
49 },
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080050
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080051 info_files: function(folder) {
52 request = new Name ().add (this.files)./*add (folder_in_question).*/addSegment (PARAMS.offset?PARAMS.offset:0);
53 return { request:request, callback: new FilesClosure (this) };
54 },
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080055
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080056 info_actions: function (type/*"file" or "folder"*/, fileOrFolder /*file or folder name*/) {
57 if (type=="file" && !fileOrFolder) {
58 return { error: "info_actions: fileOrFolder parameter is missing" };
59 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080060
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080061 request = new Name ().add (this.actions).add (type);
62 if (fileOrFolder) {
63 request.add (fileOrFolder);
64 }
65 request.addSegment (PARAMS.offset?PARAMS.offset:0);
66 return { request: request, callback: new HistoryClosure (this) };
67 },
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080068
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080069 cmd_restore_file: function (filename, version, hash, callback/*function (bool <- data received, status <- returned status)*/) {
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080070 request = new Name ().add (this.restore)
71 .add (filename)
72 .addSegment (version)
73 .add (hash);
74 console.log (request.to_uri ());
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080075 this.ndn.expressInterest (request, new CmdRestoreFileClosure (this, callback));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080076 }
77 });
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080078
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080079$.Class ("CmdRestoreFileClosure", {}, {
80 init: function (chronoshare, callback) {
81 this.chronoshare = chronoshare;
82 this.callback = callback;
83 },
84 upcall: function(kind, upcallInfo) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -080085 if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080086 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
87 this.callback (true, convertedData);
88 }
89 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
90 this.callback (false, "Interest timed out");
91 }
92 else {
93 this.callback (false, "Unknown error happened");
94 }
95 }
96});
97
Alexander Afanasyev1663a412013-03-02 13:52:00 -080098$.Class ("RestPipelineClosure", {}, {
99 init: function (collectionName, moreName) {
100 this.collectionName = collectionName;
101 this.moreName = moreName;
102 $("#json").empty ();
103
104 this.collection = [];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800105 },
106
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800107 upcall: function(kind, upcallInfo) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800108 if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification
109
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800110 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800111 if (PARAMS.debug) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800112 $("#json").append ($(document.createTextNode(convertedData)));
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800113 $("#json").removeClass ("hidden");
114 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800115 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800116
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800117 this.collection = this.collection.concat (data[this.collectionName]);
118 if (data[this.moreName] !== undefined) {
119 nextSegment = upcallInfo.interest.name.cut (1).addSegment (data[this.moreName]);
120 console.log ("MORE: " +nextSegment.to_uri ());
121 CHRONOSHARE.ndn.expressInterest (nextSegment, this);
122 }
123 else {
124 $("#loader").fadeOut (500); // ("hidden");
125 this.onData (this.collection);
126 }
127 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800128 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800129 $("#loader").fadeOut (500); // ("hidden");
130 this.onTimeout (upcallInfo.interest);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800131 }
132 else {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800133 $("#loader").fadeOut (500); // ("hidden");
134 this.onUnknownError (upcallInfo.interest);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800135 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800136
137 return Closure.RESULT_OK; // make sure we never re-express the interest
138 },
139
140 onData: function(data) {
141 },
142
143 onTimeout: function () {
144 $("#error").html ("Interest timed out");
145 $("#error").removeClass ("hidden");
146 },
147
148 onUnknownError: function () {
149 $("#error").html ("Unknown error happened");
150 $("#error").removeClass ("hidden");
151 }
152});
153
154// $.Class ("FilesClosure", {}, {
155RestPipelineClosure ("FilesClosure", {}, {
156 init: function (chronoshare) {
157 this._super("files", "more");
158 this.chronoshare = chronoshare;
159 },
160
161 onData: function(data) {
162 tbody = $("<tbody />", { "id": "file-list-files" });
163
164 /// @todo Eventually set title for other pages
165 $("title").text ("ChronoShare - List of files" + (PARAMS.item?" - "+PARAMS.item:""));
166
167 // error handling?
168 newcontent = $("<div />", { "id": "content" }).append (
169 $("<h2 />").append ($(document.createTextNode("List of files ")), $("<green />").text (PARAMS.item)),
170 $("<table />", { "class": "item-list" })
171 .append ($("<thead />")
172 .append ($("<tr />")
173 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
174 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
175 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
176 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
177 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
178 .append (tbody)
179 .append ($("<tfoot />")
180 .append ($("<tr />")
181 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
182 newcontent.hide ();
183
184 for (var i = 0; i < data.length; i++) {
185 file = data[i];
186
187 row = $("<tr />", { "class": "with-context-menu" } );
188 if (i%2) { row.addClass ("odd"); }
189
190 row.bind('mouseenter mouseleave', function() {
191 $(this).toggleClass('highlighted');
192 });
193
194 row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename)));
195 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
196
197 row.append ($("<td />", { "class": "filename border-left" })
198 .text (file.filename)
199 .prepend ($("<img />", { "src": fileExtension(file.filename) })));
200 row.append ($("<td />", { "class": "version" }).text (file.version));
201 row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum)));
202 row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp+"+00:00"))); // convert from UTC
203 row.append ($("<td />", { "class": "modified-by border-right"})
204 .append ($("<userName />").text (file.owner.userName))
205 .append ($("<seqNo> /").text (file.owner.seqNo)));
206
207 tbody = tbody.append (row);
208 }
209
210 $("#content").fadeOut ("fast", function () {
211 $(this).replaceWith (newcontent);
212 $("#content").fadeIn ("fast");
213 });
214
215 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
216 $.contextMenu({
217 selector: ".with-context-menu",
218 items: {
219 "info": {name: "x", type: "html", html: "<b>File operations</b>"},
220 "sep1": "---------",
221 history: {name: "View file history",
222 icon: "quit", // need a better icon
223 callback: function(key, opt) {
224 openHistoryForItem (opt.$trigger.attr ("filename"));
225 }},
226 }
227 });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800228 },
229
230 base_url: function () {
231 url = "#fileList"+
232 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
233 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
234 if (PARAMS.item !== undefined) {
235 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
236 }
237 return url;
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800238 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800239});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800240
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800241
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800242RestPipelineClosure ("HistoryClosure", {}, {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800243 init: function (chronoshare) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800244 this._super("actions", "more");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800245 this.chronoshare = chronoshare;
246 },
247
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800248 onData: function(data) {
249 tbody = $("<tbody />", { "id": "history-list-actions" });
250
251 /// @todo Eventually set title for other pages
252 $("title").text ("ChronoShare - Recent actions" + (PARAMS.item?" - "+PARAMS.item:""));
253
254 newcontent = $("<div />", { "id": "content" }).append (
255 $("<h2 />").append ($(document.createTextNode("Recent actions ")), $("<green />").text (PARAMS.item)),
256 $("<table />", { "class": "item-list" })
257 .append ($("<thead />")
258 .append ($("<tr />")
259 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
260 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
261 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
262 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
263 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
264 .append (tbody)
265 .append ($("<tfoot />")
266 .append ($("<tr />")
267 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
268
269 for (var i = 0; i < data.length; i++) {
270 action = data[i];
271
272 row = $("<tr />");
273 if (i%2) { row.addClass ("odd"); }
274 if (action.action=="DELETE") {
275 row.addClass ("delete");
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800276 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800277 else {
278 row.addClass ("with-context-menu");
279 row.attr ("file_version", action.version);
280 row.attr ("file_hash", action.update.hash);
281 row.attr ("file_modified_by", action.id.userName);
282 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800283
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800284 row.attr ("filename", action.filename);
285 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800286
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800287 row.bind('mouseenter mouseleave', function() {
288 $(this).toggleClass('highlighted');
289 });
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800290
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800291 row.append ($("<td />", { "class": "filename border-left" })
292 .text (action.filename)
293 .prepend ($("<img />", { "src": fileExtension(action.filename) })));
294 row.append ($("<td />", { "class": "version" }).text (action.version));
295 row.append ($("<td />", { "class": "size" }).text (action.update?SegNumToFileSize (action.update.segNum):""));
296 row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp+"+00:00"))); // conversion from UTC timezone (we store action time in UTC)
297 row.append ($("<td />", { "class": "modified-by border-right" })
298 .append ($("<userName />").text (action.id.userName))
299 .append ($("<seqNo> /").text (action.id.seqNo)));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800300
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800301 tbody = tbody.append (row);
302 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800303
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800304 $("#content").fadeOut ("fast", function () {
305 $(this).replaceWith (newcontent);
306 $("#content").fadeIn ("fast");
307 });
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800308
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800309 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
310 $.contextMenu({
311 selector: ".with-context-menu",
312 items: {
313 "sep1": "---------",
314 restore: {name: "Restore this revision",
315 icon: "cut", // need a better icon
316 callback: function(key, opt) {
317 filename = opt.$trigger.attr ("filename");
318 version = opt.$trigger.attr ("file_version");
319 hash = DataUtils.toNumbers (opt.$trigger.attr ("file_hash"));
320 console.log (hash);
321 modified_by = opt.$trigger.attr ("file_modified_by");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800322
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800323 $("<div />", { "title": "Restore version " + version + "?" })
324 .append ($("<p />")
325 .append ($("<span />", { "class": "ui-icon ui-icon-alert",
326 "style": "float: left; margin: 0 7px 50px 0;" }),
327 $(document.createTextNode ("Are you sure you want restore version ")),
328 $("<green/>").text (version),
329 $(document.createTextNode (" by ")),
330 $("<green/>").text (modified_by)))
331 .dialog ({
332 resizable: true,
333 height:200,
334 width:300,
335 modal: true,
336 buttons: {
337 "Restore": function() {
338 self = $(this);
339 CHRONOSHARE.cmd_restore_file (filename, version, hash, function(didGetData, response) {
340 if (!didGetData || response != "OK") {
341 custom_alert (response);
342 }
343 console.log (response);
344 self.dialog ("close");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800345
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800346 $.timer (function() {CHRONOSHARE.run ();}).once (1000);
347 });
348 },
349 Cancel: function() {
350 $(this).dialog ("close");
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800351 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800352 }
353 });
354 // openHistoryForItem (opt.$trigger.attr ("filename"));
355 }},
356 "sep2": "---------",
357 }
358 });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800359 },
360
361 base_url: function (page) {
362 url = "#"+page+
363 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
364 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
365 if (PARAMS.item !== undefined) {
366 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
367 }
368 return url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800369 }
370});
371
372
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800373
374