MediaWiki:Gadget-trailblazer.js: Difference between revisions

From RuneRealm Wiki
Jump to navigation Jump to search
Content added Content deleted
No edit summary
No edit summary
 
Line 22: Line 22:
'use strict';
'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().then( function () {
$('.tbz-region,.tbz-badge').each(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).click(rswtbz.openInt).children('a').click(function (e) {
mw.loader.using(['ext.gadget.rsw-util'], function () {
$(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));

Latest revision as of 12:06, 20 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 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
      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 openInt(e) {
      e.preventDefault();
      var openPopup = function openPopup() {
        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 initButtons() {
      $('.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 loadSettings() {
      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 updateAreas() {
      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);