22585cf94260213b4a5cdc992d4da84fedd140df
[tine20] / tine20 / Admin / js / container / EditDialog.js
1 /*
2  * Tine 2.0
3  * 
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Philipp Schüle <p.schuele@metaways.de>
6  * @copyright   Copyright (c) 2011 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8  
9 /*global Ext, Tine*/
10
11 Ext.ns('Tine.Admin.container');
12
13 /**
14  * @namespace   Tine.Admin.container
15  * @class       Tine.Admin.ContainerEditDialog
16  * @extends     Tine.widgets.dialog.EditDialog
17  * 
18  * <p>Container Edit Dialog</p>
19  * <p>
20  * </p>
21  * 
22  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
23  * @author      Philipp Schüle <p.schuele@metaways.de>
24  * 
25  * @param       {Object} config
26  * @constructor
27  * Create a new Tine.Admin.ContainerEditDialog
28  * 
29  * TODO add note for personal containers (note is sent to container owner)
30  */
31 Tine.Admin.ContainerEditDialog = Ext.extend(Tine.widgets.dialog.EditDialog, {
32     
33     /**
34      * @private
35      */
36     windowNamePrefix: 'containerEditWindow_',
37     appName: 'Admin',
38     recordClass: Tine.Admin.Model.Container,
39     recordProxy: Tine.Admin.containerBackend,
40     evalGrants: false,
41     modelStore: null,
42
43     /**
44      * executed after record got updated from proxy
45      */
46     onRecordLoad: function () {
47         Tine.Admin.ContainerEditDialog.superclass.onRecordLoad.apply(this, arguments);
48         
49         // load grants store if editing record
50         if (this.record && this.record.id) {
51             this.grantsStore.loadData({
52                 results:    this.record.get('account_grants'),
53                 totalcount: this.record.get('account_grants').length
54             });
55         }
56     },    
57     
58     /**
59      * executed when record gets updated from form
60      */
61     onRecordUpdate: function () {
62         Tine.Admin.ContainerEditDialog.superclass.onRecordUpdate.apply(this, arguments);
63         
64         // get grants from grants grid
65         this.record.set('account_grants', '');
66         var grants = [];
67         this.grantsStore.each(function(grant){
68             grants.push(grant.data);
69         });
70         this.record.set('account_grants', grants);
71     },
72     
73     /**
74      * create grants store + grid
75      * 
76      * @return {Tine.widgets.container.GrantsGrid}
77      */
78     initGrantsGrid: function () {
79         this.grantsStore = new Ext.data.JsonStore({
80             root: 'results',
81             totalProperty: 'totalcount',
82             id: 'account_id',
83             fields: Tine.Tinebase.Model.Grant
84         });
85        
86         this.grantsGrid = new Tine.widgets.container.GrantsGrid({
87             flex: 1,
88             store: this.grantsStore,
89             grantContainer: this.record.data,
90             alwaysShowAdminGrant: true,
91             showHidden: true
92         });
93         
94         return this.grantsGrid;
95     },
96
97     /**
98      * returns dialog
99      */
100     getFormItems: function () {
101         var userApplications = Tine.Tinebase.registry.get('userApplications');
102         this.appStore = new Ext.data.JsonStore({
103             root: 'results',
104             totalProperty: 'totalcount',
105             fields: Tine.Admin.Model.Application
106         });
107
108         this.appStore.loadData({
109             results: userApplications,
110             totalcount: userApplications.length
111         });
112
113         this.modelStore = new Ext.data.ArrayStore({
114             idIndex: 0,
115             fields: [{name: 'value'}, {name: 'name'}]
116         });
117
118         return {
119             layout: 'vbox',
120             layoutConfig: {
121                 align: 'stretch',
122                 pack: 'start'
123             },
124             border: false,
125             items: [{
126                 xtype: 'columnform',
127                 border: false,
128                 autoHeight: true,
129                 items: [[{
130                     columnWidth: 0.225,
131                     fieldLabel: this.app.i18n._('Name'), 
132                     name: 'name',
133                     allowBlank: false,
134                     maxLength: 40
135                 }, {
136                     xtype: 'combo',
137                     readOnly: this.record.id != 0,
138                     store: this.appStore,
139                     columnWidth: 0.225,
140                     name: 'application_id',
141                     displayField: 'name',
142                     valueField: 'id',
143                     fieldLabel: this.app.i18n._('Application'),
144                     mode: 'local',
145                     anchor: '100%',
146                     allowBlank: false,
147                     forceSelection: true,
148                     listeners: {
149                         scope: this,
150                         'select': function (combo, rec) {
151                             this.modelStore.loadData(this.getApplicationModels(rec, false));
152                             Ext.getCmp('modelCombo').setValue('');
153                         }
154                     }
155                 },
156                 {
157                     xtype: 'combo',
158                     readOnly: this.record.id != 0,
159                     store: this.modelStore,
160                     columnWidth: 0.225,
161                     name: 'model',
162                     displayField: 'name',
163                     valueField: 'value',
164                     fieldLabel: this.app.i18n._('Model'),
165                     mode: 'local',
166                     anchor: '100%',
167                     allowBlank: false,
168                     forceSelection: true,
169                     editable: false,
170                     id: 'modelCombo'
171                 }, {
172                     xtype: 'combo',
173                     columnWidth: 0.225,
174                     name: 'type',
175                     fieldLabel: this.app.i18n._('Type'),
176                     store: [['personal', this.app.i18n._('personal')], ['shared', this.app.i18n._('shared')]],
177                     listeners: {
178                         scope: this,
179                         select: function (combo, record) {
180                             this.getForm().findField('note').setDisabled(record.data.field1 === 'shared');
181                         }
182                     },
183                     mode: 'local',
184                     anchor: '100%',
185                     allowBlank: false,
186                     forceSelection: true
187                 }, {
188                     xtype: 'colorfield',
189                     columnWidth: 0.1,
190                     fieldLabel: this.app.i18n._('Color'),
191                     name: 'color'
192                 }]]
193             }, 
194                 this.initGrantsGrid(), {
195                     emptyText: this.app.i18n._('Note for Owner'),
196                     disabled: this.record.get('type') == 'shared',
197                     xtype: 'textarea',
198                     border: false,
199                     autoHeight: true,
200                     name: 'note'
201                 }
202                ]
203         };
204     }
205 });
206
207 /**
208  * Container Edit Popup
209  * 
210  * @param   {Object} config
211  * @return  {Ext.ux.Window}
212  */
213 Tine.Admin.ContainerEditDialog.openWindow = function (config) {
214     var window = Tine.WindowFactory.getWindow({
215         width: 600,
216         height: 400,
217         name: Tine.Admin.ContainerEditDialog.prototype.windowNamePrefix + Ext.id(),
218         contentPanelConstructor: 'Tine.Admin.ContainerEditDialog',
219         contentPanelConstructorConfig: config
220     });
221     return window;
222 };