2019-06-14 00:20:14 +00:00
import {
2019-07-29 15:42:42 +00:00
addMr ,
2019-06-14 00:20:14 +00:00
authorizeUser ,
2019-07-29 15:42:42 +00:00
changeSelectedMr ,
2019-06-14 00:20:14 +00:00
logoutUser ,
postComment ,
2019-07-29 15:42:42 +00:00
saveComment ,
2019-06-14 00:20:14 +00:00
toggleForm ,
2019-07-29 15:42:42 +00:00
} from '../components' ;
import {
CHANGE _MR _ID _BUTTON ,
2019-06-14 00:20:14 +00:00
COLLAPSE _BUTTON ,
COMMENT _BUTTON ,
LOGIN ,
LOGOUT ,
2019-07-29 15:42:42 +00:00
MR _ID _BUTTON ,
} from '../shared' ;
2019-06-14 00:20:14 +00:00
import { state } from './state' ;
2019-07-29 15:42:42 +00:00
import debounce from './utils' ;
2019-06-14 00:20:14 +00:00
const noop = ( ) => { } ;
2019-07-29 15:42:42 +00:00
// State needs to be bound here to be acted on
// because these are called by click events and
// as such are called with only the `event` object
const eventLookup = id => {
2019-06-14 00:20:14 +00:00
switch ( id ) {
2019-07-29 15:42:42 +00:00
case CHANGE _MR _ID _BUTTON :
return ( ) => {
saveComment ( ) ;
changeSelectedMr ( state ) ;
} ;
2019-06-14 00:20:14 +00:00
case COLLAPSE _BUTTON :
return toggleForm ;
case COMMENT _BUTTON :
return postComment . bind ( null , state ) ;
case LOGIN :
return authorizeUser . bind ( null , state ) ;
case LOGOUT :
2019-07-29 15:42:42 +00:00
return ( ) => {
saveComment ( ) ;
logoutUser ( state ) ;
} ;
case MR _ID _BUTTON :
return addMr . bind ( null , state ) ;
2019-06-14 00:20:14 +00:00
default :
return noop ;
}
} ;
const updateWindowSize = wind => {
state . innerWidth = wind . innerWidth ;
state . innerHeight = wind . innerHeight ;
} ;
2019-07-29 15:42:42 +00:00
const initializeGlobalListeners = ( ) => {
window . addEventListener ( 'resize' , debounce ( updateWindowSize . bind ( null , window ) , 200 ) ) ;
window . addEventListener ( 'beforeunload' , event => {
if ( state . usingGracefulStorage ) {
// if there is no browser storage support, reloading will lose the comment; this way, the user will be warned
// we assign the return value because it is required by Chrome see: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload#Example,
event . preventDefault ( ) ;
/* eslint-disable-next-line no-param-reassign */
event . returnValue = '' ;
}
saveComment ( ) ;
} ) ;
} ;
export { eventLookup , initializeGlobalListeners } ;