Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 1 | $.Class ("ChronoShare", { }, |
| 2 | { |
| 3 | init: function (username, foldername) { |
| 4 | $("#folder-name").text (foldername); |
| 5 | $("#user-name").text (username); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 6 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 7 | this.username = new Name (username); |
| 8 | this.files = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("files").add ("folder"); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 9 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 10 | this.actions = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("info").add ("actions"); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 11 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 12 | this.restore = new Name ("/localhost").add (this.username).add ("chronoshare").add (foldername).add ("cmd").add ("restore").add ("file"); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 13 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 14 | this.ndn = new NDN ({host:"127.0.0.1"}); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 15 | this.ndn.verify = false; //disable content verification, works WAAAAY faster |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 16 | }, |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 17 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 18 | run: function () { |
| 19 | console.log ("RUN page: " + PAGE); |
| 20 | $("#loader").fadeIn (500); |
| 21 | $("#error").addClass ("hidden"); |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 22 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 23 | cmd = {}; |
| 24 | if (PAGE == "fileList") { |
| 25 | cmd = this.info_files (PARAMS.item); |
| 26 | } |
| 27 | else if (PAGE == "folderHistory") { |
| 28 | cmd = this.info_actions ("folder", PARAMS.item); |
| 29 | } |
| 30 | else if (PAGE == "fileHistory") { |
| 31 | cmd = this.info_actions ("file", PARAMS.item); |
| 32 | } |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 33 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 34 | if (cmd.request && cmd.callback) { |
| 35 | console.log (cmd.request.to_uri ()); |
| 36 | this.ndn.expressInterest (cmd.request, cmd.callback); |
| 37 | } |
| 38 | else { |
| 39 | $("#loader").fadeOut (500); // ("hidden"); |
| 40 | $("#content").empty (); |
| 41 | if (cmd.error) { |
| 42 | $("#error").html (cmd.error); |
| 43 | } |
| 44 | else { |
| 45 | $("#error").html ("Unknown error with " + PAGE); |
| 46 | } |
| 47 | $("#error").removeClass ("hidden"); |
| 48 | } |
| 49 | }, |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 50 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 51 | info_files: function(folder) { |
| 52 | request = new Name ().add (this.files)./*add (folder_in_question).*/addSegment (PARAMS.offset?PARAMS.offset:0); |
| 53 | return { request:request, callback: new FilesClosure (this) }; |
| 54 | }, |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 56 | info_actions: function (type/*"file" or "folder"*/, fileOrFolder /*file or folder name*/) { |
| 57 | if (type=="file" && !fileOrFolder) { |
| 58 | return { error: "info_actions: fileOrFolder parameter is missing" }; |
| 59 | } |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 60 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 61 | request = new Name ().add (this.actions).add (type); |
| 62 | if (fileOrFolder) { |
| 63 | request.add (fileOrFolder); |
| 64 | } |
| 65 | request.addSegment (PARAMS.offset?PARAMS.offset:0); |
| 66 | return { request: request, callback: new HistoryClosure (this) }; |
| 67 | }, |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 68 | |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 69 | cmd_restore_file: function (filename, version, hash, callback/*function (bool <- data received, status <- returned status)*/) { |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 70 | request = new Name ().add (this.restore) |
| 71 | .add (filename) |
| 72 | .addSegment (version) |
| 73 | .add (hash); |
| 74 | console.log (request.to_uri ()); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 75 | this.ndn.expressInterest (request, new CmdRestoreFileClosure (this, callback)); |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 76 | }, |
| 77 | |
| 78 | get_file: function (modifiedBy, hash, segments, callback/*function (bool <- data received, data <- returned data)*/) { |
| 79 | baseName = new Name (modifiedBy) |
| 80 | .add ("chronoshare").add ("file") |
| 81 | .add (hash); |
| 82 | |
| 83 | new FileGetter (this.ndn, baseName, segments, callback) |
| 84 | .start (); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 85 | } |
| 86 | }); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 88 | $.Class ("CmdRestoreFileClosure", {}, { |
| 89 | init: function (chronoshare, callback) { |
| 90 | this.chronoshare = chronoshare; |
| 91 | this.callback = callback; |
| 92 | }, |
| 93 | upcall: function(kind, upcallInfo) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 94 | if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 95 | convertedData = DataUtils.toString (upcallInfo.contentObject.content); |
| 96 | this.callback (true, convertedData); |
| 97 | } |
| 98 | else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
| 99 | this.callback (false, "Interest timed out"); |
| 100 | } |
| 101 | else { |
| 102 | this.callback (false, "Unknown error happened"); |
| 103 | } |
| 104 | } |
| 105 | }); |
| 106 | |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 107 | $.Class ("FileGetter", {}, { |
| 108 | init: function (ndn, baseName, segments, callback) { |
| 109 | this.ndn = ndn; |
| 110 | this.callback = callback; |
| 111 | this.baseName = baseName; |
| 112 | this.segments = segments; |
| 113 | this.lastSegmentRequested = -1; |
| 114 | |
| 115 | this.data = ""; |
| 116 | }, |
| 117 | |
| 118 | start: function () { |
| 119 | this.lastSegmentRequested ++; |
| 120 | |
| 121 | request = new Name () |
| 122 | .add (this.baseName) |
| 123 | .addSegment (this.lastSegmentRequested); |
| 124 | |
| 125 | console.log (request.to_uri ()); |
| 126 | this.ndn.expressInterest (request, this); |
| 127 | }, |
| 128 | |
| 129 | upcall: function(kind, upcallInfo) { |
| 130 | if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification |
| 131 | convertedData = DataUtils.toString (upcallInfo.contentObject.content); |
| 132 | |
| 133 | this.data += convertedData; |
| 134 | |
| 135 | if (this.lastSegmentRequested+1 == this.segments) { |
| 136 | this.callback (true, this.data); |
| 137 | } |
| 138 | else { |
| 139 | this.start (); |
| 140 | } |
| 141 | } |
| 142 | else { |
| 143 | this.callback (false, "Interest timed out"); |
| 144 | } |
| 145 | } |
| 146 | }); |
| 147 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 148 | $.Class ("RestPipelineClosure", {}, { |
| 149 | init: function (collectionName, moreName) { |
| 150 | this.collectionName = collectionName; |
| 151 | this.moreName = moreName; |
| 152 | $("#json").empty (); |
| 153 | |
| 154 | this.collection = []; |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 155 | this.counter = 0; |
Alexander Afanasyev | 46bd806 | 2013-02-27 23:59:15 -0800 | [diff] [blame] | 156 | }, |
| 157 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 158 | upcall: function(kind, upcallInfo) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 159 | if (kind == Closure.UPCALL_CONTENT || kind == Closure.UPCALL_CONTENT_UNVERIFIED) { //disable content verification |
| 160 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 161 | convertedData = DataUtils.toString (upcallInfo.contentObject.content); |
Alexander Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame] | 162 | if (PARAMS.debug) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 163 | $("#json").append ($(document.createTextNode(convertedData))); |
Alexander Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame] | 164 | $("#json").removeClass ("hidden"); |
| 165 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 166 | data = JSON.parse (convertedData); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 167 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 168 | this.collection = this.collection.concat (data[this.collectionName]); |
| 169 | if (data[this.moreName] !== undefined) { |
| 170 | nextSegment = upcallInfo.interest.name.cut (1).addSegment (data[this.moreName]); |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 171 | this.counter ++; |
| 172 | |
| 173 | if (this.counter < 5) { |
| 174 | console.log ("MORE: " +nextSegment.to_uri ()); |
| 175 | CHRONOSHARE.ndn.expressInterest (nextSegment, this); |
| 176 | } |
| 177 | else { |
| 178 | $("#loader").fadeOut (500); // ("hidden"); |
| 179 | this.onData (this.collection, data[this.moreName]); |
| 180 | } |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 181 | } |
| 182 | else { |
| 183 | $("#loader").fadeOut (500); // ("hidden"); |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 184 | this.onData (this.collection, undefined); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 185 | } |
| 186 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 187 | else if (kind == Closure.UPCALL_INTEREST_TIMED_OUT) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 188 | $("#loader").fadeOut (500); // ("hidden"); |
| 189 | this.onTimeout (upcallInfo.interest); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 190 | } |
| 191 | else { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 192 | $("#loader").fadeOut (500); // ("hidden"); |
| 193 | this.onUnknownError (upcallInfo.interest); |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 194 | } |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 195 | |
| 196 | return Closure.RESULT_OK; // make sure we never re-express the interest |
| 197 | }, |
| 198 | |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 199 | onData: function(data, more) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 200 | }, |
| 201 | |
| 202 | onTimeout: function () { |
| 203 | $("#error").html ("Interest timed out"); |
| 204 | $("#error").removeClass ("hidden"); |
| 205 | }, |
| 206 | |
| 207 | onUnknownError: function () { |
| 208 | $("#error").html ("Unknown error happened"); |
| 209 | $("#error").removeClass ("hidden"); |
| 210 | } |
| 211 | }); |
| 212 | |
| 213 | // $.Class ("FilesClosure", {}, { |
| 214 | RestPipelineClosure ("FilesClosure", {}, { |
| 215 | init: function (chronoshare) { |
| 216 | this._super("files", "more"); |
| 217 | this.chronoshare = chronoshare; |
| 218 | }, |
| 219 | |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 220 | onData: function(data, more) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 221 | tbody = $("<tbody />", { "id": "file-list-files" }); |
| 222 | |
| 223 | /// @todo Eventually set title for other pages |
| 224 | $("title").text ("ChronoShare - List of files" + (PARAMS.item?" - "+PARAMS.item:"")); |
| 225 | |
| 226 | // error handling? |
| 227 | newcontent = $("<div />", { "id": "content" }).append ( |
| 228 | $("<h2 />").append ($(document.createTextNode("List of files ")), $("<green />").text (PARAMS.item)), |
| 229 | $("<table />", { "class": "item-list" }) |
| 230 | .append ($("<thead />") |
| 231 | .append ($("<tr />") |
| 232 | .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename")) |
| 233 | .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version")) |
| 234 | .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size")) |
| 235 | .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified")) |
| 236 | .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By")))) |
| 237 | .append (tbody) |
| 238 | .append ($("<tfoot />") |
| 239 | .append ($("<tr />") |
| 240 | .append ($("<td />", { "colspan": "5", "class": "border-right border-left" }))))); |
| 241 | newcontent.hide (); |
| 242 | |
| 243 | for (var i = 0; i < data.length; i++) { |
| 244 | file = data[i]; |
| 245 | |
| 246 | row = $("<tr />", { "class": "with-context-menu" } ); |
| 247 | if (i%2) { row.addClass ("odd"); } |
| 248 | |
| 249 | row.bind('mouseenter mouseleave', function() { |
| 250 | $(this).toggleClass('highlighted'); |
| 251 | }); |
| 252 | |
| 253 | row.attr ("filename", file.filename); //encodeURIComponent(encodeURIComponent(file.filename))); |
| 254 | row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) }); |
| 255 | |
| 256 | row.append ($("<td />", { "class": "filename border-left" }) |
| 257 | .text (file.filename) |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 258 | .prepend ($("<img />", { "src": imgFullPath(fileExtension(file.filename)) }))); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 259 | row.append ($("<td />", { "class": "version" }).text (file.version)); |
| 260 | row.append ($("<td />", { "class": "size" }).text (SegNumToFileSize (file.segNum))); |
| 261 | row.append ($("<td />", { "class": "modified" }).text (new Date (file.timestamp+"+00:00"))); // convert from UTC |
| 262 | row.append ($("<td />", { "class": "modified-by border-right"}) |
| 263 | .append ($("<userName />").text (file.owner.userName)) |
| 264 | .append ($("<seqNo> /").text (file.owner.seqNo))); |
| 265 | |
| 266 | tbody = tbody.append (row); |
| 267 | } |
| 268 | |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 269 | displayContent (newcontent, more, this.base_url ()); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 270 | |
| 271 | $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup |
| 272 | $.contextMenu({ |
| 273 | selector: ".with-context-menu", |
| 274 | items: { |
| 275 | "info": {name: "x", type: "html", html: "<b>File operations</b>"}, |
| 276 | "sep1": "---------", |
| 277 | history: {name: "View file history", |
| 278 | icon: "quit", // need a better icon |
| 279 | callback: function(key, opt) { |
| 280 | openHistoryForItem (opt.$trigger.attr ("filename")); |
| 281 | }}, |
| 282 | } |
| 283 | }); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 284 | }, |
| 285 | |
| 286 | base_url: function () { |
| 287 | url = "#fileList"+ |
| 288 | "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) + |
| 289 | "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder)); |
| 290 | if (PARAMS.item !== undefined) { |
| 291 | url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item)); |
| 292 | } |
| 293 | return url; |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 294 | } |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 295 | }); |
Zhenkai Zhu | 5c2475b | 2013-02-26 22:57:31 -0800 | [diff] [blame] | 296 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 297 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 298 | RestPipelineClosure ("HistoryClosure", {}, { |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 299 | init: function (chronoshare) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 300 | this._super("actions", "more"); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 301 | this.chronoshare = chronoshare; |
| 302 | }, |
| 303 | |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 304 | previewFile: function (file) { |
| 305 | if (fileExtension(file.attr ("filename")) == "txt") { |
| 306 | CHRONOSHARE.get_file (file.attr ("file_modified_by"), |
| 307 | DataUtils.toNumbers (file.attr ("file_hash")), |
| 308 | file.attr ("file_seg_num"), |
| 309 | function (status, data) { |
| 310 | $("<div />", { "title": "Preview of " + file.attr ("filename") + " version " + file.attr ("file_version") }) |
| 311 | .append ($("<pre />").text (data)) |
| 312 | .dialog ({ |
| 313 | resizable: true, |
| 314 | width: $(window).width() * 0.8, |
| 315 | maxHeight: $(window).height() * 0.8, |
| 316 | show: "blind", |
| 317 | hide: "fold", |
| 318 | modal: true, |
| 319 | }); |
| 320 | }); |
| 321 | } |
| 322 | else { |
| 323 | custom_alert ("Preview is not support for this type of file"); |
| 324 | } |
| 325 | }, |
| 326 | |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 327 | onData: function(data, more) { |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 328 | tbody = $("<tbody />", { "id": "history-list-actions" }); |
| 329 | |
| 330 | /// @todo Eventually set title for other pages |
| 331 | $("title").text ("ChronoShare - Recent actions" + (PARAMS.item?" - "+PARAMS.item:"")); |
| 332 | |
| 333 | newcontent = $("<div />", { "id": "content" }).append ( |
| 334 | $("<h2 />").append ($(document.createTextNode("Recent actions ")), $("<green />").text (PARAMS.item)), |
| 335 | $("<table />", { "class": "item-list" }) |
| 336 | .append ($("<thead />") |
| 337 | .append ($("<tr />") |
| 338 | .append ($("<th />", { "class": "filename border-left", "scope": "col" }).text ("Filename")) |
| 339 | .append ($("<th />", { "class": "version", "scope": "col" }).text ("Version")) |
| 340 | .append ($("<th />", { "class": "size", "scope": "col" }).text ("Size")) |
| 341 | .append ($("<th />", { "class": "modified", "scope": "col" }).text ("Modified")) |
| 342 | .append ($("<th />", { "class": "modified-by border-right", "scope": "col" }).text ("Modified By")))) |
| 343 | .append (tbody) |
| 344 | .append ($("<tfoot />") |
| 345 | .append ($("<tr />") |
| 346 | .append ($("<td />", { "colspan": "5", "class": "border-right border-left" }))))); |
| 347 | |
| 348 | for (var i = 0; i < data.length; i++) { |
| 349 | action = data[i]; |
| 350 | |
| 351 | row = $("<tr />"); |
| 352 | if (i%2) { row.addClass ("odd"); } |
| 353 | if (action.action=="DELETE") { |
| 354 | row.addClass ("delete"); |
Alexander Afanasyev | c15e016 | 2013-02-28 23:38:52 -0800 | [diff] [blame] | 355 | } |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 356 | else { |
| 357 | row.addClass ("with-context-menu"); |
| 358 | row.attr ("file_version", action.version); |
| 359 | row.attr ("file_hash", action.update.hash); |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 360 | row.attr ("file_seg_num", action.update.segNum); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 361 | row.attr ("file_modified_by", action.id.userName); |
| 362 | } |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 363 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 364 | row.attr ("filename", action.filename); |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 365 | |
| 366 | self = this; |
| 367 | if (PARAMS.item != action.filename) { |
| 368 | row.bind('click', function (e) { openHistoryForItem ($(this).attr ("filename")) }); |
| 369 | } |
| 370 | else { |
| 371 | row.bind('click', function (e) { |
| 372 | self.previewFile ($(this)); |
| 373 | }); |
| 374 | } |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 375 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 376 | row.bind('mouseenter mouseleave', function() { |
| 377 | $(this).toggleClass('highlighted'); |
| 378 | }); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 379 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 380 | row.append ($("<td />", { "class": "filename border-left" }) |
| 381 | .text (action.filename) |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 382 | .prepend ($("<img />", { "src": imgFullPath(fileExtension(action.filename)) }))); |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 383 | row.append ($("<td />", { "class": "version" }).text (action.version)); |
| 384 | row.append ($("<td />", { "class": "size" }).text (action.update?SegNumToFileSize (action.update.segNum):"")); |
| 385 | row.append ($("<td />", { "class": "timestamp" }).text (new Date (action.timestamp+"+00:00"))); // conversion from UTC timezone (we store action time in UTC) |
| 386 | row.append ($("<td />", { "class": "modified-by border-right" }) |
| 387 | .append ($("<userName />").text (action.id.userName)) |
| 388 | .append ($("<seqNo> /").text (action.id.seqNo))); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 389 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 390 | tbody = tbody.append (row); |
| 391 | } |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 392 | |
Alexander Afanasyev | 10babd2 | 2013-03-04 13:53:36 -0800 | [diff] [blame] | 393 | displayContent (newcontent, more, this.base_url (PAGE)); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 394 | |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 395 | self = this; |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 396 | $.contextMenu( 'destroy', ".with-context-menu" ); // cleanup |
| 397 | $.contextMenu({ |
| 398 | selector: ".with-context-menu", |
| 399 | items: { |
| 400 | "sep1": "---------", |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 401 | preview: {name: "Preview revision", |
| 402 | icon: "edit", // ned a better icon |
| 403 | callback: function(key, opt) { |
| 404 | self.previewFile (opt.$trigger); |
| 405 | }}, |
| 406 | "sep3": "---------", |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 407 | restore: {name: "Restore this revision", |
| 408 | icon: "cut", // need a better icon |
| 409 | callback: function(key, opt) { |
| 410 | filename = opt.$trigger.attr ("filename"); |
| 411 | version = opt.$trigger.attr ("file_version"); |
| 412 | hash = DataUtils.toNumbers (opt.$trigger.attr ("file_hash")); |
| 413 | console.log (hash); |
| 414 | modified_by = opt.$trigger.attr ("file_modified_by"); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 415 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 416 | $("<div />", { "title": "Restore version " + version + "?" }) |
| 417 | .append ($("<p />") |
| 418 | .append ($("<span />", { "class": "ui-icon ui-icon-alert", |
| 419 | "style": "float: left; margin: 0 7px 50px 0;" }), |
| 420 | $(document.createTextNode ("Are you sure you want restore version ")), |
| 421 | $("<green/>").text (version), |
| 422 | $(document.createTextNode (" by ")), |
| 423 | $("<green/>").text (modified_by))) |
| 424 | .dialog ({ |
| 425 | resizable: true, |
| 426 | height:200, |
| 427 | width:300, |
| 428 | modal: true, |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 429 | show: "blind", |
| 430 | hide: "fold", |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 431 | buttons: { |
| 432 | "Restore": function() { |
| 433 | self = $(this); |
| 434 | CHRONOSHARE.cmd_restore_file (filename, version, hash, function(didGetData, response) { |
| 435 | if (!didGetData || response != "OK") { |
| 436 | custom_alert (response); |
| 437 | } |
| 438 | console.log (response); |
| 439 | self.dialog ("close"); |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 440 | |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 441 | $.timer (function() {CHRONOSHARE.run ();}).once (1000); |
| 442 | }); |
| 443 | }, |
| 444 | Cancel: function() { |
| 445 | $(this).dialog ("close"); |
Alexander Afanasyev | 4c17b48 | 2013-03-02 01:32:35 -0800 | [diff] [blame] | 446 | } |
Alexander Afanasyev | 1663a41 | 2013-03-02 13:52:00 -0800 | [diff] [blame] | 447 | } |
| 448 | }); |
| 449 | // openHistoryForItem (opt.$trigger.attr ("filename")); |
| 450 | }}, |
| 451 | "sep2": "---------", |
| 452 | } |
| 453 | }); |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 454 | }, |
| 455 | |
Alexander Afanasyev | c24db64 | 2013-03-09 16:41:02 -0800 | [diff] [blame] | 456 | base_no_item_url: function (page) { |
| 457 | url = "#"+page+ |
| 458 | "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) + |
| 459 | "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder)); |
| 460 | return url; |
| 461 | }, |
| 462 | |
Alexander Afanasyev | 3c95c85 | 2013-03-01 18:58:50 -0800 | [diff] [blame] | 463 | base_url: function (page) { |
| 464 | url = "#"+page+ |
| 465 | "&user="+encodeURIComponent (encodeURIComponent (PARAMS.user)) + |
| 466 | "&folder="+encodeURIComponent (encodeURIComponent (PARAMS.folder)); |
| 467 | if (PARAMS.item !== undefined) { |
| 468 | url += "&item="+encodeURIComponent (encodeURIComponent (PARAMS.item)); |
| 469 | } |
| 470 | return url; |
Alexander Afanasyev | f63a514 | 2013-02-28 02:21:42 -0800 | [diff] [blame] | 471 | } |
| 472 | }); |
| 473 | |
| 474 | |
Alexander Afanasyev | fd5e627 | 2013-02-27 20:25:20 -0800 | [diff] [blame] | 475 | |
| 476 | |