Lungo.Notification.coffee 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. ###
  2. Notification system in CSS3
  3. @namespace Lungo
  4. @class Notification
  5. @author Javier Jimenez Villar <javi@tapquo.com> || @soyjavi
  6. ###
  7. Lungo.Notification = do(lng = Lungo) ->
  8. _options = []
  9. _el = null
  10. _window = null
  11. DELAY_TIME = 1
  12. ANIMATION_MILISECONDS = 200
  13. ATTRIBUTE = lng.Constants.ATTRIBUTE
  14. BINDING = lng.Constants.BINDING
  15. SELECTOR =
  16. BODY: "body"
  17. NOTIFICATION: ".notification"
  18. MODAL: ".notification .window"
  19. MODAL_HREF: ".notification .window a"
  20. WINDOW_CLOSABLE: ".notification [data-action=close], .notification > .error, .notification > .success"
  21. CONFIRM_BUTTONS: ".notification .confirm a.button"
  22. STYLE =
  23. MODAL: "modal"
  24. VISIBLE: "visible"
  25. SHOW: "show"
  26. WORKING: "working"
  27. INPUT: "input"
  28. CALLBACK_HIDE = "Lungo.Notification.hide()"
  29. MARKUP_NOTIFICATION = "<div class=\"notification\"><div class=\"window\"></div></div>"
  30. ###
  31. ###
  32. show = (title, icon, seconds, callback) ->
  33. markup = undefined
  34. if title isnt undefined
  35. markup = _markup(title, null, icon)
  36. else
  37. data_loading = lng.Attributes.loading.html
  38. markup = data_loading.replace(BINDING.START + BINDING.KEY + BINDING.END, "icon white")
  39. _show markup, "growl"
  40. _hide seconds, callback
  41. ###
  42. ###
  43. hide = ->
  44. _window.removeClass "show"
  45. setTimeout (->
  46. _el.style("display", "none").removeClass("html").removeClass("confirm").removeClass("notify").removeClass "growl"
  47. ), ANIMATION_MILISECONDS - 50
  48. ###
  49. ###
  50. confirm = (options) ->
  51. _options = options
  52. markup = _markup(options.title, options.description, options.icon)
  53. markup += _button_markup(options.accept, "accept")
  54. markup += _button_markup(options.cancel, "cancel")
  55. _show markup, "confirm"
  56. ###
  57. ###
  58. success = (title, description, icon, seconds, callback) ->
  59. _notify title, description, icon, "success", seconds, callback
  60. ###
  61. ###
  62. error = (title, description, icon, seconds, callback) ->
  63. _notify title, description, icon, "error", seconds, callback
  64. ###
  65. ###
  66. _notify = (title, description, icon, stylesheet, seconds, callback) ->
  67. _show _markup(title, description, icon), stylesheet
  68. _hide seconds, callback if seconds
  69. ###
  70. ###
  71. html = (markup, button) ->
  72. markup += (if (button) then "<a href=\"#\" class=\"button large anchor\" data-action=\"close\">" + button + "</a>" else "")
  73. _show markup, "html"
  74. _init = ->
  75. lng.dom(SELECTOR.BODY).append MARKUP_NOTIFICATION
  76. _el = lng.dom(SELECTOR.NOTIFICATION)
  77. _window = _el.children(".window")
  78. _subscribeEvents()
  79. _show = (html, stylesheet) ->
  80. _el.show()
  81. _window.removeClass STYLE.SHOW
  82. _window.removeClass("error").removeClass("success").removeClass("html").removeClass "growl"
  83. _window.addClass stylesheet
  84. _window.html html
  85. setTimeout (->
  86. _window.addClass STYLE.SHOW
  87. ), DELAY_TIME
  88. _hide = (seconds, callback) ->
  89. if seconds isnt undefined and seconds isnt 0
  90. miliseconds = seconds * 1000
  91. setTimeout (->
  92. hide()
  93. # if (callback) callback.apply(callback);
  94. setTimeout callback, ANIMATION_MILISECONDS if callback
  95. ), miliseconds
  96. _markup = (title, description, icon) ->
  97. description = (if not description then "&nbsp;" else description)
  98. "<span class=\"icon " + icon + "\"></span><strong class=\"text bold\">" + title + "</strong><small>" + description + "</small>"
  99. _button_markup = (options, callback) ->
  100. "<a href=\"#\" data-callback=\"" + callback + "\" class=\"button anchor large text thin\">" + options.label + "</a>"
  101. _subscribeEvents = ->
  102. lng.dom(SELECTOR.CONFIRM_BUTTONS).tap (event) ->
  103. button = lng.dom(this)
  104. callback = _options[button.data("callback")].callback
  105. callback.call callback if callback
  106. hide()
  107. lng.dom(SELECTOR.WINDOW_CLOSABLE).tap hide
  108. _init()
  109. show: show
  110. hide: hide
  111. error: error
  112. success: success
  113. confirm: confirm
  114. html: html