Sha256: a164f989183276cfc4b2b4f4cee11f283dec77cbb030c8ac1b9cc5ba50aea0d3

Contents?: true

Size: 1.08 KB

Versions: 22

Compression:

Stored size: 1.08 KB

Contents

# jQuery plugin checkbox
$.fn.spinaSwitch = ->
  return this.each ->
    unless $(this).attr('data-plugin-switch')
      input = $(this)
      input.attr('data-plugin-switch', true)
      input.hide()

      # Check if it is checked
      if input.is(':checked')
        klass = "switch active"
      else
        klass = "switch"

      # Insert new HTML into the DOM
      input.after('<a href="#' + input.attr("id") + '" class="' + klass + '">
                    <span class="knob"></span>
                  </a>')

# Click handlers for checkbox
$(document).on 'click', 'a.switch', (e) ->
  toggleSwitch(e)

$(document).on 'touchend', 'a.switch', (e) ->
  toggleSwitch(e)

toggleSwitch = (e) ->
  checkbox = $(e.currentTarget)
  input = $(checkbox.attr("href"))

  if checkbox.hasClass('activated') || checkbox.hasClass('active') 
    checkbox.removeClass('active')
    checkbox.removeClass('activated')
    checkbox.addClass('deactivated')
    input.prop("checked", false)
  else
    checkbox.addClass('activated')
    checkbox.removeClass('deactivated')
    input.prop("checked", true)

  return false

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
spina-template-0.2.5 app/assets/javascripts/spina/switch.js.coffee
spina-template-0.2.4 app/assets/javascripts/spina/switch.js.coffee