Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve annotation ids when reloading geojson. #747

Merged
merged 3 commits into from
Nov 16, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,22 @@ var annotation = function (type, args) {
return new annotation(type, args);
}

annotationId += 1;
var m_options = $.extend({}, {showLabel: true}, args || {}),
m_id = annotationId,
m_name = m_options.name || (
type.charAt(0).toUpperCase() + type.substr(1) + ' ' + annotationId),
m_id = m_options.annotationId;
delete m_options.annotationId;
if (m_id === undefined || (m_options.layer && m_options.layer.annotationById(m_id))) {
annotationId += 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should emit a warning here if the "requested" id is ignored. Even better, perhaps if we are wanting to preserve id's across contexts, maybe we should just make them uuid's. I think the value is fairly opaque with the exception of the default title.

Also, the id attribute isn't documented. Maybe it's better to just keep it as a private interface due to the id conflict issues. Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default title was my main motivation of using low integers. We could use uuids for the ids, change the accessor function from id to _id to indicate that it is private, and use a different counter for generating mostly distinct names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with leaving the switch to uuid until later. I think it would be good to issue a warning here to avoid confusion given how Emory is using these id's.

if (m_id !== undefined) {
console.warn('Annotation id ' + m_id + ' is in use; using ' + annotationId + ' instead.');
}
m_id = annotationId;
} else {
if (m_id > annotationId) {
annotationId = m_id;
}
}
var m_name = m_options.name || (
type.charAt(0).toUpperCase() + type.substr(1) + ' ' + m_id),
m_label = m_options.label || null,
m_description = m_options.description || undefined,
m_type = type,
Expand Down Expand Up @@ -273,7 +284,7 @@ var annotation = function (type, args) {
}
if (arg2 === undefined) {
m_options = $.extend(true, m_options, arg1);
/* For style objects, reextend them without recursiion. This allows
/* For style objects, re-extend them without recursion. This allows
* setting colors without an opacity field, for instance. */
['style', 'editStyle', 'labelStyle'].forEach(function (key) {
if (arg1[key] !== undefined) {
Expand Down
14 changes: 8 additions & 6 deletions src/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,16 +420,16 @@ var annotationLayer = function (args) {
* the given geojson object. If `undefined`, return the current
* annotations as geojson. This may be a JSON string, a javascript
* object, or a File object.
* @param {boolean} [clear] If `true`, when adding annotations, first remove
* all existing objects. If `'update'`, update existing annotations and
* remove annotations that no longer exit, If falsy, update existing
* annotations and leave unchanged annotations.
* @param {boolean|string} [clear] If `true`, when adding annotations, first
* remove all existing objects. If `'update'`, update existing
* annotations and remove annotations that no longer exist. If falsy,
* update existing annotations and leave annotations that have not chaged.
* @param {string|geo.transform|null} [gcs] `undefined` to use the interface
* gcs, `null` to use the map gcs, or any other transform.
* @param {boolean} [includeCrs] If truthy, include the coordinate system in
* the output.
* @returns {object|number|undefined} If `geojson` was undefined, the current
* annotations as a javascript object that can be converted to geojson
* annotations is a javascript object that can be converted to geojson
* using JSON.stringify. If `geojson` is specified, either the number of
* annotations now present upon success, or `undefined` if the value in
* `geojson` was not able to be parsed.
Expand Down Expand Up @@ -566,7 +566,9 @@ var annotationLayer = function (args) {
});
if (options.annotationId !== undefined) {
existing = m_this.annotationById(options.annotationId);
delete options.annotationId;
if (existing) {
delete options.annotationId;
}
}
if (existing && existing.type() === type && existing.state() === geo_annotation.state.done && existing.options('updated') === false) {
/* We could change the state of the existing annotation if it differs
Expand Down
15 changes: 13 additions & 2 deletions tests/cases/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,28 @@ describe('geo.annotation', function () {
layer = map.createLayer('annotation', {
annotations: geo.listAnnotations()
});
ann = geo.annotation.annotation('test2', {
var params = {
layer: layer,
name: 'Annotation',
state: geo.annotation.state.create
});
};
ann = geo.annotation.annotation('test2', params);
expect(ann.type()).toBe('test2');
expect(ann.state()).toBe(geo.annotation.state.create);
expect(ann.id()).toBeGreaterThan(0);
expect(ann.name()).toBe('Annotation');
expect(ann.layer()).toBe(layer);
expect(ann.coordinates()).toEqual([]);

// check that reusing an annotationId throws a warning
sinon.stub(console, 'warn', function () {});
params.annotationId = 10;
ann = geo.annotation.annotation('test2', params);
layer.addAnnotation(ann);
expect(console.warn.calledOnce).toBe(false);
geo.annotation.annotation('test2', params);
expect(console.warn.calledOnce).toBe(true);
console.warn.restore();
});
it('_exit', function () {
var ann = geo.annotation.annotation('test');
Expand Down
4 changes: 4 additions & 0 deletions tests/cases/annotationLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,10 @@ describe('geo.annotationLayer', function () {
expect(layer.geojson(sampleGeojson)).toBe(5);
expect(layer.geojson(sampleGeojson, 'update')).toBe(2);
expect(layer.geojson(sampleGeojson, true)).toBe(2);
expect(layer.annotations()[1].id()).not.toBe(1000);
sampleGeojson.properties = {annotationId: 1000};
expect(layer.geojson(sampleGeojson, true)).toBe(2);
expect(layer.annotations()[1].id()).toBe(1000);
});
it('validateAttribute', function () {
expect(layer.validateAttribute(undefined, 'other')).toBe(undefined);
Expand Down