form.js (7482B)
1 2 /** 3 * Form. 4 * 5 * @constructor 6 */ 7 Openrat.Form = function() { 8 9 const modes = { 10 showBrowserNotice : 1, 11 keepOpen : 2, 12 closeAfterSubmit : 4, 13 closeAfterSuccess : 8, 14 }; 15 16 this.setLoadStatus = function( isLoading ) { 17 $(this.element).closest('div.content').toggleClass('loader',isLoading); 18 } 19 20 this.initOnElement = function( element ) { 21 this.element = element; 22 23 let form = this; 24 25 // Autosave in Formularen. 26 // Bei Veränderungen von Checkboxen wird das Formular sofort abgeschickt. 27 if ( $(this.element).data('autosave') ) { 28 29 $(this.element).find('input[type="checkbox"]').click( function() { 30 form.submit(modes.keepOpen); 31 }); 32 $(this.element).find('select').change( function() { 33 form.submit(modes.keepOpen); 34 }); 35 } 36 37 // After click to "OK" the form is submitted. 38 // Why this?? input type=submit will submit! 39 /* 40 $(event.target).find('input.submit.ok').click( function() { 41 $(this).closest('form').submit(); 42 }); 43 */ 44 45 $(element).find('.or-form-btn--cancel').click( function() { 46 form.cancel(); 47 48 }); 49 $(element).find('.or-form-btn--reset').click( function() { 50 form.rollback(); 51 52 }); 53 $(element).find('.or-form-btn--apply').click( function() { 54 form.submit(modes.keepOpen); 55 }); 56 57 // Submithandler for the whole form. 58 $(element).submit( function( event ) { 59 60 // 61 if ($(this).data('target')=='view') 62 { 63 form.submit(); 64 event.preventDefault(); 65 } 66 // target=top will load the native way without javascript. 67 }); 68 } 69 70 this.cancel = function() { 71 //$(this.element).html('').parent().removeClass('is-open'); 72 this.close(); 73 } 74 75 76 this.rollback = function() { 77 this.element.trigger('reset'); 78 } 79 80 this.close = function() { 81 82 } 83 84 this.forwardTo = function (action, subaction, id, data) { 85 86 let view = new Openrat.View( action, subaction, id, data ); 87 view.start( $(this.element).closest('.view') ); 88 } 89 90 this.submit = function( mode ) { 91 92 if ( mode === undefined ) 93 if ( $(this.element).data('async') ) 94 mode = modes.closeAfterSubmit; 95 else 96 mode = modes.closeAfterSuccess; 97 98 99 // Show progress 100 let status = $('<div class="notice info"><div class="text loader"></div></div>'); 101 $('#noticebar').prepend(status); // Notice anhängen. 102 $(status).show(); 103 104 // Alle vorhandenen Error-Marker entfernen. 105 // Falls wieder ein Fehler auftritt, werden diese erneut gesetzt. 106 $(this.element).find('.error').removeClass('error'); 107 108 let params = $(this.element).serializeArray(); 109 let data = {}; 110 $(params).each(function(index, obj){ 111 data[obj.name] = obj.value; 112 }); 113 114 // If form does not contain action/id, get it from the workbench. 115 if (!data.id) 116 data.id = Openrat.Workbench.state.id; 117 if (!data.action) 118 data.action = Openrat.Workbench.state.action; 119 120 let formMethod = $(this.element).attr('method').toUpperCase(); 121 122 if ( formMethod == 'GET' ) 123 { 124 // Mehrseitiges Formular 125 // Die eingegebenen Formulardaten werden zur nächsten Action geschickt. 126 this.forwardTo( data.action, data.subaction,data.id,data ); 127 $(status).remove(); 128 } 129 else 130 { 131 let url = './api/'; // Alle Parameter befinden sich im Formular 132 133 // POST-Request 134 this.setLoadStatus(true); 135 //url += '?output=json'; 136 url += ''; 137 //params['output'] = 'json';// Irgendwie geht das nicht. 138 data.output = 'json'; 139 140 if ( mode == modes.closeAfterSubmit ) 141 this.close(); 142 // Async: Window is closed, but the action will be startet now. 143 144 let form = this; 145 $.ajax( { 'type':'POST',url:url, data:data, success:function(data, textStatus, jqXHR) 146 { 147 form.setLoadStatus(false); 148 $(status).remove(); 149 150 form.doResponse(data,textStatus,form.element, function() { 151 152 // The data was successful saved. 153 // Now we can close the form. 154 if ( mode == modes.closeAfterSuccess ) 155 { 156 form.close(); 157 158 // clear the dirty flag. 159 $(form.element).closest('div.panel').find('div.header ul.views li.action.active').removeClass('dirty'); 160 } 161 162 let afterSuccess = $(form.element).data('afterSuccess'); 163 let async = $(form.element).data('async' ); 164 if ( afterSuccess ) 165 { 166 if ( afterSuccess == 'reloadAll' ) 167 { 168 Openrat.Workbench.reloadAll(); 169 } 170 } else { 171 if ( async ) 172 ; // do not reload 173 else 174 Openrat.Workbench.reloadViews(); 175 } 176 177 }); 178 }, 179 error:function(jqXHR, textStatus, errorThrown) { 180 form.setLoadStatus(false); 181 $(status).remove(); 182 183 try 184 { 185 let error = jQuery.parseJSON( jqXHR.responseText ); 186 Openrat.Workbench.notify('','','error',error.error,[error.description]); 187 } 188 catch( e ) 189 { 190 let msg = jqXHR.responseText; 191 Openrat.Workbench.notify('','','error','Server Error',[msg]); 192 } 193 194 195 } 196 197 } ); 198 $(form.element).fadeIn(); 199 } 200 201 } 202 203 204 205 /** 206 * HTTP-Antwort auf einen POST-Request auswerten. 207 * 208 * @param data Formulardaten 209 * @param status Status 210 * @param element 211 */ 212 this.doResponse = function(data,status,element, onSuccess = $.noop ) 213 { 214 if ( status != 'success' ) 215 { 216 alert('Server error: ' + status); 217 return; 218 } 219 220 let form = this; 221 // Hinweismeldungen in Statuszeile anzeigen 222 $.each(data['notices'], function(idx,value) { 223 224 // Bei asynchronen Requests wird zusätzlich eine Browser-Notice erzeugt, da der 225 // Benutzer bei länger laufenden Aktionen vielleicht das Tab oder Fenster 226 // gewechselt hat. 227 let notifyBrowser = $(element).data('async'); 228 229 Openrat.Workbench.notify(value.type, value.name, value.status, value.text, value.log, notifyBrowser ); // Notice anhängen. 230 231 if ( value.status == 'ok' ) // Kein Fehler? 232 { 233 onSuccess(); 234 Openrat.Workbench.dataChangedHandler.fire(); 235 } 236 else 237 // Server liefert Fehler zurück. 238 { 239 } 240 }); 241 242 // Felder mit Fehleingaben markieren, ggf. das übergeordnete Fieldset aktivieren. 243 $.each(data['errors'], function(idx,value) { 244 $('input[name='+value+']').addClass('error').parent().addClass('error').parents('fieldset').addClass('show').addClass('open'); 245 }); 246 247 // Jetzt das erhaltene Dokument auswerten. 248 249 250 if ( data.control.redirect ) 251 // Redirect 252 window.location.href = data.control.redirect; 253 } 254 255 256 257 258 } 259