view.coffee 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. class app.View
  2. $.extend @prototype, Events
  3. constructor: ->
  4. @setupElement()
  5. @originalClassName = @el.className if @el.className
  6. @resetClass() if @constructor.className
  7. @refreshElements()
  8. @init?()
  9. @refreshElements()
  10. setupElement: ->
  11. @el ?= if typeof @constructor.el is 'string'
  12. $ @constructor.el
  13. else if @constructor.el
  14. @constructor.el
  15. else
  16. document.createElement @constructor.tagName or 'div'
  17. if @constructor.attributes
  18. for key, value of @constructor.attributes
  19. @el.setAttribute(key, value)
  20. return
  21. refreshElements: ->
  22. if @constructor.elements
  23. @[name] = @find selector for name, selector of @constructor.elements
  24. return
  25. addClass: (name) ->
  26. @el.classList.add(name)
  27. return
  28. removeClass: (name) ->
  29. @el.classList.remove(name)
  30. return
  31. resetClass: ->
  32. @el.className = @originalClassName or ''
  33. if @constructor.className
  34. @addClass name for name in @constructor.className.split ' '
  35. return
  36. find: (selector) ->
  37. $ selector, @el
  38. findAll: (selector) ->
  39. $$ selector, @el
  40. findByClass: (name) ->
  41. @findAllByClass(name)[0]
  42. findLastByClass: (name) ->
  43. all = @findAllByClass(name)[0]
  44. all[all.length - 1]
  45. findAllByClass: (name) ->
  46. @el.getElementsByClassName(name)
  47. findByTag: (tag) ->
  48. @findAllByTag(tag)[0]
  49. findLastByTag: (tag) ->
  50. all = @findAllByTag(tag)
  51. all[all.length - 1]
  52. findAllByTag: (tag) ->
  53. @el.getElementsByTagName(tag)
  54. append: (value) ->
  55. $.append @el, value.el or value
  56. return
  57. appendTo: (value) ->
  58. $.append value.el or value, @el
  59. return
  60. prepend: (value) ->
  61. $.prepend @el, value.el or value
  62. return
  63. prependTo: (value) ->
  64. $.prepend value.el or value, @el
  65. return
  66. before: (value) ->
  67. $.before @el, value.el or value
  68. return
  69. after: (value) ->
  70. $.after @el, value.el or value
  71. return
  72. remove: (value) ->
  73. $.remove value.el or value
  74. return
  75. empty: ->
  76. $.empty @el
  77. @refreshElements()
  78. return
  79. html: (value) ->
  80. @empty()
  81. @append value
  82. return
  83. tmpl: (args...) ->
  84. app.templates.render(args...)
  85. delay: (fn, args...) ->
  86. delay = if typeof args[args.length - 1] is 'number' then args.pop() else 0
  87. setTimeout fn.bind(@, args...), delay
  88. onDOM: (event, callback) ->
  89. $.on @el, event, callback
  90. return
  91. offDOM: (event, callback) ->
  92. $.off @el, event, callback
  93. return
  94. bindEvents: ->
  95. if @constructor.events
  96. @onDOM name, @[method] for name, method of @constructor.events
  97. if @constructor.routes
  98. app.router.on name, @[method] for name, method of @constructor.routes
  99. if @constructor.shortcuts
  100. app.shortcuts.on name, @[method] for name, method of @constructor.shortcuts
  101. return
  102. unbindEvents: ->
  103. if @constructor.events
  104. @offDOM name, @[method] for name, method of @constructor.events
  105. if @constructor.routes
  106. app.router.off name, @[method] for name, method of @constructor.routes
  107. if @constructor.shortcuts
  108. app.shortcuts.off name, @[method] for name, method of @constructor.shortcuts
  109. return
  110. addSubview: (view) ->
  111. (@subviews or= []).push(view)
  112. activate: ->
  113. return if @activated
  114. @bindEvents()
  115. view.activate() for view in @subviews if @subviews
  116. @activated = true
  117. true
  118. deactivate: ->
  119. return unless @activated
  120. @unbindEvents()
  121. view.deactivate() for view in @subviews if @subviews
  122. @activated = false
  123. true
  124. detach: ->
  125. @deactivate()
  126. $.remove @el
  127. return