Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 1 | var CHRONOSHARE; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 2 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 3 | var PAGE = "folderHistory"; |
| 4 | var PARAMS = [ ]; |
| 5 | var URIPARAMS = ""; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 6 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 7 | function nav_anchor (aurl) { |
| 8 | aurl = aurl.split('#'); |
| 9 | if (aurl[1]) |
| 10 | { |
| 11 | aurl_split = aurl[1].split ('&'); |
| 12 | page = aurl_split[0]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 13 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 14 | 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 Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 22 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 23 | if (page != PAGE) |
| 24 | { |
| 25 | PAGE = page; |
| 26 | PARAMS = vars; |
| 27 | URIPARAMS = aurl[1]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 29 | if (CHRONOSHARE) { |
| 30 | CHRONOSHARE.run (); |
| 31 | } |
| 32 | } |
| 33 | else if (aurl != URIPARAMS) |
| 34 | { |
| 35 | PARAMS = vars; |
| 36 | URIPARAMS = aurl[1]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 37 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 38 | 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 Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 74 | |
| 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 | */ |
| 81 | StringHashToUint8Array = 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 97 | $.Class ("FilesClosure", {}, { |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 98 | init: function (chronoshare) { |
| 99 | this.chronoshare = chronoshare; |
| 100 | }, |
| 101 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 102 | 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 110 | // error handling? |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 111 | 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 123 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 124 | var html = $("#file-list-files"); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 125 | for (var i = 0; i < data.files.length; i++) { |
| 126 | file = data.files[i]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 127 | |
| 128 | row = $("<tr />"); |
| 129 | if (i%2) { row.addClass ("odd"); } |
| 130 | |
| 131 | row.bind('mouseenter mouseleave', function() { |
| 132 | $(this).toggleClass('highlighted'); |
| 133 | }); |
| 134 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 135 | // fileHistoryUrl = new Name () |
| 136 | // .add (this.chronoshare.actions) |
| 137 | // .add (file.filename) |
| 138 | // .add ("nonce") |
| 139 | // .addSegment (0); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 140 | // row.attr ("history", fileHistoryUrl.to_uri ()); |
| 141 | row.bind('click', function () { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 142 | 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 Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 151 | }); |
| 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 161 | } |
| 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 170 | } |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 171 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 172 | }); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 174 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 175 | $.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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 245 | $.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 Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 251 | this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions").add ("folder"); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 252 | |
| 253 | this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file"); |
| 254 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 255 | // 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 257 | }, |
| 258 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 259 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 260 | run: function () { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 261 | $("#content").empty (); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 262 | $("#loader").fadeIn (500); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 263 | $("#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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 286 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 287 | }); |
| 288 | |
| 289 | |