<commit_before># SectionToggle (lp.SectionToggle)
# Add an handler button to control the h-visible-size of an element
#
# Obs:
# On a early stage and still very opinionated, which means that it needs abstractions.
#
# Arguments: 
#   _args (An hash containing)
#     selector    : [string] The target element
#     text        : [array]  An array of strings with the text title for each state
#     maxHeight   : [number] The default visible height size for the target selector
#     delegate    : [object] A wrapper object for callback object [onUpdate]
#     shadow      : [boolean] Determines whether or not the text is cut off by a shadow
#
# Example:
#  args =
#    selector: 'div.partner-review-facilities'
#    text: ['more facilities', 'less facilities']
#    maxheight: maxheight
#    delegate:
#      onupdate: (section, state) =>
#        console.log(state)
#  new lp.SectionToggle(args)
# 
# Dependencies:
#   jQuery

 
define ['jquery'], ($) ->
 
  class SectionToggle
  
    @version: '0.0.1'
    
    constructor: (@args={}) ->
      @target = $(@args.selector)
      @wrapper = $(@target).find('.js-read-more-wrapper')
      @baseHeight = @wrapper.height()
      @addHandler()

    findheight: () ->
      nodes = @wrapper.children()
      height = 0
      for (i = 0; i< nodes.length; i++ )
        height += $(nodes[i]).height()
      height
    
    addHandler: ->
      if @findHeight() > @args.maxHeight
        @template = "<div class='btn--read-more js-handler'>#{(@args.text)[0]}</div>"
        if @args.shadow
          @template = "<div class='read-more__handler'>" + @template + "</div>"
        @wrapper.append(@template)
        @bindEvent()
        @close()
    
    bindEvent: ->
      $(@target).find('div.js-handler').on('click', (e) =>
        e.preventDefault()
        if @state is 'close' then @open() else @close()
        @onUpdate()
      )

    open: ->
      @wrapper.height(@baseHeight)
      # Wait for the transition to finish before taking away the gradient mask
      setTimeout(=>
        $(@target).addClass('is-open').removeClass('is-closed')
      , 500)
      @setHandlerText(@args.text[1])
      @state = 'open'

    close: ->
      @wrapper.css({'overflow': 'hidden'})
      if @args.shadow is true 
        @wrapper.height(@args.maxHeight-(@args.maxHeight%18)-2) 
      else 
        @wrapper.height(@args.maxHeight)
      $(@target).addClass('is-closed').removeClass('is-open')
      @setHandlerText(@args.text[0])
      @state = 'close'

    setHandlerText: (_text) ->
      $(@target).find('div.js-handler').text(_text)

    onUpdate: ->
      @args.delegate.onUpdate(@,@state, @args.selector) if @args.delegate && @args.delegate.onUpdate

<commit_msg>Partially rewritten to work with max-height values instead of explicit heights<commit_after># SectionToggle (lp.SectionToggle)
# Add an handler button to control the h-visible-size of an element
#
# Obs:
# On a early stage and still very opinionated, which means that it needs abstractions.
#
# Arguments: 
#   _args (An hash containing)
#     selector    : [string] The target element
#     text        : [array]  An array of strings with the text title for each state
#     maxHeight   : [number] The default visible height size for the target selector
#     delegate    : [object] A wrapper object for callback object [onUpdate]
#     shadow      : [boolean] Determines whether or not the text is cut off by a shadow
#
# Example:
#  args =
#    selector: 'div.partner-review-facilities'
#    text: ['more facilities', 'less facilities']
#    maxHeight: maxheight
#    delegate:
#      onupdate: (section, state) =>
#        console.log(state)
#  new lp.SectionToggle(args)
# 
# Dependencies:
#   jQuery

 
define ['jquery'], ($) ->
 
  class SectionToggle
  
    @version: '0.0.2'
    
    constructor: (@args={}) ->
      @target = $(@args.selector)
      @target.toggleClass('is-open')
      @wrapper = $(@target).find('.js-read-more-wrapper')
      @wrapper.css({'overflow': 'hidden'})
      @includeHandler = @findHeight(@args.maxHeight)
      @addHandler() if @includeHandler

    findHeight: (maxHeight) ->
      nodes = @wrapper.children()
      height = 0
      i = 0
      while ( height < maxHeight )
        height += $(nodes[i]).height()
        i++
      return height >= maxHeight

    addHandler: ->
      @template = "<div class='btn--read-more js-handler'>#{(@args.text)[0]}</div>"
      if @args.shadow
        @template = "<div class='read-more__handler'>" + @template + "</div>"
      @wrapper.append(@template)
      @handler = @target.find 'div.js-handler'
      @bindEvent()
      @close()
    
    bindEvent: ->
      @handler.on 'click', (e) =>
        e.preventDefault()
        if @state is 'close' then @open() else @close()
        @onUpdate()

    open: ->
      @wrapper.css({'max-height': '5000px'})
      # Wait for the transition to finish before taking away the gradient mask
      # TODO: check for transitionend and use that instead if it's available
      setTimeout(=>
        @toggleClasses()
      , 500)
      @setHandlerText(@args.text[1])
      @state = 'open'

    close: ->
      @wrapper.css({'max-height': @args.maxHeight + 'px'})
      @toggleClasses()
      @setHandlerText(@args.text[0])
      @state = 'close'

    setHandlerText: (_text) ->
      @handler.text _text

    onUpdate: ->
      @args.delegate.onUpdate(@,@state, @args.selector) if @args.delegate && @args.delegate.onUpdate

    toggleClasses: ->
      @target.toggleClass('is-open is-closed')
{'fraction_non_alphanumeric': 0.1481615649338888, 'fraction_numerical': 0.005252671617460605, 'mean_word_length': 2.6666666666666665, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2368465', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1663, 'n_words': 447}