MediaWiki:Common.js: Difference between revisions

From Business Heroes Food Truck Simulation
No edit summary
Tag: Reverted
No edit summary
 
(61 intermediate revisions by the same user not shown)
Line 3: Line 3:
$('#p-contentnavigation').append($('#editor-menu'));
$('#p-contentnavigation').append($('#editor-menu'));


const server = mw.config.get ( 'wgServer' );
const server     = mw.config.get ( 'wgServer' );
const path    = mw.config.get ( 'wgScriptPath' );
const path       = mw.config.get ( 'wgScriptPath' );
const classes = ['im-marketing', 'im-finance', 'im-operations', 'im-planning', 'im-hr', 'im-accounting'];
const categories = mw.config.get ( 'wgCategories' );
const curpage   = mw.config.get( 'wgTitle' );
// const manpage    = window.location.pathname.replace(/\/index\.php\//, '');
const areas      = ['Marketing', 'Finance', 'Operations', 'Planning', 'HR', 'Accounting'];
const contentWrapper = document.querySelector('#content-wrapper');
const headings = contentWrapper.querySelectorAll('h2 > span, h3 > span');
var firstElement = [];


// Generate links for all dropdown togglers in the sidebar menu
$(function() {
$('#top-panel .dropdown-toggle')
.each(function(i,v){
// add a link and click event to the dropdown togglers
$('#top-panel .dropdown-toggle').each(function(i,v){
var p = $(v).text();
var p = $(v).text();
var url = new URL( server + path + '/index.php/' + encodeURIComponent(p));
var url = new URL( server + path + '/index.php/' + encodeURIComponent(p));
$(v).attr('href', url);
$(v).attr('href', url);
$(v).click(function(){
 
localStorage.setItem('current', p);
$(v).on('click', function(e){
e.stopPropagation();
$(v).click;
});
});
});
});


getActiveCategory();
// on page load check its name to highlight menu items
getCurrentPageMenu();
$('#top-panel a.dropdown-toggle').each(function(i,v){
var p = new URL($(v).attr('href')).pathname.replace(/%20/g, ' ').replace(/\/index\.php\//, '');
if(p === curpage){
$(v)
.addClass('show')
.attr('aria-expanded','true')
.parent()
.addClass('position-static show')
.find('.dropdown-menu')
.show();
} else {
$(v)
.removeClass('show')
.attr('aria-expanded','false')
.parent()
.removeClass('position-static show')
.find('.dropdown-menu')
.hide();
}
});


$('.im-icon')
// highlight active category
    .click(function(){
getActiveCategory();
for(var i = 0, j = classes.length; i < j; i++) {
 
    if($(this).hasClass('' + classes[i] + '')) {
// follow headings on scroll
        localStorage.setItem('active', '' + classes[i] + '');
var observer = new IntersectionObserver(function (entries) {
        break;
entries.forEach(function (entry) {
    }
if (entry.isIntersecting) {
var heading = entry.target;
var headingId = heading.getAttribute('id');
 
if (!(headingId === null) && !(headingId === undefined)) {
 
var className = "n-" + headingId.replace(/_/g, "-");
firstElement = document.querySelectorAll('a[class*="' + className + '" i]');
 
if (!(firstElement.item(0) === null) && !(firstElement.item(0) === undefined)) {
$('.nav-link').removeClass('active');
firstElement.item(0).classList.add('active');
}
}
}
}
})
.each(function(){
    for(var i = 0, j = classes.length; i < j; i++) {
    prefix = "/index.php/";
    page = decodeURIComponent($(this).attr('href').replace(prefix,''));
        $(this).attr('title', page);
    }
});
});
});
headings.forEach(function (heading) {
observer.observe(heading);
});


function getActiveButton(){
});
var getactive = localStorage.getItem('active');
$('.im-icon.' + getactive + '').click();
$('.im-icon.' + getactive + '').parent().addClass('im-active');
}
 
function getCurrentPageMenu(){
var getcurrent = localStorage.getItem('current');
$('#' + getcurrent + '').click();
$('#' + getcurrent + '').addClass('show').parent().addClass('position-static show').find('a').attr('aria-expanded', 'true');
}


function getActiveCategory(){
function getActiveCategory(){
var cat = [];
for(var i = 0, j = areas.length; i < j; i++) {
cat = mw.config.get('wgCategories');
$('.im-' + areas[i].toLowerCase()).attr('title', areas[i]);
if(cat.includes("Planning")) {
if(categories.includes(areas[i])) {
    $('.im-planning').parent().addClass('im-active');
$('.im-' + areas[i].toLowerCase()).parent().addClass('im-active');
localStorage.setItem('active', 'im-planning' );
} else {
} else if(cat.includes("Finance")) {
$('.im-' + areas[i].toLowerCase()).parent().removeClass('im-active');
$('.im-finance').parent().addClass('im-active');
}
localStorage.setItem('active', 'im-finance' );
} else if(cat.includes("Marketing")) {
$('.im-marketing').parent().addClass('im-active');
localStorage.setItem('active', 'im-marketing' );
} else if(cat.includes("Operations")) {
$('.im-operations').parent().addClass('im-active');
localStorage.setItem('active', 'im-operations' );
} else if(cat.includes("HR")) {
$('.im-hr').parent().addClass('im-active');
localStorage.setItem('active', 'im-hr' );
} else if(cat.includes("accounting")) {
$('.im-accounting').parent().addClass('im-active');
localStorage.setItem('active', 'im-accounting' );
} else {
getActiveButton();
}
}
}
}

Latest revision as of 17:23, 22 December 2022

/* Any JavaScript here will be loaded for all users on every page load. */

$('#p-contentnavigation').append($('#editor-menu'));

const server     = mw.config.get ( 'wgServer' );
const path       = mw.config.get ( 'wgScriptPath' );
const categories = mw.config.get ( 'wgCategories' );
const curpage    = mw.config.get( 'wgTitle' );
// const manpage    = window.location.pathname.replace(/\/index\.php\//, '');
const areas      = ['Marketing', 'Finance', 'Operations', 'Planning', 'HR', 'Accounting'];
const contentWrapper = document.querySelector('#content-wrapper');
const headings = contentWrapper.querySelectorAll('h2 > span, h3 > span');
var firstElement = [];

$(function() {
	
	// add a link and click event to the dropdown togglers
	$('#top-panel .dropdown-toggle').each(function(i,v){
		var p = $(v).text();
		var url = new URL( server + path + '/index.php/' + encodeURIComponent(p));
		$(v).attr('href', url);

		$(v).on('click', function(e){
			e.stopPropagation();
			$(v).click;
		});
	});

	// on page load check its name to highlight menu items
	$('#top-panel a.dropdown-toggle').each(function(i,v){
		var p = new URL($(v).attr('href')).pathname.replace(/%20/g, ' ').replace(/\/index\.php\//, '');
		if(p === curpage){
			$(v)
			.addClass('show')
			.attr('aria-expanded','true')
			.parent()
			.addClass('position-static show')
			.find('.dropdown-menu')
			.show();
		} else {
			$(v)
			.removeClass('show')
			.attr('aria-expanded','false')
			.parent()
			.removeClass('position-static show')
			.find('.dropdown-menu')
			.hide();
		}
	});

	// highlight active category
	getActiveCategory();

// follow headings on scroll
var observer = new IntersectionObserver(function (entries) {
	entries.forEach(function (entry) {
		if (entry.isIntersecting) {
			var heading = entry.target;
			var headingId = heading.getAttribute('id');

			if (!(headingId === null) && !(headingId === undefined)) {

				var className = "n-" + headingId.replace(/_/g, "-");
				firstElement = document.querySelectorAll('a[class*="' + className + '" i]');

				if (!(firstElement.item(0) === null) && !(firstElement.item(0) === undefined)) {
					$('.nav-link').removeClass('active');
					firstElement.item(0).classList.add('active');
				}
			}
		}
	});
});
headings.forEach(function (heading) {
	observer.observe(heading);
});

});

function getActiveCategory(){
	for(var i = 0, j = areas.length; i < j; i++) {
		$('.im-' + areas[i].toLowerCase()).attr('title', areas[i]);
		if(categories.includes(areas[i])) {
			$('.im-' + areas[i].toLowerCase()).parent().addClass('im-active');
		} else {
			$('.im-' + areas[i].toLowerCase()).parent().removeClass('im-active');
		}
	}
}