2016-11-11 16:47:32 +13:00
|
|
|
|
2019-04-22 21:39:40 +12:00
|
|
|
import ReactGA from 'react-ga';
|
2019-09-05 15:36:11 +12:00
|
|
|
const uiActions = require('./actions.js');
|
2016-11-11 16:47:32 +13:00
|
|
|
|
2020-02-13 10:41:20 +13:00
|
|
|
const UIMiddleware = (function () {
|
2019-09-05 15:36:11 +12:00
|
|
|
/**
|
2016-11-11 16:47:32 +13:00
|
|
|
* The actual middleware inteceptor
|
2019-09-05 15:36:11 +12:00
|
|
|
* */
|
|
|
|
|
return (store) => (next) => (action) => {
|
2020-02-13 10:41:20 +13:00
|
|
|
switch (action.type) {
|
2019-09-05 15:36:11 +12:00
|
|
|
case 'MOPIDY_STATE':
|
|
|
|
|
|
|
|
|
|
// Get the current track from our index
|
|
|
|
|
var { tracks } = store.getState().core;
|
|
|
|
|
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);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'SET_WINDOW_TITLE':
|
|
|
|
|
|
|
|
|
|
var window_title = '';
|
|
|
|
|
|
|
|
|
|
if (action.play_state) {
|
|
|
|
|
var { play_state } = action;
|
|
|
|
|
} else {
|
|
|
|
|
var { play_state } = store.getState().mopidy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (action.title) {
|
|
|
|
|
var { title } = action;
|
|
|
|
|
} else {
|
|
|
|
|
var title = store.getState().ui.window_title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
document.title = `${window_title} ${title}`;
|
|
|
|
|
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'OPEN_MODAL':
|
|
|
|
|
if (store.getState().ui.allow_reporting) {
|
2020-06-12 22:42:49 +12:00
|
|
|
ReactGA.event({ category: 'Modal', action: 'Opened', label: action.modal.name });
|
|
|
|
|
}
|
2019-09-05 15:36:11 +12:00
|
|
|
$('body').addClass('modal-open');
|
|
|
|
|
store.dispatch(uiActions.hideContextMenu());
|
|
|
|
|
store.dispatch(uiActions.hideTouchContextMenu());
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'CLOSE_MODAL':
|
|
|
|
|
if (store.getState().ui.allow_reporting) {
|
|
|
|
|
ReactGA.event({ category: 'Modal', action: 'Closed', label: null });
|
2016-11-11 16:47:32 +13:00
|
|
|
}
|
2019-09-05 15:36:11 +12: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 (typeof notification === 'undefined') return false;
|
2020-06-12 22:42:49 +12:00
|
|
|
if (typeof notification !== 'undefined') notification.requestPermission((permission) => { });
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
// handle nested data objects
|
|
|
|
|
var data = {};
|
|
|
|
|
if (typeof (action.data)) data = action.data;
|
|
|
|
|
if (typeof (data.data)) data = { ...data, ...data.data };
|
|
|
|
|
|
|
|
|
|
// construct our browser notification
|
|
|
|
|
var title = '';
|
|
|
|
|
var options = {
|
|
|
|
|
body: '',
|
|
|
|
|
dir: 'auto',
|
|
|
|
|
lang: 'EN',
|
|
|
|
|
tag: 'iris',
|
|
|
|
|
};
|
|
|
|
|
if (data.title) title = data.title;
|
|
|
|
|
if (data.body) options.body = data.body;
|
|
|
|
|
if (data.icon) options.icon = data.icon;
|
|
|
|
|
|
|
|
|
|
// make it so
|
|
|
|
|
var notification = new notification(title, options);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'CREATE_NOTIFICATION':
|
|
|
|
|
|
|
|
|
|
// start a timeout to close this notification
|
|
|
|
|
if (!action.notification.sticky) {
|
2020-02-13 10:41:20 +13:00
|
|
|
setTimeout(
|
2019-09-05 15:36:11 +12:00
|
|
|
() => {
|
|
|
|
|
store.dispatch(uiActions.closeNotification(action.notification.key));
|
|
|
|
|
},
|
|
|
|
|
action.notification.duration * 1000,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'CLOSE_NOTIFICATION':
|
|
|
|
|
var notifications = { ...store.getState().ui.notifications };
|
|
|
|
|
|
|
|
|
|
// start a timeout to remove this notification
|
|
|
|
|
// This gives us time to animate out the notification before we remove the data
|
2020-02-13 10:41:20 +13:00
|
|
|
setTimeout(
|
2019-09-05 15:36:11 +12:00
|
|
|
() => {
|
|
|
|
|
store.dispatch(uiActions.removeNotification(action.key));
|
|
|
|
|
},
|
|
|
|
|
200,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'REMOVE_NOTIFICATION':
|
|
|
|
|
|
|
|
|
|
// Manual removal
|
|
|
|
|
if (action.manual) {
|
|
|
|
|
var notifications = { ...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;
|
2016-11-11 16:47:32 +13:00
|
|
|
|
2019-09-05 15:36:11 +12:00
|
|
|
case 'BROADCASTS_LOADED':
|
|
|
|
|
var suppressed_broadcasts = [];
|
|
|
|
|
if (store.getState().ui.suppressed_broadcasts !== undefined) {
|
|
|
|
|
suppressed_broadcasts = store.getState().ui.suppressed_broadcasts;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < action.broadcasts.length; i++) {
|
|
|
|
|
const 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),
|
|
|
|
|
content: (broadcast.message ? broadcast.message : null),
|
|
|
|
|
links: (broadcast.links ? broadcast.links : null),
|
|
|
|
|
type: 'broadcast',
|
|
|
|
|
sticky: true,
|
|
|
|
|
};
|
|
|
|
|
store.dispatch(uiActions.createNotification(data));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'START_PROCESS':
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: action.key,
|
|
|
|
|
data: action.data,
|
|
|
|
|
});
|
|
|
|
|
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,
|
|
|
|
|
});
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'PROCESS_CANCELLED':
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: `${action.key}_CANCELLED`,
|
|
|
|
|
});
|
2020-05-25 20:50:25 +12:00
|
|
|
store.dispatch(uiActions.closeProcess(action.key));
|
2019-09-05 15:36:11 +12:00
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2020-02-13 10:41:20 +13:00
|
|
|
case 'PROCESS_FINISHED':
|
|
|
|
|
store.dispatch({
|
|
|
|
|
type: `${action.key}_FINISHED`,
|
|
|
|
|
});
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
// start a timeout to remove this notification
|
|
|
|
|
// This gives us time to animate out the notification before we remove the data
|
2020-02-13 10:41:20 +13:00
|
|
|
if (action.completionMessage) {
|
|
|
|
|
store.dispatch(uiActions.updateProcess(
|
|
|
|
|
action.key,
|
|
|
|
|
action.completionMessage.content,
|
|
|
|
|
{},
|
|
|
|
|
action.completionMessage.description,
|
|
|
|
|
action.completionMessage.level,
|
|
|
|
|
));
|
|
|
|
|
if (!action.completionMessage.sticky) {
|
|
|
|
|
setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
store.dispatch(uiActions.closeProcess(action.key));
|
|
|
|
|
},
|
|
|
|
|
5000,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
store.dispatch(uiActions.closeProcess(action.key));
|
|
|
|
|
}
|
2019-09-05 15:36:11 +12:00
|
|
|
|
|
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2020-02-13 10:41:20 +13:00
|
|
|
case 'CLOSE_PROCESS':
|
|
|
|
|
setTimeout(
|
|
|
|
|
() => {
|
|
|
|
|
store.dispatch(uiActions.removeProcess(action.key));
|
|
|
|
|
},
|
|
|
|
|
200,
|
|
|
|
|
);
|
2019-09-05 15:36:11 +12:00
|
|
|
next(action);
|
|
|
|
|
break;
|
|
|
|
|
|
2020-02-13 10:41:20 +13:00
|
|
|
// This action is irrelevant to us, pass it on to the next middleware
|
2019-09-05 15:36:11 +12:00
|
|
|
default:
|
|
|
|
|
return next(action);
|
|
|
|
|
}
|
2020-02-13 10:41:20 +13:00
|
|
|
};
|
2019-09-05 15:36:11 +12:00
|
|
|
}());
|
2016-11-11 16:47:32 +13:00
|
|
|
|
2020-02-13 10:41:20 +13:00
|
|
|
export default UIMiddleware;
|