UNPKG

4.96 kBJavaScriptView Raw
1import {root} from './root';
2import {isArray, isString, isUndef} from './types';
3import {trim} from './string';
4
5/**
6 * DOM utilities
7 */
8
9const doc = root.document;
10
11/**
12 * Returns text + text of children of given node
13 * @param {NodeElement} node
14 * @return {String}
15 */
16export const getText = (node) => {
17 if (isUndef(node.textContent)) {
18 return trim(node.innerText);
19 }
20 return trim(node.textContent);
21};
22
23/**
24 * Returns the first text node contained in the supplied node
25 * @param {NodeElement} node node
26 * @return {String}
27 */
28export const getFirstTextNode = (node) => {
29 for (let i = 0; i < node.childNodes.length; i++) {
30 let n = node.childNodes[i];
31 if (n.nodeType === 3) {
32 return n.data;
33 }
34 }
35};
36
37/**
38 * Creates an html element with given collection of attributes
39 * @param {String} tag a string of the html tag to create
40 * @param {Array} an undetermined number of arrays containing the with 2
41 * items, the attribute name and its value ['id','myId']
42 * @return {Object} created element
43 */
44export const createElm = (...args) => {
45 let tag = args[0];
46 if (!isString(tag)) {
47 return null;
48 }
49
50 let el = doc.createElement(tag);
51 for (let i = 0; i < args.length; i++) {
52 let arg = args[i];
53
54 if (isArray(arg) && arg.length === 2) {
55 el.setAttribute(arg[0], arg[1]);
56 }
57 }
58 return el;
59};
60
61/**
62 * Removes passed node from DOM
63 * @param {DOMElement} node
64 * @return {DOMElement} old node reference
65 */
66export const removeElm = (node) => node.parentNode.removeChild(node);
67
68/**
69 * Returns a text node with given text
70 * @param {String} txt
71 * @return {Object}
72 */
73export const createText = (txt) => doc.createTextNode(txt);
74
75/**
76 * Determine whether the passed elements is assigned the given class
77 * @param {DOMElement} ele DOM element
78 * @param {String} cls CSS class name
79 * @returns {Boolean}
80 */
81export const hasClass = (ele, cls) => {
82 if (isUndef(ele)) {
83 return false;
84 }
85
86 if (supportsClassList()) {
87 return ele.classList.contains(cls);
88 }
89 return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
90};
91
92/**
93 * Adds the specified class to the passed element
94 * @param {DOMElement} ele DOM element
95 * @param {String} cls CSS class name
96 */
97export const addClass = (ele, cls) => {
98 if (isUndef(ele)) {
99 return;
100 }
101
102 if (supportsClassList()) {
103 ele.classList.add(cls);
104 return;
105 }
106
107 if (ele.className === '') {
108 ele.className = cls;
109 }
110 else if (!hasClass(ele, cls)) {
111 ele.className += ' ' + cls;
112 }
113};
114
115/**
116 * Removes the specified class to the passed element
117 * @param {DOMElement} ele DOM element
118 * @param {String} cls CSS class name
119 */
120export const removeClass = (ele, cls) => {
121 if (isUndef(ele)) {
122 return;
123 }
124
125 if (supportsClassList()) {
126 ele.classList.remove(cls);
127 return;
128 }
129 let reg = new RegExp('(\\s|^)' + cls + '(\\s|$)', 'g');
130 ele.className = ele.className.replace(reg, '');
131};
132
133/**
134 * Creates and returns an option element
135 * @param {String} text option text
136 * @param {String} value option value
137 * @param {Boolean} isSel whether option is selected
138 * @return {Object} option element
139 */
140export const createOpt = (text, value, isSel) => {
141 let isSelected = isSel ? true : false;
142 let opt = isSelected ?
143 createElm('option', ['value', value], ['selected', 'true']) :
144 createElm('option', ['value', value]);
145 opt.appendChild(createText(text));
146 return opt;
147};
148
149/**
150 * Creates and returns a checklist item
151 * @param {String} id index of check item
152 * @param {String} chkValue check item value
153 * @param {String} labelText check item label text
154 * @return {Object} li DOM element
155 */
156export const createCheckItem = (id, chkValue, labelText) => {
157 let li = createElm('li');
158 let label = createElm('label', ['for', id]);
159 let check = createElm('input',
160 ['id', id],
161 ['name', id],
162 ['type', 'checkbox'],
163 ['value', chkValue]
164 );
165 label.appendChild(check);
166 label.appendChild(createText(labelText));
167 li.appendChild(label);
168 li.label = label;
169 li.check = check;
170 return li;
171};
172
173/**
174 * Returns the element matching the supplied Id
175 * @param {String} id Element identifier
176 * @return {DOMElement}
177 */
178export const elm = (id) => doc.getElementById(id);
179
180/**
181 * Returns list of element matching the supplied tag name
182 * @param {String} tagname Tag name
183 * @return {NodeList}
184 */
185export const tag = (o, tagname) => o.getElementsByTagName(tagname);
186
187// HTML5 classList API
188function supportsClassList() {
189 return doc.documentElement.classList;
190}