From 4b66f0dcca336f89e1df7fe682f59e48f48016c9 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Sun, 29 May 2011 20:22:24 -0700 Subject: [PATCH] UI work; permanode creation (but not upload) --- server/go/camlistored/ui/disco.html | 1 + server/go/camlistored/ui/index.html | 18 +++- server/go/camlistored/ui/sigdebug.js | 76 ++++++++++++++ server/go/camlistored/ui/signing.html | 1 + server/go/camlistored/ui/ui.js | 141 +++++++++++++++----------- 5 files changed, 175 insertions(+), 62 deletions(-) create mode 100644 server/go/camlistored/ui/sigdebug.js diff --git a/server/go/camlistored/ui/disco.html b/server/go/camlistored/ui/disco.html index 78bfd7a7c..f2c990453 100644 --- a/server/go/camlistored/ui/disco.html +++ b/server/go/camlistored/ui/disco.html @@ -2,6 +2,7 @@ Camlistored UI + diff --git a/server/go/camlistored/ui/index.html b/server/go/camlistored/ui/index.html index d5a32b07e..53325a9ef 100644 --- a/server/go/camlistored/ui/index.html +++ b/server/go/camlistored/ui/index.html @@ -1,15 +1,31 @@ Camlistored UI + + + +

Camlistored UI

[Debug: discovery | signing | -search]

+search | +jsfiles]

+ + - create a new item or collection

Upload

diff --git a/server/go/camlistored/ui/sigdebug.js b/server/go/camlistored/ui/sigdebug.js new file mode 100644 index 000000000..fab3a5232 --- /dev/null +++ b/server/go/camlistored/ui/sigdebug.js @@ -0,0 +1,76 @@ +var sigdisco = null; + +function discoverJsonSign() { + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return; } + if (xhr.status != 200) { + console.log("no status 200; got " + xhr.status); + return; + } + sigdisco = JSON.parse(xhr.responseText); + document.getElementById("sigdiscores").innerHTML = JSON.stringify(sigdisco); + }; + xhr.open("GET", disco.jsonSignRoot + "/camli/sig/discovery", true); + xhr.send(); +} + +function addKeyRef() { + if (!sigdisco) { + alert("must do jsonsign discovery first"); + return; + } + clearta = document.getElementById("clearjson"); + var j; + try { + j = JSON.parse(clearta.value); + } catch (x) { + alert(x); + return + } + j.camliSigner = sigdisco.publicKeyBlobRef; + clearta.value = JSON.stringify(j); +} + +function doSign() { + if (!sigdisco) { + alert("must do jsonsign discovery first"); + return; + } + clearta = document.getElementById("clearjson"); + + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return; } + if (xhr.status != 200) { + alert("got status " + xhr.status) + return; + } + document.getElementById("signedjson").value = xhr.responseText; + }; + xhr.open("POST", sigdisco.signHandler, true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send("json=" + encodeURIComponent(clearta.value)); +} + +function doVerify() { + if (!sigdisco) { + alert("must do jsonsign discovery first"); + return; + } + + signedta = document.getElementById("signedjson"); + + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return; } + if (xhr.status != 200) { + alert("got status " + xhr.status) + return; + } + document.getElementById("verifyinfo").innerHTML = "
" + xhr.responseText + "
"; + }; + xhr.open("POST", sigdisco.verifyHandler, true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send("sjson=" + encodeURIComponent(signedta.value)); +} diff --git a/server/go/camlistored/ui/signing.html b/server/go/camlistored/ui/signing.html index ea7afcd9b..e9ba3b3ce 100644 --- a/server/go/camlistored/ui/signing.html +++ b/server/go/camlistored/ui/signing.html @@ -2,6 +2,7 @@ Camlistored UI + diff --git a/server/go/camlistored/ui/ui.js b/server/go/camlistored/ui/ui.js index 01aadc979..7fc9c586f 100644 --- a/server/go/camlistored/ui/ui.js +++ b/server/go/camlistored/ui/ui.js @@ -38,82 +38,74 @@ function discover() { xhr.send(); } -var sigdisco = null; +function saneOpts(opts) { + if (!opts) { + opts = {} + } + if (!opts.success) { + opts.success = function() {}; + } + if (!opts.fail) { + opts.fail = function() {}; + } + return opts; +} -function discoverJsonSign() { +var cachedCamliSigDiscovery; + +function camliSigDiscovery(opts) { + opts = saneOpts(opts); + if (cachedCamliSigDiscovery) { + opts.success(cachedCamliSigDiscovery); + return; + } var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState != 4) { return; } if (xhr.status != 200) { - console.log("no status 200; got " + xhr.status); + opts.fail("no status 200; got " + xhr.status); return; } sigdisco = JSON.parse(xhr.responseText); - document.getElementById("sigdiscores").innerHTML = JSON.stringify(sigdisco); + cachedCamliSigDiscovery = sigdisco; + opts.success(sigdisco); }; xhr.open("GET", disco.jsonSignRoot + "/camli/sig/discovery", true); xhr.send(); } -function addKeyRef() { - if (!sigdisco) { - alert("must do jsonsign discovery first"); - return; - } - clearta = document.getElementById("clearjson"); - var j; - try { - j = JSON.parse(clearta.value); - } catch (x) { - alert(x); - return - } - j.camliSigner = sigdisco.publicKeyBlobRef; - clearta.value = JSON.stringify(j); +function camliSign(clearObj, opts) { + opts = saneOpts(opts); + + camliSigDiscovery( + { + success: function(sigConf) { + if (!sigConf.publicKeyBlobRef) { + opts.fail("Missing sigConf.publicKeyBlobRef"); + return; + } + clearObj.camliSigner = sigConf.publicKeyBlobRef; + clearText = JSON.stringify(clearObj, null, 2); + + var xhr = new XMLHttpRequest(); + xhr.onreadystatechange = function() { + if (xhr.readyState != 4) { return; } + if (xhr.status != 200) { + opts.fail("got status " + xhr.status); + return; + } + opts.success(xhr.responseText); + }; + xhr.open("POST", sigConf.signHandler, true); + xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); + xhr.send("json=" + encodeURIComponent(clearText)); + }, + fail: function(errMsg) { + opts.fail(errMsg); + } + }); } -function doSign() { - if (!sigdisco) { - alert("must do jsonsign discovery first"); - return; - } - clearta = document.getElementById("clearjson"); - - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function() { - if (xhr.readyState != 4) { return; } - if (xhr.status != 200) { - alert("got status " + xhr.status) - return; - } - document.getElementById("signedjson").value = xhr.responseText; - }; - xhr.open("POST", sigdisco.signHandler, true); - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.send("json=" + encodeURIComponent(clearta.value)); -} - -function doVerify() { - if (!sigdisco) { - alert("must do jsonsign discovery first"); - return; - } - - signedta = document.getElementById("signedjson"); - - var xhr = new XMLHttpRequest(); - xhr.onreadystatechange = function() { - if (xhr.readyState != 4) { return; } - if (xhr.status != 200) { - alert("got status " + xhr.status) - return; - } - document.getElementById("verifyinfo").innerHTML = "
" + xhr.responseText + "
"; - }; - xhr.open("POST", sigdisco.verifyHandler, true); - xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); - xhr.send("sjson=" + encodeURIComponent(signedta.value)); -} function search() { var xhr = new XMLHttpRequest(); @@ -128,3 +120,30 @@ function search() { xhr.open("GET", disco.searchRoot + "camli/search", true); xhr.send(); } + + + +function createNewPermanode() { + var json = { + "camliVersion": 1, + "camliType": "permanode", + "random": ""+Math.random() + }; + camliSign(json, { + success: function(got) { + alert("got signed: " + got); + }, + fail: function(msg) { + alert("sign fail: " + msg); + } + }); +} + +function camliOnload(e) { + var btnNew = document.getElementById("btnNew"); + if (btnNew) { + btnNew.addEventListener("click", createNewPermanode); + } +} + +window.addEventListener("load", camliOnload); \ No newline at end of file