deal with virtual fields in recorddisplaypanel
[tine20] / tine20 / Tinebase / js / widgets / display / RecordDisplayPanel.js
1 /*
2  * Tine 2.0
3  *
4  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
5  * @author      Cornelius Weiss <c.weiss@metaways.de>
6  * @copyright   Copyright (c) 2016 Metaways Infosystems GmbH (http://www.metaways.de)
7  */
8
9 Ext.ns('Tine.widgets.display');
10
11 /**
12  * @class       Tine.widgets.display.RecordDisplayPanel
13  * @namespace   Tine.widgets.display
14  * @extends     Ext.ux.display.DisplayPanel
15  *
16  * @author      Cornelius Weiss <c.weiss@metaways.de>
17  *
18  * Panel for displaying information of a singel record.
19  */
20 Tine.widgets.display.RecordDisplayPanel = Ext.extend(Ext.ux.display.DisplayPanel, {
21
22     /**
23      * @cfg {Ext.data.Record} recordClass
24      * record definition class
25      */
26     recordClass: null,
27
28     /**
29      * @property {String}
30      */
31     appName: null,
32     /**
33      * @property {String}
34      */
35     modelName: null,
36     /**
37      * @property {Tine.Tinebase.Application}
38      */
39     app: null,
40
41     /* private */
42     layout: 'fit',
43     border: false,
44
45     /**
46      * initializes the component, builds this.fields, calls parent
47      */
48     initComponent: function() {
49         this.appName = this.recordClass.getMeta('appName');
50         this.modelName = this.recordClass.getMeta('modelName');
51
52         this.app = Tine.Tinebase.appMgr.get(this.appName);
53
54         var containerProperty = this.recordClass.getMeta('containerProperty');
55         if (! this.containerRenderer && this.recordClass.hasField(containerProperty)) {
56             this.containerRenderer = Tine.widgets.grid.RendererManager.get(this.appName, this.modelName, containerProperty, Tine.widgets.grid.RendererManager.CATEGORY_DISPLAYPANEL);
57         }
58
59         this.items = [{
60             layout: 'vbox',
61             border: false,
62             layoutConfig: {
63                 align:'stretch'
64             },
65             items: [{
66                 layout: 'hbox',
67                 flex: 0,
68                 height: 16,
69                 border: false,
70                 style: 'padding-left: 5px; padding-right: 5px',
71                 layoutConfig: {
72                     align:'stretch'
73                 },
74                 items: this.getHeadlineItems()
75             }, {
76                 layout: 'hbox',
77                 flex: 1,
78                 border: false,
79                 layoutConfig: {
80                     padding:'5',
81                     align:'stretch'
82                 },
83                 defaults:{
84                     margins:'0 5 0 0'
85                 },
86                 items: this.getBodyItems()
87             }]
88         }];
89
90         Tine.widgets.display.RecordDisplayPanel.superclass.initComponent.call(this);
91     },
92
93     getHeadlineItems: function() {
94         var headlineItems = [{
95             flex: 0.5,
96             xtype: 'ux.displayfield',
97             name: this.recordClass.getMeta('titleProperty'),
98             style: 'padding-top: 2px',
99             cls: 'x-ux-display-header',
100             htmlEncode: false,
101             renderer: this.titleRenderer.createDelegate(this)
102         }];
103
104         if (this.recordClass.getMeta('containerProperty')) {
105             headlineItems.push({
106                 flex: 0.5,
107                 xtype: 'ux.displayfield',
108                 style: 'text-align: right;',
109                 cls: 'x-ux-display-header',
110                 name: this.recordClass.getMeta('containerProperty'),
111                 htmlEncode: false,
112                 renderer: this.containerRenderer
113             });
114         }
115
116         return headlineItems;
117     },
118
119     getBodyItems: function() {
120         var modelConfig = this.recordClass.getModelConfiguration(),
121             fieldsToExclude = ['tags', 'notes', 'attachments', 'relations', 'customfields',
122                 this.recordClass.getMeta('idProperty'),
123                 this.recordClass.getMeta('titleProperty'),
124                 this.recordClass.getMeta('containerProperty')
125             ],
126             fieldNames = this.recordClass.getFieldNames(),
127             displayFields = [],
128             displayAreas = [];
129
130         Ext.each(Tine.Tinebase.Model.genericFields, function(field) {fieldsToExclude.push(field.name)});
131
132         Ext.each(fieldNames, function(fieldName) {
133             var fieldDefinition = modelConfig.fields[fieldName],
134                 fieldType = fieldDefinition.type || 'textfield',
135                 field = {
136                     xtype: 'ux.displayfield',
137                     name: fieldDefinition.fieldName,
138                     fieldLabel: this.app.i18n._hidden(fieldDefinition.label || fieldDefinition.fieldName),
139                 };
140
141             if (fieldType === 'virtual') {
142                 field.fieldLabel = fieldDefinition.config.label;
143                 fieldType = fieldDefinition.config.type || 'textfield';
144             }
145
146             if (fieldsToExclude.indexOf(fieldDefinition.fieldName) < 0 && ! fieldDefinition.shy) {
147                 if (fieldType == 'text') {
148                     Ext.apply(field, {
149                         flex: 1,
150                         cls: 'x-ux-display-background-border',
151                         xtype: 'ux.displaytextarea'
152                     });
153                     displayAreas.push(field);
154                 } else {
155                     var renderer = Tine.widgets.grid.RendererManager.get(this.appName, this.modelName, fieldDefinition.fieldName, Tine.widgets.grid.RendererManager.CATEGORY_DISPLAYPANEL);
156                     if (renderer) {
157                         field.renderer = renderer;
158                     }
159                     displayFields.push(field);
160                 }
161             }
162         }, this);
163
164         // auto height
165         this.defaultHeight = 25 +  displayFields.length * 18;
166
167         return [{
168             flex: 1,
169             layout: 'ux.display',
170             labelWidth: 150,
171             autoScroll: true,
172             layoutConfig: {
173                 background: 'solid'
174             },
175             items: [displayFields]
176         }].concat(displayAreas);
177     },
178
179     loadRecord: function(record) {
180         this.record = record;
181
182         this.supr().loadRecord.apply(this, arguments);
183     },
184
185     titleRenderer: function(title) {
186         return this.record ? Tine.Tinebase.EncodingHelper.encode(this.record.getTitle()) : Tine.Tinebase.EncodingHelper.encode(title);
187     }
188 });
189
190 Ext.reg('ux.displaypanel', Ext.ux.display.DisplayPanel);