MediaWiki:Gadget-trailblazer.js: Difference between revisions

no edit summary
No edit summary
No edit summary
 
Line 22:
'use strict';
 
;
;(function($, mw, rs){
(function ($, mw, rs) {
var portletLink;
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 init() {
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
var rswtbz = {
rswtbz.loadSettings().then(function () {
// localStorage key
if (rswtbz.settings.areas && rswtbz.settings.areas.length) {
localKey: 'rsw-tbz-localstorage',
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 openInt(e) {
e.preventDefault();
var openPopup = function openPopup() {
mw.log('Open trailblazer popup');
rswtbz.loadSettings().then(rswtbz.updateAll(), rswtbz.updateAll());
 
// Google analytics tracker
formConst: false,
if (typeof ga === 'function') {
formMade: false,
ga('gtag_UA_126479006_1.send', 'event', 'Gadget-trailblazer', 'Open', 'Normal');
}
 
// Start constant update
// Settings
clearInterval(rswtbz.updatetimer);
settings: {
rswtbz.updatetimer = setInterval(rswtbz.update, 60000);
dcache: '1970-01-01T01:00:00+00:00',
window.OOUIWindowManager.openWindow('trailblazerPopup');
areas: [ 'misthalin' ],
};
cache: {
if (!rswtbz.formMade && !rswtbz.formConst) {
curupdate: []
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) {
* Starts the events gadget
mw.warn('Failed to load modal js');
* @return {undefined}
mw.log(err);
*/
rswtbz.formConst = false;
init: function () {
throw 'Failed to load modal js';
mw.log('Starting Trailblazer Gadget');
});
 
} else if (!rswtbz.formConst) {
portletLink = mw.util.addPortletLink(
openPopup();
'p-personal',
} else {
'',
mw.log('Waiting for initialisation to finish');
'League',
}
'pt-events',
},
'Trailblazer Reloaded League preferences',
/**
null,
* Adds links to open interface to trailblazer region buttons
$('#pt-userpage, #pt-anonuserpage')
* @return {undefined}
);
*/
 
initButtons: function initButtons() {
$(portletLink).find('a').addClass('oo-ui-icon-calendar').click(rswtbz.openInt);
$('.tbz-int-button').each(function () {
rswtbz.initButtons();
$(this).click(rswtbz.openInt);
 
});
// Load settings and events from localStorage page load to handle locked/unlocked
rswtbz.loadSettings $('.tbz-region,.tbz-badge').theneach( function () {
if (!$(this).hasClass("tbz-no-global")) {
if (rswtbz.settings.areas && rswtbz.settings.areas.length) {
$(this).click(rswtbz.openInt).children('a').click(function (e) {
rswtbz.updateAreas();
e.stopPropagation();
}
});
}, function () {
}
console.warn('Error loading events settings!');
});
},
/**
 
* Loads settings from browser localStorage
/**
* @return {Promise}
* Opens the interface (and initialises if not done yet)
*/
* Is a click() event handler
loadSettings: function loadSettings() {
* @param {eventObject} jQuery event object
mw.log('Loading settings');
* @return {undefined}
return new Promise(function (resolve, reject) {
*/
if (!rs.hasLocalStorage()) {
openInt: function (e) {
console.warn('Browser does not support localStorage');
e.preventDefault();
reject();
var openPopup = function() {
}
mw.log('Open trailblazer popup');
var prefs = {};
rswtbz.loadSettings().then(rswtbz.updateAll(), rswtbz.updateAll());
try {
 
prefs = JSON.parse(localStorage.getItem(rswtbz.localKey));
// Google analytics tracker
} catch (err) {
if (typeof ga === 'function') {
prefs = {};
ga('gtag_UA_126479006_1.send', 'event', 'Gadget-trailblazer', 'Open', 'Normal');
console.warn('Error loading settings (events)');
}
}
 
for (var p in prefs) {
// Start constant update
rswtbz.settings[p] = prefs[p];
clearInterval(rswtbz.updatetimer);
}
rswtbz.updatetimer = setInterval(rswtbz.update, 60000);
resolve();
});
window.OOUIWindowManager.openWindow( 'trailblazerPopup' );
},
};
/**
if (!rswtbz.formMade && !rswtbz.formConst) {
* Updates the areas unlocked
mw.log('Loading trailblazer modal script');
* @return {undefined}
rswtbz.formConst = true;
*/
mw.loader.using(['moment', 'mediawiki.api', 'oojs-ui-core', 'oojs-ui-windows', 'oojs-ui-widgets', 'ext.gadget.trailblazer-modal']).then( function () {
updateAreas: function updateAreas() {
rswtbz.initInt().then(openPopup);
mw.log('Updating areas');
}, function (err) {
if (!Array.isArray(rswtbz.settings.areas)) {
mw.warn('Failed to load modal js');
rswtbz.settings.areas = ['misthalin'];
mw.log(err);
}
rswtbz.formConst = false;
$('.tbz-region,.tbz-badge').each(function () {
throw 'Failed to load modal js';
if (!$(this).hasClass("tbz-no-global")) {
});
var obj = $(this),
} else if (!rswtbz.formConst) {
areaid = obj.attr('data-tbz-area');
openPopup();
var areaList = areaid.split('&');
} else {
var allRegionsUnlocked = areaList.every(function (area) {
mw.log('Waiting for initialisation to finish');
return rswtbz.settings.areas.indexOf(area) !== -1;
}
});
},
if (allRegionsUnlocked) {
 
obj.removeClass('tbz-locked');
/**
obj.addClass('tbz-unlocked');
* Adds links to open interface to trailblazer region buttons
} else {
* @return {undefined}
obj.removeClass('tbz-unlocked');
*/
obj.addClass('tbz-locked');
initButtons: function () {
}
$('.tbz-int-button').each(function() {
}
$(this).click(rswtbz.openInt);
});
}
$('.tbz-region,.tbz-badge').each(function() {
};
if (!$(this).hasClass("tbz-no-global")) {
$(this) mw.click(rswtbzloader.openInt).childrenusing(['a')ext.click(gadget.rsw-util'], function (e) {
$(rswtbz.init);
e.stopPropagation();
window.rswtbz = rswtbz;
});
});
}
})(jQuery, mediaWiki, rswiki);
});
},
 
/**
* 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));