hedera-web/js/htk/toast.js

118 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-03-06 23:33:54 +00:00
/**
* Class to show toast messages.
**/
2016-09-26 09:28:47 +00:00
module.exports =
2015-08-17 18:02:14 +00:00
{
maxMessages: 6
,timeout: 10 /* Seconds */
,_container: null
,_timeouts: null
2015-03-06 23:33:54 +00:00
2015-08-17 18:02:14 +00:00
/**
* Shows a normal toast message.
*
* @param String message The message text
**/
,showMessage: function (message)
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
this._showText (message, 'message');
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
/**
* Shows a warning toast message.
*
* @param String message The message text
**/
,showWarning: function (message)
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
this._showText (message, 'warning');
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
/**
* Shows an error toast message.
*
* @param String message The message text
**/
,showError: function (message)
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
this._showText (message, 'error');
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
/**
* Hides all currently displayed toast messages.
**/
,hide: function ()
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
if (!this._container)
return;
if (this._timeouts)
for (var i = 0; i < this._timeouts.length; i++)
clearTimeout (this._timeouts[i]);
this._timeouts = null;
document.removeEventListener ('mousedown', this.hideHandler);
Vn.Node.remove (this._container);
this._container = null;
this.nodes = [];
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
,_createContainer: function ()
{
if (this._container)
return;
2015-03-06 23:33:54 +00:00
2015-08-17 18:02:14 +00:00
var container = document.createElement ('div');
container.className = 'htk-toast';
document.body.appendChild (container);
this.hideHandler = this.hide.bind (this);
document.addEventListener ('mousedown', this.hideHandler);
this._timeouts = [];
this._container = container;
}
,_showText: function (message, className)
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
this._createContainer ();
if (this._timeouts.length >= this.maxMessages)
this._onMessageTimeout ();
var node = document.createElement ('div');
node.className = className;
node.addEventListener ('mousedown', this._onMessageMouseDown);
var textNode = document.createTextNode (message);
node.appendChild (textNode);
this._container.appendChild (node);
var timeoutId = setTimeout (this._onMessageTimeout.bind (this), this.timeout * 1000);
this._timeouts.push (timeoutId);
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
,_onMessageTimeout: function ()
2015-03-06 23:33:54 +00:00
{
2015-08-17 18:02:14 +00:00
if (!this._container)
return;
var nodes = this._container.childNodes;
if (nodes.length > 0)
2015-03-15 12:44:57 +00:00
{
2015-08-17 18:02:14 +00:00
clearTimeout (this._timeouts.shift ());
Vn.Node.remove (nodes[0]);
2015-03-15 12:44:57 +00:00
}
2015-08-17 18:02:14 +00:00
if (nodes.length == 0)
this.hide ();
2015-03-06 23:33:54 +00:00
}
2015-08-17 18:02:14 +00:00
,_onMessageMouseDown: function (event)
2015-03-06 23:33:54 +00:00
{
event.stopPropagation ();
}
2015-08-17 18:02:14 +00:00
};