blob: ffd972409c2cbf205436bbdf9f4a4e12a3371d0a [file] [log] [blame]
Alexander Afanasyevf63a5142013-02-28 02:21:42 -08001var CHRONOSHARE;
Alexander Afanasyev46bd8062013-02-27 23:59:15 -08002
Alexander Afanasyevf63a5142013-02-28 02:21:42 -08003var PAGE = "folderHistory";
4var PARAMS = [ ];
5var URIPARAMS = "";
Alexander Afanasyev46bd8062013-02-27 23:59:15 -08006
Alexander Afanasyevf63a5142013-02-28 02:21:42 -08007function nav_anchor (aurl) {
8 aurl = aurl.split('#');
9 if (aurl[1])
10 {
11 aurl_split = aurl[1].split ('&');
12 page = aurl_split[0];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080013
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080014 vars = [ ];
15 for (var i = 1; i < aurl_split.length; i++)
16 {
17 hash = aurl_split[i].split('=');
18 vars.push(hash[0]);
19 // there is strange double-encoding problem...
20 vars[hash[0]] = decodeURIComponent (decodeURIComponent (hash[1]));
21 }
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080022
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080023 if (page != PAGE)
24 {
25 PAGE = page;
26 PARAMS = vars;
27 URIPARAMS = aurl[1];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080028
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080029 if (CHRONOSHARE) {
30 CHRONOSHARE.run ();
31 }
32 }
33 else if (aurl != URIPARAMS)
34 {
35 PARAMS = vars;
36 URIPARAMS = aurl[1];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080037
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080038 if (CHRONOSHARE) {
39 CHRONOSHARE.run ();
40 }
41 }
42 }
43}
44
45$(document).ready (function () {
46 nav_anchor (window.location.href);
47
48 if (!PARAMS.user || !PARAMS.folder)
49 {
50 $("#error").html ("user and folder must be be specified in the URL");
51 $("#error").removeClass ("hidden");
52 return;
53 }
54 else {
55 // update in-page URLs
56 $(".needs-get-url").each (function (index,element) {
57 this.href += "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user))
58 + "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder));
59 });
60 $(".needs-get-url").removeClass ("needs-get-url");
61 }
62
63 CHRONOSHARE = new ChronoShare (PARAMS.user, PARAMS.folder);
64 CHRONOSHARE.run ();
65
Alexander Afanasyevf63a5142013-02-28 02:21:42 -080066 $(window).on('hashchange', function() {
67 nav_anchor (window.location.href);
68 });
69});
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080070
71/**
72 * @brief Convert binary data represented as non-escaped hex string to Uint8Array
73 * @param str String like ba0cb43e4b9639c114a0487d5faa7c70452533963fc8beb37d1b67c09a48a21d
74 *
75 * Note that if string length is odd, null will be returned
76 */
77StringHashToUint8Array = function (str) {
78 if (str.length % 2 != 0) {
79 return null;
80 }
81
82 var buf = new Uint8Array (str.length / 2);
83
84 for (var i = 0; i < str.length; i+=2) {
85 value = parseInt (str.substring (i, i+2), 16);
86 buf[i/2] = value;
87 }
88
89 return buf;
90};
91
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080092
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080093$.Class ("FilesClosure", {}, {
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080094 init: function (chronoshare) {
95 this.chronoshare = chronoshare;
96 },
97
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080098 upcall: function(kind, upcallInfo) {
99 $("#loader").fadeOut (500); // ("hidden");
100 if (kind == Closure.UPCALL_CONTENT) {
101 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800102 if (PARAMS.debug) {
103 $("#json").text (convertedData);
104 $("#json").removeClass ("hidden");
105 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800106 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800107
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800108 // error handling?
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800109 table = $("#content").append (
110 $("<table />", { "class": "item-list" })
111 .append ($("<thead />")
112 .append ($("<tr />")
113 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
114 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
115 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
116 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
117 .append ($("<tbody />", { "id": "file-list-files" }))
118 .append ($("<tfoot />")
119 .append ($("<tr />")
120 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800121
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800122 var html = $("#file-list-files");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800123 for (var i = 0; i < data.files.length; i++) {
124 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800125
126 row = $("<tr />");
127 if (i%2) { row.addClass ("odd"); }
128
129 row.bind('mouseenter mouseleave', function() {
130 $(this).toggleClass('highlighted');
131 });
132
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800133 row.attr ("filename", encodeURIComponent(encodeURIComponent(file.filename)));
134
135 row.bind('click', function (e) {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800136 url = "#fileHistory";
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800137 url += "&item=" + $(this).attr ("filename");
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800138 pos = URIPARAMS.indexOf ("&");
139 if (pos >= 0) {
140 url += URIPARAMS.substring (pos)
141 }
142
143 document.location = url;
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800144 });
145
146 row.append ($("<td />", {"class": "border-left"}).text (file.filename));
147 row.append ($("<td />").text (file.version));
148 row.append ($("<td />").text (new Date (file.timestamp)));
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800149 row.append ($("<td />", {"class": "border-right"})
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800150 .append ($("<userName />").text (file.owner.userName))
151 .append ($("<seqNo> /").text (file.owner.seqNo)));
152
153 html = html.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800154 }
155 }
156 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
157 $("#error").html ("Interest timed out");
158 $("#error").removeClass ("hidden");
159 }
160 else {
161 $("#error").html ("Unknown error happened");
162 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800163 }
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800164 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800165});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800166
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800167
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800168$.Class ("HistoryClosure", {}, {
169 init: function (chronoshare) {
170 this.chronoshare = chronoshare;
171 },
172
173 upcall: function(kind, upcallInfo) {
174 $("#loader").fadeOut (500); // ("hidden");
175 if (kind == Closure.UPCALL_CONTENT) {
176 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800177 if (PARAMS.debug) {
178 $("#json").text (convertedData);
179 $("#json").removeClass ("hidden");
180 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800181 data = JSON.parse (convertedData);
182
183 // error handling?
184 table = $("#content").append (
185 $("<table />", { "class": "item-list" })
186 .append ($("<thead />")
187 .append ($("<tr />")
188 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
189 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
190 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
191 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
192 .append ($("<tbody />", { "id": "history-list-actions" }))
193 .append ($("<tfoot />")
194 .append ($("<tr />")
195 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
196
197 var html = $("#history-list-actions");
198 for (var i = 0; i < data.actions.length; i++) {
199 action = data.actions[i];
200
201 row = $("<tr />");
202 if (i%2) { row.addClass ("odd"); }
203
204 row.bind('mouseenter mouseleave', function() {
205 $(this).toggleClass('highlighted');
206 });
207
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800208 row.attr ("filename", encodeURIComponent(encodeURIComponent(action.filename)));
209
210 row.bind('click', function (e) {
211 url = "#fileHistory";
212 url += "&item=" + $(this).attr ("filename");
213 pos = URIPARAMS.indexOf ("&");
214 if (pos >= 0) {
215 url += URIPARAMS.substring (pos)
216 }
217
218 document.location = url;
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800219 });
220
221 row.append ($("<td />", {"class": "border-left"}).text (action.filename));
222 row.append ($("<td />").text (action.version));
223 row.append ($("<td />").text (new Date (action.timestamp)));
224 row.append ($("<td />")
225 .append ($("<userName />").text (action.id.userName))
226 .append ($("<seqNo> /").text (action.id.seqNo)));
227
228 html = html.append (row);
229 }
230 }
231 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
232 $("#error").html ("Interest timed out");
233 $("#error").removeClass ("hidden");
234 }
235 else {
236 $("#error").html ("Unknown error happened");
237 $("#error").removeClass ("hidden");
238 }
239 }
240});
241
242
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800243$.Class ("ChronoShare", { },
244 {
245 init: function (username, foldername) {
246 this.username = new Name (username);
247 this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder");
248
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800249 this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800250
251 this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file");
252
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800253 // this.ndn = new NDN ({host:"127.0.0.1", getHostAndPort: function() { return {host: "127.0.0.1", port: 9696}}});
254 this.ndn = new NDN ({host:"127.0.0.1"});
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800255 },
256
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800257
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800258 run: function () {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800259 $("#content").empty ();
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800260 $("#loader").fadeIn (500);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800261 $("#error").addClass ("hidden");
262
263 if (PAGE == "fileList") {
264 request = new Name ().add (this.files)./*add (folder_in_question).*/add ("nonce").addSegment (0);
265 console.log (request.to_uri ());
266 this.ndn.expressInterest (request, new FilesClosure (this));
267 }
268 else if (PAGE == "folderHistory") {
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800269 request = new Name ().add (this.actions).add ("folder")./*add (folder_in_question).*/add ("nonce").addSegment (0);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800270 console.log (request.to_uri ());
271 this.ndn.expressInterest (request, new HistoryClosure (this));
272 }
273 else if (PAGE == "fileHistory") {
274 if (!PARAMS.item) {
275 $("#loader").fadeOut (500); // ("hidden");
276 $("#error").html ("incorrect input for fileHistory command");
277 $("#error").removeClass ("hidden");
278 return;
279 }
Alexander Afanasyev39dbc4b2013-03-01 10:39:23 -0800280 request = new Name ().add (this.actions).add ("file").add (PARAMS.item).add ("nonce").addSegment (0);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800281 console.log (request.to_uri ());
282 this.ndn.expressInterest (request, new HistoryClosure (this));
283 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800284 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800285 });
286
287