aboutsummaryrefslogtreecommitdiffstats
path: root/lib/offline-support.js
blob: e0b1c3b2998803501cc07c0c1d6eee714627253a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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 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;
        // 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 &&
            // 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) {
//    console.log("Submit Callback!");
//    if (jetpack.__parent__.navigator.onLine) {
//        var serForm = this
//                .serializeForm(jetpack.tabs.focused.contentWindow.document.forms
//                        .namedItem("changeform"));
////        console.log("serForm:\n" + serForm.toSource());
//    } 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) {
                    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);
        });
    }
}
*/