Passed
Push — master ( d2e487...b38026 )
by Tobias
05:31
created

Resources/public/js/editInPlace.js   A

Complexity

Total Complexity 19
Complexity/F 2.38

Size

Lines of Code 143
Function Count 8

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 83
c 0
b 0
f 0
dl 0
loc 143
rs 10
wmc 19
mnd 11
bc 11
fnc 8
bpm 1.375
cpm 2.375
noi 16

1 Function

Rating   Name   Duplication   Size   Complexity  
D editInPlace.js ➔ doSave 0 30 12
1
/*
2
 * This file is part of the PHP Translation package.
3
 *
4
 * (c) PHP Translation team <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
(function () {
10
    if (typeof customElements.define !== "undefined") {
0 ignored issues
show
Bug introduced by
The variable customElements seems to be never declared. If this is a global, consider adding a /** global: customElements */ 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...
11
        customElements.define("x-trans", HTMLElement);
0 ignored issues
show
Bug introduced by
The variable HTMLElement seems to be never declared. If this is a global, consider adding a /** global: HTMLElement */ 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...
12
13
        return;
14
    }
15
16
    document.registerElement("x-trans", {
17
        prototype: Object.create(HTMLElement.prototype)
18
    });
19
})();
20
21
/**
22
 * TranslationBundleEditInPlace boot the ContentTools editor and handle saves.
23
 *
24
 * @author Damien Alexandre <[email protected]>
25
 * @param saveUrl The AJAX API Endpoint
26
 */
27
var TranslationBundleEditInPlace = function(saveUrl) {
28
    var editor, httpRequest;
29
30
    /* Tools for HTML blocks - no image or video support */
31
    var HTML_TOOLS = [
32
        [
33
            'bold',
34
            'italic',
35
            'link',
36
            'align-left',
37
            'align-center',
38
            'align-right'
39
        ], [
40
            'heading',
41
            'subheading',
42
            'paragraph',
43
            'unordered-list',
44
            'ordered-list',
45
            'table',
46
            'indent',
47
            'unindent',
48
            'line-break'
49
        ], [
50
            'undo',
51
            'redo',
52
            'remove'
53
        ]
54
    ];
55
56
    /* Tools for basic string, no HTML allowed */
57
    var STRING_TOOLS = [
58
        [
59
            'undo',
60
            'redo'
61
        ]
62
    ];
63
64
    // Set the default to SIMPLE
65
    ContentTools.DEFAULT_TOOLS = STRING_TOOLS;
0 ignored issues
show
Bug introduced by
The variable ContentTools seems to be never declared. If this is a global, consider adding a /** global: ContentTools */ 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...
66
    editor = ContentTools.EditorApp.get();
67
    editor.init('x-trans', 'data-key', function(domRegion) {
68
        // true = fixture (string of text)
69
        // false = classic HTML block
70
        return domRegion.dataset.key.split('.').pop() !== 'html';
71
    });
72
73
    // Treat x-trans tags as Text
74
    ContentEdit.TagNames.get().register(ContentEdit.Text, 'x-trans');
0 ignored issues
show
Bug introduced by
The variable ContentEdit seems to be never declared. If this is a global, consider adding a /** global: ContentEdit */ 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...
75
76
    // Save to backend
77
    editor.addEventListener('saved', function(ev) {
78
        if (Object.keys(ev.detail().regions).length === 0) {
79
            return;
80
        }
81
82
        doSave(ev.detail().regions);
83
    });
84
85
    function doSave(regions) {
86
        editor.busy(true);
87
88
        httpRequest = new XMLHttpRequest();
89
        if (!httpRequest) {
90
            alert('Giving up :( Cannot create an XMLHTTP instance');
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
91
            return false;
92
        }
93
94
        httpRequest.onreadystatechange = function() {
95
            if (httpRequest.readyState === XMLHttpRequest.DONE) {
96
                editor.busy(false);
97
98
                if (httpRequest.status === 200) {
99
                    new ContentTools.FlashUI('ok');
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new ContentTools.FlashUI("ok") is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
Bug introduced by
The variable ContentTools seems to be never declared. If this is a global, consider adding a /** global: ContentTools */ 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...
100
                } else if (httpRequest.status === 400) {
101
                    alert(httpRequest.responseText);
0 ignored issues
show
Debugging Code Best Practice introduced by
The alert UI element is often considered obtrusive and is generally only used as a temporary measure. Consider replacing it with another UI element.
Loading history...
102
                    new ContentTools.FlashUI('no');
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new ContentTools.FlashUI("no") is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
103
                } else {
104
                    alert('Error: we could not save the translations! Please retry.');
105
                    new ContentTools.FlashUI('no');
0 ignored issues
show
Unused Code Best Practice introduced by
The object created with new ContentTools.FlashUI("no") is not used but discarded. Consider invoking another function instead of a constructor if you are doing this purely for side effects.
Loading history...
106
                }
107
            }
108
        };
109
110
        httpRequest.open('POST', saveUrl, true);
111
        httpRequest.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
112
        httpRequest.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
113
        httpRequest.send(JSON.stringify(regions));
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
114
    }
115
116
    // On focus, change the tools
117
    ContentEdit.Root.get().bind('focus', function(element) {
118
        // @todo Display the translation key & placeholder somewhere in the UI?
119
        var tools;
120
121
        if (element.isFixed()) {
122
            tools = STRING_TOOLS;
123
        } else {
124
            tools = HTML_TOOLS;
125
        }
126
127
        if (editor.toolbox().tools() !== tools) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if editor.toolbox().tools() !== tools is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
128
            return editor.toolbox().tools(tools);
129
        }
130
    });
131
132
    // Any click on links / button... should prevent default if editing is on
133
    document.addEventListener('click', function(event) {
134
        event = event || window.event;
135
        var target = event.target || event.srcElement;
136
137
        while (target) {
138
            // Disable the default behavior on some active elements
139
            if (target instanceof HTMLAnchorElement || target instanceof HTMLButtonElement || target instanceof HTMLLabelElement) {
0 ignored issues
show
Bug introduced by
The variable HTMLLabelElement seems to be never declared. If this is a global, consider adding a /** global: HTMLLabelElement */ 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...
Bug introduced by
The variable HTMLAnchorElement seems to be never declared. If this is a global, consider adding a /** global: HTMLAnchorElement */ 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...
Bug introduced by
The variable HTMLButtonElement seems to be never declared. If this is a global, consider adding a /** global: HTMLButtonElement */ 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...
140
                if(ContentTools.EditorApp.get().isEditing()) {
0 ignored issues
show
Bug introduced by
The variable ContentTools seems to be never declared. If this is a global, consider adding a /** global: ContentTools */ 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...
141
                    // Link or button found, prevent default!
142
                    event.preventDefault();
143
                    event.stopPropagation();
144
                }
145
                break;
146
            }
147
148
            target = target.parentNode;
149
        }
150
    }, true);
151
};
152