All files / mobile.startup/amcOutreach amcOutreach.js

61.53% Statements 8/13
0% Branches 0/2
0% Functions 0/2
61.53% Lines 8/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81  1x 1x 1x     1x 1x                     1x         1x         1x                                                                                                        
const
	toast = require( '../showOnPageReload' ),
	createPromoCampaign = require( '../promoCampaign/promoCampaign' ),
	amcOutreachDrawer = require( './amcOutreachDrawer' ),
	// MW constants should be kept in sync with onMakeGlobalVariableScript() from
	// MobileFrontendHooks.php
	MW_CONFIG_CAMPAIGN_ACTIVE_NAME = 'wgMFAmcOutreachActive',
	MW_CONFIG_USER_ELIGIBLE_NAME = 'wgMFAmcOutreachUserEligible',
	// This object contains arbitrary actions that are meant to only be shown one
	// time at most. Each action is either 'eligible' or 'ineligible' at any given
	// time.
	//
	// When a new action is desired, it should be added to this object.
	//
	// In other languages, this would be an enum. However, because JS doesn't
	// support enums, the keys/value names are identical in an attempt to mimic
	// some of their functionality. The names in this object are used by
	// promoCampaign to mark when an action has become 'ineligible'.
	ACTIONS = {
		onDesktopLink: 'onDesktopLink',
		onHistoryLink: 'onHistoryLink',
		onTalkLink: 'onTalkLink'
	},
	CAMPAIGN_NAME = 'amc-outreach';
 
// singleton;
let campaign;
 
module.exports = {
	/**
	 * @return {PromoCampaign}
	 */
	loadCampaign: () => {
		if ( campaign ) {
			return campaign;
		}
 
		campaign = createPromoCampaign(
			/**
			 * This callback is executed by promoCampaign's `showIfEligible` method.
			 * promoCampaign will only execute it when an action is 'eligible'.
			 *
			 * @param {string} action Name of one of the actions in the ACTIONS
			 * object. This is used by the drawer to notify promoCampaign when the
			 * action has become 'ineligible' (e.g. after enabling or dismissing the
			 * drawer).
			 * @param {onBeforeHide} onBeforeHide Callback exected after user
			 * dismisses drawer.
			 * @param {string} returnToTitle Title of page to redirect to after user enables
			 * AMC
			 * @param {string} [returnToQuery] Optional query params to add to redirected
			 * URL after user enables AMC. Can also include anchor (e.g.
			 * `foo=bar#/Talk`
			 * @return {Drawer|null}
			 */
			( action, onBeforeHide, returnToTitle, returnToQuery ) => {
				return amcOutreachDrawer(
					action,
					campaign,
					mw.message,
					mw.util,
					toast,
					mw.user.tokens.get( 'csrfToken' ),
					onBeforeHide,
					returnToTitle,
					returnToQuery
				);
			},
			ACTIONS,
			CAMPAIGN_NAME,
			// in minerva desktop, this config will not be set
			!!mw.config.get( MW_CONFIG_CAMPAIGN_ACTIVE_NAME ),
			!!mw.config.get( MW_CONFIG_USER_ELIGIBLE_NAME ),
			mw.storage
		);
 
		return campaign;
	},
	ACTIONS
};