raimund@587: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz raimund@587: * Software engineering by Intevation GmbH raimund@587: * raimund@587: * This file is Free Software under the GNU GPL (v>=3) raimund@587: * and comes with ABSOLUTELY NO WARRANTY! Check out raimund@587: * the documentation coming with IMIS-Labordaten-Application for details. raimund@587: */ raimund@587: dustin@742: /** dustin@742: * A Controller for a Probe form dustin@742: */ raimund@587: Ext.define('Lada.controller.form.Probe', { raimund@587: extend: 'Ext.app.Controller', raimund@587: dustin@742: /** dustin@742: * Initialize the Controller dustin@742: * It has 4 listeners dustin@742: */ raimund@587: init: function() { raimund@587: this.control({ raimund@587: 'probeform button[action=save]': { raimund@587: click: this.save raimund@587: }, raimund@587: 'probeform button[action=discard]': { raimund@587: click: this.discard raimund@587: }, raimund@587: 'probeform': { raimund@587: dirtychange: this.dirtyForm dustin@717: }, dustin@717: 'probeform [xtype="datetime"] field': { dustin@717: blur: this.checkDate raimund@587: } raimund@587: }); raimund@587: }, raimund@587: dustin@742: /** dustin@742: * The save function saves the content of the Location form. dustin@742: * On success it will reload the Store, dustin@742: * on failure, it will display an Errormessage dustin@742: */ raimund@587: save: function(button) { raimund@587: var formPanel = button.up('form'); raimund@587: var data = formPanel.getForm().getFieldValues(true); raimund@587: for (var key in data) { raimund@587: formPanel.getForm().getRecord().set(key, data[key]); raimund@587: } raimund@587: formPanel.getForm().getRecord().save({ raimund@587: success: function(record, response) { raimund@587: var json = Ext.decode(response.response.responseText); raimund@620: if (json) { raimund@587: button.setDisabled(true); raimund@587: button.up('toolbar').down('button[action=discard]') raimund@587: .setDisabled(true); raimund@587: formPanel.clearMessages(); raimund@587: formPanel.setRecord(record); raimund@587: formPanel.setMessages(json.errors, json.warnings); raimund@639: if (response.action === 'create' && json.success) { raimund@639: button.up('window').close(); raimund@639: var win = Ext.create('Lada.view.window.ProbeEdit', { raimund@639: record: record raimund@639: }); raimund@639: win.show(); raimund@639: win.initData(); raimund@639: } raimund@587: } raimund@587: }, raimund@587: failure: function(record, response) { raimund@587: button.setDisabled(true); raimund@587: button.up('toolbar').down('button[action=discard]') raimund@587: .setDisabled(true); raimund@678: var rec = formPanel.getForm().getRecord(); raimund@678: rec.dirty = false; raimund@728: formPanel.getForm().loadRecord(record); raimund@587: var json = response.request.scope.reader.jsonData; raimund@587: if (json) { dustin@693: if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){ dustin@693: formPanel.setMessages(json.errors, json.warnings); dustin@693: } dustin@693: dustin@693: if(json.message){ dustin@701: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title') dustin@701: +' #'+json.message, dustin@693: Lada.getApplication().bundle.getMsg(json.message)); dustin@701: } else { dustin@701: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@701: Lada.getApplication().bundle.getMsg('err.msg.generic.body')); dustin@693: } dustin@720: formPanel.clearMessages(); raimund@728: //formPanel.setRecord(record); dustin@720: formPanel.setMessages(json.errors, json.warnings); dustin@701: } else { dustin@701: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@701: Lada.getApplication().bundle.getMsg('err.msg.response.body')); raimund@587: } dustin@720: raimund@587: } raimund@587: }); raimund@587: }, raimund@587: dustin@742: /** dustin@742: * The discard function resets the Location form dustin@742: * to its original state. dustin@742: */ raimund@587: discard: function(button) { raimund@587: var formPanel = button.up('form'); raimund@587: formPanel.getForm().loadRecord(formPanel.getForm().getRecord()); raimund@587: }, raimund@587: dustin@742: /** dustin@742: * The dirtyForm function enables or disables the save and discard dustin@742: * button which are present in the toolbar of the form. dustin@742: * The Buttons are only active if the content of the form was altered dustin@742: * (the form is dirty). dustin@742: * In Additon it calls the disableChildren() function of the window dustin@742: * embedding the form. Likewise it calls the embedding windows dustin@742: * enableChilren() function dustin@742: */ raimund@587: dirtyForm: function(form, dirty) { raimund@587: if (dirty) { raimund@587: form.owner.down('button[action=save]').setDisabled(false); raimund@587: form.owner.down('button[action=discard]').setDisabled(false); dustin@686: form.owner.up('window').disableChildren(); raimund@587: } raimund@587: else { raimund@587: form.owner.down('button[action=save]').setDisabled(true); raimund@587: form.owner.down('button[action=discard]').setDisabled(true); dustin@686: form.owner.up('window').enableChildren(); // todo this might not be true in all cases raimund@587: } dustin@717: }, dustin@717: dustin@742: /** dustin@742: * checkDate() is called when a xtype=datetime field was modified dustin@742: * It checks for two things: dustin@742: * - Is the date in the future dustin@742: * - Does the date belong to a time period and the end is before start dustin@742: * In both cases it adds a warning to the field which was checked. dustin@742: */ dustin@717: checkDate: function(field) { dustin@717: var now = Date.now(); dustin@717: var w = 0 //amount of warnings dustin@717: var e = 0 //errors dustin@717: var emsg = ''; dustin@717: var wmsg = ''; dustin@717: dustin@717: if (field.getValue() > now){ dustin@717: wmsg += Lada.getApplication().bundle.getMsg('661'); dustin@717: w++; dustin@717: } dustin@717: // This field might be a field within a DateTime-Period. dustin@717: // Search for Partner field (period: end/start) and validate dustin@717: // End Before Start validation dustin@717: if (field.period) { dustin@717: var partners = new Array(); dustin@717: partners[0] = field.up('fieldset').down('datetime[period=start]').down().getValue() dustin@717: partners[1] = field.up('fieldset').down('datetime[period=end]').down().getValue() dustin@717: if (partners[0] && partners[1] && partners[0] > partners [1]) { dustin@717: var msg = Lada.getApplication().bundle.getMsg('662'); dustin@717: field.up('fieldset').showWarningOrError(true, msg, false, ''); dustin@717: } else { dustin@717: field.up('fieldset').clearMessages(); dustin@717: } dustin@717: } dustin@717: dustin@717: if (w) { dustin@717: field.up().showWarnings(wmsg); dustin@717: } dustin@717: if (e) { dustin@717: field.up().showErrors(emsg); dustin@717: } dustin@717: dustin@717: // Clear Warnings or Errors if none Are Present dustin@717: if (w == 0 && e == 0) { dustin@717: field.up().clearWarningOrError(); dustin@717: } raimund@587: } raimund@587: });