Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Add Annotations bundling changes back #95

Merged
merged 6 commits into from
May 9, 2017
Merged

Fix: Add Annotations bundling changes back #95

merged 6 commits into from
May 9, 2017

Conversation

pramodsum
Copy link
Contributor

No description provided.

@@ -255,6 +268,12 @@ class BaseViewer extends EventEmitter {
this.addListener('togglepointannotationmode', () => {
this.annotator.togglePointModeHandler();
});

this.addListener('load', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this in "destroy"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need to specifically remove this when you have removeAllListeners() without a param.

fileId,
token,
canAnnotate: this.canAnnotate
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think breaking this out into a function is super valuable.

@@ -65,8 +65,29 @@ class Annotator extends EventEmitter {
* @return {void}
*/
init() {
this.annotatedElement = this.getAnnotatedEl(this.container);
this.annotationService = this.initAnnotationService(this.options);
this.notification = new Notification(this.annotatedElement);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { apiHost, fileId, token } = this.options;

this.annotatedEl = this.getAnnotatedEl(this.container);
this.notification = new Notification(this.annotatedEl);
this.annotationService = new AnnotationService({
    apiHost,
    fileId,
    token,
    canAnnotation: this.canAnnotate
});

@@ -83,6 +96,8 @@ class BaseViewer extends EventEmitter {
fullscreen.removeAllListeners();
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler);
this.removeAllListeners();
this.removeAllListeners('togglepointannotationmode');
this.removeAllListeners('load');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't need these two - removeAllListeners() without an argument removes all listeners.

@@ -255,6 +268,12 @@ class BaseViewer extends EventEmitter {
this.addListener('togglepointannotationmode', () => {
this.annotator.togglePointModeHandler();
});

this.addListener('load', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't need to specifically remove this when you have removeAllListeners() without a param.

}

/**
* Returns whether or not viewer has the option to annotate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns whether or not annotations are enabled for this viewer.

// Construct and init annotator
this.annotator = new this.annotatorLoader.CONSTRUCTOR({
canAnnotate,
container: this.options.container,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this.options when we destructure above

apiHost,
fileId: file.id,
token
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just move these into the annotator constructor options.

this.annotator = new this.annotatorLoader.CONSTRUCTOR({
canAnnotate,
container: this.options.container,
options: annotationOptions,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

options: {
    apiHost,
    fileId: file.id,
    token,
}


/**
* Returns whether or not viewer is annotatable. If an optional type is
* passed in, we check if that type of annotation is allowed.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returns whether or not viewer both supports annotations and has annotations enabled.

* @return {boolean} Whether or not viewer is annotatable
*/
isAnnotatable(type) {
if (type && this.annotationTypes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we pass isAnnotatable with a type anywhere anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPointModeClickHandler() in DocBaseViewer and ImageViewer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const { TYPE: annotationTypes } = this.annotatorConf; after you remove this.annotationTypes

// Attempts to load annotations assets and initializes annotations if
// the assets are available, the showAnnotations flag is true, and the
// expiring embed is not a shared link
// TODO(@spramod): Determine the expected behavior on shared links
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Has there been any update on this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asked by Tony...

@@ -83,6 +96,8 @@ class BaseViewer extends EventEmitter {
fullscreen.removeAllListeners();
document.defaultView.removeEventListener('resize', this.debouncedResizeHandler);
this.removeAllListeners();
this.removeAllListeners('togglepointannotationmode');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I think I was wrong here. It looks like when you call removeAllListeners with no arguments it removes all listeners bound. https://nodejs.org/api/events.html#events_emitter_removealllisteners_eventname

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

asked by Tony...

@@ -255,6 +270,12 @@ class BaseViewer extends EventEmitter {
this.addListener('togglepointannotationmode', () => {
this.annotator.togglePointModeHandler();
});

this.addListener('load', () => {
if (window.BoxAnnotations !== undefined && this.isViewerAnnotatable()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just do (if window.boxAnnotations && this.isViewerAnnotatable())?

const fileVersionID = file.file_version.id;

// Users can currently only view annotations on mobile
const canAnnotate = checkPermission(file, PERMISSION_ANNOTATE) && !Browser.isMobile();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe extract this out to a class value since we do it twice?

isViewerAnnotatable() {
// Respect viewer-specific annotation option if it is set
const viewerAnnotations = this.getViewerOption('annotations');
if (typeof viewerAnnotations === 'boolean') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What else could viewerAnnotations be?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other option would be undefined, this is really just double checking against if developers set it to anything other than a boolean


annotateButtonEl.title = __('annotation_point_toggle');
annotateButtonEl.classList.remove(CLASS_HIDDEN);
annotateButtonEl.addEventListener('click', handler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be removed separately since it's a DOM event listener.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't actually remove the event listener unless you also save a reference to the handler. Unlike event emitter event listeners, DOM listeners can't be removed unless you specific the handler as the second param.

I don't think we ever removed this handler in the previous implementation... to do so we'd have to do:

this.annotateClickHandler = handler;
...
annotateButtonEl.addEventListener('click', this.annotateClickHandler);

And in the cleanup, remove with:

annotateButtonEl.removeEventListener('click', this.annotateClickHandler);

Don't have to null check for this.annotateClickHandler - if it doesn't exist, removeEventListener doesn't do anything.

* @return {Function|null} Click handler
*/
/* eslint-disable no-unused-vars */
getPointModeClickHandler(containerEl) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing the containerEl param in the JSDoc, maybe use @inheritdoc here?

// the assets are available, the showAnnotations flag is true, and the
// expiring embed is not a shared link
if (this.areAnnotationsEnabled() && !this.options.sharedLink) {
this.loadAssets(ANNOTATIONS_JS);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's potentially a timing issue if the document loads before annotations JS is ready. Doesn't seem super likely but it's possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave this for now - I think we could hook into each viewer's loadAssets if needed but it gets a bit complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea it happens very rarely but it does happen. I can look into this during the refactor

/* global BoxAnnotations */
this.loader = new BoxAnnotations();

this.annotatorLoader = this.loader.determineAnnotator(this.options.viewer.NAME);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Naming is confusing between this.loader and this.annotatorLoader.

Do you use this.loader anywhere else? If not I'd do:

const boxAnnotations = new BoxAnnotations;
this.annotatorConf = boxAnnotations.determineAnnotator(this.options.viewer.NAME);
if (!this.annotatorConf) {
    return;
}

return;
}

this.annotationTypes = this.annotatorLoader.TYPE;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's already available on this.annotatorConf.TYPE, I wouldn't define it again

*/
initAnnotations() {
const { apiHost, container, file, location, token } = this.options;
const fileVersionID = file.file_version.id;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const { id: fileId, file_version: { id: fileVersionId } } = file;

This creates two const variables, fileId and fileVersionId, set to file.id and file.file_version.id respectively. Use camelCase Id instead of ID - I know annotations code base uses fileVersionID and threadID and annotationID right now, we can update that later, but the rest of preview uses Id

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh cool! Didn't know I could do that!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also made sure to fix fileID and fileVersionID everywhere in BoxAnnotations.js so this shouldn't be anywhere in annotations anymore

* @return {boolean} Whether or not viewer is annotatable
*/
isAnnotatable(type) {
if (type && this.annotationTypes) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use const { TYPE: annotationTypes } = this.annotatorConf; after you remove this.annotationTypes

}
}

// Respect viewer-specific annotation option if it is set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// Return whether or not annotations are enabled for this viewer


if (!supportedType) {
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simplify this to:

if (!annotationTypes.some((annotationType) => type === annotationType)) {
    return false;
}


annotateButtonEl.title = __('annotation_point_toggle');
annotateButtonEl.classList.remove(CLASS_HIDDEN);
annotateButtonEl.addEventListener('click', handler);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't actually remove the event listener unless you also save a reference to the handler. Unlike event emitter event listeners, DOM listeners can't be removed unless you specific the handler as the second param.

I don't think we ever removed this handler in the previous implementation... to do so we'd have to do:

this.annotateClickHandler = handler;
...
annotateButtonEl.addEventListener('click', this.annotateClickHandler);

And in the cleanup, remove with:

annotateButtonEl.removeEventListener('click', this.annotateClickHandler);

Don't have to null check for this.annotateClickHandler - if it doesn't exist, removeEventListener doesn't do anything.

- Replacing fileVersionID with fileVersionId (same with fileID)
- Making sure isAnnotatable() is not overridden in Viewers
@pramodsum pramodsum merged commit bbc6ea3 into box:master May 9, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants