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