Advertisement
Guest User

Untitled

a guest
May 20th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. import StringHelper from 'src/script/helper/string.helper';
  2.  
  3. export default class DomAccess {
  4.  
  5. /**
  6. * Returns whether or not the element is an HTML node
  7. *
  8. * @param {HTMLElement} element
  9. * @returns {boolean}
  10. */
  11. static isNode(element) {
  12. if (!element) return false;
  13.  
  14. if (typeof Node === 'object') {
  15. return element instanceof Node;
  16. }
  17.  
  18. const isObject = typeof element === 'object';
  19. const isNumber = typeof element.nodeType === 'number';
  20. const isString = typeof element.nodeName === 'string';
  21.  
  22. const HtmlNode = isObject && isNumber && isString;
  23. const RootNode = element === document || element === window;
  24.  
  25. return element && (HtmlNode || RootNode);
  26. }
  27.  
  28. /**
  29. * Returns if the given element has the requested attribute/property
  30. * @param {HTMLElement} element
  31. * @param {string} attribute
  32. */
  33. static hasAttribute(element, attribute) {
  34. if (!DomAccess.isNode(element)) {
  35. throw new Error('The element must be a valid HTML Node!');
  36. }
  37.  
  38. if (typeof element.hasAttribute !== 'function') return false;
  39.  
  40. return element.hasAttribute(attribute);
  41. }
  42.  
  43. /**
  44. * Returns the value of a given element's attribute/property
  45. * @param {HTMLElement|EventTarget} element
  46. * @param {string} attribute
  47. * @param {boolean} strict
  48. * @returns {*|this|string}
  49. */
  50. static getAttribute(element, attribute, strict = true) {
  51. if (strict && DomAccess.hasAttribute(element, attribute) === false) {
  52. throw new Error(`The required property "${attribute}" does not exist!`);
  53. }
  54.  
  55. if (typeof element.getAttribute !== 'function') {
  56. if (strict) {
  57. throw new Error('This node doesn\'t support the getAttribute function!');
  58. }
  59.  
  60. return undefined;
  61. }
  62.  
  63. return element.getAttribute(attribute);
  64. }
  65.  
  66. /**
  67. * Returns the value of a given elements dataset entry
  68. *
  69. * @param {HTMLElement|EventTarget} element
  70. * @param {string} key
  71. * @param {boolean} strict
  72. * @returns {*|this|string}
  73. */
  74. static getDataAttribute(element, key, strict = true) {
  75. const keyWithoutData = key.replace(/^data(|-)/, '');
  76. const parsedKey = StringHelper.toLowerCamelCase(keyWithoutData, '-');
  77. if (!DomAccess.isNode(element)) {
  78. if (strict) {
  79. throw new Error('The passed node is not a valid HTML Node!');
  80. }
  81.  
  82. return undefined;
  83. }
  84.  
  85. if (typeof element.dataset === 'undefined') {
  86. if (strict) {
  87. throw new Error('This node doesn\'t support the dataset attribute!');
  88. }
  89.  
  90. return undefined;
  91. }
  92.  
  93. const attribute = element.dataset[parsedKey];
  94.  
  95. if (typeof attribute === 'undefined') {
  96. if (strict) {
  97. throw new Error(`The required data attribute "${key}" does not exist on ${element}!`);
  98. }
  99.  
  100. return attribute;
  101. }
  102.  
  103. return StringHelper.parsePrimitive(attribute);
  104. }
  105.  
  106. /**
  107. * Returns the selected element of a defined parent node
  108. * @param {HTMLElement|EventTarget} parentNode
  109. * @param {string} selector
  110. * @param {boolean} strict
  111. * @returns {HTMLElement}
  112. */
  113. static querySelector(parentNode, selector, strict = true) {
  114. if (strict && !DomAccess.isNode(parentNode)) {
  115. throw new Error('The parent node is not a valid HTML Node!');
  116. }
  117.  
  118. const element = parentNode.querySelector(selector) || false;
  119.  
  120. if (strict && element === false) {
  121. throw new Error(`The required element "${selector}" does not exist in parent node!`);
  122. }
  123.  
  124. return element;
  125. }
  126.  
  127. /**
  128. * Returns the selected elements of a defined parent node
  129. *
  130. * @param {HTMLElement|EventTarget} parentNode
  131. * @param {string} selector
  132. * @param {boolean} strict
  133. * @returns {NodeList|false}
  134. */
  135. static querySelectorAll(parentNode, selector, strict = true) {
  136. if (strict && !DomAccess.isNode(parentNode)) {
  137. throw new Error('The parent node is not a valid HTML Node!');
  138. }
  139.  
  140. let elements = parentNode.querySelectorAll(selector);
  141. if (elements.length === 0) {
  142. elements = false;
  143. }
  144.  
  145. if (strict && elements === false) {
  146. throw new Error(`At least one item of "${selector}" must exist in parent node!`);
  147. }
  148.  
  149. return elements;
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement