MediaWiki:Gadget-trailblazer.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
 
No edit summary
Line 48: Line 48:
mw.log('Starting Trailblazer Gadget');
mw.log('Starting Trailblazer Gadget');


portletLink = mw.mw.util.addPortletLink(
portletLink = mw.util.addPortletLink(
'p-personal',
'p-personal',
'',
'',
Line 187: Line 187:
};
};


mw.loader.using(['ext.gadget.rsw-mw.util'], function () {
mw.loader.using(['ext.gadget.rsw-util'], function () {
$(rswtbz.init);
$(rswtbz.init);
window.rswtbz = rswtbz;
window.rswtbz = rswtbz;

Revision as of 17:15, 17 October 2024

/**
* Displays locked/unlocked Trailblazer League regions
*
* @version 1.1
* @author Elessar2
* 
*/

/*
* Classses used:
* tbz-region/tbz-badge (to specify that it's an area element)
* tbz-int-button (opens the interface instead of link)
* attribute: data-tbz-area="[area id]" where [area id] is the area id (see tbareas below)
*
* Sets:
* tbz-locked
* tbz-unlocked
* tbz-int-button (to first link in tbz-region if #links > 1)
*/

/*global jQuery, mediaWiki, mw, rswiki, rs, ga */
'use strict';

;(function($, mw, rs){
var portletLink;

var rswtbz = {
	// localStorage key
	localKey: 'rsw-tbz-localstorage',

	formConst: false,
	formMade: false,

	// Settings
	settings: {
		dcache: '1970-01-01T01:00:00+00:00',
		areas: [ 'misthalin' ],
		cache: {
			curupdate: []
		}
	},

	/**
	 * Starts the events gadget
	 * @return {undefined}
	 */
	init: function () {
		mw.log('Starting Trailblazer Gadget');

		portletLink = mw.util.addPortletLink(
			'p-personal',
			'',
			'League',
			'pt-events',
			'Trailblazer Reloaded League preferences',
			null,
			$('#pt-userpage, #pt-anonuserpage')
		);

		$(portletLink).find('a').addClass('oo-ui-icon-calendar').click(rswtbz.openInt);
		rswtbz.initButtons();

		// Load settings and events from localStorage page load to handle locked/unlocked
		rswtbz.loadSettings().then( function () {
			if (rswtbz.settings.areas && rswtbz.settings.areas.length) {
				rswtbz.updateAreas();
			}
		}, function () {
			console.warn('Error loading events settings!');
		});
	},

	/**
	 * Opens the interface (and initialises if not done yet)
	 * Is a click() event handler
	 * @param  {eventObject} jQuery event object
	 * @return {undefined}
	 */
	openInt: function (e) {
		e.preventDefault();
		var openPopup = function() {
			mw.log('Open trailblazer popup');
			rswtbz.loadSettings().then(rswtbz.updateAll(), rswtbz.updateAll());

			// Google analytics tracker
			if (typeof ga === 'function') {
				ga('gtag_UA_126479006_1.send', 'event', 'Gadget-trailblazer', 'Open', 'Normal');
			}

			// Start constant update
			clearInterval(rswtbz.updatetimer);
			rswtbz.updatetimer = setInterval(rswtbz.update, 60000);
			
			window.OOUIWindowManager.openWindow( 'trailblazerPopup' );
		};
		if (!rswtbz.formMade && !rswtbz.formConst) {
			mw.log('Loading trailblazer modal script');
			rswtbz.formConst = true;
			mw.loader.using(['moment', 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets', 'ext.gadget.trailblazer-modal']).then( function () {
				rswtbz.initInt().then(openPopup);
			}, function (err) {
				mw.warn('Failed to load modal js');
				mw.log(err);
				rswtbz.formConst = false;
				throw 'Failed to load modal js';
			});
		} else if (!rswtbz.formConst) {
			openPopup();
		} else {
			mw.log('Waiting for initialisation to finish');
		}
	},

	/**
	 * Adds links to open interface to trailblazer region buttons
	 * @return {undefined}
	 */
	initButtons: function () {
		$('.tbz-int-button').each(function() {
			$(this).click(rswtbz.openInt);
		});
		$('.tbz-region,.tbz-badge').each(function() {
			if (!$(this).hasClass("tbz-no-global")) {
				$(this).click(rswtbz.openInt).children('a').click(function (e) {
					e.stopPropagation();
				});
			}
		});
	},

	/**
	 * Loads settings from browser localStorage
	 * @return {Promise}
	 */
	loadSettings: function () {
		mw.log('Loading settings');
		return new Promise( function (resolve, reject) {
			if (!rs.hasLocalStorage()) {
				console.warn('Browser does not support localStorage');
				reject();
			}

			var prefs = {};
			try {
				prefs = JSON.parse(localStorage.getItem(rswtbz.localKey));
			} catch (err) {
				prefs = {};
				console.warn('Error loading settings (events)');
			}
			for (var p in prefs) {
				rswtbz.settings[p] = prefs[p];
			}
			resolve();
		} );
	},

	/**
	 * Updates the areas unlocked
	 * @return {undefined}
	 */
	updateAreas: function () {
		mw.log('Updating areas');
		if (!Array.isArray(rswtbz.settings.areas)) {
			rswtbz.settings.areas = ['misthalin'];
		}

		$('.tbz-region,.tbz-badge').each(function() {
			if (!$(this).hasClass("tbz-no-global")) {
				var obj = $(this),
					areaid = obj.attr('data-tbz-area');
				var areaList = areaid.split('&');
				var allRegionsUnlocked = areaList.every(
					function(area) {
						return rswtbz.settings.areas.indexOf(area) !== -1;
					}
				);
				if (allRegionsUnlocked) {
					obj.removeClass('tbz-locked');
					obj.addClass('tbz-unlocked');
				} else {
					obj.removeClass('tbz-unlocked');
					obj.addClass('tbz-locked');
				}
			}
		});
	}
};

mw.loader.using(['ext.gadget.rsw-util'], function () {
	$(rswtbz.init);
	window.rswtbz = rswtbz;
});

}(jQuery, mediaWiki, rswiki));