@focus-daima/gis-workbench 0.0.0-stage → 0.9.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/store.js ADDED
@@ -0,0 +1,189 @@
1
+ import {
2
+ DEFAULT_ATTRIBUTION,
3
+ DEFAULT_LAYER_NAME,
4
+ DEFAULT_TILE_SIZE,
5
+ DEFAULT_TILE_URL,
6
+ } from '../shared.js';
7
+ import { mergeLayerStyle } from '../style-schema.js';
8
+
9
+ /** The in-memory layer collection is the single source the map and layer panel read. */
10
+ let layers = [];
11
+ let sequence = 0;
12
+ let view = null;
13
+ let viewSequence = 0;
14
+ const listeners = new Set();
15
+
16
+ function notify() {
17
+ for (const listener of [...listeners]) listener();
18
+ }
19
+
20
+ function asNumber(value, fallback, min, max) {
21
+ const number = typeof value === 'number' ? value : Number(value);
22
+ if (!Number.isFinite(number)) return fallback;
23
+ return Math.min(max, Math.max(min, number));
24
+ }
25
+
26
+ function stableId(key) {
27
+ let hash = 2166136261;
28
+ for (let index = 0; index < key.length; index += 1) {
29
+ hash ^= key.charCodeAt(index);
30
+ hash = Math.imul(hash, 16777619);
31
+ }
32
+ return 'gis-' + (hash >>> 0).toString(36);
33
+ }
34
+
35
+ /**
36
+ * Resolve a protocol-relative tile template against the page protocol. The
37
+ * GIS tile service is served outside the GUI origin, so the browser must know
38
+ * the concrete scheme before the map engine requests a tile.
39
+ */
40
+ export function resolveTileUrl(template) {
41
+ const trimmed = String(template || '').trim();
42
+ if (/^https?:\/\//i.test(trimmed)) return trimmed;
43
+ if (trimmed.startsWith('//')) {
44
+ const protocol = typeof window !== 'undefined' && window.location ? window.location.protocol : 'http:';
45
+ return protocol + trimmed;
46
+ }
47
+ if (trimmed.startsWith('/') && typeof window !== 'undefined' && window.location && window.location.origin) {
48
+ return window.location.origin + trimmed;
49
+ }
50
+ return trimmed;
51
+ }
52
+
53
+ export function getLayers() {
54
+ return layers;
55
+ }
56
+
57
+ export function subscribe(listener) {
58
+ listeners.add(listener);
59
+ return () => {
60
+ listeners.delete(listener);
61
+ };
62
+ }
63
+
64
+ export function addLayer(input) {
65
+ const name = String(input && input.name ? input.name : DEFAULT_LAYER_NAME).trim() || DEFAULT_LAYER_NAME;
66
+ const tileUrl = String(input && input.tileUrl ? input.tileUrl : DEFAULT_TILE_URL).trim() || DEFAULT_TILE_URL;
67
+ const key = String(input && input.key ? input.key : 'layer-' + (sequence += 1));
68
+ const next = {
69
+ key,
70
+ name,
71
+ renderKind: 'raster-tiles',
72
+ tileUrl,
73
+ tileSize: Number(input && input.tileSize) === 512 ? 512 : DEFAULT_TILE_SIZE,
74
+ minzoom: asNumber(input && input.minzoom, 0, 0, 24),
75
+ maxzoom: asNumber(input && input.maxzoom, 18, 0, 24),
76
+ attribution: String(input && input.attribution ? input.attribution : DEFAULT_ATTRIBUTION),
77
+ visible: !(input && input.visible === false),
78
+ source: String(input && input.source ? input.source : 'conversation'),
79
+ z: asNumber(input && input.z, 1000, -1000, 10000),
80
+ };
81
+
82
+ const existing = layers.find((layer) => layer.tileUrl === next.tileUrl);
83
+ if (existing) {
84
+ const updated = Object.assign({}, existing, {
85
+ name: next.name,
86
+ tileSize: next.tileSize,
87
+ minzoom: next.minzoom,
88
+ maxzoom: next.maxzoom,
89
+ attribution: next.attribution,
90
+ });
91
+ layers = layers.map((layer) => (layer.key === existing.key ? updated : layer));
92
+ notify();
93
+ return updated;
94
+ }
95
+
96
+ const layer = Object.assign({}, next, { id: stableId(key) });
97
+ layers = [...layers, layer].sort((left, right) => (left.z || 0) - (right.z || 0));
98
+ notify();
99
+ return layer;
100
+ }
101
+
102
+ /** Replace the persisted layers of one project while keeping session-only layers. */
103
+ export function replaceProjectLayers(projectId, incoming) {
104
+ const kept = layers.filter((layer) => layer.source !== 'project');
105
+ const projectLayers = (Array.isArray(incoming) ? incoming : []).map((layer) => Object.assign({}, layer, {
106
+ id: layer.id || stableId(layer.key),
107
+ source: 'project',
108
+ persisted: true,
109
+ projectId,
110
+ }));
111
+ layers = [...kept, ...projectLayers].sort((left, right) => (left.z || 0) - (right.z || 0));
112
+ notify();
113
+ }
114
+
115
+ export function updateProjectLayerStyle(layerId, stylePatch) {
116
+ let changed = false;
117
+ layers = layers.map((layer) => {
118
+ if (layer.layerId !== layerId && layer.id !== layerId) return layer;
119
+ changed = true;
120
+ return Object.assign({}, layer, { style: mergeLayerStyle(layer.style, stylePatch) });
121
+ });
122
+ if (changed) notify();
123
+ return changed;
124
+ }
125
+
126
+ export function reorderLayers(sourceKey, targetKey, after) {
127
+ if (!sourceKey || !targetKey || sourceKey === targetKey) return false;
128
+ const sourceIndex = layers.findIndex((layer) => layer.key === sourceKey);
129
+ if (sourceIndex < 0) return false;
130
+ const next = layers.slice();
131
+ const [moved] = next.splice(sourceIndex, 1);
132
+ let targetIndex = next.findIndex((layer) => layer.key === targetKey);
133
+ if (targetIndex < 0) return false;
134
+ if (after) targetIndex += 1;
135
+ next.splice(targetIndex, 0, moved);
136
+ layers = next.map((layer, index) => layer.z === index ? layer : Object.assign({}, layer, { z: index }));
137
+ notify();
138
+ return true;
139
+ }
140
+
141
+ export function removeLayer(key) {
142
+ const next = layers.filter((layer) => layer.key !== key);
143
+ if (next.length === layers.length) return;
144
+ layers = next;
145
+ notify();
146
+ }
147
+
148
+ export function toggleLayerVisibility(key) {
149
+ let changed = false;
150
+ layers = layers.map((layer) => {
151
+ if (layer.key !== key) return layer;
152
+ changed = true;
153
+ return Object.assign({}, layer, { visible: !layer.visible });
154
+ });
155
+ if (changed) notify();
156
+ }
157
+
158
+ export function clearLayers() {
159
+ if (layers.length === 0) return;
160
+ layers = [];
161
+ notify();
162
+ }
163
+
164
+ /**
165
+ * Latest camera request. Every call produces a new token so the map reacts even
166
+ * when the same coordinates are requested twice in a row.
167
+ */
168
+ export function getView() {
169
+ return view;
170
+ }
171
+
172
+ export function setView(input) {
173
+ const center = input && Array.isArray(input.center) ? input.center : null;
174
+ if (!center || center.length < 2) return null;
175
+ const lng = Number(center[0]);
176
+ const lat = Number(center[1]);
177
+ if (!Number.isFinite(lng) || !Number.isFinite(lat)) return null;
178
+ const zoom = asNumber(input && input.zoom, 11, 0, 24);
179
+ viewSequence += 1;
180
+ view = {
181
+ token: viewSequence,
182
+ center: [Math.min(180, Math.max(-180, lng)), Math.min(85, Math.max(-85, lat))],
183
+ zoom,
184
+ label: String(input && input.label ? input.label : '').trim(),
185
+ key: String(input && input.key ? input.key : 'view-' + viewSequence),
186
+ };
187
+ notify();
188
+ return view;
189
+ }