User:Aeywoo/customNavLinks.js

From Support 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.
// Custom Nav Links (GlobalNavButtons)
// @author: Jr Mime
// @author: Caburum
// @ported by: Aeywoo
// <pre>

mw.loader.using(["mediawiki.template.mustache"]).then(function () {
	glnbutt = {};
	glnbutt.config = $.extend({
		keepLinks: [] // tracking ids of top-level links to keep
	}, window.CustomNavLinksConf);

	if (mw.config.get("skin") !== "vector") return console.error("[CustomNavLinks] Unrecognized skin:", mw.config.get("skin"));

	glnbutt.init = function () {
		if (window.customNavLinks) {
			$.each(window.customNavLinks, function(i, glnbutton) {
				if (typeof glnbutton !== "object") {} // Do nothing
				else if (!glnbutton.isMain && !glnbutton.whoIsMain) { // Normal
					$("header#wikigg-header > #p-personal > div.vector-menu-content.body > ul.vector-menu-content-list.menu").prepend(glnbutt.createTemplate("normal", {
						text: glnbutton.text,
						url: glnbutton.url,
						shortName: glnbutton.shortName,
						hoverText: glnbutton.hoverText,
					}));
				} else if (glnbutton.isMain) { // Parent
					$("header#wikigg-header > #p-personal > div.vector-menu-content.body > ul.vector-menu-content-list.menu").prepend(glnbutt.createTemplate("parent", {
						text: glnbutton.text,
						url: glnbutton.url,
						shortName: glnbutton.shortName,
						hoverText: glnbutton.hoverText,
					}));
				} else if (!glnbutton.isMain && glnbutton.whoIsMain) { // Child
					$("#p-custom-parent-" + glnbutton.whoIsMain + " div.vector-menu-content ul.vector-menu-content-list.menu").append(glnbutt.createTemplate("child", {
						text: glnbutton.text,
						url: glnbutton.url,
					}));
				}
				i++;
			});
			mw.hook("dev.CustomNavLinks").fire(); // for anyone needing to implement additional logic
		}
	};

	glnbutt.templates = {
		normal:
			'<li id="p-custom-normal-{{{shortName}}}" class="mw-list-item">' +
				'<a {{{url}}} title="{{{hoverText}}}">' +
					'<span>{{text}}</span>' +
				'</a>' +
			'</li>',
		parent:
			'<li id="p-custom-parent-{{{shortName}}}" class="mw-list-item vector-menu vector-menu-dropdown vector-menu-dropdown-noicon">' +
				'<input id="p-custom-li-{{{shortName}}}-checkbox" type="checkbox" class="vector-menu-checkbox" role="button" aria-haspopup="false" aria-labelledby="p-custom-li-{{{shortName}}}-label" title="{{{hoverText}}}">' +
				'<label id="p-custom-li-{{{shortName}}}-label" for="p-custom-li-{{{shortName}}}-checkbox" class="vector-menu-heading">' +
					'<span class="vector-menu-heading-label">{{text}}</span>' +
				'</label>' +
				'<div class="vector-menu-content">' +
					'<ul class="vector-menu-content-list menu">' +
						// Children here
					'</ul>' +
				'</div>' +
			'</li>',
		child: '<li class="mw-list-item" id="p-custom-child"><a {{{url}}}>{{text}}</a></li>'
	};

	glnbutt.createTemplate = function (template, args) {
		args = {
			text: args.text,
			url: args.url ? 'href="' + args.url + '"' : '',
			shortName: args.shortName || '',
			hoverText: args.hoverText || '',
			smallText: /\b\S{7,}\b/g.test(args.text) ? '' : ''
		};
		return Mustache.render(glnbutt.templates[template], args);
	};
	glnbutt.init();
});
// </pre>