Home Reference Source

src/dom.js

  1. import {root} from './root';
  2. import {isArray, isString, isUndef} from './types';
  3. import {trim} from './string';
  4.  
  5. /**
  6. * DOM utilities
  7. */
  8.  
  9. const doc = root.document;
  10.  
  11. /**
  12. * Returns text + text of children of given node
  13. * @param {NodeElement} node
  14. * @return {String}
  15. */
  16. export 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. */
  28. export 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 html tag name
  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. */
  44. export 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. */
  66. export 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. */
  73. export 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. */
  81. export 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. */
  97. export 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. */
  120. export 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. */
  140. export 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. * @param {Array} extraAttr array containing attribute name and its value
  155. * @return {Object} li DOM element
  156. */
  157. export const createCheckItem = (id, chkValue, labelText, extraAttr = []) => {
  158. let li = createElm('li');
  159. let label = createElm('label', ['for', id]);
  160. let check = createElm('input',
  161. ['id', id],
  162. ['name', id],
  163. ['type', 'checkbox'],
  164. ['value', chkValue],
  165. extraAttr
  166. );
  167. label.appendChild(check);
  168. label.appendChild(createText(labelText));
  169. li.appendChild(label);
  170. li.label = label;
  171. li.check = check;
  172. return li;
  173. };
  174.  
  175. /**
  176. * Returns the element matching the supplied Id
  177. * @param {String} id Element identifier
  178. * @return {DOMElement}
  179. */
  180. export const elm = (id) => doc.getElementById(id);
  181.  
  182. /**
  183. * Returns list of element matching the supplied tag name
  184. * @param {String} tagname Tag name
  185. * @return {NodeList}
  186. */
  187. export const tag = (o, tagname) => o.getElementsByTagName(tagname);
  188.  
  189. // HTML5 classList API
  190. function supportsClassList() {
  191. return doc.documentElement.classList;
  192. }