Skip to content

Commit

Permalink
Merge pull request #1204 from OpenGeoscience/split-annotation-js
Browse files Browse the repository at this point in the history
refactor: Refactor annotation code to make it less monolithic.
  • Loading branch information
manthey authored May 13, 2022
2 parents c8c9edd + 0122feb commit 610139f
Show file tree
Hide file tree
Showing 10 changed files with 1,446 additions and 1,364 deletions.
1,374 changes: 11 additions & 1,363 deletions src/annotation.js → src/annotation/annotation.js

Large diffs are not rendered by default.

33 changes: 33 additions & 0 deletions src/annotation/circleAnnotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const $ = require('jquery');
const inherit = require('../inherit');
const registerAnnotation = require('../registry').registerAnnotation;
const markerFeature = require('../markerFeature');

const ellipseAnnotation = require('./ellipseAnnotation');

/**
* Circle annotation class.
*
* Circles are a subset of rectangles with a fixed aspect ratio.
*
* @class
* @alias geo.circleAnnotation
* @extends geo.annotation
*
* @param {geo.circleAnnotation.spec?} [args] Options for the annotation.
* @param {string} [annotationName='circle'] Override the annotation name.
*/
var circleAnnotation = function (args, annotationName) {
'use strict';
args = $.extend({}, args, {constraint: 1});
if (!(this instanceof circleAnnotation)) {
return new circleAnnotation(args, annotationName);
}
ellipseAnnotation.call(this, args, annotationName || 'circle');
};
inherit(circleAnnotation, ellipseAnnotation);
var circleRequiredFeatures = {};
circleRequiredFeatures[markerFeature.capabilities.feature] = true;
registerAnnotation('circle', circleAnnotation, circleRequiredFeatures);

module.exports = circleAnnotation;
80 changes: 80 additions & 0 deletions src/annotation/ellipseAnnotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
const $ = require('jquery');
const inherit = require('../inherit');
const registerAnnotation = require('../registry').registerAnnotation;
const markerFeature = require('../markerFeature');

const annotationState = require('./annotation').state;
const rectangleAnnotation = require('./rectangleAnnotation');

/**
* Ellipse annotation class.
*
* Ellipses are always rendered as markers.
*
* @class
* @alias geo.ellipseAnnotation
* @extends geo.annotation
*
* @param {geo.ellipseAnnotation.spec?} [args] Options for the annotation.
* @param {string} [annotationName='ellipse'] Override the annotation name.
*/
var ellipseAnnotation = function (args, annotationName) {
'use strict';
if (!(this instanceof ellipseAnnotation)) {
return new ellipseAnnotation(args, annotationName);
}

rectangleAnnotation.call(this, args, annotationName || 'ellipse');

var m_this = this;

/**
* Get a list of renderable features for this annotation.
*
* @returns {array} An array of features.
*/
this.features = function () {
var opt = m_this.options(),
state = m_this.state(),
features;
features = [];
if (opt.corners && opt.corners.length >= 4) {
const style = m_this.styleForState(state);
const w = ((opt.corners[0].x - opt.corners[1].x) ** 2 + (opt.corners[0].y - opt.corners[1].y) ** 2) ** 0.5;
const h = ((opt.corners[0].x - opt.corners[3].x) ** 2 + (opt.corners[0].y - opt.corners[3].y) ** 2) ** 0.5;
const radius = Math.max(w, h) / 2 / m_this.layer().map().unitsPerPixel(0);
const aspect = w ? h / w : 1e20;
const rotation = -Math.atan2(opt.corners[1].y - opt.corners[0].y, opt.corners[1].x - opt.corners[0].x);
features = [{
marker: {
x: (opt.corners[0].x + opt.corners[1].x + opt.corners[2].x + opt.corners[3].x) / 4,
y: (opt.corners[0].y + opt.corners[1].y + opt.corners[2].y + opt.corners[3].y) / 4,
style: $.extend(
{}, style,
{
radius: radius,
symbolValue: aspect,
rotation: rotation,
strokeOffset: 0,
radiusIncludesStroke: false,
scaleWithZoom: markerFeature.scaleMode.fill,
rotateWithMap: true,
strokeOpacity: style.stroke === false ? 0 : style.strokeOpacity,
fillOpacity: style.fill === false ? 0 : style.fillOpacity
})
}
}];
}
if (state === annotationState.edit) {
m_this._addEditHandles(features, opt.corners);
}
return features;
};
};
inherit(ellipseAnnotation, rectangleAnnotation);

var ellipseRequiredFeatures = {};
ellipseRequiredFeatures[markerFeature.capabilities.feature] = true;
registerAnnotation('ellipse', ellipseAnnotation, ellipseRequiredFeatures);

module.exports = ellipseAnnotation;
21 changes: 21 additions & 0 deletions src/annotation/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const annotation = require('./annotation');

/**
* @namespace geo.annotation
*/
module.exports = {
state: annotation.state,
actionOwner: annotation.actionOwner,
annotation: annotation.annotation,
_editHandleFeatureLevel: annotation._editHandleFeatureLevel,
defaultEditHandleStyle: annotation.defaultEditHandleStyle,
constrainAspectRatio: annotation.constrainAspectRatio,
baseAnnotation: annotation,
circleAnnotation: require('./circleAnnotation'),
ellipseAnnotation: require('./ellipseAnnotation'),
lineAnnotation: require('./lineAnnotation'),
pointAnnotation: require('./pointAnnotation'),
polygonAnnotation: require('./polygonAnnotation'),
rectangleAnnotation: require('./rectangleAnnotation'),
squareAnnotation: require('./squareAnnotation')
};
Loading

0 comments on commit 610139f

Please sign in to comment.