salix/client/core/src/dialog/dialog.js

119 lines
3.0 KiB
JavaScript
Raw Normal View History

import {module} from '../module';
2017-02-07 13:34:26 +00:00
import Component from '../lib/component';
import './style.scss';
/**
* Dialog component.
*/
export default class Dialog extends Component {
/**
* Contructor.
*/
constructor($element) {
super($element);
$element.addClass('vn-dialog');
this.dialog = $element[0].firstChild;
this.element.addEventListener('mousedown', event => this.onBackgroundMouseDown(event));
}
/**
* Displays the dialog to the user.
*/
show() {
let style = this.dialog.style;
let screenMargin = 20;
let window = this.window;
let innerWidth = window.innerWidth;
let innerHeight = window.innerHeight;
let width = this.dialog.offsetWidth;
let height = this.dialog.offsetHeight;
2017-05-25 10:52:38 +00:00
if (width + screenMargin > innerWidth) {
width = innerWidth - dblMargin;
2017-05-25 10:52:38 +00:00
style.width = width + 'px';
}
2017-05-25 10:52:38 +00:00
if (height + screenMargin > innerHeight) {
height = innerHeight - dblMargin;
2017-05-25 10:52:38 +00:00
style.height = height + 'px';
}
this.keypressHandler =
event => this.onKeypress(event);
this.document.addEventListener('keypress',
this.keypressHandler);
this.element.style.display = 'block';
2017-05-25 10:52:38 +00:00
if (this.onOpen)
this.onOpen();
}
/**
* Hides the dialog calling the response handler.
*/
hide() {
this.fireResponse();
this.realHide();
}
/**
* Calls the response handler.
2017-06-03 11:01:47 +00:00
*
* @param {String} response The response code
* @return {Boolean} %true if response was canceled, %false otherwise
*/
fireResponse(response) {
let cancel = false;
2017-05-25 10:52:38 +00:00
if (this.onResponse)
cancel = this.onResponse({response: response});
return cancel;
}
realHide() {
this.element.style.display = 'none';
this.document.removeEventListener('keypress',
this.keypressHandler);
this.lastEvent = null;
}
onButtonClick(event) {
let buttonBar = this.element.querySelector('.button-bar');
2017-05-25 10:52:38 +00:00
let buttons = buttonBar.querySelector('tpl-buttons');
let node = event.target;
2017-05-25 10:52:38 +00:00
while (node.parentNode != buttons) {
if (node == buttonBar) return;
node = node.parentNode;
}
let response = node.getAttribute('response');
let cancel = this.fireResponse(response);
2017-05-25 10:52:38 +00:00
if (cancel !== false) this.realHide();
}
onDialogMouseDown(event) {
this.lastEvent = event;
}
onBackgroundMouseDown(event) {
if (event != this.lastEvent)
this.hide();
}
onKeypress(event) {
2017-05-25 10:52:38 +00:00
if (event.keyCode == 27) // Esc
this.hide();
}
}
Dialog.$inject = ['$element'];
module.component('vnDialog', {
2017-05-31 07:46:05 +00:00
template: require('./dialog.html'),
transclude: {
2017-05-25 10:52:38 +00:00
tplBody: 'tplBody',
tplButtons: 'tplButtons'
},
bindings: {
onOpen: '&',
onResponse: '&'
},
controller: Dialog
});