7 215
editací
(Založena nová stránka s textem „// ajaxSubmit // Submit a form through Ajax. Doesn't handle file uploads yet. // // Parameters: // form DOM element The form to submi…“) |
Bez shrnutí editace |
||
Řádek 18: | Řádek 18: | ||
function ajaxSubmit (form, button, func, want_result) | function ajaxSubmit (form, button, func, want_result) | ||
{ | { | ||
if (want_result && (!func || typeof (func) != 'function' || func.length < 1)) { | if (want_result && (!func || typeof (func) != 'function' || func.length < 1)) { | ||
/**** TODO: improve error handling: should throw an exception! */ | /**** TODO: improve error handling: should throw an exception! */ | ||
Řádek 29: | Řádek 29: | ||
return; | return; | ||
} | } | ||
var is_simple = false; | var is_simple = false; | ||
// True if it's a GET request, or if the form is 'application/x-www-form-urlencoded' | // True if it's a GET request, or if the form is 'application/x-www-form-urlencoded' | ||
var boundary = null; | var boundary = null; | ||
// Otherwise, it's 'multipart/form-data', and the multipart delimiter is 'boundary' | // Otherwise, it's 'multipart/form-data', and the multipart delimiter is 'boundary' | ||
function encode_entry (name, value) | function encode_entry (name, value) | ||
{ | { | ||
Řádek 46: | Řádek 46: | ||
+ value.replace(/\r?\n/g, '\r\n') + '\r\n'; // RFC 2046: newlines always must be represented as CR-LF | + value.replace(/\r?\n/g, '\r\n') + '\r\n'; // RFC 2046: newlines always must be represented as CR-LF | ||
} | } | ||
function encode_field (element) | function encode_field (element) | ||
{ | { | ||
Řádek 53: | Řádek 53: | ||
return encode_entry (name, element.value); | return encode_entry (name, element.value); | ||
} | } | ||
function form_add_argument (args, field) | function form_add_argument (args, field) | ||
{ | { | ||
Řádek 63: | Řádek 63: | ||
return args + field; | return args + field; | ||
} | } | ||
var request; | var request; | ||
if (window.LAPI && window.LAPI.Ajax && window.LAPI.Ajax.getRequest) { | if (window.LAPI && window.LAPI.Ajax && window.LAPI.Ajax.getRequest) { | ||
Řádek 85: | Řádek 85: | ||
} | } | ||
// Encode the field values | // Encode the field values | ||
var is_get = method == 'GET'; | var is_get = method == 'GET'; | ||
var encoding = form.getAttribute ('enctype'); | var encoding = form.getAttribute ('enctype'); | ||
Řádek 94: | Řádek 94: | ||
is_simple = | is_simple = | ||
is_get || !encoding || encoding == 'application/x-www-form-urlencoded'; | is_get || !encoding || encoding == 'application/x-www-form-urlencoded'; | ||
var args = ""; | var args = ""; | ||
var boundary_string = '----' + mw.config.get('wgArticleId')+mw.config.get('wgCurRevisionId') + 'auto_submit_by_lupo'; | var boundary_string = '----' + mw.config.get('wgArticleId')+mw.config.get('wgCurRevisionId') + 'auto_submit_by_lupo'; | ||
boundary = null; | boundary = null; | ||
if (!is_simple) boundary = '--' + boundary_string; | if (!is_simple) boundary = '--' + boundary_string; | ||
for (var i = 0; i < form.elements.length; i++) { | for (var i = 0; i < form.elements.length; i++) { | ||
var element = form.elements[i]; | var element = form.elements[i]; | ||
Řádek 137: | Řádek 137: | ||
if (button && button.form == form && button.type == 'submit') | if (button && button.form == form && button.type == 'submit') | ||
args = form_add_argument (args, encode_field (button)); | args = form_add_argument (args, encode_field (button)); | ||
// Close the multipart request | // Close the multipart request | ||
if (!is_simple && args.length > 0) args = args + boundary; | if (!is_simple && args.length > 0) args = args + boundary; | ||
if (method == 'GET') { | if (method == 'GET') { | ||
uri = uri + (uri.indexOf('?') < 0 ? '?' : '&') + args; args = null; | uri = uri + (uri.indexOf('?') < 0 ? '?' : '&') + args; args = null; | ||
Řádek 170: | Řádek 170: | ||
request.send (args); | request.send (args); | ||
} | } | ||
// submitAndClose | // submitAndClose | ||
// Submit a form and close the window containing it as soon as the request has been | // Submit a form and close the window containing it as soon as the request has been |