2016-11-16 17:00:33 +00:00
|
|
|
( function ( mw, $ ) {
|
2016-11-10 18:02:29 +00:00
|
|
|
mw.popups.reducers = {};
|
|
|
|
|
2016-11-17 21:41:56 +00:00
|
|
|
/**
|
|
|
|
* Creates the next state tree from the current state tree and some updates.
|
|
|
|
*
|
2016-11-30 12:44:59 +00:00
|
|
|
* N.B. OO.copy doesn't copy Element instances, whereas $.extend does.
|
|
|
|
* However, OO.copy does copy properties whose values are undefined or null,
|
|
|
|
* whereas $.extend doesn't. Since the state tree contains an Element
|
|
|
|
* instance - the preview.activeLink property - we need to use $.extend. But
|
|
|
|
* to copy undefined/null into the state we need to manually iterate over
|
|
|
|
* updates and check with hasOwnProperty to copy over to the new state.
|
|
|
|
*
|
2016-11-17 21:41:56 +00:00
|
|
|
* In [change listeners](/doc/change_listeners.md), for example, we talk about
|
|
|
|
* the previous state and the current state (the `prevState` and `state`
|
|
|
|
* parameters, respectively). Since
|
|
|
|
* [reducers](http://redux.js.org/docs/basics/Reducers.html) take the current
|
|
|
|
* state and an action and make updates, "next state" seems appropriate.
|
|
|
|
*
|
|
|
|
* @param {Object} state
|
|
|
|
* @param {Object} updates
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
function nextState( state, updates ) {
|
|
|
|
var result = $.extend( {}, state ),
|
|
|
|
key;
|
|
|
|
|
|
|
|
for ( key in updates ) {
|
|
|
|
if ( updates.hasOwnProperty( key ) ) {
|
|
|
|
result[key] = updates[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-11-10 18:02:29 +00:00
|
|
|
/**
|
|
|
|
* Reducer for actions that modify the state of the preview model
|
|
|
|
*
|
|
|
|
* @param {Object} state before action
|
|
|
|
* @param {Object} action Redux action that modified state.
|
|
|
|
* Must have `type` property.
|
|
|
|
* @return {Object} state after action
|
|
|
|
*/
|
|
|
|
mw.popups.reducers.preview = function ( state, action ) {
|
|
|
|
if ( state === undefined ) {
|
|
|
|
state = {
|
2016-11-14 19:37:11 +00:00
|
|
|
enabled: undefined,
|
|
|
|
sessionToken: undefined,
|
|
|
|
pageToken: undefined,
|
|
|
|
linkInteractionToken: undefined,
|
2016-11-10 18:02:29 +00:00
|
|
|
activeLink: undefined,
|
2016-11-21 11:08:06 +00:00
|
|
|
activeEvent: undefined,
|
2016-11-25 12:42:02 +00:00
|
|
|
interactionStarted: undefined,
|
2016-11-28 12:00:07 +00:00
|
|
|
shouldShow: false,
|
|
|
|
isUserDwelling: false
|
2016-11-10 18:02:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( action.type ) {
|
|
|
|
case mw.popups.actionTypes.BOOT:
|
2016-11-17 21:41:56 +00:00
|
|
|
return nextState( state, {
|
2016-11-14 19:37:11 +00:00
|
|
|
enabled: action.isUserInCondition,
|
|
|
|
sessionToken: action.sessionToken,
|
|
|
|
pageToken: action.pageToken
|
2016-11-10 18:02:29 +00:00
|
|
|
} );
|
2016-11-16 16:16:43 +00:00
|
|
|
case mw.popups.actionTypes.LINK_DWELL:
|
2016-11-17 21:41:56 +00:00
|
|
|
return nextState( state, {
|
2016-11-16 19:45:10 +00:00
|
|
|
activeLink: action.el,
|
2016-11-21 11:08:06 +00:00
|
|
|
activeEvent: action.event,
|
2016-11-16 16:16:43 +00:00
|
|
|
interactionStarted: action.interactionStarted,
|
2016-11-28 12:00:07 +00:00
|
|
|
linkInteractionToken: action.linkInteractionToken,
|
|
|
|
|
|
|
|
// When the user dwells on a link with their keyboard, a preview is
|
|
|
|
// renderered, and then dwells on another link, the LINK_ABANDON_END
|
|
|
|
// action will be ignored.
|
|
|
|
//
|
|
|
|
// Ensure that all the preview is hidden.
|
|
|
|
shouldShow: false
|
2016-11-16 16:16:43 +00:00
|
|
|
} );
|
2016-11-28 12:00:07 +00:00
|
|
|
case mw.popups.actionTypes.LINK_ABANDON_END:
|
|
|
|
if ( action.el !== state.activeLink ) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* falls through */
|
|
|
|
case mw.popups.actionTypes.PREVIEW_ABANDON_END:
|
|
|
|
if ( !state.isUserDwelling ) {
|
|
|
|
return nextState( state, {
|
|
|
|
activeLink: undefined,
|
|
|
|
activeEvent: undefined,
|
|
|
|
interactionStarted: undefined,
|
|
|
|
linkInteractionToken: undefined,
|
|
|
|
fetchResponse: undefined,
|
|
|
|
shouldShow: false
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2016-12-01 09:58:49 +00:00
|
|
|
return state;
|
|
|
|
|
2016-11-28 12:00:07 +00:00
|
|
|
case mw.popups.actionTypes.PREVIEW_DWELL:
|
2016-11-17 21:41:56 +00:00
|
|
|
return nextState( state, {
|
2016-11-28 12:00:07 +00:00
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.PREVIEW_ABANDON_START:
|
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: false
|
2016-11-16 16:16:43 +00:00
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.FETCH_START:
|
2016-11-17 21:41:56 +00:00
|
|
|
return nextState( state, {
|
2016-11-16 16:16:43 +00:00
|
|
|
fetchResponse: undefined
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.FETCH_END:
|
2016-11-25 12:42:02 +00:00
|
|
|
if ( action.el === state.activeLink ) {
|
|
|
|
return nextState( state, {
|
|
|
|
fetchResponse: action.result,
|
|
|
|
shouldShow: true
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* falls through */
|
2016-11-10 18:02:29 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reducer for actions that modify the state of the view
|
|
|
|
*
|
|
|
|
* @param {Object} state before action
|
|
|
|
* @param {Object} action Redux action that modified state.
|
|
|
|
* Must have `type` property.
|
|
|
|
* @return {Object} state after action
|
|
|
|
*/
|
|
|
|
mw.popups.reducers.renderer = function ( state, action ) {
|
|
|
|
if ( state === undefined ) {
|
|
|
|
state = {
|
2016-11-16 16:16:43 +00:00
|
|
|
isAnimating: false,
|
2016-11-10 18:02:29 +00:00
|
|
|
isInteractive: false,
|
|
|
|
showSettings: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( action.type ) {
|
|
|
|
case mw.popups.actionTypes.PREVIEW_ANIMATING:
|
|
|
|
return $.extend( {}, state, {
|
2016-11-16 16:16:43 +00:00
|
|
|
isAnimating: true,
|
|
|
|
isInteractive: false,
|
|
|
|
showSettings: false
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.PREVIEW_INTERACTIVE:
|
|
|
|
return $.extend( OO.copy( state ), {
|
|
|
|
isAnimating: false,
|
|
|
|
isInteractive: true,
|
|
|
|
showSettings: false
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.PREVIEW_CLICK:
|
|
|
|
return $.extend( OO.copy( state ), {
|
|
|
|
isAnimating: false,
|
|
|
|
isInteractive: false,
|
|
|
|
showSettings: false
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.COG_CLICK:
|
|
|
|
return $.extend( OO.copy( state ), {
|
|
|
|
isAnimating: true,
|
|
|
|
isInteractive: false,
|
|
|
|
showSettings: true
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.SETTINGS_DIALOG_INTERACTIVE:
|
|
|
|
return $.extend( OO.copy( state ), {
|
|
|
|
isAnimating: false,
|
|
|
|
isInteractive: true,
|
|
|
|
showSettings: true
|
|
|
|
} );
|
|
|
|
case mw.popups.actionTypes.SETTINGS_DIALOG_CLOSED:
|
|
|
|
return $.extend( OO.copy( state ), {
|
|
|
|
isAnimating: false,
|
|
|
|
isInteractive: false,
|
|
|
|
showSettings: false
|
2016-11-10 18:02:29 +00:00
|
|
|
} );
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
2016-11-16 17:00:33 +00:00
|
|
|
}( mediaWiki, jQuery ) );
|