comparison app/controller/grid/MessungList.js @ 1076:e32c10cf5499

Added model/store, view and controller for messung list.
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 08 Apr 2016 19:32:02 +0200
parents
children dbd435256f77
comparison
equal deleted inserted replaced
1075:d23427c68285 1076:e32c10cf5499
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU GPL (v>=3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out
6 * the documentation coming with IMIS-Labordaten-Application for details.
7 */
8
9 /**
10 * Controller for the MessungList result grid.
11 */
12 Ext.define('Lada.controller.grid.MessungList', {
13 extend: 'Ext.app.Controller',
14 requires: [
15 'Lada.view.window.ProbeEdit'
16 ],
17
18 /**
19 * Initialize the Controller with listeners
20 */
21 init: function() {
22 this.control({
23 'messunglistgrid': {
24 itemdblclick: this.editItem,
25 select: this.activateButtons,
26 deselect: this.deactivateButtons
27 }
28 });
29 this.callParent(arguments);
30 },
31
32 /**
33 * This function is called after a Row in the
34 * {@link Lada.view.grid.ProbeList}
35 * was double-clicked.
36 * The function opens a {@link Lada.view.window.ProbeEdit}
37 * or a {@link Lada.view.window.Messprogramm}.
38 * To determine which window has to be opened, the function
39 * analyse the records modelname.
40 */
41 editItem: function(grid, record) {
42 var probeRecord = Ext.create('Lada.model.ProbeList');
43 probeRecord.setId(record.get('probeId'));
44 probeRecord.set('owner', record.get('owner'));
45 probeRecord.set('readonly', record.get('readonly'));
46
47 var probeWin = Ext.create('Lada.view.window.ProbeEdit', {
48 record: probeRecord,
49 style: 'z-index: -1;' //Fixes an Issue where windows could not be created in IE8
50 });
51
52 probeWin.setPosition(30);
53 probeWin.show();
54 probeWin.initData();
55
56 Ext.ClassManager.get('Lada.model.Probe').load(record.get('probeId'), {
57 failure: function(record, action) {
58 me.setLoading(false);
59 // TODO
60 console.log('An unhandled Failure occured. See following Response and Record');
61 console.log(action);
62 console.log(record);
63 },
64 success: function(precord, response) {
65 var messungWin = Ext.create('Lada.view.window.MessungEdit', {
66 parentWindow: grid.up('window'),
67 probe: precord,
68 record: record,
69 grid: grid
70 });
71 messungWin.show();
72 messungWin.setPosition(window.innerWidth - 30 - messungWin.width);
73 messungWin.initData();
74 }
75 });
76 },
77
78 /**
79 * Send the selection to a Printservice
80 */
81 printSelection: function(button) {
82
83 //disable Button and setLoading...
84 button.disable();
85 button.setLoading(true);
86
87 var grid = button.up('grid');
88 var selection = grid.getView().getSelectionModel().getSelection();
89 var i18n = Lada.getApplication().bundle;
90 var me = this;
91 var columns = [];
92 var columnNames = [];
93 var visibleColumns = [];
94 var displayName = '';
95 var data = [];
96
97 // Write the columns to an array
98 try {
99 for (key in selection[0].data) {
100 // Do not write owner or readonly or id
101 if (["owner", "readonly", "id", "probeId"].indexOf(key) == -1){
102 columns.push(key);
103 }
104 }
105 }
106 catch (e) {
107 console.log(e);
108 }
109
110 //Retrieve visible columns' id's and names.
111 // and set displayName
112 try {
113 var grid = button.up('grid');
114 var cman = grid.columnManager;
115 var cols = cman.getColumns();
116
117 displayName = grid.down('tbtext').text;
118
119 for (key in cols) {
120 if (cols[key].dataIndex) {
121 visibleColumns[cols[key].dataIndex] = cols[key].text;
122 }
123 }
124 }
125 catch (e) {
126 console.log(e);
127 }
128
129
130 // Retrieve Data from selection
131 try {
132 for (item in selection) {
133 var row = selection[item].data;
134 var out = [];
135 //Lookup every column and write to data array.
136 for (key in columns){
137 var attr = columns[key];
138 //Only write data to output when the column is not hidden.
139 if (row[attr] != null &&
140 visibleColumns[attr] != null) {
141 out.push(row[attr].toString());
142 }
143 else if (visibleColumns[attr] != null) {
144 out.push('');
145 }
146 }
147 data.push(out);
148 }
149 }
150 catch (e){
151 console.log(e);
152 }
153
154 //Retrieve the names of the columns.
155 try {
156 var grid = button.up('grid');
157 var cman = grid.columnManager;
158 var cols = cman.getColumns();
159 //Iterate columns and find column names for the key...
160 // This WILL run into bad behaviour when column-keys exist twice.
161 for (key in columns){
162 for (k in cols){
163 if (cols[k].dataIndex == columns[key]){
164 columnNames.push(cols[k].text);
165 break;
166 }
167 }
168 }
169 }
170 catch (e) {
171 console.log(e);
172 }
173
174 var printData = {
175 'layout': 'A4 landscape',
176 'outputFormat': 'pdf',
177 'attributes': {
178 'title': 'Auszug aus LADA',
179 'displayName': displayName,
180 'table': {
181 'columns': columnNames,
182 'data': data
183 }
184 }
185 }
186
187 Ext.Ajax.request({
188 url: 'lada-printer/buildreport.pdf',
189 //configure a proxy in apache conf!
190 jsonData: printData,
191 binary: true,
192 success: function(response) {
193 var content = response.responseBytes;
194 var filetype = response.getResponseHeader('Content-Type');
195 var blob = new Blob([content],{type: filetype});
196 saveAs(blob, 'lada-print.pdf');
197 button.enable();
198 button.setLoading(false);
199 },
200 failure: function(response) {
201 console.log('failure');
202 // Error handling
203 // TODO
204 //console.log(response.responseText)
205 button.enable();
206 button.setLoading(false);
207 if (response.responseText) {
208 try {
209 var json = Ext.JSON.decode(response.responseText);
210 }
211 catch(e){
212 console.log(e);
213 }
214 }
215 if (json) {
216 if(json.errors.totalCount > 0 || json.warnings.totalCount > 0){
217 formPanel.setMessages(json.errors, json.warnings);
218 }
219 if(json.message){
220 Ext.Msg.alert(Lada.getApplication().bundle.getMsg('err.msg.generic.title')
221 +' #'+json.message,
222 Lada.getApplication().bundle.getMsg(json.message));
223 } else {
224 Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
225 i18n.getMsg('err.msg.print.noContact'));
226 }
227 } else {
228 Ext.Msg.alert(i18n.getMsg('err.msg.generic.title'),
229 i18n.getMsg('err.msg.print.noContact'));
230 }
231 }
232 });
233 },
234
235 /**
236 * Toggles the buttons in the toolbar
237 **/
238 activateButtons: function(rowModel, record) {
239 var grid = rowModel.view.up('grid');
240 this.buttonToggle(true, grid);
241 },
242
243 /**
244 * Toggles the buttons in the toolbar
245 **/
246 deactivateButtons: function(rowModel, record) {
247 var grid = rowModel.view.up('grid');
248 // Only disable buttons when nothing is selected
249 if (rowModel.selected.items == 0) {
250 this.buttonToggle(false, grid);
251 }
252 },
253
254 /**
255 * Enables/Disables a set of buttons
256 **/
257 buttonToggle: function(enabled, grid) {
258 if (!enabled) {
259 grid.down('button[action=print]').disable();
260 grid.down('button[action=setStatus]').disable();
261 }
262 else {
263 grid.down('button[action=print]').enable();
264 // TODO: enable button only on messungen with owner == true and
265 // readonly == false
266 grid.down('button[action=setStatus]').enable();
267 }
268 },
269
270 reload: function(btn) {
271 if (btn === 'yes') {
272 location.reload();
273 }
274 }
275 });

http://lada.wald.intevation.org