MediaWiki:Common.js/cellTemplate.js

From Kairosoft Wiki
Jump to navigation Jump to search

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* This script's purpose it to make "Template:Blank cell" work properly in the VisualEditor, by transferring it's attributes to the corresponding cell it belongs to. */
function swapAllCellTemplates() {
	var blankCellTemplates = document.querySelectorAll(".cellTemplate");
	Array.prototype.forEach.call(blankCellTemplates, function(template) {
		var parentCell = (template.parentNode.nodeName == "TD" ? template.parentNode : template.parentNode.parentNode);
		if(parentCell.nodeName == "TD") {
			swapAttribute("style", template, parentCell);
			swapAttribute("title", template, parentCell);
			swapAttribute("data-sort-value", template, parentCell);
			template.removeAttribute("class");
		}
	});
}
function swapAttribute(attr,fromElem,toElem) {
	var value = fromElem.getAttribute(attr);
	if(value != null && value != undefined) {
		toElem.setAttribute(attr, value);
		fromElem.removeAttribute(attr);
	}
}

swapAllCellTemplates(); /*Call once on page loading*/

/*Re-apply update after VisualEditor usage.*/
var elemToWatch = document.querySelector("#WikiaMainContent");
if(elemToWatch) {
	var mutationObserver = new MutationObserver(swapAllCellTemplates);
	if(mutationObserver) {
		mutationObserver.observe(elemToWatch, { childList:true, subtree:true });
	} else {
		elemToWatch.addEventListener("DOMNodeInserted", swapAllCellTemplates);
	}
}