From ad1aa517c7052d26e7243f6574b3deacddd0b161 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Sun, 9 Sep 2012 07:08:13 -0400 Subject: [PATCH] Add "enable" and "disable" jQuery functions Handles (un)setting the disabled attribute and adding/removing the 'disabled' class --- app/assets/javascripts/main.js.coffee | 19 ++++++++++++++----- app/assets/javascripts/note.js | 4 ++-- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/app/assets/javascripts/main.js.coffee b/app/assets/javascripts/main.js.coffee index a614cf5b..84129069 100644 --- a/app/assets/javascripts/main.js.coffee +++ b/app/assets/javascripts/main.js.coffee @@ -12,26 +12,27 @@ window.disableButtonIfEmptyField = (field_selector, button_selector) -> field = $(field_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", -> if $(this).val() is "" - closest_submit.attr("disabled", "disabled").addClass "disabled" + closest_submit.disable() else - closest_submit.removeAttr("disabled").removeClass "disabled" + closest_submit.enable() $ -> $(".one_click_select").live 'click', -> $(this).select() + # Disable form buttons while a form is submitting $('body').on 'ajax:complete, ajax:beforeSend, submit', 'form', (e) -> buttons = $('[type="submit"]', this) switch e.type when 'ajax:beforeSend', 'submit' - buttons.attr('disabled', 'disabled') + buttons.disable() else - buttons.removeAttr('disabled') + buttons.enable() # Show/Hide the profile menu when hovering the account box $('.account-box').hover -> $(this).toggleClass('hover') @@ -81,4 +82,12 @@ $ -> $.extend default_options, 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) diff --git a/app/assets/javascripts/note.js b/app/assets/javascripts/note.js index 160f75c9..79ab086b 100644 --- a/app/assets/javascripts/note.js +++ b/app/assets/javascripts/note.js @@ -25,11 +25,11 @@ var NoteList = { $(this).closest('li').fadeOut(); }); $(".note-form-holder").live("ajax:before", function(){ - $(".submit_note").attr("disabled", "disabled"); + $(".submit_note").disable() }) $(".note-form-holder").live("ajax:complete", function(){ - $(".submit_note").removeAttr("disabled"); + $(".submit_note").enable() }) disableButtonIfEmptyField(".note-text", ".submit_note");