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