From 83b92d9ef01cdf640eb7f88e218569e6a5aebbdd Mon Sep 17 00:00:00 2001 From: Craig Andrews Date: Wed, 11 Nov 2009 15:37:17 -0500 Subject: [PATCH] Refactored the favorite button ajaxification to a separate function --- js/util.js | 83 +++++++++++++++++++++++++++++------------------------- 1 file changed, 45 insertions(+), 38 deletions(-) diff --git a/js/util.js b/js/util.js index 2165957c3b..879bb38aec 100644 --- a/js/util.js +++ b/js/util.js @@ -81,32 +81,6 @@ $(document).ready(function(){ // XXX: refactor this code - var favoptions = { dataType: 'xml', - beforeSubmit: function(data, target, options) { - $(target).addClass('processing'); - return true; - }, - success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); - var dis = new_form.id; - var fav = dis.replace('disfavor', 'favor'); - $('form#'+fav).replaceWith(new_form); - $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden); - } - }; - - var disoptions = { dataType: 'xml', - beforeSubmit: function(data, target, options) { - $(target).addClass('processing'); - return true; - }, - success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); - var fav = new_form.id; - var dis = fav.replace('favor', 'disfavor'); - $('form#'+dis).replaceWith(new_form); - $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden); - } - }; - var joinoptions = { dataType: 'xml', success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); var leave = new_form.id; @@ -125,20 +99,8 @@ $(document).ready(function(){ } }; - function addAjaxHidden() { - var ajax = document.createElement('input'); - ajax.setAttribute('type', 'hidden'); - ajax.setAttribute('name', 'ajax'); - ajax.setAttribute('value', 1); - this.appendChild(ajax); - } - - $("form.form_favor").ajaxForm(favoptions); - $("form.form_disfavor").ajaxForm(disoptions); $("form.form_group_join").ajaxForm(joinoptions); $("form.form_group_leave").ajaxForm(leaveoptions); - $("form.form_favor").each(addAjaxHidden); - $("form.form_disfavor").each(addAjaxHidden); $("form.form_group_join").each(addAjaxHidden); $("form.form_group_leave").each(addAjaxHidden); @@ -252,6 +214,7 @@ $(document).ready(function(){ $('#'+li.id).fadeIn(2500); NoticeReply(); NoticeAttachments(); + NoticeFavors(); } } $("#notice_data-text").val(""); @@ -270,8 +233,52 @@ $(document).ready(function(){ NoticeReply(); NoticeAttachments(); NoticeDataAttach(); + NoticeFavors(); }); +function addAjaxHidden() { + var ajax = document.createElement('input'); + ajax.setAttribute('type', 'hidden'); + ajax.setAttribute('name', 'ajax'); + ajax.setAttribute('value', 1); + this.appendChild(ajax); +} + +function NoticeFavors() { + + // XXX: refactor this code + var favoptions = { dataType: 'xml', + beforeSubmit: function(data, target, options) { + $(target).addClass('processing'); + return true; + }, + success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); + var dis = new_form.id; + var fav = dis.replace('disfavor', 'favor'); + $('form#'+fav).replaceWith(new_form); + $('form#'+dis).ajaxForm(disoptions).each(addAjaxHidden); + } + }; + + var disoptions = { dataType: 'xml', + beforeSubmit: function(data, target, options) { + $(target).addClass('processing'); + return true; + }, + success: function(xml) { var new_form = document._importNode($('form', xml).get(0), true); + var fav = new_form.id; + var dis = fav.replace('favor', 'disfavor'); + $('form#'+dis).replaceWith(new_form); + $('form#'+fav).ajaxForm(favoptions).each(addAjaxHidden); + } + }; + + $("form.form_favor").ajaxForm(favoptions); + $("form.form_disfavor").ajaxForm(disoptions); + $("form.form_favor").each(addAjaxHidden); + $("form.form_disfavor").each(addAjaxHidden); +} + function NoticeReply() { if ($('#notice_data-text').length > 0 && $('#content .notice_reply').length > 0) { $('#content .notice').each(function() {