MediaWiki:Vector.js

From Animal Well 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.
/* All JavaScript here will be loaded for users of the Vector skin */
/* Adapted from bunburrows.wiki.gg */

var vectorjs = {
	util: {
		ready: function(callback) {
			if (document.readyState === "loading") {
				document.addEventListener("DOMContentLoaded", callback);
			} else callback();
		},

		load: function(url, callback) {
			fetch(url).then(function(response) {
				if (response.ok) response.text().then(callback);
			});
		},

		loadAndAppend: function(url, selector) {
			vectorjs.util.load(url, function(text) {
				vectorjs.util.ready(function() {
					document.querySelectorAll(selector).forEach(function(node) {
						node.insertAdjacentHTML("beforeend", text);
					});
				});
			});
		}
	},
	
	fireflies: function() {
		for (var i = 0; i < 11; i++) {
			var firefly = document.createElement("div");
			firefly.setAttribute("class", "firefly");
			
			const currentDiv = document.getElementById("wikigg-header");
			document.body.insertBefore(firefly, currentDiv);
		}
	},

	expandCactions: function() {
		vectorjs.util.ready(function() {
			var cactions = document.querySelector("#p-cactions");
			var viewsContent = document.querySelector("#p-views .vector-menu-content-list");
			var watch = viewsContent.querySelector(".mw-watchlink");
			cactions.querySelectorAll(".mw-list-item").forEach(function(node) {
				if (watch) watch.before(node);
				else viewsContent.append(node);
			});
		});
	},

	
	replaceHeadIcons: function() {
		vectorjs.util.loadAndAppend("/images/1/15/Head-icon-nstab.svg", "[id^=ca-nstab]");
		vectorjs.util.loadAndAppend("/images/7/79/Head-icon-talk.svg", "#ca-talk");
		vectorjs.util.loadAndAppend("/images/d/d6/Head-icon-view.svg", "#ca-view");
		vectorjs.util.loadAndAppend("/images/5/5a/Head-icon-edit.svg", "#ca-edit");
		vectorjs.util.loadAndAppend("/images/c/c6/Head-icon-viewsource.svg", "#ca-viewsource");
		vectorjs.util.loadAndAppend("/images/1/17/Head-icon-history.svg", "#ca-history");
		vectorjs.util.loadAndAppend("/images/0/08/Head-icon-delete.svg", "#ca-delete");
		vectorjs.util.loadAndAppend("/images/9/9e/Head-icon-move.svg", "#ca-move");
		vectorjs.util.loadAndAppend("/images/d/d1/Head-icon-protect.svg", "#ca-protect");
		vectorjs.util.loadAndAppend("/images/0/03/Head-icon-watch.svg", ".mw-watchlink");
	}
	
};




if (CSS.supports("selector(&)")) {
	vectorjs.replaceHeadIcons(),
	vectorjs.fireflies(),
	vectorjs.expandCactions();
}

///////////////////////////////////////////////////////////////////////////////

/* Copied from https://terraria.wiki.gg/wiki/MediaWiki:Common.js */
/* Make sidebar sections collapsible */
$(function(){
	$panel = $('#mw-panel');
	$("#mw-panel .portal").each(function(index, el){
		var $el = $(el);
		var $id = $el.attr("id");
		if(!$id){
			return;
		}
		// for < 1366px
		$el.removeClass('expanded');
		// for >= 1366px
		if(localStorage.getItem('sidebar_c_'+$id) === "y"){
			$el.addClass('collapsed').find('.body').slideUp(0);
		}
	});
	$("#mw-panel .portal").on("click", "h3", function(event){
		var $el = $(this).parent();
		var $id = $el.attr("id");
		if(!$id){
			return;
		}
		event.stopPropagation();
		if($panel.width() < 200){
			$el.toggleClass('collapsed');
			if($el.hasClass('collapsed')){ // more consistent between class and slide status.
				localStorage.setItem('sidebar_c_'+$id, "y");
				$el.find('.body').slideUp('fast');
			}
			else{
				localStorage.setItem('sidebar_c_'+$id, "n");
				$el.find('.body').slideDown('fast');
			}
		}
		else{
			$("#mw-panel .portal").not($el).removeClass('expanded');
			$el.toggleClass('expanded');
		}
	});
});

///////////////////////////////////////////////////////////////////////////////