perf: Explicitly have annotation defaults.
MODIFY
src/annotation/annotation.js
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,12 +803,16 @@ 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;
}
}
+ }
for (i = 0; i < textFeature.usedStyles.length; i += 1) {
key = textFeature.usedStyles[i];
value = util.ensureFunction(objLabelStyle[key])();
@@ -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,

MODIFY
src/annotation/circleAnnotation.js
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);

MODIFY
src/annotation/ellipseAnnotation.js
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);

MODIFY
src/annotation/lineAnnotation.js
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);

MODIFY
src/annotation/pointAnnotation.js
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);

MODIFY
src/annotation/polygonAnnotation.js
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];

MODIFY
src/annotation/rectangleAnnotation.js
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);

MODIFY
src/annotation/squareAnnotation.js
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);

MODIFY
src/annotationLayer.js
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;
}

MODIFY
tests/cases/annotationLayer.js
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: {

perf: Explicitly have annotation defaults.