Passed
Push — main ( fac3c3...502827 )
by Stefan
02:24
created

RichFmConnector.handlePostMessage   B

Complexity

Conditions 8

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 29
rs 7.3333
c 0
b 0
f 0
cc 8
1
/**
2
 * Class to call the rich filemanager from a page.
3
 * Filemanager can be created as independent window or in 'modal' mode
4
 * running in a dynamicaly created iframe.
5
 * The path of the selected image is set as value of an edit field or
6
 * as src of an image element.
7
 * If specified, height and/or widthof the selected image file is set as
8
 * value to the corresponding edit field.
9
 */
10
class RichFmConnector
11
{
12
    /**
13
     * Constructor need the path to the rich filemanager
14
     */
15
    constructor(strRfmPath)
16
    {
17
        this.strRfmPath = strRfmPath;
18
        this.editID = null;
19
        this.imgID = null;
20
        this.imgWidthID = null;
21
        this.imgHeightID = null;
22
        this.oBrowserWnd = null;
23
        this.oBrowserDiv = null;
24
    }
25
26
    /**
27
     * Filebrowser is created inside a independent window.
28
     */
29
    browseWindow(editID, imgID, strExpand)
30
    {
31
        this.editID = editID;
32
        this.imgID = imgID;
33
        
34
        let strBrowser = this.strRfmPath;
35
        if (strExpand != '') {
36
            strBrowser += "?expandedFolder=" + strExpand;
37
        }
38
39
        let iWidth = screen.width * 0.7;
0 ignored issues
show
Bug introduced by
The variable screen seems to be never declared. If this is a global, consider adding a /** global: screen */ 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...
40
        let iHeight = screen.height * 0.7;
41
        let iLeft = (screen.width - iWidth) / 2 ;
42
        let iTop = (screen.height - iHeight) / 2 ;
43
44
        let strOptions = "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,dependent=yes";
45
        strOptions += ",width=" + iWidth ;
46
        strOptions += ",height=" + iHeight ;
47
        strOptions += ",left=" + iLeft ;
48
        strOptions += ",top=" + iTop ;
49
        
50
        this.oBrowserWnd = window.open(strBrowser, 'BrowseWindow', strOptions);
51
        
52
        window.addEventListener('message', (e) => { this.handlePostMessage(e.data); });
53
    }
54
    
55
    browseServerModal(strExpand)
56
    {
57
        let strBrowser = this.strRfmPath;
58
        if (strExpand != '') {
59
            strBrowser += "?expandedFolder=" + strExpand;
60
        }
61
        
62
        this.oBrowserDiv = document.createElement('div');
63
        this.oBrowserDiv.id = 'fm-container';
64
        let oHeader = document.createElement('h1');
65
        oHeader.append(document.createTextNode('Rich Filemanager'));
66
        oHeader.onclick = (e) => { this.handleOnClose(e); };
67
        this.oBrowserDiv.append(oHeader);
68
        let oBrowserIFrame = document.createElement('iframe');
69
        oBrowserIFrame.id = 'fm-iframe';
70
        oBrowserIFrame.src = strBrowser;
71
        this.oBrowserDiv.append(oBrowserIFrame);
72
        document.body.append(this.oBrowserDiv);
73
        // $('body').css('overflow-y', 'hidden');
74
        
75
        window.onmessage = (e) => { this.handlePostMessage(e.data); };
76
    }
77
78
    handlePostMessage(data)
79
    {
80
        if (data.source === 'richfilemanager') {
81
            let oElement = this.getElement(this.editID);
82
            if (oElement) {
83
                oElement.value = data.preview_url;
84
            }
85
            oElement = this.getElement(this.imgID);
86
            if (oElement) {
87
                oElement.src = data.preview_url;
88
            }
89
            oElement = this.getElement(this.imgWidthID);
90
            if (oElement) {
91
                oElement.value = data.ressourceObject.attributes.width;
92
            }
93
            oElement = this.getElement(this.imgHeightID);
94
            if (oElement) {
95
                oElement.value = data.ressourceObject.attributes.height;
96
            }
97
            
98
            if (this.oBrowserWnd !== null) {
99
                this.oBrowserWnd.close();
100
                this.oBrowserWnd = null;
101
            } else if (this.oBrowserDiv !== null) {
102
                this.oBrowserDiv.remove();
103
                this.oBrowserDiv = null;
104
            }
105
        }
106
    }
107
    
108
    handleOnClose(e)
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
109
    {
110
        if (this.oBrowserDiv !== null) {
111
            this.oBrowserDiv.remove();
112
            this.oBrowserDiv = null;
113
        }
114
    }
115
    
116
    getElement(id)
117
    {
118
        var oElement = null;
119
        if (id != null && id != '') {
0 ignored issues
show
Best Practice introduced by
Comparing id to null using the != operator is not safe. Consider using !== instead.
Loading history...
120
            oElement = document.getElementById(id);
121
        }
122
        return oElement;
123
    }
124
}
125