// This script adds an "Add definition" button to the toolbox section of the sidebar.
window.bodyContent = document.getElementById('bodyContent');
if (mw.config.get("wgNamespaceNumber") === 0 && mw.config.get("wgCurRevisionId") && mw.config.get("wgAction") == "view" && !/&printable=yes|&diff=|&oldid=/.test(window.location.search)) {
$(function() {
mw.util.addPortletLink('p-tb', 'javascript:addDefinition()', 'Add definition');
});
}
var definitionHover, tempDefinitionText;
window.setUpBoxToBeAdded = function() {
bodyContent.appendChild(currentBoxToBeAdded);
document.onmousemove =
function(e) {
e = e || event;
currentBoxToBeAdded.style.left = e.clientX + 2 + 'px';
currentBoxToBeAdded.style.top = e.clientY + 2 + 'px';
};
document.body.style.cursor = 'move';
};
window.addDefinition = function() {
var instructions = "Type in your definition, drag it to where it should be placed, and click there to place it.",
temp;
window.currentBoxToBeAdded = $('<input>', {
size: 100
})
.on("blur", function() {
if (definitionHover) {
addDefinition2(definitionHover, currentBoxToBeAdded.lastChild.lastChild.value.replace(instructions, ''));
} else {
tempDefinitionText = currentBoxToBeAdded.lastChild.lastChild.value.replace(instructions, '');
}
bodyContent.removeChild(currentBoxToBeAdded);
document.onmousemove = null;
document.body.style.cursor = '';
for (var ols = document.getElementsByTagName('ol'), i = 0; i < ols.length; i++) {
for (var lis = ols.getElementsByTagName('li'), ii = 0; ii < lis.length; ii++) {
if (lis.parentNode == ols) {
lis.onmouseover = lis.onmouseout = null;
}
}
}
}).val(tempDefinitionText || "")
.appendTo($("<nobr>Definition: </nobr>")).parent()
.appendTo($('<div>', {
style: 'border: 1px solid #000000;' +
'position:fixed; left:200px; top:500px; z-index:5;' +
'padding:10px; background-color:#FFFFFF;'
})).parent();
setUpBoxToBeAdded();
temp = currentBoxToBeAdded.lastChild.lastChild;
temp.focus();
if (!temp.value) {
temp.value = instructions;
temp.style.color = "#AAA";
temp.onkeydown = function() {
this.style.color = "#000";
this.value = '';
this.onkeydown = null;
};
}
$("#mw-content-text").on("mouseover", "ol > li", function() {
this.style.borderBottom = '1px solid #000000';
definitionHover = this;
});
$("#mw-content-text").on("mouseout", "ol > li", function() {
this.style.borderBottom = this.style.borderTopStyle == "dashed" ? "2px #00FF00 dashed" : "";
definitionHover = null;
});
};
function addDefinition2(q, newdef) {
q.style.borderBottom = q.style.borderTopStyle == "dashed" ? "2px #00FF00 dashed" : "";
definitionHover = tempDefinitionText = null;
var qq = $('<li><span></span></li>');
new mw.Api().parse(newdef, {
title: mw.config.get("wgPageName")
}).done(function(res) {
qq.lastChild.innerHTML = res;
});
function addDefinition3(wikitext) {
var prevheader = q,
prevols = 0,
prevlis = 1;
while (prevheader.previousSibling) {
prevheader = prevheader.previousSibling;
if (prevheader.nodeName.toLowerCase() == "li") {
prevlis++;
}
}
prevheader = prevheader.parentNode;
while (!/^h\d$/i.test(prevheader.nodeName)) {
prevheader = prevheader.previousSibling;
if (prevheader.nodeName.toLowerCase() == "ol") {
prevols++;
}
}
var findNumberOfHeaders =
Number(prevheader.lastChild.getElementsByTagName('a').href.match(/\d*$/));
wikitext =
(
wikitext.replace(
RegExp("((?:(^|\n)=*?){" + findNumberOfHeaders + "}(*?\n#*?\n(?!#)){" + prevols + "}(*?\n#(?!)){" + prevlis + "}*?)(\n(?!#)|$)"),
'$1\n# ' + newdef + '\n'
)
);
ccc = wikitext;
return ccc;
}
var updatecatscallback;
var editor = new Editor();
editor.addEdit({
edit: addDefinition3,
redo: function() {
q.parentNode.insertBefore(qq, q.nextSibling);
if (window.makedefsidebox && qq.childNodes.length == 1 && window.defsidebuttons && defsidebuttons.length) {
makedefsidebox(qq);
} // User:Yair_rand/editor.js stuff
if (window.updateCategories && window.tabbedLanguages) {
updatecatscallback = updateCategories((q.parentNode.parentNode.nodeName.toLowerCase() == "li" ? q.parentNode.parentNode : q).parentNode.parentNode, newdef);
}
},
undo: function() {
q.parentNode.removeChild(qq);
updatecatscallback && updatecatscallback();
},
summary: "+def: " + newdef
}, qq);
}