Skip to content

Instantly share code, notes, and snippets.

@sibsfinx
Created July 23, 2013 11:37
Show Gist options
  • Save sibsfinx/6061721 to your computer and use it in GitHub Desktop.
Save sibsfinx/6061721 to your computer and use it in GitHub Desktop.
scroll messages to bottom + submit by key
$ ->
submitByKey('.j-submit-by-key')
$ ->
messagesEl = $('.j-messages-list')
scrollToBottom messagesEl
$('#new_message_form')
.on 'ajax:success', (evt, data, status, xhr) ->
messagesEl.append xhr.responseText
scrollToBottom messagesEl
.on 'ajax:error', (evt, data, status, xhr) ->
console.log('scrollToBottom AJAX Error')
@scrollToBottom = (el) ->
return if !el || el.length <= 0
lastEl = $(el).children().last()
firstEl = $(el).children().last()
if !lastEl || !firstEl || lastEl.length <= 0
return
else
scrollVal = $(el).children().last().position().top - $(el).children().first().position().top
$(el).scrollTop(scrollVal)
@submitByKey = (inputSelector) ->
allowSend = true
$(inputSelector).focus ->
$(inputSelector).removeClass('j-submit-by-key-active')
$(this).addClass('j-submit-by-key-active')
$(this).on 'keydown', (event) ->
if event.which == 13 && !event.shiftKey && !event.ctrlKey && $(this).val().replace(/^\s+|\s+$/g, "").length > 0 && allowSend
$(this).closest("form").addClass('j-form-active').submit()
$(@).attr("disabled","disabled")
allowSend = false
else
event.preventDefault
$(document).on 'ajax:success', '.j-form-active', (evt, data, status, xhr) ->
$('.j-form-active textarea').removeAttr('disabled').val('')
$('.j-form-active').removeClass('.j-form-active')
allowSend = true
$(inputSelector+'[autofocus="autofocus"]').blur().focus()
= f.input :body, :as => :text, :autofocus => true, :input_html => { :class => 'j-submit-by-key' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment