Denote/denote/static/js/denote.js

184 lines
4.4 KiB
JavaScript
Raw Normal View History

2015-09-24 18:31:03 +02:00
function setPreference(cname, cvalue) {
var params = "set=1" + "&name=" + cname + "&value=" + cvalue;
var req = new XMLHttpRequest();
req.open('POST', '/preferences', true);
req.setRequestHeader("X-CSRFToken", get_csrf_token());
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.onreadystatechange = function (aEvt) {
if (req.readyState == 4 && req.status != 200) {
alert('Error setting preference');
}
};
req.send(params);
}
function getPreference(cname) {
var params = "get=1" + "&name=" + cname;
var req = new XMLHttpRequest();
req.open('POST', '/preferences', false);
req.setRequestHeader("X-CSRFToken", get_csrf_token());
req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
req.send(params);
if(req.status == 200)
return JSON.parse(req.responseText)[cname];
return null;
}
function set_visible(id, visible, display)
2015-09-24 18:31:03 +02:00
{
widget = document.getElementById(id);
if (visible)
{
widget.style.display = display;
widget.style.visibility = "visible";
}
else
{
widget.style.display = "none";
widget.style.visibility = "hidden";
}
return widget;
}
function updateHiddenCategories(cat_id, add)
{
if (hidden_categories == null)
hidden_categories = [];
for(i=0; i<hidden_categories.length; i++)
{
if (hidden_categories[i] == cat_id)
{
if (add) return;
hidden_categories.splice(i, 1);
break;
}
}
if (add)
hidden_categories.push(cat_id);
setPreference('hidden_categories', hidden_categories.join(","));
}
function hide_category(cat_id, update_cookie)
2015-09-24 18:31:03 +02:00
{
set_visible("content_" + cat_id, false, "");
set_visible("minus_" + cat_id, false, "");
2015-09-24 18:31:03 +02:00
set_visible("plus_" + cat_id, true, "inline-block");
if (update_cookie)
updateHiddenCategories(cat_id, true);
}
function show_category(cat_id, update_cookie)
2015-09-24 18:31:03 +02:00
{
set_visible("content_" + cat_id, true, "block");
2015-09-24 18:31:03 +02:00
set_visible("minus_" + cat_id, true, "inline-block");
set_visible("plus_" + cat_id, false, "");
2015-09-24 18:31:03 +02:00
if (update_cookie)
updateHiddenCategories(cat_id, false);
}
function category_setup()
{
if (hidden_categories == null)
hidden_categories = [];
categories = document.getElementById("categories");
for(i=0; i<categories.childNodes.length; i++)
{
category = categories.childNodes[i];
if (category.nodeType != Node.ELEMENT_NODE) continue;
categoryId = category.getAttribute("category_id");
2016-06-14 11:09:11 +02:00
if (categoryId == null) continue;
2015-09-24 18:31:03 +02:00
hide = false;
for(a=0; a<hidden_categories.length;a++)
{
if (hidden_categories[a] == categoryId)
{
hide = true;
break;
}
}
if (hide)
hide_category(categoryId, false);
else
show_category(categoryId, false);
}
}
function startup()
{
category_setup();
if (location.search.search('edit_note') != -1)
edit_note();
2015-09-24 18:31:03 +02:00
}
function edit_category(cat_id, name)
{
set_visible("category_" + cat_id, false, "");
set_visible("edit_category_" + cat_id, true, "block");
2015-09-24 18:31:03 +02:00
input = document.getElementById("cat_name_" + cat_id);
input.value = name;
input.focus();
}
function end_edit_category(cat_id)
{
set_visible("category_" + cat_id, true, "block");
set_visible("edit_category_" + cat_id, false, "");
2015-09-24 18:31:03 +02:00
}
function submit_category_name(cat_id, orig)
{
me = document.getElementById("cat_name_" + cat_id);
if (me.value.localeCompare(orig) != 0)
{
form = document.getElementById("edit_category_" + cat_id);
form.submit();
return true;
}
else
return false;
return false;
}
function handleKeyPress(e, cat_id, orig){
var key=e.keyCode || e.which;
if (key==13)
submit_category_name(cat_id, orig);
}
function edit_note()
{
document.body.scrollTop = document.documentElement.scrollTop = 0;
set_visible("title", false, "");
set_visible("transformed_content", false, "");
set_visible("edit_button", false, "");
set_visible("form_delete", false, "");
set_visible("div_edit", true, "block");
2015-09-24 18:31:03 +02:00
}
function cancel_edit_note()
{
document.body.scrollTop = document.documentElement.scrollTop = 0;
set_visible("title", true, "block");
set_visible("transformed_content", true, "block");
2015-09-24 18:31:03 +02:00
set_visible("edit_button", true, "inline");
set_visible("form_delete", true, "inline");
set_visible("div_edit", false, "");
}
function DoEdit(url) {
window.location.href = '/note/' + url + '?edit_note=1';
return false;
2015-09-24 18:31:03 +02:00
}