diff options
-rw-r--r-- | public/javascripts/application.js | 34 | ||||
-rw-r--r-- | public/javascripts/jquery.gitorious_extensions.js | 38 | ||||
-rw-r--r-- | test/javascripts/rails_form_replacer_test.js | 30 |
3 files changed, 69 insertions, 33 deletions
diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 1e7e338..9d31e3a 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -184,39 +184,7 @@ $(document).ready(function() { }); jQuery('abbr.timeago').timeago(); - jQuery.each(jQuery("a[data-request-method]"), function (){ - var action = $(this).attr("href"); - var httpMethod = $(this).attr("data-request-method"); - $(this).attr("onclick", ""); - var newElement = jQuery("<a>"); - newElement.html($(this).html()); - newElement.insertAfter($(this)); - $(this).hide(); - newElement.bind("click", function (){ - newElement.removeClass("waiting").addClass("waiting"); - jQuery.ajax({ - url: action, - type: "post", - data: {"_method": httpMethod}, - success: function (data, status) { - if (httpMethod == "post") { - newElement.html(newElement.html().replace("Start","Stop")); - httpMethod = "delete" - } - else { - newElement.html(newElement.html().replace("Stop","Start")); - httpMethod = "post" - } - - }, - complete: function(xhr, textStatus) { - newElement.removeClass("waiting"); - action = xhr.getResponseHeader("Location"); - } - }); - return false; - }); - }) + jQuery("a[data-request-method]").replaceRailsGeneratedForm(); }); if (!Gitorious) diff --git a/public/javascripts/jquery.gitorious_extensions.js b/public/javascripts/jquery.gitorious_extensions.js index b164716..29f7ba1 100644 --- a/public/javascripts/jquery.gitorious_extensions.js +++ b/public/javascripts/jquery.gitorious_extensions.js @@ -118,3 +118,41 @@ jQuery.fn.slideToggle = function(speed) { }); return $(this); }; + + +// Replace Rails' obtrusive hijacking of a elements with a custom action +jQuery.fn.replaceRailsGeneratedForm = function (options){ + options = jQuery.extend({ + linkName: "start_watching", + backend: jQuery.ajax + }); + var action = $(this).attr("href"); + var httpMethod = $(this).attr("data-request-method"); + var newElement = jQuery("<a>"); + newElement.attr("href", "#" + options.linkName); + newElement.attr("id", options.linkName); + newElement.html($(this).html()); + newElement.insertAfter($(this)); + $(this).hide(); + newElement.bind("click", function (){ + options.backend({ + url: action, + type: "post", + data: {"_method": httpMethod}, + success: function (data, status) { + if (httpMethod == "post") { + newElement.html(newElement.html().replace("Start", "Stop")); + httpMethod = "delete"; + } else { + newElement.html(newElement.html().replace("Stop", "Start")); + httpMethod = "post"; + } + }, + complete: function (xhr, textStatus) { + action = xhr.getResponseHeader("Location"); + } + }); + return false; + }); + return newElement; +}
\ No newline at end of file diff --git a/test/javascripts/rails_form_replacer_test.js b/test/javascripts/rails_form_replacer_test.js new file mode 100644 index 0000000..54b5313 --- /dev/null +++ b/test/javascripts/rails_form_replacer_test.js @@ -0,0 +1,30 @@ +/* + #-- + # Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies) + # + # This program is free software: you can redistribute it and/or modify + # it under the terms of the GNU Affero General Public License as published by + # the Free Software Foundation, either version 3 of the License, or + # (at your option) any later version. + # + # This program is distributed in the hope that it will be useful, + # but WITHOUT ANY WARRANTY; without even the implied warranty of + # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + # GNU Affero General Public License for more details. + # + # You should have received a copy of the GNU Affero General Public License + # along with this program. If not, see <http://www.gnu.org/licenses/>. + #-- +*/ + +TestCase("Replace Rails generated forms", { + "test stuff": function () { + /*DOC += <a data-request-method="delete">Foo</a*/ + var clicked = false; + var backend = function (options){ + clicked = true; + }; + var el = jQuery("a[data-request-method]").replaceRailsGeneratedForm(); + assertEquals("start_watching", el.attr("id")); + } +}); |