dustin@757: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz dustin@757: * Software engineering by Intevation GmbH dustin@757: * dustin@757: * This file is Free Software under the GNU GPL (v>=3) dustin@757: * and comes with ABSOLUTELY NO WARRANTY! Check out dustin@757: * the documentation coming with IMIS-Labordaten-Application for details. dustin@757: */ dustin@757: dustin@757: /** dustin@757: * A Controller for a Probe form dustin@757: */ dustin@757: Ext.define('Lada.controller.form.Messprogramm', { dustin@757: extend: 'Ext.app.Controller', dustin@757: dustin@757: /** dustin@757: * Initialize the Controller dustin@757: */ dustin@757: init: function() { dustin@757: this.control({ dustin@757: 'messprogrammform button[action=save]': { dustin@757: click: this.save dustin@757: }, dustin@757: 'messprogrammform button[action=discard]': { dustin@757: click: this.discard dustin@757: }, dustin@757: 'messprogrammform': { dustin@757: dirtychange: this.dirtyForm dustin@757: }, dustin@777: 'messprogrammform datetime textfield': { dustin@777: blur: this.checkDatePeriod dustin@774: }, dustin@777: 'messprogrammform [name="teilintervallVon"]': { dustin@757: change: this.synchronizeSlider, dustin@757: blur: this.checkPeriod dustin@757: }, dustin@777: 'messprogrammform [name="teilintervallBis"]': { dustin@777: change: this.synchronizeSlider, dustin@777: blur: this.checkPeriod dustin@777: }, dustin@777: 'messprogrammform probenintervall combobox': { dustin@757: select: this.updateIntervalls raimund@784: }, raimund@784: 'messprogrammform panel[xtype="deskriptor] combobox': { raimund@784: select: this.deskriptorSelect dustin@757: } dustin@757: }); dustin@757: }, dustin@757: /** dustin@757: * When the Probenintervall was changed, update the Sliders dustin@757: * and the the numberfield. dustin@757: */ dustin@757: updateIntervalls: function(field, records) { dustin@757: console.log('update Intervalls'); dustin@757: var form = field.up('messprogrammform'); dustin@757: var record = form.getRecord(); dustin@757: form.populateIntervall(record, field.getValue()); dustin@757: }, dustin@757: dustin@757: /** dustin@757: * When the Slider was used, dustin@757: * update the Value of the Teilintervallfields dustin@757: */ dustin@757: synchronizeFields: function(slider, newValue, thumb) { dustin@757: console.log('Synchronize Fields'); dustin@757: var formPanel = slider.up('form'); dustin@757: if (thumb.index == 0) { dustin@757: formPanel.getForm() dustin@757: .findField('teilintervallVon') dustin@757: .setValue(newValue); dustin@757: } dustin@757: else if (thumb.index == 1) { dustin@757: formPanel.getForm() dustin@757: .findField('teilintervallBis') dustin@757: .setValue(newValue); dustin@757: } dustin@757: dustin@757: }, dustin@757: dustin@757: /** dustin@757: * When the IntervallFields were used, dustin@757: * update the Slider dustin@757: */ dustin@757: synchronizeSlider: function(field, newValue, oldValue) { dustin@757: console.log('Synchronize Slider'); dustin@757: var formPanel = field.up('form'); dustin@757: if (field.name == 'teilintervallVon') { dustin@757: formPanel.down('probenintervallslider') dustin@757: .setValue(0, newValue, false); dustin@757: } dustin@757: else if (field.name == 'teilintervallBis') { dustin@757: formPanel.down('probenintervallslider') dustin@757: .setValue(1, newValue, false); dustin@757: } dustin@757: dustin@757: }, dustin@757: /** dustin@757: * The save function saves the content of the Location form. dustin@757: * On success it will reload the Store, dustin@757: * on failure, it will display an Errormessage dustin@757: */ dustin@757: save: function(button) { dustin@757: var formPanel = button.up('form'); dustin@757: var data = formPanel.getForm().getFieldValues(true); dustin@757: for (var key in data) { dustin@757: formPanel.getForm().getRecord().set(key, data[key]); dustin@757: } dustin@757: formPanel.getForm().getRecord().save({ dustin@757: success: function(record, response) { dustin@757: var json = Ext.decode(response.response.responseText); dustin@757: if (json) { dustin@757: button.setDisabled(true); dustin@757: button.up('toolbar').down('button[action=discard]') dustin@757: .setDisabled(true); dustin@757: formPanel.clearMessages(); dustin@757: formPanel.setRecord(record); dustin@757: formPanel.setMessages(json.errors, json.warnings); dustin@757: if (response.action === 'create' && json.success) { dustin@757: button.up('window').close(); dustin@759: var win = Ext.create('Lada.view.window.Messprogramm', { dustin@757: record: record dustin@757: }); dustin@757: win.show(); dustin@757: win.initData(); dustin@757: } dustin@757: } dustin@757: }, dustin@757: failure: function(record, response) { dustin@757: button.setDisabled(true); dustin@757: button.up('toolbar').down('button[action=discard]') dustin@757: .setDisabled(true); dustin@757: var rec = formPanel.getForm().getRecord(); dustin@757: rec.dirty = false; dustin@757: formPanel.getForm().loadRecord(record); dustin@757: var json = response.request.scope.reader.jsonData; dustin@757: if (json) { dustin@757: if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){ dustin@757: formPanel.setMessages(json.errors, json.warnings); dustin@757: } dustin@757: dustin@757: if(json.message){ dustin@757: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title') dustin@757: +' #'+json.message, dustin@757: Lada.getApplication().bundle.getMsg(json.message)); dustin@757: } else { dustin@757: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@757: Lada.getApplication().bundle.getMsg('err.msg.generic.body')); dustin@757: } dustin@757: formPanel.clearMessages(); dustin@757: //formPanel.setRecord(record); dustin@757: formPanel.setMessages(json.errors, json.warnings); dustin@757: } else { dustin@757: Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.save.title'), dustin@757: Lada.getApplication().bundle.getMsg('err.msg.response.body')); dustin@757: } dustin@757: dustin@757: } dustin@757: }); dustin@757: }, dustin@757: dustin@757: /** dustin@757: * The discard function resets the Location form dustin@757: * to its original state. dustin@757: */ dustin@757: discard: function(button) { dustin@757: var formPanel = button.up('form'); dustin@757: formPanel.getForm().loadRecord(formPanel.getForm().getRecord()); dustin@757: formPanel.getForm().owner.populateIntervall( dustin@757: formPanel.getForm().getRecord()); dustin@757: }, dustin@757: dustin@757: /** dustin@757: * The dirtyForm function enables or disables the save and discard dustin@757: * button which are present in the toolbar of the form. dustin@757: * The Buttons are only active if the content of the form was altered dustin@757: * (the form is dirty). dustin@757: */ dustin@757: dirtyForm: function(form, dirty) { dustin@757: if (dirty) { dustin@757: form.owner.down('button[action=save]').setDisabled(false); dustin@757: form.owner.down('button[action=discard]').setDisabled(false); dustin@757: } dustin@757: else { dustin@757: form.owner.down('button[action=save]').setDisabled(true); dustin@757: form.owner.down('button[action=discard]').setDisabled(true); dustin@757: } dustin@757: }, dustin@757: dustin@757: /** dustin@757: * checkPeriod() is called when a fields defining an intervall dustin@757: * were modified dustin@757: * The function validates if the start is smaller than end. dustin@757: */ dustin@757: checkPeriod: function(field) { dustin@757: // This field might be a field within a Period. dustin@757: // Search for Partner field (period: end/start) and validate dustin@757: // End Before Start validation dustin@757: if (field.period) { dustin@757: var partners = new Array(); dustin@757: partners[0] = field.up('fieldset').down('numberfield[period=start]').getValue() dustin@757: partners[1] = field.up('fieldset').down('numberfield[period=end]').getValue() dustin@757: if (partners[0] && partners[1] && partners[0] > partners [1]) { dustin@757: var msg = Lada.getApplication().bundle.getMsg('662'); dustin@757: field.up('fieldset').showWarningOrError(true, msg, false, ''); dustin@757: } else { dustin@757: field.up('fieldset').clearMessages(); dustin@757: } dustin@757: } dustin@777: }, dustin@777: dustin@777: /** dustin@777: * checkDatePeriod() is called when a fields defining an intervall dustin@777: * were modified dustin@777: * The function validates if the start is smaller than end. dustin@777: * Same as checkPeriod but requires DATETIME fields dustin@777: */ dustin@777: checkDatePeriod: function(field) { dustin@777: // This field might be a field within a Period. dustin@777: // Search for Partner field (period: end/start) and validate dustin@777: // End Before Start validation dustin@777: if (field.period) { dustin@777: var partners = new Array(); dustin@777: partners[0] = field.up('fieldset') dustin@777: .down('datetime[period=start]') dustin@777: .down('textfield') dustin@777: .getValue() dustin@777: partners[1] = field.up('fieldset') dustin@777: .down('datetime[period=end]') dustin@777: .down('textfield') dustin@777: .getValue() dustin@777: if (partners[0] && partners[1] && partners[0] > partners [1]) { dustin@777: var msg = Lada.getApplication().bundle.getMsg('662'); dustin@777: field.up('fieldset').showWarningOrError(true, msg, false, ''); dustin@777: } else { dustin@777: field.up('fieldset').clearMessages(); dustin@777: } dustin@777: } raimund@784: }, raimund@784: raimund@784: deskriptorSelect: function(field, records) { raimund@784: var desk = field.up('deskriptor'); raimund@784: var media = field.up('messprogrammform').down('textfield[name="mediaDesk"]'); raimund@784: var current = media.getValue().split(' '); raimund@784: this.clearChildDesk(field.up('deskriptor'), current); raimund@784: if (current.length < 13) { raimund@784: for (var i = 0; i <= 12; i++) { raimund@784: if (i === 0) { raimund@784: current.push('D:'); raimund@784: } raimund@784: else if (i === desk.layer + 1) { raimund@784: var value; raimund@784: if (records[0].get('sn') < 10) { raimund@784: value = '0' + records[0].get('sn'); raimund@784: } raimund@784: else { raimund@784: value = records[0].get('sn'); raimund@784: } raimund@784: current.push(value); raimund@784: } raimund@784: else { raimund@784: current.push('00'); raimund@784: } raimund@784: } raimund@784: } raimund@784: else { raimund@784: var value; raimund@784: if (records[0].get('sn') < 10) { raimund@784: value = '0' + records[0].get('sn'); raimund@784: } raimund@784: else { raimund@784: value = records[0].get('sn'); raimund@784: } raimund@784: current[desk.layer + 1] = value; raimund@784: } raimund@784: media.setValue(current.join(' ').trim()); raimund@784: }, raimund@784: raimund@784: clearChildDesk: function(field, media) { raimund@784: var allS = field.up('fieldset').items.items; raimund@784: for (var i = field.layer + 1; i < 12; i++) { raimund@784: allS[i].clearValue(); raimund@784: media[i + 1] = '00'; raimund@784: } raimund@784: } raimund@784: });