blob: b4c3d731626ac68cb13234e1eda1f4bebea0f0bc [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 Afanasyev3c95c852013-03-01 18:58:50 -080068 cmd_restore_file: function (filename, version, hash) {
69 request = new Name ().add (this.restore)
70 .add (filename)
71 .addSegment (version)
72 .add (hash);
73 console.log (request.to_uri ());
74 }
75 });
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080076
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080077$.Class ("FilesClosure", {}, {
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080078 init: function (chronoshare) {
79 this.chronoshare = chronoshare;
80 },
81
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080082 upcall: function(kind, upcallInfo) {
83 $("#loader").fadeOut (500); // ("hidden");
84 if (kind == Closure.UPCALL_CONTENT) {
85 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -080086 if (PARAMS.debug) {
87 $("#json").text (convertedData);
88 $("#json").removeClass ("hidden");
89 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080090 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080091
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080092 tbody = $("<tbody />", { "id": "file-list-files" });
93
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080094 // error handling?
Alexander Afanasyev3c95c852013-03-01 18:58:50 -080095 newcontent = $("<div />", { "id": "content" }).append (
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -080096 $("<h2 />").append ($(document.createTextNode("List of files ")), $("<green />").text (PARAMS.item)),
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080097 $("<table />", { "class": "item-list" })
98 .append ($("<thead />")
99 .append ($("<tr />")
100 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
101 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800102 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800103 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
104 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800105 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800106 .append ($("<tfoot />")
107 .append ($("<tr />")
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800108 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
109 newcontent.hide ();
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800110
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800111 for (var i = 0; i < data.files.length; i++) {
112 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800113
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800114 row = $("<tr />", { "class": "with-context-menu" } );
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800115 if (i%2) { row.addClass ("odd"); }
116
117 row.bind('mouseenter mouseleave', function() {
118 $(this).toggleClass('highlighted');
119 });
120
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800121 row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename)));
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800122 row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) });
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800123
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800124 row.append ($("<td />", { "class": "filename border-left" })
125 .text (file.filename)
126 .prepend ($("<img />", { "src": fileExtension(file.filename) })));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800127 row.append ($("<td />", { "class": "version" }).text (file.version));
128 row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum)));
129 row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp)));
130 row.append ($("<td />", { "class": "modified-by border-right"})
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800131 .append ($("<userName />").text (file.owner.userName))
132 .append ($("<seqNo> /").text (file.owner.seqNo)));
133
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800134 tbody = tbody.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800135 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800136
137 // if (!PARAMS.offset || PARAMS.offset==0)
138 // {
139 $("#content").fadeOut ("fast", function () {
140 $(this).replaceWith (newcontent);
141 $("#content").fadeIn ("fast");
142 });
143
144 self = this; // small "cheat"
145 $("#content-nav").fadeOut ("fast", function () {
146 $("#content-nav a").hide ();
147
148 if (PARAMS.offset !== undefined || data.more !== undefined) {
149 $("#content-nav").fadeIn ("fast");
150
151 if (data.more !== undefined) {
152 $("#get-more").show ();
153
154 $("#get-more").unbind ('click').click (function () {
155 url = self.base_url ();
156 url += "&offset="+data.more;
157
158 document.location = url;
159 });
160 }
161 if (PARAMS.offset > 0) {
162 $("#get-less").show ();
163
164 $("#get-less").unbind ('click').click (function () {
165 url = self.base_url ();
166 if (PARAMS.offset > 1) {
167 url += "&offset="+(PARAMS.offset - 1);
168 }
169
170 document.location = url;
171 });
172 }
173 }
174 });
175 // }
176 // else {
177 // tbody.children ().each (function (index, row) {
178 // $("#history-list-actions").append (row);
179 // });
180 // }
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800181
182 $.contextMenu({
183 selector: ".with-context-menu",
184 events: {
185 show: function (opt) {
186 console.log(opt.$trigger.attr ("filename"));
187 opt.items.info.name = opt.$trigger.attr ("filename");
188 console.log (opt.items.info.name);
189 },
190 },
191 items: {
192 "info": {name: "x", type: "html", html: "<b>File operations</b>"},
193 "sep1": "---------",
194 history: {name: "View file history",
195 icon: "quit", // need a better icon
196 callback: function(key, opt) {
197 openHistoryForItem (opt.$trigger.attr ("filename"));
198 }},
199 // bar: {name: "Bar", callback: function(key, opt){ alert("Bar!") }}
200 }
201 // there's more, have a look at the demos and docs...
202 });
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800203 }
204 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
205 $("#error").html ("Interest timed out");
206 $("#error").removeClass ("hidden");
207 }
208 else {
209 $("#error").html ("Unknown error happened");
210 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800211 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800212 },
213
214 base_url: function () {
215 url = "#fileList"+
216 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
217 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
218 if (PARAMS.item !== undefined) {
219 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
220 }
221 return url;
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800222 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800223});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800224
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800225
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800226$.Class ("HistoryClosure", {}, {
227 init: function (chronoshare) {
228 this.chronoshare = chronoshare;
229 },
230
231 upcall: function(kind, upcallInfo) {
232 $("#loader").fadeOut (500); // ("hidden");
233 if (kind == Closure.UPCALL_CONTENT) {
234 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800235 if (PARAMS.debug) {
236 $("#json").text (convertedData);
237 $("#json").removeClass ("hidden");
238 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800239 data = JSON.parse (convertedData);
240
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800241 tbody = $("<tbody />", { "id": "history-list-actions" });
242
243 newcontent = $("<div />", { "id": "content" }).append (
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800244 $("<h2 />").append ($(document.createTextNode("Recent actions ")), $("<green />").text (PARAMS.item)),
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800245 $("<table />", { "class": "item-list" })
246 .append ($("<thead />")
247 .append ($("<tr />")
248 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
249 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
250 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
251 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800252 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800253 .append ($("<tfoot />")
254 .append ($("<tr />")
255 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
256
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800257 for (var i = 0; i < data.actions.length; i++) {
258 action = data.actions[i];
259
260 row = $("<tr />");
261 if (i%2) { row.addClass ("odd"); }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800262 if (action.action=="DELETE") { row.addClass ("delete"); }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800263
264 row.bind('mouseenter mouseleave', function() {
265 $(this).toggleClass('highlighted');
266 });
267
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800268 row.attr ("filename", action.filename);
269 row.attr ("file_version", action.version);
270 row.attr ("file_hash", action.hash);
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800271
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800272 // row.bind('click', function (e) {
273 // url = "#fileHistory";
274 // url += "&item=" + $(this).attr ("filename");
275 // pos = URIPARAMS.indexOf ("&");
276 // if (pos >= 0) {
277 // url += URIPARAMS.substring (pos)
278 // }
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800279
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800280 // document.location = url;
281 // });
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800282
Alexander Afanasyevf7c7cde2013-03-02 00:01:32 -0800283 row.append ($("<td />", { "class": "filename border-left" })
284 .text (action.filename)
285 .prepend ($("<img />", { "src": fileExtension(action.filename) })));
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800286 row.append ($("<td />", { "class": "version" }).text (action.version));
287 row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp)));
288 row.append ($("<td />", { "class": "modified-by border-right" })
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800289 .append ($("<userName />").text (action.id.userName))
290 .append ($("<seqNo> /").text (action.id.seqNo)));
291
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800292 tbody = tbody.append (row);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800293 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800294
295 // if (!PARAMS.offset || PARAMS.offset==0)
296 // {
297 $("#content").fadeOut ("fast", function () {
298 $(this).replaceWith (newcontent);
299 $("#content").fadeIn ("fast");
300 });
301
302 self = this; // small "cheat"
303 $("#content-nav").fadeOut ("fast", function () {
304 $("#content-nav a").hide ();
305
306 if (PARAMS.offset !== undefined || data.more !== undefined) {
307 $("#content-nav").fadeIn ("fast");
308
309 if (data.more !== undefined) {
310 $("#get-more").show ();
311
312 $("#get-more").unbind ('click').click (function () {
313 url = self.base_url (PAGE);
314 url += "&offset="+data.more;
315
316 document.location = url;
317 });
318 }
319 if (PARAMS.offset > 0) {
320 $("#get-less").show ();
321
322 $("#get-less").unbind ('click').click (function () {
323 url = self.base_url (PAGE);
324 if (PARAMS.offset > 1) {
325 url += "&offset="+(PARAMS.offset - 1);
326 }
327
328 document.location = url;
329 });
330 }
331 }
332 });
333 // }
334 // else {
335 // tbody.children ().each (function (index, row) {
336 // $("#history-list-actions").append (row);
337 // });
338 // }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800339 }
340 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
341 $("#error").html ("Interest timed out");
342 $("#error").removeClass ("hidden");
343 }
344 else {
345 $("#error").html ("Unknown error happened");
346 $("#error").removeClass ("hidden");
347 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800348 },
349
350 base_url: function (page) {
351 url = "#"+page+
352 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
353 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
354 if (PARAMS.item !== undefined) {
355 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
356 }
357 return url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800358 }
359});
360
361
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800362
363