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() { torsten@66: torsten@66: this.callParent(); torsten@66: torsten@66: this.getForm().trackResetOnLoad = true; //Workaround torsten@66: torsten@66: if (Ext.isString(this.model)) { torsten@66: torsten@66: //Load a model to be updated torsten@66: if (this.modelId) { torsten@66: 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: torsten@66: //Load an empty record to be inserted torsten@66: } else { torsten@66: this.bindModel(Ext.create(this.model, {})); torsten@66: } torsten@66: torsten@66: } else { torsten@66: torsten@66: //Bind the provided model to be updated torsten@66: this.bindModel(this.model); torsten@66: torsten@66: } torsten@66: torsten@66: this.addEvents('loadsuccess', 'loadfailure', 'savesuccess', '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()) { torsten@133: url += this.model.getEidi(); 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); torsten@66: } 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); torsten@83: this.parseResponse(operation); torsten@66: this.fireEvent('loadsuccess', this, record, operation); torsten@66: }, torsten@66: torsten@66: onModelLoadFailure: function(record, operation) { torsten@83: this.parseResponse(operation); 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: console.log(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; torsten@280: 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'); torsten@228: for (var i = childs.length - 1; i >= 0; i--){ torsten@228: childs[i].setVisible(false); torsten@228: } torsten@270: /* Find Save-Button and hide it */ torsten@270: var win = this.up('window'); torsten@270: var buttons = win.query('.button'); torsten@270: for (var j = buttons.length - 1; j >= 0; j--){ torsten@270: if (buttons[j].text === 'Speichern') { torsten@270: buttons[j].setVisible(false); torsten@270: }; torsten@270: }; torsten@221: } torsten@85: }, 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); torsten@185: } else { torsten@211: this.setReadOnly(this.model.get('readonly')); torsten@133: } torsten@66: } torsten@66: });