notif.coffee 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. class app.views.Notif extends app.View
  2. @className: '_notif'
  3. @activeClass: '_in'
  4. @defautOptions:
  5. autoHide: 15000
  6. @events:
  7. click: 'onClick'
  8. constructor: (@type, @options = {}) ->
  9. @options = $.extend {}, @constructor.defautOptions, @options
  10. super
  11. init: ->
  12. @show()
  13. return
  14. show: ->
  15. if @timeout
  16. clearTimeout @timeout
  17. @timeout = @delay @hide, @options.autoHide
  18. else
  19. @render()
  20. @position()
  21. @activate()
  22. @appendTo document.body
  23. @el.offsetWidth # force reflow
  24. @addClass @constructor.activeClass
  25. @timeout = @delay @hide, @options.autoHide if @options.autoHide
  26. return
  27. hide: ->
  28. clearTimeout @timeout
  29. @timeout = null
  30. @detach()
  31. return
  32. render: ->
  33. @html @tmpl("notif#{@type}")
  34. return
  35. position: ->
  36. notifications = $$ ".#{app.views.Notif.className}"
  37. if notifications.length
  38. lastNotif = notifications[notifications.length - 1]
  39. @el.style.top = lastNotif.offsetTop + lastNotif.offsetHeight + 16 + 'px'
  40. return
  41. onClick: (event) =>
  42. unless event.target.tagName is 'A'
  43. $.stopEvent(event)
  44. @hide()
  45. return