2022-06-06 08:53:59 +00:00
|
|
|
require('./style.scss');
|
2022-06-06 12:49:18 +00:00
|
|
|
var Component = require('vn/component');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
2015-03-06 23:33:54 +00:00
|
|
|
* Class to handle popups.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-06-06 08:53:59 +00:00
|
|
|
module.exports = new Class({
|
2022-06-06 12:49:18 +00:00
|
|
|
Extends: Component
|
2015-03-06 23:33:54 +00:00
|
|
|
,Tag: 'htk-popup'
|
2022-06-06 08:53:59 +00:00
|
|
|
,Properties: {
|
2015-12-15 15:22:46 +00:00
|
|
|
/**
|
|
|
|
* The popup child.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-06-06 08:53:59 +00:00
|
|
|
child: {
|
2022-06-06 12:49:18 +00:00
|
|
|
type: Component
|
2022-05-26 06:08:31 +00:00
|
|
|
,set: function(x) {
|
2015-12-15 15:22:46 +00:00
|
|
|
this._child = x;
|
2022-05-26 06:08:31 +00:00
|
|
|
this._setChildNode(x.node);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2022-05-26 06:08:31 +00:00
|
|
|
,get: function() {
|
2015-12-15 15:22:46 +00:00
|
|
|
return this._child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The popup child Node.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-06-06 08:53:59 +00:00
|
|
|
,childNode: {
|
2015-12-15 15:22:46 +00:00
|
|
|
type: Object
|
2022-05-26 06:08:31 +00:00
|
|
|
,set: function(x) {
|
2015-12-15 15:22:46 +00:00
|
|
|
this._child = null;
|
2022-05-26 06:08:31 +00:00
|
|
|
this._setChildNode(x);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2022-05-26 06:08:31 +00:00
|
|
|
,get: function() {
|
2015-12-15 15:22:46 +00:00
|
|
|
return this.node.firstChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Indicates how the dialog must be displayed.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-06-06 08:53:59 +00:00
|
|
|
,modal: {
|
2015-12-15 15:22:46 +00:00
|
|
|
type: Boolean
|
2022-05-26 06:08:31 +00:00
|
|
|
,set: function(x) {
|
2015-12-15 15:22:46 +00:00
|
|
|
this._modal = x;
|
|
|
|
}
|
2022-05-26 06:08:31 +00:00
|
|
|
,get: function() {
|
2015-12-15 15:22:46 +00:00
|
|
|
return this._modal;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,_parent: null
|
2015-12-15 15:22:46 +00:00
|
|
|
,_modal: false
|
|
|
|
,_isOpen: false
|
|
|
|
,_child: null
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,initialize: function(props) {
|
|
|
|
this._bgMouseDownHandler = this._bgMouseDown.bind(this);
|
2022-06-06 16:02:17 +00:00
|
|
|
Component.prototype.initialize.call(this, props);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2016-10-16 14:16:08 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,render: function() {
|
|
|
|
var div = this.createRoot('div');
|
2016-10-16 14:16:08 +00:00
|
|
|
div.className = 'htk-popup';
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,_setChildNode: function(childNode) {
|
|
|
|
Vn.Node.removeChilds(this.node);
|
|
|
|
this.node.appendChild(childNode);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
2022-06-06 08:53:59 +00:00
|
|
|
,show: function(parent, event) {
|
2015-11-09 08:14:33 +00:00
|
|
|
this._parent = parent;
|
2022-06-06 08:53:59 +00:00
|
|
|
this._lastEvent = event;
|
2022-05-26 06:08:31 +00:00
|
|
|
this.open();
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,isModal: function() {
|
|
|
|
return this._modal || Vn.isMobile();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2015-07-21 14:16:07 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,open: function() {
|
|
|
|
if (this._isOpen) {
|
|
|
|
this.reset();
|
2016-05-02 13:05:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
this.node.addEventListener('mousedown', this._onMouseDown.bind(this));
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
if (this.isModal()) {
|
|
|
|
var bg = this._bg = this.createElement('div');
|
2016-05-04 14:36:51 +00:00
|
|
|
bg.className = 'htk-background';
|
2022-05-26 06:08:31 +00:00
|
|
|
bg.addEventListener('mousedown', this._bgMouseDownHandler);
|
|
|
|
Htk.Toast.pushTop(bg);
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
Vn.Node.addClass(this.node, 'modal');
|
|
|
|
bg.appendChild(this.node);
|
2016-05-04 14:36:51 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
this.doc.body.appendChild(bg);
|
|
|
|
setTimeout(this._onOpacityTimeout.bind(this), 0);
|
|
|
|
} else {
|
|
|
|
this.doc.addEventListener('mousedown', this._bgMouseDownHandler);
|
|
|
|
this.doc.body.appendChild(this.node);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
this._isOpen = true;
|
2022-05-26 06:08:31 +00:00
|
|
|
this.reset();
|
|
|
|
setTimeout(this._onResetTimeout.bind(this), 0);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,_onOpacityTimeout: function() {
|
2016-05-04 14:36:51 +00:00
|
|
|
if (this._bg)
|
|
|
|
this._bg.style.opacity = 1;
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,_onResetTimeout: function() {
|
|
|
|
this.reset();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,reset: function() {
|
2015-12-15 15:22:46 +00:00
|
|
|
if (!this._isOpen)
|
|
|
|
return;
|
2016-10-30 22:48:18 +00:00
|
|
|
|
|
|
|
var node = this._node;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2016-10-30 22:48:18 +00:00
|
|
|
var style = node.style;
|
2016-05-04 14:36:51 +00:00
|
|
|
style.height = '';
|
|
|
|
style.width = '';
|
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
var margin = 20;
|
|
|
|
var dblMargin = margin * 2;
|
2016-10-30 22:48:18 +00:00
|
|
|
var width = node.offsetWidth;
|
|
|
|
var height = node.offsetHeight;
|
2016-12-05 12:39:29 +00:00
|
|
|
var innerWidth = window.innerWidth;
|
|
|
|
var innerHeight = window.innerHeight;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
if (width + dblMargin > innerWidth) {
|
2015-12-15 15:22:46 +00:00
|
|
|
width = innerWidth - dblMargin;
|
|
|
|
style.width = width +'px';
|
|
|
|
}
|
2022-05-26 06:08:31 +00:00
|
|
|
if (height + dblMargin > innerHeight) {
|
2015-12-15 15:22:46 +00:00
|
|
|
height = innerHeight - dblMargin;
|
|
|
|
style.height = height +'px';
|
|
|
|
}
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
if (this.isModal()) {
|
2016-10-30 22:48:18 +00:00
|
|
|
style.marginLeft = (-node.offsetWidth / 2) +'px';
|
|
|
|
style.marginTop = (-node.offsetHeight / 2) +'px';
|
2022-05-26 06:08:31 +00:00
|
|
|
} else {
|
2015-12-15 15:22:46 +00:00
|
|
|
var spacing = 4;
|
2022-05-26 06:08:31 +00:00
|
|
|
var rect = this._parent.getBoundingClientRect();
|
2015-12-15 15:22:46 +00:00
|
|
|
var left = rect.left;
|
|
|
|
var top = rect.top + spacing + this._parent.offsetHeight;
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (left + width > innerWidth)
|
2016-12-05 12:39:29 +00:00
|
|
|
left -= (left + width) - window.innerWidth + margin;
|
2015-12-15 15:22:46 +00:00
|
|
|
if (top + height > innerHeight)
|
|
|
|
top -= height + this._parent.offsetHeight + spacing * 2;
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (left < 0)
|
|
|
|
left = margin;
|
|
|
|
if (top < 0)
|
|
|
|
top = margin;
|
|
|
|
|
|
|
|
style.top = (top) +'px';
|
|
|
|
style.left = (left) +'px';
|
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,hide: function() {
|
2015-12-15 15:22:46 +00:00
|
|
|
if (!this._isOpen)
|
|
|
|
return;
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
if (this._bg) {
|
|
|
|
Htk.Toast.popTop();
|
|
|
|
Vn.Node.remove(this._bg);
|
|
|
|
Vn.Node.removeClass(this._node, 'modal');
|
2016-05-04 14:36:51 +00:00
|
|
|
this._bg = null;
|
2022-05-26 06:08:31 +00:00
|
|
|
} else
|
|
|
|
this.doc.removeEventListener('mousedown', this._bgMouseDownHandler);
|
2016-05-04 14:36:51 +00:00
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
Vn.Node.remove(this._node);
|
2015-11-09 08:14:33 +00:00
|
|
|
this._parent = null;
|
2015-12-15 15:22:46 +00:00
|
|
|
this._isOpen = false;
|
2022-05-30 01:30:33 +00:00
|
|
|
this.emit('closed');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,_bgMouseDown: function(e) {
|
2016-05-04 14:36:51 +00:00
|
|
|
if (e !== this._lastEvent)
|
2022-05-26 06:08:31 +00:00
|
|
|
this.hide();
|
2016-05-04 14:36:51 +00:00
|
|
|
|
|
|
|
this._lastEvent = null;
|
|
|
|
}
|
|
|
|
|
2022-05-26 06:08:31 +00:00
|
|
|
,_onMouseDown: function(e) {
|
2016-05-04 14:36:51 +00:00
|
|
|
this._lastEvent = e;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
2015-12-15 15:22:46 +00:00
|
|
|
|