Commit 54dea463 authored by Robert Knight's avatar Robert Knight

Use annotation event constants in annotation mapper

Use event constants consistently for events that are emitted
via $rootScope.
parent cadfe3fd
'use strict';
var angular = require('angular');
var events = require('./events');
// Fetch the container object for the passed annotation from the threading
// service, but only return it if it has an associated message.
......@@ -28,14 +31,14 @@ function annotationMapper($rootScope, threading, store) {
var container = getContainer(threading, annotation);
if (container !== null) {
angular.copy(annotation, container.message);
$rootScope.$emit('annotationUpdated', container.message);
$rootScope.$emit(events.ANNOTATION_UPDATED, container.message);
return;
}
loaded.push(new store.AnnotationResource(annotation));
});
$rootScope.$emit('annotationsLoaded', loaded);
$rootScope.$emit(events.ANNOTATIONS_LOADED, loaded);
}
function unloadAnnotations(annotations) {
......@@ -45,13 +48,13 @@ function annotationMapper($rootScope, threading, store) {
annotation = angular.copy(annotation, container.message);
}
$rootScope.$emit('annotationDeleted', annotation);
$rootScope.$emit(events.ANNOTATION_DELETED, annotation);
});
}
function createAnnotation(annotation) {
annotation = new store.AnnotationResource(annotation);
$rootScope.$emit('beforeAnnotationCreated', annotation);
$rootScope.$emit(events.BEFORE_ANNOTATION_CREATED, annotation);
return annotation;
}
......@@ -59,7 +62,7 @@ function annotationMapper($rootScope, threading, store) {
return annotation.$delete({
id: annotation.id
}).then(function () {
$rootScope.$emit('annotationDeleted', annotation);
$rootScope.$emit(events.ANNOTATION_DELETED, annotation);
return annotation;
});
}
......
'use strict';
var events = require('./events');
/** Watch the UI state and update scope properties. */
// @ngInject
function AnnotationUIController($rootScope, $scope, annotationUI) {
......@@ -24,7 +26,7 @@ function AnnotationUIController($rootScope, $scope, annotationUI) {
$scope.focusedAnnotations = map;
});
$rootScope.$on('annotationDeleted', function (event, annotation) {
$rootScope.$on(events.ANNOTATION_DELETED, function (event, annotation) {
annotationUI.removeSelectedAnnotation(annotation);
});
}
......
......@@ -35,6 +35,9 @@ module.exports = {
/** An annotation was either deleted or unloaded. */
ANNOTATION_DELETED: 'annotationDeleted',
/** An annotation has been updated. */
ANNOTATION_UPDATED: 'annotationUpdated',
/** A set of annotations were loaded from the server. */
ANNOTATIONS_LOADED: 'annotationsLoaded',
};
'use strict';
var angular = require('angular');
var events = require('../events');
describe('annotationMapper', function() {
var sandbox = sinon.sandbox.create();
var $rootScope;
......@@ -40,16 +44,18 @@ describe('annotationMapper', function() {
var annotations = [{id: 1}, {id: 2}, {id: 3}];
annotationMapper.loadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationsLoaded', [{}, {}, {}]);
assert.calledWith($rootScope.$emit, events.ANNOTATIONS_LOADED,
[{}, {}, {}]);
});
it('also includes replies in the annotationLoaded event', function () {
sandbox.stub($rootScope, '$emit');
var annotations = [{id: 1}]
var annotations = [{id: 1}];
var replies = [{id: 2}, {id: 3}];
annotationMapper.loadAnnotations(annotations, replies);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationsLoaded', [{}, {}, {}]);
assert.calledWith($rootScope.$emit, events.ANNOTATIONS_LOADED,
[{}, {}, {}]);
});
it('triggers the annotationUpdated event for each annotation in the threading cache', function () {
......@@ -60,7 +66,8 @@ describe('annotationMapper', function() {
annotationMapper.loadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationUpdated', cached.message);
assert.calledWith($rootScope.$emit, events.ANNOTATION_UPDATED,
cached.message);
});
it('also triggers annotationUpdated for cached replies', function () {
......@@ -71,7 +78,8 @@ describe('annotationMapper', function() {
fakeThreading.idTable[3] = cached;
annotationMapper.loadAnnotations(annotations, replies);
assert($rootScope.$emit.calledWith("annotationUpdated", {id: 3}))
assert($rootScope.$emit.calledWith(events.ANNOTATION_UPDATED,
{id: 3}));
});
it('replaces the properties on the cached annotation with those from the loaded one', function () {
......@@ -82,7 +90,7 @@ describe('annotationMapper', function() {
annotationMapper.loadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationUpdated', {
assert.calledWith($rootScope.$emit, events.ANNOTATION_UPDATED, {
id: 1,
url: 'http://example.com'
});
......@@ -96,7 +104,7 @@ describe('annotationMapper', function() {
annotationMapper.loadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationsLoaded', []);
assert.calledWith($rootScope.$emit, events.ANNOTATIONS_LOADED, []);
});
});
......@@ -105,10 +113,9 @@ describe('annotationMapper', function() {
sandbox.stub($rootScope, '$emit');
var annotations = [{id: 1}, {id: 2}, {id: 3}];
annotationMapper.unloadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationDeleted', annotations[0]);
assert.calledWith($rootScope.$emit, 'annotationDeleted', annotations[1]);
assert.calledWith($rootScope.$emit, 'annotationDeleted', annotations[2]);
annotations.forEach(function (annot) {
assert.calledWith($rootScope.$emit, events.ANNOTATION_DELETED, annot);
});
});
it('replaces the properties on the cached annotation with those from the deleted one', function () {
......@@ -118,8 +125,7 @@ describe('annotationMapper', function() {
fakeThreading.idTable[1] = cached;
annotationMapper.unloadAnnotations(annotations);
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationDeleted', {
assert.calledWith($rootScope.$emit, events.ANNOTATION_DELETED, {
id: 1,
url: 'http://example.com'
});
......@@ -146,7 +152,8 @@ describe('annotationMapper', function() {
var ann = {};
fakeStore.AnnotationResource.returns(ann);
annotationMapper.createAnnotation();
assert.calledWith($rootScope.$emit, 'beforeAnnotationCreated', ann);
assert.calledWith($rootScope.$emit,
events.BEFORE_ANNOTATION_CREATED, ann);
});
});
......@@ -163,8 +170,8 @@ describe('annotationMapper', function() {
var p = Promise.resolve();
var ann = {$delete: sandbox.stub().returns(p)};
annotationMapper.deleteAnnotation(ann).then(function () {
assert.called($rootScope.$emit);
assert.calledWith($rootScope.$emit, 'annotationDeleted', ann);
assert.calledWith($rootScope.$emit,
events.ANNOTATION_DELETED, ann);
}).then(done, done);
$rootScope.$apply();
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment