Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 1 | Components.utils.import("chrome://modules/content/ndn-js.jsm"); |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 2 | Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 3 | |
Jeff Thompson | 3077981 | 2013-02-04 23:59:45 -0800 | [diff] [blame] | 4 | function ndnToolbarGetVersion(selector) { |
| 5 | try { |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 6 | if (window._content.document.location.protocol != "ndn:") { |
Jeff Thompson | 35cfc26 | 2012-12-16 12:06:40 -0800 | [diff] [blame] | 7 | alert("The address must start with ndn:"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 8 | return; |
| 9 | } |
| 10 | |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 11 | // Parse the same as in ndnProtocolService newChannel. |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 12 | var uriParts = NdnProtocolInfo.splitUri(window._content.document.location.href); |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 13 | var name = new Name(uriParts.name); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 14 | var indexOfVersion = getIndexOfVersion(name); |
| 15 | if (indexOfVersion < 0) { |
Jeff Thompson | bd82926 | 2012-11-30 22:28:37 -0800 | [diff] [blame] | 16 | alert("The ndn address does not have a version"); |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 17 | return; |
| 18 | } |
Jeff Thompson | 3077981 | 2013-02-04 23:59:45 -0800 | [diff] [blame] | 19 | |
| 20 | var escapedVersion = Name.toEscapedString(name.components[indexOfVersion]); |
| 21 | |
| 22 | var childSelector; |
| 23 | if (selector == "earliest") |
| 24 | childSelector = "ndn.ChildSelector=0"; |
| 25 | else if (selector == "latest") |
| 26 | childSelector = "ndn.ChildSelector=1"; |
| 27 | else if (selector == "previous") |
| 28 | childSelector = "ndn.ChildSelector=1&ndn.Exclude=" + escapedVersion + ",*"; |
| 29 | else if (selector == "next") |
| 30 | childSelector = "ndn.ChildSelector=0&ndn.Exclude=*," + escapedVersion; |
| 31 | else |
| 32 | // Don't expect this to happen. |
| 33 | return; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 34 | |
| 35 | var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion)); |
Jeff Thompson | 3077981 | 2013-02-04 23:59:45 -0800 | [diff] [blame] | 36 | var searchWithChildSelector = (uriParts.search == "" ? "?" : uriParts.search + "&") + childSelector; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 37 | |
Jeff Thompson | 8107ec8 | 2013-01-12 21:53:27 -0800 | [diff] [blame] | 38 | var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash; |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 39 | window._content.document.location = uri; |
Jeff Thompson | 3077981 | 2013-02-04 23:59:45 -0800 | [diff] [blame] | 40 | } catch (ex) { |
| 41 | dump("ndnToolbarGetVersion exception: " + ex + "\n" + ex.stack); |
| 42 | } |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 43 | } |
| 44 | |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 45 | /* |
| 46 | * Return the index of the last component that starts with 0xfd, or -1 if not found. |
| 47 | */ |
Jeff Thompson | 02b9f4d | 2012-11-29 14:40:57 -0800 | [diff] [blame] | 48 | function getIndexOfVersion(name) { |
| 49 | for (var i = name.components.length - 1; i >= 0; --i) { |
| 50 | if (name.components[i].length >= 1 && name.components[i][0] == 0xfd) |
| 51 | return i; |
| 52 | } |
| 53 | |
| 54 | return -1; |
| 55 | } |
Jeff Thompson | 6ad5c36 | 2012-12-27 17:57:02 -0800 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * This is called when the connected NDN hub changes. |
| 59 | */ |
| 60 | function onNdnHubChanged(host, port) { |
| 61 | document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port); |
| 62 | } |
| 63 | |
Jeff Thompson | b2f91ea | 2013-01-13 15:59:26 -0800 | [diff] [blame] | 64 | window.addEventListener("load", function() { NdnProtocolInfo.addNdnHubChangedListener(onNdnHubChanged); }, false); |