@@ -166,6 +166,156 @@ function removeTagBullet(element, hiddenInputId) {
166166
167167}
168168
169+
170+ /**
171+ * Show a popup with a result message from an operation
172+ *
173+ * @param {string } heading Heading for the PopUp
174+ * @param {list } domlist List of DOMELements to add in the "content" section
175+ */
176+ function responsePopUp ( heading , domlist ) {
177+ const popup = document . getElementById ( 'response-popup' ) ;
178+ popup . querySelector ( 'header h2' ) . textContent = heading ;
179+
180+ const content = document . getElementById ( 'response-content' ) ;
181+ content . innerHTML = '' ;
182+ domlist . forEach ( dom => {
183+ content . appendChild ( dom ) ;
184+ } ) ;
185+
186+ // Close actual Popup if open and open Response PopUp
187+ const openPopUp = document . querySelector ( 'dialog[open] header button' )
188+ if ( openPopUp ) {
189+ openPopUp . click ( ) ;
190+ }
191+ openModal ( popup , { 'currentTarget' : { 'dataset' : { } } } ) ;
192+ }
193+
194+ /** Show a popup with an error message from an operation
195+ *
196+ * @param {string } heading Heading for the PopUp
197+ * @param {number } error Error code
198+ * @param {string } responseText Response text from the operation
199+ */
200+ function errorPopUp ( heading , error , responseText ) {
201+ const reason1 = document . createElement ( 'p' ) ;
202+ reason1 . innerHTML = "Fehler " + error ;
203+ reason1 . className = 'error' ;
204+ const reason2 = document . createElement ( 'pre' ) ;
205+ reason2 . innerHTML = responseText ;
206+ responsePopUp ( heading , [ reason1 , reason2 ] ) ;
207+ }
208+
209+ /**
210+ * Sort Table Rows
211+ *
212+ * @param {string } table_id The ID of the tabel to sort rows in
213+ *
214+ * @param {number } col_index The index of the column to compare rows content with
215+ */
216+ function sortTable ( table_id , col_index ) {
217+ const table = document . getElementById ( table_id ) ;
218+ if ( ! table ) {
219+ console . error ( "Table '" , table_id , "' to sort was not found" ) ;
220+ }
221+ if ( typeof col_index == 'undefined' ) {
222+ col_index = 0 ;
223+ }
224+
225+ var switching = true ;
226+ var switchcount = 0 ;
227+ var dir = "asc" ;
228+
229+ while ( switching ) {
230+ // Loop until nothing else was switched
231+ switching = false ;
232+ var rows = table . rows ;
233+
234+ /*
235+ Loop through the actual order of rows and
236+ compare if one pair should be switched.
237+ Break if a pair is found and switch it later.
238+ This loop will be started again after the switch.
239+ */
240+ for ( var i = 1 ; i < ( rows . length - 1 ) ; i ++ ) {
241+ var should_switch = false ;
242+ var x = rows [ i ] . getElementsByTagName ( 'td' ) [ col_index ] ;
243+ var y = rows [ i + 1 ] . getElementsByTagName ( 'td' ) [ col_index ] ;
244+
245+ // Compare values with special handling for numbers
246+ if ( col_index == 0 ) {
247+ // Compare timestamp from dataset
248+ var x_val = Number ( x . getElementsByTagName ( 'input' ) [ 0 ] . dataset . txdate ) ;
249+ var y_val = Number ( y . getElementsByTagName ( 'input' ) [ 0 ] . dataset . txdate ) ;
250+
251+ } else if ( col_index == 5 ) {
252+ // Compare amount (numbers)
253+ var x_val = Number ( x . innerText . slice ( 0 , - 4 ) ) ;
254+ var y_val = Number ( y . innerText . slice ( 0 , - 4 ) ) ;
255+
256+ } else {
257+ // Compare normal innerText
258+ var x_val = x . innerText . toLowerCase ( ) ;
259+ var y_val = y . innerText . toLowerCase ( ) ;
260+ }
261+
262+ // Compare (depending on direction)
263+ if ( dir == "asc" ) {
264+
265+ if ( x_val > y_val ) {
266+ // Switch needed: Brake!
267+ should_switch = true ;
268+ break ;
269+ }
270+
271+ } else if ( dir == "desc" ) {
272+
273+ if ( x_val < y_val ) {
274+ // Switch needed: Brake!
275+ should_switch = true ;
276+ break ;
277+ }
278+
279+ }
280+ }
281+
282+ // Was a needed switch found in the loop?
283+ if ( should_switch ) {
284+ // Yes: Switch it now
285+ rows [ i ] . parentNode . insertBefore ( rows [ i + 1 ] , rows [ i ] ) ;
286+
287+ // Remember that we at least switched one time
288+ switchcount ++ ;
289+
290+ // Reset switching-var to get into the next iteration of the while loop
291+ switching = true ;
292+
293+ } else {
294+ // There is no need for a switch...
295+ if ( switchcount == 0 && dir == "asc" ) {
296+ // ...and there weren't any switches before:
297+ // Change the direction of the sorting if it is still default 'asc'
298+ dir = "desc" ;
299+
300+ // Reset switching-var to get into the next iteration of the while loop
301+ switching = true ;
302+ }
303+ }
304+
305+ }
306+
307+ const table_headings = rows [ 0 ] . getElementsByTagName ( 'th' ) ;
308+ for ( var i = 0 ; i < table_headings . length ; i ++ ) {
309+ table_headings [ i ] . classList . remove ( 'sorted-asc' ) ;
310+ table_headings [ i ] . classList . remove ( 'sorted-desc' ) ;
311+ }
312+ if ( col_index == 0 ) {
313+ table_headings [ 1 ] . classList . add ( 'sorted-' + dir ) ;
314+ } else {
315+ table_headings [ col_index ] . classList . add ( 'sorted-' + dir ) ;
316+ }
317+ }
318+
169319// ----------------------------------------------------------------------------
170320// -- AJAX Functions ----------------------------------------------------------
171321// ----------------------------------------------------------------------------
0 commit comments