From a84a92119092ae4c4fa3cab57e186c972e216e1a Mon Sep 17 00:00:00 2001 From: David Manthey Date: Tue, 24 May 2022 15:54:25 -0400 Subject: [PATCH] perf: Explicitly have annotation defaults. --- src/annotation/annotation.js | 15 ++++++- src/annotation/circleAnnotation.js | 9 ++++- src/annotation/ellipseAnnotation.js | 8 +++- src/annotation/lineAnnotation.js | 49 +++++++++++++--------- src/annotation/pointAnnotation.js | 51 ++++++++++++----------- src/annotation/polygonAnnotation.js | 58 ++++++++++++++++----------- src/annotation/rectangleAnnotation.js | 53 +++++++++++++----------- src/annotation/squareAnnotation.js | 9 ++++- src/annotationLayer.js | 7 +++- tests/cases/annotationLayer.js | 8 ++-- 10 files changed, 166 insertions(+), 101 deletions(-) diff --git a/src/annotation/annotation.js b/src/annotation/annotation.js index dd93396f0f..8ec7c763d3 100644 --- a/src/annotation/annotation.js +++ b/src/annotation/annotation.js @@ -100,7 +100,7 @@ var annotation = function (type, args) { } var m_this = this, - m_options = $.extend({}, {showLabel: true}, args || {}), + m_options = $.extend(true, {}, this.constructor.defaults, args || {}), m_id = m_options.annotationId; delete m_options.annotationId; if (m_id === undefined || (m_options.layer && m_options.layer.annotationById(m_id))) { @@ -803,10 +803,14 @@ var annotation = function (type, args) { key = styles[i]; value = util.ensureFunction(objStyle[key])(); if (value !== undefined) { + let defvalue = ((m_this.constructor.defaults || {}).style || {})[key]; if (key.toLowerCase().match(/color$/)) { value = util.convertColorToHex(value, 'needed'); + defvalue = defvalue !== undefined ? util.convertColorToHex(defvalue, 'needed') : defvalue; + } + if (value !== defvalue) { + obj.properties[key] = value; } - obj.properties[key] = value; } } for (i = 0; i < textFeature.usedStyles.length; i += 1) { @@ -1414,6 +1418,13 @@ function constrainAspectRatio(ratio) { return constraintFunction; } +/** + * This object contains the default options to initialize the class. + */ +annotation.defaults = { + showLabel: true +}; + module.exports = { state: annotationState, actionOwner: annotationActionOwner, diff --git a/src/annotation/circleAnnotation.js b/src/annotation/circleAnnotation.js index 8f3f0f5603..ad7956623c 100644 --- a/src/annotation/circleAnnotation.js +++ b/src/annotation/circleAnnotation.js @@ -19,13 +19,20 @@ const ellipseAnnotation = require('./ellipseAnnotation'); */ var circleAnnotation = function (args, annotationName) { 'use strict'; - args = $.extend({}, args, {constraint: 1}); if (!(this instanceof circleAnnotation)) { return new circleAnnotation(args, annotationName); } + args = $.extend(true, {}, this.constructor.defaults, args, {constraint: 1}); ellipseAnnotation.call(this, args, annotationName || 'circle'); }; inherit(circleAnnotation, ellipseAnnotation); + +/** + * This object contains the default options to initialize the class. + */ +circleAnnotation.defaults = $.extend({}, ellipseAnnotation.defaults, { +}); + var circleRequiredFeatures = {}; circleRequiredFeatures[markerFeature.capabilities.feature] = true; registerAnnotation('circle', circleAnnotation, circleRequiredFeatures); diff --git a/src/annotation/ellipseAnnotation.js b/src/annotation/ellipseAnnotation.js index e3efa8c2ba..425097911b 100644 --- a/src/annotation/ellipseAnnotation.js +++ b/src/annotation/ellipseAnnotation.js @@ -23,7 +23,7 @@ var ellipseAnnotation = function (args, annotationName) { if (!(this instanceof ellipseAnnotation)) { return new ellipseAnnotation(args, annotationName); } - + args = $.extend(true, {}, this.constructor.defaults, args); rectangleAnnotation.call(this, args, annotationName || 'ellipse'); var m_this = this; @@ -117,6 +117,12 @@ var ellipseAnnotation = function (args, annotationName) { }; inherit(ellipseAnnotation, rectangleAnnotation); +/** + * This object contains the default options to initialize the class. + */ +ellipseAnnotation.defaults = $.extend({}, rectangleAnnotation.defaults, { +}); + var ellipseRequiredFeatures = {}; ellipseRequiredFeatures[markerFeature.capabilities.feature] = true; registerAnnotation('ellipse', ellipseAnnotation, ellipseRequiredFeatures); diff --git a/src/annotation/lineAnnotation.js b/src/annotation/lineAnnotation.js index 665be33e43..d0a6eeec09 100644 --- a/src/annotation/lineAnnotation.js +++ b/src/annotation/lineAnnotation.js @@ -37,8 +37,7 @@ var lineAnnotation = function (args) { if (!(this instanceof lineAnnotation)) { return new lineAnnotation(args); } - - args = $.extend(true, {}, { + args = $.extend(true, {}, this.constructor.defaults, { style: { line: function (d) { /* Return an array that has the same number of items as we have @@ -48,16 +47,7 @@ var lineAnnotation = function (args) { }, position: function (d, i) { return m_this.options('vertices')[i]; - }, - strokeColor: {r: 0, g: 0, b: 0}, - strokeOpacity: 1, - strokeWidth: 3, - closed: false, - lineCap: 'butt', - lineJoin: 'miter' - }, - highlightStyle: { - strokeWidth: 5 + } }, createStyle: { line: function (d) { @@ -68,15 +58,9 @@ var lineAnnotation = function (args) { }, position: function (d, i) { return m_this.options('vertices')[i]; - }, - strokeColor: {r: 0, g: 0, b: 1}, - strokeOpacity: 1, - strokeWidth: 3, - closed: false, - lineCap: 'butt', - lineJoin: 'miter' + } } - }, args || {}); + }, args); args.vertices = args.vertices || args.coordinates || []; delete args.coordinates; annotation.call(this, 'line', args); @@ -337,6 +321,31 @@ var lineAnnotation = function (args) { }; inherit(lineAnnotation, annotation); +/** + * This object contains the default options to initialize the class. + */ +lineAnnotation.defaults = $.extend({}, annotation.defaults, { + style: { + strokeColor: {r: 0, g: 0, b: 0}, + strokeOpacity: 1, + strokeWidth: 3, + closed: false, + lineCap: 'butt', + lineJoin: 'miter' + }, + highlightStyle: { + strokeWidth: 5 + }, + createStyle: { + strokeColor: {r: 0, g: 0, b: 1}, + strokeOpacity: 1, + strokeWidth: 3, + closed: false, + lineCap: 'butt', + lineJoin: 'miter' + } +}); + var lineRequiredFeatures = {}; lineRequiredFeatures[lineFeature.capabilities.basic] = [annotationState.create]; registerAnnotation('line', lineAnnotation, lineRequiredFeatures); diff --git a/src/annotation/pointAnnotation.js b/src/annotation/pointAnnotation.js index 2dadfe3279..f51d93d20c 100644 --- a/src/annotation/pointAnnotation.js +++ b/src/annotation/pointAnnotation.js @@ -40,29 +40,7 @@ var pointAnnotation = function (args) { return new pointAnnotation(args); } - args = $.extend(true, {}, { - style: { - fill: true, - fillColor: {r: 0, g: 1, b: 0}, - fillOpacity: 0.25, - radius: 10, - scaled: false, - stroke: true, - strokeColor: {r: 0, g: 0, b: 0}, - strokeOpacity: 1, - strokeWidth: 3 - }, - createStyle: { - fillColor: {r: 0.3, g: 0.3, b: 0.3}, - fillOpacity: 0.25, - strokeColor: {r: 0, g: 0, b: 1} - }, - highlightStyle: { - fillColor: {r: 0, g: 1, b: 1}, - fillOpacity: 0.5, - strokeWidth: 5 - } - }, args || {}); + args = $.extend(true, {}, this.constructor.defaults, args); args.position = args.position || (args.coordinates ? args.coordinates[0] : undefined); delete args.coordinates; annotation.call(this, 'point', args); @@ -201,6 +179,33 @@ var pointAnnotation = function (args) { }; inherit(pointAnnotation, annotation); +/** + * This object contains the default options to initialize the class. + */ +pointAnnotation.defaults = $.extend({}, annotation.defaults, { + style: { + fill: true, + fillColor: {r: 0, g: 1, b: 0}, + fillOpacity: 0.25, + radius: 10, + scaled: false, + stroke: true, + strokeColor: {r: 0, g: 0, b: 0}, + strokeOpacity: 1, + strokeWidth: 3 + }, + createStyle: { + fillColor: {r: 0.3, g: 0.3, b: 0.3}, + fillOpacity: 0.25, + strokeColor: {r: 0, g: 0, b: 1} + }, + highlightStyle: { + fillColor: {r: 0, g: 1, b: 1}, + fillOpacity: 0.5, + strokeWidth: 5 + } +}); + var pointRequiredFeatures = {}; pointRequiredFeatures[pointFeature.capabilities.feature] = true; registerAnnotation('point', pointAnnotation, pointRequiredFeatures); diff --git a/src/annotation/polygonAnnotation.js b/src/annotation/polygonAnnotation.js index f69ccd4877..dde0234a43 100644 --- a/src/annotation/polygonAnnotation.js +++ b/src/annotation/polygonAnnotation.js @@ -43,27 +43,11 @@ var polygonAnnotation = function (args) { return new polygonAnnotation(args); } - args = $.extend(true, {}, { + args = $.extend(true, {}, this.constructor.defaults, { style: { - fill: true, - fillColor: {r: 0, g: 1, b: 0}, - fillOpacity: 0.25, - polygon: function (d) { return d.polygon; }, - stroke: true, - strokeColor: {r: 0, g: 0, b: 0}, - strokeOpacity: 1, - strokeWidth: 3, - uniformPolygon: true - }, - highlightStyle: { - fillColor: {r: 0, g: 1, b: 1}, - fillOpacity: 0.5, - strokeWidth: 5 + polygon: function (d) { return d.polygon; } }, createStyle: { - closed: false, - fillColor: {r: 0.3, g: 0.3, b: 0.3}, - fillOpacity: 0.25, line: function (d) { const coord = m_this._coordinates(); /* Return an array that has the same number of items as we have @@ -76,12 +60,9 @@ var polygonAnnotation = function (args) { return d.x; } return m_this.options('vertices')[i]; - }, - stroke: false, - strokeColor: {r: 0, g: 0, b: 1} - }, - allowBooleanOperations: true - }, args || {}); + } + } + }, args); args.vertices = args.vertices || args.coordinates || []; delete args.coordinates; annotation.call(this, 'polygon', args); @@ -325,6 +306,35 @@ var polygonAnnotation = function (args) { }; inherit(polygonAnnotation, annotation); +/** + * This object contains the default options to initialize the class. + */ +polygonAnnotation.defaults = $.extend({}, annotation.defaults, { + style: { + fill: true, + fillColor: {r: 0, g: 1, b: 0}, + fillOpacity: 0.25, + stroke: true, + strokeColor: {r: 0, g: 0, b: 0}, + strokeOpacity: 1, + strokeWidth: 3, + uniformPolygon: true + }, + highlightStyle: { + fillColor: {r: 0, g: 1, b: 1}, + fillOpacity: 0.5, + strokeWidth: 5 + }, + createStyle: { + closed: false, + fillColor: {r: 0.3, g: 0.3, b: 0.3}, + fillOpacity: 0.25, + stroke: false, + strokeColor: {r: 0, g: 0, b: 1} + }, + allowBooleanOperations: true +}); + var polygonRequiredFeatures = {}; polygonRequiredFeatures[polygonFeature.capabilities.feature] = true; polygonRequiredFeatures[lineFeature.capabilities.basic] = [annotationState.create]; diff --git a/src/annotation/rectangleAnnotation.js b/src/annotation/rectangleAnnotation.js index 36e796c74b..a6f89238db 100644 --- a/src/annotation/rectangleAnnotation.js +++ b/src/annotation/rectangleAnnotation.js @@ -48,30 +48,7 @@ var rectangleAnnotation = function (args, annotationName) { return new rectangleAnnotation(args, annotationName); } - args = $.extend(true, {}, { - style: { - fill: true, - fillColor: {r: 0, g: 1, b: 0}, - fillOpacity: 0.25, - polygon: function (d) { return d.polygon; }, - stroke: true, - strokeColor: {r: 0, g: 0, b: 0}, - strokeOpacity: 1, - strokeWidth: 3, - uniformPolygon: true - }, - highlightStyle: { - fillColor: {r: 0, g: 1, b: 1}, - fillOpacity: 0.5, - strokeWidth: 5 - }, - createStyle: { - fillColor: {r: 0.3, g: 0.3, b: 0.3}, - fillOpacity: 0.25, - strokeColor: {r: 0, g: 0, b: 1} - }, - allowBooleanOperations: true - }, args || {}); + args = $.extend(true, {}, this.constructor.defaults, args); args.corners = args.corners || args.coordinates || []; delete args.coordinates; annotation.call(this, annotationName || 'rectangle', args); @@ -409,6 +386,34 @@ var rectangleAnnotation = function (args, annotationName) { }; inherit(rectangleAnnotation, annotation); +/** + * This object contains the default options to initialize the class. + */ +rectangleAnnotation.defaults = $.extend({}, annotation.defaults, { + style: { + fill: true, + fillColor: {r: 0, g: 1, b: 0}, + fillOpacity: 0.25, + polygon: function (d) { return d.polygon; }, + stroke: true, + strokeColor: {r: 0, g: 0, b: 0}, + strokeOpacity: 1, + strokeWidth: 3, + uniformPolygon: true + }, + highlightStyle: { + fillColor: {r: 0, g: 1, b: 1}, + fillOpacity: 0.5, + strokeWidth: 5 + }, + createStyle: { + fillColor: {r: 0.3, g: 0.3, b: 0.3}, + fillOpacity: 0.25, + strokeColor: {r: 0, g: 0, b: 1} + }, + allowBooleanOperations: true +}); + var rectangleRequiredFeatures = {}; rectangleRequiredFeatures[polygonFeature.capabilities.feature] = true; registerAnnotation('rectangle', rectangleAnnotation, rectangleRequiredFeatures); diff --git a/src/annotation/squareAnnotation.js b/src/annotation/squareAnnotation.js index 820c2071e8..cddb7cf409 100644 --- a/src/annotation/squareAnnotation.js +++ b/src/annotation/squareAnnotation.js @@ -19,13 +19,20 @@ const rectangleAnnotation = require('./rectangleAnnotation'); */ var squareAnnotation = function (args, annotationName) { 'use strict'; - args = $.extend({}, args, {constraint: 1}); if (!(this instanceof squareAnnotation)) { return new squareAnnotation(args, annotationName); } + args = $.extend(true, {}, this.constructor.defaults, args, {constraint: 1}); rectangleAnnotation.call(this, args, annotationName || 'square'); }; inherit(squareAnnotation, rectangleAnnotation); + +/** + * This object contains the default options to initialize the class. + */ +squareAnnotation.defaults = $.extend({}, rectangleAnnotation.defaults, { +}); + var squareRequiredFeatures = {}; squareRequiredFeatures[polygonFeature.capabilities.feature] = true; registerAnnotation('square', squareAnnotation, squareRequiredFeatures); diff --git a/src/annotationLayer.js b/src/annotationLayer.js index 95e6231bd9..03d993c1a3 100644 --- a/src/annotationLayer.js +++ b/src/annotationLayer.js @@ -693,7 +693,12 @@ var annotationLayer = function (arg) { */ this.geojson = function (geojson, clear, gcs, includeCrs) { if (geojson !== undefined) { - var reader = registry.createFileReader('geojsonReader', {layer: m_this}); + var reader = registry.createFileReader('geojsonReader', { + layer: m_this, + lineStyle: require('./annotation/lineAnnotation').defaults.style, + pointStyle: require('./annotation/pointAnnotation').defaults.style, + polygonStyle: require('./annotation/polygonAnnotation').defaults.style + }); if (!reader.canRead(geojson)) { return; } diff --git a/tests/cases/annotationLayer.js b/tests/cases/annotationLayer.js index c8ff39af4a..254bde1c3d 100644 --- a/tests/cases/annotationLayer.js +++ b/tests/cases/annotationLayer.js @@ -828,10 +828,10 @@ describe('geo.annotationLayer', function () { }; layer.geojson(badattr, true); var attr = layer.geojson().features[0].properties; - expect(attr.radius).toBeGreaterThan(0); - expect(attr.fillOpacity).toBeGreaterThan(0); - expect(attr.fillColor).toBe('#00ff00'); - expect(attr.scaled).toBe(false); + expect(attr.radius).toBe(undefined); + expect(attr.fillOpacity).toBe(undefined); + expect(attr.fillColor).toBe(undefined); + expect(attr.scaled).toBe(undefined); var goodattr = { type: 'Feature', geometry: {