Skip to content

Commit

Permalink
Merge pull request #1212 from OpenGeoscience/annotation-defaults
Browse files Browse the repository at this point in the history
perf: Explicitly have annotation defaults
  • Loading branch information
manthey authored May 26, 2022
2 parents 73fa35b + a84a921 commit f8a3c20
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 101 deletions.
15 changes: 13 additions & 2 deletions src/annotation/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 8 additions & 1 deletion src/annotation/circleAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 7 additions & 1 deletion src/annotation/ellipseAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 29 additions & 20 deletions src/annotation/lineAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
51 changes: 28 additions & 23 deletions src/annotation/pointAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
58 changes: 34 additions & 24 deletions src/annotation/polygonAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
53 changes: 29 additions & 24 deletions src/annotation/rectangleAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion src/annotation/squareAnnotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit f8a3c20

Please sign in to comment.