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 raimund@497: * 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: /** raimund@497: * List of errors in the form. raimund@497: * Typically set after the server validates the form submission torsten@283: */ torsten@75: errors: null, torsten@283: /** raimund@497: * List of warnings in the form. raimund@497: * Typically set after the server validates the form submission torsten@283: */ torsten@75: warnings: null, torsten@283: /** raimund@497: * The generic (error) message for the form. raimund@497: * Typically set after the server validates the form submission torsten@283: */ torsten@75: message: null, torsten@283: /** raimund@497: * Flag to indicate if the validation succeeds. raimund@497: * 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: raimund@497: this.getForm().trackResetOnLoad = true; // Workaround torsten@66: torsten@66: if (Ext.isString(this.model)) { raimund@497: // 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: }); raimund@497: // 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 { raimund@497: // Bind the provided model to be updated torsten@66: this.bindModel(this.model); 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: raimund@497: commit: function() { 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; raimund@497: var method = 'POST'; torsten@133: if (this.model.getId()) { raimund@490: url += this.model.getId(); raimund@497: 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'); raimund@497: this.fireEvent( raimund@497: 'savesuccess', raimund@497: this, raimund@497: this.model, raimund@497: response); raimund@491: } raimund@491: else { torsten@66: console.log('Save was not successfull'); torsten@75: this.form.markInvalid(this.errors); raimund@497: this.fireEvent( raimund@497: 'savefailure', raimund@497: this, raimund@497: this.model, raimund@497: 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) { raimund@497: if (typeof (ignoreFields) === 'undefined') { raimund@497: ignoreFields = []; torsten@280: } torsten@221: /* Iterate over all fields and set them readonly */ torsten@228: if (bReadOnly) { raimund@497: 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@497: var k; raimund@497: for (k = ignoreFields.length - 1; k >= 0; k--) { raimund@497: console.log(ignoreFields[k] + '===' + field.getName()); raimund@497: if (ignoreFields[k] === field.getName(true)) { torsten@280: ignore = true; raimund@497: } raimund@497: } raimund@497: // 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: * */ raimund@497: 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); raimund@497: } raimund@497: } 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: 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: }); raimund@497: var key; raimund@497: var label; raimund@497: for (key in this.warnings) { raimund@497: 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', raimund@497: text: key + ': ' + this.warnings[key], rrenkert@406: margin: '4 0 0 5' rrenkert@406: }] rrenkert@406: }); rrenkert@406: messages.insert(0, label); rrenkert@406: } raimund@497: for (key in this.errors) { raimund@497: 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', raimund@497: 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: });