Signal-Desktop/js/views/confirmation_dialog_view.js

80 lines
2.0 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2015-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2019-11-07 21:36:16 +00:00
/* global Backbone, Whisper, i18n */
// eslint-disable-next-line func-names
(function () {
2018-04-27 21:25:04 +00:00
window.Whisper = window.Whisper || {};
2018-04-27 21:25:04 +00:00
Whisper.ConfirmationDialogView = Whisper.View.extend({
className: 'confirmation-dialog modal',
templateName: 'confirmation-dialog',
initialize(options) {
2019-11-07 21:36:16 +00:00
this.previousFocus = document.activeElement;
2018-04-27 21:25:04 +00:00
this.message = options.message;
this.hideCancel = options.hideCancel;
2018-04-27 21:25:04 +00:00
this.resolve = options.resolve;
this.okText = options.okText || i18n('ok');
2018-04-27 21:25:04 +00:00
this.reject = options.reject;
this.cancelText = options.cancelText || i18n('cancel');
2019-11-07 21:36:16 +00:00
if (Whisper.activeConfirmationView) {
Whisper.activeConfirmationView.remove();
Whisper.activeConfirmationView = null;
}
Whisper.activeConfirmationView = this;
2018-04-27 21:25:04 +00:00
this.render();
},
events: {
2019-11-07 21:36:16 +00:00
keydown: 'onKeydown',
2018-04-27 21:25:04 +00:00
'click .ok': 'ok',
'click .cancel': 'cancel',
},
2019-11-07 21:36:16 +00:00
remove() {
if (this.previousFocus && this.previousFocus.focus) {
this.previousFocus.focus();
}
Backbone.View.prototype.remove.call(this);
},
render_attributes() {
2018-04-27 21:25:04 +00:00
return {
message: this.message,
showCancel: !this.hideCancel,
cancel: this.cancelText,
ok: this.okText,
};
},
ok() {
2018-04-27 21:25:04 +00:00
this.remove();
if (this.resolve) {
this.resolve();
}
},
cancel() {
2018-04-27 21:25:04 +00:00
this.remove();
if (this.reject) {
2020-04-28 21:18:41 +00:00
this.reject(new Error('User clicked cancel button'));
2018-04-27 21:25:04 +00:00
}
},
2019-11-07 21:36:16 +00:00
onKeydown(event) {
2018-04-27 21:25:04 +00:00
if (event.key === 'Escape' || event.key === 'Esc') {
this.cancel();
2019-11-07 21:36:16 +00:00
event.preventDefault();
event.stopPropagation();
2018-04-27 21:25:04 +00:00
}
},
focusCancel() {
// We delay this call because we might be called inside click handlers
// which would set focus to themselves afterwards!
setTimeout(() => this.$('.cancel').focus(), 1);
2018-04-27 21:25:04 +00:00
},
});
})();