var RedirectHttp = { <% configs.each do |(key, value)| %> <%= key %>: <%= value.to_json %>, <% end %> // Generates a redirection input with the specified value. Inputs // have names like '<%= prefix %>[key]' to prevent collisions and // facilitate parsing server-side. redirect_input: function(key, value) { var input = document.createElement("input"); input.setAttribute("id", '<%= prefix %>[' + key + ']'); input.setAttribute("name", '<%= prefix %>[' + key + ']'); input.setAttribute("type", 'hidden'); input.setAttribute("value", value); return input; }, // Redirect the form, adds the redirection inputs, and then submits the // form, if submit is true or undefined. After submit, the redirection // inputs are removed from the form. redirect_form: function(form, submit) { if(typeof submit == 'undefined') { submit = true }; if(submit) { // form.submit() raises an error if an input is named submit // (note you're hosed if the input is not type=submit) var submit_inputs = []; var inputs = document.getElementsByTagName('input'); for(i=0; isubmit'); submit_inputs.push(input); } } // set redirection inputs var original_action = form.getAttribute('<%= prefix %>original_action'); var redirect_inputs = [ <% configs.each do |(key, value)| %> RedirectHttp.redirect_input('<%= key %>', RedirectHttp.<%= key %>), <% end %> RedirectHttp.redirect_input('original_action', original_action) ]; form.setAttribute('action', '<%= redirect_action %>'); for(i=0; ioriginal_href'); redirect_params = [ <% configs.each do |(key, value)| %> escape('<%= prefix %>[<%= key %>]') + "=" + escape(RedirectHttp.<%= key %>), <% end %> '<%= prefix %>[original_action]=' + escape(original_href) ]; location.href = '<%= redirect_action %>?' + redirect_params.join("&").replace(/%20/g, "+"); if(RedirectHttp.submit_request) { this.revert(); location.href = link.getAttribute("href"); }; }; }, // Wraps the current onX event so that it may be called without interfering // with the redirection (ie wrapper). Note events are not called again // when a form is submitted or a redirection is performed by the wrapper. wrap: function(current, wrapper) { return "try {" + " var <%= prefix %>result = function(<%= prefix %>this) { " + current.replace(/(\W)this(?=\W)/g, "$1<%= prefix %>this") + " }(this);" + " RedirectHttp." + wrapper + "(this, <%= prefix %>result);" + "} catch(ex) {" + "alert('Error during redirection: ' + ex.message + '\\nOriginal onsubmit: " + current.replace(/'/g, "\\'") + "');" + "}; return false;" }, // Redirects forms and links to '<%= redirect_action %>' redirect: function() { try { // redirect forms to the redirect_action var forms = document.getElementsByTagName('form'); for(i=0; i if(form.getAttribute("class") != "<%= prefix %>form") { // store the original action form.setAttribute("<%= prefix %>original_action", form.getAttribute('action')); // redirect onSubmit var current_on_submit = ''; if(form.hasAttribute("onSubmit")) current_on_submit = form.getAttribute("onSubmit"); var on_submit = RedirectHttp.wrap(current_on_submit, 'redirect_form'); form.setAttribute("onSubmit", on_submit); form.setAttribute("<%= prefix %>original_callback", current_on_submit); } } // redirect links to the redirect_action var links = document.getElementsByTagName('a'); for(i=0; i if(link.getAttribute("class") != "<%= prefix %>link") { // store the original href link.setAttribute("<%= prefix %>original_href", link.getAttribute("href")); // redirect onClick var current_on_click = ''; if(link.hasAttribute("onClick")) current_on_click = link.getAttribute("onClick"); var on_click = RedirectHttp.wrap(current_on_click, 'redirect_link'); link.setAttribute("onClick", on_click); link.setAttribute("<%= prefix %>original_callback", current_on_click); }; } } catch(ex) { alert_error(ex); } }, // Reverts redirection and removes the redirection div revert: function() { try { // redirect forms back to original actions var forms = document.getElementsByTagName('form'); for(i=0; ioriginal_action')) { form.setAttribute('action', form.getAttribute('<%= prefix %>original_action')); form.removeAttribute('<%= prefix %>original_action'); form.setAttribute('onSubmit', form.getAttribute('<%= prefix %>original_callback')); form.removeAttribute('<%= prefix %>original_callback'); } } // redirect links back to original hrefs var links = document.getElementsByTagName('a'); for(i=0; ioriginal_href')) { link.setAttribute('href', link.getAttribute('<%= prefix %>original_href')); link.removeAttribute('<%= prefix %>original_href'); link.setAttribute('onClick', link.getAttribute('<%= prefix %>original_callback')); link.removeAttribute('<%= prefix %>original_callback'); } } } catch(ex) { alert_error(ex); } // remove self var redirect_http = document.getElementById("<%= prefix %>") if(redirect_http) document.body.removeChild(redirect_http); }, // Alerts errors due to RedirectHttp alert_error: function(ex) { alert("Redirect HTTP Error: " + ex.message + "\n\n The webpage may have been corrupted; be sure to refresh."); } };