Jhernandez has uploaded a new change for review. (
https://gerrit.wikimedia.org/r/337833 )
Change subject: Hygiene: Remove unnecessary IIFE and use proper requires
......................................................................
Hygiene: Remove unnecessary IIFE and use proper requires
In index.js. Instead of using the global variable/object popups, require
things from their files so that we can remove the global variables when
we can run qunit tests with commonjs in node.
Change-Id: I85408f01eca27f97cf46b2076176fcc16c037829
---
M src/index.js
1 file changed, 149 insertions(+), 139 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Popups
refs/changes/33/337833/1
diff --git a/src/index.js b/src/index.js
index bdeb453..191371f 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,5 +1,24 @@
-( function ( mw, popups, Redux, ReduxThunk, $ ) {
- var BLACKLISTED_LINKS = [
+var mw = mediaWiki,
+ Redux = require( 'redux' ),
+ ReduxThunk = require( 'redux-thunk' ),
+
+ 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' ),
+ checkin = require( './checkin' ),
+ renderer = require( './renderer' ),
+
+ changeListeners = require( './changeListeners' ),
+ actions = require( './actions' ),
+ reducers = require( './reducers' ),
+
+ BLACKLISTED_LINKS = [
'.extiw',
'.image',
'.new',
@@ -9,152 +28,143 @@
'.cancelLink a'
];
- /**
- * 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 popups.gateway.createRESTBaseGateway( $.ajax );
- }
- return popups.gateway.createMediaWikiApiGateway( new mw.Api() );
+/**
+ * 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 );
+ }
+ return createMediaWikiApiGateway( new mw.Api() );
+}
+
+/**
+ * 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 {mw.eventLog.Schema} schema
+ * @param {ext.popups.UserSettings} userSettings
+ * @param {Function} settingsDialog
+ * @param {ext.popups.PreviewBehavior} previewBehavior
+ */
+function registerChangeListeners( store, actions, schema, userSettings,
settingsDialog, previewBehavior ) {
+ registerChangeListener( store, changeListeners.footerLink( actions ) );
+ registerChangeListener( store, changeListeners.linkTitle() );
+ registerChangeListener( store, changeListeners.render( previewBehavior
) );
+ registerChangeListener( store, changeListeners.eventLogging( actions,
schema ) );
+ registerChangeListener( store, changeListeners.syncUserSettings(
userSettings ) );
+ registerChangeListener( store, changeListeners.settings( actions,
settingsDialog ) );
+}
+
+/**
+ * Binds the actions (or "action creators") to the
+ * [store](http://redux.js.org/docs/api/Store.html#store).
+ *
+ * @param {Redux.Store} store
+ * @return {Object}
+ */
+function createBoundActions( store ) {
+ return Redux.bindActionCreators( actions, store.dispatch );
+}
+
+/**
+ * Creates the reducer for all actions.
+ *
+ * @return {Redux.Reducer}
+ */
+function createRootReducer() {
+ return Redux.combineReducers( reducers );
+}
+
+/*
+ * 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:
+ * - Setup `checkin` actions
+ * - 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,
+ actions,
+
+ // So-called "services".
+ generateToken = mw.user.generateRandomSessionId,
+ gateway = createGateway( mw.config ),
+ userSettings,
+ settingsDialog,
+ isEnabled,
+ schema,
+ previewBehavior;
+
+ userSettings = createUserSettings( mw.storage );
+ settingsDialog = createSettingsDialogRenderer();
+ schema = createSchema( mw.config, window );
+
+ isEnabled = createIsEnabled( mw.user, userSettings, mw.config );
+
+ // 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;
}
- /**
- * Subscribes the registered change listeners to the
- * [store](http://redux.js.org/docs/api/Store.html#store).
- *
- * Change listeners are registered by setting a property on
- * `popups.changeListeners`.
- *
- * @param {Redux.Store} store
- * @param {Object} actions
- * @param {mw.eventLog.Schema} schema
- * @param {ext.popups.UserSettings} userSettings
- * @param {Function} settingsDialog
- * @param {ext.popups.PreviewBehavior} previewBehavior
- */
- function registerChangeListeners( store, actions, schema, userSettings,
settingsDialog, previewBehavior ) {
+ store = Redux.createStore(
+ createRootReducer(),
+ compose( Redux.applyMiddleware(
+ ReduxThunk.default
+ ) )
+ );
+ actions = createBoundActions( store );
- // Sugar.
- var changeListeners = popups.changeListeners,
- registerChangeListener = popups.registerChangeListener;
+ previewBehavior = createPreviewBehavior( mw.config, mw.user, actions );
- registerChangeListener( store, changeListeners.footerLink(
actions ) );
- registerChangeListener( store, changeListeners.linkTitle() );
- registerChangeListener( store, changeListeners.render(
previewBehavior ) );
- registerChangeListener( store, changeListeners.eventLogging(
actions, schema ) );
- registerChangeListener( store,
changeListeners.syncUserSettings( userSettings ) );
- registerChangeListener( store, changeListeners.settings(
actions, settingsDialog ) );
- }
+ registerChangeListeners( store, actions, schema, userSettings,
settingsDialog, previewBehavior );
- /**
- * Binds the actions (or "action creators") to the
- * [store](http://redux.js.org/docs/api/Store.html#store).
- *
- * @param {Redux.Store} store
- * @return {Object}
- */
- function createBoundActions( store ) {
- return Redux.bindActionCreators( popups.actions, store.dispatch
);
- }
+ actions.boot(
+ isEnabled,
+ mw.user,
+ userSettings,
+ generateToken,
+ mw.config
+ );
- /**
- * Creates the reducer for all actions.
- *
- * @return {Redux.Reducer}
- */
- function createRootReducer() {
- return Redux.combineReducers( popups.reducers );
- }
+ mw.hook( 'wikipage.content' ).add( function ( $container ) {
+ var previewLinks =
+ processLinks(
+ $container,
+ BLACKLISTED_LINKS,
+ mw.config
+ );
- /*
- * 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:
- * - Setup `checkin` actions
- * - 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,
- actions,
+ checkin.setupActions( actions.checkin );
- // So-called "services".
- generateToken = mw.user.generateRandomSessionId,
- gateway = createGateway( mw.config ),
- userSettings,
- settingsDialog,
- isEnabled,
- schema,
- previewBehavior;
+ renderer.init();
- userSettings = popups.createUserSettings( mw.storage );
- settingsDialog = popups.createSettingsDialogRenderer();
- schema = popups.createSchema( mw.config, window );
+ previewLinks
+ .on( 'mouseover focus', function ( event ) {
+ actions.linkDwell( this, event, gateway,
generateToken );
+ } )
+ .on( 'mouseout blur', function () {
+ actions.abandon( this );
+ } )
+ .on( 'click', function () {
+ actions.linkClick( this );
+ } );
- isEnabled = popups.isEnabled( mw.user, userSettings, mw.config
);
-
- // 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;
- }
-
- store = Redux.createStore(
- createRootReducer(),
- compose( Redux.applyMiddleware(
- ReduxThunk.default
- ) )
- );
- actions = createBoundActions( store );
-
- previewBehavior = popups.createPreviewBehavior( mw.config,
mw.user, actions );
-
- registerChangeListeners( store, actions, schema, userSettings,
settingsDialog, previewBehavior );
-
- actions.boot(
- isEnabled,
- mw.user,
- userSettings,
- generateToken,
- mw.config
- );
-
- mw.hook( 'wikipage.content' ).add( function ( $container ) {
- var previewLinks =
- popups.processLinks(
- $container,
- BLACKLISTED_LINKS,
- mw.config
- );
-
- popups.checkin.setupActions( actions.checkin );
-
- popups.renderer.init();
-
- previewLinks
- .on( 'mouseover focus', function ( event ) {
- actions.linkDwell( this, event,
gateway, generateToken );
- } )
- .on( 'mouseout blur', function () {
- actions.abandon( this );
- } )
- .on( 'click', function () {
- actions.linkClick( this );
- } );
-
- } );
} );
+} );
- // FIXME: Currently needs to be exposed for testing purposes
- mw.popups = popups;
- window.Redux = Redux;
- window.ReduxThunk = ReduxThunk;
-}( mediaWiki, require( './popups' ), require( 'redux' ), require(
'redux-thunk' ), jQuery ) );
+// FIXME: Currently needs to be exposed for testing purposes
+mw.popups = require( './popups' );
+window.Redux = Redux;
+window.ReduxThunk = ReduxThunk;
--
To view, visit https://gerrit.wikimedia.org/r/337833
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I85408f01eca27f97cf46b2076176fcc16c037829
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Popups
Gerrit-Branch: master
Gerrit-Owner: Jhernandez <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits