diff --git a/js/src/annotations/annotationTooltip.js b/js/src/annotations/annotationTooltip.js index eed3d6c588..5e7f8dd22b 100644 --- a/js/src/annotations/annotationTooltip.js +++ b/js/src/annotations/annotationTooltip.js @@ -1,7 +1,6 @@ (function($) { $.AnnotationTooltip = function(options) { - jQuery.extend(this, { targetElement: null, annotations: [], @@ -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()]); }); @@ -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); @@ -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); } } }); @@ -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]); + }); }); @@ -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, @@ -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]); }); @@ -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]); + }); }, diff --git a/js/src/annotations/osd-region-draw-tool.js b/js/src/annotations/osd-region-draw-tool.js index a8c0b3c97a..f50e28ea30 100644 --- a/js/src/annotations/osd-region-draw-tool.js +++ b/js/src/annotations/osd-region-draw-tool.js @@ -9,6 +9,8 @@ eventEmitter: null }, options); + this.eventsSubscriptions = []; + this.init(); this.listenForActions(); }; @@ -52,6 +54,7 @@ }, render: function() { + this.svgOverlay.restoreEditedShapes(); this.svgOverlay.paperScope.activate(); this.svgOverlay.paperScope.project.clear(); @@ -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(); }, @@ -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, @@ -215,37 +163,46 @@ 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.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations'); + // return to pointer mode + _this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId); _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) { @@ -275,13 +232,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)); diff --git a/js/src/annotations/osd-svg-overlay.js b/js/src/annotations/osd-svg-overlay.js index 97c21e8dd9..0ebd3e3d52 100644 --- a/js/src/annotations/osd-svg-overlay.js +++ b/js/src/annotations/osd-svg-overlay.js @@ -66,28 +66,31 @@ this.slotWindowElement = state.getWindowElement(this.windowId); this.state = state; this.eventEmitter = eventEmitter; - var _thisResize = function() { + this.eventsSubscriptions = []; + + this._thisResize = function(){ _this.resize(); }; - this.viewer.addHandler('animation', _thisResize); - this.viewer.addHandler('open', _thisResize); - this.viewer.addHandler('animation-finish', _thisResize); - this.viewer.addHandler('update-viewport', _thisResize); - this.viewer.addHandler('resize', _thisResize); - this.viewer.addHandler('rotate', _thisResize); - this.viewer.addHandler('constrain', _thisResize); - - var _thisDestroy = function(){ + + this.viewer.addHandler('animation', this._thisResize); + this.viewer.addHandler('open', this._thisResize); + this.viewer.addHandler('animation-finish', this._thisResize); + this.viewer.addHandler('update-viewport', this._thisResize); + this.viewer.addHandler('resize',this._thisResize); + this.viewer.addHandler('rotate', this._thisResize); + this.viewer.addHandler('constrain',this._thisResize); + + this._thisDestroy = function(){ _this.destroy(); }; - this.viewer.addHandler('close',_thisDestroy); + this.viewer.addHandler('close',this._thisDestroy); - _this.eventEmitter.subscribe('modeChange.' + _this.windowId,function(event,newMode){ + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('modeChange.' + _this.windowId,function(event,newMode){ _this.currentTool = ''; - }); + })); - _this.eventEmitter.subscribe('toggleDrawingTool.' + _this.windowId, function(event, tool) { + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('toggleDrawingTool.' + _this.windowId, function(event, tool) { //qtip code should NOT be here if (_this.disabled) { jQuery('.qtip' + _this.windowId).qtip('hide'); @@ -100,9 +103,9 @@ _this.currentTool = _this.tools[i]; } } - }); + })); - _this.eventEmitter.subscribe('toggleDefaultDrawingTool.' + _this.windowId, function(event) { + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('toggleDefaultDrawingTool.' + _this.windowId, function(event) { //qtip code should NOT be here if (_this.disabled) { jQuery('.qtip' + _this.windowId).qtip('hide'); @@ -121,22 +124,22 @@ break; } } - }); + })); var _thisHandleDeleteShapeEvent = function(event,shape){ _this.handleDeleteShapeEvent(event,shape); }; - _this.eventEmitter.subscribe('deleteShape.' + _this.windowId, _thisHandleDeleteShapeEvent); + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('deleteShape.' + _this.windowId, _thisHandleDeleteShapeEvent)); - _this.eventEmitter.subscribe('changeBorderColor.' + _this.windowId, function(event, color) { + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('changeBorderColor.' + _this.windowId, function(event, color) { _this.strokeColor = color; if (_this.hoveredPath) { _this.hoveredPath.strokeColor = color; _this.paperScope.view.draw(); } - }); + })); - _this.eventEmitter.subscribe('changeFillColor.' + _this.windowId, function(event, color, alpha) { + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('changeFillColor.' + _this.windowId, function(event, color, alpha) { _this.fillColor = color; _this.fillColorAlpha = alpha; if (_this.hoveredPath && _this.hoveredPath.closed) { @@ -144,9 +147,9 @@ _this.hoveredPath.fillColor.alpha = alpha; _this.paperScope.view.draw(); } - }); + })); - _this.eventEmitter.subscribe('toggleBorderType.' + _this.windowId, function (event, type) { + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('toggleBorderType.' + _this.windowId, function (event, type) { if (type == 'solid') { _this.dashArray = []; } else if (type == 'dashed') { @@ -158,7 +161,161 @@ _this.hoveredPath.dashArray = _this.dashArray; _this.paperScope.view.draw(); } - }); + })); + + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('annotationEditSave.'+_this.windowId,function(event,oaAnno){ + var onAnnotationSaved = jQuery.Deferred(); + var windowElement = _this.state.getWindowElement(_this.windowId); + if (!_this.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.getSVGString(_this.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(); + } + + jQuery.when(onAnnotationSaved.promise()).then(function(){ + _this.eventEmitter.publish('annotationEditSaveSuccessful.'+_this.windowId); + _this.eventEmitter.publish('SET_ANNOTATION_EDITING.' + _this.windowId, { + "annotationId" : oaAnno['@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.eventsSubscriptions.push(_this.eventEmitter.subscribe('annotationEditCancel.'+_this.windowId,function(event,id){ + + _this.eventEmitter.publish('SET_ANNOTATION_EDITING.' + _this.windowId, { + "annotationId" : id, + "isEditable" : false, + "tooltip" : _this.annoTooltip // whats the point of this? maybe when we add confirm for cancel? + }); + _this.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations'); + // return to pointer mode + _this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' +_this.windowId); + + })); + + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('clearDraftPaths.'+_this.windowId,function(){ + _this.clearDraftData(); + })); + + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('onAnnotationCreated.'+_this.windowId,function(event,oaAnno){ + //should remove the styles added for newly created annotation + for(var i=0;i<_this.draftPaths.length;i++){ + if(_this.draftPaths[i].data && _this.draftPaths[i].data.newlyCreated){ + _this.draftPaths[i].strokeWidth /= _this.draftPaths[i].data.newlyCreatedStrokeFactor; + _this.draftPaths[i].data.currentStrokeValue /= _this.draftPaths[i].data.newlyCreatedStrokeFactor; + delete _this.draftPaths[i].data.newlyCreated; + delete _this.draftPaths[i].data.newlyCreatedStrokeFactor; + } + } + + var svg = _this.getSVGString(_this.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('annotationCreated.' + _this.windowId, [oaAnno]); + + // return to pointer mode + _this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId); + + //reenable viewer tooltips + _this.eventEmitter.publish('enableTooltips.' + _this.windowId); + + _this.clearDraftData(); + _this.annoTooltip = null; + _this.annoEditorVisible = false; + })); + + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('onAnnotationCreatedCanceled.'+_this.windowId,function(event,cancelCallback,immediate){ + var cancel = function(){ + _this.eventEmitter.publish('SET_STATE_MACHINE_POINTER.' + _this.windowId); + + _this.clearDraftData(); + _this.annoTooltip = null; + _this.annoEditorVisible = false; + }; + if (!immediate) { + new $.DialogBuilder().confirm(i18n.t('cancelAnnotation'), function (result) { + if (!result) { + return; + } + cancel(); + if (cancelCallback) { + cancelCallback(); + } + }); + }else{ + cancel(); + } + })); + + this.eventsSubscriptions.push(_this.eventEmitter.subscribe('onAnnotationDeleted.' + _this.windowId,function(event,id,callback){ + _this.eventEmitter.publish('annotationDeleted.' + _this.windowId, [id]); + _this.eventEmitter.publish('modeChange.' + _this.windowId, 'displayAnnotations'); + if(callback){ + callback(); + } + })); + + this.resize(); this.show(); @@ -222,18 +379,26 @@ listenForActions: function() { var _this = this; - this.eventEmitter.subscribe('SET_OVERLAY_TOOLTIP.' + this.windowId, function(event, options) { + this.eventsSubscriptions.push(this.eventEmitter.subscribe('SET_OVERLAY_TOOLTIP.' + this.windowId, function(event, options) { _this.annoTooltip = options.tooltip; _this.annoEditorVisible = options.visible; _this.draftPaths = options.paths; - }); + })); - this.eventEmitter.subscribe('CANCEL_ACTIVE_ANNOTATIONS.' + this.windowId, function(event) { + this.eventsSubscriptions.push(this.eventEmitter.subscribe('CANCEL_ACTIVE_ANNOTATIONS.' + this.windowId, function(event) { //for now, don't worry about getting user confirmation, just cancel everything _this.clearDraftData(); _this.annoTooltip = null; _this.annoEditorVisible = false; - }); + })); + + this.eventsSubscriptions.push(this.eventEmitter.subscribe('ANNOTATIONS_LIST_UPDATED',function(event,opts){ + + if(_this.windowId === opts.windowId){ + _this.eventEmitter.publish('refreshOverlay.'+_this.windowId); + } + + })); }, deleteShape:function(shape){ @@ -763,6 +928,7 @@ // Set special style for newly created shapes var newlyCreatedStrokeFactor = this.drawingToolsSettings.newlyCreatedShapeStrokeWidthFactor || 5; + shape.data.newlyCreatedStrokeFactor = newlyCreatedStrokeFactor; shape.data.newlyCreated = true; shape.data.currentStrokeValue *= newlyCreatedStrokeFactor; shape.strokeWidth *= newlyCreatedStrokeFactor; @@ -786,7 +952,7 @@ } if (this.availableExternalCommentsPanel) { - this.eventEmitter.publish('annotationShapeCreated.' + this.windowId, [this, shape]); + this.eventEmitter.publish('annotationShapeCreated.' + this.windowId, [this, shape]);//ne trbqva toq this return; } var _this = this; @@ -797,36 +963,7 @@ return _this.draftPaths.length; }, onAnnotationCreated: function(oaAnno) { - //should remove the styles added for newly created annotation - for(var i=0;i<_this.draftPaths.length;i++){ - if(_this.draftPaths[i].data && _this.draftPaths[i].data.newlyCreated){ - _this.draftPaths[i].strokeWidth /= newlyCreatedStrokeFactor; - _this.draftPaths[i].data.currentStrokeValue /=newlyCreatedStrokeFactor; - delete _this.draftPaths[i].data.newlyCreated; - } - } - - var svg = _this.getSVGString(_this.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('annotationCreated.' + _this.windowId, [oaAnno, shape]); - }, - onCancel: function() { - _this.clearDraftData(); - _this.annoTooltip = null; - _this.annoEditorVisible = false; - }, - onCompleted: function() { - _this.clearDraftData(); - _this.annoTooltip = null; - _this.annoEditorVisible = false; + _this.eventEmitter.publish('onAnnotationCreated.'+_this.windowId,[oaAnno,shape]); } }); _this.annoEditorVisible = true; @@ -862,26 +999,20 @@ // Should unsubscribe from all events // Should have no references in order to be garbage collected destroy:function(){ - this.eventEmitter.unsubscribe('deleteShape.' + this.windowId); - this.eventEmitter.unsubscribe('toggleDrawingTool.' + this.windowId); - this.eventEmitter.unsubscribe('toggleBorderType.' + this.windowId); - this.eventEmitter.unsubscribe('toggleDefaultDrawingTool.' + this.windowId); - this.eventEmitter.unsubscribe('changeBorderColor.' + this.windowId); - this.eventEmitter.unsubscribe('changeFillColor.' + this.windowId); - this.eventEmitter.unsubscribe('changeBorderColor.' + this.windowId); - this.eventEmitter.unsubscribe('SET_OVERLAY_TOOLTIP.' + this.windowId); - this.eventEmitter.unsubscribe('CANCEL_ACTIVE_ANNOTATIONS.' + this.windowId); - this.eventEmitter.unsubscribe('modeChange.' + this.windowId); - this.eventEmitter.unsubscribe('CANCEL_ACTIVE_ANNOTATIONS.' + this.windowId); - - this.viewer.removeAllHandlers('animation'); - this.viewer.removeAllHandlers('open'); - this.viewer.removeAllHandlers('animation-finish'); - this.viewer.removeAllHandlers('update-viewport'); - this.viewer.removeAllHandlers('resize'); - this.viewer.removeAllHandlers('rotate'); - this.viewer.removeAllHandlers('constrain'); - this.viewer.removeAllHandlers('close'); + var _this = this; + + this.eventsSubscriptions.forEach(function(event){ + _this.eventEmitter.unsubscribe(event.name,event.handler); + }); + + this.viewer.removeHandler('animation',this._thisResize); + this.viewer.removeHandler('open',this._thisResize); + this.viewer.removeHandler('animation-finish',this._thisResize); + this.viewer.removeHandler('update-viewport',this._thisResize); + this.viewer.removeHandler('resize',this._thisResize); + this.viewer.removeHandler('rotate',this._thisResize); + this.viewer.removeHandler('constrain',this._thisResize); + this.viewer.removeHandler('close',this._thisDestroy); } }; }(Mirador)); diff --git a/js/src/settings.js b/js/src/settings.js index c8d877bb92..39b857be12 100644 --- a/js/src/settings.js +++ b/js/src/settings.js @@ -135,8 +135,6 @@ }, 'availableExternalCommentsPanel': false, - 'shapeHandleSize':10, - 'availableCanvasTools': [ diff --git a/js/src/utils/eventemitter.js b/js/src/utils/eventemitter.js index f5f098798d..72cc80afad 100644 --- a/js/src/utils/eventemitter.js +++ b/js/src/utils/eventemitter.js @@ -16,11 +16,23 @@ ['subscribe', 'unsubscribe', 'publish'].forEach(function(action) { $.EventEmitter.prototype[action] = function() { var args = Array.prototype.slice.call(arguments); + var event; + if(action === 'subscribe'){ + event = { + name:args[0], + handler:args[1] + }; + } + args[0] = this.emitterId.toString() +"::"+ args[0]; if (this.debug) { console.trace("EventEmitter:"+action+":", args); } jQuery[action].apply(jQuery, args); + + if(event){ + return event; + } }; }); })(Mirador); \ No newline at end of file diff --git a/js/src/widgets/imageView.js b/js/src/widgets/imageView.js index 1304360b0b..519371fa63 100755 --- a/js/src/widgets/imageView.js +++ b/js/src/widgets/imageView.js @@ -262,7 +262,7 @@ this.element.find('.mirador-osd-refresh-mode').on('click', function() { //update annotation list from endpoint _this.eventEmitter.publish('updateAnnotationList.'+_this.windowId); - _this.eventEmitter.publish('refreshOverlay.'+_this.windowId, ''); + // _this.eventEmitter.publish('refreshOverlay.'+_this.windowId, ''); }); //Annotation specific controls diff --git a/karma.conf.js b/karma.conf.js index 69e9f38722..bc6d6039ea 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -43,6 +43,7 @@ module.exports = function(config) { 'js/src/widgets/*.js', 'js/src/utils/*.js', // spec + 'spec/**/*.stub.js', 'spec/**/*.js', {pattern: 'spec/data/*', watched: true, served: true, included: false}, {pattern: 'spec/fixtures/*json', watched: true, served: true, included: false}, diff --git a/spec/annotations/event-emitter.stub.js b/spec/annotations/event-emitter.stub.js index bb0945de11..8bb801cf12 100644 --- a/spec/annotations/event-emitter.stub.js +++ b/spec/annotations/event-emitter.stub.js @@ -6,12 +6,12 @@ window.MockEventEmitter.prototype = { subscribe: function (eventName, callback) { - this.eventEmmiter.subscribe(eventName,callback); if(this.events[eventName]){ this.events[eventName]++; }else{ this.events[eventName] = 1; } + return this.eventEmmiter.subscribe(eventName,callback); }, publish: function (eventName,args) { this.eventEmmiter.publish(eventName,args); diff --git a/spec/annotations/osd-region-draw-tool.test.js b/spec/annotations/osd-region-draw-tool.test.js index 18297f9db3..84e2b05c5e 100644 --- a/spec/annotations/osd-region-draw-tool.test.js +++ b/spec/annotations/osd-region-draw-tool.test.js @@ -1,7 +1,19 @@ describe('OsdRegionDrawTool', function() { + var config = { + osdViewer:{ + svgOverlay:function(){ + return MockOverlay.getOverlay(); + }, + addHandler:jasmine.createSpy(), + removeHandler:jasmine.createSpy() + }, + eventEmitter: new MockEventEmitter(new Mirador.EventEmitter()) + }; + beforeEach(function() { + this.drawTool = new Mirador.OsdRegionDrawTool(config); }); afterEach(function() { @@ -45,4 +57,13 @@ describe('OsdRegionDrawTool', function() { xdescribe('getAnnoFromRegion', function() { }); -}); + + it('should unsubscibe from all events when destroying',function(){ + this.drawTool.destroy(); + for(var key in this.drawTool.eventEmitter.events){ + expect(this.drawTool.eventEmitter.events[key]).toBe(0); + } + expect(this.drawTool.osdViewer.addHandler.callCount).toBe(this.drawTool.osdViewer.removeHandler.callCount); + }); + +}); \ No newline at end of file diff --git a/spec/annotations/osd-svg-overlay.test.js b/spec/annotations/osd-svg-overlay.test.js index 9b1e575b37..6b8131992d 100644 --- a/spec/annotations/osd-svg-overlay.test.js +++ b/spec/annotations/osd-svg-overlay.test.js @@ -66,7 +66,7 @@ describe('Overlay', function() { }, 'addHandler': function(eventName, functionBody) { }, - 'removeAllHandlers':function(eventName){ + 'removeHandler':function(eventName,functionBody){ } }; var drawingToolsSettings = { @@ -521,10 +521,10 @@ describe('Overlay', function() { }); it('should unsubscibe from all events when destroying',function(){ - this.overlay.destroy(); - for(var key in this.overlay.eventEmitter.events){ - expect(this.overlay.eventEmitter.events[key]).toBe(0); - } + this.overlay.destroy(); + for(var key in this.overlay.eventEmitter.events){ + expect(this.overlay.eventEmitter.events[key]).toBe(0); + } }); diff --git a/spec/annotations/overlay.stub.js b/spec/annotations/overlay.stub.js index 5c91039970..1deadc815f 100644 --- a/spec/annotations/overlay.stub.js +++ b/spec/annotations/overlay.stub.js @@ -29,7 +29,11 @@ onDrawFinish: jasmine.createSpy(), getName: function (tool) { return tool.idPrefix + '1'; - } + }, + show: jasmine.createSpy(), + hide: jasmine.createSpy(), + disable: jasmine.createSpy(), + destroy: jasmine.createSpy() } } }