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

New: Show banner when doc has xrefs #1013

Merged
merged 4 commits into from
Jun 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/i18n/en-US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ download_file=Download File
text_truncated=This file has been truncated due to size limits. Please download to view the whole file.
# Button tooltip to toggle Thumbnails Sidebar
toggle_thumbnails=Toggle thumbnails
# Message when file has inaccessible xrefs
hasxrefs=This preview has links you cannot view, open in its native application to view
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved

# Error messages
# Default preview error message
Expand Down
5 changes: 3 additions & 2 deletions src/lib/PreviewUI.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,15 @@ class PreviewUI {
* @public
* @param {string} message - Notification message
* @param {string} [buttonText] - Optional text to show in button
* @param {boolean} [persist] - Optional boolean to persist the notification
* @return {void}
*/
showNotification(message, buttonText) {
showNotification(message, buttonText, persist) {
if (!this.notification) {
return;
}

this.notification.show(message, buttonText);
this.notification.show(message, buttonText, persist);
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/lib/__tests__/metadataAPI-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,16 @@ describe('metadataAPI', () => {

return metadataAPI.getXrefsMetadata('123', 'autocad').then((response) => {
expect(stubs.get).to.have.been.called;
expect(response).to.eql(expResponse);
expect(response).to.eql({ hasxrefs: true });
});
});

it('Should return manufactured global template on api 404', () => {
const expResponse = { hasxrefs: 'false' };
stubs.get.rejects({ response: { status: 404 } });

return metadataAPI.getXrefsMetadata('123', 'autocad').then((response) => {
expect(stubs.get).to.have.been.called;
expect(response).to.eql(expResponse);
expect(response).to.eql({ hasxrefs: false });
});
});

Expand Down
6 changes: 6 additions & 0 deletions src/lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,9 @@ export const ANNOTATOR_EVENT = {
error: 'annotationerror',
scale: 'scaleannotations'
};

export const METADATA = {
FIELD_HASXREFS: 'hasxrefs',
SCOPE_GLOBAL: 'global',
TEMPLATE_AUTOCAD: 'autocad'
};
35 changes: 23 additions & 12 deletions src/lib/metadataAPI.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import api from './api';
import { getHeaders } from './util';
import { METADATA } from './constants';

const { FIELD_HASXREFS, SCOPE_GLOBAL } = METADATA;

const metadataAPI = {
/**
Expand All @@ -14,17 +17,25 @@ const metadataAPI = {
return Promise.reject(new Error('id and template are required parameters'));
}

return metadataAPI.getMetadata(id, 'global', template, options).catch((err) => {
const { response } = err;
// If the http response is 404, this is a valid case because the metadata template
// may not be initialized on the requested file. Resolve the promise with
// a constructed hasxrefs value
if (response && response.status === 404) {
return Promise.resolve({ hasxrefs: 'false' });
}

throw err;
});
return metadataAPI
.getMetadata(id, SCOPE_GLOBAL, template, options)
.then((response) => {
// The hasxrefs value is returned as a string 'false' or 'true' so we want
// to convert this to a boolean
const { [FIELD_HASXREFS]: hasxrefsValue } = response;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
return { ...response, [FIELD_HASXREFS]: hasxrefsValue === 'true' };
})
.catch((err) => {
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
const { response } = err;
// If the http response is 404, this is a valid case because the metadata template
// may not be initialized on the requested file. Resolve the promise with
// a constructed hasxrefs value
if (response && response.status === 404) {
return Promise.resolve({ [FIELD_HASXREFS]: false });
}

throw err;
});
},

/**
Expand All @@ -49,7 +60,7 @@ const metadataAPI = {
* @param {string} apiHost - API host
* @return {string} The metadata url
*/
getMetadataURL(fileId, scope = 'global', template, apiHost) {
getMetadataURL(fileId, scope = SCOPE_GLOBAL, template, apiHost) {
return `${apiHost}/2.0/files/${fileId}/metadata/${scope}/${template}`;
}
};
Expand Down
38 changes: 30 additions & 8 deletions src/lib/viewers/doc/DocBaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import RepStatus from '../../RepStatus';
import PreviewError from '../../PreviewError';
import ThumbnailsSidebar from '../../ThumbnailsSidebar';
import {
ANNOTATOR_EVENT,
CLASS_BOX_PREVIEW_FIND_BAR,
CLASS_BOX_PREVIEW_THUMBNAILS_CLOSE_ACTIVE,
CLASS_BOX_PREVIEW_THUMBNAILS_CLOSE,
CLASS_BOX_PREVIEW_THUMBNAILS_CONTAINER,
CLASS_BOX_PREVIEW_THUMBNAILS_OPEN_ACTIVE,
CLASS_BOX_PREVIEW_THUMBNAILS_OPEN,
CLASS_CRAWLER,
CLASS_HIDDEN,
CLASS_IS_SCROLLABLE,
DOC_STATIC_ASSETS_VERSION,
ENCODING_TYPES,
METADATA,
PERMISSION_DOWNLOAD,
PRELOAD_REP_NAME,
STATUS_SUCCESS,
QUERY_PARAM_ENCODING,
ENCODING_TYPES,
CLASS_BOX_PREVIEW_THUMBNAILS_CONTAINER,
ANNOTATOR_EVENT,
CLASS_BOX_PREVIEW_THUMBNAILS_CLOSE,
CLASS_BOX_PREVIEW_THUMBNAILS_CLOSE_ACTIVE,
CLASS_BOX_PREVIEW_THUMBNAILS_OPEN,
CLASS_BOX_PREVIEW_THUMBNAILS_OPEN_ACTIVE
STATUS_SUCCESS
} from '../../constants';
import { checkPermission, getRepresentation } from '../../file';
import { appendQueryParams, createAssetUrlCreator, getMidpoint, getDistance, getClosestPageToPinch } from '../../util';
Expand All @@ -40,6 +41,7 @@ import {
import { JS, PRELOAD_JS, CSS } from './docAssets';
import { ERROR_CODE, VIEWER_EVENT, LOAD_METRIC, USER_DOCUMENT_THUMBNAIL_EVENTS } from '../../events';
import Timer from '../../Timer';
import metadataAPI from '../../metadataAPI';

const CURRENT_PAGE_MAP_KEY = 'doc-current-page-map';
const DEFAULT_SCALE_DELTA = 1.1;
Expand Down Expand Up @@ -69,6 +71,7 @@ const METRICS_WHITELIST = [
USER_DOCUMENT_THUMBNAIL_EVENTS.NAVIGATE,
USER_DOCUMENT_THUMBNAIL_EVENTS.OPEN
];
const { FIELD_HASXREFS, TEMPLATE_AUTOCAD } = METADATA;

class DocBaseViewer extends BaseViewer {
//--------------------------------------------------------------------------
Expand Down Expand Up @@ -326,6 +329,8 @@ class DocBaseViewer extends BaseViewer {
super.load();
this.showPreload();

this.checkForXrefs();

const template = this.options.representation.content.url_template;
this.pdfUrl = this.createContentUrlWithAuthParams(template);

Expand All @@ -334,6 +339,23 @@ class DocBaseViewer extends BaseViewer {
.catch(this.handleAssetError);
}

/**
* Checks for xrefs, depending on file extension
* @return {void}
*/
checkForXrefs() {
const { extension, id } = this.options.file;

if (extension === 'dwg') {
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
metadataAPI.getXrefsMetadata(id, TEMPLATE_AUTOCAD, this.options).then((response) => {
const { [FIELD_HASXREFS]: hasxrefsValue } = response;
ConradJChan marked this conversation as resolved.
Show resolved Hide resolved
if (hasxrefsValue) {
this.options.ui.showNotification(__('hasxrefs'), null, true);
}
});
}
}

/**
* Loads a document after assets and representation are ready.
*
Expand Down