Convert the sidebar to ES6 syntax

This commit is contained in:
Tim van der Meij 2017-04-17 20:32:21 +02:00
parent 0e8f020e2e
commit 26ad82f5c2
No known key found for this signature in database
GPG Key ID: 8C3FD2925A5F2762

View File

@ -16,9 +16,9 @@
import { mozL10n } from './ui_utils'; import { mozL10n } from './ui_utils';
import { RenderingStates } from './pdf_rendering_queue'; import { RenderingStates } from './pdf_rendering_queue';
var UI_NOTIFICATION_CLASS = 'pdfSidebarNotification'; const UI_NOTIFICATION_CLASS = 'pdfSidebarNotification';
var SidebarView = { const SidebarView = {
NONE: 0, NONE: 0,
THUMBS: 1, THUMBS: 1,
OUTLINE: 2, OUTLINE: 2,
@ -53,15 +53,11 @@ var SidebarView = {
* for documents containing outline/attachments. The default value is `false`. * for documents containing outline/attachments. The default value is `false`.
*/ */
class PDFSidebar {
/** /**
* @class
*/
var PDFSidebar = (function PDFSidebarClosure() {
/**
* @constructs PDFSidebar
* @param {PDFSidebarOptions} options * @param {PDFSidebarOptions} options
*/ */
function PDFSidebar(options) { constructor(options) {
this.isOpen = false; this.isOpen = false;
this.active = SidebarView.THUMBS; this.active = SidebarView.THUMBS;
this.isInitialViewSet = false; this.isInitialViewSet = false;
@ -94,8 +90,7 @@ var PDFSidebar = (function PDFSidebarClosure() {
this._addEventListeners(); this._addEventListeners();
} }
PDFSidebar.prototype = { reset() {
reset: function PDFSidebar_reset() {
this.isInitialViewSet = false; this.isInitialViewSet = false;
this._hideUINotification(null); this._hideUINotification(null);
@ -103,32 +98,32 @@ var PDFSidebar = (function PDFSidebarClosure() {
this.outlineButton.disabled = false; this.outlineButton.disabled = false;
this.attachmentsButton.disabled = false; this.attachmentsButton.disabled = false;
}, }
/** /**
* @returns {number} One of the values in {SidebarView}. * @returns {number} One of the values in {SidebarView}.
*/ */
get visibleView() { get visibleView() {
return (this.isOpen ? this.active : SidebarView.NONE); return (this.isOpen ? this.active : SidebarView.NONE);
}, }
get isThumbnailViewVisible() { get isThumbnailViewVisible() {
return (this.isOpen && this.active === SidebarView.THUMBS); return (this.isOpen && this.active === SidebarView.THUMBS);
}, }
get isOutlineViewVisible() { get isOutlineViewVisible() {
return (this.isOpen && this.active === SidebarView.OUTLINE); return (this.isOpen && this.active === SidebarView.OUTLINE);
}, }
get isAttachmentsViewVisible() { get isAttachmentsViewVisible() {
return (this.isOpen && this.active === SidebarView.ATTACHMENTS); return (this.isOpen && this.active === SidebarView.ATTACHMENTS);
}, }
/** /**
* @param {number} view - The sidebar view that should become visible, * @param {number} view - The sidebar view that should become visible,
* must be one of the values in {SidebarView}. * must be one of the values in {SidebarView}.
*/ */
setInitialView: function PDFSidebar_setInitialView(view) { setInitialView(view) {
if (this.isInitialViewSet) { if (this.isInitialViewSet) {
return; return;
} }
@ -148,15 +143,15 @@ var PDFSidebar = (function PDFSidebarClosure() {
// since `this.switchView` dispatched the event if the view changed. // since `this.switchView` dispatched the event if the view changed.
this._dispatchEvent(); this._dispatchEvent();
} }
}, }
/** /**
* @param {number} view - The sidebar view that should be switched to, * @param {number} view - The sidebar view that should be switched to,
* must be one of the values in {SidebarView}. * must be one of the values in {SidebarView}.
* @param {boolean} forceOpen - (optional) Ensure that the sidebar is open. * @param {boolean} forceOpen - (optional) Ensure that the sidebar is open.
* The default value is false. * The default value is `false`.
*/ */
switchView: function PDFSidebar_switchView(view, forceOpen) { switchView(view, forceOpen = false) {
if (view === SidebarView.NONE) { if (view === SidebarView.NONE) {
this.close(); this.close();
return; return;
@ -223,9 +218,9 @@ var PDFSidebar = (function PDFSidebarClosure() {
this._dispatchEvent(); this._dispatchEvent();
} }
this._hideUINotification(this.active); this._hideUINotification(this.active);
}, }
open: function PDFSidebar_open() { open() {
if (this.isOpen) { if (this.isOpen) {
return; return;
} }
@ -242,9 +237,9 @@ var PDFSidebar = (function PDFSidebarClosure() {
this._dispatchEvent(); this._dispatchEvent();
this._hideUINotification(this.active); this._hideUINotification(this.active);
}, }
close: function PDFSidebar_close() { close() {
if (!this.isOpen) { if (!this.isOpen) {
return; return;
} }
@ -256,42 +251,42 @@ var PDFSidebar = (function PDFSidebarClosure() {
this._forceRendering(); this._forceRendering();
this._dispatchEvent(); this._dispatchEvent();
}, }
toggle: function PDFSidebar_toggle() { toggle() {
if (this.isOpen) { if (this.isOpen) {
this.close(); this.close();
} else { } else {
this.open(); this.open();
} }
}, }
/** /**
* @private * @private
*/ */
_dispatchEvent: function PDFSidebar_dispatchEvent() { _dispatchEvent() {
this.eventBus.dispatch('sidebarviewchanged', { this.eventBus.dispatch('sidebarviewchanged', {
source: this, source: this,
view: this.visibleView, view: this.visibleView,
}); });
}, }
/** /**
* @private * @private
*/ */
_forceRendering: function PDFSidebar_forceRendering() { _forceRendering() {
if (this.onToggled) { if (this.onToggled) {
this.onToggled(); this.onToggled();
} else { // Fallback } else { // Fallback
this.pdfViewer.forceRendering(); this.pdfViewer.forceRendering();
this.pdfThumbnailViewer.forceRendering(); this.pdfThumbnailViewer.forceRendering();
} }
}, }
/** /**
* @private * @private
*/ */
_updateThumbnailViewer: function PDFSidebar_updateThumbnailViewer() { _updateThumbnailViewer() {
var pdfViewer = this.pdfViewer; var pdfViewer = this.pdfViewer;
var thumbnailViewer = this.pdfThumbnailViewer; var thumbnailViewer = this.pdfThumbnailViewer;
@ -305,12 +300,12 @@ var PDFSidebar = (function PDFSidebarClosure() {
} }
} }
thumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber); thumbnailViewer.scrollThumbnailIntoView(pdfViewer.currentPageNumber);
}, }
/** /**
* @private * @private
*/ */
_showUINotification: function (view) { _showUINotification(view) {
if (this.disableNotification) { if (this.disableNotification) {
return; return;
} }
@ -336,17 +331,17 @@ var PDFSidebar = (function PDFSidebarClosure() {
this.attachmentsButton.classList.add(UI_NOTIFICATION_CLASS); this.attachmentsButton.classList.add(UI_NOTIFICATION_CLASS);
break; break;
} }
}, }
/** /**
* @private * @private
*/ */
_hideUINotification: function (view) { _hideUINotification(view) {
if (this.disableNotification) { if (this.disableNotification) {
return; return;
} }
var removeNotification = function (view) { var removeNotification = (view) => {
switch (view) { switch (view) {
case SidebarView.OUTLINE: case SidebarView.OUTLINE:
this.outlineButton.classList.remove(UI_NOTIFICATION_CLASS); this.outlineButton.classList.remove(UI_NOTIFICATION_CLASS);
@ -355,7 +350,7 @@ var PDFSidebar = (function PDFSidebarClosure() {
this.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS); this.attachmentsButton.classList.remove(UI_NOTIFICATION_CLASS);
break; break;
} }
}.bind(this); };
if (!this.isOpen && view !== null) { if (!this.isOpen && view !== null) {
// Only hide the notifications when the sidebar is currently open, // Only hide the notifications when the sidebar is currently open,
@ -374,76 +369,71 @@ var PDFSidebar = (function PDFSidebarClosure() {
this.toggleButton.title = mozL10n.get('toggle_sidebar.title', null, this.toggleButton.title = mozL10n.get('toggle_sidebar.title', null,
'Toggle Sidebar'); 'Toggle Sidebar');
}, }
/** /**
* @private * @private
*/ */
_addEventListeners: function PDFSidebar_addEventListeners() { _addEventListeners() {
var self = this; this.mainContainer.addEventListener('transitionend', (evt) => {
if (evt.target === this.mainContainer) {
self.mainContainer.addEventListener('transitionend', function(evt) { this.outerContainer.classList.remove('sidebarMoving');
if (evt.target === /* mainContainer */ this) {
self.outerContainer.classList.remove('sidebarMoving');
} }
}); });
// Buttons for switching views. // Buttons for switching views.
self.thumbnailButton.addEventListener('click', function() { this.thumbnailButton.addEventListener('click', () => {
self.switchView(SidebarView.THUMBS); this.switchView(SidebarView.THUMBS);
}); });
self.outlineButton.addEventListener('click', function() { this.outlineButton.addEventListener('click', () => {
self.switchView(SidebarView.OUTLINE); this.switchView(SidebarView.OUTLINE);
}); });
self.outlineButton.addEventListener('dblclick', function() { this.outlineButton.addEventListener('dblclick', () => {
self.pdfOutlineViewer.toggleOutlineTree(); this.pdfOutlineViewer.toggleOutlineTree();
}); });
self.attachmentsButton.addEventListener('click', function() { this.attachmentsButton.addEventListener('click', () => {
self.switchView(SidebarView.ATTACHMENTS); this.switchView(SidebarView.ATTACHMENTS);
}); });
// Disable/enable views. // Disable/enable views.
self.eventBus.on('outlineloaded', function(e) { this.eventBus.on('outlineloaded', (evt) => {
var outlineCount = e.outlineCount; var outlineCount = evt.outlineCount;
self.outlineButton.disabled = !outlineCount; this.outlineButton.disabled = !outlineCount;
if (outlineCount) { if (outlineCount) {
self._showUINotification(SidebarView.OUTLINE); this._showUINotification(SidebarView.OUTLINE);
} else if (self.active === SidebarView.OUTLINE) { } else if (this.active === SidebarView.OUTLINE) {
// If the outline view was opened during document load, switch away // If the outline view was opened during document load, switch away
// from it if it turns out that the document has no outline. // from it if it turns out that the document has no outline.
self.switchView(SidebarView.THUMBS); this.switchView(SidebarView.THUMBS);
} }
}); });
self.eventBus.on('attachmentsloaded', function(e) { this.eventBus.on('attachmentsloaded', (evt) => {
var attachmentsCount = e.attachmentsCount; var attachmentsCount = evt.attachmentsCount;
self.attachmentsButton.disabled = !attachmentsCount; this.attachmentsButton.disabled = !attachmentsCount;
if (attachmentsCount) { if (attachmentsCount) {
self._showUINotification(SidebarView.ATTACHMENTS); this._showUINotification(SidebarView.ATTACHMENTS);
} else if (self.active === SidebarView.ATTACHMENTS) { } else if (this.active === SidebarView.ATTACHMENTS) {
// If the attachment view was opened during document load, switch away // If the attachment view was opened during document load, switch away
// from it if it turns out that the document has no attachments. // from it if it turns out that the document has no attachments.
self.switchView(SidebarView.THUMBS); this.switchView(SidebarView.THUMBS);
} }
}); });
// Update the thumbnailViewer, if visible, when exiting presentation mode. // Update the thumbnailViewer, if visible, when exiting presentation mode.
self.eventBus.on('presentationmodechanged', function(e) { this.eventBus.on('presentationmodechanged', (evt) => {
if (!e.active && !e.switchInProgress && self.isThumbnailViewVisible) { if (!evt.active && !evt.switchInProgress && this.isThumbnailViewVisible) {
self._updateThumbnailViewer(); this._updateThumbnailViewer();
} }
}); });
}, }
}; }
return PDFSidebar;
})();
export { export {
SidebarView, SidebarView,