aboutsummaryrefslogtreecommitdiffstats
path: root/lib/offline-support.js
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2010-06-17 18:20:03 +0200
committerMatěj Cepl <mcepl@redhat.com>2010-06-17 18:20:03 +0200
commit9279d5bc3902ab0b18af3a3f8440b8ddd884dec5 (patch)
tree1f642276cfbd30c70cae6307253020206d87e235 /lib/offline-support.js
parent620ca44cfa360d6e100215619acab8ae1eb10f34 (diff)
downloadbugzilla-triage-9279d5bc3902ab0b18af3a3f8440b8ddd884dec5.tar.gz
Inheritance works, now there are jetpack-prototype-related bugs
Diffstat (limited to 'lib/offline-support.js')
-rw-r--r--lib/offline-support.js153
1 files changed, 153 insertions, 0 deletions
diff --git a/lib/offline-support.js b/lib/offline-support.js
new file mode 100644
index 0000000..4849bd3
--- /dev/null
+++ b/lib/offline-support.js
@@ -0,0 +1,153 @@
+/*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 FIXME 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) {
+ let 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) {
+ let val = element.value;
+ // console.log("val.toSource() = " + val.toSource());
+ /*
+ * 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 &&
+ // FIXME 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) {
+// console.log("Submit Callback!");
+// if (jetpack.__parent__.navigator.onLine) {
+// let serForm = this
+// .serializeForm(jetpack.tabs.focused.contentWindow.document.forms
+// .namedItem("changeform"));
+//// console.log("serForm:\n" + serForm.toSource());
+// } else {
+// let 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) {
+ // FIXME 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 ""; // FIXME check other HTTP headers to be set
+
+ let bugID = formData.bugNo;
+ let req = new XMLHttpRequest();
+ req.open("POST", formData.action, true);
+ // FIXME 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) {
+ console.log("Sent form for bug " + bugID);
+ 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);
+ });
+ }
+}
+*/ \ No newline at end of file