Sha256: 6b8651bbdd4fb775aee08ed8d3ea8097d7d8da296cc8fcc4fd9a326e62ec9e62

Contents?: true

Size: 1.44 KB

Versions: 12

Compression:

Stored size: 1.44 KB

Contents

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

@class up.toast
###
up.toast = (($) ->

  u = up.util
  b = up.browser

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

  state = u.config
    $toast: null

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

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

  isOpen = ->
    !!state.$toast
    
  addAction = ($actions, label, callback) ->
    $action = $('<span class="up-toast-action"></span>').text(label)
    $action.on 'click', callback
    $action.appendTo($actions)
  
  open = (message, options = {}) ->
    close()
    $toast = $('<div class="up-toast"></div>').prependTo('body')
    $message = $('<div class="up-toast-message"></div>').appendTo($toast)
    $actions = $('<div class="up-toast-actions"></div>').appendTo($toast)

    message = messageToHtml(message)
    $message.html(message)

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

    addAction($actions, 'Close', close)

    state.$toast = $toast

  close = ->
    if isOpen()
      state.$toast.remove()
      state.$toast = null

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

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

)(jQuery)

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
unpoly-rails-0.57.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.7 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.6 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.5 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.4 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.3 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.2 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.56.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.55.1 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.55.0 lib/assets/javascripts/unpoly/toast.coffee
unpoly-rails-0.54.1 lib/assets/javascripts/unpoly/toast.coffee