blob: fcff0e6158e5edfb4d5c64e9f5b8898defb950fc [file] [log] [blame]
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08001Components.utils.import("chrome://modules/content/ndn-js.jsm");
Jeff Thompson6ad5c362012-12-27 17:57:02 -08002Components.utils.import("chrome://modules/content/NdnProtocolInfo.jsm");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08003
Jeff Thompson30779812013-02-04 23:59:45 -08004function ndnToolbarGetVersion(selector) {
5 try {
Jeff Thompsonbd829262012-11-30 22:28:37 -08006 if (window._content.document.location.protocol != "ndn:") {
Jeff Thompson35cfc262012-12-16 12:06:40 -08007 alert("The address must start with ndn:");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -08008 return;
9 }
10
Jeff Thompsonbd829262012-11-30 22:28:37 -080011 // Parse the same as in ndnProtocolService newChannel.
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080012 var uriParts = NdnProtocolInfo.splitUri(window._content.document.location.href);
Jeff Thompson8107ec82013-01-12 21:53:27 -080013 var name = new Name(uriParts.name);
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080014 var indexOfVersion = getIndexOfVersion(name);
15 if (indexOfVersion < 0) {
Jeff Thompsonbd829262012-11-30 22:28:37 -080016 alert("The ndn address does not have a version");
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080017 return;
18 }
Jeff Thompson30779812013-02-04 23:59:45 -080019
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 Thompson02b9f4d2012-11-29 14:40:57 -080034
35 var nameWithoutVersion = new Name(name.components.slice(0, indexOfVersion));
Jeff Thompson30779812013-02-04 23:59:45 -080036 var searchWithChildSelector = (uriParts.search == "" ? "?" : uriParts.search + "&") + childSelector;
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080037
Jeff Thompson8107ec82013-01-12 21:53:27 -080038 var uri = "ndn:" + nameWithoutVersion.to_uri() + searchWithChildSelector + uriParts.hash;
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080039 window._content.document.location = uri;
Jeff Thompson30779812013-02-04 23:59:45 -080040 } catch (ex) {
41 dump("ndnToolbarGetVersion exception: " + ex + "\n" + ex.stack);
42 }
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080043}
44
Jeff Thompson6ad5c362012-12-27 17:57:02 -080045/*
46 * Return the index of the last component that starts with 0xfd, or -1 if not found.
47 */
Jeff Thompson02b9f4d2012-11-29 14:40:57 -080048function 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 Thompson6ad5c362012-12-27 17:57:02 -080056
57/*
58 * This is called when the connected NDN hub changes.
59 */
60function onNdnHubChanged(host, port) {
61 document.getElementById("ndnHubLabel").setAttribute("value", "Hub: " + host + ":" + port);
62}
63
Jeff Thompsonb2f91ea2013-01-13 15:59:26 -080064window.addEventListener("load", function() { NdnProtocolInfo.addNdnHubChangedListener(onNdnHubChanged); }, false);