Skip to content

Commit e662fb7

Browse files
committed
page title now changes also when selecting papers
1 parent aabdea9 commit e662fb7

3 files changed

Lines changed: 74 additions & 22 deletions

File tree

vis/js/intermediate.js

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import { getChartSize, getListSize } from "./utils/dimensions";
2626
import Headstart from "./components/Headstart";
2727
import { sanitizeInputData } from "./utils/data";
2828
import { createAnimationCallback } from "./utils/eventhandlers";
29-
import { removeQueryParams, handleReduxAction } from "./utils/url";
29+
import { removeQueryParams, handleUrlAction } from "./utils/url";
3030
import debounce from "./utils/debounce";
31+
import { handleTitleAction } from "./utils/title";
3132

3233
/**
3334
* Class to sit between the "old" mediator and the
@@ -427,7 +428,7 @@ function createQueryParameterMiddleware() {
427428
return function () {
428429
return (next) => (action) => {
429430
if (!action.canceled && !action.isFromBackButton) {
430-
handleReduxAction(action);
431+
handleUrlAction(action);
431432
}
432433

433434
return next(action);
@@ -436,21 +437,13 @@ function createQueryParameterMiddleware() {
436437
}
437438

438439
function createPageTitleMiddleware(itm) {
439-
return function () {
440+
return function ({ getState }) {
440441
return (next) => (action) => {
441-
if (
442-
action.type === "ZOOM_IN" &&
443-
!action.canceled &&
444-
action.selectedAreaData
445-
) {
446-
document.title = `${action.selectedAreaData.title} | ${itm.originalTitle}`;
447-
}
448-
if (action.type === "ZOOM_OUT" && !action.canceled) {
449-
document.title = itm.originalTitle;
442+
if (!action.canceled && !action.isFromBackButton) {
443+
const state = getState();
444+
handleTitleAction(action, itm.originalTitle, state);
450445
}
451446

452-
// TODO select paper / deselect paper ?
453-
454447
return next(action);
455448
};
456449
};

vis/js/utils/title.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Sets correct page title based on the current action and state.
3+
*
4+
* @param {Object} action the Redux action object
5+
* @param {string} defaultTitle the original page title Headstart has when it loads
6+
* @param {Object} state the Redux state object
7+
*/
8+
export const handleTitleAction = (action, defaultTitle, state) => {
9+
switch (action.type) {
10+
case "ZOOM_IN":
11+
document.title = getZoomInTitle(
12+
action.selectedAreaData,
13+
action.selectedPaperData,
14+
defaultTitle
15+
);
16+
return;
17+
case "ZOOM_OUT":
18+
document.title = defaultTitle;
19+
return;
20+
case "SELECT_PAPER":
21+
document.title = getSelectPaperTitle(action.paper, defaultTitle);
22+
return;
23+
case "DESELECT_PAPER":
24+
document.title = getDeselectPaperTitle(defaultTitle, state);
25+
return;
26+
default:
27+
return;
28+
}
29+
};
30+
31+
const getZoomInTitle = (areaData, paperData, defaultTitle) => {
32+
if (!areaData) {
33+
return document.title;
34+
}
35+
36+
if (paperData) {
37+
return `${paperData.title} | ${defaultTitle}`;
38+
}
39+
40+
return `${areaData.title} | ${defaultTitle}`;
41+
};
42+
43+
const getSelectPaperTitle = (paperData, defaultTitle) => {
44+
if (!paperData) {
45+
return document.title;
46+
}
47+
48+
return `${paperData.title} | ${defaultTitle}`;
49+
};
50+
51+
const getDeselectPaperTitle = (defaultTitle, state) => {
52+
if (state.selectedBubble) {
53+
return getZoomInTitle(
54+
{ title: state.selectedBubble.title },
55+
null,
56+
defaultTitle
57+
);
58+
}
59+
60+
return defaultTitle;
61+
};

vis/js/utils/url.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,12 @@ const addRemoveQueryParams = (paramsToAdd, paramsToRemove) => {
3737
window.history.pushState("", "", url.pathname + url.search);
3838
};
3939

40-
//document.title = `${action.selectedAreaData.title} | ${itm.originalTitle}`;
41-
//document.title = itm.originalTitle;
42-
43-
//document.title = `${action.paper.title} | ${itm.originalTitle}`;
44-
//document.title = itm.originalTitle;
45-
46-
// TODO docs
47-
export const handleReduxAction = (action) => {
40+
/**
41+
* Changes page url based on the current Redux action.
42+
*
43+
* @param {Object} action the Redux action object
44+
*/
45+
export const handleUrlAction = (action) => {
4846
switch (action.type) {
4947
case "ZOOM_IN":
5048
return handleZoomIn(action);

0 commit comments

Comments
 (0)