blob: 91fe02e071ed50afaf6bb08d82376ef9c4af31c5 [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"});
15 },
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080016
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080017 run: function () {
18 console.log ("RUN page: " + PAGE);
19 $("#loader").fadeIn (500);
20 $("#error").addClass ("hidden");
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080021
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080022 cmd = {};
23 if (PAGE == "fileList") {
24 cmd = this.info_files (PARAMS.item);
25 }
26 else if (PAGE == "folderHistory") {
27 cmd = this.info_actions ("folder", PARAMS.item);
28 }
29 else if (PAGE == "fileHistory") {
30 cmd = this.info_actions ("file", PARAMS.item);
31 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080032
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080033 if (cmd.request && cmd.callback) {
34 console.log (cmd.request.to_uri ());
35 this.ndn.expressInterest (cmd.request, cmd.callback);
36 }
37 else {
38 $("#loader").fadeOut (500); // ("hidden");
39 $("#content").empty ();
40 if (cmd.error) {
41 $("#error").html (cmd.error);
42 }
43 else {
44 $("#error").html ("Unknown error with " + PAGE);
45 }
46 $("#error").removeClass ("hidden");
47 }
48 },
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080049
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080050 info_files: function(folder) {
51 request = new Name ().add (this.files)./*add (folder_in_question).*/addSegment (PARAMS.offset?PARAMS.offset:0);
52 return { request:request, callback: new FilesClosure (this) };
53 },
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080054
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080055 info_actions: function (type/*"file" or "folder"*/, fileOrFolder /*file or folder name*/) {
56 if (type=="file" && !fileOrFolder) {
57 return { error: "info_actions: fileOrFolder parameter is missing" };
58 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080059
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080060 request = new Name ().add (this.actions).add (type);
61 if (fileOrFolder) {
62 request.add (fileOrFolder);
63 }
64 request.addSegment (PARAMS.offset?PARAMS.offset:0);
65 return { request: request, callback: new HistoryClosure (this) };
66 },
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080067
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080068 cmd_restore_file: function (filename, version, hash, callback/*function (bool <- data received, status <- returned status)*/) {
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080069 request = new Name ().add (this.restore)
70 .add (filename)
71 .addSegment (version)
72 .add (hash);
73 console.log (request.to_uri ());
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080074 this.ndn.expressInterest (request, new CmdRestoreFileClosure (this, callback));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080075 }
76 });
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080077
Alexander Afanasyev4c17b482013-03-02 01:32:35 -080078$.Class ("CmdRestoreFileClosure", {}, {
79 init: function (chronoshare, callback) {
80 this.chronoshare = chronoshare;
81 this.callback = callback;
82 },
83 upcall: function(kind, upcallInfo) {
84 if (kind == Closure.UPCALL_CONTENT) {
85 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
86 this.callback (true, convertedData);
87 }
88 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
89 this.callback (false, "Interest timed out");
90 }
91 else {
92 this.callback (false, "Unknown error happened");
93 }
94 }
95});
96
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080097$.Class ("FilesClosure", {}, {
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080098 init: function (chronoshare) {
99 this.chronoshare = chronoshare;
100 },
101
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800102 upcall: function(kind, upcallInfo) {
103 $("#loader").fadeOut (500); // ("hidden");
104 if (kind == Closure.UPCALL_CONTENT) {
105 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800106 if (PARAMS.debug) {
107 $("#json").text (convertedData);
108 $("#json").removeClass ("hidden");
109 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800110 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800111
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800112 tbody = $("<tbody />", { "id": "file-list-files" });
113
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800114 /// @todo Eventually set title for other pages
115 $("title").text ("ChronoShare - List of files" + (PARAMS.item?" - "+PARAMS.item:""));
116
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800117 // error handling?
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800118 newcontent = $("<div />", { "id": "content" }).append (
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800119 $("<h2 />").append ($(document.createTextNode("List of files ")), $("<green />").text (PARAMS.item)),
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800120 $("<table />", { "class": "item-list" })
121 .append ($("<thead />")
122 .append ($("<tr />")
123 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
124 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800125 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800126 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
127 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800128 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800129 .append ($("<tfoot />")
130 .append ($("<tr />")
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800131 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
132 newcontent.hide ();
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800133
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800134 for (var i = 0; i < data.files.length; i++) {
135 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800136
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800137 row = $("<tr />", { "class": "with-context-menu" } );
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800138 if (i%2) { row.addClass ("odd"); }
139
140 row.bind('mouseenter mouseleave', function() {
141 $(this).toggleClass('highlighted');
142 });
143
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800144 row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename)));
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800145 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800146
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800147 row.append ($("<td />", { "class": "filename border-left" })
148 .text (file.filename)
149 .prepend ($("<img />", { "src": fileExtension(file.filename) })));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800150 row.append ($("<td />", { "class": "version" }).text (file.version));
151 row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum)));
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800152 row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp+"+00:00"))); // convert from UTC
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800153 row.append ($("<td />", { "class": "modified-by border-right"})
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800154 .append ($("<userName />").text (file.owner.userName))
155 .append ($("<seqNo> /").text (file.owner.seqNo)));
156
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800157 tbody = tbody.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800158 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800159
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800160 displayContent (newcontent, data.more, this.base_url ());
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800161
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800162 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800163 $.contextMenu({
164 selector: ".with-context-menu",
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800165 items: {
166 "info": {name: "x", type: "html", html: "<b>File operations</b>"},
167 "sep1": "---------",
168 history: {name: "View file history",
169 icon: "quit", // need a better icon
170 callback: function(key, opt) {
171 openHistoryForItem (opt.$trigger.attr ("filename"));
172 }},
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800173 }
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800174 });
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800175 }
176 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
177 $("#error").html ("Interest timed out");
178 $("#error").removeClass ("hidden");
179 }
180 else {
181 $("#error").html ("Unknown error happened");
182 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800183 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800184 },
185
186 base_url: function () {
187 url = "#fileList"+
188 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
189 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
190 if (PARAMS.item !== undefined) {
191 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
192 }
193 return url;
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800194 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800195});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800196
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800197
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800198$.Class ("HistoryClosure", {}, {
199 init: function (chronoshare) {
200 this.chronoshare = chronoshare;
201 },
202
203 upcall: function(kind, upcallInfo) {
204 $("#loader").fadeOut (500); // ("hidden");
205 if (kind == Closure.UPCALL_CONTENT) {
206 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800207 if (PARAMS.debug) {
208 $("#json").text (convertedData);
209 $("#json").removeClass ("hidden");
210 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800211 data = JSON.parse (convertedData);
212
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800213 tbody = $("<tbody />", { "id": "history-list-actions" });
214
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800215 /// @todo Eventually set title for other pages
216 $("title").text ("ChronoShare - Recent actions" + (PARAMS.item?" - "+PARAMS.item:""));
217
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800218 newcontent = $("<div />", { "id": "content" }).append (
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800219 $("<h2 />").append ($(document.createTextNode("Recent actions ")), $("<green />").text (PARAMS.item)),
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800220 $("<table />", { "class": "item-list" })
221 .append ($("<thead />")
222 .append ($("<tr />")
223 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
224 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800225 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800226 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
227 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800228 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800229 .append ($("<tfoot />")
230 .append ($("<tr />")
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800231 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800232
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800233 for (var i = 0; i < data.actions.length; i++) {
234 action = data.actions[i];
235
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800236 row = $("<tr />");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800237 if (i%2) { row.addClass ("odd"); }
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800238 if (action.action=="DELETE") {
239 row.addClass ("delete");
240 }
241 else {
242 row.addClass ("with-context-menu");
243 row.attr ("file_version", action.version);
244 row.attr ("file_hash", action.update.hash);
245 row.attr ("file_modified_by", action.id.userName);
246 }
247
248 row.attr ("filename", action.filename);
249 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800250
251 row.bind('mouseenter mouseleave', function() {
252 $(this).toggleClass('highlighted');
253 });
254
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800255 row.append ($("<td />", { "class": "filename border-left" })
256 .text (action.filename)
257 .prepend ($("<img />", { "src": fileExtension(action.filename) })));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800258 row.append ($("<td />", { "class": "version" }).text (action.version));
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800259 row.append ($("<td />", { "class": "size" }).text (action.update?SegNumToFileSize (action.update.segNum):""));
260 row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp+"+00:00"))); // conversion from UTC timezone (we store action time in UTC)
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800261 row.append ($("<td />", { "class": "modified-by border-right" })
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800262 .append ($("<userName />").text (action.id.userName))
263 .append ($("<seqNo> /").text (action.id.seqNo)));
264
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800265 tbody = tbody.append (row);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800266 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800267
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800268 displayContent (newcontent, data.more, this.base_url (PAGE));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800269
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800270 $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup
271 $.contextMenu({
272 selector: ".with-context-menu",
273 items: {
274 "sep1": "---------",
275 restore: {name: "Restore this revision",
276 icon: "cut", // need a better icon
277 callback: function(key, opt) {
278 filename = opt.$trigger.attr ("filename");
279 version = opt.$trigger.attr ("file_version");
280 hash = DataUtils.toNumbers (opt.$trigger.attr ("file_hash"));
281 console.log (hash);
282 modified_by = opt.$trigger.attr ("file_modified_by");
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800283
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800284 $("<div />", { "title": "Restore version " + version + "?" })
285 .append ($("<p />")
286 .append ($("<span />", { "class": "ui-icon ui-icon-alert",
287 "style": "float: left; margin: 0 7px 50px 0;" }),
288 $(document.createTextNode ("Are you sure you want restore version ")),
289 $("<green/>").text (version),
290 $(document.createTextNode (" by ")),
291 $("<green/>").text (modified_by)))
292 .dialog ({
293 resizable: true,
294 height:200,
295 width:300,
296 modal: true,
297 buttons: {
298 "Restore": function() {
299 self = $(this);
300 CHRONOSHARE.cmd_restore_file (filename, version, hash, function(didGetData, response) {
301 if (!didGetData || response != "OK") {
302 custom_alert (response);
303 }
304 console.log (response);
305 self.dialog ("close");
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800306
Alexander Afanasyev4c17b482013-03-02 01:32:35 -0800307 $.timer (function() {CHRONOSHARE.run ();}).once (1000);
308 });
309 },
310 Cancel: function() {
311 $(this).dialog ("close");
312 }
313 }
314 });
315 // openHistoryForItem (opt.$trigger.attr ("filename"));
316 }},
317 "sep2": "---------",
318 }
319 });
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800320 }
321 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
322 $("#error").html ("Interest timed out");
323 $("#error").removeClass ("hidden");
324 }
325 else {
326 $("#error").html ("Unknown error happened");
327 $("#error").removeClass ("hidden");
328 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800329 },
330
331 base_url: function (page) {
332 url = "#"+page+
333 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
334 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
335 if (PARAMS.item !== undefined) {
336 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
337 }
338 return url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800339 }
340});
341
342
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800343
344