view.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. toggleClass: (name) ->
  32. @el.classList.toggle(name)
  33. return
  34. hasClass: (name) ->
  35. @el.classList.contains(name)
  36. resetClass: ->
  37. @el.className = @originalClassName or ''
  38. if @constructor.className
  39. @addClass name for name in @constructor.className.split ' '
  40. return
  41. find: (selector) ->
  42. $ selector, @el
  43. findAll: (selector) ->
  44. $$ selector, @el
  45. findByClass: (name) ->
  46. @findAllByClass(name)[0]
  47. findLastByClass: (name) ->
  48. all = @findAllByClass(name)[0]
  49. all[all.length - 1]
  50. findAllByClass: (name) ->
  51. @el.getElementsByClassName(name)
  52. findByTag: (tag) ->
  53. @findAllByTag(tag)[0]
  54. findLastByTag: (tag) ->
  55. all = @findAllByTag(tag)
  56. all[all.length - 1]
  57. findAllByTag: (tag) ->
  58. @el.getElementsByTagName(tag)
  59. append: (value) ->
  60. $.append @el, value.el or value
  61. return
  62. appendTo: (value) ->
  63. $.append value.el or value, @el
  64. return
  65. prepend: (value) ->
  66. $.prepend @el, value.el or value
  67. return
  68. prependTo: (value) ->
  69. $.prepend value.el or value, @el
  70. return
  71. before: (value) ->
  72. $.before @el, value.el or value
  73. return
  74. after: (value) ->
  75. $.after @el, value.el or value
  76. return
  77. remove: (value) ->
  78. $.remove value.el or value
  79. return
  80. empty: ->
  81. $.empty @el
  82. @refreshElements()
  83. return
  84. html: (value) ->
  85. @empty()
  86. @append value
  87. return
  88. tmpl: (args...) ->
  89. app.templates.render(args...)
  90. delay: (fn, args...) ->
  91. delay = if typeof args[args.length - 1] is 'number' then args.pop() else 0
  92. setTimeout fn.bind(@, args...), delay
  93. onDOM: (event, callback) ->
  94. $.on @el, event, callback
  95. return
  96. offDOM: (event, callback) ->
  97. $.off @el, event, callback
  98. return
  99. bindEvents: ->
  100. if @constructor.events
  101. @onDOM name, @[method] for name, method of @constructor.events
  102. if @constructor.routes
  103. app.router.on name, @[method] for name, method of @constructor.routes
  104. if @constructor.shortcuts
  105. app.shortcuts.on name, @[method] for name, method of @constructor.shortcuts
  106. return
  107. unbindEvents: ->
  108. if @constructor.events
  109. @offDOM name, @[method] for name, method of @constructor.events
  110. if @constructor.routes
  111. app.router.off name, @[method] for name, method of @constructor.routes
  112. if @constructor.shortcuts
  113. app.shortcuts.off name, @[method] for name, method of @constructor.shortcuts
  114. return
  115. addSubview: (view) ->
  116. (@subviews or= []).push(view)
  117. activate: ->
  118. return if @activated
  119. @bindEvents()
  120. view.activate() for view in @subviews if @subviews
  121. @activated = true
  122. true
  123. deactivate: ->
  124. return unless @activated
  125. @unbindEvents()
  126. view.deactivate() for view in @subviews if @subviews
  127. @activated = false
  128. true
  129. detach: ->
  130. @deactivate()
  131. $.remove @el
  132. return