-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.ts
211 lines (173 loc) · 4.78 KB
/
index.ts
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import './style.css';
import maplibregl from 'maplibre-gl';
import 'maplibre-gl/dist/maplibre-gl.css';
import {WebMercatorViewport, Deck} from '@deck.gl/core';
import {vectorTableSource, createViewportSpatialFilter} from '@carto/api-client';
import {BASEMAP, VectorTileLayer} from '@deck.gl/carto';
import {debounce} from './utils';
import {Grid} from 'gridjs';
import 'gridjs/dist/theme/mermaid.css';
const apiBaseUrl = import.meta.env.VITE_API_BASE_URL;
const accessToken = import.meta.env.VITE_API_ACCESS_TOKEN;
const connectionName = 'carto_dw';
const cartoConfig = {apiBaseUrl, accessToken, connectionName};
// init deckgl
const INITIAL_VIEW_STATE = {
latitude: 41.8097343,
longitude: -110.5556199,
zoom: 3,
bearing: 0,
pitch: 0
};
const deck = new Deck({
canvas: 'deck-canvas',
initialViewState: INITIAL_VIEW_STATE,
controller: true
});
// Add basemap
const map = new maplibregl.Map({
container: 'map',
style: BASEMAP.POSITRON,
interactive: false
});
// prepare and init widgets HTML elements
const tableWidgetDom = document.querySelector<HTMLDivElement>('#table-widget');
let tableWidget = new Grid({
data: [],
columns: []
}).render(tableWidgetDom);
// define source
let dataSource;
async function initSource() {
return (dataSource = await vectorTableSource({
...cartoConfig,
tableName: 'carto-demo-data.demo_tables.osm_pois_usa'
}));
}
// SPATIAL FILTER
// prepare a function to get the new viewport state, that we'll pass debounced to our widgets to minimize requests
let viewportSpatialFilter;
const debouncedUpdateSpatialFilter = debounce(viewState => {
const viewport = new WebMercatorViewport(viewState);
viewportSpatialFilter = createViewportSpatialFilter(viewport.getBounds());
renderWidgets();
}, 300);
// sync deckgl map after user interaction, obtain new viewport after
deck.setProps({
onViewStateChange: ({viewState}) => {
const {longitude, latitude, ...rest} = viewState;
map.jumpTo({center: [longitude, latitude], ...rest});
debouncedUpdateSpatialFilter(viewState);
}
});
// RENDER
// render Widgets function
async function renderWidgets() {
// Exit if dataSource is not ready
if (!dataSource) {
return;
}
// configure widgets
const columns = {
osm_id: 'OSM ID',
name: 'Name',
group_name: 'Group Name',
subgroup_name: 'Subgroup Name'
};
// render widgets!
tableWidget
.updateConfig({
server: {
url: '',
data: async opts => {
let colName = 'OSM_ID';
let dir = 'asc';
let offset = 0;
const urlParams = new URLSearchParams(opts.url);
if (urlParams.get('order') && urlParams.get('dir')) {
colName = urlParams.get('order');
dir = urlParams.get('dir');
}
if (urlParams.get('offset')) {
offset = parseInt(urlParams.get('offset'));
}
const tableData = await dataSource.widgetSource.getTable({
columns: Object.keys(columns),
spatialFilter: viewportSpatialFilter,
sortBy: Object.keys(columns).find(key => columns[key] === colName),
sortDirection: dir,
limit: 5,
offset: offset
});
const response = {
data: tableData.rows.map(row => Object.values(row)),
total: tableData.totalCount
};
return response;
}
},
columns: Object.values(columns),
style: {
table: {
'font-size': '11px'
},
td: {
padding: '8px'
},
th: {
padding: '8px'
}
},
sort: {
multiColumn: false,
server: {
url: (prev, sortedCols) => {
if (!sortedCols.length) return prev;
const col = sortedCols[0];
const dir = col.direction === 1 ? 'asc' : 'desc';
let colName = Object.values(columns)[col.index];
return `${prev}&order=${colName}&dir=${dir}`;
}
}
},
pagination: {
server: {
url: (prev, page, limit) => `${prev}&offset=${page * limit}`
},
limit: 5
}
})
.forceRender();
}
// render Layers function
async function renderLayers() {
// Exit if dataSource is not ready
if (!dataSource) {
return;
}
// now for the layers
const layers = [
new VectorTileLayer({
id: 'places',
pickable: true,
data: dataSource,
opacity: 1,
getFillColor: [3, 111, 226],
getLineColor: [255, 255, 255],
getPointRadius: 50,
getLineWidth: 10,
pointRadiusMinPixels: 1,
lineWidthMinPixels: 0.3
})
];
deck.setProps({
layers: layers
});
}
// render everything!
async function initialize() {
await initSource();
renderWidgets();
renderLayers();
}
initialize();