Skip to content

Commit 35b2d47

Browse files
committed
perf: Add addMultipleAnnotations method
This is primarily faster because of the less events being triggered. The addAnnotation method can also ask to not trigger events.
1 parent 1272b99 commit 35b2d47

File tree

3 files changed

+63
-9
lines changed

3 files changed

+63
-9
lines changed

src/annotationLayer.js

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -491,18 +491,22 @@ var annotationLayer = function (arg) {
491491
* gcs, `null` to use the map gcs, or any other transform.
492492
* @param {boolean} [update] If `false`, don't update the layer after adding
493493
* the annotation.
494+
* @param {boolean} [trigger] If `false`, do trigger add_before and add
495+
* events.
494496
* @returns {this} The current layer.
495497
* @fires geo.event.annotation.add_before
496498
* @fires geo.event.annotation.add
497499
*/
498-
this.addAnnotation = function (annotation, gcs, update) {
500+
this.addAnnotation = function (annotation, gcs, update, trigger) {
499501
if (m_annotationIds[annotation.id()] === undefined) {
500502
while (m_this.annotationById(annotation.id())) {
501503
annotation.newId();
502504
}
503-
m_this.geoTrigger(geo_event.annotation.add_before, {
504-
annotation: annotation
505-
});
505+
if (trigger !== false) {
506+
m_this.geoTrigger(geo_event.annotation.add_before, {
507+
annotation: annotation
508+
});
509+
}
506510
m_annotations.push(annotation);
507511
m_annotationIds[annotation.id()] = annotation;
508512
annotation.layer(m_this);
@@ -516,8 +520,43 @@ var annotationLayer = function (arg) {
516520
m_this.modified();
517521
m_this.draw();
518522
}
523+
if (trigger !== false) {
524+
m_this.geoTrigger(geo_event.annotation.add, {
525+
annotation: annotation
526+
});
527+
}
528+
}
529+
return m_this;
530+
};
531+
532+
/**
533+
* Add multiple annotations to the layer. The annotations could be in any
534+
* state.
535+
*
536+
* @param {geo.annotation[]} annotations The annotations to add.
537+
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
538+
* gcs, `null` to use the map gcs, or any other transform.
539+
* @param {boolean} [update] If `false`, don't update the layer after adding
540+
* the annotation.
541+
* @returns {this} The current layer.
542+
* @fires geo.event.annotation.add_before
543+
* @fires geo.event.annotation.add
544+
*/
545+
this.addMultipleAnnotations = function (annotations, gcs, update) {
546+
const added = [];
547+
m_this.geoTrigger(geo_event.annotation.add_before, {
548+
annotations: annotations
549+
});
550+
for (let i = 0; i < annotations.length; i += 1) {
551+
const annotation = annotations[i];
552+
if (m_annotationIds[annotation.id()] === undefined) {
553+
this.addAnnotation(annotation, gcs, update, false);
554+
added.push(annotation);
555+
}
556+
}
557+
if (added.length) {
519558
m_this.geoTrigger(geo_event.annotation.add, {
520-
annotation: annotation
559+
annotations: added
521560
});
522561
}
523562
return m_this;

src/event.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -656,20 +656,23 @@ geo_event.camera.viewport = 'geo_camera_viewport';
656656
geo_event.annotation = {};
657657

658658
/**
659-
* Triggered when an annotation has been added.
659+
* Triggered when or more multiple annotations have been added.
660660
*
661661
* @event geo.event.annotation.add
662662
* @type {geo.event.base}
663-
* @property {geo.annotation} annotation The annotation that was added.
663+
* @property {geo.annotation} [annotation] The annotation that was added.
664+
* @property {geo.annotation} [annotations] The annotations that were added.
664665
*/
665666
geo_event.annotation.add = 'geo_annotation_add';
666667

667668
/**
668-
* Triggered when an annotation is about to be added.
669+
* Triggered when one or multiple annotations are about to be added.
669670
*
670671
* @event geo.event.annotation.add_before
671672
* @type {geo.event.base}
672-
* @property {geo.annotation} annotation The annotation that will be added.
673+
* @property {geo.annotation} [annotation] The annotation that will be added.
674+
* @property {geo.annotation[]} [annotations] The annotations that will be
675+
* added.
673676
*/
674677
geo_event.annotation.add_before = 'geo_annotation_add_before';
675678

tests/cases/annotationLayer.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ describe('geo.annotationLayer', function () {
110110
layer.removeAllAnnotations();
111111
expect(layer.annotations().length).toBe(0);
112112
});
113+
it('multipleAnnotations', function () {
114+
var poly = geo.annotation.polygonAnnotation({
115+
state: geo.annotation.state.create, layer: layer}),
116+
rect = geo.annotation.rectangleAnnotation({
117+
layer: layer,
118+
corners: [{x: 0, y: 0}, {x: 1, y: 0}, {x: 1, y: 1}, {x: 0, y: 1}]});
119+
expect(layer.annotations().length).toBe(0);
120+
layer.addMultipleAnnotations([poly, rect]);
121+
expect(layer.annotations().length).toBe(2);
122+
expect(layer.annotations()[0]).toBe(poly);
123+
expect(layer.annotations()[1]).toBe(rect);
124+
});
113125
});
114126
describe('Public utility functions', function () {
115127
var map, layer,

0 commit comments

Comments
 (0)