Skip to content

Commit

Permalink
fix: check if eventListeners is defined before iterating it (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur-miller authored Feb 27, 2020
1 parent 6d829fc commit 39448d6
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions src/CornerstoneViewport/CornerstoneViewport.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,13 +146,13 @@ class CornerstoneViewport extends Component {
// ~~ EVENTS: CORNERSTONE
this._handleOnElementEnabledEvent();
this._bindInternalCornerstoneEventListeners();
this._bindExternalEventListeners("cornerstone");
this._bindExternalEventListeners('cornerstone');

cornerstone.enable(this.element, cornerstoneOptions);

// ~~ EVENTS: ELEMENT
this._bindInternalElementEventListeners();
this._bindExternalEventListeners("element");
this._bindExternalEventListeners('element');

if (resizeThrottleMs) {
windowResizeHandler.enable(this.element, resizeThrottleMs);
Expand Down Expand Up @@ -281,7 +281,7 @@ class CornerstoneViewport extends Component {

// ~~ OVERLAY
if (isOverlayVisible !== prevIsOverlayVisible)
updatedState.isOverlayVisible = isOverlayVisible
updatedState.isOverlayVisible = isOverlayVisible;

// ~~ STATE: Update aggregated state changes
if (Object.keys(updatedState).length > 0) {
Expand All @@ -303,8 +303,8 @@ class CornerstoneViewport extends Component {

this._bindInternalCornerstoneEventListeners(clear);
this._bindInternalElementEventListeners(clear);
this._bindExternalEventListeners("cornerstone", clear);
this._bindExternalEventListeners("element", clear);
this._bindExternalEventListeners('cornerstone', clear);
this._bindExternalEventListeners('element', clear);

this._setupLoadHandlers(clear);
if (this.props.isStackPrefetchEnabled) {
Expand Down Expand Up @@ -479,19 +479,22 @@ class CornerstoneViewport extends Component {
* @param {boolean} [clear=false] - True to clear event listeners
* @returns {undefined}
*/
_bindExternalEventListeners(targetType, clear=false) {
_bindExternalEventListeners(targetType, clear = false) {
const addOrRemoveEventListener = clear
? 'removeEventListener'
: 'addEventListener';
? 'removeEventListener'
: 'addEventListener';

const cornerstoneEvents = Object.values(cornerstone.EVENTS);
const cornerstoneToolsEvents = Object.values(cornerstoneTools.EVENTS);
const events = cornerstoneEvents.concat(cornerstoneToolsEvents);
const targetElementOrCornerstone =
targetType === 'element' ? this.element : cornerstone.events;
targetType === 'element' ? this.element : cornerstone.events;
const boundMethod = this._handleExternalEventListeners.bind(this);
for (let i = 0; i < events.length; i++) {
targetElementOrCornerstone[addOrRemoveEventListener](events[i], boundMethod);
targetElementOrCornerstone[addOrRemoveEventListener](
events[i],
boundMethod
);
}
}

Expand All @@ -501,11 +504,17 @@ class CornerstoneViewport extends Component {
* @returns {undefined}
*/
_validateExternalEventsListeners() {
if (!this.props.eventListeners) return;

const cornerstoneEvents = Object.values(cornerstone.EVENTS);
const cornerstoneToolsEvents = Object.values(cornerstoneTools.EVENTS);

for (let i = 0; i < this.props.eventListeners.length; i++) {
const { target: targetType, eventName, handler } = this.props.eventListeners[i];
const {
target: targetType,
eventName,
handler,
} = this.props.eventListeners[i];
if (
!cornerstoneEvents.includes(eventName) &&
!cornerstoneToolsEvents.includes(eventName)
Expand All @@ -524,7 +533,7 @@ class CornerstoneViewport extends Component {
* @param {event}
* @returns {undefined}
*/
_handleExternalEventListeners(event){
_handleExternalEventListeners(event) {
if (!this.props.eventListeners) {
return;
}
Expand Down

0 comments on commit 39448d6

Please sign in to comment.