2013-09-25 00:46:54 +09:00
|
|
|
/* Copyright 2012 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-05-04 10:05:53 +09:00
|
|
|
import { NullL10n } from './ui_utils';
|
2017-05-31 10:38:22 +09:00
|
|
|
import { PasswordResponses } from 'pdfjs-lib';
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2016-04-19 06:59:46 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PasswordPromptOptions
|
|
|
|
* @property {string} overlayName - Name of the overlay for the overlay manager.
|
2016-04-22 01:39:11 +09:00
|
|
|
* @property {HTMLDivElement} container - Div container for the overlay.
|
2016-04-19 06:59:46 +09:00
|
|
|
* @property {HTMLParagraphElement} label - Label containing instructions for
|
|
|
|
* entering the password.
|
|
|
|
* @property {HTMLInputElement} input - Input field for entering the password.
|
|
|
|
* @property {HTMLButtonElement} submitButton - Button for submitting the
|
|
|
|
* password.
|
|
|
|
* @property {HTMLButtonElement} cancelButton - Button for cancelling password
|
|
|
|
* entry.
|
|
|
|
*/
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2017-04-16 07:11:44 +09:00
|
|
|
class PasswordPrompt {
|
2016-04-19 06:59:46 +09:00
|
|
|
/**
|
|
|
|
* @param {PasswordPromptOptions} options
|
2017-05-26 21:52:23 +09:00
|
|
|
* @param {OverlayManager} overlayManager - Manager for the viewer overlays.
|
2017-05-04 10:05:53 +09:00
|
|
|
* @param {IL10n} l10n - Localization service.
|
2016-04-19 06:59:46 +09:00
|
|
|
*/
|
2017-05-04 10:05:53 +09:00
|
|
|
constructor(options, overlayManager, l10n = NullL10n) {
|
2016-04-19 06:59:46 +09:00
|
|
|
this.overlayName = options.overlayName;
|
2016-04-22 01:39:11 +09:00
|
|
|
this.container = options.container;
|
2016-04-19 06:59:46 +09:00
|
|
|
this.label = options.label;
|
|
|
|
this.input = options.input;
|
|
|
|
this.submitButton = options.submitButton;
|
|
|
|
this.cancelButton = options.cancelButton;
|
2017-05-26 21:52:23 +09:00
|
|
|
this.overlayManager = overlayManager;
|
2017-05-04 10:05:53 +09:00
|
|
|
this.l10n = l10n;
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2016-04-19 06:59:46 +09:00
|
|
|
this.updateCallback = null;
|
|
|
|
this.reason = null;
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2016-04-19 06:59:46 +09:00
|
|
|
// Attach the event listeners.
|
|
|
|
this.submitButton.addEventListener('click', this.verify.bind(this));
|
|
|
|
this.cancelButton.addEventListener('click', this.close.bind(this));
|
2017-04-16 07:11:44 +09:00
|
|
|
this.input.addEventListener('keydown', (e) => {
|
2014-05-13 19:08:39 +09:00
|
|
|
if (e.keyCode === 13) { // Enter key
|
2016-04-19 06:59:46 +09:00
|
|
|
this.verify();
|
2014-05-13 19:08:39 +09:00
|
|
|
}
|
2017-04-16 07:11:44 +09:00
|
|
|
});
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2017-05-26 21:52:23 +09:00
|
|
|
this.overlayManager.register(this.overlayName, this.container,
|
|
|
|
this.close.bind(this), true);
|
2016-04-19 06:59:46 +09:00
|
|
|
}
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2017-04-16 07:11:44 +09:00
|
|
|
open() {
|
2017-05-26 21:52:23 +09:00
|
|
|
this.overlayManager.open(this.overlayName).then(() => {
|
2017-04-16 07:11:44 +09:00
|
|
|
this.input.focus();
|
2016-04-19 06:59:46 +09:00
|
|
|
|
2017-05-04 10:05:53 +09:00
|
|
|
let promptString;
|
2017-04-16 07:11:44 +09:00
|
|
|
if (this.reason === PasswordResponses.INCORRECT_PASSWORD) {
|
2017-05-04 10:05:53 +09:00
|
|
|
promptString = this.l10n.get('password_invalid', null,
|
2017-04-16 07:11:44 +09:00
|
|
|
'Invalid password. Please try again.');
|
2017-05-04 10:05:53 +09:00
|
|
|
} else {
|
|
|
|
promptString = this.l10n.get('password_label', null,
|
|
|
|
'Enter the password to open this PDF file.');
|
2017-04-16 07:11:44 +09:00
|
|
|
}
|
2016-04-19 06:59:46 +09:00
|
|
|
|
2017-05-04 10:05:53 +09:00
|
|
|
promptString.then((msg) => {
|
|
|
|
this.label.textContent = msg;
|
|
|
|
});
|
2017-04-16 07:11:44 +09:00
|
|
|
});
|
|
|
|
}
|
2016-04-19 06:59:46 +09:00
|
|
|
|
2017-04-16 07:11:44 +09:00
|
|
|
close() {
|
2017-05-26 21:52:23 +09:00
|
|
|
this.overlayManager.close(this.overlayName).then(() => {
|
2017-04-16 07:11:44 +09:00
|
|
|
this.input.value = '';
|
|
|
|
});
|
|
|
|
}
|
2013-09-25 00:46:54 +09:00
|
|
|
|
2017-04-16 07:11:44 +09:00
|
|
|
verify() {
|
2017-06-30 19:55:22 +09:00
|
|
|
let password = this.input.value;
|
2017-04-16 07:11:44 +09:00
|
|
|
if (password && password.length > 0) {
|
|
|
|
this.close();
|
|
|
|
return this.updateCallback(password);
|
2013-09-25 00:46:54 +09:00
|
|
|
}
|
2017-04-16 07:11:44 +09:00
|
|
|
}
|
2016-04-19 06:59:46 +09:00
|
|
|
|
2017-04-16 07:11:44 +09:00
|
|
|
setUpdateCallback(updateCallback, reason) {
|
|
|
|
this.updateCallback = updateCallback;
|
|
|
|
this.reason = reason;
|
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export {
|
|
|
|
PasswordPrompt,
|
|
|
|
};
|