2016-09-26 09:28:47 +00:00
|
|
|
|
|
|
|
var Widget = require ('./widget');
|
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
2015-03-06 23:33:54 +00:00
|
|
|
* Class to handle popups.
|
2015-01-23 13:09:30 +00:00
|
|
|
**/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports = new Class
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
2016-09-26 09:28:47 +00:00
|
|
|
Extends: Widget
|
2015-03-06 23:33:54 +00:00
|
|
|
,Tag: 'htk-popup'
|
2015-12-15 15:22:46 +00:00
|
|
|
,Properties:
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The popup child.
|
|
|
|
**/
|
|
|
|
child:
|
|
|
|
{
|
2016-09-26 09:28:47 +00:00
|
|
|
type: Widget
|
2015-12-15 15:22:46 +00:00
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._child = x;
|
|
|
|
this.setChildNode (x.getNode ());
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The popup child Node.
|
|
|
|
**/
|
|
|
|
,childNode:
|
|
|
|
{
|
|
|
|
type: Object
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._child = null;
|
|
|
|
this.setChildNode (x);
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this.node.firstChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Indicates how the dialog must be displayed.
|
|
|
|
**/
|
|
|
|
,modal:
|
|
|
|
{
|
|
|
|
type: Boolean
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._modal = x;
|
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,initialize: function (props)
|
2015-03-06 23:33:54 +00:00
|
|
|
{
|
2015-11-09 08:14:33 +00:00
|
|
|
var div = this.createElement ('div');
|
|
|
|
div.className = 'htk-popup';
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
this._bgMouseDownHandler = this._bgMouseDown.bind (this);
|
2015-11-09 08:14:33 +00:00
|
|
|
this.parent (props);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,setChild: function (child)
|
|
|
|
{
|
|
|
|
this.setChildNode (child.getNode ());
|
|
|
|
}
|
|
|
|
|
|
|
|
,setChildNode: function (childNode)
|
|
|
|
{
|
|
|
|
Vn.Node.removeChilds (this.node);
|
|
|
|
this.node.appendChild (childNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
,show: function (parent)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-11-09 08:14:33 +00:00
|
|
|
this._parent = parent;
|
2015-12-15 15:22:46 +00:00
|
|
|
this.open ();
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2015-12-15 15:22:46 +00:00
|
|
|
|
|
|
|
,isModal: function ()
|
2015-03-06 23:33:54 +00:00
|
|
|
{
|
2015-12-15 15:22:46 +00:00
|
|
|
return this._modal || Vn.isMobile ();
|
|
|
|
}
|
2015-07-21 14:16:07 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
,open: function ()
|
|
|
|
{
|
2016-05-02 13:05:49 +00:00
|
|
|
if (this._isOpen)
|
|
|
|
{
|
|
|
|
this.reset ();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
this.node.addEventListener ('mousedown', this._onMouseDown.bind (this));
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (this.isModal ())
|
|
|
|
{
|
2016-05-04 14:36:51 +00:00
|
|
|
var bg = this._bg = document.createElement ('div');
|
|
|
|
bg.className = 'htk-background';
|
|
|
|
bg.addEventListener ('mousedown', this._bgMouseDownHandler);
|
2015-12-15 15:22:46 +00:00
|
|
|
|
|
|
|
Vn.Node.addClass (this.node, 'modal');
|
2016-05-04 14:36:51 +00:00
|
|
|
bg.appendChild (this.node);
|
|
|
|
|
|
|
|
document.body.appendChild (bg);
|
|
|
|
setTimeout (this._onOpacityTimeout.bind (this), 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
document.addEventListener ('mousedown', this._bgMouseDownHandler);
|
|
|
|
document.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;
|
|
|
|
this.reset ();
|
2015-12-19 15:42:38 +00:00
|
|
|
setTimeout (this._onResetTimeout.bind (this), 0);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
,_onOpacityTimeout: function ()
|
2015-07-07 15:27:47 +00:00
|
|
|
{
|
2016-05-04 14:36:51 +00:00
|
|
|
if (this._bg)
|
|
|
|
this._bg.style.opacity = 1;
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_onResetTimeout: function ()
|
|
|
|
{
|
|
|
|
this.reset ();
|
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
,reset: function ()
|
|
|
|
{
|
|
|
|
if (!this._isOpen)
|
|
|
|
return;
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
var style = this.node.style;
|
|
|
|
style.height = '';
|
|
|
|
style.width = '';
|
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
var margin = 20;
|
|
|
|
var dblMargin = margin * 2;
|
2015-07-07 15:27:47 +00:00
|
|
|
var width = this.node.offsetWidth;
|
|
|
|
var height = this.node.offsetHeight;
|
2015-12-15 15:22:46 +00:00
|
|
|
var innerWidth = Vn.Browser.getInnerWidth ();
|
|
|
|
var innerHeight = Vn.Browser.getInnerHeight ();
|
|
|
|
|
|
|
|
if (width + dblMargin > innerWidth)
|
|
|
|
{
|
|
|
|
width = innerWidth - dblMargin;
|
|
|
|
style.width = width +'px';
|
|
|
|
}
|
|
|
|
if (height + dblMargin > innerHeight)
|
|
|
|
{
|
|
|
|
height = innerHeight - dblMargin;
|
|
|
|
style.height = height +'px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.isModal ())
|
|
|
|
{
|
|
|
|
style.top = '50%';
|
|
|
|
style.left = '50%';
|
|
|
|
|
|
|
|
style.marginLeft = (-this.node.offsetWidth / 2) +'px';
|
|
|
|
style.marginTop = (-this.node.offsetHeight / 2) +'px';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
var spacing = 4;
|
|
|
|
var rect = this._parent.getBoundingClientRect ();
|
|
|
|
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)
|
|
|
|
left -= (left + width) - Vn.Browser.getInnerWidth () + margin;
|
|
|
|
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
|
|
|
|
2015-03-06 23:33:54 +00:00
|
|
|
,hide: function ()
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-12-15 15:22:46 +00:00
|
|
|
if (!this._isOpen)
|
|
|
|
return;
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
if (this._bg)
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2016-05-04 14:36:51 +00:00
|
|
|
Vn.Node.remove (this._bg);
|
2015-12-15 15:22:46 +00:00
|
|
|
Vn.Node.removeClass (this.node, 'modal');
|
2016-05-04 14:36:51 +00:00
|
|
|
this._bg = null;
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2016-05-04 14:36:51 +00:00
|
|
|
else
|
|
|
|
document.removeEventListener ('mousedown', this._bgMouseDownHandler);
|
|
|
|
|
2015-07-07 15:27:47 +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;
|
2015-01-23 13:09:30 +00:00
|
|
|
this.signalEmit ('closed');
|
|
|
|
}
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
,_bgMouseDown: function (e)
|
|
|
|
{
|
|
|
|
if (e !== this._lastEvent)
|
|
|
|
this.hide ();
|
|
|
|
|
|
|
|
this._lastEvent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
,_onMouseDown: function (e)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
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
|
|
|
|