1
0
Fork 0
mirror of https://github.com/koalyptus/TableFilter.git synced 2024-05-27 11:02:51 +02:00
TableFilter/src/dom.js

169 lines
4.4 KiB
JavaScript
Raw Normal View History

2016-05-15 04:56:12 +02:00
import {isUndef} from './types';
2016-05-20 10:08:39 +02:00
import {trim} from './string';
2014-11-16 01:29:07 +01:00
/**
* DOM utilities
*/
2015-05-30 14:23:33 +02:00
export default {
/**
* Returns text + text of children of given node
* @param {NodeElement} node
* @return {String}
*/
2016-05-15 04:56:12 +02:00
getText(node) {
if (isUndef(node.textContent)) {
2016-05-20 10:08:39 +02:00
return trim(node.innerText);
}
2016-05-20 10:08:39 +02:00
return trim(node.textContent);
2015-05-30 14:23:33 +02:00
},
/**
* Returns the first text node contained in the supplied node
* @param {NodeElement} node node
* @return {String}
*/
2016-05-15 04:56:12 +02:00
getFirstTextNode(node) {
for (let i = 0; i < node.childNodes.length; i++) {
let n = node.childNodes[i];
2016-05-15 04:56:12 +02:00
if (n.nodeType === 3) {
return n.data;
}
}
},
2015-05-30 14:23:33 +02:00
/**
* Creates an html element with given collection of attributes
* @param {String} tag a string of the html tag to create
* @param {Array} an undetermined number of arrays containing the with 2
* items, the attribute name and its value ['id','myId']
* @return {Object} created element
*/
2016-05-15 04:56:12 +02:00
create(tag) {
if (!tag || tag === '') {
2015-05-30 14:23:33 +02:00
return;
}
2014-11-16 01:29:07 +01:00
2015-05-30 14:23:33 +02:00
let el = document.createElement(tag),
args = arguments;
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
if (args.length > 1) {
for (let i = 0; i < args.length; i++) {
2015-05-30 14:23:33 +02:00
let argtype = typeof args[i];
2016-05-15 04:56:12 +02:00
if (argtype === 'object' && args[i].length === 2) {
2015-05-30 14:23:33 +02:00
el.setAttribute(args[i][0], args[i][1]);
}
2014-11-16 01:29:07 +01:00
}
}
2015-05-30 14:23:33 +02:00
return el;
},
2015-12-05 14:37:59 +01:00
/**
* Removes passed node from DOM
* @param {DOMElement} node
* @return {DOMElement} old node reference
*/
2016-05-15 04:56:12 +02:00
remove(node) {
2015-12-05 14:37:59 +01:00
return node.parentNode.removeChild(node);
},
2015-05-30 14:23:33 +02:00
/**
* Returns a text node with given text
2015-06-27 16:47:13 +02:00
* @param {String} txt
2015-05-30 14:23:33 +02:00
* @return {Object}
*/
2016-05-15 04:56:12 +02:00
text(txt) {
2015-06-27 16:47:13 +02:00
return document.createTextNode(txt);
2015-05-30 14:23:33 +02:00
},
2016-05-15 04:56:12 +02:00
hasClass(ele, cls) {
if (!ele) { return false; }
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
if (supportsClassList()) {
2015-05-30 14:23:33 +02:00
return ele.classList.contains(cls);
}
2016-05-15 04:56:12 +02:00
return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
2015-05-30 14:23:33 +02:00
},
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
addClass(ele, cls) {
if (!ele) { return; }
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
if (supportsClassList()) {
2015-05-30 14:23:33 +02:00
ele.classList.add(cls);
return;
}
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
if (ele.className === '') {
2015-05-30 14:23:33 +02:00
ele.className = cls;
}
2016-05-15 04:56:12 +02:00
else if (!this.hasClass(ele, cls)) {
2015-07-25 10:33:21 +02:00
ele.className += ' ' + cls;
2015-05-30 14:23:33 +02:00
}
},
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
removeClass(ele, cls) {
if (!ele) { return; }
2014-11-16 01:29:07 +01:00
2016-05-15 04:56:12 +02:00
if (supportsClassList()) {
2015-05-30 14:23:33 +02:00
ele.classList.remove(cls);
return;
}
2016-05-15 04:56:12 +02:00
let reg = new RegExp('(\\s|^)' + cls + '(\\s|$)', 'g');
2015-05-30 14:23:33 +02:00
ele.className = ele.className.replace(reg, '');
},
/**
* Creates and returns an option element
* @param {String} text option text
* @param {String} value option value
* @param {Boolean} isSel whether option is selected
* @return {Object} option element
*/
2016-05-15 04:56:12 +02:00
createOpt(text, value, isSel) {
2015-05-30 14:23:33 +02:00
let isSelected = isSel ? true : false,
opt = isSelected ?
2016-05-15 04:56:12 +02:00
this.create('option', ['value', value], ['selected', 'true']) :
this.create('option', ['value', value]);
2015-05-30 14:23:33 +02:00
opt.appendChild(this.text(text));
return opt;
},
/**
* Creates and returns a checklist item
* @param {Number} chkIndex index of check item
* @param {String} chkValue check item value
* @param {String} labelText check item label text
* @return {Object} li DOM element
*/
2016-05-15 04:56:12 +02:00
createCheckItem(chkIndex, chkValue, labelText) {
2015-05-30 14:23:33 +02:00
let li = this.create('li'),
label = this.create('label', ['for', chkIndex]),
check = this.create('input',
['id', chkIndex],
['name', chkIndex],
['type', 'checkbox'],
['value', chkValue]
);
label.appendChild(check);
label.appendChild(this.text(labelText));
li.appendChild(label);
li.label = label;
li.check = check;
return li;
},
2016-05-15 04:56:12 +02:00
id(key) {
2015-12-05 14:37:59 +01:00
return document.getElementById(key);
2015-05-30 14:23:33 +02:00
},
2016-05-15 04:56:12 +02:00
tag(o, tagname) {
2015-05-30 14:23:33 +02:00
return o.getElementsByTagName(tagname);
2014-11-16 01:29:07 +01:00
}
};
// HTML5 classList API
2016-05-15 04:56:12 +02:00
function supportsClassList() {
2014-11-16 01:29:07 +01:00
return document.documentElement.classList;
}