robguy21
1/14/2016 - 6:46 PM

word_limit.coffee

do () ->
  if ['competitions-page'].indexOf(processWire.template) != -1

    $textarea = $('.story-js')
    timer = null

    checkLength = (str) ->
      if str.split(' ').length > 500
        $('#wrap_story').trigger('storyexceedsbounds')

    limitString = (str) ->
      strArr = str.split(' ').slice(0, 500)
      strWords = strArr.join(' ')
      strChars = strWords.length

      return strChars

    $textarea.on 'keyup', (e) ->
      if timer
        clearTimeout timer

      timer = setTimeout checkLength.bind(null, this.value), 150

    $('#wrap_story').on 'storyexceedsbounds', () ->
      currCharLimit = limitString($('#story').val())
      textAreaVal = $('.story-js').val().substr(0, currCharLimit)
      $('.story-js').val(textAreaVal)