-
Notifications
You must be signed in to change notification settings - Fork 383
/
vectorstyler.js
136 lines (124 loc) · 3.66 KB
/
vectorstyler.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
135
136
/**
* Copyright 2016, 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 {
NEW_VECTOR_RULE,
SELECT_VECTOR_RULE,
REMOVE_VECTOR_RULE,
SET_VECTORSTYLE_PARAMETER,
SET_VECTOR_LAYER,
SET_VECTOR_RULE_PARAMETER
} from '../actions/vectorstyler';
import { STYLER_RESET } from '../actions/styler';
import assign from 'object-assign';
import { isObject, findIndex } from 'lodash';
const baseStyle = {
Point: {
type: "Point",
color: { r: 0, g: 0, b: 255, a: 1 },
width: 3,
fill: { r: 0, g: 0, b: 255, a: 0.1 },
radius: 10,
marker: false,
markName: "circle"
},
Line: {
type: "Line",
color: { r: 0, g: 0, b: 255, a: 1 },
width: 3
},
Polygon: {
type: "Polygon",
color: { r: 0, g: 0, b: 255, a: 1 },
width: 3,
fill: { r: 0, g: 0, b: 255, a: 0.1 }
}
};
const initialSpec = {
rules: []
};
function getType(layer) {
switch (layer.describeLayer.geometryType) {
case 'Polygon':
case 'MultiPolygon': {
return "Polygon";
}
case 'MultiLineString':
case 'LineString': {
return "Line";
}
case 'Point':
case 'MultiPoint': {
return "Point";
}
default: {
return "Polygon";
}
}
}
function getBaseSymbol(type = "Polygon") {
let newSymbol = {};
let symbol = baseStyle[type];
Object.keys(symbol).reduce((pr, next) => {
pr[next] = isObject(symbol[next]) ? assign({}, symbol[next]) : symbol[next];
return pr;
}, newSymbol);
return newSymbol;
}
function getRuleIdx(rules, id) {
return findIndex(rules, (r) => {return r.id === id; });
}
function vectorstyler(state = initialSpec, action) {
switch (action.type) {
case NEW_VECTOR_RULE: {
const newRule = {
id: action.id,
symbol: getBaseSymbol(getType(state.layer)),
name: 'New Rule'
};
return assign({}, state, {rule: newRule.id, rules: state.rules ? [...state.rules, newRule] : [newRule]});
}
case SELECT_VECTOR_RULE: {
return assign({}, state, {rule: action.id});
}
case REMOVE_VECTOR_RULE: {
const idx = getRuleIdx(state.rules, action.id);
let newSelected = state.rules[idx - 1] ? state.rules[idx - 1].id : undefined;
if (newSelected === undefined) {
newSelected = state.rules[idx + 1] ? state.rules[idx + 1].id : undefined;
}
return assign({}, state, {rule: newSelected, rules: state.rules.filter((rule) => rule.id !== action.id)});
}
case SET_VECTOR_RULE_PARAMETER: {
let newRules = state.rules.slice();
const ruleIdx = getRuleIdx(newRules, state.rule);
const activeRule = newRules[ruleIdx];
newRules[ruleIdx] = assign({}, activeRule, {[action.property]: action.value});
return assign({}, state, {rules: newRules});
}
case SET_VECTORSTYLE_PARAMETER: {
let newRules = state.rules.slice();
const ruleIdx = getRuleIdx(newRules, state.rule);
const activeRule = newRules[ruleIdx];
newRules[ruleIdx] = assign( {}, activeRule, {
[action.component]: assign({}, activeRule[action.component], {
[action.property]: action.value
})
});
return assign({}, state, {rules: newRules});
}
case SET_VECTOR_LAYER: {
return assign({}, initialSpec, { layer: action.layer});
}
case STYLER_RESET: {
return initialSpec;
}
default:
return state;
}
}
export default vectorstyler;