Skip to content

Commit

Permalink
Merge pull request ProjectMirador#921 from arthurian/refactor-pubsub
Browse files Browse the repository at this point in the history
Refactor pub/sub to localize events
  • Loading branch information
rsinghal committed May 12, 2016
2 parents d9f75e8 + 6d07956 commit f401c6f
Show file tree
Hide file tree
Showing 40 changed files with 662 additions and 420 deletions.
20 changes: 12 additions & 8 deletions js/src/annotations/annotationTooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
targetElement: null,
annotations: [],
windowId: "",
eventEmitter: null
}, options);

this.init();
Expand Down Expand Up @@ -68,8 +69,8 @@
},
events: {
render: function(event, api) {
jQuery.publish('annotationEditorAvailable.' + _this.windowId);
jQuery.publish('disableTooltips.' + _this.windowId);
_this.eventEmitter.publish('annotationEditorAvailable.' + _this.windowId);
_this.eventEmitter.publish('disableTooltips.' + _this.windowId);

jQuery(selector).parent().parent().draggable();

Expand Down Expand Up @@ -97,7 +98,7 @@

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

Expand Down Expand Up @@ -197,7 +198,7 @@
}
var display = jQuery(this).parents('.annotation-display');
var id = display.attr('data-anno-id');
jQuery.publish('annotationDeleted.' + _this.windowId, [id]);
_this.eventEmitter.publish('annotationDeleted.' + _this.windowId, [id]);
api.hide();
display.remove();
});
Expand Down Expand Up @@ -291,10 +292,11 @@
},

setTooltipContent: function(annotations) {
var _this = this;
var api = jQuery(this.targetElement).qtip('api');
if (api) {
api.set({'content.text': this.getViewerContent(annotations)});
jQuery.publish('tooltipViewerSet.' + this.windowId);
_this.eventEmitter.publish('tooltipViewerSet.' + this.windowId);
}
},

Expand Down Expand Up @@ -350,9 +352,10 @@
},

freezeQtip: function(api, oaAnno, viewerParams) {
var _this = this;
if (this.inEditOrCreateMode) { throw 'AnnotationTooltip already in edit mode'; }
this.inEditOrCreateMode = true;
jQuery.publish('disableRectTool.' + this.windowId);
_this.eventEmitter.publish('disableRectTool.' + this.windowId);
var editorContainer = this.editorTemplate({
id: jQuery.isEmptyObject(oaAnno) ? "" : oaAnno['@id'],
windowId: this.windowId
Expand All @@ -361,7 +364,7 @@
'content.text': editorContainer,
'hide.event': false
});
jQuery.publish('annotationEditorAvailable.' + this.windowId);
_this.eventEmitter.publish('annotationEditorAvailable.' + this.windowId);
//add rich text editor
this.activeEditor = new this.editor(
jQuery.extend({}, this.editorOptions, {
Expand All @@ -376,9 +379,10 @@
},

unFreezeQtip: function(api, oaAnno, viewerParams) {
var _this = this;
if (!this.inEditOrCreateMode) { throw 'AnnotationTooltip not in edit mode'; }
this.inEditOrCreateMode = false;
jQuery.publish('enableRectTool.' + this.windowId);
_this.eventEmitter.publish('enableRectTool.' + this.windowId);
api.set({
'content.text': this.getViewerContent([oaAnno]),
'hide.event': 'mouseleave'
Expand Down
11 changes: 6 additions & 5 deletions js/src/annotations/catchEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
username: "mirador-test",
annotationsList: [], //OA list for Mirador use
annotationsListCatch: null, //internal list for module use
windowID: null
windowID: null,
eventEmitter: null
}, options);

this.init();
Expand Down Expand Up @@ -95,7 +96,7 @@
_this.annotationsList.push(_this.getAnnotationInOA(value));
});
_this.dfd.resolve(true);
jQuery.publish('catchAnnotationsLoaded.'+_this.windowID, _this.annotationsListCatch);
_this.eventEmitter.publish('catchAnnotationsLoaded.'+_this.windowID, _this.annotationsListCatch);
}
},
error: function() {
Expand Down Expand Up @@ -123,7 +124,7 @@
if (typeof successCallback === "function") {
successCallback();
}
jQuery.publish('catchAnnotationDeleted.'+_this.windowID, annotationID);
_this.eventEmitter.publish('catchAnnotationDeleted.'+_this.windowID, annotationID);
},
error: function() {
if (typeof errorCallback === "function") {
Expand Down Expand Up @@ -152,7 +153,7 @@
if (typeof successCallback === "function") {
successCallback();
}
jQuery.publish('catchAnnotationUpdated.'+_this.windowID, annotation);
_this.eventEmitter.publish('catchAnnotationUpdated.'+_this.windowID, annotation);
},
error: function() {
if (typeof errorCallback === "function") {
Expand Down Expand Up @@ -185,7 +186,7 @@
if (typeof successCallback === "function") {
successCallback(_this.getAnnotationInOA(data));
}
jQuery.publish('catchAnnotationCreated.'+_this.windowID, data);
_this.eventEmitter.publish('catchAnnotationCreated.'+_this.windowID, data);
},
error: function() {
if (typeof errorCallback === "function") {
Expand Down
3 changes: 2 additions & 1 deletion js/src/annotations/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
jQuery.extend(this, {
dfd: null,
annotationsList: [], //OA list for Mirador use
windowID: null
windowID: null,
eventEmitter: null
}, options);

this.init();
Expand Down
3 changes: 2 additions & 1 deletion js/src/annotations/localStorageEndpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
prefix: null,
dfd: null,
annotationsList: [],
windowID: null
windowID: null,
eventEmitter: null
}, options);

this.init();
Expand Down
30 changes: 16 additions & 14 deletions js/src/annotations/osd-region-draw-tool.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
parent: null,
osd: null,
list: null,
annotationsToShapesMap: {}
annotationsToShapesMap: {},
eventEmitter: null
}, options);

this.init();
Expand All @@ -15,7 +16,7 @@
$.OsdRegionDrawTool.prototype = {

init: function() {
this.svgOverlay = this.osdViewer.svgOverlay(this.osdViewer.id, this.windowId, this.state);
this.svgOverlay = this.osdViewer.svgOverlay(this.osdViewer.id, this.windowId, this.state, this.eventEmitter);
this.svgOverlay.show();
this.svgOverlay.disable();
},
Expand Down Expand Up @@ -50,7 +51,7 @@
if (!window.confirm("Do you want to delete this shape and annotation?")) {
return false;
}
jQuery.publish('annotationDeleted.' + _this.windowId, [oaAnno['@id']]);
_this.eventEmitter.publish('annotationDeleted.' + _this.windowId, [oaAnno['@id']]);
this.svgOverlay.removeFocus();
return true;
} else {
Expand All @@ -67,7 +68,7 @@
};
}
oaAnno.on.selector.value = _this.svgOverlay.getSVGString(shapeArray);
jQuery.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
_this.eventEmitter.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
this.svgOverlay.removeFocus();
return true;
}
Expand Down Expand Up @@ -110,7 +111,7 @@
}
}
for (var j = 0; j < oaAnnos.length; j++) {
jQuery.publish('annotationUpdated.' + _this.windowId, [oaAnnos[j]]);
_this.eventEmitter.publish('annotationUpdated.' + _this.windowId, [oaAnnos[j]]);
}
this.svgOverlay.restoreEditedShapes();
},
Expand Down Expand Up @@ -142,13 +143,14 @@
return deferred;
});
jQuery.when.apply(jQuery, deferreds).done(function() {
jQuery.publish('overlaysRendered.' + _this.windowId);
_this.eventEmitter.publish('overlaysRendered.' + _this.windowId);
});

var windowElement = _this.state.getWindowElement(_this.windowId);
this.annoTooltip = new $.AnnotationTooltip({
targetElement: jQuery(this.osdViewer.element),
state: _this.state,
eventEmitter: _this.eventEmitter,
windowId: _this.parent.windowId
});
this.annoTooltip.initializeViewerUpgradableToEditor({
Expand All @@ -157,7 +159,7 @@
getAnnoFromRegion: _this.getAnnoFromRegion.bind(this),
onAnnotationSaved: function(oaAnno) {
//save to endpoint
jQuery.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
_this.eventEmitter.publish('annotationUpdated.' + _this.windowId, [oaAnno]);
}
});
this.svgOverlay.paperScope.view.draw();
Expand Down Expand Up @@ -209,36 +211,36 @@
bindEvents: function() {
var _this = this;

jQuery.subscribe('refreshOverlay.' + _this.windowId, function(event) {
_this.eventEmitter.subscribe('refreshOverlay.' + _this.windowId, function(event) {
_this.svgOverlay.restoreEditedShapes();
_this.svgOverlay.deselectAll();
_this.svgOverlay.mode = '';
_this.render();
});
jQuery.subscribe('deleteShape.' + _this.windowId, function(event) {
_this.eventEmitter.subscribe('deleteShape.' + _this.windowId, function(event) {
_this.deleteShape();
});
jQuery.subscribe('updateEditedShape.' + _this.windowId, function(event) {
_this.eventEmitter.subscribe('updateEditedShape.' + _this.windowId, function(event) {
_this.saveEditedShape();
});

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

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

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

jQuery.subscribe('enableTooltips.' + _this.windowId, function() {
_this.eventEmitter.subscribe('enableTooltips.' + _this.windowId, function() {
if (_this.annoTooltip) {
_this.annoTooltip.inEditOrCreateMode = false;
}
Expand Down
Loading

0 comments on commit f401c6f

Please sign in to comment.