blob: ea6a6b872ed44ace2dbc566aa1570ea50ed7f530 [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 Afanasyev10babd22013-03-04 13:53:36 -0800105 this.counter = 0;
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800106 },
107
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800108 upcall: function(kind, upcallInfo) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800109 if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification
110
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800111 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800112 if (PARAMS.debug) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800113 $("#json").append ($(document.createTextNode(convertedData)));
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800114 $("#json").removeClass ("hidden");
115 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800116 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800117
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800118 this.collection = this.collection.concat (data[this.collectionName]);
119 if (data[this.moreName] !== undefined) {
120 nextSegment = upcallInfo.interest.name.cut (1).addSegment (data[this.moreName]);
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800121 this.counter ++;
122
123 if (this.counter < 5) {
124 console.log ("MORE: " +nextSegment.to_uri ());
125 CHRONOSHARE.ndn.expressInterest (nextSegment, this);
126 }
127 else {
128 $("#loader").fadeOut (500); // ("hidden");
129 this.onData (this.collection, data[this.moreName]);
130 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800131 }
132 else {
133 $("#loader").fadeOut (500); // ("hidden");
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800134 this.onData (this.collection, undefined);
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800135 }
136 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800137 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800138 $("#loader").fadeOut (500); // ("hidden");
139 this.onTimeout (upcallInfo.interest);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800140 }
141 else {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800142 $("#loader").fadeOut (500); // ("hidden");
143 this.onUnknownError (upcallInfo.interest);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800144 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800145
146 return Closure.RESULT_OK; // make sure we never re-express the interest
147 },
148
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800149 onData: function(data, more) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800150 },
151
152 onTimeout: function () {
153 $("#error").html ("Interest timed out");
154 $("#error").removeClass ("hidden");
155 },
156
157 onUnknownError: function () {
158 $("#error").html ("Unknown error happened");
159 $("#error").removeClass ("hidden");
160 }
161});
162
163// $.Class ("FilesClosure", {}, {
164RestPipelineClosure ("FilesClosure", {}, {
165 init: function (chronoshare) {
166 this._super("files", "more");
167 this.chronoshare = chronoshare;
168 },
169
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800170 onData: function(data, more) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800171 tbody = $("<tbody />", { "id": "file-list-files" });
172
173 /// @todo Eventually set title for other pages
174 $("title").text ("ChronoShare - List of files" + (PARAMS.item?" - "+PARAMS.item:""));
175
176 // error handling?
177 newcontent = $("<div />", { "id": "content" }).append (
178 $("<h2 />").append ($(document.createTextNode("List of files ")), $("<green />").text (PARAMS.item)),
179 $("<table />", { "class": "item-list" })
180 .append ($("<thead />")
181 .append ($("<tr />")
182 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
183 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
184 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
185 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
186 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
187 .append (tbody)
188 .append ($("<tfoot />")
189 .append ($("<tr />")
190 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
191 newcontent.hide ();
192
193 for (var i = 0; i < data.length; i++) {
194 file = data[i];
195
196 row = $("<tr />", { "class": "with-context-menu" } );
197 if (i%2) { row.addClass ("odd"); }
198
199 row.bind('mouseenter mouseleave', function() {
200 $(this).toggleClass('highlighted');
201 });
202
203 row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename)));
204 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
205
206 row.append ($("<td />", { "class": "filename border-left" })
207 .text (file.filename)
208 .prepend ($("<img />", { "src": fileExtension(file.filename) })));
209 row.append ($("<td />", { "class": "version" }).text (file.version));
210 row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum)));
211 row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp+"+00:00"))); // convert from UTC
212 row.append ($("<td />", { "class": "modified-by border-right"})
213 .append ($("<userName />").text (file.owner.userName))
214 .append ($("<seqNo> /").text (file.owner.seqNo)));
215
216 tbody = tbody.append (row);
217 }
218
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800219 displayContent (newcontent, more, this.base_url ());
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800220
221 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
222 $.contextMenu({
223 selector: ".with-context-menu",
224 items: {
225 "info": {name: "x", type: "html", html: "<b>File operations</b>"},
226 "sep1": "---------",
227 history: {name: "View file history",
228 icon: "quit", // need a better icon
229 callback: function(key, opt) {
230 openHistoryForItem (opt.$trigger.attr ("filename"));
231 }},
232 }
233 });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800234 },
235
236 base_url: function () {
237 url = "#fileList"+
238 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
239 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
240 if (PARAMS.item !== undefined) {
241 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
242 }
243 return url;
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800244 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800245});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800246
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800247
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800248RestPipelineClosure ("HistoryClosure", {}, {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800249 init: function (chronoshare) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800250 this._super("actions", "more");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800251 this.chronoshare = chronoshare;
252 },
253
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800254 onData: function(data, more) {
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800255 tbody = $("<tbody />", { "id": "history-list-actions" });
256
257 /// @todo Eventually set title for other pages
258 $("title").text ("ChronoShare - Recent actions" + (PARAMS.item?" - "+PARAMS.item:""));
259
260 newcontent = $("<div />", { "id": "content" }).append (
261 $("<h2 />").append ($(document.createTextNode("Recent actions ")), $("<green />").text (PARAMS.item)),
262 $("<table />", { "class": "item-list" })
263 .append ($("<thead />")
264 .append ($("<tr />")
265 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
266 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
267 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
268 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
269 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
270 .append (tbody)
271 .append ($("<tfoot />")
272 .append ($("<tr />")
273 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
274
275 for (var i = 0; i < data.length; i++) {
276 action = data[i];
277
278 row = $("<tr />");
279 if (i%2) { row.addClass ("odd"); }
280 if (action.action=="DELETE") {
281 row.addClass ("delete");
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800282 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800283 else {
284 row.addClass ("with-context-menu");
285 row.attr ("file_version", action.version);
286 row.attr ("file_hash", action.update.hash);
287 row.attr ("file_modified_by", action.id.userName);
288 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800289
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800290 row.attr ("filename", action.filename);
291 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800292
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800293 row.bind('mouseenter mouseleave', function() {
294 $(this).toggleClass('highlighted');
295 });
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800296
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800297 row.append ($("<td />", { "class": "filename border-left" })
298 .text (action.filename)
299 .prepend ($("<img />", { "src": fileExtension(action.filename) })));
300 row.append ($("<td />", { "class": "version" }).text (action.version));
301 row.append ($("<td />", { "class": "size" }).text (action.update?SegNumToFileSize (action.update.segNum):""));
302 row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp+"+00:00"))); // conversion from UTC timezone (we store action time in UTC)
303 row.append ($("<td />", { "class": "modified-by border-right" })
304 .append ($("<userName />").text (action.id.userName))
305 .append ($("<seqNo> /").text (action.id.seqNo)));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800306
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800307 tbody = tbody.append (row);
308 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800309
Alexander Afanasyev10babd22013-03-04 13:53:36 -0800310 displayContent (newcontent, more, this.base_url (PAGE));
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800311
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800312 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
313 $.contextMenu({
314 selector: ".with-context-menu",
315 items: {
316 "sep1": "---------",
317 restore: {name: "Restore this revision",
318 icon: "cut", // need a better icon
319 callback: function(key, opt) {
320 filename = opt.$trigger.attr ("filename");
321 version = opt.$trigger.attr ("file_version");
322 hash = DataUtils.toNumbers (opt.$trigger.attr ("file_hash"));
323 console.log (hash);
324 modified_by = opt.$trigger.attr ("file_modified_by");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800325
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800326 $("<div />", { "title": "Restore version " + version + "?" })
327 .append ($("<p />")
328 .append ($("<span />", { "class": "ui-icon ui-icon-alert",
329 "style": "float: left; margin: 0 7px 50px 0;" }),
330 $(document.createTextNode ("Are you sure you want restore version ")),
331 $("<green/>").text (version),
332 $(document.createTextNode (" by ")),
333 $("<green/>").text (modified_by)))
334 .dialog ({
335 resizable: true,
336 height:200,
337 width:300,
338 modal: true,
339 buttons: {
340 "Restore": function() {
341 self = $(this);
342 CHRONOSHARE.cmd_restore_file (filename, version, hash, function(didGetData, response) {
343 if (!didGetData || response != "OK") {
344 custom_alert (response);
345 }
346 console.log (response);
347 self.dialog ("close");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800348
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800349 $.timer (function() {CHRONOSHARE.run ();}).once (1000);
350 });
351 },
352 Cancel: function() {
353 $(this).dialog ("close");
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800354 }
Alexander Afanasyev1663a412013-03-02 13:52:00 -0800355 }
356 });
357 // openHistoryForItem (opt.$trigger.attr ("filename"));
358 }},
359 "sep2": "---------",
360 }
361 });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800362 },
363
364 base_url: function (page) {
365 url = "#"+page+
366 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
367 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
368 if (PARAMS.item !== undefined) {
369 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
370 }
371 return url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800372 }
373});
374
375
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800376
377