Sha256: b98ff380f293aa1fc8572cded208d4009288cf2843f26eb1be98600d530712d3

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

# -----------------------------------------------------------------------------
# Author: Alexander Kravets <alex@slatestudio.com>,
#         Slate Studio (http://www.slatestudio.com)
#
# Coding Guide:
#   https://github.com/thoughtbot/guides/tree/master/style/coffeescript
# -----------------------------------------------------------------------------

# -----------------------------------------------------------------------------
# EXPANDABLE GROUP
# -----------------------------------------------------------------------------
#
# Usage: onInitialize: (form, group) -> new ExpandableGroup(form, group, 'Details')
#
# -----------------------------------------------------------------------------

@_expandableGroupStateCache = {}

class @ExpandableGroup
  constructor: (@form, @group, name) ->
    @$expander =$ """<a href='#' class='group-edit hidden'>#{ name }</a>"""
    @group.$el.before @$expander

    @_restoreExpanderFromCache()

    @$expander.on 'click', (e) =>
      @$expander.toggleClass('hidden')
      @_cacheExpanderState()
      e.preventDefault()


  _restoreExpanderFromCache: ->
    if _expandableGroupStateCache.__hash

      if _expandableGroupStateCache.__hash == window.location.hash
        if _expandableGroupStateCache[@_groupId()]
          @$expander.removeClass 'hidden'

      if _expandableGroupStateCache.__hash.endsWith 'new'
        @$expander.removeClass 'hidden'


  _cacheExpanderState: ->
    _expandableGroupStateCache.__hash = window.location.hash
    _expandableGroupStateCache[@_groupId()] = @group.$el.is(':visible')


  _groupId: ->
    groupIndex = $('form').find(".group.#{ @group.klassName }").index(@group.$el)
    return "#{ @group.klassName }-#{ groupIndex }"

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
formagic-0.3.4 app/assets/javascripts/formagic/group.coffee
formagic-0.3.3 app/assets/javascripts/formagic/group.coffee
formagic-0.3.2 app/assets/javascripts/formagic/group.coffee
formagic-0.3.1 app/assets/javascripts/formagic/group.coffee
formagic-0.3.0 app/assets/javascripts/formagic/group.coffee