Files
Iris/src/js/services/ui/middleware.js

248 lines
8.6 KiB
JavaScript
Raw Normal View History

2016-11-11 16:47:32 +13:00
2019-04-22 21:39:40 +12:00
import ReactGA from 'react-ga';
import * as helpers from '../../helpers';
2016-11-11 16:47:32 +13:00
2016-12-14 08:29:24 +13:00
var uiActions = require('./actions.js')
var mopidyActions = require('../mopidy/actions.js')
2016-11-11 16:47:32 +13:00
const UIMiddleware = (function(){
/**
* The actual middleware inteceptor
**/
return store => next => action => {
switch(action.type){
case 'MOPIDY_STATE':
2017-10-30 11:59:45 +13:00
// Get the current track from our index
var tracks = store.getState().core.tracks;
var current_track_uri = store.getState().core.current_track;
var current_track = null;
if (tracks[current_track_uri] !== undefined){
current_track = tracks[current_track_uri];
}
next(action);
2017-01-04 15:24:13 +13:00
break
2018-06-28 08:12:03 +12:00
case 'SET_WINDOW_TITLE':
var window_title = "";
if (action.play_state){
var play_state = action.play_state;
} else {
var play_state = store.getState().mopidy.play_state;
}
if (action.title){
var title = action.title;
} else {
var title = store.getState().ui.window_title;
}
document.title = window_title+' '+title;
next(action);
break;
2017-01-24 10:13:12 +13:00
case 'OPEN_MODAL':
if (store.getState().ui.allow_reporting){
ReactGA.event({ category: 'Modal', action: 'Opened', label: action.modal.name });
}
2017-01-24 10:13:12 +13:00
$('body').addClass('modal-open')
2017-07-20 09:34:21 +12:00
store.dispatch(uiActions.hideContextMenu())
store.dispatch(uiActions.hideTouchContextMenu())
2017-01-24 10:13:12 +13:00
next(action)
break
case 'CLOSE_MODAL':
if (store.getState().ui.allow_reporting){
ReactGA.event({ category: 'Modal', action: 'Closed', label: null });
}
2017-01-24 10:13:12 +13:00
$('body').removeClass('modal-open')
next(action)
break
case 'HIDE_CONTEXT_MENU':
if (store.getState().ui.context_menu){
setTimeout(
() => {
store.dispatch(uiActions.removeContextMenu())
},
200
);
next(action);
}
break;
case 'HIDE_TOUCH_CONTEXT_MENU':
if (store.getState().ui.context_menu){
setTimeout(
() => {
store.dispatch(uiActions.removeTouchContextMenu())
},
200
);
next(action);
}
break;
case 'BROWSER_NOTIFICATION':
var notification = window.Notification || window.mozNotification || window.webkitNotification;
if ('undefined' === typeof notification) return false;
if ('undefined' !== typeof notification) notification.requestPermission(function(permission){});
// handle nested data objects
var data = {}
2017-10-10 13:05:28 +13:00
if (typeof(action.data) ) data = action.data
if (typeof(data.data) ) data = Object.assign({}, data, data.data)
// construct our browser notification
var title = '';
var options = {
body: '',
dir: 'auto',
lang: 'EN',
tag: 'iris'
};
2017-10-10 13:05:28 +13:00
if (data.title ) title = data.title;
if (data.body ) options.body = data.body;
if (data.icon ) options.icon = data.icon;
// make it so
2017-10-10 13:05:28 +13:00
var notification = new notification(title, options );
break
2016-12-13 11:08:13 +13:00
case 'CREATE_NOTIFICATION':
// start a timeout to close this notification
2017-05-29 16:29:49 +12:00
if (!action.notification.sticky){
var timeout = setTimeout(
function(){
store.dispatch(uiActions.closeNotification(action.notification.key))
2017-05-29 16:29:49 +12:00
},
action.notification.duration * 1000
2017-05-29 16:29:49 +12:00
)
}
next(action);
break;
2017-05-29 16:29:49 +12:00
case 'CLOSE_NOTIFICATION':
var notifications = Object.assign({}, store.getState().ui.notifications);
2017-05-29 16:29:49 +12:00
// start a timeout to remove this notification
// This gives us time to animate out the notification before we remove the data
var timeout = setTimeout(
function(){
store.dispatch(uiActions.removeNotification(action.key))
},
200
)
next(action);
break;
2017-05-29 16:29:49 +12:00
case 'REMOVE_NOTIFICATION':
// Manual removal
if (action.manual){
var notifications = Object.assign({}, store.getState().ui.notifications);
// If a broadcast, add to suppressed_broadcasts
if (notifications[action.key] && notifications[action.key].type == 'broadcast'){
store.dispatch({
type: 'SUPPRESS_BROADCAST',
key: action.key
})
}
}
next(action);
break;
2017-05-29 16:29:49 +12:00
case 'BROADCASTS_LOADED':
var suppressed_broadcasts = []
2018-07-13 16:48:41 +12:00
if (store.getState().ui.suppressed_broadcasts !== undefined){
2017-05-29 16:29:49 +12:00
suppressed_broadcasts = store.getState().ui.suppressed_broadcasts
}
for (var i = 0; i < action.broadcasts.length; i++){
var broadcast = action.broadcasts[i]
if (!suppressed_broadcasts.includes(broadcast.key)){
if (broadcast.message){
var data = {
key: (broadcast.key ? broadcast.key : null),
title: (broadcast.title ? broadcast.title : null),
2019-02-05 22:35:52 +13:00
content: (broadcast.message ? broadcast.message : null),
links: (broadcast.links ? broadcast.links : null),
type: 'broadcast',
sticky: true
}
store.dispatch(uiActions.createNotification(data));
}
}
2017-05-29 16:29:49 +12:00
}
2016-12-13 11:08:13 +13:00
next(action)
break
2017-08-10 16:41:19 +12:00
case 'START_PROCESS':
store.dispatch({
2017-08-11 21:59:14 +12:00
type: action.key,
data: action.data
2017-08-10 16:41:19 +12:00
})
store.dispatch({
type: action.key+'_STARTED'
})
next(action)
break
case 'RESUME_PROCESS':
store.dispatch({
type: action.key,
data: store.getState().ui.processes[action.key].data
})
2017-08-10 16:41:19 +12:00
next(action)
break
case 'PROCESS_CANCELLED':
2017-08-10 16:41:19 +12:00
store.dispatch({
type: action.key+'_CANCELLED'
})
next(action)
break
2018-02-28 09:15:34 +13:00
case 'PROCESS_FINISHING':
// start a timeout to remove this notification
// This gives us time to animate out the notification before we remove the data
var timeout = setTimeout(
function(){
store.dispatch(uiActions.processFinished(action.key))
},
200
)
next(action);
break;
case 'PROCESS_FINISHED':
store.dispatch({
type: action.key+'_FINISHED'
2018-02-28 09:15:34 +13:00
});
next(action);
break;
2016-11-11 16:47:32 +13:00
// This action is irrelevant to us, pass it on to the next middleware
default:
2016-12-13 11:08:13 +13:00
return next(action)
2016-11-11 16:47:32 +13:00
}
}
})();
export default UIMiddleware