editor.js/src/components/dom.js
Peter Savchenko c1afcf0205
Toolbar, Toolbox, UI (#239)
* Toolbox making

* Add Toolbox buttons click handler

* Toolbar, Toolbox, UI

* Updates

* update css prefix
2017-12-24 15:35:05 +03:00

101 lines
No EOL
2.1 KiB
JavaScript

/**
* DOM manupulations helper
*/
export default class Dom {
/**
* Helper for making Elements with classname and attributes
*
* @param {string} tagName - new Element tag name
* @param {array|string} classNames - list or name of CSS classname(s)
* @param {Object} attributes - any attributes
* @return {Element}
*/
static make(tagName, classNames = null, attributes = {}) {
var el = document.createElement(tagName);
if ( Array.isArray(classNames) ) {
el.classList.add(...classNames);
} else if( classNames ) {
el.classList.add(classNames);
}
for (let attrName in attributes) {
el[attrName] = attributes[attrName];
}
return el;
}
/**
* Append one or several elements to the parent
*
* @param {Element} parent - where to append
* @param {Element|Element[]} - element ore elements list
*/
static append(parent, elements) {
if ( Array.isArray(elements) ) {
elements.forEach( el => parent.appendChild(el) );
} else {
parent.appendChild(elements);
}
}
/**
* Selector Decorator
*
* Returns first match
*
* @param {Element} el - element we searching inside. Default - DOM Document
* @param {String} selector - searching string
*
* @returns {Element}
*/
static find(el = document, selector) {
return el.querySelector(selector);
}
/**
* Selector Decorator.
*
* Returns all matches
*
* @param {Element} el - element we searching inside. Default - DOM Document
* @param {String} selector - searching string
* @returns {NodeList}
*/
static findAll(el = document, selector) {
return el.querySelectorAll(selector);
}
/**
* Check if object is DOM node
*
* @param {Object} node
* @returns {boolean}
*/
static isElement(node) {
return node && typeof node === 'object' && node.nodeType && node.nodeType === Node.ELEMENT_NODE;
}
};