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

feat(loading): Add options for loading icon and progress bar #1344

Merged
merged 2 commits into from
Mar 29, 2021
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
4 changes: 0 additions & 4 deletions src/lib/Preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -1175,10 +1175,6 @@ class Preview extends EventEmitter {
this.ui.showLoadingDownloadButton(this.download);
}

// Update the loading indicators
this.ui.hideCrawler();
this.ui.setLoadingIcon(this.file.extension);

// Determine the asset loader to use
const loader = this.getLoader(this.file);

Expand Down
17 changes: 2 additions & 15 deletions src/lib/__tests__/Preview-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1666,8 +1666,6 @@ describe('lib/Preview', () => {
stubs.destroy = jest.spyOn(preview, 'destroy');
stubs.checkPermission = jest.spyOn(file, 'checkPermission').mockReturnValue(true);
stubs.canDownload = jest.spyOn(file, 'canDownload').mockReturnValue(false);
stubs.hideCrawler = jest.spyOn(preview.ui, 'hideCrawler').mockImplementation();
stubs.setLoadingIcon = jest.spyOn(preview.ui, 'setLoadingIcon').mockImplementation();
stubs.showLoadingDownloadButton = jest.spyOn(preview.ui, 'showLoadingDownloadButton').mockImplementation();
stubs.loadPromiseResolve = Promise.resolve();
stubs.determineRepresentationStatusPromise = Promise.resolve();
Expand Down Expand Up @@ -1715,27 +1713,16 @@ describe('lib/Preview', () => {

test('should show the loading download button if file can be downloaded', () => {
stubs.canDownload.mockReturnValue(true);
preview.loadViewer();
preview.loadViewer({});
jstoffan marked this conversation as resolved.
Show resolved Hide resolved
expect(stubs.showLoadingDownloadButton).toBeCalled();
});

test("should not show the loading download button if file can't be downloaded", () => {
stubs.canDownload.mockReturnValue(false);
preview.loadViewer();
preview.loadViewer({});
expect(stubs.showLoadingDownloadButton).not.toBeCalled();
});

test('should show the file-specific loading icon', () => {
preview.file.extension = 'pdf';
preview.loadViewer();
expect(stubs.setLoadingIcon).toBeCalledWith('pdf');
});

test('should hide the loading crawler', () => {
preview.loadViewer();
expect(stubs.hideCrawler).toBeCalled();
});

test('should throw an unsupported error if there is no loader for general file types', () => {
stubs.getLoader.mockReturnValue(undefined);

Expand Down
17 changes: 17 additions & 0 deletions src/lib/viewers/BaseViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ class BaseViewer extends EventEmitter {
// From the perspective of viewers bp-content holds everything
this.containerEl = container.querySelector(SELECTOR_BOX_PREVIEW_CONTENT);

// Update the loading indicators
this.setupLoading();

// Attach event listeners
this.addCommonListeners();

Expand All @@ -220,6 +223,20 @@ class BaseViewer extends EventEmitter {
this.isSetup = true;
}

/**
* Removes the crawler and sets the file type specific loading icon
*
* @return {void}
*/
setupLoading() {
const { file = {} } = this.options;

if (this.previewUI) {
this.previewUI.hideCrawler();
this.previewUI.setLoadingIcon(file.extension);
}
}

/**
* Destroys the viewer
*
Expand Down
15 changes: 14 additions & 1 deletion src/lib/viewers/__tests__/BaseViewer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ describe('lib/viewers/BaseViewer', () => {
},
});
base.previewUI = {
replaceHeader: jest.fn(),
hideCrawler: jest.fn(),
notification: {
show: jest.fn(),
hide: jest.fn(),
},
replaceHeader: jest.fn(),
setLoadingIcon: jest.fn(),
};
});

Expand Down Expand Up @@ -116,6 +118,17 @@ describe('lib/viewers/BaseViewer', () => {
});
});

describe('setupLoading()', () => {
test('should hide the crawler and set the file-specific loading icon', () => {
base.options.file = { extension: 'pdf' };

base.setupLoading();

expect(base.previewUI.hideCrawler).toBeCalled();
expect(base.previewUI.setLoadingIcon).toBeCalledWith('pdf');
});
});

describe('getResizeHandler()', () => {
test('should return a resize handler', () => {
expect(typeof base.getResizeHandler()).toBe('function');
Expand Down
2 changes: 1 addition & 1 deletion src/lib/viewers/error/PreviewErrorViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class PreviewErrorViewer extends BaseViewer {
* @override
* @return {void}
*/
finishLoadingSetup() {
setupLoading() {
/* no op, custom loading logic for errors */
}

Expand Down