Passed
Push — main ( c0b881...db16a9 )
by Thierry
13:47 queued 11:55
created

src/ajax/upload.js   A

Complexity

Total Complexity 9
Complexity/F 3

Size

Lines of Code 55
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 9
eloc 28
mnd 6
bc 6
fnc 3
dl 0
loc 55
rs 10
bpm 2
cpm 3
noi 2
c 0
b 0
f 0
1
/**
2
 * Class: jaxon.ajax.upload
3
 */
4
5
(function(self, dom, console) {
6
    /**
7
     * @param {object} oRequest A request object, created initially by a call to <jaxon.ajax.request.initialize>
8
     * @param {string=} oRequest.upload The HTML file upload field id
9
     *
10
     * @returns {boolean}
11
     */
12
    const initRequest = (oRequest) => {
13
        if (!oRequest.upload) {
14
            return false;
15
        }
16
17
        oRequest.upload = {
18
            id: oRequest.upload,
19
            input: null,
20
            form: null,
21
        };
22
        const input = dom.$(oRequest.upload.id);
23
24
        if (!input) {
25
            console.log('Unable to find input field for file upload with id ' + oRequest.upload.id);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
26
            return false;
27
        }
28
        if (input.type !== 'file') {
29
            console.log('The upload input field with id ' + oRequest.upload.id + ' is not of type file');
30
            return false;
31
        }
32
        if (input.files.length === 0) {
33
            console.log('There is no file selected for upload in input field with id ' + oRequest.upload.id);
34
            return false;
35
        }
36
        if (input.name === undefined) {
37
            console.log('The upload input field with id ' + oRequest.upload.id + ' has no name attribute');
38
            return false;
39
        }
40
        oRequest.upload.input = input;
41
        oRequest.upload.form = input.form;
42
        return true;
43
    };
44
45
    /**
46
     * Check upload data and initialize the request.
47
     *
48
     * @param {object} oRequest A request object, created initially by a call to <jaxon.ajax.request.initialize>
49
     *
50
     * @returns {void}
51
     */
52
    self.initialize = (oRequest) => {
53
        // The content type shall not be set when uploading a file with FormData.
54
        // It will be set by the browser.
55
        if (!initRequest(oRequest)) {
56
            oRequest.postHeaders['content-type'] = oRequest.contentType;
57
        }
58
    }
59
})(jaxon.ajax.upload, jaxon.utils.dom, console);
0 ignored issues
show
Bug introduced by
The variable jaxon seems to be never declared. If this is a global, consider adding a /** global: jaxon */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
60