/*
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() {
var blobRef = Camli.getQueryParam('p');
return (blobRef && Camli.isPlausibleBlobRef(blobRef)) ? blobRef : null;
}
function handleFormTitleSubmit(e) {
e.stopPropagation();
e.preventDefault();
var inputTitle = document.getElementById("inputTitle");
inputTitle.disabled = true;
var btnSaveTitle = document.getElementById("btnSaveTitle");
btnSaveTitle.disabled = true;
var startTime = new Date();
camliNewSetAttributeClaim(
getPermanodeParam(),
"title",
inputTitle.value,
{
success: function() {
var elapsedMs = new Date().getTime() - startTime.getTime();
setTimeout(function() {
inputTitle.disabled = false;
btnSaveTitle.disabled = false;
buildPermanodeUi();
}, Math.max(250 - elapsedMs, 0));
},
fail: function(msg) {
alert(msg);
inputTitle.disabled = false;
btnSaveTitle.disabled = false;
}
});
}
function handleFormTagsSubmit(e) {
e.stopPropagation();
e.preventDefault();
var input = document.getElementById("inputNewTag");
var btn = document.getElementById("btnAddTag");
if (input.value == "") {
return;
}
input.disabled = true;
btn.disabled = true;
var startTime = new Date();
var tags = input.value.split(/\s*,\s*/);
var nRemain = tags.length;
var oneDone = function() {
nRemain--;
if (nRemain == 0) {
var elapsedMs = new Date().getTime() - startTime.getTime();
setTimeout(function() {
input.value = '';
input.disabled = false;
btn.disabled = false;
buildPermanodeUi();
}, 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();
}
});
}
}
function handleFormAccessSubmit(e) {
e.stopPropagation();
e.preventDefault();
var selectAccess = document.getElementById("selectAccess");
selectAccess.disabled = true;
var btnSaveAccess = document.getElementById("btnSaveAccess");
btnSaveAccess.disabled = true;
var operation = camliNewDelAttributeClaim;
var value = "";
if (selectAccess.value != "private") {
operation = camliNewSetAttributeClaim;
value = selectAccess.value;
}
var startTime = new Date();
operation(
getPermanodeParam(),
"camliAccess",
value,
{
success: function() {
var elapsedMs = new Date().getTime() - startTime.getTime();
setTimeout(function() {
selectAccess.disabled = false;
btnSaveAccess.disabled = false;
}, Math.max(250 - elapsedMs, 0));
},
fail: function(msg) {
alert(msg);
selectAccess.disabled = false;
btnSaveAccess.disabled = false;
}
});
}
function deleteTagFunc(tag, strikeEle, removeEle) {
return function(e) {
strikeEle.innerHTML = "" + strikeEle.innerHTML + "";
camliNewDelAttributeClaim(
getPermanodeParam(),
"tag",
tag,
{
success: function() {
removeEle.parentNode.removeChild(removeEle);
},
fail: function(msg) {
alert(msg);
}
});
};
}
function onTypeChange() {
var sel = document.getElementById("type");
var dnd = document.getElementById("dnd");
var btnGallery = document.getElementById("btnGallery");
if (sel.value == "collection" || sel.value == "") {
dnd.style.display = "block";
btnGallery.style.visibility = 'visible';
} else {
dnd.style.display = "none";
btnGallery.style.visibility = 'hidden';
}
}
function handleFiles(files) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
startFileUpload(file);
}
}
function startFileUpload(file) {
var dnd = document.getElementById("dnd");
var up = document.createElement("div");
up.className= 'camli-dnd-item';
dnd.appendChild(up);
var info = "name=" + file.name + " size=" + file.size + "; type=" + file.type;
var setStatus = function(status) {
up.innerHTML = info + " " + status;
};
setStatus("(scanning)");
var onFail = function(msg) {
up.innerHTML = info + " fail: ";
up.appendChild(document.createTextNode(msg));
};
var onGotFileSchemaRef = function(fileref) {
setStatus(" fileref: " + fileref + "");
camliCreateNewPermanode(
{
success: function(filepn) {
var doneWithAll = function() {
setStatus("- done");
buildPermanodeUi();
};
var addMemberToParent = function() {
setStatus("adding member");
camliNewAddAttributeClaim(getPermanodeParam(), "camliMember", filepn, { success: doneWithAll, fail: onFail });
};
var makePermanode = function() {
setStatus("making permanode");
camliNewSetAttributeClaim(filepn, "camliContent", fileref, { success: addMemberToParent, fail: onFail });
};
makePermanode();
},
fail: onFail
});
};
camliUploadFile(file, {
onContentsRef: function(contentsRef) {
setStatus("(checking for dup of " + contentsRef + ")");
},
success: onGotFileSchemaRef,
fail: onFail
});
}
function onFileFormSubmit(e) {
e.stopPropagation();
e.preventDefault();
handleFiles(document.getElementById("fileInput").files);
}
function $(id) { return document.getElementById(id) }
function onFileInputChange(e) {
var s = "";
var files = $("fileInput").files;
for (var i = 0; i < files.length; i++) {
var file = files[i];
s += "
" + file.name + "
"; } var fl = $("filelist"); fl.innerHTML = s; } function setupFilesHandlers() { var dnd = document.getElementById("dnd"); document.getElementById("fileForm").addEventListener("submit", onFileFormSubmit); document.getElementById("fileInput").addEventListener("change", onFileInputChange); var stop = function(e) { this.classList && this.classList.add('camli-dnd-over'); e.stopPropagation(); e.preventDefault(); }; dnd.addEventListener("dragenter", stop, false); dnd.addEventListener("dragover", stop, false); dnd.addEventListener("dragleave", function() { this.classList.remove('camli-dnd-over'); }, false); var drop = function(e) { this.classList.remove('camli-dnd-over'); stop(e); var dt = e.dataTransfer; var files = dt.files; document.getElementById("info").innerHTML = ""; handleFiles(files); }; dnd.addEventListener("drop", drop, false); } // member: child permanode function deleteMember(member, strikeEle, removeEle) { return function(e) { strikeEle.innerHTML = "