Skip to content

Commit

Permalink
Refactor event emitter to return subscription object containing the e…
Browse files Browse the repository at this point in the history
…vent name and handler

on subscribe in order unsubscripe only for the registered handler

Refactor draw-tool to unsubscribe from events when osd is destroyed
Refactor overlay to not remove all osd handlers when destroyed
Refactor annotationTooltip to not include any save/create/edit annotation logic
AvailableExternalComments setting now shows tooltip
Add config whether to allow saving edited annotation without shapes
  • Loading branch information
radpet committed Aug 12, 2016
1 parent 5d67ac2 commit 1b18335
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 216 deletions.
78 changes: 24 additions & 54 deletions js/src/annotations/annotationTooltip.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(function($) {

$.AnnotationTooltip = function(options) {

jQuery.extend(this, {
targetElement: null,
annotations: [],
Expand Down Expand Up @@ -82,22 +81,11 @@
jQuery(selector + ' a.cancel').on("click", function(event) {
event.preventDefault();

var returnToPointer = function(){
_this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId);
var cancelCallback = function(){
api.destroy();
if (params.onCancel) { params.onCancel(); }
};

if (_this.activeEditor.isDirty()) {
new $.DialogBuilder().confirm(i18n.t('cancelAnnotation'),function(result){
if(!result){
return;
}
returnToPointer();
});
}else{
returnToPointer();
}
_this.eventEmitter.publish('onAnnotationCreatedCanceled.'+_this.windowId,[cancelCallback,!_this.activeEditor.isDirty()]);

});

Expand All @@ -108,16 +96,10 @@
}
var annotation = _this.activeEditor.createAnnotation();
if (params.onAnnotationCreated) { params.onAnnotationCreated(annotation); }
// return to pointer mode
_this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId);

api.destroy();
//reenable viewer tooltips
_this.eventEmitter.publish('enableTooltips.' + _this.windowId);
_this.activeEditor = null;
_this.activeEditorTip = null;

if (params.onCompleted) { params.onCompleted(); }
});

_this.activeEditor.show(selector);
Expand Down Expand Up @@ -177,14 +159,13 @@
hidden: function(event, api) {
if (params.onTooltipHidden) { params.onTooltipHidden(event, api); }
},
visible: function(event, api) {
visible: function (event, api) {
_this.removeAllEvents(api, params);
_this.addViewerEvents(api, params);
},
move: function(event, api) {
move: function (event, api) {
_this.removeAllEvents(api, params);
_this.addViewerEvents(api, params);
_this.addEditorEvents(api, params);
}
}
});
Expand Down Expand Up @@ -216,10 +197,13 @@
}
var display = jQuery(elem).parents('.annotation-display');
var id = display.attr('data-anno-id');
_this.eventEmitter.publish('annotationDeleted.' + _this.windowId, [id]);
_this.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations');
api.hide();
display.remove();
var callback = function(){
api.hide();
display.remove();
};

_this.eventEmitter.publish('onAnnotationDeleted.' + _this.windowId, [id,callback]);

});

});
Expand All @@ -229,9 +213,15 @@
var display = jQuery(this).parents('.annotation-display');
var id = display.attr('data-anno-id');
var oaAnno = viewerParams.getAnnoFromRegion(id)[0];
_this.freezeQtip(api, oaAnno, viewerParams);
_this.removeAllEvents(api, viewerParams);
_this.addEditorEvents(api, viewerParams);
// Don't show built in editor if external is available
if(!_this.state.getStateProperty('availableExternalCommentsPanel')){
_this.freezeQtip(api, oaAnno, viewerParams);
_this.removeAllEvents(api, viewerParams);
_this.addEditorEvents(api, viewerParams);
}else{
_this.eventEmitter.publish('annotationInEditMode.' + _this.windowId,[oaAnno]);
}

_this.eventEmitter.publish('SET_ANNOTATION_EDITING.' + _this.windowId, {
"annotationId" : id,
"isEditable" : true,
Expand All @@ -257,22 +247,7 @@
var oaAnno = viewerParams.getAnnoFromRegion(id)[0];

_this.activeEditor.updateAnnotation(oaAnno);

jQuery.when(viewerParams.onAnnotationSaved(oaAnno)).then(function(){

_this.unFreezeQtip(api, oaAnno, viewerParams);
_this.eventEmitter.publish('SET_ANNOTATION_EDITING.' + _this.windowId, {
"annotationId" : id,
"isEditable" : false,
"tooltip" : _this
});
_this.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations');
// return to pointer mode
_this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId);

},function(){
// confirmation rejected don't do anything
});
_this.eventEmitter.publish('annotationEditSave.'+_this.windowId,[oaAnno]);

});

Expand All @@ -282,14 +257,9 @@
var id = display.attr('data-anno-id');
var oaAnno = viewerParams.getAnnoFromRegion(id)[0];
_this.unFreezeQtip(api, oaAnno, viewerParams);
_this.eventEmitter.publish('SET_ANNOTATION_EDITING.' + _this.windowId, {
"annotationId" : id,
"isEditable" : false,
"tooltip" : _this
});
_this.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations');
// return to pointer mode
_this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId);

_this.eventEmitter.publish('annotationEditCancel.' + _this.windowId,[id]);

});
},

Expand Down
107 changes: 35 additions & 72 deletions js/src/annotations/osd-region-draw-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
eventEmitter: null
}, options);

this.eventsSubscriptions = [];

this.init();
this.listenForActions();
};
Expand Down Expand Up @@ -52,6 +54,7 @@
},

render: function() {

this.svgOverlay.restoreEditedShapes();
this.svgOverlay.paperScope.activate();
this.svgOverlay.paperScope.project.clear();
Expand Down Expand Up @@ -93,62 +96,7 @@
this.annoTooltip.initializeViewerUpgradableToEditor({
container: windowElement,
viewport: windowElement,
getAnnoFromRegion: _this.getAnnoFromRegion.bind(this),
onAnnotationSaved: function (oaAnno) {
var onAnnotationSaved = jQuery.Deferred();

if (!_this.svgOverlay.draftPaths.length) {
new $.DialogBuilder(windowElement).dialog({
message: i18n.t('editModalSaveAnnotationWithNoShapesMsg'),
buttons: {
success: {
label: i18n.t('editModalBtnSaveWithoutShapes'),
className: 'btn-success',
callback: function () {
oaAnno.on = {
"@type": "oa:SpecificResource",
"full": _this.state.getWindowObjectById(_this.windowId).canvasID
};
//save to endpoint
_this.eventEmitter.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
onAnnotationSaved.resolve();
}
},
danger: {
label: i18n.t('editModalBtnDeleteAnnotation'),
className: 'btn-danger',
callback: function () {
_this.eventEmitter.publish('annotationDeleted.' + _this.windowId, [oaAnno['@id']]);
onAnnotationSaved.resolve();
}
},
main: {
label: i18n.t('cancel'),
className: 'btn-default',
callback: function () {
onAnnotationSaved.reject();
}
}
}

});

} else {
var svg = _this.svgOverlay.getSVGString(_this.svgOverlay.draftPaths);
oaAnno.on = {
"@type": "oa:SpecificResource",
"full": _this.state.getWindowObjectById(_this.windowId).canvasID,
"selector": {
"@type": "oa:SvgSelector",
"value": svg
}
};
//save to endpoint
_this.eventEmitter.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
onAnnotationSaved.resolve();
}
return onAnnotationSaved.promise();
}
getAnnoFromRegion: _this.getAnnoFromRegion.bind(this)
});
this.svgOverlay.paperScope.view.draw();
},
Expand Down Expand Up @@ -184,10 +132,10 @@
}
}
this.svgOverlay.paperScope.view.draw();
if (_this.svgOverlay.availableExternalCommentsPanel) {
_this.eventEmitter.publish('annotationMousePosition.' + _this.windowId, [annotations]);
return;
}
//if (_this.svgOverlay.availableExternalCommentsPanel) {
// _this.eventEmitter.publish('annotationMousePosition.' + _this.windowId, [annotations]);
// return;
//}
_this.annoTooltip.showViewer({
annotations: annotations,
triggerEvent: event,
Expand Down Expand Up @@ -215,37 +163,43 @@
listenForActions: function() {
var _this = this;

_this.eventEmitter.subscribe('refreshOverlay.' + _this.windowId, function(event) {
this._thisDestroy = function(){
_this.destroy();
};

_this.osdViewer.addHandler('close', this._thisDestroy);

this.eventsSubscriptions.push(_this.eventEmitter.subscribe('refreshOverlay.' + _this.windowId, function(event) {
_this.svgOverlay.restoreEditedShapes();
_this.svgOverlay.deselectAll();
_this.svgOverlay.mode = '';
_this.render();
});
}));

_this.eventEmitter.subscribe('updateTooltips.' + _this.windowId, function(event, location, absoluteLocation) {
this.eventsSubscriptions.push(_this.eventEmitter.subscribe('updateTooltips.' + _this.windowId, function(event, location, absoluteLocation) {
if (_this.annoTooltip && !_this.annoTooltip.inEditOrCreateMode) {
_this.showTooltipsFromMousePosition(event, location, absoluteLocation);
}
});
}));

_this.eventEmitter.subscribe('removeTooltips.' + _this.windowId, function() {
this.eventsSubscriptions.push(_this.eventEmitter.subscribe('removeTooltips.' + _this.windowId, function() {
jQuery(_this.osdViewer.element).qtip('destroy', true);
});
}));

_this.eventEmitter.subscribe('disableTooltips.' + _this.windowId, function() {
this.eventsSubscriptions.push(_this.eventEmitter.subscribe('disableTooltips.' + _this.windowId, function() {
if (_this.annoTooltip) {
_this.annoTooltip.inEditOrCreateMode = true;
}
});
}));

_this.eventEmitter.subscribe('enableTooltips.' + _this.windowId, function() {
this.eventsSubscriptions.push(_this.eventEmitter.subscribe('enableTooltips.' + _this.windowId, function() {
if (_this.annoTooltip) {
_this.annoTooltip.inEditOrCreateMode = false;
}
_this.svgOverlay.restoreDraftShapes();
});
}));

_this.eventEmitter.subscribe('SET_ANNOTATION_EDITING.' + _this.windowId, function(event, options) {
this.eventsSubscriptions.push(_this.eventEmitter.subscribe('SET_ANNOTATION_EDITING.' + _this.windowId, function(event, options) {
jQuery.each(_this.annotationsToShapesMap, function(key, paths) {
// if we have a matching annotationId, pass the boolean value on for each path, otherwise, always pass false
if (key === options.annotationId) {
Expand Down Expand Up @@ -275,13 +229,22 @@
}
});
_this.svgOverlay.paperScope.view.draw();
});
}));
},

getAnnoFromRegion: function(regionId) {
return this.list.filter(function(annotation) {
return annotation['@id'] === regionId;
});
},

destroy: function () {
var _this = this;
this.eventsSubscriptions.forEach(function(event){
_this.eventEmitter.unsubscribe(event.name,event.handler);
});
this.osdViewer.removeHandler('close', this._thisDestroy);
}

};
}(Mirador));

0 comments on commit 1b18335

Please sign in to comment.