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 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 66 | $(window).on('hashchange', function() { |
| 67 | nav_anchor (window.location.href); |
| 68 | }); |
| 69 | }); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 70 | |
| 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 | */ |
| 77 | StringHashToUint8Array = 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 93 | $.Class ("FilesClosure", {}, { |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 94 | init: function (chronoshare) { |
| 95 | this.chronoshare = chronoshare; |
| 96 | }, |
| 97 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 98 | upcall: function(kind, upcallInfo) { |
| 99 | $("#loader").fadeOut (500); // ("hidden"); |
| 100 | if (kind == Closure.UPCALL_CONTENT) { |
| 101 | convertedData = DataUtils.toString (upcallInfo.contentObject.content); |
Alexander Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame^] | 102 | if (PARAMS.debug) { |
| 103 | $("#json").text (convertedData); |
| 104 | $("#json").removeClass ("hidden"); |
| 105 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 106 | data = JSON.parse (convertedData); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 107 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 108 | // error handling? |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 109 | 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 121 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 122 | var html = $("#file-list-files"); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 123 | for (var i = 0; i < data.files.length; i++) { |
| 124 | file = data.files[i]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 125 | |
| 126 | row = $("<tr />"); |
| 127 | if (i%2) { row.addClass ("odd"); } |
| 128 | |
| 129 | row.bind('mouseenter mouseleave', function() { |
| 130 | $(this).toggleClass('highlighted'); |
| 131 | }); |
| 132 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 133 | // fileHistoryUrl = new Name () |
| 134 | // .add (this.chronoshare.actions) |
| 135 | // .add (file.filename) |
| 136 | // .add ("nonce") |
| 137 | // .addSegment (0); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 138 | // row.attr ("history", fileHistoryUrl.to_uri ()); |
| 139 | row.bind('click', function () { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 140 | 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 Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 148 | }); |
| 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 Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame^] | 153 | row.append ($("<td />", {"class": "border-right"}) |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 154 | .append ($("<userName />").text (file.owner.userName)) |
| 155 | .append ($("<seqNo> /").text (file.owner.seqNo))); |
| 156 | |
| 157 | html = html.append (row); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 158 | } |
| 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 167 | } |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 168 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 169 | }); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 171 | |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 172 | $.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 Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame^] | 181 | if (PARAMS.debug) { |
| 182 | $("#json").text (convertedData); |
| 183 | $("#json").removeClass ("hidden"); |
| 184 | } |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 185 | 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 244 | $.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 Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 250 | 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] | 251 | |
| 252 | this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file"); |
| 253 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 254 | // 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 256 | }, |
| 257 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 258 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 259 | run: function () { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 260 | $("#content").empty (); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 261 | $("#loader").fadeIn (500); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 262 | $("#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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 285 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 286 | }); |
| 287 | |
| 288 | |