torsten@472: /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz torsten@472: * Software engineering by Intevation GmbH torsten@472: * torsten@472: * This file is Free Software under the GNU GPL (v>=3) torsten@472: * and comes with ABSOLUTELY NO WARRANTY! Check out torsten@472: * the documentation coming with IMIS-Labordaten-Application for details. torsten@472: */ torsten@472: torsten@283: /** torsten@283: * Generic Lada specific form. torsten@283: * torsten@283: * See http://moduscreate.com/expert-ext-js-model-integration-in-forms/ for torsten@283: * more details torsten@283: */ torsten@66: Ext.define('Lada.view.widgets.LadaForm', { torsten@66: extend: 'Ext.form.Panel', torsten@66: torsten@66: alias: 'widget.ladaform', torsten@283: bodyPadding: '10 10', torsten@283: border: 0, torsten@66: torsten@66: /** torsten@66: * Can be a reference to a model instance or a model class name. torsten@66: */ torsten@66: model: null, torsten@66: /** torsten@66: * Set to the id of the model instance and the model will be loaded for you. torsten@66: * Only applicable if model provided is a model class name (string). torsten@66: */ torsten@66: modelId: null, torsten@283: /** torsten@283: * List of errors in the form. Typically set after the server validates the form submission torsten@283: */ torsten@75: errors: null, torsten@283: /** torsten@283: * List of warnings in the form. Typically set after the server validates the form submission torsten@283: */ torsten@75: warnings: null, torsten@283: /** torsten@283: * The generic (error) message for the form. Typically set after the server validates the form submission torsten@283: */ torsten@75: message: null, torsten@283: /** torsten@283: * Flag to indicate if the validation succeeds. Typically set after the server validates the form submission torsten@283: */ torsten@133: success: null, torsten@283: /** torsten@283: * Flag to indicate if the form should be rendered in readonly mode. Will torsten@283: * be set after calling the {setReadOnly} function. torsten@283: */ torsten@87: readonly: false, torsten@75: torsten@66: initComponent: function() { raimund@491: this.callParent(arguments); torsten@66: torsten@66: this.getForm().trackResetOnLoad = true; //Workaround torsten@66: torsten@66: if (Ext.isString(this.model)) { torsten@66: //Load a model to be updated torsten@66: if (this.modelId) { torsten@66: Ext.ClassManager.get(this.model).load(this.modelId, { torsten@66: failure: this.onModelLoadFailure, torsten@66: success: this.onModelLoadSuccess, torsten@66: scope: this torsten@66: }); torsten@66: //Load an empty record to be inserted raimund@491: } raimund@491: else { torsten@66: this.bindModel(Ext.create(this.model, {})); torsten@66: } raimund@491: } raimund@491: else { torsten@66: torsten@66: //Bind the provided model to be updated torsten@66: this.bindModel(this.model); torsten@66: torsten@66: } raimund@491: this.addEvents( raimund@491: 'loadsuccess', raimund@491: 'loadfailure', raimund@491: 'savesuccess', raimund@491: 'savefailure'); torsten@66: }, torsten@66: torsten@66: bindModel: function(model) { torsten@66: this.model = model; torsten@66: this.loadRecord(model); torsten@189: // Set the form to readonly if the models readonly attribute is torsten@211: // true torsten@211: if (model.get('readonly') === true) { torsten@189: this.setReadOnly(true); torsten@189: } torsten@66: }, torsten@66: torsten@66: commit: function(callback, scope) { torsten@84: if (this.form.isDirty() && this.form.isValid()) { torsten@66: this.form.updateRecord(this.model); torsten@66: torsten@133: var data = this.model.getAllData(); torsten@133: var baseUrl = this.model.getProxy().url; torsten@133: var url = baseUrl; torsten@133: var method = "POST"; torsten@133: if (this.model.getId()) { raimund@490: url += this.model.getId(); torsten@133: method = "PUT"; torsten@133: } torsten@133: torsten@133: Ext.Ajax.request({ torsten@133: url: url, torsten@133: jsonData: data, torsten@133: method: method, torsten@133: callback: function(option, success, response) { torsten@133: this.parseResponse(response); torsten@133: if (this.success) { torsten@66: console.log('Save was successfull'); torsten@255: this.fireEvent('savesuccess', this, this.model, response); raimund@491: } raimund@491: else { torsten@66: console.log('Save was not successfull'); torsten@75: this.form.markInvalid(this.errors); torsten@255: this.fireEvent('savefailure', this, this.model, response); torsten@66: } torsten@66: }, torsten@66: scope: this torsten@66: }); torsten@66: } torsten@66: }, torsten@66: torsten@66: onModelLoadSuccess: function(record, operation) { torsten@66: this.bindModel(record); rrenkert@406: this.parseResponse(operation.response); torsten@66: this.fireEvent('loadsuccess', this, record, operation); torsten@66: }, torsten@66: torsten@66: onModelLoadFailure: function(record, operation) { rrenkert@406: this.parseResponse(operation.response); torsten@66: this.fireEvent('loadfailure', this, record, operation); torsten@74: }, torsten@74: torsten@74: translateReturnCodes: function(codes) { torsten@74: var translated = {}; torsten@74: for (var k in codes) { torsten@74: translated[k] = Lada.getApplication().bundle.getMsg(codes[k]); torsten@74: } torsten@74: return translated; torsten@83: }, torsten@280: /** torsten@280: * Will set the form into readonly state. torsten@280: * @param {Boolean} Flag to indicate if the form should be set to readonly torsten@280: * or not. torsten@280: * @param {Array} [ignoreFields="[]"] A list of fieldnames to ignore. torsten@280: */ torsten@280: setReadOnly: function (bReadOnly, ignoreFields) { torsten@280: if(typeof(ignoreFields)==='undefined') { torsten@280: ignoreFields = Array(); torsten@280: } torsten@221: /* Iterate over all fields and set them readonly */ torsten@228: if (bReadOnly) { torsten@228: this.getForm().getFields().each (function (field) { torsten@280: // Check if the field name is in the list of fields to ignore torsten@280: var ignore = false; raimund@491: for (var i = ignoreFields.length - 1; i >= 0; i--) { torsten@280: console.log(ignoreFields[i] + "===" + field.getName()); torsten@280: if (ignoreFields[i] === field.getName(true)) { torsten@280: ignore = true; torsten@280: }; torsten@280: }; torsten@228: //field.setDisabled(bReadOnly); torsten@280: if (!ignore) { torsten@280: field.setReadOnly(true); torsten@280: } torsten@228: }); torsten@228: /* Iterate over all toolbars of lists and hide them */ torsten@228: var childs = this.query('toolbar'); raimund@491: for (var i = childs.length - 1; i >= 0; i--) { torsten@228: childs[i].setVisible(false); torsten@228: } torsten@361: /* torsten@361: * Find Save-Button and hide it. Only hide it if there are not torsten@361: * fields left in the form which are editable torsten@361: * */ torsten@361: if (ignoreFields.length == 0) { torsten@361: var win = this.up('window'); torsten@361: var buttons = win.query('.button'); raimund@491: for (var j = buttons.length - 1; j >= 0; j--) { torsten@361: if (buttons[j].text === 'Speichern') { torsten@361: buttons[j].setVisible(false); torsten@361: }; torsten@270: }; torsten@361: } torsten@221: } torsten@85: }, raimund@491: torsten@133: parseResponse: function(response) { torsten@133: var json = Ext.decode(response.responseText); torsten@133: if (json) { torsten@133: this.success = json.success; torsten@133: this.errors = this.translateReturnCodes(json.errors); torsten@133: this.warnings = this.translateReturnCodes(json.warnings); torsten@133: this.message = Lada.getApplication().bundle.getMsg(json.message); rrenkert@406: var e = Ext.Object.isEmpty(this.warnings); rrenkert@406: if (!Ext.Object.isEmpty(this.warnings) || rrenkert@406: !Ext.Object.isEmpty(this.errors)) { rrenkert@406: this.createMessages(); rrenkert@406: } raimund@491: } raimund@491: else { torsten@211: this.setReadOnly(this.model.get('readonly')); torsten@133: } rrenkert@406: }, raimund@491: rrenkert@406: createMessages: function() { rrenkert@406: var messages = Ext.create('Ext.form.Panel', { rrenkert@406: bodyPadding: '5 5 5 5' rrenkert@406: }); rrenkert@406: for (var key in this.warnings) { rrenkert@406: var label = Ext.create('Ext.container.Container', { rrenkert@406: layout: 'hbox', rrenkert@406: bodyPadding: '5 5 5 5', rrenkert@406: items: [{ rrenkert@406: xtype: 'image', rrenkert@406: src: 'gfx/icon-warning.gif', rrenkert@406: width: 18, rrenkert@406: height: 18 rrenkert@406: }, { rrenkert@406: xtype: 'label', rrenkert@406: text: key + ": " + this.warnings[key], rrenkert@406: margin: '4 0 0 5' rrenkert@406: }] rrenkert@406: }); rrenkert@406: messages.insert(0, label); rrenkert@406: } rrenkert@406: for (var key in this.errors) { rrenkert@406: var label = Ext.create('Ext.container.Container', { rrenkert@406: layout: 'hbox', rrenkert@406: bodyPadding: '5 5 5 5', rrenkert@406: items: [{ rrenkert@406: xtype: 'image', rrenkert@406: src: 'gfx/icon-error.gif', rrenkert@406: width: 18, rrenkert@406: height: 18 rrenkert@406: }, { rrenkert@406: xtype: 'label', rrenkert@406: text: key + ": " + this.errors[key], rrenkert@406: margin: '4 0 0 5' rrenkert@406: }] rrenkert@406: }); rrenkert@406: messages.insert(0, label); rrenkert@406: } rrenkert@406: this.insert(0, messages); torsten@66: } torsten@66: });