2018-02-12 16:01:20 +00:00
|
|
|
/**
|
|
|
|
* @author Richard Davey <rich@photonstorm.com>
|
2019-01-15 16:20:22 +00:00
|
|
|
* @copyright 2019 Photon Storm Ltd.
|
2018-02-12 16:01:20 +00:00
|
|
|
* @license {@link https://github.com/photonstorm/phaser/blob/master/license.txt|MIT License}
|
|
|
|
*/
|
|
|
|
|
2017-10-12 14:09:52 +00:00
|
|
|
/**
|
2018-01-26 03:40:49 +00:00
|
|
|
* Adds the given element to the DOM. If a parent is provided the element is added as a child of the parent, providing it was able to access it.
|
2018-10-10 12:41:55 +00:00
|
|
|
* If no parent was given it falls back to using `document.body`.
|
2017-10-12 14:09:52 +00:00
|
|
|
*
|
2018-02-13 00:40:51 +00:00
|
|
|
* @function Phaser.DOM.AddToDOM
|
2017-10-12 14:09:52 +00:00
|
|
|
* @since 3.0.0
|
|
|
|
*
|
2018-03-19 15:29:26 +00:00
|
|
|
* @param {HTMLElement} element - The element to be added to the DOM. Usually a Canvas object.
|
2018-03-20 14:58:02 +00:00
|
|
|
* @param {(string|HTMLElement)} [parent] - The parent in which to add the element. Can be a string which is passed to `getElementById` or an actual DOM object.
|
2018-04-08 19:13:02 +00:00
|
|
|
* @param {boolean} [overflowHidden=true] - Whether or not to hide overflowing content inside the parent.
|
2017-10-12 14:09:52 +00:00
|
|
|
*
|
2018-03-19 15:29:26 +00:00
|
|
|
* @return {HTMLElement} The element that was added to the DOM.
|
2017-10-12 14:09:52 +00:00
|
|
|
*/
|
2017-01-09 23:54:14 +00:00
|
|
|
var AddToDOM = function (element, parent, overflowHidden)
|
2016-11-25 02:08:33 +00:00
|
|
|
{
|
|
|
|
if (overflowHidden === undefined) { overflowHidden = true; }
|
|
|
|
|
|
|
|
var target;
|
|
|
|
|
|
|
|
if (parent)
|
|
|
|
{
|
|
|
|
if (typeof parent === 'string')
|
|
|
|
{
|
|
|
|
// Hopefully an element ID
|
|
|
|
target = document.getElementById(parent);
|
|
|
|
}
|
|
|
|
else if (typeof parent === 'object' && parent.nodeType === 1)
|
|
|
|
{
|
2018-10-09 17:13:56 +00:00
|
|
|
// Quick test for a HTMLElement
|
2016-11-25 02:08:33 +00:00
|
|
|
target = parent;
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 20:40:46 +00:00
|
|
|
else if (element.parentElement)
|
|
|
|
{
|
|
|
|
return element;
|
|
|
|
}
|
2016-11-25 02:08:33 +00:00
|
|
|
|
2018-10-09 17:13:56 +00:00
|
|
|
// Fallback, covers an invalid ID and a non HTMLElement object
|
2016-11-25 02:08:33 +00:00
|
|
|
if (!target)
|
|
|
|
{
|
|
|
|
target = document.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overflowHidden && target.style)
|
|
|
|
{
|
|
|
|
target.style.overflow = 'hidden';
|
|
|
|
}
|
|
|
|
|
|
|
|
target.appendChild(element);
|
|
|
|
|
|
|
|
return element;
|
2017-01-09 23:54:14 +00:00
|
|
|
};
|
2016-11-25 02:08:33 +00:00
|
|
|
|
|
|
|
module.exports = AddToDOM;
|