Commit ad1aa517 authored by Robert Speicher's avatar Robert Speicher

Add "enable" and "disable" jQuery functions

Handles (un)setting the disabled attribute and adding/removing the
'disabled' class
parent 3b89f140
...@@ -12,26 +12,27 @@ window.disableButtonIfEmptyField = (field_selector, button_selector) -> ...@@ -12,26 +12,27 @@ window.disableButtonIfEmptyField = (field_selector, button_selector) ->
field = $(field_selector) field = $(field_selector)
closest_submit = field.closest("form").find(button_selector) closest_submit = field.closest("form").find(button_selector)
closest_submit.attr("disabled", "disabled").addClass("disabled") if field.val() is "" closest_submit.disable() if field.val() is ""
field.on "keyup", -> field.on "keyup", ->
if $(this).val() is "" if $(this).val() is ""
closest_submit.attr("disabled", "disabled").addClass "disabled" closest_submit.disable()
else else
closest_submit.removeAttr("disabled").removeClass "disabled" closest_submit.enable()
$ -> $ ->
$(".one_click_select").live 'click', -> $(".one_click_select").live 'click', ->
$(this).select() $(this).select()
# Disable form buttons while a form is submitting
$('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) -> $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) ->
buttons = $('[type="submit"]', this) buttons = $('[type="submit"]', this)
switch e.type switch e.type
when 'ajax:beforeSend', 'submit' when 'ajax:beforeSend', 'submit'
buttons.attr('disabled', 'disabled') buttons.disable()
else else
buttons.removeAttr('disabled') buttons.enable()
# Show/Hide the profile menu when hovering the account box # Show/Hide the profile menu when hovering the account box
$('.account-box').hover -> $(this).toggleClass('hover') $('.account-box').hover -> $(this).toggleClass('hover')
...@@ -81,4 +82,12 @@ $ -> ...@@ -81,4 +82,12 @@ $ ->
$.extend default_options, options $.extend default_options, options
_chosen.apply this, [default_options] _chosen.apply this, [default_options]
# Disable an element and add the 'disabled' Bootstrap class
$.fn.extend disable: ->
$(this).attr('disabled', 'disabled').addClass('disabled')
# Enable an element and remove the 'disabled' Bootstrap class
$.fn.extend enable: ->
$(this).removeAttr('disabled').removeClass('disabled')
)(jQuery) )(jQuery)
...@@ -25,11 +25,11 @@ var NoteList = { ...@@ -25,11 +25,11 @@ var NoteList = {
$(this).closest('li').fadeOut(); }); $(this).closest('li').fadeOut(); });
$(".note-form-holder").live("ajax:before", function(){ $(".note-form-holder").live("ajax:before", function(){
$(".submit_note").attr("disabled", "disabled"); $(".submit_note").disable()
}) })
$(".note-form-holder").live("ajax:complete", function(){ $(".note-form-holder").live("ajax:complete", function(){
$(".submit_note").removeAttr("disabled"); $(".submit_note").enable()
}) })
disableButtonIfEmptyField(".note-text", ".submit_note"); disableButtonIfEmptyField(".note-text", ".submit_note");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment