dom.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. var config = require('../config')
  2. /**
  3. * Check if a node is in the document.
  4. *
  5. * @param {Node} node
  6. * @return {Boolean}
  7. */
  8. var doc =
  9. typeof document !== 'undefined' &&
  10. document.documentElement
  11. exports.inDoc = function (node) {
  12. return doc && doc.contains(node)
  13. }
  14. /**
  15. * Extract an attribute from a node.
  16. *
  17. * @param {Node} node
  18. * @param {String} attr
  19. */
  20. exports.attr = function (node, attr) {
  21. attr = config.prefix + attr
  22. var val = node.getAttribute(attr)
  23. if (val !== null) {
  24. node.removeAttribute(attr)
  25. }
  26. return val
  27. }
  28. /**
  29. * Insert el before target
  30. *
  31. * @param {Element} el
  32. * @param {Element} target
  33. */
  34. exports.before = function (el, target) {
  35. target.parentNode.insertBefore(el, target)
  36. }
  37. /**
  38. * Insert el after target
  39. *
  40. * @param {Element} el
  41. * @param {Element} target
  42. */
  43. exports.after = function (el, target) {
  44. if (target.nextSibling) {
  45. exports.before(el, target.nextSibling)
  46. } else {
  47. target.parentNode.appendChild(el)
  48. }
  49. }
  50. /**
  51. * Remove el from DOM
  52. *
  53. * @param {Element} el
  54. */
  55. exports.remove = function (el) {
  56. el.parentNode.removeChild(el)
  57. }
  58. /**
  59. * Prepend el to target
  60. *
  61. * @param {Element} el
  62. * @param {Element} target
  63. */
  64. exports.prepend = function (el, target) {
  65. if (target.firstChild) {
  66. exports.before(el, target.firstChild)
  67. } else {
  68. target.appendChild(el)
  69. }
  70. }
  71. /**
  72. * Replace target with el
  73. *
  74. * @param {Element} target
  75. * @param {Element} el
  76. */
  77. exports.replace = function (target, el) {
  78. var parent = target.parentNode
  79. parent.insertBefore(el, target)
  80. parent.removeChild(target)
  81. }
  82. /**
  83. * Copy attributes from one element to another.
  84. *
  85. * @param {Element} from
  86. * @param {Element} to
  87. */
  88. exports.copyAttributes = function (from, to) {
  89. if (from.hasAttributes()) {
  90. var attrs = from.attributes
  91. for (var i = 0, l = attrs.length; i < l; i++) {
  92. var attr = attrs[i]
  93. to.setAttribute(attr.name, attr.value)
  94. }
  95. }
  96. }
  97. /**
  98. * Add event listener shorthand.
  99. *
  100. * @param {Element} el
  101. * @param {String} event
  102. * @param {Function} cb
  103. */
  104. exports.on = function (el, event, cb) {
  105. el.addEventListener(event, cb)
  106. }
  107. /**
  108. * Remove event listener shorthand.
  109. *
  110. * @param {Element} el
  111. * @param {String} event
  112. * @param {Function} cb
  113. */
  114. exports.off = function (el, event, cb) {
  115. el.removeEventListener(event, cb)
  116. }