Skip to content

Commit

Permalink
Improved comment js
Browse files Browse the repository at this point in the history
  • Loading branch information
steveyken committed Jan 23, 2014
1 parent e1bd214 commit aa724c5
Showing 1 changed file with 21 additions and 46 deletions.
67 changes: 21 additions & 46 deletions app/assets/javascripts/crm_comments.js.coffee
Expand Up @@ -4,55 +4,30 @@
# See MIT-LICENSE file or http://www.opensource.org/licenses/mit-license.php
#------------------------------------------------------------------------------

# When comments are added to an entity, disable the add button
# and add a spinner to indicate request is processing
(($) ->

addSpinnerToComments = ->
$('div.new_comment').each ->
container = $(this)
unless container.hasClass('withSpinner')
container.find('form').on 'submit', ->
container.find('form [type=submit]').attr("disabled", "disabled")
container.find('.spinner').show()
container.addClass("withSpinner")
toggleComment = (el) ->
$container = $(el).closest('div.new_comment')
baseId = $container.attr('id').replace('_comment_new', '')
$post = $container.find('#' + baseId + '_post')
$ask = $container.find('#' + baseId + '_ask')
$comment = $container.find('#' + baseId + '_comment_comment')
$post.toggle()
$ask.toggle()
if $comment.is(":visible")
$container.find('form [type=submit]').removeAttr("disabled")
$container.find('.spinner').hide()
crm.textarea_user_autocomplete(baseId + '_comment_comment')
$comment.focus()

toggleComment = (container) ->
baseId = container.attr('id').replace('_comment_new', '')
post = container.find('#' + baseId + '_post')
ask = container.find('#' + baseId + '_ask')
comment = container.find('#' + baseId + '_comment_comment')
post.toggle()
ask.toggle()
if comment.is(":visible")
container.find('form [type=submit]').removeAttr("disabled")
container.find('.spinner').hide()
comment.focus()
# Show/hide the comment form
$(document).on 'click', '.new_comment a.cancel, input[name=post_new_note]', ->
toggleComment(this)

addOpenCloseToComments = ->
$('div.new_comment').each ->
container = $(this)
unless container.hasClass('withOpenClose')
baseId = container.attr('id').replace('_comment_new', '')
post = container.find('#' + baseId + '_post')
ask = container.find('#' + baseId + '_ask')
container.find('.cancel').on 'click', (event) ->
toggleComment(container)
false
new_comment = container.find('#' + baseId + '_post_new_note')
new_comment.on 'click', ->
toggleComment(container)
crm.textarea_user_autocomplete(baseId + '_comment_comment')
container.addClass("withOpenClose")

# Apply when document is loaded
$(document).ready ->
addSpinnerToComments()
addOpenCloseToComments()

# Apply when jquery event (e.g. search) occurs
$(document).ajaxComplete ->
addSpinnerToComments()
addOpenCloseToComments()
# When comment form is submitted, disable the form button and show the spinner
$(document).on 'click', 'div.new_comment [type=submit]', ->
$(this).attr("disabled", "disabled")
$(this).prev('.spinner').show()
$(this).closest('form').submit()

)(jQuery)

0 comments on commit aa724c5

Please sign in to comment.