Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 1 | // 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 | */ |
| 54 | StringHashToUint8Array = 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 69 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 70 | $.Class ("FilesClosure", {}, { |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 71 | init: function (chronoshare) { |
| 72 | this.chronoshare = chronoshare; |
| 73 | }, |
| 74 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 75 | 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 Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 82 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 83 | // error handling? |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 84 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 85 | var html = $("tbody#files"); |
| 86 | for (var i = 0; i < data.files.length; i++) { |
| 87 | file = data.files[i]; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 88 | |
| 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 114 | } |
| 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 123 | } |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 124 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 125 | }); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 127 | |
| 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 Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 134 | this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions").add ("file"); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 135 | |
| 136 | this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file"); |
| 137 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 138 | // 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 140 | }, |
| 141 | |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 142 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 143 | 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 Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 147 | $("#loader").fadeIn (500); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame^] | 148 | this.ndn.expressInterest (request, new FilesClosure (this)); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 149 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 150 | }); |
| 151 | |
| 152 | |