blob: bf8b4a1364de0690b01346638fb60059ff1f677c [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 Afanasyevf63a5142013-02-28 02:21:42 -080096 $("<table />", { "class": "item-list" })
97 .append ($("<thead />")
98 .append ($("<tr />")
99 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
100 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800101 .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size"))
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800102 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
103 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800104 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800105 .append ($("<tfoot />")
106 .append ($("<tr />")
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800107 .append ($("<td />", { "colspan": "5", "class": "border-right border-left" })))));
108 newcontent.hide ();
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800109
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800110 for (var i = 0; i < data.files.length; i++) {
111 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800112
113 row = $("<tr />");
114 if (i%2) { row.addClass ("odd"); }
115
116 row.bind('mouseenter mouseleave', function() {
117 $(this).toggleClass('highlighted');
118 });
119
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800120 row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename)));
121
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800122
123 row.bind('click', function (e) {
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800124 url = new HistoryClosure (null).base_url ("fileHistory")
125 url += "&item=" + encodeURIComponent (encodeURIComponent ($(this).attr ("filename")));
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800126
127 document.location = url;
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800128 });
129
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800130 row.append ($("<td />", { "class": "filename border-left" }).text (file.filename));
131 row.append ($("<td />", { "class": "version" }).text (file.version));
132 row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum)));
133 row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp)));
134 row.append ($("<td />", { "class": "modified-by border-right"})
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800135 .append ($("<userName />").text (file.owner.userName))
136 .append ($("<seqNo> /").text (file.owner.seqNo)));
137
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800138 tbody = tbody.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800139 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800140
141 // if (!PARAMS.offset || PARAMS.offset==0)
142 // {
143 $("#content").fadeOut ("fast", function () {
144 $(this).replaceWith (newcontent);
145 $("#content").fadeIn ("fast");
146 });
147
148 self = this; // small "cheat"
149 $("#content-nav").fadeOut ("fast", function () {
150 $("#content-nav a").hide ();
151
152 if (PARAMS.offset !== undefined || data.more !== undefined) {
153 $("#content-nav").fadeIn ("fast");
154
155 if (data.more !== undefined) {
156 $("#get-more").show ();
157
158 $("#get-more").unbind ('click').click (function () {
159 url = self.base_url ();
160 url += "&offset="+data.more;
161
162 document.location = url;
163 });
164 }
165 if (PARAMS.offset > 0) {
166 $("#get-less").show ();
167
168 $("#get-less").unbind ('click').click (function () {
169 url = self.base_url ();
170 if (PARAMS.offset > 1) {
171 url += "&offset="+(PARAMS.offset - 1);
172 }
173
174 document.location = url;
175 });
176 }
177 }
178 });
179 // }
180 // else {
181 // tbody.children ().each (function (index, row) {
182 // $("#history-list-actions").append (row);
183 // });
184 // }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800185 }
186 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
187 $("#error").html ("Interest timed out");
188 $("#error").removeClass ("hidden");
189 }
190 else {
191 $("#error").html ("Unknown error happened");
192 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800193 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800194 },
195
196 base_url: function () {
197 url = "#fileList"+
198 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
199 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
200 if (PARAMS.item !== undefined) {
201 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
202 }
203 return url;
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800204 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800205});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800206
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800207
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800208$.Class ("HistoryClosure", {}, {
209 init: function (chronoshare) {
210 this.chronoshare = chronoshare;
211 },
212
213 upcall: function(kind, upcallInfo) {
214 $("#loader").fadeOut (500); // ("hidden");
215 if (kind == Closure.UPCALL_CONTENT) {
216 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800217 if (PARAMS.debug) {
218 $("#json").text (convertedData);
219 $("#json").removeClass ("hidden");
220 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800221 data = JSON.parse (convertedData);
222
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800223 tbody = $("<tbody />", { "id": "history-list-actions" });
224
225 newcontent = $("<div />", { "id": "content" }).append (
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800226 $("<table />", { "class": "item-list" })
227 .append ($("<thead />")
228 .append ($("<tr />")
229 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
230 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
231 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
232 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800233 .append (tbody)
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800234 .append ($("<tfoot />")
235 .append ($("<tr />")
236 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
237
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800238 for (var i = 0; i < data.actions.length; i++) {
239 action = data.actions[i];
240
241 row = $("<tr />");
242 if (i%2) { row.addClass ("odd"); }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800243 if (action.action=="DELETE") { row.addClass ("delete"); }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800244
245 row.bind('mouseenter mouseleave', function() {
246 $(this).toggleClass('highlighted');
247 });
248
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800249 // row.attr ("filename", );//encodeURIComponent(encodeURIComponent(action.filename)));
250 // row.attr ("version",
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800251
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800252 // row.bind('click', function (e) {
253 // url = "#fileHistory";
254 // url += "&item=" + $(this).attr ("filename");
255 // pos = URIPARAMS.indexOf ("&");
256 // if (pos >= 0) {
257 // url += URIPARAMS.substring (pos)
258 // }
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800259
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800260 // document.location = url;
261 // });
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800262
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800263 row.append ($("<td />", { "class": "filename border-left" }).text (action.filename));
264 row.append ($("<td />", { "class": "version" }).text (action.version));
265 row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp)));
266 row.append ($("<td />", { "class": "modified-by border-right" })
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800267 .append ($("<userName />").text (action.id.userName))
268 .append ($("<seqNo> /").text (action.id.seqNo)));
269
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800270 tbody = tbody.append (row);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800271 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800272
273 // if (!PARAMS.offset || PARAMS.offset==0)
274 // {
275 $("#content").fadeOut ("fast", function () {
276 $(this).replaceWith (newcontent);
277 $("#content").fadeIn ("fast");
278 });
279
280 self = this; // small "cheat"
281 $("#content-nav").fadeOut ("fast", function () {
282 $("#content-nav a").hide ();
283
284 if (PARAMS.offset !== undefined || data.more !== undefined) {
285 $("#content-nav").fadeIn ("fast");
286
287 if (data.more !== undefined) {
288 $("#get-more").show ();
289
290 $("#get-more").unbind ('click').click (function () {
291 url = self.base_url (PAGE);
292 url += "&offset="+data.more;
293
294 document.location = url;
295 });
296 }
297 if (PARAMS.offset > 0) {
298 $("#get-less").show ();
299
300 $("#get-less").unbind ('click').click (function () {
301 url = self.base_url (PAGE);
302 if (PARAMS.offset > 1) {
303 url += "&offset="+(PARAMS.offset - 1);
304 }
305
306 document.location = url;
307 });
308 }
309 }
310 });
311 // }
312 // else {
313 // tbody.children ().each (function (index, row) {
314 // $("#history-list-actions").append (row);
315 // });
316 // }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800317 }
318 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
319 $("#error").html ("Interest timed out");
320 $("#error").removeClass ("hidden");
321 }
322 else {
323 $("#error").html ("Unknown error happened");
324 $("#error").removeClass ("hidden");
325 }
Alexander Afanasyev3c95c852013-03-01 18:58:50 -0800326 },
327
328 base_url: function (page) {
329 url = "#"+page+
330 "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) +
331 "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
332 if (PARAMS.item !== undefined) {
333 url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item));
334 }
335 return url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800336 }
337});
338
339
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800340
341