comparison app/view/widgets/LadaForm.js @ 497:7c0653e8d9f7

Fixed some js related issues (unused vars, arrays, etc.) and code style.
author Raimund Renkert <raimund.renkert@intevation.de>
date Thu, 06 Nov 2014 10:38:17 +0100
parents 850ccfe5f3c4
children 8b4ec61c5752
comparison
equal deleted inserted replaced
496:d07e5086a64b 497:7c0653e8d9f7
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz 1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
2 * Software engineering by Intevation GmbH 2 * Software engineering by Intevation GmbH
3 * 3 *
4 * This file is Free Software under the GNU GPL (v>=3) 4 * This file is Free Software under the GNU GPL (v>=3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out 5 * and comes with ABSOLUTELY NO WARRANTY! Check out
6 * the documentation coming with IMIS-Labordaten-Application for details. 6 * the documentation coming with IMIS-Labordaten-Application for details.
7 */ 7 */
8 8
9 /** 9 /**
10 * Generic Lada specific form. 10 * Generic Lada specific form.
11 * 11 *
27 * Set to the id of the model instance and the model will be loaded for you. 27 * Set to the id of the model instance and the model will be loaded for you.
28 * Only applicable if model provided is a model class name (string). 28 * Only applicable if model provided is a model class name (string).
29 */ 29 */
30 modelId: null, 30 modelId: null,
31 /** 31 /**
32 * List of errors in the form. Typically set after the server validates the form submission 32 * List of errors in the form.
33 * Typically set after the server validates the form submission
33 */ 34 */
34 errors: null, 35 errors: null,
35 /** 36 /**
36 * List of warnings in the form. Typically set after the server validates the form submission 37 * List of warnings in the form.
38 * Typically set after the server validates the form submission
37 */ 39 */
38 warnings: null, 40 warnings: null,
39 /** 41 /**
40 * The generic (error) message for the form. Typically set after the server validates the form submission 42 * The generic (error) message for the form.
43 * Typically set after the server validates the form submission
41 */ 44 */
42 message: null, 45 message: null,
43 /** 46 /**
44 * Flag to indicate if the validation succeeds. Typically set after the server validates the form submission 47 * Flag to indicate if the validation succeeds.
48 * Typically set after the server validates the form submission
45 */ 49 */
46 success: null, 50 success: null,
47 /** 51 /**
48 * Flag to indicate if the form should be rendered in readonly mode. Will 52 * Flag to indicate if the form should be rendered in readonly mode. Will
49 * be set after calling the {setReadOnly} function. 53 * be set after calling the {setReadOnly} function.
51 readonly: false, 55 readonly: false,
52 56
53 initComponent: function() { 57 initComponent: function() {
54 this.callParent(arguments); 58 this.callParent(arguments);
55 59
56 this.getForm().trackResetOnLoad = true; //Workaround 60 this.getForm().trackResetOnLoad = true; // Workaround
57 61
58 if (Ext.isString(this.model)) { 62 if (Ext.isString(this.model)) {
59 //Load a model to be updated 63 // Load a model to be updated
60 if (this.modelId) { 64 if (this.modelId) {
61 Ext.ClassManager.get(this.model).load(this.modelId, { 65 Ext.ClassManager.get(this.model).load(this.modelId, {
62 failure: this.onModelLoadFailure, 66 failure: this.onModelLoadFailure,
63 success: this.onModelLoadSuccess, 67 success: this.onModelLoadSuccess,
64 scope: this 68 scope: this
65 }); 69 });
66 //Load an empty record to be inserted 70 // Load an empty record to be inserted
67 } 71 }
68 else { 72 else {
69 this.bindModel(Ext.create(this.model, {})); 73 this.bindModel(Ext.create(this.model, {}));
70 } 74 }
71 } 75 }
72 else { 76 else {
73 77 // Bind the provided model to be updated
74 //Bind the provided model to be updated
75 this.bindModel(this.model); 78 this.bindModel(this.model);
76
77 } 79 }
78 this.addEvents( 80 this.addEvents(
79 'loadsuccess', 81 'loadsuccess',
80 'loadfailure', 82 'loadfailure',
81 'savesuccess', 83 'savesuccess',
90 if (model.get('readonly') === true) { 92 if (model.get('readonly') === true) {
91 this.setReadOnly(true); 93 this.setReadOnly(true);
92 } 94 }
93 }, 95 },
94 96
95 commit: function(callback, scope) { 97 commit: function() {
96 if (this.form.isDirty() && this.form.isValid()) { 98 if (this.form.isDirty() && this.form.isValid()) {
97 this.form.updateRecord(this.model); 99 this.form.updateRecord(this.model);
98 100
99 var data = this.model.getAllData(); 101 var data = this.model.getAllData();
100 var baseUrl = this.model.getProxy().url; 102 var baseUrl = this.model.getProxy().url;
101 var url = baseUrl; 103 var url = baseUrl;
102 var method = "POST"; 104 var method = 'POST';
103 if (this.model.getId()) { 105 if (this.model.getId()) {
104 url += this.model.getId(); 106 url += this.model.getId();
105 method = "PUT"; 107 method = 'PUT';
106 } 108 }
107 109
108 Ext.Ajax.request({ 110 Ext.Ajax.request({
109 url: url, 111 url: url,
110 jsonData: data, 112 jsonData: data,
111 method: method, 113 method: method,
112 callback: function(option, success, response) { 114 callback: function(option, success, response) {
113 this.parseResponse(response); 115 this.parseResponse(response);
114 if (this.success) { 116 if (this.success) {
115 console.log('Save was successfull'); 117 console.log('Save was successfull');
116 this.fireEvent('savesuccess', this, this.model, response); 118 this.fireEvent(
119 'savesuccess',
120 this,
121 this.model,
122 response);
117 } 123 }
118 else { 124 else {
119 console.log('Save was not successfull'); 125 console.log('Save was not successfull');
120 this.form.markInvalid(this.errors); 126 this.form.markInvalid(this.errors);
121 this.fireEvent('savefailure', this, this.model, response); 127 this.fireEvent(
128 'savefailure',
129 this,
130 this.model,
131 response);
122 } 132 }
123 }, 133 },
124 scope: this 134 scope: this
125 }); 135 });
126 } 136 }
149 * @param {Boolean} Flag to indicate if the form should be set to readonly 159 * @param {Boolean} Flag to indicate if the form should be set to readonly
150 * or not. 160 * or not.
151 * @param {Array} [ignoreFields="[]"] A list of fieldnames to ignore. 161 * @param {Array} [ignoreFields="[]"] A list of fieldnames to ignore.
152 */ 162 */
153 setReadOnly: function (bReadOnly, ignoreFields) { 163 setReadOnly: function (bReadOnly, ignoreFields) {
154 if(typeof(ignoreFields)==='undefined') { 164 if (typeof (ignoreFields) === 'undefined') {
155 ignoreFields = Array(); 165 ignoreFields = [];
156 } 166 }
157 /* Iterate over all fields and set them readonly */ 167 /* Iterate over all fields and set them readonly */
158 if (bReadOnly) { 168 if (bReadOnly) {
159 this.getForm().getFields().each (function (field) { 169 this.getForm().getFields().each(function (field) {
160 // Check if the field name is in the list of fields to ignore 170 // Check if the field name is in the list of fields to ignore
161 var ignore = false; 171 var ignore = false;
162 for (var i = ignoreFields.length - 1; i >= 0; i--) { 172 var k;
163 console.log(ignoreFields[i] + "===" + field.getName()); 173 for (k = ignoreFields.length - 1; k >= 0; k--) {
164 if (ignoreFields[i] === field.getName(true)) { 174 console.log(ignoreFields[k] + '===' + field.getName());
175 if (ignoreFields[k] === field.getName(true)) {
165 ignore = true; 176 ignore = true;
166 }; 177 }
167 }; 178 }
168 //field.setDisabled(bReadOnly); 179 // field.setDisabled(bReadOnly);
169 if (!ignore) { 180 if (!ignore) {
170 field.setReadOnly(true); 181 field.setReadOnly(true);
171 } 182 }
172 }); 183 });
173 /* Iterate over all toolbars of lists and hide them */ 184 /* Iterate over all toolbars of lists and hide them */
177 } 188 }
178 /* 189 /*
179 * Find Save-Button and hide it. Only hide it if there are not 190 * Find Save-Button and hide it. Only hide it if there are not
180 * fields left in the form which are editable 191 * fields left in the form which are editable
181 * */ 192 * */
182 if (ignoreFields.length == 0) { 193 if (ignoreFields.length === 0) {
183 var win = this.up('window'); 194 var win = this.up('window');
184 var buttons = win.query('.button'); 195 var buttons = win.query('.button');
185 for (var j = buttons.length - 1; j >= 0; j--) { 196 for (var j = buttons.length - 1; j >= 0; j--) {
186 if (buttons[j].text === 'Speichern') { 197 if (buttons[j].text === 'Speichern') {
187 buttons[j].setVisible(false); 198 buttons[j].setVisible(false);
188 }; 199 }
189 }; 200 }
190 } 201 }
191 } 202 }
192 }, 203 },
193 204
194 parseResponse: function(response) { 205 parseResponse: function(response) {
196 if (json) { 207 if (json) {
197 this.success = json.success; 208 this.success = json.success;
198 this.errors = this.translateReturnCodes(json.errors); 209 this.errors = this.translateReturnCodes(json.errors);
199 this.warnings = this.translateReturnCodes(json.warnings); 210 this.warnings = this.translateReturnCodes(json.warnings);
200 this.message = Lada.getApplication().bundle.getMsg(json.message); 211 this.message = Lada.getApplication().bundle.getMsg(json.message);
201 var e = Ext.Object.isEmpty(this.warnings);
202 if (!Ext.Object.isEmpty(this.warnings) || 212 if (!Ext.Object.isEmpty(this.warnings) ||
203 !Ext.Object.isEmpty(this.errors)) { 213 !Ext.Object.isEmpty(this.errors)) {
204 this.createMessages(); 214 this.createMessages();
205 } 215 }
206 } 216 }
211 221
212 createMessages: function() { 222 createMessages: function() {
213 var messages = Ext.create('Ext.form.Panel', { 223 var messages = Ext.create('Ext.form.Panel', {
214 bodyPadding: '5 5 5 5' 224 bodyPadding: '5 5 5 5'
215 }); 225 });
216 for (var key in this.warnings) { 226 var key;
217 var label = Ext.create('Ext.container.Container', { 227 var label;
228 for (key in this.warnings) {
229 label = Ext.create('Ext.container.Container', {
218 layout: 'hbox', 230 layout: 'hbox',
219 bodyPadding: '5 5 5 5', 231 bodyPadding: '5 5 5 5',
220 items: [{ 232 items: [{
221 xtype: 'image', 233 xtype: 'image',
222 src: 'gfx/icon-warning.gif', 234 src: 'gfx/icon-warning.gif',
223 width: 18, 235 width: 18,
224 height: 18 236 height: 18
225 }, { 237 }, {
226 xtype: 'label', 238 xtype: 'label',
227 text: key + ": " + this.warnings[key], 239 text: key + ': ' + this.warnings[key],
228 margin: '4 0 0 5' 240 margin: '4 0 0 5'
229 }] 241 }]
230 }); 242 });
231 messages.insert(0, label); 243 messages.insert(0, label);
232 } 244 }
233 for (var key in this.errors) { 245 for (key in this.errors) {
234 var label = Ext.create('Ext.container.Container', { 246 label = Ext.create('Ext.container.Container', {
235 layout: 'hbox', 247 layout: 'hbox',
236 bodyPadding: '5 5 5 5', 248 bodyPadding: '5 5 5 5',
237 items: [{ 249 items: [{
238 xtype: 'image', 250 xtype: 'image',
239 src: 'gfx/icon-error.gif', 251 src: 'gfx/icon-error.gif',
240 width: 18, 252 width: 18,
241 height: 18 253 height: 18
242 }, { 254 }, {
243 xtype: 'label', 255 xtype: 'label',
244 text: key + ": " + this.errors[key], 256 text: key + ': ' + this.errors[key],
245 margin: '4 0 0 5' 257 margin: '4 0 0 5'
246 }] 258 }]
247 }); 259 });
248 messages.insert(0, label); 260 messages.insert(0, label);
249 } 261 }

http://lada.wald.intevation.org