1 | /*! |
---|
2 | * Ext JS Library 3.0.0 |
---|
3 | * Copyright(c) 2006-2009 Ext JS, LLC |
---|
4 | * licensing@extjs.com |
---|
5 | * http://www.extjs.com/license |
---|
6 | */ |
---|
7 | Ext.ns('Ext.ux.grid'); |
---|
8 | |
---|
9 | /** |
---|
10 | * @class Ext.ux.grid.RowExpander |
---|
11 | * @extends Ext.util.Observable |
---|
12 | * Plugin (ptype = 'rowexpander') that adds the ability to have a Column in a grid which enables |
---|
13 | * a second row body which expands/contracts. The expand/contract behavior is configurable to react |
---|
14 | * on clicking of the column, double click of the row, and/or hitting enter while a row is selected. |
---|
15 | * |
---|
16 | * @ptype rowexpander |
---|
17 | */ |
---|
18 | Ext.ux.grid.RowExpander = Ext.extend(Ext.util.Observable, { |
---|
19 | /** |
---|
20 | * @cfg {Boolean} expandOnEnter |
---|
21 | * <tt>true</tt> to toggle selected row(s) between expanded/collapsed when the enter |
---|
22 | * key is pressed (defaults to <tt>true</tt>). |
---|
23 | */ |
---|
24 | expandOnEnter : true, |
---|
25 | /** |
---|
26 | * @cfg {Boolean} expandOnDblClick |
---|
27 | * <tt>true</tt> to toggle a row between expanded/collapsed when double clicked |
---|
28 | * (defaults to <tt>true</tt>). |
---|
29 | */ |
---|
30 | expandOnDblClick : true, |
---|
31 | |
---|
32 | header : '', |
---|
33 | width : 20, |
---|
34 | sortable : false, |
---|
35 | fixed : true, |
---|
36 | menuDisabled : true, |
---|
37 | dataIndex : '', |
---|
38 | id : 'expander', |
---|
39 | lazyRender : true, |
---|
40 | enableCaching : true, |
---|
41 | |
---|
42 | constructor: function(config){ |
---|
43 | Ext.apply(this, config); |
---|
44 | |
---|
45 | this.addEvents({ |
---|
46 | /** |
---|
47 | * @event beforeexpand |
---|
48 | * Fires before the row expands. Have the listener return false to prevent the row from expanding. |
---|
49 | * @param {Object} this RowExpander object. |
---|
50 | * @param {Object} Ext.data.Record Record for the selected row. |
---|
51 | * @param {Object} body body element for the secondary row. |
---|
52 | * @param {Number} rowIndex The current row index. |
---|
53 | */ |
---|
54 | beforeexpand: true, |
---|
55 | /** |
---|
56 | * @event expand |
---|
57 | * Fires after the row expands. |
---|
58 | * @param {Object} this RowExpander object. |
---|
59 | * @param {Object} Ext.data.Record Record for the selected row. |
---|
60 | * @param {Object} body body element for the secondary row. |
---|
61 | * @param {Number} rowIndex The current row index. |
---|
62 | */ |
---|
63 | expand: true, |
---|
64 | /** |
---|
65 | * @event beforecollapse |
---|
66 | * Fires before the row collapses. Have the listener return false to prevent the row from collapsing. |
---|
67 | * @param {Object} this RowExpander object. |
---|
68 | * @param {Object} Ext.data.Record Record for the selected row. |
---|
69 | * @param {Object} body body element for the secondary row. |
---|
70 | * @param {Number} rowIndex The current row index. |
---|
71 | */ |
---|
72 | beforecollapse: true, |
---|
73 | /** |
---|
74 | * @event collapse |
---|
75 | * Fires after the row collapses. |
---|
76 | * @param {Object} this RowExpander object. |
---|
77 | * @param {Object} Ext.data.Record Record for the selected row. |
---|
78 | * @param {Object} body body element for the secondary row. |
---|
79 | * @param {Number} rowIndex The current row index. |
---|
80 | */ |
---|
81 | collapse: true |
---|
82 | }); |
---|
83 | |
---|
84 | Ext.ux.grid.RowExpander.superclass.constructor.call(this); |
---|
85 | |
---|
86 | if(this.tpl){ |
---|
87 | if(typeof this.tpl == 'string'){ |
---|
88 | this.tpl = new Ext.Template(this.tpl); |
---|
89 | } |
---|
90 | this.tpl.compile(); |
---|
91 | } |
---|
92 | |
---|
93 | this.state = {}; |
---|
94 | this.bodyContent = {}; |
---|
95 | }, |
---|
96 | |
---|
97 | getRowClass : function(record, rowIndex, p, ds){ |
---|
98 | p.cols = p.cols-1; |
---|
99 | var content = this.bodyContent[record.id]; |
---|
100 | if(!content && !this.lazyRender){ |
---|
101 | content = this.getBodyContent(record, rowIndex); |
---|
102 | } |
---|
103 | if(content){ |
---|
104 | p.body = content; |
---|
105 | } |
---|
106 | return this.state[record.id] ? 'x-grid3-row-expanded' : 'x-grid3-row-collapsed'; |
---|
107 | }, |
---|
108 | |
---|
109 | init : function(grid){ |
---|
110 | this.grid = grid; |
---|
111 | |
---|
112 | var view = grid.getView(); |
---|
113 | view.getRowClass = this.getRowClass.createDelegate(this); |
---|
114 | |
---|
115 | view.enableRowBody = true; |
---|
116 | |
---|
117 | |
---|
118 | grid.on('render', this.onRender, this); |
---|
119 | grid.on('destroy', this.onDestroy, this); |
---|
120 | }, |
---|
121 | |
---|
122 | // @private |
---|
123 | onRender: function() { |
---|
124 | var grid = this.grid; |
---|
125 | var mainBody = grid.getView().mainBody; |
---|
126 | mainBody.on('mousedown', this.onMouseDown, this, {delegate: '.x-grid3-row-expander'}); |
---|
127 | if (this.expandOnEnter) { |
---|
128 | this.keyNav = new Ext.KeyNav(this.grid.getGridEl(), { |
---|
129 | 'enter' : this.onEnter, |
---|
130 | scope: this |
---|
131 | }); |
---|
132 | } |
---|
133 | if (this.expandOnDblClick) { |
---|
134 | grid.on('rowdblclick', this.onRowDblClick, this); |
---|
135 | } |
---|
136 | }, |
---|
137 | |
---|
138 | // @private |
---|
139 | onDestroy: function() { |
---|
140 | this.keyNav.disable(); |
---|
141 | delete this.keyNav; |
---|
142 | var mainBody = this.grid.getView().mainBody; |
---|
143 | mainBody.un('mousedown', this.onMouseDown, this); |
---|
144 | }, |
---|
145 | // @private |
---|
146 | onRowDblClick: function(grid, rowIdx, e) { |
---|
147 | this.toggleRow(rowIdx); |
---|
148 | }, |
---|
149 | |
---|
150 | onEnter: function(e) { |
---|
151 | var g = this.grid; |
---|
152 | var sm = g.getSelectionModel(); |
---|
153 | var sels = sm.getSelections(); |
---|
154 | for (var i = 0, len = sels.length; i < len; i++) { |
---|
155 | var rowIdx = g.getStore().indexOf(sels[i]); |
---|
156 | this.toggleRow(rowIdx); |
---|
157 | } |
---|
158 | }, |
---|
159 | |
---|
160 | getBodyContent : function(record, index){ |
---|
161 | if(!this.enableCaching){ |
---|
162 | return this.tpl.apply(record.data); |
---|
163 | } |
---|
164 | var content = this.bodyContent[record.id]; |
---|
165 | if(!content){ |
---|
166 | content = this.tpl.apply(record.data); |
---|
167 | this.bodyContent[record.id] = content; |
---|
168 | } |
---|
169 | return content; |
---|
170 | }, |
---|
171 | |
---|
172 | onMouseDown : function(e, t){ |
---|
173 | e.stopEvent(); |
---|
174 | var row = e.getTarget('.x-grid3-row'); |
---|
175 | this.toggleRow(row); |
---|
176 | }, |
---|
177 | |
---|
178 | renderer : function(v, p, record){ |
---|
179 | p.cellAttr = 'rowspan="2"'; |
---|
180 | return '<div class="x-grid3-row-expander"> </div>'; |
---|
181 | }, |
---|
182 | |
---|
183 | beforeExpand : function(record, body, rowIndex){ |
---|
184 | if(this.fireEvent('beforeexpand', this, record, body, rowIndex) !== false){ |
---|
185 | if(this.tpl && this.lazyRender){ |
---|
186 | body.innerHTML = this.getBodyContent(record, rowIndex); |
---|
187 | } |
---|
188 | return true; |
---|
189 | }else{ |
---|
190 | return false; |
---|
191 | } |
---|
192 | }, |
---|
193 | |
---|
194 | toggleRow : function(row){ |
---|
195 | if(typeof row == 'number'){ |
---|
196 | row = this.grid.view.getRow(row); |
---|
197 | } |
---|
198 | this[Ext.fly(row).hasClass('x-grid3-row-collapsed') ? 'expandRow' : 'collapseRow'](row); |
---|
199 | }, |
---|
200 | |
---|
201 | expandRow : function(row){ |
---|
202 | if(typeof row == 'number'){ |
---|
203 | row = this.grid.view.getRow(row); |
---|
204 | } |
---|
205 | var record = this.grid.store.getAt(row.rowIndex); |
---|
206 | var body = Ext.DomQuery.selectNode('tr:nth(2) div.x-grid3-row-body', row); |
---|
207 | if(this.beforeExpand(record, body, row.rowIndex)){ |
---|
208 | this.state[record.id] = true; |
---|
209 | Ext.fly(row).replaceClass('x-grid3-row-collapsed', 'x-grid3-row-expanded'); |
---|
210 | this.fireEvent('expand', this, record, body, row.rowIndex); |
---|
211 | } |
---|
212 | }, |
---|
213 | |
---|
214 | collapseRow : function(row){ |
---|
215 | if(typeof row == 'number'){ |
---|
216 | row = this.grid.view.getRow(row); |
---|
217 | } |
---|
218 | var record = this.grid.store.getAt(row.rowIndex); |
---|
219 | var body = Ext.fly(row).child('tr:nth(1) div.x-grid3-row-body', true); |
---|
220 | if(this.fireEvent('beforecollapse', this, record, body, row.rowIndex) !== false){ |
---|
221 | this.state[record.id] = false; |
---|
222 | Ext.fly(row).replaceClass('x-grid3-row-expanded', 'x-grid3-row-collapsed'); |
---|
223 | this.fireEvent('collapse', this, record, body, row.rowIndex); |
---|
224 | } |
---|
225 | } |
---|
226 | }); |
---|
227 | |
---|
228 | Ext.preg('rowexpander', Ext.ux.grid.RowExpander); |
---|
229 | |
---|
230 | //backwards compat |
---|
231 | Ext.grid.RowExpander = Ext.ux.grid.RowExpander; |
---|