bruce-models 4.9.9 → 5.0.0

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.
@@ -4147,61 +4147,228 @@ var Calculator;
4147
4147
  EValueType[EValueType["RandomColor"] = 5] = "RandomColor";
4148
4148
  })(EValueType = Calculator.EValueType || (Calculator.EValueType = {}));
4149
4149
  /**
4150
- * Calculates the value of arbitrary field options.
4151
- * The context calling this should validate results and parse stuff if needed.
4152
- * @deprecated use specific calls: eg GetColor or GetNumber.
4153
- * @param fields
4154
- * @param entity
4155
- * @param tags
4156
- * @returns
4150
+ * Calculates a value-type 'T' based on given set of fields.
4151
+ * The supplied data + tags are fed into the calculation.
4152
+ * When a value fails to be calculated, the 'defaultValue' is returned.
4153
+ * If no 'defaultValue' is provided, then 'null' is returned.
4157
4154
  */
4158
- function GetValue(fields, entity, tags) {
4159
- if (!(fields === null || fields === void 0 ? void 0 : fields.length)) {
4160
- return null;
4155
+ function Calculate(params) {
4156
+ let { fields, tags, data, defaultValue, type } = params;
4157
+ if (fields == null) {
4158
+ return defaultValue;
4159
+ }
4160
+ if (data == null || typeof data != "object") {
4161
+ data = {};
4161
4162
  }
4162
- if (!tags) {
4163
+ if (tags == null || !Array.isArray(tags)) {
4163
4164
  tags = [];
4164
4165
  }
4165
- if (!entity) {
4166
- entity = {};
4166
+ if (type == null) {
4167
+ type = "any";
4168
+ }
4169
+ // Normalize fields to be an array of options.
4170
+ let fieldsArr = [];
4171
+ if (Array.isArray(fields)) {
4172
+ fieldsArr = fields;
4167
4173
  }
4168
- for (let i = 0; i < fields.length; i++) {
4169
- const field = fields[i];
4170
- let value;
4174
+ else {
4175
+ fieldsArr = [fields];
4176
+ }
4177
+ // Indicator for old data.
4178
+ // When a range is provided in older data, it is expected that invalid/failed mappings should default to the minimum value.
4179
+ // We are deprecating this logic.
4180
+ // The first step of deprecation is to avoid using this logic when multiple calculations are provided.
4181
+ // TODO: When we've reviewed all existing uses, we can remove this logic entirely and just update the needed style records.
4182
+ let shouldRangesDefaultToMin = true;
4183
+ // This reduce totals the number of mappings and gradients.
4184
+ // >1 means we should not default to min as there are multiple calculations.
4185
+ if (fieldsArr.reduce((acc, field) => acc += (field.type == EValueType.Mapping || field.type == EValueType.Gradient) ? 1 : 0, 0) > 1) {
4186
+ shouldRangesDefaultToMin = false;
4187
+ }
4188
+ const assertColor = (color) => {
4189
+ if (color && typeof color == "string") {
4190
+ color = Color.ColorFromStr(color);
4191
+ }
4192
+ if (color && typeof color == "object" && !isNaN(color.red) && color.red != null) {
4193
+ return color;
4194
+ }
4195
+ return null;
4196
+ };
4197
+ const isColor = (value) => {
4198
+ if (value && typeof value == "object" && !isNaN(value.red) && value.red != null) {
4199
+ return true;
4200
+ }
4201
+ return false;
4202
+ };
4203
+ // Calculate the value.
4204
+ let value = null;
4205
+ for (let i = 0; i < fieldsArr.length; i++) {
4206
+ let field = fieldsArr[i];
4207
+ if (field) {
4208
+ // Dereference.
4209
+ // We do this because we'll be modifying the field to fix bad data.
4210
+ field = JSON.parse(JSON.stringify(field));
4211
+ }
4212
+ // Bad data has it set to 'color' but expects the input to be a number.
4213
+ if (type == "number" && field.type == EValueType.Color) {
4214
+ field.type = EValueType.Input;
4215
+ }
4171
4216
  switch (field.type) {
4172
4217
  case EValueType.Color:
4218
+ if (type === "number") {
4219
+ break;
4220
+ }
4173
4221
  value = field.value;
4174
- if (value) {
4175
- value = Color.ColorFromStr(value);
4222
+ value = assertColor(value);
4223
+ if (value && type == "string") {
4224
+ value = `rgba(${value.red},${value.green},${value.blue},${value.alpha})`;
4176
4225
  }
4177
4226
  break;
4178
4227
  case EValueType.Gradient:
4179
- value = GetGradientValue(field.value, entity);
4180
- break;
4181
- case EValueType.Input:
4182
- value = GetInputValue(field.value, entity);
4183
- break;
4184
- case EValueType.Mapping:
4185
- value = GetMappingValue(field.value, entity);
4228
+ if (type === "number") {
4229
+ break;
4230
+ }
4231
+ value = GetGradientValue(field.value, data, shouldRangesDefaultToMin);
4232
+ value = assertColor(value);
4233
+ if (value && type == "string") {
4234
+ value = `rgba(${value.red},${value.green},${value.blue},${value.alpha})`;
4235
+ }
4186
4236
  break;
4187
4237
  case EValueType.RandomColor:
4238
+ if (type === "number") {
4239
+ break;
4240
+ }
4188
4241
  value = Color.RandomColor();
4242
+ value = assertColor(value);
4243
+ if (value && type == "string") {
4244
+ value = `rgba(${value.red},${value.green},${value.blue},${value.alpha})`;
4245
+ }
4189
4246
  break;
4190
4247
  case EValueType.TagColor:
4248
+ if (type === "number") {
4249
+ break;
4250
+ }
4191
4251
  for (let i = 0; i < tags.length; i++) {
4192
4252
  const tag = tags[i];
4193
4253
  if (tag.Color) {
4194
4254
  value = Color.ColorFromStr(tag.Color);
4195
- break;
4255
+ value = assertColor(value);
4256
+ if (value) {
4257
+ break;
4258
+ }
4259
+ }
4260
+ }
4261
+ if (value && type == "string") {
4262
+ const cColor = value;
4263
+ value = `rgba(${cColor.red},${cColor.green},${cColor.blue},${cColor.alpha})`;
4264
+ }
4265
+ break;
4266
+ // Input can be an arbitrary value.
4267
+ // Eg: Result is a colour, a number, a string.
4268
+ // This means we first calculate the value, then validate it in the context of the desired type.
4269
+ case EValueType.Input:
4270
+ value = GetInputValue(field.value, data);
4271
+ if (value != null) {
4272
+ if (type == "number") {
4273
+ if (typeof value == "string" && value != "" && value != null && value != undefined) {
4274
+ value = Number(value);
4275
+ }
4276
+ else if (typeof value != "number") {
4277
+ value = null;
4278
+ }
4279
+ }
4280
+ else if (type == "string") {
4281
+ if (typeof value == "number") {
4282
+ value = String(value);
4283
+ }
4284
+ else if (typeof value == "object") {
4285
+ if (isColor(value)) {
4286
+ value = `rgba(${value.red},${value.green},${value.blue},${value.alpha})`;
4287
+ }
4288
+ else {
4289
+ value = null;
4290
+ }
4291
+ }
4292
+ }
4293
+ else if (type == "color") {
4294
+ value = assertColor(value);
4295
+ }
4296
+ }
4297
+ break;
4298
+ // Mapping can be an arbitrary value.
4299
+ // Eg: mapping a value to a Client File ID, or a colour, or a number.
4300
+ // This means we first calculate the value, then validate it in the context of the desired type.
4301
+ case EValueType.Mapping:
4302
+ value = GetMappingValue(field.value, data);
4303
+ if (value != null) {
4304
+ if (type == "number") {
4305
+ if (typeof value == "string" && value != "" && value != null && value != undefined) {
4306
+ value = Number(value);
4307
+ }
4308
+ else if (typeof value != "number") {
4309
+ value = null;
4310
+ }
4311
+ }
4312
+ else if (type == "string") {
4313
+ if (typeof value == "number") {
4314
+ value = String(value);
4315
+ }
4316
+ else if (typeof value == "object") {
4317
+ if (isColor(value)) {
4318
+ value = `rgba(${value.red},${value.green},${value.blue},${value.alpha})`;
4319
+ }
4320
+ else {
4321
+ value = null;
4322
+ }
4323
+ }
4324
+ }
4325
+ else if (type == "color") {
4326
+ value = assertColor(typeof value === "number" ? String(value) : value);
4196
4327
  }
4197
4328
  }
4198
4329
  break;
4199
4330
  }
4200
- if (value || value == 0) {
4201
- return value;
4331
+ // Extra layer of validation.
4332
+ // This is a backup in case some branch above failed to convert into the right type.
4333
+ if (type == "number") {
4334
+ if (typeof value !== "number" || isNaN(value)) {
4335
+ value = null;
4336
+ }
4337
+ }
4338
+ else if (type == "color") {
4339
+ if (typeof value !== "object" || !value) {
4340
+ value = null;
4341
+ }
4342
+ }
4343
+ else if (type == "string") {
4344
+ if (typeof value !== "string" || !value) {
4345
+ value = null;
4346
+ }
4347
+ }
4348
+ if (value != null) {
4349
+ break;
4202
4350
  }
4203
4351
  }
4204
- return null;
4352
+ // Return the value or default.
4353
+ return value != null ? value : defaultValue;
4354
+ }
4355
+ Calculator.Calculate = Calculate;
4356
+ /**
4357
+ * Calculates the value of arbitrary field options.
4358
+ * The context calling this should validate results and parse stuff if needed.
4359
+ * @param fields
4360
+ * @param entity
4361
+ * @param tags
4362
+ * @returns
4363
+ */
4364
+ function GetValue(fields, entity = {}, tags = []) {
4365
+ return Calculate({
4366
+ fields: fields,
4367
+ data: entity,
4368
+ defaultValue: null,
4369
+ tags: tags,
4370
+ type: "any"
4371
+ });
4205
4372
  }
4206
4373
  Calculator.GetValue = GetValue;
4207
4374
  /**
@@ -4211,53 +4378,14 @@ var Calculator;
4211
4378
  * @param entity
4212
4379
  * @param tags
4213
4380
  */
4214
- function GetColor(fields, entity, tags) {
4215
- if (!(fields === null || fields === void 0 ? void 0 : fields.length)) {
4216
- return null;
4217
- }
4218
- if (!tags) {
4219
- tags = [];
4220
- }
4221
- if (!entity) {
4222
- entity = {};
4223
- }
4224
- for (let i = 0; i < fields.length; i++) {
4225
- const field = fields[i];
4226
- let value;
4227
- switch (field.type) {
4228
- case EValueType.Color:
4229
- value = field.value;
4230
- break;
4231
- case EValueType.Gradient:
4232
- value = GetGradientValue(field.value, entity);
4233
- break;
4234
- case EValueType.Input:
4235
- value = GetInputValue(field.value, entity);
4236
- break;
4237
- case EValueType.Mapping:
4238
- value = GetMappingValue(field.value, entity);
4239
- break;
4240
- case EValueType.RandomColor:
4241
- value = Color.RandomColor();
4242
- break;
4243
- case EValueType.TagColor:
4244
- for (let i = 0; i < tags.length; i++) {
4245
- const tag = tags[i];
4246
- if (tag.Color) {
4247
- value = Color.ColorFromStr(tag.Color);
4248
- break;
4249
- }
4250
- }
4251
- break;
4252
- }
4253
- if (typeof value === "string") {
4254
- value = Color.ColorFromStr(value);
4255
- }
4256
- if (value && typeof value === "object" && (value.red || value.red == 0)) {
4257
- return value;
4258
- }
4259
- }
4260
- return null;
4381
+ function GetColor(fields, entity = {}, tags = []) {
4382
+ return Calculate({
4383
+ fields: fields,
4384
+ data: entity,
4385
+ defaultValue: null,
4386
+ tags: tags,
4387
+ type: "color"
4388
+ });
4261
4389
  }
4262
4390
  Calculator.GetColor = GetColor;
4263
4391
  /**
@@ -4268,39 +4396,14 @@ var Calculator;
4268
4396
  * @param tags
4269
4397
  * @returns
4270
4398
  */
4271
- function GetNumber(fields, entity, tags) {
4272
- if (!(fields === null || fields === void 0 ? void 0 : fields.length)) {
4273
- return null;
4274
- }
4275
- if (!tags) {
4276
- tags = [];
4277
- }
4278
- if (!entity) {
4279
- entity = {};
4280
- }
4281
- for (let i = 0; i < fields.length; i++) {
4282
- const field = fields[i];
4283
- let value;
4284
- switch (field.type) {
4285
- case EValueType.Gradient:
4286
- value = GetGradientValue(field.value, entity);
4287
- break;
4288
- case EValueType.Color:
4289
- case EValueType.Input:
4290
- value = GetInputValue(field.value, entity);
4291
- break;
4292
- case EValueType.Mapping:
4293
- value = GetMappingValue(field.value, entity);
4294
- break;
4295
- }
4296
- if (value != "" && value != null && value != undefined) {
4297
- value = Number(value);
4298
- }
4299
- if (value || value == 0) {
4300
- return value;
4301
- }
4302
- }
4303
- return null;
4399
+ function GetNumber(fields, entity = {}, tags = []) {
4400
+ return Calculate({
4401
+ fields: fields,
4402
+ data: entity,
4403
+ defaultValue: null,
4404
+ tags: tags,
4405
+ type: "number"
4406
+ });
4304
4407
  }
4305
4408
  Calculator.GetNumber = GetNumber;
4306
4409
  /**
@@ -4311,68 +4414,14 @@ var Calculator;
4311
4414
  * @param tags
4312
4415
  * @returns
4313
4416
  */
4314
- function GetString(fields, entity, tags) {
4315
- if (!(fields === null || fields === void 0 ? void 0 : fields.length)) {
4316
- return null;
4317
- }
4318
- if (!tags) {
4319
- tags = [];
4320
- }
4321
- if (!entity) {
4322
- entity = {};
4323
- }
4324
- for (let i = 0; i < fields.length; i++) {
4325
- const field = fields[i];
4326
- let value;
4327
- switch (field.type) {
4328
- case EValueType.Gradient:
4329
- {
4330
- const tmp = GetGradientValue(field.value, entity);
4331
- // This avoids null turning into "null".
4332
- if (tmp) {
4333
- value = String(tmp);
4334
- }
4335
- }
4336
- break;
4337
- case EValueType.Color:
4338
- case EValueType.Input:
4339
- {
4340
- const tmp = GetInputValue(field.value, entity);
4341
- // This avoids null turning into "null".
4342
- if (tmp) {
4343
- value = String(tmp);
4344
- }
4345
- }
4346
- break;
4347
- case EValueType.Mapping:
4348
- {
4349
- const tmp = GetMappingValue(field.value, entity);
4350
- // This avoids null turning into "null".
4351
- if (tmp) {
4352
- value = String(tmp);
4353
- }
4354
- }
4355
- break;
4356
- case EValueType.RandomColor:
4357
- // Would be nice to randomize based on an attribute or entity ID instead of pure random.
4358
- var color = Color.RandomColor();
4359
- value = `rgba(${color.red},${color.green},${color.blue},${color.alpha})`;
4360
- break;
4361
- case EValueType.TagColor:
4362
- for (let i = 0; i < tags.length; i++) {
4363
- const tag = tags[i];
4364
- if (tag.Color) {
4365
- value = tag.Color;
4366
- break;
4367
- }
4368
- }
4369
- break;
4370
- }
4371
- if (value) {
4372
- return value;
4373
- }
4374
- }
4375
- return null;
4417
+ function GetString(fields, entity = {}, tags = []) {
4418
+ return Calculate({
4419
+ fields: fields,
4420
+ data: entity,
4421
+ defaultValue: null,
4422
+ tags: tags,
4423
+ type: "string"
4424
+ });
4376
4425
  }
4377
4426
  Calculator.GetString = GetString;
4378
4427
  /**
@@ -4440,9 +4489,11 @@ var Calculator;
4440
4489
  * It is intended to be parsed and validated within a value-type context. Eg: GetColor, GetNumber, GetString.
4441
4490
  * @param value
4442
4491
  * @param entity
4492
+ * @param defaultToMin if a value fails to be calculated, should we default to the minimum value?
4493
+ * Default is true for backwards compatibility.
4443
4494
  * @returns
4444
4495
  */
4445
- function GetGradientValue(value, entity) {
4496
+ function GetGradientValue(value, entity, defaultToMin) {
4446
4497
  const min = +value.points[0].position;
4447
4498
  const max = +value.points[value.points.length - 1].position;
4448
4499
  const attrPaths = parseLegacyPath(value.field);
@@ -14417,7 +14468,7 @@ var DataSource;
14417
14468
  })(DataSource || (DataSource = {}));
14418
14469
 
14419
14470
  // This is updated with the package.json version on build.
14420
- const VERSION = "4.9.9";
14471
+ const VERSION = "5.0.0";
14421
14472
 
14422
14473
  export { VERSION, AnnDocument, CustomForm, AbstractApi, Api, BruceApi, GlobalApi, GuardianApi, ApiGetters, Calculator, Bounds, BruceEvent, CacheControl, Camera, Cartes, Carto, Color, DelayQueue, Geometry, UTC, BruceVariable, LRUCache, GeoJson, EntityAttachmentType, EntityAttachment, EntityComment, EntityLink, EntityLod, EntityLodCategory, EntityRelationType, EntityRelation, EntitySource, EntityTag, EntityType, Entity, EntityCoords, EntityTypeVisualSettings, EntityAttribute, EntityHistoricData, EntityTableView, Comment, ClientFile, ProgramKey, ZoomControl, MenuItem, ProjectViewBookmark, ProjectView, ProjectViewLegacyTile, ProjectViewTile, ProjectViewLegacy, ProjectViewLegacyBookmark, PendingAction, MessageBroker, HostingLocation, Style, Tileset, Permission, Session, UserGroup, User, Account, AccountInvite, AccountFeatures, AccountLimits, EncryptUtils, MathUtils, ObjectUtils, PathUtils, UrlUtils, DataLab, ImportAssembly, ImportCad, ImportCsv, ImportJson, ImportGeoJson, ImportKml, ImportedFile, Markup, Uploader, Plugin, ENVIRONMENT, DataSource };
14423
14474
  //# sourceMappingURL=bruce-models.es5.js.map