-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Source.js
245 lines (215 loc) · 5.94 KB
/
Source.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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/**
* @module ol/source/Source
*/
import BaseObject from '../Object.js';
import {get as getProjection} from '../proj.js';
/**
* @typedef {'undefined' | 'loading' | 'ready' | 'error'} State
* State of the source, one of 'undefined', 'loading', 'ready' or 'error'.
*/
/**
* A function that takes a {@link import("../View.js").ViewStateLayerStateExtent} and returns a string or
* an array of strings representing source attributions.
*
* @typedef {function(import("../View.js").ViewStateLayerStateExtent): (string|Array<string>)} Attribution
*/
/**
* A type that can be used to provide attribution information for data sources.
*
* It represents either
* * a simple string (e.g. `'© Acme Inc.'`)
* * an array of simple strings (e.g. `['© Acme Inc.', '© Bacme Inc.']`)
* * a function that returns a string or array of strings ({@link module:ol/source/Source~Attribution})
*
* @typedef {string|Array<string>|Attribution} AttributionLike
*/
/**
* @typedef {Object} Options
* @property {AttributionLike} [attributions] Attributions.
* @property {boolean} [attributionsCollapsible=true] Attributions are collapsible.
* @property {import("../proj.js").ProjectionLike} [projection] Projection. Default is the view projection.
* @property {import("./Source.js").State} [state='ready'] State.
* @property {boolean} [wrapX=false] WrapX.
* @property {boolean} [interpolate=false] Use interpolated values when resampling. By default,
* the nearest neighbor is used when resampling.
*/
/**
* @classdesc
* Abstract base class; normally only used for creating subclasses and not
* instantiated in apps.
* Base class for {@link module:ol/layer/Layer~Layer} sources.
*
* A generic `change` event is triggered when the state of the source changes.
* @abstract
* @api
*/
class Source extends BaseObject {
/**
* @param {Options} options Source options.
*/
constructor(options) {
super();
/**
* @protected
* @type {import("../proj/Projection.js").default|null}
*/
this.projection = getProjection(options.projection);
/**
* @private
* @type {?Attribution}
*/
this.attributions_ = adaptAttributions(options.attributions);
/**
* @private
* @type {boolean}
*/
this.attributionsCollapsible_ =
options.attributionsCollapsible !== undefined
? options.attributionsCollapsible
: true;
/**
* This source is currently loading data. Sources that defer loading to the
* map's tile queue never set this to `true`.
* @type {boolean}
*/
this.loading = false;
/**
* @private
* @type {import("./Source.js").State}
*/
this.state_ = options.state !== undefined ? options.state : 'ready';
/**
* @private
* @type {boolean}
*/
this.wrapX_ = options.wrapX !== undefined ? options.wrapX : false;
/**
* @private
* @type {boolean}
*/
this.interpolate_ = !!options.interpolate;
/**
* @protected
* @type {function(import("../View.js").ViewOptions):void}
*/
this.viewResolver = null;
/**
* @protected
* @type {function(Error):void}
*/
this.viewRejector = null;
const self = this;
/**
* @private
* @type {Promise<import("../View.js").ViewOptions>}
*/
this.viewPromise_ = new Promise(function (resolve, reject) {
self.viewResolver = resolve;
self.viewRejector = reject;
});
}
/**
* Get the attribution function for the source.
* @return {?Attribution} Attribution function.
* @api
*/
getAttributions() {
return this.attributions_;
}
/**
* @return {boolean} Attributions are collapsible.
* @api
*/
getAttributionsCollapsible() {
return this.attributionsCollapsible_;
}
/**
* Get the projection of the source.
* @return {import("../proj/Projection.js").default|null} Projection.
* @api
*/
getProjection() {
return this.projection;
}
/**
* @param {import("../proj/Projection").default} [projection] Projection.
* @return {Array<number>|null} Resolutions.
*/
getResolutions(projection) {
return null;
}
/**
* @return {Promise<import("../View.js").ViewOptions>} A promise for view-related properties.
*/
getView() {
return this.viewPromise_;
}
/**
* Get the state of the source, see {@link import("./Source.js").State} for possible states.
* @return {import("./Source.js").State} State.
* @api
*/
getState() {
return this.state_;
}
/**
* @return {boolean|undefined} Wrap X.
*/
getWrapX() {
return this.wrapX_;
}
/**
* @return {boolean} Use linear interpolation when resampling.
*/
getInterpolate() {
return this.interpolate_;
}
/**
* Refreshes the source. The source will be cleared, and data from the server will be reloaded.
* @api
*/
refresh() {
this.changed();
}
/**
* Set the attributions of the source.
* @param {AttributionLike|undefined} attributions Attributions.
* Can be passed as `string`, `Array<string>`, {@link module:ol/source/Source~Attribution},
* or `undefined`.
* @api
*/
setAttributions(attributions) {
this.attributions_ = adaptAttributions(attributions);
this.changed();
}
/**
* Set the state of the source.
* @param {import("./Source.js").State} state State.
*/
setState(state) {
this.state_ = state;
this.changed();
}
}
/**
* Turns the attributions option into an attributions function.
* @param {AttributionLike|undefined} attributionLike The attribution option.
* @return {Attribution|null} An attribution function (or null).
*/
function adaptAttributions(attributionLike) {
if (!attributionLike) {
return null;
}
if (Array.isArray(attributionLike)) {
return function (frameState) {
return attributionLike;
};
}
if (typeof attributionLike === 'function') {
return attributionLike;
}
return function (frameState) {
return [attributionLike];
};
}
export default Source;