pdf.js/web/hand_tool.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

/* Copyright 2013 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2017-04-15 00:32:36 +09:00
import { GrabToPan } from './grab_to_pan';
import { localized } from './ui_utils';
2016-04-16 23:47:48 +09:00
/**
* @typedef {Object} HandToolOptions
* @property {HTMLDivElement} container - The document container.
2016-04-26 07:57:15 +09:00
* @property {EventBus} eventBus - The application event bus.
2017-05-08 04:30:07 +09:00
* @property {BasePreferences} preferences - Object for reading/writing
* persistent settings.
2016-04-16 23:47:48 +09:00
*/
2017-05-08 04:30:07 +09:00
class HandTool {
2016-04-16 23:47:48 +09:00
/**
* @param {HandToolOptions} options
*/
2017-05-08 04:30:07 +09:00
constructor({ container, eventBus, preferences, }) {
this.container = container;
this.eventBus = eventBus;
2016-04-16 23:47:48 +09:00
this.wasActive = false;
this.handTool = new GrabToPan({
2016-04-16 23:47:48 +09:00
element: this.container,
onActiveChanged: (isActive) => {
this.eventBus.dispatch('handtoolchanged', { isActive, });
},
});
2016-04-16 23:47:48 +09:00
this.eventBus.on('togglehandtool', this.toggle.bind(this));
2017-05-08 04:30:07 +09:00
let enableOnLoad = preferences.get('enableHandToolOnLoad');
Promise.all([localized, enableOnLoad]).then((values) => {
if (values[1] === true) {
this.handTool.activate();
}
2017-05-08 04:30:07 +09:00
}).catch(function(reason) {});
this.eventBus.on('presentationmodechanged', (evt) => {
if (evt.switchInProgress) {
return;
}
if (evt.active) {
this.enterPresentationMode();
} else {
this.exitPresentationMode();
}
});
2016-04-16 23:47:48 +09:00
}
2017-05-08 04:30:07 +09:00
/**
* @return {boolean}
*/
get isActive() {
return !!this.handTool.active;
}
2016-04-16 23:47:48 +09:00
2017-05-08 04:30:07 +09:00
toggle() {
this.handTool.toggle();
}
2017-05-08 04:30:07 +09:00
enterPresentationMode() {
if (this.isActive) {
this.wasActive = true;
this.handTool.deactivate();
}
2017-05-08 04:30:07 +09:00
}
2016-04-16 23:47:48 +09:00
2017-05-08 04:30:07 +09:00
exitPresentationMode() {
if (this.wasActive) {
this.wasActive = false;
this.handTool.activate();
}
}
}
export {
HandTool,
};