1ff6cfaa80555f09c83030b1e1228371d39f0185
[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 (fieldsToExclude.indexOf(fieldDefinition.fieldName) < 0 && ! fieldDefinition.shy) {
142                 if (fieldType == 'text') {
143                     Ext.apply(field, {
144                         flex: 1,
145                         cls: 'x-ux-display-background-border',
146                         xtype: 'ux.displaytextarea'
147                     });
148                     displayAreas.push(field);
149                 } else {
150                     var renderer = Tine.widgets.grid.RendererManager.get(this.appName, this.modelName, fieldDefinition.fieldName, Tine.widgets.grid.RendererManager.CATEGORY_DISPLAYPANEL);
151                     if (renderer) {
152                         field.renderer = renderer;
153                     }
154                     displayFields.push(field);
155                 }
156             }
157         }, this);
158
159         // auto height
160         this.defaultHeight = 25 +  displayFields.length * 18;
161
162         return [{
163             flex: 1,
164             layout: 'ux.display',
165             labelWidth: 150,
166             autoScroll: true,
167             layoutConfig: {
168                 background: 'solid'
169             },
170             items: [displayFields]
171         }].concat(displayAreas);
172     },
173
174     loadRecord: function(record) {
175         this.record = record;
176
177         this.supr().loadRecord.apply(this, arguments);
178     },
179
180     titleRenderer: function(title) {
181         return this.record ? Tine.Tinebase.EncodingHelper.encode(this.record.getTitle()) : Tine.Tinebase.EncodingHelper.encode(title);
182     }
183 });
184
185 Ext.reg('ux.displaypanel', Ext.ux.display.DisplayPanel);