2017-05-26 17:35:07 +00:00
|
|
|
/**
|
|
|
|
* @module popups
|
|
|
|
*/
|
|
|
|
|
2017-02-14 20:19:12 +00:00
|
|
|
var mw = mediaWiki,
|
|
|
|
$ = jQuery,
|
|
|
|
Redux = require( 'redux' ),
|
|
|
|
ReduxThunk = require( 'redux-thunk' ),
|
|
|
|
constants = require( './constants' ),
|
|
|
|
|
|
|
|
createRESTBaseGateway = require( './gateway/rest' ),
|
|
|
|
createMediaWikiApiGateway = require( './gateway/mediawiki' ),
|
|
|
|
createUserSettings = require( './userSettings' ),
|
|
|
|
createPreviewBehavior = require( './previewBehavior' ),
|
|
|
|
createSchema = require( './schema' ),
|
|
|
|
createSettingsDialogRenderer = require( './settingsDialog' ),
|
|
|
|
registerChangeListener = require( './changeListener' ),
|
|
|
|
createIsEnabled = require( './isEnabled' ),
|
|
|
|
processLinks = require( './processLinks' ),
|
|
|
|
renderer = require( './renderer' ),
|
2017-03-07 00:27:38 +00:00
|
|
|
statsvInstrumentation = require( './statsvInstrumentation' ),
|
2017-02-14 20:19:12 +00:00
|
|
|
|
|
|
|
changeListeners = require( './changeListeners' ),
|
|
|
|
actions = require( './actions' ),
|
|
|
|
reducers = require( './reducers' ),
|
|
|
|
|
|
|
|
BLACKLISTED_LINKS = [
|
|
|
|
'.extiw',
|
|
|
|
'.image',
|
|
|
|
'.new',
|
|
|
|
'.internal',
|
|
|
|
'.external',
|
|
|
|
'.oo-ui-buttonedElement-button',
|
|
|
|
'.cancelLink a'
|
|
|
|
];
|
|
|
|
|
2017-04-27 12:53:15 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Function} ext.popups.EventTracker
|
|
|
|
*
|
|
|
|
* An analytics event tracker like `mw.track`.
|
|
|
|
*/
|
|
|
|
|
2017-02-14 20:19:12 +00:00
|
|
|
/**
|
|
|
|
* Creates a gateway with sensible values for the dependencies.
|
|
|
|
*
|
|
|
|
* @param {mw.Map} config
|
|
|
|
* @return {ext.popups.Gateway}
|
|
|
|
*/
|
|
|
|
function createGateway( config ) {
|
|
|
|
if ( config.get( 'wgPopupsAPIUseRESTBase' ) ) {
|
|
|
|
return createRESTBaseGateway( $.ajax, constants );
|
2016-11-16 19:45:10 +00:00
|
|
|
}
|
2017-02-14 20:19:12 +00:00
|
|
|
return createMediaWikiApiGateway( new mw.Api(), constants );
|
|
|
|
}
|
|
|
|
|
2017-04-27 12:53:15 +00:00
|
|
|
/**
|
|
|
|
* Gets the appropriate analytics event tracker for logging metrics to StatsD
|
|
|
|
* via the [the "StatsD timers and counters" analytics event protocol][0].
|
|
|
|
*
|
|
|
|
* If logging metrics to StatsD is enabled for the user, then the appriopriate
|
|
|
|
* function is `mw.track`; otherwise it's `$.noop`.
|
|
|
|
*
|
|
|
|
* [0]: https://github.com/wikimedia/mediawiki-extensions-WikimediaEvents/blob/master/modules/ext.wikimediaEvents.statsd.js
|
|
|
|
*
|
|
|
|
* @param {Object} user
|
|
|
|
* @param {Object} config
|
|
|
|
* @param {Object} experiments
|
|
|
|
* @return {ext.popups.EventTracker}
|
|
|
|
*/
|
|
|
|
function getStatsvTracker( user, config, experiments ) {
|
|
|
|
return statsvInstrumentation.isEnabled( user, config, experiments ) ? mw.track : $.noop;
|
|
|
|
}
|
|
|
|
|
2017-02-14 20:19:12 +00:00
|
|
|
/**
|
|
|
|
* Subscribes the registered change listeners to the
|
|
|
|
* [store](http://redux.js.org/docs/api/Store.html#store).
|
|
|
|
*
|
|
|
|
* @param {Redux.Store} store
|
|
|
|
* @param {Object} actions
|
|
|
|
* @param {ext.popups.UserSettings} userSettings
|
|
|
|
* @param {Function} settingsDialog
|
|
|
|
* @param {ext.popups.PreviewBehavior} previewBehavior
|
2017-04-27 12:53:15 +00:00
|
|
|
* @param {ext.popups.EventTracker} statsvTracker
|
2017-02-14 20:19:12 +00:00
|
|
|
*/
|
2017-04-27 12:53:15 +00:00
|
|
|
function registerChangeListeners( store, actions, userSettings, settingsDialog, previewBehavior, statsvTracker ) {
|
2017-02-14 20:19:12 +00:00
|
|
|
registerChangeListener( store, changeListeners.footerLink( actions ) );
|
|
|
|
registerChangeListener( store, changeListeners.linkTitle() );
|
|
|
|
registerChangeListener( store, changeListeners.render( previewBehavior ) );
|
2017-04-27 12:53:15 +00:00
|
|
|
registerChangeListener( store, changeListeners.statsv( actions, statsvTracker ) );
|
2017-02-14 20:19:12 +00:00
|
|
|
registerChangeListener( store, changeListeners.syncUserSettings( userSettings ) );
|
|
|
|
registerChangeListener( store, changeListeners.settings( actions, settingsDialog ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initialize the application by:
|
|
|
|
* 1. Creating the state store
|
|
|
|
* 2. Binding the actions to such store
|
|
|
|
* 3. Trigger the boot action to bootstrap the system
|
|
|
|
* 4. When the page content is ready:
|
|
|
|
* - Process the eligible links for page previews
|
|
|
|
* - Initialize the renderer
|
|
|
|
* - Bind hover and click events to the eligible links to trigger actions
|
|
|
|
*/
|
|
|
|
mw.requestIdleCallback( function () {
|
|
|
|
var compose = Redux.compose,
|
|
|
|
store,
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions,
|
2017-02-14 20:19:12 +00:00
|
|
|
|
|
|
|
// So-called "services".
|
|
|
|
generateToken = mw.user.generateRandomSessionId,
|
|
|
|
gateway = createGateway( mw.config ),
|
|
|
|
userSettings,
|
|
|
|
settingsDialog,
|
2017-04-27 12:53:15 +00:00
|
|
|
statsvTracker,
|
2017-02-14 20:19:12 +00:00
|
|
|
isEnabled,
|
|
|
|
schema,
|
2017-04-27 12:53:15 +00:00
|
|
|
previewBehavior;
|
2017-02-14 20:19:12 +00:00
|
|
|
|
|
|
|
userSettings = createUserSettings( mw.storage );
|
|
|
|
settingsDialog = createSettingsDialogRenderer();
|
2017-04-27 12:53:15 +00:00
|
|
|
statsvTracker = getStatsvTracker( mw.user, mw.config, mw.experiments );
|
2017-02-14 20:19:12 +00:00
|
|
|
|
2017-02-27 19:31:34 +00:00
|
|
|
isEnabled = createIsEnabled( mw.user, userSettings, mw.config, mw.experiments );
|
2017-02-14 20:19:12 +00:00
|
|
|
|
|
|
|
// If debug mode is enabled, then enable Redux DevTools.
|
|
|
|
if ( mw.config.get( 'debug' ) === true ) {
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
|
|
|
compose = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
|
2016-11-14 19:37:11 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 20:19:12 +00:00
|
|
|
store = Redux.createStore(
|
2017-03-14 11:33:15 +00:00
|
|
|
Redux.combineReducers( reducers ),
|
2017-02-14 20:19:12 +00:00
|
|
|
compose( Redux.applyMiddleware(
|
|
|
|
ReduxThunk.default
|
|
|
|
) )
|
|
|
|
);
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions = Redux.bindActionCreators( actions, store.dispatch );
|
2017-02-14 20:19:12 +00:00
|
|
|
|
2017-03-14 11:28:58 +00:00
|
|
|
previewBehavior = createPreviewBehavior( mw.config, mw.user, boundActions );
|
2017-02-14 20:19:12 +00:00
|
|
|
|
2017-03-07 00:27:38 +00:00
|
|
|
registerChangeListeners(
|
2017-03-16 21:03:21 +00:00
|
|
|
store, boundActions, userSettings, settingsDialog,
|
2017-04-27 12:53:15 +00:00
|
|
|
previewBehavior, statsvTracker
|
2017-03-07 00:27:38 +00:00
|
|
|
);
|
2017-02-14 20:19:12 +00:00
|
|
|
|
2017-03-16 21:03:21 +00:00
|
|
|
// Load EventLogging schema if possible...
|
|
|
|
mw.loader.using( 'ext.eventLogging.Schema' ).done( function () {
|
|
|
|
schema = createSchema( mw.config, window );
|
2017-04-25 11:26:49 +00:00
|
|
|
registerChangeListener( store, changeListeners.eventLogging( boundActions, schema, statsvTracker ) );
|
2017-03-16 21:03:21 +00:00
|
|
|
} );
|
|
|
|
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions.boot(
|
2017-02-14 20:19:12 +00:00
|
|
|
isEnabled,
|
|
|
|
mw.user,
|
|
|
|
userSettings,
|
|
|
|
generateToken,
|
|
|
|
mw.config
|
|
|
|
);
|
|
|
|
|
|
|
|
mw.hook( 'wikipage.content' ).add( function ( $container ) {
|
|
|
|
var previewLinks =
|
|
|
|
processLinks(
|
|
|
|
$container,
|
|
|
|
BLACKLISTED_LINKS,
|
|
|
|
mw.config
|
|
|
|
);
|
|
|
|
|
|
|
|
renderer.init();
|
|
|
|
|
|
|
|
previewLinks
|
2017-03-08 17:08:16 +00:00
|
|
|
.on( 'mouseover keyup', function ( event ) {
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions.linkDwell( this, event, gateway, generateToken );
|
2017-02-14 20:19:12 +00:00
|
|
|
} )
|
|
|
|
.on( 'mouseout blur', function () {
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions.abandon( this );
|
2017-02-14 20:19:12 +00:00
|
|
|
} )
|
|
|
|
.on( 'click', function () {
|
2017-03-14 11:28:58 +00:00
|
|
|
boundActions.linkClick( this );
|
2017-02-14 20:19:12 +00:00
|
|
|
} );
|
2016-11-16 17:00:33 +00:00
|
|
|
|
2016-11-08 10:05:40 +00:00
|
|
|
} );
|
2017-02-14 20:19:12 +00:00
|
|
|
} );
|
2016-11-08 10:05:40 +00:00
|
|
|
|
2017-02-14 20:19:12 +00:00
|
|
|
window.Redux = Redux;
|
|
|
|
window.ReduxThunk = ReduxThunk;
|