Commit ad1aa517c7052d26e7243f6574b3deacddd0b161

Authored by Robert Speicher
1 parent 3b89f140

Add "enable" and "disable" jQuery functions

Handles (un)setting the disabled attribute and adding/removing the
'disabled' class
app/assets/javascripts/main.js.coffee
@@ -12,26 +12,27 @@ window.disableButtonIfEmptyField = (field_selector, button_selector) -> @@ -12,26 +12,27 @@ window.disableButtonIfEmptyField = (field_selector, button_selector) ->
12 field = $(field_selector) 12 field = $(field_selector)
13 closest_submit = field.closest("form").find(button_selector) 13 closest_submit = field.closest("form").find(button_selector)
14 14
15 - closest_submit.attr("disabled", "disabled").addClass("disabled") if field.val() is "" 15 + closest_submit.disable() if field.val() is ""
16 16
17 field.on "keyup", -> 17 field.on "keyup", ->
18 if $(this).val() is "" 18 if $(this).val() is ""
19 - closest_submit.attr("disabled", "disabled").addClass "disabled" 19 + closest_submit.disable()
20 else 20 else
21 - closest_submit.removeAttr("disabled").removeClass "disabled" 21 + closest_submit.enable()
22 22
23 $ -> 23 $ ->
24 $(".one_click_select").live 'click', -> 24 $(".one_click_select").live 'click', ->
25 $(this).select() 25 $(this).select()
26 26
  27 + # Disable form buttons while a form is submitting
27 $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) -> 28 $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
28 buttons = $('[type="submit"]', this) 29 buttons = $('[type="submit"]', this)
29 30
30 switch e.type 31 switch e.type
31 when 'ajax:beforeSend', 'submit' 32 when 'ajax:beforeSend', 'submit'
32 - buttons.attr('disabled', 'disabled') 33 + buttons.disable()
33 else 34 else
34 - buttons.removeAttr('disabled') 35 + buttons.enable()
35 36
36 # Show/Hide the profile menu when hovering the account box 37 # Show/Hide the profile menu when hovering the account box
37 $('.account-box').hover -> $(this).toggleClass('hover') 38 $('.account-box').hover -> $(this).toggleClass('hover')
@@ -81,4 +82,12 @@ $ -> @@ -81,4 +82,12 @@ $ ->
81 $.extend default_options, options 82 $.extend default_options, options
82 _chosen.apply this, [default_options] 83 _chosen.apply this, [default_options]
83 84
  85 + # Disable an element and add the 'disabled' Bootstrap class
  86 + $.fn.extend disable: ->
  87 + $(this).attr('disabled', 'disabled').addClass('disabled')
  88 +
  89 + # Enable an element and remove the 'disabled' Bootstrap class
  90 + $.fn.extend enable: ->
  91 + $(this).removeAttr('disabled').removeClass('disabled')
  92 +
84 )(jQuery) 93 )(jQuery)
app/assets/javascripts/note.js
@@ -25,11 +25,11 @@ var NoteList = { @@ -25,11 +25,11 @@ var NoteList = {
25 $(this).closest('li').fadeOut(); }); 25 $(this).closest('li').fadeOut(); });
26 26
27 $(".note-form-holder").live("ajax:before", function(){ 27 $(".note-form-holder").live("ajax:before", function(){
28 - $(".submit_note").attr("disabled", "disabled"); 28 + $(".submit_note").disable()
29 }) 29 })
30 30
31 $(".note-form-holder").live("ajax:complete", function(){ 31 $(".note-form-holder").live("ajax:complete", function(){
32 - $(".submit_note").removeAttr("disabled"); 32 + $(".submit_note").enable()
33 }) 33 })
34 34
35 disableButtonIfEmptyField(".note-text", ".submit_note"); 35 disableButtonIfEmptyField(".note-text", ".submit_note");