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.
|
2016-12-20 09:32:17 +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.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-12-15 15:22:46 +00:00
|
|
|
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;
|
2016-10-20 08:37:08 +00:00
|
|
|
this._setChildNode (x.node);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this._child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* The popup child Node.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-12-15 15:22:46 +00:00
|
|
|
,childNode:
|
|
|
|
{
|
|
|
|
type: Object
|
|
|
|
,set: function (x)
|
|
|
|
{
|
|
|
|
this._child = null;
|
2016-10-20 08:37:08 +00:00
|
|
|
this._setChildNode (x);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
,get: function ()
|
|
|
|
{
|
|
|
|
return this.node.firstChild;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Indicates how the dialog must be displayed.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2015-12-15 15:22:46 +00:00
|
|
|
,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
|
2017-03-22 16:57:21 +00:00
|
|
|
,_fitParent: false
|
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
|
|
|
{
|
2016-05-04 14:36:51 +00:00
|
|
|
this._bgMouseDownHandler = this._bgMouseDown.bind (this);
|
2017-03-22 16:57:21 +00:00
|
|
|
this._bgKeyPressHandler = this._bgKeyPress.bind (this);
|
2015-11-09 08:14:33 +00:00
|
|
|
this.parent (props);
|
2015-03-06 23:33:54 +00:00
|
|
|
}
|
2016-10-16 14:16:08 +00:00
|
|
|
|
|
|
|
,render: function ()
|
|
|
|
{
|
|
|
|
var div = this.createRoot ('div');
|
|
|
|
div.className = 'htk-popup';
|
|
|
|
}
|
2015-03-06 23:33:54 +00:00
|
|
|
|
2016-10-20 08:37:08 +00:00
|
|
|
,_setChildNode: function (childNode)
|
2015-03-06 23:33:54 +00:00
|
|
|
{
|
|
|
|
Vn.Node.removeChilds (this.node);
|
|
|
|
this.node.appendChild (childNode);
|
|
|
|
}
|
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
/**
|
|
|
|
* Shows the popup relative to another element.
|
|
|
|
*
|
|
|
|
* @param {Node} parent The relative element
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {boolean} fitParent Wether to set the width same to the parent
|
2017-03-22 16:57:21 +00:00
|
|
|
* @param {HTMLEvent} ignoreEvent An optional event object to ignore
|
|
|
|
*/
|
|
|
|
,show: function (parent, fitParent, ignoreEvent)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2015-11-09 08:14:33 +00:00
|
|
|
this._parent = parent;
|
2017-03-22 16:57:21 +00:00
|
|
|
this._fitParent = fitParent;
|
|
|
|
this.open (ignoreEvent);
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2015-07-21 14:16:07 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
/**
|
|
|
|
* Opens the popup.
|
|
|
|
*
|
|
|
|
* @param {Node} parent The relative element
|
2017-04-21 10:53:15 +00:00
|
|
|
* @param {boolean} fitParent Wether to set the width same to the parent
|
2017-03-22 16:57:21 +00:00
|
|
|
*/
|
|
|
|
,open: function (ignoreEvent)
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
if (ignoreEvent)
|
|
|
|
this._ignoreEvent = ignoreEvent;
|
|
|
|
|
2016-05-02 13:05:49 +00:00
|
|
|
if (this._isOpen)
|
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
this.resetAsync ();
|
2016-05-02 13:05:49 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-03-22 16:57:21 +00:00
|
|
|
|
|
|
|
var node = this.node;
|
2016-05-02 13:05:49 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
node.addEventListener ('mousedown', this._onPopupMouseDown.bind (this));
|
|
|
|
this.doc.addEventListener ('keypress', this._bgKeyPressHandler);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2015-12-15 15:22:46 +00:00
|
|
|
if (this.isModal ())
|
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
var bg = this._bg = this.createElement ('div');
|
2016-05-04 14:36:51 +00:00
|
|
|
bg.className = 'htk-background';
|
|
|
|
bg.addEventListener ('mousedown', this._bgMouseDownHandler);
|
2016-10-30 22:48:18 +00:00
|
|
|
Htk.Toast.pushTop (bg);
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
Vn.Node.addClass (node, 'modal');
|
|
|
|
bg.appendChild (node);
|
2016-05-04 14:36:51 +00:00
|
|
|
|
2016-10-16 14:16:08 +00:00
|
|
|
this.doc.body.appendChild (bg);
|
2016-05-04 14:36:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
this.doc.addEventListener ('mousedown', this._bgMouseDownHandler);
|
2017-03-22 16:57:21 +00:00
|
|
|
this.doc.body.appendChild (node);
|
|
|
|
Vn.Node.addClass (node, 'fixed');
|
|
|
|
this.resetAsync ();
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
2017-03-22 16:57:21 +00:00
|
|
|
|
|
|
|
setTimeout (this._onOpacityTimeout.bind (this), 10);
|
2015-12-15 15:22:46 +00:00
|
|
|
this._isOpen = true;
|
2017-03-22 16:57:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns if the popup window shoud be modal based on the modal property
|
|
|
|
* and if the device is mobile.
|
|
|
|
*
|
2017-04-21 10:53:15 +00:00
|
|
|
* @return {boolean} %true if it's modal, %false otherwise
|
2017-03-22 16:57:21 +00:00
|
|
|
*/
|
|
|
|
,isModal: function ()
|
|
|
|
{
|
|
|
|
return this._modal || Vn.isMobile () || !this._parent;
|
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
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
if (!this._isOpen)
|
|
|
|
return;
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
if (this._bg)
|
|
|
|
this._bg.style.opacity = 1;
|
2017-03-22 16:57:21 +00:00
|
|
|
|
|
|
|
this._node.style.opacity = 1;
|
2015-12-15 15:22:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
,_onResetTimeout: function ()
|
|
|
|
{
|
|
|
|
this.reset ();
|
|
|
|
}
|
2017-03-22 16:57:21 +00:00
|
|
|
|
|
|
|
,resetAsync: function ()
|
|
|
|
{
|
|
|
|
setTimeout (this._onResetTimeout.bind (this));
|
|
|
|
}
|
2015-07-07 15:27:47 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
/**
|
|
|
|
* Repositions the popup.
|
|
|
|
*/
|
2015-12-15 15:22:46 +00:00
|
|
|
,reset: function ()
|
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
if (!this._isOpen || this.isModal ())
|
2015-12-15 15:22:46 +00:00
|
|
|
return;
|
2017-03-22 16:57:21 +00:00
|
|
|
|
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;
|
2017-03-22 16:57:21 +00:00
|
|
|
style.visibility = 'hidden';
|
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;
|
2017-03-22 16:57:21 +00:00
|
|
|
|
|
|
|
var iniWidth;
|
|
|
|
var iniHeight;
|
|
|
|
var width = iniWidth = node.offsetWidth;
|
|
|
|
var height = iniHeight = node.offsetHeight;
|
2015-12-15 15:22:46 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
var windowWidth = window.innerWidth;
|
|
|
|
var windowHeight = window.innerHeight;
|
|
|
|
|
|
|
|
if (width + dblMargin > windowWidth)
|
|
|
|
width = windowWidth - dblMargin;
|
|
|
|
if (height + dblMargin > windowHeight)
|
|
|
|
height = windowHeight - dblMargin;
|
|
|
|
|
|
|
|
var rect = this._parent.getBoundingClientRect ();
|
|
|
|
var left = rect.left;
|
|
|
|
var top = rect.top + this._parent.offsetHeight;
|
|
|
|
|
|
|
|
if (this._fitParent)
|
|
|
|
width = this._parent.offsetWidth;
|
|
|
|
|
|
|
|
if (left + width + margin > windowWidth)
|
|
|
|
left -= (left + width) - window.windowWidth + margin;
|
|
|
|
if (top + height + margin > windowHeight)
|
|
|
|
top -= height + this._parent.offsetHeight;
|
|
|
|
|
|
|
|
if (left < margin)
|
|
|
|
left = margin;
|
|
|
|
if (top < margin)
|
|
|
|
top = margin;
|
|
|
|
|
|
|
|
style.top = (top) +'px';
|
|
|
|
style.left = (left) +'px';
|
|
|
|
|
|
|
|
if (width != iniWidth)
|
|
|
|
style.width = (width) +'px';
|
|
|
|
if (height != iniHeight)
|
|
|
|
style.height = (height) +'px';
|
|
|
|
|
|
|
|
style.visibility = 'initial';
|
2015-07-07 15:27:47 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
/**
|
|
|
|
* Hides the popup.
|
|
|
|
*/
|
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;
|
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
var node = this._node;
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
if (this._bg)
|
2015-12-15 15:22:46 +00:00
|
|
|
{
|
2016-10-30 22:48:18 +00:00
|
|
|
Htk.Toast.popTop ();
|
2016-05-04 14:36:51 +00:00
|
|
|
Vn.Node.remove (this._bg);
|
2017-03-22 16:57:21 +00:00
|
|
|
Vn.Node.removeClass (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
|
2017-03-22 16:57:21 +00:00
|
|
|
{
|
2016-10-16 14:16:08 +00:00
|
|
|
this.doc.removeEventListener ('mousedown', this._bgMouseDownHandler);
|
2017-03-22 16:57:21 +00:00
|
|
|
Vn.Node.removeClass (node, 'fixed');
|
|
|
|
}
|
2016-05-04 14:36:51 +00:00
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
Vn.Node.remove (node);
|
2015-11-09 08:14:33 +00:00
|
|
|
this._parent = null;
|
2015-12-15 15:22:46 +00:00
|
|
|
this._isOpen = false;
|
2017-04-19 06:16:37 +00:00
|
|
|
this.emit ('closed');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 14:36:51 +00:00
|
|
|
,_bgMouseDown: function (e)
|
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
if (e !== this._ignoreEvent)
|
2016-05-04 14:36:51 +00:00
|
|
|
this.hide ();
|
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
this._ignoreEvent = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
,_bgKeyPress: function (e)
|
|
|
|
{
|
|
|
|
if (e.keyCode == 27) // Escape
|
|
|
|
this.hide ();
|
2016-05-04 14:36:51 +00:00
|
|
|
}
|
|
|
|
|
2017-03-22 16:57:21 +00:00
|
|
|
,_onPopupMouseDown: function (e)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-03-22 16:57:21 +00:00
|
|
|
this._ignoreEvent = e;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
});
|
2015-12-15 15:22:46 +00:00
|
|
|
|