Commit 34caa11b authored by Nick Stenning's avatar Nick Stenning

Translate the annotationMapper service to JavaScript

In preparation for making some substantial changes to this piece of
code, I'm translating it to JS.
parent 129ddace
# Wraps the annotation store to trigger events for the CRUD actions
module.exports = [
'$rootScope', 'threading', 'store',
($rootScope, threading, store) ->
loadAnnotations: (annotations) ->
annotations = for annotation in annotations
container = threading.idTable[annotation.id]
if container?.message
angular.copy(annotation, container.message)
$rootScope.$emit('annotationUpdated', container.message)
continue
else
annotation
annotations = (new store.AnnotationResource(a) for a in annotations)
$rootScope.$emit('annotationsLoaded', annotations)
unloadAnnotations: (annotations) ->
for annotation in annotations
container = threading.idTable[annotation.id]
if container?.message
if annotation isnt container.message
annotation = angular.copy(annotation, container.message)
$rootScope.$emit('annotationDeleted', annotation)
createAnnotation: (annotation) ->
annotation = new store.AnnotationResource(annotation)
$rootScope.$emit('beforeAnnotationCreated', annotation)
annotation
deleteAnnotation: (annotation) ->
annotation.$delete(id: annotation.id).then ->
$rootScope.$emit('annotationDeleted', annotation)
annotation
]
'use strict';
// Fetch the container object for the passed annotation from the threading
// service, but only return it if it has an associated message.
function getContainer(threading, annotation) {
var container = threading.idTable[annotation.id];
if (container === null || typeof container === 'undefined') {
return null;
}
// Also return null if the container has no message
if (!container.message) {
return null;
}
return container;
}
// Wraps the annotation store to trigger events for the CRUD actions
// @ngInject
function annotationMapper($rootScope, threading, store) {
function loadAnnotations(annotations) {
var loaded = [];
annotations.forEach(function (annotation) {
var container = getContainer(threading, annotation);
if (container !== null) {
angular.copy(annotation, container.message);
$rootScope.$emit('annotationUpdated', container.message);
return;
}
loaded.push(new store.AnnotationResource(annotation));
});
$rootScope.$emit('annotationsLoaded', loaded);
}
function unloadAnnotations(annotations) {
annotations.forEach(function (annotation) {
var container = getContainer(threading, annotation);
if (container !== null && annotation !== container.message) {
annotation = angular.copy(annotation, container.message);
}
$rootScope.$emit('annotationDeleted', annotation);
});
}
function createAnnotation(annotation) {
annotation = new store.AnnotationResource(annotation);
$rootScope.$emit('beforeAnnotationCreated', annotation);
return annotation;
}
function deleteAnnotation(annotation) {
return annotation.$delete({
id: annotation.id
}).then(function () {
$rootScope.$emit('annotationDeleted', annotation);
return annotation;
});
}
return {
loadAnnotations: loadAnnotations,
unloadAnnotations: unloadAnnotations,
createAnnotation: createAnnotation,
deleteAnnotation: deleteAnnotation
};
}
module.exports = annotationMapper;
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