Project

General

Profile

Download (10.2 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / flarecast / workspace / client-admin / js / dictionary / TemplateProp.js @ master

1
/***************************************
2
* Copyright 2010-2014 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
* 
4
* This file is part of SITools2.
5
* 
6
* SITools2 is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
* 
11
* SITools2 is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU General Public License for more details.
15
* 
16
* You should have received a copy of the GNU General Public License
17
* along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
***************************************/
19
/*global Ext, sitools, ID, i18n, document, showResponse, alertFailure, LOCALE, ImageChooser, 
20
 showHelp, loadUrl*/
21
Ext.namespace('sitools.admin.dictionary');
22

    
23
Ext.define('sitools.admin.dictionary.TemplateProp', { 
24
    extend : 'Ext.Window',
25
        alias : 'widget.s-templateprop',
26
    width : 700,
27
    height : 480,
28
    modal : true,
29
    pageSize : ADMIN_PANEL_NB_ELEMENTS,
30
    layout : 'fit',
31

    
32
    initComponent : function () {
33
        
34
        if (this.action == 'modify') {
35
            this.title = i18n.get('label.modifyTemplate');
36
        }
37
        if (this.action == 'create') {
38
            this.title = i18n.get('label.createTemplate');
39
        }
40

    
41
        var storeProperty = Ext.create('Ext.data.JsonStore', {
42
            id : 'storePropertiesSelect',
43
            proxy : {
44
                type : 'memory',
45
                reader : {
46
                    type : 'json',
47
                    root : 'property',
48
                    idProperty : 'name'
49
                }
50
            },
51
            fields : [ {
52
                name : 'name',
53
                type : 'string'
54
            }, {
55
                name : 'value',
56
                type : 'string'
57
            } ]
58
        });
59

    
60
        var smProperty = Ext.create('Ext.selection.RowModel', {
61
            mode : 'SINGLE'
62
        });
63

    
64
        var tbar = {
65
            xtype : 'toolbar',
66
            defaults : {
67
                scope : this
68
            },
69
            items : [ {
70
                text : i18n.get('label.create'),
71
                icon : loadUrl.get('APP_URL') + loadUrl.get('APP_CLIENT_PUBLIC_URL')+'/res/images/icons/toolbar_create.png',
72
                handler : this.onCreateProperty
73
            }, {
74
                text : i18n.get('label.delete'),
75
                icon : loadUrl.get('APP_URL') + loadUrl.get('APP_CLIENT_PUBLIC_URL')+'/res/images/icons/toolbar_delete.png',
76
                handler : this.onDeleteProperty
77
            } ]
78
        };
79

    
80
        var cellEditing = Ext.create('Ext.grid.plugin.CellEditing', {
81
            clicksToEdit: 1
82
        });
83
        
84
        var gridProperty = Ext.create('Ext.grid.Panel', {
85
            id : 'gridPropertySelect',
86
            title : i18n.get('title.gridProperty'),
87
            store : storeProperty,
88
            tbar : tbar,
89
            forceFit : true,
90
            columns :  [{
91
                header : i18n.get('headers.name'),
92
                dataIndex : 'name',
93
                width : 100,
94
                editor : {
95
                    xtype : 'textfield',
96
                    allowBlank : false
97
                }
98
            }, {
99
                header : i18n.get('headers.value'),
100
                dataIndex : 'value',
101
                width : 150,
102
                editor : {
103
                    xtype : 'textfield',
104
                    allowBlank : true
105
                }
106
            }],
107
            selModel : smProperty,
108
            plugins : [cellEditing]
109
        });
110
        
111
        this.items = [{
112
            xtype : 'tabpanel',
113
            height : 450,
114
            activeTab : 0,
115
            items : [{
116
                    height : 400,
117
                    title : i18n.get('label.templateInfo'),
118
                    xtype : 'form',
119
                    border : false,
120
                    padding : 10,
121
                    items : [ {
122
                        xtype : 'hidden',
123
                        name : 'id'
124
                    }, {
125
                        xtype : 'textfield',
126
                        name : 'name',
127
                        fieldLabel : i18n.get('label.name'),
128
                        anchor : '100%', 
129
                        allowBlank : false
130
                    }, {
131
                        xtype : 'textfield',
132
                        name : 'description',
133
                        fieldLabel : i18n.get('label.description'),
134
                        anchor : '100%'
135
                    }]
136
            }, gridProperty ],
137
            buttons : [ {
138
                text : i18n.get('label.ok'),
139
                scope : this,
140
                handler : this.onValidate
141

    
142
            }, {
143
                text : i18n.get('label.cancel'),
144
                scope : this,
145
                handler : function () {
146
                    this.close();
147
                }
148
            } ]
149

    
150
        } ];
151
        this.callParent(arguments);
152
    },
153

    
154
    onUpload : function () {
155
        // TODO gerer l'upload de fichier.
156
        Ext.Msg.alert('upload non impl&eacute;ment&eacute;');
157
    },
158
    onCreateProperty : function () {
159
        this.down('gridpanel').getStore().add({});
160
    },
161
    onDeleteProperty : function () {
162
        var grid = this.down('gridpanel');
163
        var rec = grid.getSelectionModel().getLastSelected();
164
        if (!rec) {
165
            Ext.Msg.alert(i18n.get('label.warning'), i18n.get('warning.noselection'));
166
            return;
167
        }
168
        grid.getStore().remove(rec);
169

    
170
    },
171
    onValidate : function () {
172
        var f, putObject = {}, store, tmp = [], i;
173
        f = this.down('form').getForm();
174
                if (!f.isValid()) {
175
            Ext.Msg.alert(i18n.get('label.error'), i18n.get('warning.invalidForm'));
176
            return false;
177
        }
178
        if (this.action == 'modify') {
179
                        Ext.iterate(f.getValues(), function (key, value) {
180
                if (key == 'image') {
181
                    // TODO : definir une liste de mediaType et type
182
                    putObject.image = {};
183
                    putObject.image.url = value;
184
                    putObject.image.type = "Image";
185
                    putObject.image.mediaType = "Image";
186
                } else {
187
                    putObject[key] = value;
188
                }
189
            }, this);
190

    
191
            store = this.down('gridpanel').getStore();
192
            if (store.getCount() > 0) {
193
                putObject.properties = [];
194
                
195
                for (i = 0; i < store.getCount(); i++) {
196
                    putObject.properties.push(store.getAt(i).data);
197
                }
198
            }
199

    
200
            Ext.Ajax.request({
201
                url : this.url,
202
                method : 'PUT',
203
                scope : this,
204
                jsonData : putObject,
205
                success : function (ret) {
206
                    this.close();
207
                    this.store.reload();
208
                },
209
                failure : alertFailure
210
            });
211
        }
212
        if (this.action == 'create') {
213
            Ext.iterate(f.getValues(), function (key, value) {
214
                if (key == 'image') {
215
                    // TODO : definir une liste de mediaType et type
216
                    putObject.image = {};
217
                    putObject.image.url = value;
218
                    putObject.image.type = "Image";
219
                    putObject.image.mediaType = "Image";
220
                } else {
221
                    putObject[key] = value;
222
                }
223
            }, this);
224

    
225
            store = this.down('gridpanel').getStore();
226
            if (store.getCount() > 0) {
227
                putObject.properties = [];
228
                for (i = 0; i < store.getCount(); i++) {
229
                    putObject.properties.push(store.getAt(i).data);
230
                }
231
            }
232
            Ext.Ajax.request({
233
                url : this.url,
234
                method : 'POST',
235
                scope : this,
236
                jsonData : putObject,
237
                success : function (ret) {
238
                    this.close();
239
                    this.store.reload();
240
                    // Ext.Msg.alert(i18n.get('label.information'),
241
                    // i18n.get('msg.uservalidate'));
242
                },
243
                failure : alertFailure
244
            });
245
        }
246

    
247
    },
248

    
249
    onRender : function () {
250
        this.callParent(arguments);
251
        if (this.url) {
252
            // var gs = this.groupStore, qs = this.quotaStore;
253
            var i;
254
            if (this.action == 'modify') {
255
                Ext.Ajax.request({
256
                    url : this.url,
257
                    method : 'GET',
258
                    scope : this,
259
                    success : function (ret) {
260
                        var f = this.down('form').getForm();
261
                        var store = this.down('gridpanel').getStore();
262

    
263
                        var data = Ext.decode(ret.responseText).template;
264
                        var rec = {};
265
                        rec.id = data.id;
266
                        rec.name = data.name;
267
                        rec.description = data.description;
268
                        
269
                        f.setValues(rec);
270

    
271
                        if (!data.properties) {
272
                            return;
273
                        }
274

    
275
                        var properties;
276
                        if (typeof data.properties[0] && data.properties[0] instanceof Array) {
277
                            properties = data.properties[0];
278
                        } else {
279
                            properties = data.properties;
280
                        }
281
                        for (i = 0; i < properties.length; i++) {
282
//                            rec = new Ext.data.Record(properties[i]);
283
//                            store.add(rec);
284
                            store.add(properties[i]);
285
                        }
286

    
287
                    },
288
                    failure : function (ret) {
289
                        var data = Ext.decode(ret.responseText);
290
                        Ext.Msg.alert(i18n.get('label.warning'), data.errorMessage);
291
                    }
292
                });
293
            }
294
        }
295
        this.getEl().on('keyup', function (e) {
296
            if (e.getKey() == e.ENTER) {
297
                this.onValidate();
298
            }
299
        }, this);
300
    }
301

    
302
});
303

    
304