blob: d94bb5fa72e47fd36dba2ce67efce13d4f770197 [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
66 $("a").click (function () {
67 nav_anchor (this.href)
68 });
69
70 $(window).on('hashchange', function() {
71 nav_anchor (window.location.href);
72 });
73});
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080074
75/**
76 * @brief Convert binary data represented as non-escaped hex string to Uint8Array
77 * @param str String like ba0cb43e4b9639c114a0487d5faa7c70452533963fc8beb37d1b67c09a48a21d
78 *
79 * Note that if string length is odd, null will be returned
80 */
81StringHashToUint8Array = function (str) {
82 if (str.length % 2 != 0) {
83 return null;
84 }
85
86 var buf = new Uint8Array (str.length / 2);
87
88 for (var i = 0; i < str.length; i+=2) {
89 value = parseInt (str.substring (i, i+2), 16);
90 buf[i/2] = value;
91 }
92
93 return buf;
94};
95
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080096
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);
106 $("#json").text (convertedData);
107 $("#json").removeClass ("hidden");
108 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800109
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800110 // error handling?
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800111 table = $("#content").append (
112 $("<table />", { "class": "item-list" })
113 .append ($("<thead />")
114 .append ($("<tr />")
115 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
116 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
117 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
118 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
119 .append ($("<tbody />", { "id": "file-list-files" }))
120 .append ($("<tfoot />")
121 .append ($("<tr />")
122 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800123
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800124 var html = $("#file-list-files");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800125 for (var i = 0; i < data.files.length; i++) {
126 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800127
128 row = $("<tr />");
129 if (i%2) { row.addClass ("odd"); }
130
131 row.bind('mouseenter mouseleave', function() {
132 $(this).toggleClass('highlighted');
133 });
134
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800135 // fileHistoryUrl = new Name ()
136 // .add (this.chronoshare.actions)
137 // .add (file.filename)
138 // .add ("nonce")
139 // .addSegment (0);
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800140 // row.attr ("history", fileHistoryUrl.to_uri ());
141 row.bind('click', function () {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800142 url = "#fileHistory";
143 url += "&item=" + encodeURIComponent(encodeURIComponent(file.filename));
144 pos = URIPARAMS.indexOf ("&");
145 if (pos >= 0) {
146 url += URIPARAMS.substring (pos)
147 }
148
149 document.location = url;
150 nav_anchor (document.location.href);
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800151 });
152
153 row.append ($("<td />", {"class": "border-left"}).text (file.filename));
154 row.append ($("<td />").text (file.version));
155 row.append ($("<td />").text (new Date (file.timestamp)));
156 row.append ($("<td />")
157 .append ($("<userName />").text (file.owner.userName))
158 .append ($("<seqNo> /").text (file.owner.seqNo)));
159
160 html = html.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800161 }
162 }
163 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
164 $("#error").html ("Interest timed out");
165 $("#error").removeClass ("hidden");
166 }
167 else {
168 $("#error").html ("Unknown error happened");
169 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800170 }
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800171 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800172});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800173
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800174
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800175$.Class ("HistoryClosure", {}, {
176 init: function (chronoshare) {
177 this.chronoshare = chronoshare;
178 },
179
180 upcall: function(kind, upcallInfo) {
181 $("#loader").fadeOut (500); // ("hidden");
182 if (kind == Closure.UPCALL_CONTENT) {
183 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
184 $("#json").text (convertedData);
185 $("#json").removeClass ("hidden");
186 data = JSON.parse (convertedData);
187
188 // error handling?
189 table = $("#content").append (
190 $("<table />", { "class": "item-list" })
191 .append ($("<thead />")
192 .append ($("<tr />")
193 .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename"))
194 .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version"))
195 .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified"))
196 .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By"))))
197 .append ($("<tbody />", { "id": "history-list-actions" }))
198 .append ($("<tfoot />")
199 .append ($("<tr />")
200 .append ($("<td />", { "colspan": "4", "class": "border-right border-left" })))));
201
202 var html = $("#history-list-actions");
203 for (var i = 0; i < data.actions.length; i++) {
204 action = data.actions[i];
205
206 row = $("<tr />");
207 if (i%2) { row.addClass ("odd"); }
208
209 row.bind('mouseenter mouseleave', function() {
210 $(this).toggleClass('highlighted');
211 });
212
213 fileHistoryUrl = new Name ()
214 .add (this.chronoshare.actions)
215 .add (action.filename)
216 .add ("nonce")
217 .addSegment (0);
218 // row.attr ("history", fileHistoryUrl.to_uri ());
219 row.bind('click', function () {
220 // alert (fileHistoryUrl.to_uri ());
221 });
222
223 row.append ($("<td />", {"class": "border-left"}).text (action.filename));
224 row.append ($("<td />").text (action.version));
225 row.append ($("<td />").text (new Date (action.timestamp)));
226 row.append ($("<td />")
227 .append ($("<userName />").text (action.id.userName))
228 .append ($("<seqNo> /").text (action.id.seqNo)));
229
230 html = html.append (row);
231 }
232 }
233 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
234 $("#error").html ("Interest timed out");
235 $("#error").removeClass ("hidden");
236 }
237 else {
238 $("#error").html ("Unknown error happened");
239 $("#error").removeClass ("hidden");
240 }
241 }
242});
243
244
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800245$.Class ("ChronoShare", { },
246 {
247 init: function (username, foldername) {
248 this.username = new Name (username);
249 this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder");
250
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800251 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 -0800252
253 this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file");
254
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800255 // this.ndn = new NDN ({host:"127.0.0.1", getHostAndPort: function() { return {host: "127.0.0.1", port: 9696}}});
256 this.ndn = new NDN ({host:"127.0.0.1"});
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800257 },
258
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800259
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800260 run: function () {
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800261 $("#content").empty ();
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800262 $("#loader").fadeIn (500);
Alexander Afanasyevf63a5142013-02-28 02:21:42 -0800263 $("#error").addClass ("hidden");
264
265 if (PAGE == "fileList") {
266 request = new Name ().add (this.files)./*add (folder_in_question).*/add ("nonce").addSegment (0);
267 console.log (request.to_uri ());
268 this.ndn.expressInterest (request, new FilesClosure (this));
269 }
270 else if (PAGE == "folderHistory") {
271 request = new Name ().add (this.actions)./*add (folder_in_question).*/add ("nonce").addSegment (0);
272 console.log (request.to_uri ());
273 this.ndn.expressInterest (request, new HistoryClosure (this));
274 }
275 else if (PAGE == "fileHistory") {
276 if (!PARAMS.item) {
277 $("#loader").fadeOut (500); // ("hidden");
278 $("#error").html ("incorrect input for fileHistory command");
279 $("#error").removeClass ("hidden");
280 return;
281 }
282 request = new Name ().add (this.actions).add (PARAMS.item).add ("nonce").addSegment (0);
283 console.log (request.to_uri ());
284 this.ndn.expressInterest (request, new HistoryClosure (this));
285 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800286 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800287 });
288
289