camlistored: make name-setting on permanode page functional

This commit is contained in:
Daniel Erat 2011-05-30 16:32:25 -07:00
parent 335f3187a8
commit 96cf953488
3 changed files with 77 additions and 7 deletions

View File

@ -35,6 +35,20 @@ function saneOpts(opts) {
return opts;
}
// Format |dateVal| as specified by RFC 3339.
function dateToRfc3339String(dateVal) {
// Return a string containing |num| zero-padded to |length| digits.
var pad = function(num, length) {
var numStr = "" + num;
while (numStr.length < length) {
numStr = "0" + numStr;
}
return numStr;
}
return dateVal.getUTCFullYear() + "-" + pad(dateVal.getUTCMonth(), 2) + "-" + pad(dateVal.getUTCDate(), 2) + "T" +
pad(dateVal.getUTCHours(), 2) + ":" + pad(dateVal.getUTCMinutes(), 2) + ":" + pad(dateVal.getUTCSeconds(), 2) + "Z";
}
var cachedCamliSigDiscovery;
// opts.success called with discovery object
@ -194,7 +208,7 @@ function camliCreateNewPermanode(opts) {
});
},
fail: function(msg) {
alert("sign fail: " + msg);
opts.fail("sign permanode fail: " + msg);
}
});
}
@ -215,3 +229,36 @@ function getQueryParam(key) {
function isPlausibleBlobRef(blobRef) {
return /^\w+-[a-f0-9]+$/.test(blobRef);
}
// Helper function for camliNewSetAttributeClaim() (and eventually, for
// similar functions to add or delete attributes).
function changeAttribute(permanode, claimType, attribute, value, opts) {
opts = saneOpts(opts);
var json = {
"camliVersion": 1,
"camliType": "claim",
"permaNode": permanode,
"claimType": claimType,
"claimDate": dateToRfc3339String(new Date()),
"attribute": attribute,
"value": value
};
camliSign(json, {
success: function(signedBlob) {
camliUploadString(signedBlob, {
success: opts.success,
fail: function(msg) {
opts.fail("upload " + claimType + " fail: " + msg);
}
});
},
fail: function(msg) {
opts.fail("sign " + claimType + " fail: " + msg);
}
});
}
// Create and upload a new set-attribute claim.
function camliNewSetAttributeClaim(permanode, attribute, value, opts) {
changeAttribute(permanode, "set-attribute", attribute, value, opts);
}

View File

@ -1,6 +1,9 @@
<html>
<head>
<title>Permanode</title>
<script type="text/javascript" src="base64.js"></script>
<script type="text/javascript" src="Crypto.js"></script>
<script type="text/javascript" src="SHA1.js"></script>
<script type="text/javascript" src="camli.js"></script>
<script type="text/javascript" src="?camli.mode=config&cb=onConfiguration"></script>
<script type="text/javascript" src="permanode.js"></script>
@ -8,8 +11,8 @@
<body>
<h1>Permanode</h1>
<form>
<p>Name: <input type="inputName" size="30" value="(loading)" disabled="true" />
<input type="button" id="btnSave" value="Save" disabled="true" />
<p>Name: <input type="text" id="inputName" size="30" value="(loading)" />
<input type="button" id="btnSave" value="Save" />
<p><span id="permanode"></span></p>
</form>

View File

@ -20,9 +20,29 @@ function getPermanodeParam() {
return (blobRef && isPlausibleBlobRef(blobRef)) ? blobRef : null;
}
function btnSaveName(e) {
var inputName = document.getElementById("inputName");
if (!inputName) {
alert("missing inputName");
}
camliNewSetAttributeClaim(
getPermanodeParam(),
"name",
inputName.value,
{
fail: function(msg) { alert(msg); }
});
}
window.addEventListener("load", function (e) {
var permanode = getPermanodeParam();
if (permanode) {
document.getElementById('permanode').innerText = permanode;
}
var permanode = getPermanodeParam();
if (permanode) {
document.getElementById('permanode').innerText = permanode;
}
var btnSave = document.getElementById("btnSave");
if (!btnSave) {
alert("missing btnSave");
}
btnSave.addEventListener("click", btnSaveName);
});