-
Notifications
You must be signed in to change notification settings - Fork 383
/
search.js
138 lines (135 loc) · 4.56 KB
/
search.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
137
138
/*
* 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 {
TEXT_SEARCH_RESULTS_LOADED,
TEXT_SEARCH_RESULTS_PURGE,
TEXT_SEARCH_RESET,
TEXT_SEARCH_ADD_MARKER,
TEXT_SEARCH_TEXT_CHANGE,
TEXT_SEARCH_LOADING,
TEXT_SEARCH_ERROR,
TEXT_SEARCH_NESTED_SERVICES_SELECTED,
TEXT_SEARCH_CANCEL_ITEM,
TEXT_SEARCH_SET_HIGHLIGHTED_FEATURE,
UPDATE_RESULTS_STYLE,
CHANGE_SEARCH_TOOL,
CHANGE_FORMAT,
CHANGE_COORD
} from '../actions/search';
import { RESET_CONTROLS } from '../actions/controls';
import assign from 'object-assign';
/**
* Manages the state of the map search with it's results
* The properties represent the shape of the state
* @prop {boolan} loading loading state
* @prop {object} error the last error, if any
* @prop {string} searchText the search text
* @prop {array} results the results
* @prop {object} markerPosition the markerPosition
* @prop {object} selectedServicess tores the services currently selected by the user
* @prop {object} selectedItems the selected items
*
* @example
*{
* search: {
* searchText: 'test',
* error: null,
* loading: false,
* results: [
* {
* properties: {
* place_id: '130504451',
* licence: 'Data © OpenStreetMap contributors, ODbL 1.0. http://www.openstreetmap.org/copyright',
* osm_type: 'way',
* osm_id: '294145572',
* lat: '6.82439805',
* lon: '81.0004103985287',
* display_name: 'test, Bandarawela, Badulla District, Uva, Sri Lanka',
* 'class': 'landuse',
* type: 'forest',
* importance: 0.31,
* },
* id: '294145572',
* type: 'Feature',
* bbox: [
* 81.0001165,
* 6.8238999,
* 81.0008042,
* 6.8248084
* ],
* geometry: {
* type: 'Polygon',
* coordinates: [
* [[ 81.0001165, 6.8242576],
* [81.0001892, 6.8245385],
* [81.0003879, 6.8248084],
* [81.0008042, 6.8241984],
* [81.0003606, 6.8238999],
* [81.0001165, 6.8242576]
* ]]
* },
* __SERVICE__: {
* type: 'nominatim'
* },
* __PRIORITY__: 0
* },
* ]
* }
*}
* @memberof reducers
*/
function search(state = null, action) {
switch (action.type) {
case TEXT_SEARCH_LOADING: {
return assign({}, state, {loading: action.loading});
}
case TEXT_SEARCH_ERROR: {
return assign({}, state, {error: action.error});
}
case TEXT_SEARCH_TEXT_CHANGE:
return assign({}, state, { searchText: action.searchText, error: null });
case TEXT_SEARCH_RESULTS_LOADED:
let results = action.results;
if (action.append === true && state && state.results) {
results = [...state.results, ...action.results];
}
return assign({}, state, { results: results, error: null });
case TEXT_SEARCH_RESULTS_PURGE:
return assign({}, state, { results: null, error: null});
case TEXT_SEARCH_ADD_MARKER:
return assign({}, state, { markerPosition: action.markerPosition, markerLabel: action.markerLabel });
case TEXT_SEARCH_SET_HIGHLIGHTED_FEATURE:
return assign({}, state, {highlightedFeature: action.highlightedFeature});
case TEXT_SEARCH_RESET:
return { style: state.style || {} };
case RESET_CONTROLS:
return null;
case TEXT_SEARCH_NESTED_SERVICES_SELECTED:
return assign({}, state, {
selectedServices: action.services,
searchText: action.searchText,
selectedItems: (state.selectedItems || []).concat(action.items)
});
case TEXT_SEARCH_CANCEL_ITEM:
return assign({}, {
selectedItems: state.selectedItems && state.selectedItems.filter(item => item !== action.item),
searchText: state.searchText === "" && action.item && action.item.text ? action.item.text.substring(0, action.item.text.length) : state.searchText
});
case UPDATE_RESULTS_STYLE:
return assign({}, state, {style: action.style});
case CHANGE_SEARCH_TOOL:
return {...state, activeSearchTool: action.activeSearchTool};
case CHANGE_FORMAT:
return {...state, format: action.format};
case CHANGE_COORD:
return {...state, coordinate: {...state.coordinate, [action.coord]: action.val}};
default:
return state;
}
}
export default search;