Skip to content

Instantly share code, notes, and snippets.

@zorbash
Created April 16, 2015 14:08
Show Gist options
  • Select an option

  • Save zorbash/fc4d58c7c8e8bc21a1b5 to your computer and use it in GitHub Desktop.

Select an option

Save zorbash/fc4d58c7c8e8bc21a1b5 to your computer and use it in GitHub Desktop.
class Toggler
defaults =
toggled_class: 'open'
constructor: (el, options) ->
@$element = $(el)
@options = $.extend {}, defaults, options
@init()
$target: -> $(@$element.data('target'))
open: ->
@$element.trigger('toggler.open')
$target.addClass('open')
close: ->
@$element.trigger('toggler.close')
@$target().removeClass('open')
onClick: => $target?.toggleClass @options.toggled_class
bindHandlers: ->
@$element.on 'click', @onClick
Toggler::init = ->
@bindHandlers()
$.fn.toggler = (options, params...) ->
@each ->
toggler = $.data @, 'plugin_toggler'
if typeof options == 'string'
toggler?[options].apply toggler, params
else
$.data @, 'plugin_toggler', new Toggler @, options if !toggler
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment