PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/app/coffeescripts/jquery.rails_flash_notifications.coffee

https://gitlab.com/ykazemi/canvas-lms
CoffeeScript | 136 lines | 98 code | 18 blank | 20 comment | 19 complexity | 8bf57b41073edd983f49432c1280d811 MD5 | raw file
  1. # does Rails-style flash message/error boxes that drop down from the top of the screen
  2. define [
  3. 'i18n!shared.flash_notices'
  4. 'jquery'
  5. 'underscore'
  6. 'compiled/fn/preventDefault'
  7. 'str/htmlEscape'
  8. 'jqueryui/effects/drop'
  9. 'vendor/jquery.cookie'
  10. ], (I18n, $, _, preventDefault, htmlEscape) ->
  11. $holder = []
  12. $screenreader_holder = []
  13. initFlashContainer = ->
  14. $holder = $("#flash_message_holder")
  15. return if $holder.length == 0 # not defined yet; call $.initFlashContainer later
  16. $screenreader_holder = $("#flash_screenreader_holder")
  17. $holder.on 'click', '.close_link', preventDefault(->)
  18. $holder.on 'click', 'li', ->
  19. $this = $(this)
  20. return if $this.hasClass('no_close')
  21. $.cookie('unsupported_browser_dismissed', '1') if $this.hasClass('unsupported_browser')
  22. $this.stop(true, true).remove()
  23. initFlashContainer() # look for the container on script load
  24. ###
  25. xsslint safeString.function escapeContent
  26. ###
  27. escapeContent = (content) ->
  28. if content.hasOwnProperty('html') then content.html else htmlEscape(content)
  29. screenReaderFlashBox = (type, content) ->
  30. # nothing to do here if $screenreader_holder is not yet defined
  31. if $screenreader_holder.length > 0
  32. existing_nodes = $screenreader_holder.find('span')
  33. if existing_nodes.length > 0 && content.string
  34. message_text = content.string
  35. matching_node = _.find existing_nodes, (node) ->
  36. $(node).text() == message_text
  37. if matching_node
  38. # need to remove and re-add error for accessibility, and to ensure
  39. # duplicate errors/messages do not pile up
  40. $(matching_node).remove()
  41. $screenreader_node = $("""
  42. <span>#{escapeContent(content)}</span>
  43. """)
  44. $screenreader_node.appendTo($screenreader_holder)
  45. # We are removing all the attributes and readding them due to Jaws inability
  46. # to communicate with IE. If we were to just remove the element out right
  47. # NVDA would interrupt itself, however if we were to hide element Jaws will
  48. # read the element multiple times.
  49. window.setTimeout((->
  50. $screenreader_node.parent().each(->
  51. attributes = $.extend(true, {}, this.attributes);
  52. i = attributes.length;
  53. while( i-- )
  54. this.removeAttributeNode(attributes[i])
  55. $screenreader_node.remove()
  56. parentNode = this
  57. Array.prototype.forEach.call(attributes,(attribute) ->
  58. $(parentNode).attr(attribute.name, attribute.value))
  59. )
  60. ), 7000)
  61. flashBox = (type, content, timeout, cssOptions = {}) ->
  62. if type is "success"
  63. icon = "check"
  64. else if type is "warning" || type is "error"
  65. icon = "warning"
  66. else
  67. icon = "info"
  68. $node = $("""
  69. <li class="ic-flash-#{htmlEscape(type)}">
  70. <div class="ic-flash__icon" aria-hidden="true">
  71. <i class="icon-#{htmlEscape(icon)}"></i>
  72. </div>
  73. #{escapeContent(content)}
  74. <button type="button" class="Button Button--icon-action close_link">
  75. <span class="screenreader-only">
  76. #{htmlEscape I18n.t("close", "Close")}
  77. </span>
  78. <i class="icon-x" aria-hidden="true"></i>
  79. </button>
  80. </li>
  81. """)
  82. $node.appendTo($holder).
  83. css(_.extend(zIndex: 1, cssOptions)).
  84. show('drop', direction: "up", 'fast', -> $(this).css('z-index', 2)).
  85. delay(timeout || 7000).
  86. animate({'z-index': 1}, 0).
  87. fadeOut('slow', -> $(this).slideUp('fast', -> $(this).remove()))
  88. setTimeout((-> screenReaderFlashBox(type, content)), 100)
  89. # Pops up a small notification box at the top of the screen.
  90. $.flashMessage = (content, timeout = 3000) ->
  91. flashBox("success", content, timeout)
  92. # Pops up a small error box at the top of the screen.
  93. $.flashError = (content, timeout) ->
  94. flashBox("error", content, timeout)
  95. # Pops up a small warning box at the top of the screen.
  96. $.flashWarning = (content, timeout = 3000) ->
  97. flashBox("warning", content, timeout)
  98. $.screenReaderFlashMessage = (content) ->
  99. screenReaderFlashBox('success', content)
  100. $.screenReaderFlashError = (content) ->
  101. screenReaderFlashBox('error', content)
  102. # This is for when you want to clear the flash message content prior to
  103. # updating it with new content. Makes it so the SR only reads this one
  104. # message.
  105. $.screenReaderFlashMessageExclusive = (content) ->
  106. # nothing to do here if $screenreader_holder is not yet defined
  107. if $screenreader_holder.length > 0
  108. $screenreader_holder.html("""
  109. <span>#{escapeContent(content)}</span>
  110. """)
  111. $.initFlashContainer = ->
  112. initFlashContainer()
  113. renderServerNotifications = ->
  114. if ENV.notices?
  115. for notice in ENV.notices
  116. flashBox(notice.type, notice.content)
  117. $ ->
  118. setTimeout(renderServerNotifications, 500)