/*jslint onevar: false, browser: true, evil: true, laxbreak: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, maxerr: 1000, immed: false, white: false, plusplus: false, regexp: false, undef: false */
/*global jetpack */
// Released under the MIT/X11 license
// http://www.opensource.org/licenses/mit-license.php
"use strict";
/* Offline supporting functions */
/**
*
* @todo TODO this probably makes a closure and a memory leak name='changeform'
* investigate
* https://developer.mozilla.org/en/How_to_Turn_Off_Form_Autocompletion
*
* <form method="post" action="process_bug.cgi" autocomplete="off">
*
* Reading
* http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13
* random notes: - 17.13.3 provides all steps necessary - enctype !=
* application/x-www-form-urlencoded => SHOULD fails (no further questions
* needed) - http://www.w3.org/MarkUp/html-spec/html-spec_8.html#SEC8.2.1. is
* nice explanation (albeit quite dated) - on multiple values
* http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.6.1 -
* příliš jednoduché
* http://www.innovation.ch/java/HTTPClient/emulating_forms.html -
*/
RHBugzillaPage.prototype.serializeForm = function(form) {
var serialForm = {
dataOut : "",
name : form.name,
method : form.method,
acceptCharset : form.acceptCharset,
action : form.action, // TODO shouldn't we get a non-relative URL?
enctype : form.enctype,
cookie : this.doc.cookie,
autocomplete : form.getAttribute("autocomplete"),
bugNo : this.bugNo
};
function genURIElement(sName, sValue) {
return encodeURIComponent(sName) + "=" + encodeURIComponent(sValue);
}
/**
* @param o
* control to be serialized
* @return String with the serialized control
*/
function serializeControl(element) {
var val = element.value;
/*
* on HTMLSelectElement we have an attribute 'type' of type DOMString,
* readonly The type of this form control. This is the string
* "select-multiple" when the multiple attribute is true and the string
* "select-one" when false.
*/
if ((val == null) || (val == undefined) || (val == "")) {
return;
} else if (val instanceof Array) {
return val.map(function(x) {
return genURIElement(element.name, x.value);
}).join("&");
} else if (val instanceof String) {
return genURIElement(element.name, val);
} else { // assume HTMLCollection
return Array.map(val, function(x) {
return genURIElement(element.name, x.value);
}).join("&");
}
}
serialForm.dataOut = Array.filter(form.elements,function(el) {
return !el.disabled && el.name &&
// TODO shouldn't I just add && el.value here?
(el.checked || /select|textarea/i.test(el.nodeName) ||
/text|hidden|password|search/i.test(el.type));
}).map(serializeControl).join("&");
return serialForm;
};
//RHBugzillaPage.prototype.submitCallback = function(evt) {
// if (jetpack.__parent__.navigator.onLine) {
// var serForm = this
// .serializeForm(jetpack.tabs.focused.contentWindow.document.forms
// .namedItem("changeform"));
// } else {
// var serForm = this
// .serializeForm(jetpack.tabs.focused.contentWindow.document.forms
// .namedItem("changeform"));
// myStorage.forms[this.bugNo] = serForm;
// evt.stopPropagation();
// evt.preventDefault();
// }
//};
/**
*
*
* Yes, this is correct, this is NOT method of RHBugzillaPage!
*/
/*function onlineCallback() {
function deserializeAndSend(formData) {
// TODO notImplemented
// is it enough to just
// run XMLHttpRequest? Probably yes, this is just a form
// and this is just a HTTP request
// it is probably better to get already processed
// application/x-www-form-urlencoded
// see http://htmlhelp.com/reference/html40/forms/form.html for details
// and also https://developer.mozilla.org/en/AJAX/Getting_Started
// what's?
// https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference\
// /Global_Functions/encodeURI & co.
// this seems to be also interesting
// https://developer.mozilla.org/en/Code_snippets/Post_data_to_window
console.error("Sending bugs not implemented yet!");
return ""; // TODO check other HTTP headers to be set
var bugID = formData.bugNo;
var req = new XMLHttpRequest();
req.open("POST", formData.action, true);
// TODO co očekávám za odpověď? req.overrideMimeType("text/xml");
// * Accept-Encoding
// * Accept-Language
// * Accept (MIME types)
req.setRequestHeader("Connection", "keep-alive");
req.setRequestHeader("Keep-Alive", 300);
req.setRequestHeader("Content-Type", formData.enctype);
req.setRequestHeader("Referer", bugURL + bugID);
req.setRequestHeader("Accept-Charset", formData.acceptCharset);
req.setRequestHeader("Cookie", formData.cookie);
req.onreadystatechange = function(aEvt) {
if (req.readyState == 4) {
if (req.status == 200) {
delete myStorage.forms[bugID];
} else {
console.error("Sending form for bug " + bugID + "failed!");
}
}
};
req.send(formData.data);
}
if (myStorage.forms.length > 0) {
myStorage.forms.forEach(function(x) {
deserializeAndSend(x);
});
}
}
*/