2020-01-13 23:34:58 +00:00
|
|
|
var drawers = require( './drawers.js' );
|
|
|
|
|
2017-07-12 15:12:40 +00:00
|
|
|
/*
|
|
|
|
* Warn people if they're trying to switch to desktop but have cookies disabled.
|
|
|
|
*/
|
2019-09-18 03:07:48 +00:00
|
|
|
module.exports = function ( amcOutreach, currentPage ) {
|
2017-07-12 15:12:40 +00:00
|
|
|
/**
|
|
|
|
* Checks whether cookies are enabled
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @return {boolean} Whether cookies are enabled
|
|
|
|
*/
|
|
|
|
function cookiesEnabled() {
|
|
|
|
// If session cookie already set, return true
|
|
|
|
if ( $.cookie( 'mf_testcookie' ) === 'test_value' ) {
|
|
|
|
return true;
|
|
|
|
// Otherwise try to set mf_testcookie and return true if it was set
|
|
|
|
} else {
|
|
|
|
$.cookie( 'mf_testcookie', 'test_value', {
|
|
|
|
path: '/'
|
|
|
|
} );
|
|
|
|
return $.cookie( 'mf_testcookie' ) === 'test_value';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An event handler for the toggle to desktop link.
|
|
|
|
* If cookies are enabled it will redirect you to desktop site as described in
|
|
|
|
* the link href associated with the handler.
|
|
|
|
* If cookies are not enabled, show a toast and die.
|
|
|
|
* @method
|
|
|
|
* @ignore
|
|
|
|
* @return {boolean|undefined}
|
|
|
|
*/
|
|
|
|
function desktopViewClick() {
|
|
|
|
if ( !cookiesEnabled() ) {
|
2019-07-02 21:10:10 +00:00
|
|
|
mw.notify(
|
2017-07-12 15:12:40 +00:00
|
|
|
mw.msg( 'mobile-frontend-cookies-required' ),
|
2019-04-23 14:49:59 +00:00
|
|
|
{ type: 'error' }
|
2017-07-12 15:12:40 +00:00
|
|
|
);
|
|
|
|
// Prevent default action
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-18 03:07:48 +00:00
|
|
|
/**
|
|
|
|
* @method
|
|
|
|
* @ignore
|
2020-01-13 23:34:58 +00:00
|
|
|
* @param {JQuery.Event} ev
|
2019-09-18 03:07:48 +00:00
|
|
|
* @return {boolean|undefined}
|
|
|
|
*/
|
2020-01-13 23:34:58 +00:00
|
|
|
function amcDesktopClickHandler( ev ) {
|
2019-09-18 03:07:48 +00:00
|
|
|
var
|
|
|
|
self = this,
|
|
|
|
executeWrappedEvent = function () {
|
|
|
|
if ( desktopViewClick() === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
window.location = self.href;
|
|
|
|
},
|
|
|
|
amcCampaign = amcOutreach.loadCampaign(),
|
|
|
|
onDismiss = function () {
|
|
|
|
executeWrappedEvent();
|
2020-01-13 23:34:58 +00:00
|
|
|
},
|
|
|
|
drawer = amcCampaign.showIfEligible(
|
|
|
|
amcOutreach.ACTIONS.onDesktopLink,
|
|
|
|
onDismiss,
|
|
|
|
currentPage.title
|
|
|
|
);
|
2019-09-18 03:07:48 +00:00
|
|
|
|
2020-01-13 23:34:58 +00:00
|
|
|
if ( drawer ) {
|
|
|
|
ev.preventDefault();
|
|
|
|
// stopPropagation is needed to prevent drawer from immediately closing
|
|
|
|
// when shown (drawers.js adds a click event to window when drawer is
|
|
|
|
// shown
|
|
|
|
ev.stopPropagation();
|
|
|
|
|
|
|
|
drawers.displayDrawer( drawer, {} );
|
|
|
|
drawers.lockScroll();
|
|
|
|
|
|
|
|
return;
|
2019-09-18 03:07:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return executeWrappedEvent();
|
|
|
|
}
|
|
|
|
|
2019-04-03 23:32:18 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2019-09-18 03:07:48 +00:00
|
|
|
$( '#mw-mf-display-toggle' ).on( 'click', amcDesktopClickHandler );
|
2019-07-02 21:10:10 +00:00
|
|
|
};
|