blob: fcd0bd406b777d116853d9ffc51032f84ed245b3 [file] [log] [blame]
Alexander Afanasyev46bd8062013-02-27 23:59:15 -08001// var PAGE = "fileList";
2// var PARAMS = [ ];
3// var URIPARAMS = "";
4
5// $(document).ready (function () {
6// function nav_anchor (aurl) {
7// aurl = aurl.split('#');
8// if (aurl[1])
9// {
10// aurl_split = aurl[1].split ('?');
11// page = aurl_split[0];
12
13// vars = [ ];
14// if (aurl_split[1]) {
15// var hashes = aurl_split[1].slice (aurl_split[1].indexOf ('?') + 1).split ('&');
16// for(var i = 0; i < hashes.length; i++)
17// {
18// hash = hashes[i].split('=');
19// vars.push(hash[0]);
20// vars[hash[0]] = hash[1];
21// }
22// }
23
24// // alert (aurl_split[1]);
25
26// if (page != PAGE)
27// {
28// alert (page);
29// PAGE = page;
30// PARAMS = vars;
31// URIPARAMS = aurl_split[1];
32// alert (URIPARAMS);
33// }
34// else if (aurl_split[1] != URIPARAMS)
35// {
36// alert (aurl_split[1]);
37// }
38// }
39// }
40
41// nav_anchor (window.location.href);
42
43// $("a").click (function(){
44// nav_anchor (this.href)
45// });
46// });
47
48/**
49 * @brief Convert binary data represented as non-escaped hex string to Uint8Array
50 * @param str String like ba0cb43e4b9639c114a0487d5faa7c70452533963fc8beb37d1b67c09a48a21d
51 *
52 * Note that if string length is odd, null will be returned
53 */
54StringHashToUint8Array = function (str) {
55 if (str.length % 2 != 0) {
56 return null;
57 }
58
59 var buf = new Uint8Array (str.length / 2);
60
61 for (var i = 0; i < str.length; i+=2) {
62 value = parseInt (str.substring (i, i+2), 16);
63 buf[i/2] = value;
64 }
65
66 return buf;
67};
68
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080069
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080070$.Class ("FilesClosure", {}, {
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080071 init: function (chronoshare) {
72 this.chronoshare = chronoshare;
73 },
74
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080075 upcall: function(kind, upcallInfo) {
76 $("#loader").fadeOut (500); // ("hidden");
77 if (kind == Closure.UPCALL_CONTENT) {
78 convertedData = DataUtils.toString (upcallInfo.contentObject.content);
79 $("#json").text (convertedData);
80 $("#json").removeClass ("hidden");
81 data = JSON.parse (convertedData);
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080082
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080083 // error handling?
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -080084
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -080085 var html = $("tbody#files");
86 for (var i = 0; i < data.files.length; i++) {
87 file = data.files[i];
Alexander Afanasyev46bd8062013-02-27 23:59:15 -080088
89 row = $("<tr />");
90 if (i%2) { row.addClass ("odd"); }
91
92 row.bind('mouseenter mouseleave', function() {
93 $(this).toggleClass('highlighted');
94 });
95
96 fileHistoryUrl = new Name ()
97 .add (this.chronoshare.actions)
98 .add (file.filename)
99 .add ("nonce")
100 .addSegment (0);
101 // row.attr ("history", fileHistoryUrl.to_uri ());
102 row.bind('click', function () {
103 // alert (fileHistoryUrl.to_uri ());
104 });
105
106 row.append ($("<td />", {"class": "border-left"}).text (file.filename));
107 row.append ($("<td />").text (file.version));
108 row.append ($("<td />").text (new Date (file.timestamp)));
109 row.append ($("<td />")
110 .append ($("<userName />").text (file.owner.userName))
111 .append ($("<seqNo> /").text (file.owner.seqNo)));
112
113 html = html.append (row);
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800114 }
115 }
116 else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) {
117 $("#error").html ("Interest timed out");
118 $("#error").removeClass ("hidden");
119 }
120 else {
121 $("#error").html ("Unknown error happened");
122 $("#error").removeClass ("hidden");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800123 }
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800124 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800125});
Zhenkai Zhu5c2475b2013-02-26 22:57:31 -0800126
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800127
128$.Class ("ChronoShare", { },
129 {
130 init: function (username, foldername) {
131 this.username = new Name (username);
132 this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder");
133
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800134 this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions").add ("file");
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800135
136 this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file");
137
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800138 // this.ndn = new NDN ({host:"127.0.0.1", getHostAndPort: function() { return {host: "127.0.0.1", port: 9696}}});
139 this.ndn = new NDN ({host:"127.0.0.1"});
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800140 },
141
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800142
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800143 run: function () {
144 request = new Name ().add (this.files)./*add (folder_in_question).*/add ("nonce").addSegment (0);
145 console.log (request.to_uri ());
146 $("#files").empty ();
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800147 $("#loader").fadeIn (500);
Alexander Afanasyev46bd8062013-02-27 23:59:15 -0800148 this.ndn.expressInterest (request, new FilesClosure (this));
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800149 }
Alexander Afanasyevfd5e6272013-02-27 20:25:20 -0800150 });
151
152