-
Notifications
You must be signed in to change notification settings - Fork 383
/
print.js
134 lines (126 loc) · 3.92 KB
/
print.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Copyright 2015, GeoSolutions Sas.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
import {
SET_PRINT_PARAMETER,
PRINT_CAPABILITIES_LOADED,
PRINT_CAPABILITIES_ERROR,
CONFIGURE_PRINT_MAP,
CHANGE_PRINT_ZOOM_LEVEL,
CHANGE_MAP_PRINT_PREVIEW,
PRINT_SUBMITTING,
PRINT_CREATED,
PRINT_ERROR,
PRINT_CANCEL
} from '../actions/print';
import { TOGGLE_CONTROL } from '../actions/controls';
import { isObject, get } from 'lodash';
import assign from 'object-assign';
const initialSpec = {
antiAliasing: true,
iconSize: 24,
legendDpi: 96,
fontFamily: "Verdana",
fontSize: 8,
bold: false,
italic: false,
resolution: 96,
name: '',
description: ''
};
const getSheetName = (name = '') => {
return name.split('_')[0];
};
function print(state = {spec: initialSpec, capabilities: null, map: null, isLoading: false, pdfUrl: null}, action) {
switch (action.type) {
case TOGGLE_CONTROL: {
if (action.control === 'print') {
return assign({}, state, {pdfUrl: null, isLoading: false, error: null});
}
return state;
}
case PRINT_CAPABILITIES_LOADED: {
const layouts = get(action, 'capabilities.layouts', [{name: 'A4'}]);
const sheetName = layouts.filter(l => getSheetName(l.name) === state.spec.sheet).length ?
state.spec.sheet : getSheetName(layouts[0].name);
return assign({}, state, {
capabilities: action.capabilities,
spec: assign({}, state.spec || {}, {
sheet: sheetName,
resolution: action.capabilities
&& action.capabilities.dpis
&& action.capabilities.dpis.length
&& action.capabilities.dpis[0].value
})
});
}
case SET_PRINT_PARAMETER: {
return assign({}, state, {
spec: assign({}, state.spec, {[action.name]: action.value})
}
);
}
case CONFIGURE_PRINT_MAP: {
const layers = action.layers.map((layer) => {
return layer.title ? assign({}, layer, {
title: isObject(layer.title) && action.currentLocale && layer.title[action.currentLocale]
|| isObject(layer.title) && layer.title.default
|| layer.title
}) : layer;
});
return assign({}, state, {
map: {
center: action.center,
zoom: action.zoom,
scaleZoom: action.scaleZoom,
scale: action.scale,
layers,
projection: action.projection
},
error: null
}
);
}
case CHANGE_PRINT_ZOOM_LEVEL: {
const diff = action.zoom - state.map.scaleZoom;
return assign({}, state, {
map: assign({}, state.map, {
scaleZoom: action.zoom,
zoom: state.map.zoom + diff,
scale: action.scale
})
}
);
}
case CHANGE_MAP_PRINT_PREVIEW: {
return assign({}, state, {
map: assign({}, state.map, {
size: action.size
})
}
);
}
case PRINT_SUBMITTING: {
return assign({}, state, {isLoading: true, pdfUrl: null, error: null});
}
case PRINT_CREATED: {
return assign({}, state, {isLoading: false, pdfUrl: action.url, error: null});
}
case PRINT_ERROR: {
return assign({}, state, {isLoading: false, pdfUrl: null, error: action.error});
}
case PRINT_CAPABILITIES_ERROR: {
return assign({}, state, {isLoading: false, pdfUrl: null, error: action.error});
}
case PRINT_CANCEL: {
return assign({}, state, {isLoading: false, pdfUrl: null, error: null});
}
default:
return state;
}
}
export default print;