From 33236091f2bcbf54fddffd3f9d70fcd5793158cb Mon Sep 17 00:00:00 2001 From: Nicholas Ray Date: Tue, 17 Sep 2019 21:07:48 -0600 Subject: [PATCH] Trigger AmcOutreach on History, Talk, and Desktop links This follows up on the work from MF: - I399dd70b5b93fda8c6d1735e3861c5ab2da43bdb - Id59381ddc330de2b8017963d6a25c6567317faf7 Event handlers are added to the history link, talk link, and desktop link to support amc outreach. When eligible, the drawer is shown. When ineligible, logic from the regular handler is executed. Bug: T226069 Depends-On: Id59381ddc330de2b8017963d6a25c6567317faf7 Change-Id: I0cce0bb6f44801c383556f8c26ee865032d86c8a --- resources/skins.minerva.scripts/init.js | 34 ++++++++++++- .../skins.minerva.scripts/mobileRedirect.js | 33 +++++++++++- resources/skins.minerva.scripts/talk.js | 50 ++++++++++++++----- 3 files changed, 101 insertions(+), 16 deletions(-) diff --git a/resources/skins.minerva.scripts/init.js b/resources/skins.minerva.scripts/init.js index 7beb3c146..8d61e4982 100644 --- a/resources/skins.minerva.scripts/init.js +++ b/resources/skins.minerva.scripts/init.js @@ -136,7 +136,7 @@ * to show a human friendly date in seconds, minutes, hours, days * months or years * @ignore - * @param {JQuery.Object} [$lastModifiedLink] + * @param {JQuery.Object} $lastModifiedLink */ function initHistoryLink( $lastModifiedLink ) { var delta, historyUrl, msg, $bar, @@ -162,6 +162,34 @@ } } + /** + * @method + * @param {JQuery.Event} ev + */ + function amcHistoryClickHandler( ev ) { + var + self = this, + amcOutreach = mobile.amcOutreach, + amcCampaign = amcOutreach.loadCampaign(), + onDismiss = function () { + toast.showOnPageReload( mw.message( 'mobile-frontend-amc-outreach-dismissed-message' ).text() ); + window.location = self.href; + }; + + if ( amcCampaign.showIfEligible( amcOutreach.ACTIONS.onHistoryLink, onDismiss, currentPage.title, 'action=history' ) ) { + ev.preventDefault(); + } + } + + /** + * @method + * @param {JQuery.Object} $lastModifiedLink + * @ignore + */ + function initAmcHistoryLink( $lastModifiedLink ) { + $lastModifiedLink.one( 'click', amcHistoryClickHandler ); + } + /** * Initialisation function for last modified times * @@ -319,12 +347,14 @@ // - search search(); // - mobile redirect - mobileRedirect(); + mobileRedirect( mobile.amcOutreach, currentPage ); // Update anything else that needs enhancing (e.g. watchlist) initModifiedInfo(); initRegistrationInfo(); // eslint-disable-next-line no-jquery/no-global-selector initHistoryLink( $( 'a.last-modified-bar__text' ) ); + // eslint-disable-next-line no-jquery/no-global-selector + initAmcHistoryLink( $( '.last-modified-bar__text a' ) ); if ( toolbarElement ) { Toolbar.bind( window, toolbarElement ); Toolbar.render( window, toolbarElement ); diff --git a/resources/skins.minerva.scripts/mobileRedirect.js b/resources/skins.minerva.scripts/mobileRedirect.js index 8e8ed6a09..c70dd80e5 100644 --- a/resources/skins.minerva.scripts/mobileRedirect.js +++ b/resources/skins.minerva.scripts/mobileRedirect.js @@ -1,7 +1,7 @@ /* * Warn people if they're trying to switch to desktop but have cookies disabled. */ -module.exports = function () { +module.exports = function ( amcOutreach, currentPage ) { /** * Checks whether cookies are enabled * @method @@ -41,6 +41,35 @@ module.exports = function () { } } + /** + * @method + * @ignore + * @return {boolean|undefined} + */ + function amcDesktopClickHandler() { + var + self = this, + executeWrappedEvent = function () { + if ( desktopViewClick() === false ) { + return false; + } + + window.location = self.href; + }, + amcCampaign = amcOutreach.loadCampaign(), + onDismiss = function () { + executeWrappedEvent(); + }; + + if ( amcCampaign.showIfEligible( amcOutreach.ACTIONS.onDesktopLink, + onDismiss, currentPage.title ) ) { + // prevent default/stop propagation + return false; + } + + return executeWrappedEvent(); + } + // eslint-disable-next-line no-jquery/no-global-selector - $( '#mw-mf-display-toggle' ).on( 'click', desktopViewClick ); + $( '#mw-mf-display-toggle' ).on( 'click', amcDesktopClickHandler ); }; diff --git a/resources/skins.minerva.scripts/talk.js b/resources/skins.minerva.scripts/talk.js index ef8d95140..4123b1e8c 100644 --- a/resources/skins.minerva.scripts/talk.js +++ b/resources/skins.minerva.scripts/talk.js @@ -3,6 +3,8 @@ */ module.exports = function ( mobile ) { var + toast = mobile.toast, + currentPage = mobile.currentPage(), loader = mobile.rlModuleLoader, loadingOverlay = mobile.loadingOverlay, eventBus = mobile.eventBusSingleton, @@ -88,7 +90,7 @@ module.exports = function ( mobile ) { }, // T184273 using `currentPage` because 'wgPageName' // contains underscores instead of spaces. - currentPageTitle: mobile.currentPage().title, + currentPageTitle: currentPage.title, licenseMsg: skin.getLicenseMsg(), eventBus: eventBus, id: id @@ -110,22 +112,46 @@ module.exports = function ( mobile ) { } } ); + function changeHash() { + // eslint-disable-next-line no-jquery/no-class-state + if ( $talk.hasClass( 'add' ) ) { + window.location.hash = '#/talk/new'; + } else { + window.location.hash = '#/talk'; + } + } + + /** + * @method + * @param {JQuery.Event} ev + * @return {undefined} + */ + function amcTalkClickHandler( ev ) { + var + amcOutreach = mobile.amcOutreach, + amcCampaign = amcOutreach.loadCampaign(), + onDismiss = function () { + changeHash(); + toast.show( mw.message( 'mobile-frontend-amc-outreach-dismissed-message' ).text() ); + }; + + // avoiding navigating to original URL + // DO NOT USE stopPropagation or you'll break click tracking in WikimediaEvents + ev.preventDefault(); + + if ( amcCampaign.showIfEligible( amcOutreach.ACTIONS.onTalkLink, onDismiss, currentPage.title, '#/talk' ) ) { + return; + } + + changeHash(); + } + /** * Create route '#/talk' * @ignore */ function init() { - $talk.on( 'click', function ( ev ) { - // eslint-disable-next-line no-jquery/no-class-state - if ( $talk.hasClass( 'add' ) ) { - window.location.hash = '#/talk/new'; - } else { - window.location.hash = '#/talk'; - } - // avoiding navigating to original URL - // DO NOT USE stopPropagation or you'll break click tracking in WikimediaEvents - ev.preventDefault(); - } ); + $talk.on( 'click', amcTalkClickHandler ); } init();