1
0

notif.js 1.3 KB

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