Roo/ViewPanel.js
[roojs1] / Roo / ViewPanel.js
1 /*
2  * Based on:
3  * Ext JS Library 1.1.1
4  * Copyright(c) 2006-2007, Ext JS, LLC.
5  *
6  * Originally Released Under LGPL - original licence link has changed is not relivant.
7  *
8  * Fork - LGPL
9  * <script type="text/javascript">
10  */
11  
12 /**
13  * @class Roo.grid.ViewPanel
14  * @extends Roo.grid.Grid
15  * Class for creating and editable grid.
16  * @param {String/HTMLElement/Roo.Element} container The element into which this grid will be rendered - 
17  * The container MUST have some type of size defined for the grid to fill. The container will be 
18  * automatically set to position relative if it isn't already.
19  * @param {Object} dataSource The data model to bind to
20  * @param {Object} colModel The column model with info about this grid's columns
21  */
22 Roo.grid.ViewPanel = function(container, config){
23     Roo.grid.ViewPanel.superclass.constructor.call(this, container, config);
24     this.getGridEl().addClass("xedit-grid");
25
26     if(!this.selModel){
27         this.selModel = new Roo.grid.CellSelectionModel();
28     }
29
30     this.activeEditor = null;
31
32         this.addEvents({
33             /**
34              * @event beforeedit
35              * Fires before cell editing is triggered. The edit event object has the following properties <br />
36              * <ul style="padding:5px;padding-left:16px;">
37              * <li>grid - This grid</li>
38              * <li>record - The record being edited</li>
39              * <li>field - The field name being edited</li>
40              * <li>value - The value for the field being edited.</li>
41              * <li>row - The grid row index</li>
42              * <li>column - The grid column index</li>
43              * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
44              * </ul>
45              * @param {Object} e An edit event (see above for description)
46              */
47             "beforeedit" : true,
48             /**
49              * @event afteredit
50              * Fires after a cell is edited. <br />
51              * <ul style="padding:5px;padding-left:16px;">
52              * <li>grid - This grid</li>
53              * <li>record - The record being edited</li>
54              * <li>field - The field name being edited</li>
55              * <li>value - The value being set</li>
56              * <li>originalValue - The original value for the field, before the edit.</li>
57              * <li>row - The grid row index</li>
58              * <li>column - The grid column index</li>
59              * </ul>
60              * @param {Object} e An edit event (see above for description)
61              */
62             "afteredit" : true,
63             /**
64              * @event validateedit
65              * Fires after a cell is edited, but before the value is set in the record. 
66          * You can use this to modify the value being set in the field, Return false
67              * to cancel the change. The edit event object has the following properties <br />
68              * <ul style="padding:5px;padding-left:16px;">
69          * <li>editor - This editor</li>
70              * <li>grid - This grid</li>
71              * <li>record - The record being edited</li>
72              * <li>field - The field name being edited</li>
73              * <li>value - The value being set</li>
74              * <li>originalValue - The original value for the field, before the edit.</li>
75              * <li>row - The grid row index</li>
76              * <li>column - The grid column index</li>
77              * <li>cancel - Set this to true to cancel the edit or return false from your handler.</li>
78              * </ul>
79              * @param {Object} e An edit event (see above for description)
80              */
81             "validateedit" : true
82         });
83     this.on("bodyscroll", this.stopEditing,  this);
84     this.on(this.clicksToEdit == 1 ? "cellclick" : "celldblclick", this.onCellDblClick,  this);
85 };
86
87
88 Roo.extend(Roo.grid.ViewPanel, Roo.grid.Grid, {
89     
90     // private
91     rendered : false,
92     
93     /**
94     * @cfg {Boolean} autoWidth True to set the grid's width to the default total width of the grid's columns instead
95     * of a fixed width. Default is false.
96     */
97     /**
98     * @cfg {Number} maxHeight Sets the maximum height of the grid - ignored if autoHeight is not on.
99     */
100     /**
101      * Called once after all setup has been completed and the grid is ready to be rendered.
102      * @return {Roo.grid.Grid} this
103      */
104     render : function()
105     {
106         var c = this.container;
107         // try to detect autoHeight/width mode
108         if((!c.dom.offsetHeight || c.dom.offsetHeight < 20) || c.getStyle("height") == "auto"){
109             this.autoHeight = true;
110         }
111         var view = this.getView();
112         view.init(this);
113
114         c.on("click", this.onClick, this);
115         c.on("dblclick", this.onDblClick, this);
116         c.on("contextmenu", this.onContextMenu, this);
117         c.on("keydown", this.onKeyDown, this);
118
119         this.relayEvents(c, ["mousedown","mouseup","mouseover","mouseout","keypress"]);
120
121         this.getSelectionModel().init(this);
122
123         view.render();
124
125         if(this.loadMask){
126             this.loadMask = new Roo.LoadMask(this.container,
127                     Roo.apply({store:this.dataSource}, this.loadMask));
128         }
129         
130         
131         if (this.toolbar && this.toolbar.xtype) {
132             this.toolbar.container = this.getView().getHeaderPanel(true);
133             this.toolbar = new Roo.Toolbar(this.toolbar);
134         }
135         if (this.footer && this.footer.xtype) {
136             this.footer.dataSource = this.getDataSource();
137             this.footer.container = this.getView().getFooterPanel(true);
138             this.footer = Roo.factory(this.footer, Roo);
139         }
140         if (this.dropTarget && this.dropTarget.xtype) {
141             delete this.dropTarget.xtype;
142             this.dropTarget =  new Roo.dd.DropTarget(this.getView().mainBody, this.dropTarget);
143         }
144         
145         
146         this.rendered = true;
147         this.fireEvent('render', this);
148         return this;
149     }
150 });