diff --git a/server/go/camlistored/ui/camli.js b/server/go/camlistored/ui/camli.js index 3a71b808f..6b5ec3ef2 100644 --- a/server/go/camlistored/ui/camli.js +++ b/server/go/camlistored/ui/camli.js @@ -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); +} diff --git a/server/go/camlistored/ui/permanode.html b/server/go/camlistored/ui/permanode.html index abe460765..4ff97e8fe 100644 --- a/server/go/camlistored/ui/permanode.html +++ b/server/go/camlistored/ui/permanode.html @@ -1,6 +1,9 @@ Permanode + + + @@ -8,8 +11,8 @@

Permanode

-

Name: - +

Name: +

diff --git a/server/go/camlistored/ui/permanode.js b/server/go/camlistored/ui/permanode.js index c7403c373..a768d2715 100644 --- a/server/go/camlistored/ui/permanode.js +++ b/server/go/camlistored/ui/permanode.js @@ -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); });