1 | /** |
||
2 | * Make an ajax call to edit element |
||
3 | * @param el |
||
4 | */ |
||
5 | function editTranslation(el) { |
||
6 | var xmlhttp = new XMLHttpRequest(); |
||
7 | |||
8 | xmlhttp.onreadystatechange = function() { |
||
9 | if (xmlhttp.readyState == XMLHttpRequest.DONE ) { |
||
10 | |||
11 | var resultDiv = el.parentElement.getElementsByClassName("ajax-result")[0]; |
||
12 | if (xmlhttp.status == 200) { |
||
13 | resultDiv.className += ' success'; |
||
14 | resultDiv.innerHTML = xmlhttp.responseText; |
||
15 | } |
||
16 | else if (xmlhttp.status == 400) { |
||
17 | resultDiv.className += ' error'; |
||
18 | resultDiv.innerHTML = xmlhttp.responseText; |
||
19 | } |
||
20 | else { |
||
21 | resultDiv.className += ' error'; |
||
22 | resultDiv.innerHTML = "Unknown error"; |
||
23 | } |
||
24 | |||
25 | setTimeout(function() {removeResultElement(resultDiv);}, 6000); |
||
26 | } |
||
27 | }; |
||
28 | |||
29 | xmlhttp.open("POST", editUrl, true); |
||
0 ignored issues
–
show
|
|||
30 | xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
||
31 | xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
||
32 | xmlhttp.send(JSON.stringify({message: el.value, key: el.getAttribute("data-key")})); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * Create a new translation |
||
37 | * @param el |
||
38 | * @param url |
||
39 | * @returns {boolean} |
||
40 | */ |
||
41 | function createTranslation(el, url) { |
||
42 | var xmlhttp = new XMLHttpRequest(); |
||
43 | var messageInput = document.getElementById('create-message'); |
||
44 | var keyInput = document.getElementById('create-key'); |
||
45 | |||
46 | xmlhttp.onreadystatechange = function() { |
||
47 | if (xmlhttp.readyState == XMLHttpRequest.DONE ) { |
||
48 | var errorDiv = el.getElementsByClassName("ajax-result")[0]; |
||
49 | |||
50 | if (xmlhttp.status == 200) { |
||
51 | messageInput.value = ""; |
||
52 | keyInput.value = ""; |
||
53 | |||
54 | var resultDiv = document.getElementById("new-translations"); |
||
55 | resultDiv.innerHTML = xmlhttp.responseText + resultDiv.innerHTML; |
||
56 | } |
||
57 | else if (xmlhttp.status == 400) { |
||
58 | errorDiv.className += ' error'; |
||
59 | errorDiv.innerHTML = xmlhttp.responseText; |
||
60 | } |
||
61 | else { |
||
62 | errorDiv.className += ' error'; |
||
63 | errorDiv.innerHTML = "Unknown error"; |
||
64 | } |
||
65 | |||
66 | setTimeout(function() {removeResultElement(errorDiv);}, 6000); |
||
67 | } |
||
68 | }; |
||
69 | |||
70 | xmlhttp.open("POST", url, true); |
||
71 | xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
||
72 | xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
||
73 | xmlhttp.send(JSON.stringify({message: messageInput.value, key: keyInput.value})); |
||
74 | |||
75 | return false; |
||
76 | } |
||
77 | |||
78 | |||
79 | /** |
||
80 | * Delete a translation. |
||
81 | * @param el |
||
82 | */ |
||
83 | function deleteTranslation(el) { |
||
84 | var xmlhttp = new XMLHttpRequest(); |
||
85 | var messageKey = el.getAttribute("data-key"); |
||
86 | |||
87 | xmlhttp.onreadystatechange = function() { |
||
88 | if (xmlhttp.readyState == XMLHttpRequest.DONE ) { |
||
89 | var row = document.getElementById(messageKey); |
||
90 | var errorDiv = row.getElementsByClassName("ajax-result")[0]; |
||
91 | |||
92 | if (xmlhttp.status == 200) { |
||
93 | row.parentNode.removeChild(row); |
||
94 | } |
||
95 | else if (xmlhttp.status == 400) { |
||
96 | errorDiv.className += ' error'; |
||
97 | errorDiv.innerHTML = xmlhttp.responseText; |
||
98 | } |
||
99 | else { |
||
100 | errorDiv.className += ' error'; |
||
101 | errorDiv.innerHTML = "Unknown error"; |
||
102 | } |
||
103 | |||
104 | setTimeout(function() {removeResultElement(errorDiv);}, 6000); |
||
105 | } |
||
106 | }; |
||
107 | |||
108 | xmlhttp.open("DELETE", editUrl, true); |
||
0 ignored issues
–
show
The variable
editUrl seems to be never declared. If this is a global, consider adding a /** global: editUrl */ 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. ![]() |
|||
109 | xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8"); |
||
110 | xmlhttp.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); |
||
111 | xmlhttp.send(JSON.stringify({key: messageKey})); |
||
112 | } |
||
113 | |||
114 | /** |
||
115 | * Remove the result element |
||
116 | * |
||
117 | * @param el |
||
118 | */ |
||
119 | function removeResultElement(el) { |
||
120 | el.innerHTML = ''; |
||
121 | el.className = "ajax-result"; |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * Toggle visibility of an element |
||
126 | * @param id |
||
127 | */ |
||
128 | function toggleElement(id) { |
||
129 | var el = document.getElementById(id); |
||
130 | if (el.offsetParent === null) { |
||
131 | el.classList.add("show"); |
||
132 | } else { |
||
133 | el.classList.remove("show"); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Show all empty messages |
||
139 | */ |
||
140 | function showOnlyEmptyMessages(el) { |
||
0 ignored issues
–
show
|
|||
141 | var elements = document.getElementsByClassName('message'); |
||
142 | for (var i = 0; i < elements.length; ++i) { |
||
143 | var element = elements[i]; |
||
144 | element.classList.add("d-none"); |
||
145 | } |
||
146 | elements = document.getElementsByClassName('empty'); |
||
147 | for (var i = 0; i < elements.length; ++i) { |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
i already seems to be declared on line 142 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared. ![]() |
|||
148 | var element = elements[i]; |
||
0 ignored issues
–
show
Comprehensibility
Naming
Best Practice
introduced
by
The variable
element already seems to be declared on line 143 . Consider using another variable name or omitting the var keyword.
This check looks for variables that are declared in multiple lines. There may be several reasons for this. In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs. If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared. ![]() |
|||
149 | element.classList.remove("d-none"); |
||
150 | } |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Show all messages |
||
155 | */ |
||
156 | function showAllMessages(el) { |
||
0 ignored issues
–
show
|
|||
157 | var elements = document.getElementsByClassName('message'); |
||
158 | for (var i = 0; i < elements.length; ++i) { |
||
159 | var element = elements[i]; |
||
160 | element.classList.remove("d-none"); |
||
161 | } |
||
162 | } |
||
163 |
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.