MediaWiki:Gadget-DefinitionsAdder.js

From Wiktionary, the free dictionary
Jump to navigation Jump to search

Note: You may have to bypass your browser’s cache to see the changes. In addition, after saving a sitewide CSS file such as MediaWiki:Common.css, it will take 5-10 minutes before the changes take effect, even if you clear your cache.

  • Mozilla / Firefox / Safari: hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (Command-R on a Macintosh);
  • Konqueror and Chrome: click Reload or press F5;
  • Opera: clear the cache in Tools → Preferences;
  • Internet Explorer: hold Ctrl while clicking Refresh, or press Ctrl-F5.


// 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[i].getElementsByTagName('li'), ii = 0; ii < lis.length; ii++) {
					if (lis[ii].parentNode == ols[i]) {
						lis[ii].onmouseover = lis[ii].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()[0];

	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>')[0];
	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')[0].href.match(/\d*$/));
		wikitext =
			(
				wikitext.replace(
					RegExp("((?:(^|\n)=[\\s\\S]*?){" + findNumberOfHeaders + "}([\\s\\S]*?\n#[\\s\\S]*?\n(?!#)){" + prevols + "}([\\s\\S]*?\n#(?![#:\\*])){" + prevlis + "}[\\s\\S]*?)(\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);
}