blob: cc1f9ae8031d3867165959dcc82823c0759dbc9e [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 Afanasyevf63a5142013-02-28 02:21:42 -0800133 // fileHistoryUrl = new Name ()
134 // .add (this.chronoshare.actions)
135 // .add (file.filename)
136 // .add ("nonce")
137 // .addSegment (0);
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800138 // row.attr ("history", fileHistoryUrl.to_uri ());
139 row.bind('click', function () {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800140 url = "#fileHistory";
141 url += "&item=" + encodeURIComponent(encodeURIComponent(file.filename));
142 pos = URIPARAMS.indexOf ("&");
143 if (pos >= 0) {
144 url += URIPARAMS.substring (pos)
145 }
146
147 document.location = url;
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800148 });
149
150 row.append ($("<td />", {"class": "border-left"}).text (file.filename));
151 row.append ($("<td />").text (file.version));
152 row.append ($("<td />").text (new Date (file.timestamp)));
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800153 row.append ($("<td />", {"class": "border-right"})
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800154 .append ($("<userName />").text (file.owner.userName))
155 .append ($("<seqNo> /").text (file.owner.seqNo)));
156
157 html = html.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800158 }
159 }
160 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
161 $("#error").html ("Interest timed out");
162 $("#error").removeClass ("hidden");
163 }
164 else {
165 $("#error").html ("Unknown error happened");
166 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800167 }
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800168 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800169});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800170
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800171
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800172$.Class ("HistoryClosure", {}, {
173 init: function (chronoshare) {
174 this.chronoshare = chronoshare;
175 },
176
177 upcall: function(kind, upcallInfo) {
178 $("#loader").fadeOut (500); // ("hidden");
179 if (kind == Closure.UPCALL_CONTENT) {
180 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
Alexander Afanasyevc15e0162013-02-28 23:38:52 -0800181 if (PARAMS.debug) {
182 $("#json").text (convertedData);
183 $("#json").removeClass ("hidden");
184 }
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800185 data = JSON.parse (convertedData);
186
187 // error handling?
188 table = $("#content").append (
189 $("<table />", { "class": "item-list" })
190 .append ($("<thead />")
191 .append ($("<tr />")
192 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
193 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
194 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
195 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
196 .append ($("<tbody />", { "id": "history-list-actions" }))
197 .append ($("<tfoot />")
198 .append ($("<tr />")
199 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
200
201 var html = $("#history-list-actions");
202 for (var i = 0; i < data.actions.length; i++) {
203 action = data.actions[i];
204
205 row = $("<tr />");
206 if (i%2) { row.addClass ("odd"); }
207
208 row.bind('mouseenter mouseleave', function() {
209 $(this).toggleClass('highlighted');
210 });
211
212 fileHistoryUrl = new Name ()
213 .add (this.chronoshare.actions)
214 .add (action.filename)
215 .add ("nonce")
216 .addSegment (0);
217 // row.attr ("history", fileHistoryUrl.to_uri ());
218 row.bind('click', function () {
219 // alert (fileHistoryUrl.to_uri ());
220 });
221
222 row.append ($("<td />", {"class": "border-left"}).text (action.filename));
223 row.append ($("<td />").text (action.version));
224 row.append ($("<td />").text (new Date (action.timestamp)));
225 row.append ($("<td />")
226 .append ($("<userName />").text (action.id.userName))
227 .append ($("<seqNo> /").text (action.id.seqNo)));
228
229 html = html.append (row);
230 }
231 }
232 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
233 $("#error").html ("Interest timed out");
234 $("#error").removeClass ("hidden");
235 }
236 else {
237 $("#error").html ("Unknown error happened");
238 $("#error").removeClass ("hidden");
239 }
240 }
241});
242
243
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800244$.Class ("ChronoShare", { },
245 {
246 init: function (username, foldername) {
247 this.username = new Name (username);
248 this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder");
249
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800250 this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions").add ("folder");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800251
252 this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file");
253
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800254 // this.ndn = new NDN ({host:"127.0.0.1", getHostAndPort: function() { return {host: "127.0.0.1", port: 9696}}});
255 this.ndn = new NDN ({host:"127.0.0.1"});
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800256 },
257
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800258
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800259 run: function () {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800260 $("#content").empty ();
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800261 $("#loader").fadeIn (500);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800262 $("#error").addClass ("hidden");
263
264 if (PAGE == "fileList") {
265 request = new Name ().add (this.files)./*add (folder_in_question).*/add ("nonce").addSegment (0);
266 console.log (request.to_uri ());
267 this.ndn.expressInterest (request, new FilesClosure (this));
268 }
269 else if (PAGE == "folderHistory") {
270 request = new Name ().add (this.actions)./*add (folder_in_question).*/add ("nonce").addSegment (0);
271 console.log (request.to_uri ());
272 this.ndn.expressInterest (request, new HistoryClosure (this));
273 }
274 else if (PAGE == "fileHistory") {
275 if (!PARAMS.item) {
276 $("#loader").fadeOut (500); // ("hidden");
277 $("#error").html ("incorrect input for fileHistory command");
278 $("#error").removeClass ("hidden");
279 return;
280 }
281 request = new Name ().add (this.actions).add (PARAMS.item).add ("nonce").addSegment (0);
282 console.log (request.to_uri ());
283 this.ndn.expressInterest (request, new HistoryClosure (this));
284 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800285 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800286 });
287
288