2017-02-02 02:24:12 +00:00
|
|
|
var actionTypes = require( './../actionTypes' ),
|
|
|
|
nextState = require( './nextState' );
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +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
|
|
|
|
*/
|
|
|
|
module.exports = function ( state, action ) {
|
|
|
|
if ( state === undefined ) {
|
|
|
|
state = {
|
|
|
|
enabled: undefined,
|
|
|
|
activeLink: undefined,
|
|
|
|
activeEvent: undefined,
|
|
|
|
activeToken: '',
|
|
|
|
shouldShow: false,
|
|
|
|
isUserDwelling: false
|
|
|
|
};
|
|
|
|
}
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
switch ( action.type ) {
|
|
|
|
case actionTypes.BOOT:
|
|
|
|
return nextState( state, {
|
|
|
|
enabled: action.isEnabled
|
|
|
|
} );
|
|
|
|
case actionTypes.SETTINGS_CHANGE:
|
|
|
|
return nextState( state, {
|
|
|
|
enabled: action.enabled
|
|
|
|
} );
|
|
|
|
case actionTypes.LINK_DWELL:
|
|
|
|
// New interaction
|
|
|
|
if ( action.el !== state.activeLink ) {
|
2016-12-13 18:04:03 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
activeLink: action.el,
|
|
|
|
activeEvent: action.event,
|
|
|
|
activeToken: action.token,
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
// When the user dwells on a link with their keyboard, a preview is
|
|
|
|
// renderered, and then dwells on another link, the ABANDON_END
|
|
|
|
// action will be ignored.
|
|
|
|
//
|
|
|
|
// Ensure that all the preview is hidden.
|
|
|
|
shouldShow: false,
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
|
|
|
} else {
|
|
|
|
// Dwelling back into the same link
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
case actionTypes.ABANDON_END:
|
|
|
|
if ( action.token === state.activeToken && !state.isUserDwelling ) {
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
activeLink: undefined,
|
|
|
|
activeToken: undefined,
|
|
|
|
activeEvent: undefined,
|
|
|
|
fetchResponse: undefined,
|
|
|
|
shouldShow: false
|
2016-12-13 10:25:17 +00:00
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
|
|
|
return state;
|
|
|
|
|
|
|
|
case actionTypes.PREVIEW_DWELL:
|
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: true
|
|
|
|
} );
|
2017-01-09 19:08:46 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
case actionTypes.ABANDON_START:
|
|
|
|
return nextState( state, {
|
|
|
|
isUserDwelling: false
|
|
|
|
} );
|
|
|
|
|
|
|
|
case actionTypes.FETCH_START:
|
|
|
|
return nextState( state, {
|
|
|
|
fetchResponse: undefined
|
|
|
|
} );
|
|
|
|
case actionTypes.FETCH_END:
|
|
|
|
if ( action.el === state.activeLink ) {
|
2016-12-13 10:25:17 +00:00
|
|
|
return nextState( state, {
|
2017-02-02 02:24:12 +00:00
|
|
|
fetchResponse: action.result,
|
|
|
|
shouldShow: true
|
2016-12-13 10:25:17 +00:00
|
|
|
} );
|
2017-02-02 02:24:12 +00:00
|
|
|
}
|
2016-12-13 10:25:17 +00:00
|
|
|
|
2017-02-02 02:24:12 +00:00
|
|
|
/* falls through */
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|