Advertisement
Guest User

Untitled

a guest
May 29th, 2015
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. import {isObject} from "./utils"
  2. import Dispatcher from "./dispatcher"
  3.  
  4. export default class Component {
  5. constructor() {
  6. this.rootNode = null
  7. this.props = {}
  8.  
  9. // Add dispatcher observers
  10. if (this.constructor.observers) {
  11. const observers = this.constructor.observers
  12. for (let eventName in observers) {
  13. const methodName = observers[eventName]
  14. Dispatcher.addObserver(eventName, this, methodName)
  15. }
  16. }
  17. }
  18.  
  19. destroy() {
  20. if (this.rootNode.parentNode) {
  21. this.rootNode.parentNode.removeChild(this.rootNode)
  22. }
  23.  
  24. // Remove dispatcher observers
  25. const observers = this.constructor.observers
  26. if (observers) {
  27. for (let eventName in observers) {
  28. Dispatcher.removeObserver(eventName, this)
  29. }
  30. }
  31. }
  32.  
  33. // Define an updatable property of the component.
  34. // Also updates the property if it's already set
  35. prop(propName, propValue = "") {
  36. let propNode = this.props[propName]
  37.  
  38. if (!propNode) {
  39. propNode = this.props[propName] = document.createTextNode(propValue)
  40. }
  41.  
  42. if (propValue !== propNode.textContent) {
  43. propNode.textContent = propValue
  44. }
  45.  
  46. return propNode
  47. }
  48.  
  49. render() {
  50. if (this.rootNode) {
  51. throw new Error("already rendered component")
  52. }
  53. if (!this.build) {
  54. throw new Error("unimplemented method build()")
  55. }
  56.  
  57. this.rootNode = this.build()
  58.  
  59. // Add DOM event handlers
  60. const eventHandlers = this.constructor.eventHandlers
  61. if (eventHandlers) {
  62. for (let eventName in eventHandlers) {
  63. const handler = eventHandlers[eventName]
  64. this.rootNode.addEventListener(eventName, handler.bind(this), true)
  65. }
  66. }
  67.  
  68. return this.rootNode
  69. }
  70.  
  71. show() {
  72. this.rootNode.classList.toggle("u-hidden", false)
  73. }
  74.  
  75. hide() {
  76. this.rootNode.classList.toggle("u-hidden", true)
  77. }
  78.  
  79. toggle() {
  80. console.warn("hello", this.rootNode)
  81. this.rootNode.classList.toggle("u-hidden")
  82. }
  83.  
  84. static tag(tagSpec, attributes, children) {
  85. if (!tagSpec) {
  86. throw new Error("No tagSpec given")
  87. }
  88.  
  89. attributes = attributes || {}
  90. children = children || []
  91.  
  92. if (!isObject(attributes)) {
  93. children = attributes
  94. attributes = {}
  95. }
  96.  
  97. const [tagName, className] = tagSpec.split(".")
  98. const node = document.createElement(tagName)
  99.  
  100. if (className) {
  101. node.classList.add(className)
  102. }
  103.  
  104. if (!(children instanceof Array)) {
  105. children = [children]
  106. }
  107.  
  108. for (let attrName in attributes) {
  109. node.setAttribute(attrName, attributes[attrName])
  110. }
  111.  
  112. for (let child of children) {
  113. if (typeof child === "object" && "render" in child) {
  114. // Component children
  115. child.render()
  116. node.appendChild(child.rootNode)
  117. } else if (typeof child === "string") {
  118. // Text children
  119. node.appendChild(document.createTextNode(child))
  120. } else {
  121. // DOM nodes children
  122. node.appendChild(child)
  123. }
  124. }
  125.  
  126. return node
  127. }
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement