MediaWiki:FR2014/Resources/Oct2014FullScreen.js

From Meta, a Wikimedia project coordination wiki

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.
/* For fullscreen banners which will only be shown once
 * After showing them, set a cookie so they don't show up again
 * and move the user into bucket C/D
 */

var fundraisingBanner = fundraisingBanner || {};

fundraisingBanner.show = function() {
    $('body').prepend($('#centralNotice'));
    $('#{{{banner}}}').css('display', 'block');
};

fundraisingBanner.hide = function() {
    // It's a fullscreen, so they will presumably always hide it
    $('#{{{banner}}}').hide();
    // After it's gone, safe to remove these bindings
    $(document).off('keyup.bannerHide');
    $(document).off('click.bannerHide');
};

fundraisingBanner.changeBucket = function() {
    /* Move user to bucket C or D for this campaign
       Although if it was just a test (no campaign) then don't bother */

    if ( !mw.centralNotice.data.getVars.banner ) {
        var newBucket = Math.floor(Math.random()  *  2)  +  2;
        mw.cnBannerControllerLib.bucketsByCampaign['{{{campaign}}}'].val = newBucket;
        mw.cnBannerControllerLib.storeBuckets();
    }

};

(function(mw) {

    // See https://stackoverflow.com/questions/6125330/javascript-navigator-cookieenabled-browser-compatibility#15582757
    var cookiesEnabled = ('cookie' in document && (document.cookie.length > 0 || (document.cookie = 'test').indexOf.call(document.cookie, 'test') > -1));

    var countCookieName = 'centralnotice_bannercount_fr12',
        cookieCount = parseInt($.cookie(countCookieName), 10) || 0,
        forced = document.location.search.match(/\bforce=1/);

    if ( cookiesEnabled ) {

        if ( $.cookie('centralnotice-frbanner-seen-fullscreen') && !forced ) {
            // Already seen a full-screen banner, so hide this one (unless forced)
            mw.centralNotice.bannerData.hideResult = true;
            mw.centralNotice.bannerData.hideReason = 'seen-fullscreen';
        } else {
            mw.centralNotice.bannerData.hideResult = false;

            // Increment cookie counter
            cookieCount += 1;
            mw.centralNotice.bannerData.cookieCount = cookieCount;
            $.cookie(countCookieName, cookieCount, { expires: 365 });

            // And set a cookie to remember they saw a fullscreen
            $.cookie('centralnotice-frbanner-seen-fullscreen', Date.now(), {expires: 250});
        }

    } else {
        mw.centralNotice.bannerData.hideResult = true;
        mw.centralNotice.bannerData.hideReason = 'cookies-disabled';
    }

    // Either way, we should bump them into Bucket C/D now
    fundraisingBanner.changeBucket();

})(mediaWiki);

/* Close fullscreen banner on pressing ESC */
$(document).on('keyup.bannerHide', function(e) {
    if (e.keyCode === 27) { 
        fundraisingBanner.hide();
    }
});

/* Close fullscreen banner when clicked outside */
$(document).on('click.bannerHide', function(e) {
    if ($(e.target).closest("#frbanner-window").length === 0) {
        fundraisingBanner.hide();
    }
});

$(document).ready(function() {
    // Actually show it if needed
    if (mw.centralNotice.bannerData.hideResult === false) {
        fundraisingBanner.show();
    }
});

mediaWiki.centralNotice.bannerData.alterImpressionData = function( impressionData ) {
    // Returning true from this function indicates the banner was shown
    if (mediaWiki.centralNotice.bannerData.hideReason) {
        impressionData.reason = mediaWiki.centralNotice.bannerData.hideReason;
    }
    if (mediaWiki.centralNotice.bannerData.cookieCount) {
        impressionData.banner_count = mediaWiki.centralNotice.bannerData.cookieCount;
    }
    return !mediaWiki.centralNotice.bannerData.hideResult;
};