2011-05-30 04:39:23 +00:00
|
|
|
/*
|
|
|
|
Copyright 2011 Google Inc.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Gets the |p| query parameter, assuming that it looks like a blobref.
|
|
|
|
function getPermanodeParam() {
|
2011-05-30 23:41:56 +00:00
|
|
|
var blobRef = getQueryParam('p');
|
|
|
|
return (blobRef && isPlausibleBlobRef(blobRef)) ? blobRef : null;
|
2011-05-30 04:39:23 +00:00
|
|
|
}
|
2011-05-30 20:36:17 +00:00
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
function handleFormTitleSubmit(e) {
|
2011-05-31 01:22:13 +00:00
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
var inputTitle = document.getElementById("inputTitle");
|
|
|
|
inputTitle.disabled = "disabled";
|
2011-05-31 01:22:13 +00:00
|
|
|
var btnSave = document.getElementById("btnSave");
|
|
|
|
btnSave.disabled = "disabled";
|
|
|
|
|
|
|
|
var startTime = new Date();
|
|
|
|
|
2011-05-30 23:32:25 +00:00
|
|
|
camliNewSetAttributeClaim(
|
|
|
|
getPermanodeParam(),
|
2011-06-01 06:25:24 +00:00
|
|
|
"title",
|
|
|
|
inputTitle.value,
|
2011-05-30 23:32:25 +00:00
|
|
|
{
|
2011-05-31 01:22:13 +00:00
|
|
|
success: function() {
|
|
|
|
var elapsedMs = new Date().getTime() - startTime.getTime();
|
|
|
|
setTimeout(function() {
|
2011-06-01 06:25:24 +00:00
|
|
|
inputTitle.disabled = null;
|
2011-05-31 01:22:13 +00:00
|
|
|
btnSave.disabled = null;
|
|
|
|
}, Math.max(250 - elapsedMs, 0));
|
|
|
|
},
|
|
|
|
fail: function(msg) {
|
|
|
|
alert(msg);
|
2011-06-01 06:25:24 +00:00
|
|
|
inputTitle.disabled = null;
|
2011-05-31 01:22:13 +00:00
|
|
|
btnSave.disabled = null;
|
|
|
|
}
|
2011-05-30 23:32:25 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
function handleFormTagsSubmit(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var input = document.getElementById("inputNewTag");
|
|
|
|
var btn = document.getElementById("btnAddTag");
|
2011-06-04 17:10:45 +00:00
|
|
|
|
|
|
|
if (input.value == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
input.disabled = "disabled";
|
|
|
|
btn.disabled = "disabled";
|
|
|
|
|
|
|
|
var startTime = new Date();
|
|
|
|
|
2011-06-04 17:10:45 +00:00
|
|
|
var tags = input.value.split(/\s*,\s*/);
|
|
|
|
var nRemain = tags.length;
|
2011-06-01 06:25:24 +00:00
|
|
|
|
2011-06-04 17:10:45 +00:00
|
|
|
var oneDone = function() {
|
|
|
|
nRemain--;
|
|
|
|
if (nRemain == 0) {
|
|
|
|
var elapsedMs = new Date().getTime() - startTime.getTime();
|
|
|
|
setTimeout(function() {
|
|
|
|
input.disabled = null;
|
|
|
|
btn.disabled = null;
|
|
|
|
}, Math.max(250 - elapsedMs, 0));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
for (idx in tags) {
|
|
|
|
var tag = tags[idx];
|
|
|
|
camliNewAddAttributeClaim(
|
|
|
|
getPermanodeParam(),
|
|
|
|
"tag",
|
|
|
|
tag,
|
|
|
|
{
|
|
|
|
success: oneDone,
|
|
|
|
fail: function(msg) {
|
|
|
|
alert(msg);
|
|
|
|
oneDone();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2011-06-01 06:25:24 +00:00
|
|
|
}
|
|
|
|
|
2011-06-03 21:16:00 +00:00
|
|
|
function deleteTagFunc(tag, strikeEle, removeEle) {
|
|
|
|
return function(e) {
|
|
|
|
strikeEle.innerHTML = "<s>" + strikeEle.innerHTML + "</s>";
|
|
|
|
camliNewDelAttributeClaim(
|
|
|
|
getPermanodeParam(),
|
|
|
|
"tag",
|
|
|
|
tag,
|
|
|
|
{
|
|
|
|
success: function() {
|
|
|
|
removeEle.innerHTML = "";
|
|
|
|
},
|
|
|
|
fail: function(msg) {
|
|
|
|
alert(msg);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2011-06-07 17:23:11 +00:00
|
|
|
function onTypeChange(e) {
|
|
|
|
var sel = document.getElementById("type");
|
|
|
|
var dnd = document.getElementById("dnd");
|
|
|
|
|
2011-06-09 01:59:56 +00:00
|
|
|
if (sel.value == "collection" || sel.value == "") {
|
2011-06-07 17:23:11 +00:00
|
|
|
dnd.style.display = "block";
|
|
|
|
} else {
|
|
|
|
dnd.style.display = "none";
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastFiles;
|
|
|
|
function handleFiles(files) {
|
|
|
|
lastFiles = files;
|
|
|
|
|
|
|
|
for (var i = 0; i < files.length; i++) {
|
|
|
|
var file = files[i];
|
2011-06-09 22:00:20 +00:00
|
|
|
startFileUpload(file);
|
2011-06-07 17:23:11 +00:00
|
|
|
}
|
2011-06-09 22:00:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function startFileUpload(file) {
|
|
|
|
var dnd = document.getElementById("dnd");
|
|
|
|
var up = document.createElement("div");
|
|
|
|
up.setAttribute("class", "fileupload");
|
|
|
|
dnd.appendChild(up);
|
|
|
|
var info = "name=" + file.name + " size=" + file.size + "; type=" + file.type;
|
2011-06-10 05:18:22 +00:00
|
|
|
|
|
|
|
var setStatus = function(status) {
|
|
|
|
up.innerHTML = info + " " + status;
|
|
|
|
};
|
|
|
|
setStatus("(scanning)");
|
|
|
|
|
2011-06-09 22:00:20 +00:00
|
|
|
var contentsRef; // set later
|
|
|
|
|
|
|
|
var onFail = function(msg) {
|
|
|
|
up.innerHTML = info + " <b>fail:</b> ";
|
|
|
|
up.appendChild(document.createTextNode(msg));
|
|
|
|
};
|
|
|
|
|
2011-06-10 05:05:57 +00:00
|
|
|
var onGotFileSchemaRef = function(fileref) {
|
2011-06-10 05:18:22 +00:00
|
|
|
setStatus(" <b>fileref: " + fileref + "</b>");
|
|
|
|
camliCreateNewPermanode(
|
|
|
|
{
|
|
|
|
success: function(filepn) {
|
|
|
|
var doneWithAll = function() {
|
|
|
|
setStatus("- done");
|
|
|
|
};
|
|
|
|
var addMember = function() {
|
|
|
|
setStatus("adding member");
|
|
|
|
camliNewAddAttributeClaim(getPermanodeParam(), "member", filepn, { success: doneWithAll, fail: onFail });
|
|
|
|
};
|
|
|
|
var makePermanode = function() {
|
|
|
|
setStatus("making permanode");
|
|
|
|
camliNewSetAttributeClaim(filepn, "camliContent", fileref, { success: addMember, fail: onFail });
|
|
|
|
};
|
|
|
|
makePermanode();
|
|
|
|
},
|
|
|
|
fail: onFail
|
|
|
|
});
|
2011-06-09 22:00:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
var fr = new FileReader();
|
|
|
|
fr.onload = function() {
|
|
|
|
dataurl = fr.result;
|
|
|
|
comma = dataurl.indexOf(",")
|
|
|
|
if (comma != -1) {
|
|
|
|
b64 = dataurl.substring(comma + 1);
|
|
|
|
var arrayBuffer = Base64.decode(b64).buffer;
|
|
|
|
var hash = Crypto.SHA1(new Uint8Array(arrayBuffer, 0));
|
|
|
|
|
|
|
|
contentsRef = "sha1-" + hash;
|
2011-06-10 05:18:22 +00:00
|
|
|
setStatus("(checking for dup of " + contentsRef + ")");
|
2011-06-10 05:05:57 +00:00
|
|
|
camliUploadFileHelper(file, contentsRef, { success: onGotFileSchemaRef, fail: onFail });
|
2011-06-09 22:00:20 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
fr.readAsDataURL(file);
|
2011-06-07 17:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function onFileFormSubmit(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
alert("TODO: upload");
|
|
|
|
}
|
|
|
|
|
|
|
|
function onFileInputChange(e) {
|
|
|
|
handleFiles(document.getElementById("fileInput").files);
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupFilesHandlers(e) {
|
|
|
|
var dnd = document.getElementById("dnd");
|
|
|
|
document.getElementById("fileForm").addEventListener("submit", onFileFormSubmit);
|
|
|
|
document.getElementById("fileInput").addEventListener("change", onFileInputChange);
|
|
|
|
|
|
|
|
stop = function(e) {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
|
|
|
};
|
|
|
|
dnd.addEventListener("dragenter", stop, false);
|
|
|
|
dnd.addEventListener("dragover", stop, false);
|
|
|
|
|
|
|
|
drop = function(e) {
|
|
|
|
stop(e);
|
|
|
|
var dt = e.dataTransfer;
|
|
|
|
var files = dt.files;
|
|
|
|
document.getElementById("info").innerHTML = "";
|
|
|
|
handleFiles(files);
|
|
|
|
};
|
|
|
|
dnd.addEventListener("drop", drop, false);
|
|
|
|
}
|
|
|
|
|
2011-05-30 20:36:17 +00:00
|
|
|
window.addEventListener("load", function (e) {
|
2011-05-30 23:32:25 +00:00
|
|
|
var permanode = getPermanodeParam();
|
|
|
|
if (permanode) {
|
2011-06-01 06:25:24 +00:00
|
|
|
document.getElementById('permanode').innerHTML = "<a href='./?p=" + permanode + "'>" + permanode + "</a>";
|
|
|
|
document.getElementById('permanodeBlob').innerHTML = "<a href='./?b=" + permanode + "'>view blob</a>";
|
2011-05-30 23:32:25 +00:00
|
|
|
}
|
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
var formTitle = document.getElementById("formTitle");
|
|
|
|
formTitle.addEventListener("submit", handleFormTitleSubmit);
|
|
|
|
var formTags = document.getElementById("formTags");
|
|
|
|
formTags.addEventListener("submit", handleFormTagsSubmit);
|
2011-05-31 01:22:13 +00:00
|
|
|
|
2011-06-07 17:23:11 +00:00
|
|
|
var selectType = document.getElementById("type");
|
|
|
|
selectType.addEventListener("change", onTypeChange);
|
|
|
|
|
|
|
|
setupFilesHandlers();
|
|
|
|
|
2011-05-31 01:22:13 +00:00
|
|
|
camliDescribeBlob(permanode, {
|
|
|
|
success: function(jres) {
|
|
|
|
if (!jres[permanode]) {
|
|
|
|
alert("didn't get blob " + permanode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var permanodeObject = jres[permanode].permanode;
|
|
|
|
if (!permanodeObject) {
|
|
|
|
alert("blob " + permanode + " isn't a permanode");
|
|
|
|
return;
|
|
|
|
}
|
2011-05-31 01:34:05 +00:00
|
|
|
|
2011-06-01 06:25:24 +00:00
|
|
|
var inputTitle = document.getElementById("inputTitle");
|
|
|
|
inputTitle.value =
|
|
|
|
(permanodeObject.attr.title && permanodeObject.attr.title.length == 1) ?
|
|
|
|
permanodeObject.attr.title[0] :
|
2011-05-31 01:34:05 +00:00
|
|
|
"";
|
2011-06-01 06:25:24 +00:00
|
|
|
inputTitle.disabled = null;
|
2011-05-31 01:34:05 +00:00
|
|
|
|
2011-06-03 21:16:00 +00:00
|
|
|
|
|
|
|
var spanTags = document.getElementById("spanTags");
|
|
|
|
while (spanTags.firstChild) {
|
|
|
|
spanTags.removeChild(spanTags.firstChild);
|
|
|
|
}
|
|
|
|
|
2011-06-10 22:03:01 +00:00
|
|
|
var membersDiv = document.getElementById("members");
|
|
|
|
membersDiv.innerHTML = "";
|
|
|
|
var members = permanodeObject.attr.member;
|
|
|
|
if (members && members.length > 0) {
|
|
|
|
var membersUl = document.createElement("ul");
|
|
|
|
for (idx in members) {
|
|
|
|
var member = members[idx];
|
|
|
|
var memberLi = document.createElement("li");
|
|
|
|
var memberLink = document.createElement("a");
|
|
|
|
memberLi.appendChild(memberLink);
|
|
|
|
memberLink.href = "./?p=" + member;
|
|
|
|
memberLink.innerText = member;
|
|
|
|
membersUl.appendChild(memberLi);
|
|
|
|
}
|
|
|
|
membersDiv.appendChild(document.createTextNode("Members:"));
|
|
|
|
membersDiv.appendChild(membersUl);
|
|
|
|
}
|
|
|
|
|
2011-06-03 21:16:00 +00:00
|
|
|
var tags = permanodeObject.attr.tag;
|
|
|
|
for (idx in tags) {
|
|
|
|
var tagSpan = document.createElement("span");
|
|
|
|
|
|
|
|
if (idx > 0) {
|
|
|
|
tagSpan.appendChild(document.createTextNode(", "));
|
|
|
|
}
|
|
|
|
var tagLink = document.createElement("i");
|
|
|
|
var tag = tags[idx];
|
|
|
|
tagLink.innerText = tags[idx];
|
|
|
|
tagSpan.appendChild(tagLink);
|
|
|
|
tagSpan.appendChild(document.createTextNode(" ["));
|
|
|
|
var delLink = document.createElement("a");
|
|
|
|
delLink.href = '#';
|
|
|
|
delLink.innerText = "X";
|
|
|
|
delLink.addEventListener("click", deleteTagFunc(tag, tagLink, tagSpan));
|
|
|
|
tagSpan.appendChild(delLink);
|
|
|
|
tagSpan.appendChild(document.createTextNode("]"));
|
|
|
|
|
|
|
|
spanTags.appendChild(tagSpan);
|
|
|
|
}
|
|
|
|
|
2011-05-31 01:34:05 +00:00
|
|
|
var btnSave = document.getElementById("btnSave");
|
|
|
|
btnSave.disabled = null;
|
2011-05-31 01:22:13 +00:00
|
|
|
},
|
|
|
|
failure: function(msg) { alert("failed to get blob description: " + msg); }
|
|
|
|
});
|
2011-05-30 20:36:17 +00:00
|
|
|
});
|