Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / examples / ux / PreviewPlugin.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.ux.PreviewPlugin
17  * @extends Ext.AbstractPlugin
18  *
19  * The Preview enables you to show a configurable preview of a record.
20  *
21  * This plugin assumes that it has control over the features used for this
22  * particular grid section and may conflict with other plugins.
23  * 
24  * @alias plugin.preview
25  * @ptype preview
26  */
27 Ext.define('Ext.ux.PreviewPlugin', {
28     extend: 'Ext.AbstractPlugin',
29     alias: 'plugin.preview',
30     requires: ['Ext.grid.feature.RowBody', 'Ext.grid.feature.RowWrap'],
31     
32     // private, css class to use to hide the body
33     hideBodyCls: 'x-grid-row-body-hidden',
34     
35     /**
36      * @cfg {String} bodyField
37      * Field to display in the preview. Must me a field within the Model definition
38      * that the store is using.
39      */
40     bodyField: '',
41     
42     /**
43      * @cfg {Boolean} previewExpanded
44      */
45     previewExpanded: true,
46     
47     constructor: function(config) {
48         this.callParent(arguments);
49         var bodyField   = this.bodyField,
50             hideBodyCls = this.hideBodyCls,
51             section     = this.getCmp(),
52             features = [{
53                 ftype: 'rowbody',
54                 getAdditionalData: function(data, idx, record, orig, view) {
55                     var o = Ext.grid.feature.RowBody.prototype.getAdditionalData.apply(this, arguments);
56                     Ext.apply(o, {
57                         rowBody: data[bodyField],
58                         rowBodyCls: section.previewExpanded ? '' : hideBodyCls
59                     });
60                     return o;
61                 }
62             },{
63                 ftype: 'rowwrap'
64             }];
65         
66         section.previewExpanded = this.previewExpanded;
67         if (!section.features) {
68             section.features = [];
69         }
70         section.features = features.concat(section.features);
71     },
72     
73     /**
74      * Toggle between the preview being expanded/hidden
75      * @param {Boolean} expanded Pass true to expand the record and false to not show the preview.
76      */
77     toggleExpanded: function(expanded) {
78         var view = this.getCmp();
79         this.previewExpanded = view.previewExpanded = expanded;
80         view.refresh();
81     }
82 });