Sha256: 219ad9deb51ae0761f1995273b184dc2d1a15763973a0745f595c21c8e6e721a

Contents?: true

Size: 1.44 KB

Versions: 11

Compression:

Stored size: 1.44 KB

Contents

###**
Toast alerts
============

@module up.toast
###
up.toast = do ->
  u = up.util
  e = up.element

  VARIABLE_FORMATTER = (arg) -> "<span class='up-toast-variable'>#{u.escapeHtml(arg)}</span>"

  state = new up.Config
    element: null

  reset = ->
    close()
    state.reset()

  messageToHtml = (message) ->
    if u.isArray(message)
      message[0] = u.escapeHtml(message[0])
      message = up.log.sprintfWithFormattedArgs(VARIABLE_FORMATTER, message...)
    else
      message = u.escapeHtml(message)
    message

  isOpen = ->
    !!state.element
    
  addAction = (label, callback) ->
    actions = state.element.querySelector('.up-toast-actions')
    action = e.affix(actions, '.up-toast-action')
    action.innerText = label
    action.addEventListener('click', callback)

  open = (message, options = {}) ->
    close()

    message = messageToHtml(message)

    state.element = e.createFromHtml """
      <div class="up-toast">
        <div class="up-toast-message">#{message}</div>
        <div class="up-toast-actions"></div>
      </div>
    """

    if action = (options.action || options.inspect)
      addAction(action.label, action.callback)

    addAction('Close', close)

    document.body.appendChild(state.element)

  close = ->
    if isOpen()
      e.remove(state.element)
      state.element = null

  # The framework is reset between tests
  up.on 'up:framework:reset', reset

  open: open
  close: close
  reset: reset
  isOpen: isOpen

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
unpoly-rails-1.0.3 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-1.0.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-1.0.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.62.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.62.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.61.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.61.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.60.3 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.60.2 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.60.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.60.0 lib/assets/javascripts/unpoly/toast.coffee