/* tears.js */
var tears = {}
function additem(e) {
var item = e.target.parentNode.cloneNode(true);
e.target.parentNode.className = "";
e.target.textContent = "x";
e.target.onclick = function(e) {
e.target.parentNode.remove();
return false;
}
item.getElementsByTagName('input')[0].value = "";
item.getElementsByTagName('a')[0].onclick = additem;
e.target.parentNode.parentNode.appendChild(item);
return false;
}
function pick(src) {
if(tears.input) {
var name = tears.input.getAttribute("data-name");
var input = document.getElementsByName(name)[0];
tears.input.src = src;
input.value = src;
tears.input = null;
}
}
function insert(text) {
var t = document.getElementsByName('text')[0];
if(!t) return;
if(t.selectionStart || t.selectionStart == '0') {
var pos = t.selectionStart;
var end = t.selectionEnd;
t.value = t.value.substring(0, pos) + text + t.value.substring(end, t.value.length);
}
var cpos = t.selectionStart;
t.setSelectionRange(cpos, cpos);
t.focus();
}
function wrap(prefix, postfix) {
var t = document.getElementsByName('text')[0];
if(!t) return;
if(t.selectionStart || t.selectionStart == '0') {
var pos = t.selectionStart;
var end = t.selectionEnd;
var n = t.value.indexOf("\n", pos);
if(n > 0 && n < end) end = n;
t.value = t.value.substring(0, pos) + prefix + t.value.substring(pos, end) + postfix + t.value.substring(end, t.value.length);
}
var cpos = end + prefix.length + postfix.length;
t.setSelectionRange(cpos, cpos);
t.focus();
}
function prepend(text) {
var t = document.getElementsByName('text')[0];
if(!t) return;
if(t.selectionStart || t.selectionStart == '0') {
var pos = t.selectionStart;
var end = t.selectionEnd;
for(;pos < end; pos++) {
if(t.value.charAt(pos) != "\n") break;
}
for(;end > pos; end--) {
if(t.value.charAt(end - 1) != "\n") break;
}
var quote = "";
var cpos = end;
if(t.value.charAt(pos - 1) != "\n") {
quote += "\n";
cpos += 1;
}
if(pos == end) {
quote += text;
cpos += text.length;
} else {
var lines = t.value.substring(pos, end).split("\n");
for(var i=0; i < lines.length; i++) {
quote += text + lines[i] + "\n";
cpos += text.length;
}
quote = quote.slice(0, -1);
}
t.value = t.value.substring(0, pos) + quote + t.value.substring(end, t.value.length);
}
t.setSelectionRange(cpos, cpos);
t.focus();
}
function marxup_bold(e) {
wrap("*", "*");
}
function marxup_header(e) {
prepend("= ");
}
function marxup_italics(e) {
wrap("_", "_");
}
function marxup_small(e) {
wrap("~", "~");
}
function marxup_link(e) {
wrap("[", "]");
}
function marxup_quote(e) {
prepend("> ");
}
function marxup_list(e) {
prepend("* ");
}
function button(node, html, fun) {
var a = document.createElement("a");
a.innerHTML = html;
a.href = "#";
a.onclick = fun;
node.parentNode.insertBefore(document.createTextNode(" "), node.nextSibling);
node.parentNode.insertBefore(a, node.nextSibling);
}
function orz(src, receiver) {
// insert("\n{" + src.replace(".t.jpg", "") + "}\n");
console.log(receiver);
console.log(src);
if(!receiver) {
console.log(src);
insert("\n{" + src + "}\n");
return;
}
var e = document.getElementsByName(receiver);
if(e && e[0]) {
e[0].value = src;
}
var imgs = document.getElementsByClassName('orz');
for(var i = 0; i < imgs.length; i++) {
if(imgs[i].getAttribute('data-name') == receiver) {
imgs[i].src = src;
}
}
}
document.addEventListener("DOMContentLoaded", function(e) {
var field = document.getElementsByName('publisert')[0];
if(field) {
new Pikaday({ field: field, format: 'YYYY-MM-DD' });
}
var title = document.getElementsByTagName('h1')[0];
var headers = document.getElementsByTagName("h2");
if(headers.length > 0) {
var menu = document.createElement("div"); menu.className = 'magisk meny';
for(var i = 0; i < headers.length; i++) {
var a = document.createElement("a");
var id = "h-" + i; headers[i].id = id; a.href = "#" + id;
a.textContent = headers[i].textContent;
menu.appendChild(a);
menu.appendChild(document.createElement("br"));
}
title.parentNode.insertBefore(menu, title.nextSibling);
}
var orz = document.getElementsByClassName('orz');
for(var i = 0; i < orz.length; i++) {
orz[i].onclick = function(e) {
window.receiver = e.target.getAttribute('data-name')
window.open("/orz");
return false;
}
}
var lol = document.getElementById('lol');
if(lol) {
console.log(orz);
button(orz[orz.length - 1], "> sitat", marxup_quote);
button(orz[orz.length - 1], "* liste", marxup_list);
button(orz[orz.length - 1], "[lenke]", marxup_link);
button(orz[orz.length - 1], "_kursiv_", marxup_italics);
button(orz[orz.length - 1], "*fet*", marxup_bold);
button(orz[orz.length - 1], "liten", marxup_small);
button(orz[orz.length - 1], "= overskrift", marxup_header);
}
var form = document.getElementById('update-article');
if(form) {
form.onsubmit = function(e) {
var stikkord = document.getElementsByName('stikkord');
if(stikkord && stikkord[0] && !stikkord[0].value) {
stikkord[0].className = "error";
return false;
}
}
}
var infoboxes = document.getElementsByClassName("infobox");
for(var i = 0; i < infoboxes.length; i++) {
Sortable.create(infoboxes[i]);
var items = infoboxes[i].getElementsByTagName('li');
for(var j = 0; j < items.length; j++) {
if(items[j].className == "last") {
var add = document.createElement("a");
add.textContent = "+";
add.href = "#";
add.onclick = additem;
items[j].appendChild(add);
} else {
var del = document.createElement("a");
del.textContent = "x";
del.href = "#";
del.onclick = function(e) {
e.target.parentNode.remove();
return false;
}
items[j].appendChild(del);
}
}
}
});