/* global Node, DocumentFragment */
'use strict';
var elements = require('../elements');
elements.disposeElements = /** @function
* @param _elements */function (_elements) {
Array.prototype.forEach.call(_elements, elements.disposeElement);
};
/**
* Dispose an element
* @param {HTMLElement} element
*/
elements.disposeElement = /** @function
* @param element */function (element) {
element.dispatchEvent(new Event('dispose'));
};
/**
* Completely removes element from the document
* @param {HTMLElement} element
*/
elements.remove = /** @function
* @param element */function (element) {
if (element instanceof DocumentFragment) {
while (element.firstChild) {
elements.remove(element.firstChild);
}
return;
}
elements.disposeElements(element.getElementsByTagName('*'));
elements.disposeElement(element);
if (element.parentNode) {
element.parentNode.removeChild(element);
}
};
/**
* Remove element's children
* @param {HTMLElement} element
*/
elements.empty = /** @function
* @param element */function (element) {
Eif (element.nodeType == Node.ELEMENT_NODE) {
elements.disposeElements(element.getElementsByTagName('*'));
}
while (element.firstChild) {
element.removeChild(element.firstChild);
}
};
/**
* Empty the element and append a text node
* @param {HTMLElement} element
* @param {String} content
*/
elements.text = /** @function
* @param element
* @param content */function (element, content) {
elements.empty(element);
elements.appendText(element, content);
};
/**
* Empty the element and set inner HTML
* @param {HTMLElement} element
* @param {String} content html content
*/
elements.html = /** @function
* @param element
* @param content */function (element, content) {
elements.empty(element);
element.innerHTML = content;
};
/**
* Apppend a text node
* @param {HTMLElement} element
* @param {String} content
*/
elements.appendText = /** @function
* @param element
* @param content */function (element, content) {
elements.append(element, elements.document(element).createTextNode(content));
};
/**
* Prepend a text node
* @param {HTMLElement} element
* @param {String} content
*/
elements.prependText = /** @function
* @param element
* @param content */function (element, content) {
elements.prepend(element, elements.document(element).createTextNode(content));
};
/**
* Append a child to an element
* @param {HTMLElement} element
* @param {Node} child
*/
elements.append = /** @function
* @param element
* @param child */function (element, child) {
element.appendChild(child);
};
/**
* Prepend a child to an element
* @param {HTMLElement} element
* @param {Node} child
*/
elements.prepend = /** @function
* @param element
* @param child */function (element, child) {
if (element.firstChild) {
element.insertBefore(child, element.firstChild);
} else {
element.appendChild(child);
}
};
/**
* Insert a new sibling before an element
* @param {HTMLElement} element
* @param {Node} newSibling
*/
elements.before = /** @function
* @param element
* @param newSibling */function (element, newSibling) {
element.parentNode.insertBefore(newSibling, element);
};
/**
* Insert a new sibling after an element
* @param {HTMLElement} element
* @param {Node} newSibling
*/
elements.after = /** @function
* @param element
* @param newSibling */function (element, newSibling) {
var parent = element.parentNode;
if (parent.lastChild === element) {
parent.appendChild(newSibling);
} else {
// HTMLElement.insertBefore(child, existingChild)
parent.insertBefore(newSibling, element.nextSibling);
}
};
/**
* Insert a new sibling before an element
* @param {HTMLElement} element
* @param {Node} newSibling
*/
elements.insertBefore = /** @function
* @param element
* @param newSibling */function (element, newSibling) {
elements.before(newSibling, element);
};
/**
* Insert a new sibling after an element
* @param {HTMLElement} element
* @param {Node} newSibling
*/
elements.insertAfter = /** @function
* @param element
* @param newSibling */function (element, newSibling) {
elements.after(newSibling, element);
};
/**
* Append an element to a new parent
* @param {HTMLElement} element
* @param {Node} newParent
*/
elements.appendTo = /** @function
* @param element
* @param newParent */function (element, newParent) {
elements.append(newParent, element);
};
/**
* Prepend an element to a new parent
* @param {HTMLElement} element
* @param {Node} newParent
*/
elements.prependTo = /** @function
* @param element
* @param newParent */function (element, newParent) {
elements.prepend(newParent, element);
};
/**
* Replace an element's content by a new content
* @param {HTMLElement} element
* @param {Node} replaceElement
*/
elements.replaceWith = /** @function
* @param element
* @param replaceElement */function (element, replaceElement) {
elements.empty(element);
elements.append(element, replaceElement);
};
//# sourceMappingURL=dom_manipulation.js.map |