view.coffee 3.4 KB

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