2011-08-25 17:54:15 +02:00
|
|
|
/**
|
2013-04-13 21:00:13 +02:00
|
|
|
* EGroupware eTemplate2 - JS Dynheight object
|
2011-08-25 17:54:15 +02:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package etemplate
|
|
|
|
* @subpackage api
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Andreas Stöckel
|
|
|
|
* @copyright Stylite 2011
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*egw:use
|
2016-06-06 17:38:20 +02:00
|
|
|
/vendor/bower-asset/jquery/dist/jquery.js;
|
2011-08-25 17:54:15 +02:00
|
|
|
et2_core_inheritance;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Object which resizes an inner node to the maximum extend of an outer node
|
|
|
|
* (without creating a scrollbar) - it achieves that by performing some very
|
|
|
|
* nasty and time consuming calculations.
|
|
|
|
*/
|
2020-02-12 22:49:22 +01:00
|
|
|
export class et2_dynheight
|
2013-04-13 21:00:13 +02:00
|
|
|
{
|
2020-02-12 22:49:22 +01:00
|
|
|
private initialized: boolean = false;
|
|
|
|
|
|
|
|
private outerNode: JQuery;
|
|
|
|
private innerNode: JQuery;
|
|
|
|
private minHeight: number = 0;
|
|
|
|
|
|
|
|
private bottomNodes: any[] = [];
|
|
|
|
private innerMargin: number = 0;
|
|
|
|
private outerMargin: number = 0;
|
|
|
|
|
|
|
|
constructor(_outerNode, _innerNode, _minHeight)
|
|
|
|
{
|
2016-06-02 16:51:15 +02:00
|
|
|
this.outerNode = jQuery(_outerNode);
|
|
|
|
this.innerNode = jQuery(_innerNode);
|
2011-08-25 17:54:15 +02:00
|
|
|
this.minHeight = _minHeight;
|
2020-02-12 22:49:22 +01:00
|
|
|
}
|
2011-08-25 17:54:15 +02:00
|
|
|
|
2020-02-24 23:15:24 +01:00
|
|
|
destroy()
|
|
|
|
{
|
|
|
|
this.outerNode = null;
|
|
|
|
this.innerNode = null;
|
|
|
|
this.bottomNodes = [];
|
|
|
|
}
|
|
|
|
|
2011-08-25 17:54:15 +02:00
|
|
|
/**
|
|
|
|
* Resizes the inner node. When this is done, the callback function is
|
|
|
|
* called.
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
|
|
|
* @param {function} _callback
|
|
|
|
* @param {object} _context
|
2011-08-25 17:54:15 +02:00
|
|
|
*/
|
2020-02-12 22:49:22 +01:00
|
|
|
update( _callback, _context)
|
|
|
|
{
|
2011-08-25 17:54:15 +02:00
|
|
|
// Check whether the inner node is actually visible - if not, don't
|
|
|
|
// trigger the callback function
|
|
|
|
if (this.innerNode.is(":visible"))
|
|
|
|
{
|
|
|
|
// Initialize the height calculation
|
|
|
|
this._initialize();
|
|
|
|
|
2013-02-22 17:01:28 +01:00
|
|
|
// Get the outer container height and offset, if available
|
2020-02-12 22:49:22 +01:00
|
|
|
const oh = this.outerNode.height();
|
|
|
|
const ot = this.outerNode.offset() ? this.outerNode.offset().top : 0;
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
// Get top and height of the inner node
|
2020-02-12 22:49:22 +01:00
|
|
|
const it = this.innerNode.offset().top;
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
// Calculate the height of the "bottomNodes"
|
2020-02-12 22:49:22 +01:00
|
|
|
let bminTop = this.bottomNodes.length ? Infinity : 0;
|
|
|
|
let bmaxBot = 0;
|
|
|
|
for (let i = 0; i < this.bottomNodes.length; i++)
|
2011-08-25 17:54:15 +02:00
|
|
|
{
|
2013-09-05 23:51:47 +02:00
|
|
|
// Ignore hidden popups
|
|
|
|
if(this.bottomNodes[i].find('.action_popup').length)
|
|
|
|
{
|
|
|
|
egw.debug('warn', "Had to skip a hidden popup - it should be removed",
|
|
|
|
this.bottomNodes[i].find('.action_popup')
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-19 19:06:53 +02:00
|
|
|
// Ignore other hidden nodes
|
|
|
|
if(!this.bottomNodes[i].is(':visible')) continue;
|
|
|
|
|
2011-08-25 17:54:15 +02:00
|
|
|
// Get height, top and bottom and calculate the maximum/minimum
|
2020-02-12 22:49:22 +01:00
|
|
|
let bh = this.bottomNodes[i].outerHeight(true);
|
|
|
|
let bt = this.bottomNodes[i].offset().top;
|
|
|
|
const bb = bh + bt;
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
if (i == 0 || bminTop > bt)
|
|
|
|
{
|
|
|
|
bminTop = bt;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == 0 || bmaxBot < bb)
|
|
|
|
{
|
|
|
|
bmaxBot = bb;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the height of the bottom container
|
2020-02-12 22:49:22 +01:00
|
|
|
const bh = Math.max(0, bmaxBot - bminTop);
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
// Calculate the new height of the inner container
|
2020-02-12 22:49:22 +01:00
|
|
|
const h = Math.max(this.minHeight, oh + ot - it - bh -
|
2013-10-03 14:53:58 +02:00
|
|
|
this.innerMargin - this.outerMargin);
|
|
|
|
this.innerNode.height(h);
|
2014-03-20 10:40:37 +01:00
|
|
|
|
2013-10-03 14:53:58 +02:00
|
|
|
// Update the width
|
2013-10-02 14:43:30 +02:00
|
|
|
// Some checking to make sure it doesn't overflow the width when user
|
|
|
|
// resizes the window
|
2020-02-12 22:49:22 +01:00
|
|
|
let w = this.outerNode.width();
|
2016-06-02 16:51:15 +02:00
|
|
|
if (w > jQuery(window).width())
|
2013-10-02 14:43:30 +02:00
|
|
|
{
|
|
|
|
// 50px border, totally arbitrary, but we just need to make sure it's inside
|
2016-06-02 16:51:15 +02:00
|
|
|
w = jQuery(window).width()-50;
|
2013-10-02 14:43:30 +02:00
|
|
|
}
|
2013-10-03 14:53:58 +02:00
|
|
|
if(w != this.innerNode.outerWidth())
|
|
|
|
{
|
|
|
|
this.innerNode.width(w);
|
|
|
|
}
|
2014-03-20 10:40:37 +01:00
|
|
|
|
2011-08-25 17:54:15 +02:00
|
|
|
// Call the callback function
|
|
|
|
if (typeof _callback != "undefined")
|
|
|
|
{
|
2011-08-26 11:58:25 +02:00
|
|
|
_callback.call(_context, w, h);
|
2011-08-25 17:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
2020-02-12 22:49:22 +01:00
|
|
|
}
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function used internally which collects all DOM-Nodes which are located
|
|
|
|
* below this element.
|
2014-03-20 10:40:37 +01:00
|
|
|
*
|
2020-02-12 22:49:22 +01:00
|
|
|
* @param {HTMLElement} _node
|
2014-03-20 10:40:37 +01:00
|
|
|
* @param {number} _bottom
|
2011-08-25 17:54:15 +02:00
|
|
|
*/
|
2020-02-12 22:49:22 +01:00
|
|
|
_collectBottomNodes( _node : any, _bottom? : number)
|
|
|
|
{
|
2011-08-25 17:54:15 +02:00
|
|
|
// Calculate the bottom position of the inner node
|
|
|
|
if (typeof _bottom == "undefined")
|
|
|
|
{
|
|
|
|
_bottom = this.innerNode.offset().top + this.innerNode.height();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_node)
|
|
|
|
{
|
|
|
|
// Accumulate the outer margin of the parent elements
|
2020-02-12 22:49:22 +01:00
|
|
|
const node = jQuery(_node);
|
|
|
|
const ooh = node.outerHeight(true);
|
|
|
|
const oh = node.height();
|
2011-08-25 17:54:15 +02:00
|
|
|
this.outerMargin += (ooh - oh) / 2; // Divide by 2 as the value contains margin-top and -bottom
|
|
|
|
|
|
|
|
// Iterate over the children of the given node and do the same
|
|
|
|
// recursively to the parent nodes until the _outerNode or body is
|
|
|
|
// reached.
|
2020-02-12 22:49:22 +01:00
|
|
|
const self = this;
|
2016-06-02 16:51:15 +02:00
|
|
|
jQuery(_node).children().each(function() {
|
2020-02-12 22:49:22 +01:00
|
|
|
const $this = jQuery(this);
|
|
|
|
const top = $this.offset().top;
|
2013-04-10 17:17:18 +02:00
|
|
|
if (this != self.innerNode[0] && top >= _bottom)
|
2011-08-25 17:54:15 +02:00
|
|
|
{
|
|
|
|
self.bottomNodes.push($this);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-06-02 16:51:15 +02:00
|
|
|
if (_node != this.outerNode[0] && _node != jQuery("body")[0])
|
2011-08-25 17:54:15 +02:00
|
|
|
{
|
|
|
|
this._collectBottomNodes(_node.parentNode, _bottom);
|
|
|
|
}
|
|
|
|
}
|
2020-02-12 22:49:22 +01:00
|
|
|
}
|
2011-08-25 17:54:15 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Used internally to calculate some information which will not change over
|
|
|
|
* the time.
|
|
|
|
*/
|
2020-02-12 22:49:22 +01:00
|
|
|
_initialize( )
|
|
|
|
{
|
2011-08-25 17:54:15 +02:00
|
|
|
if (!this.initialized)
|
|
|
|
{
|
|
|
|
// Collect all bottomNodes and calculates the outer margin
|
|
|
|
this.bottomNodes = [];
|
|
|
|
this.outerMargin = 0;
|
|
|
|
this._collectBottomNodes(this.innerNode[0].parentNode);
|
|
|
|
|
|
|
|
// Calculate the inner margin
|
2020-02-12 22:49:22 +01:00
|
|
|
const ioh = this.innerNode.outerHeight(true);
|
|
|
|
const ih = this.innerNode.height();
|
2011-08-25 17:54:15 +02:00
|
|
|
this.innerMargin = ioh - ih;
|
|
|
|
|
|
|
|
this.initialized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 22:49:22 +01:00
|
|
|
}
|