/
CatalogFunction.js
275 lines (239 loc) · 8.88 KB
/
CatalogFunction.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
"use strict";
/*global require*/
var arraysAreEqual = require("../Core/arraysAreEqual");
var CatalogItem = require("./CatalogItem");
var CatalogMember = require("./CatalogMember");
var clone = require("terriajs-cesium/Source/Core/clone").default;
var createCatalogMemberFromType = require("./createCatalogMemberFromType");
var defined = require("terriajs-cesium/Source/Core/defined").default;
var DeveloperError = require("terriajs-cesium/Source/Core/DeveloperError")
.default;
var inherit = require("../Core/inherit");
var knockout = require("terriajs-cesium/Source/ThirdParty/knockout").default;
var runLater = require("../Core/runLater");
var when = require("terriajs-cesium/Source/ThirdParty/when").default;
/**
* A member of a catalog that does some kind of parameterized processing or analysis.
*
* @alias CatalogFunction
* @constructor
* @extends CatalogMember
* @abstract
*
* @param {Terria} terria The Terria instance.
*/
var CatalogFunction = function(terria) {
CatalogMember.call(this, terria);
this._loadingPromise = undefined;
this._lastLoadInfluencingValues = undefined;
this._parameters = [];
/**
* Gets or sets a value indicating whether the group is currently loading. This property
* is observable.
* @type {Boolean}
*/
this.isLoading = false;
/**
* A catalog item that will be enabled while preparing to invoke this catalog function, in order to
* provide context for the function.
* @type {CatalogItem}
*/
this.contextItem = undefined;
knockout.track(this, ["isLoading"]);
};
inherit(CatalogMember, CatalogFunction);
Object.defineProperties(CatalogFunction.prototype, {
/**
* Gets a value indicating whether this catalog member can show information. If so, an info icon will be shown next to the item
* in the data catalog.
* @memberOf CatalogFunction.prototype
* @type {Boolean}
*/
showsInfo: {
get: function() {
return true;
}
},
/**
* Gets the parameters used to {@link CatalogFunction#invoke} to this process.
* @memberOf CatalogFunction
* @type {CatalogFunctionParameters[]}
*/
parameters: {
get: function() {
throw new DeveloperError(
"parameters must be implemented in the derived class."
);
}
},
/**
* Gets the metadata associated with this data item and the server that provided it, if applicable.
* @memberOf CatalogItem.prototype
* @type {Metadata}
*/
metadata: {
get: function() {
return CatalogItem.defaultMetadata;
}
},
/**
* Gets the set of functions used to update individual properties in {@link CatalogMember#updateFromJson}.
* When a property name in the returned object literal matches the name of a property on this instance, the value
* will be called as a function and passed a reference to this instance, a reference to the source JSON object
* literal, and the name of the property.
* @memberOf CatalogFunction.prototype
* @type {Object}
*/
updaters: {
get: function() {
return CatalogFunction.defaultUpdaters;
}
},
/**
* Gets the set of functions used to serialize individual properties in {@link CatalogMember#serializeToJson}.
* When a property name on the model matches the name of a property in the serializers object literal,
* the value will be called as a function and passed a reference to the model, a reference to the destination
* JSON object literal, and the name of the property.
* @memberOf CatalogFunction.prototype
* @type {Object}
*/
serializers: {
get: function() {
return CatalogFunction.defaultSerializers;
}
},
/**
* Gets the set of names of the properties to be serialized for this object when {@link CatalogMember#serializeToJson} is called
* for a share link.
* @memberOf ImageryLayerCatalogItem.prototype
* @type {String[]}
*/
propertiesForSharing: {
get: function() {
return CatalogFunction.defaultPropertiesForSharing;
}
}
});
CatalogFunction.defaultUpdaters = clone(CatalogMember.defaultUpdaters);
CatalogFunction.defaultUpdaters.contextItem = function(
catalogFunction,
json,
propertyName,
options
) {
var itemJson = json[propertyName];
var itemObject = (catalogFunction.contextItem = createCatalogMemberFromType(
itemJson.type,
catalogFunction.terria
));
return itemObject.updateFromJson(itemJson, options);
};
Object.freeze(CatalogFunction.defaultUpdaters);
CatalogFunction.defaultSerializers = clone(CatalogMember.defaultSerializers);
CatalogFunction.defaultSerializers.contextItem = function(
catalogFunction,
json,
propertyName,
options
) {
if (defined(catalogFunction.contextItem)) {
json[propertyName] = catalogFunction.contextItem.serializeToJson(options);
}
};
Object.freeze(CatalogFunction.defaultSerializers);
CatalogFunction.defaultPropertiesForSharing = clone(
CatalogMember.defaultPropertiesForSharing
);
/**
* Loads this function, if it's not already loaded. It is safe to
* call this method multiple times. The {@link CatalogFunction#isLoading} flag will be set while the load is in progress.
* Derived classes should implement {@link CatalogFunction#_load} to perform the actual loading for the function.
* Derived classes may optionally implement {@link CatalogFunction#_getValuesThatInfluenceLoad} to provide an array containing
* the current value of all properties that influence this function's load process. Each time that {@link CatalogFunction#load}
* is invoked, these values are checked against the list of values returned last time, and {@link CatalogFunction#_load} is
* invoked again if they are different. If {@link CatalogFunction#_getValuesThatInfluenceLoad} is undefined or returns an
* empty array, {@link CatalogFunction#_load} will only be invoked once, no matter how many times
* {@link CatalogFunction#load} is invoked.
*
* @returns {Promise} A promise that resolves when the load is complete, or undefined if the function is already loaded.
*
*/
CatalogFunction.prototype.load = function() {
if (defined(this._loadingPromise)) {
// Load already in progress.
return this._loadingPromise;
}
var loadInfluencingValues = [];
if (defined(this._getValuesThatInfluenceLoad)) {
loadInfluencingValues = this._getValuesThatInfluenceLoad();
}
if (arraysAreEqual(loadInfluencingValues, this._lastLoadInfluencingValues)) {
// Already loaded, and nothing has changed to force a re-load.
return undefined;
}
this.isLoading = true;
var that = this;
this._loadingPromise = runLater(function() {
that._lastLoadInfluencingValues = [];
if (defined(that._getValuesThatInfluenceLoad)) {
that._lastLoadInfluencingValues = that._getValuesThatInfluenceLoad();
}
// Load the catalog function itself
return when(that._load()).then(function(loadResult) {
// And then load all the parameters.
return when
.all(that.parameters.map(parameter => parameter.load()))
.then(function() {
// And then return the result of the catalog function load.
return loadResult;
});
});
})
.then(function() {
that._loadingPromise = undefined;
that.isLoading = false;
})
.otherwise(function(e) {
that._lastLoadInfluencingValues = undefined;
that._loadingPromise = undefined;
that.isLoading = false;
throw e;
});
return this._loadingPromise;
};
/**
* Invokes the function.
* @return {AsyncProcessResultCatalogItem} The result of invoking this process. Because the process typically proceeds asynchronously, the result is a temporary
* catalog item that resolves to the real one once the process finishes.
*/
CatalogFunction.prototype.invoke = function() {
throw new DeveloperError("invoke must be implemented in the derived class.");
};
/**
* Gets the current parameters to this function.
* @return {Object} An object with a property for each parameter. The property name is the `id` of the
* parameter and the property value is the value of that parameter.
*/
CatalogFunction.prototype.getParameterValues = function() {
var result = {};
this.parameters.forEach(function(parameter) {
result[parameter.id] = parameter.value;
});
return result;
};
/**
* Sets the current parameters to this function.
* @param {Object} parameterValues An object describing the parameters to set and their values. Each property name
* in this object corresponds to the `id` of a parameter, and the value of that property is the new
* value for the parameter. If there is no parameter corresponding to a property in this object, that
* property is silently ignored.
*/
CatalogFunction.prototype.setParameterValues = function(parameterValues) {
Object.keys(parameterValues).forEach(function(id) {
var parameter = this.parameters.filter(p => p.id === id)[0];
if (defined(parameter)) {
parameter.value = parameterValues[id];
}
});
};
module.exports = CatalogFunction;