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 | // A DropZone which cooperates with DragZones whose dragData contains |
---|
8 | // a "field" property representing a form Field. Fields may be dropped onto |
---|
9 | // grid data cells containing a matching data type. |
---|
10 | Ext.ux.CellFieldDropZone = Ext.extend(Ext.dd.DropZone, { |
---|
11 | constructor: function(){}, |
---|
12 | |
---|
13 | // Call the DropZone constructor using the View's scrolling element |
---|
14 | // only after the grid has been rendered. |
---|
15 | init: function(grid) { |
---|
16 | if (grid.rendered) { |
---|
17 | this.grid = grid; |
---|
18 | this.view = grid.getView(); |
---|
19 | this.store = grid.getStore(); |
---|
20 | Ext.ux.CellFieldDropZone.superclass.constructor.call(this, this.view.scroller); |
---|
21 | } else { |
---|
22 | grid.on('render', this.init, this); |
---|
23 | } |
---|
24 | }, |
---|
25 | |
---|
26 | // Scroll the main configured Element when we drag close to the edge |
---|
27 | containerScroll: true, |
---|
28 | |
---|
29 | getTargetFromEvent: function(e) { |
---|
30 | // Ascertain whether the mousemove is within a grid cell |
---|
31 | var t = e.getTarget(this.view.cellSelector); |
---|
32 | if (t) { |
---|
33 | |
---|
34 | // We *are* within a grid cell, so ask the View exactly which one, |
---|
35 | // Extract data from the Model to create a target object for |
---|
36 | // processing in subsequent onNodeXXXX methods. Note that the target does |
---|
37 | // not have to be a DOM element. It can be whatever the noNodeXXX methods are |
---|
38 | // programmed to expect. |
---|
39 | var rowIndex = this.view.findRowIndex(t); |
---|
40 | var columnIndex = this.view.findCellIndex(t); |
---|
41 | if ((rowIndex !== false) && (columnIndex !== false)) { |
---|
42 | return { |
---|
43 | node: t, |
---|
44 | record: this.store.getAt(rowIndex), |
---|
45 | fieldName: this.grid.getColumnModel().getDataIndex(columnIndex) |
---|
46 | } |
---|
47 | } |
---|
48 | } |
---|
49 | }, |
---|
50 | |
---|
51 | // On Node enter, see if it is valid for us to drop the field on that type of column. |
---|
52 | onNodeEnter: function(target, dd, e, dragData) { |
---|
53 | delete this.dropOK; |
---|
54 | if (!target) { |
---|
55 | return; |
---|
56 | } |
---|
57 | |
---|
58 | // Check that a field is being dragged. |
---|
59 | var f = dragData.field; |
---|
60 | if (!f) { |
---|
61 | return; |
---|
62 | } |
---|
63 | |
---|
64 | // Check whether the data type of the column being dropped on accepts the |
---|
65 | // dragged field type. If so, set dropOK flag, and highlight the target node. |
---|
66 | var type = target.record.fields.get(target.fieldName).type; |
---|
67 | switch (type) { |
---|
68 | case 'float': |
---|
69 | case 'int': |
---|
70 | if (!f.isXType('numberfield')) { |
---|
71 | return; |
---|
72 | } |
---|
73 | break; |
---|
74 | case 'date': |
---|
75 | if (!f.isXType('datefield')) { |
---|
76 | return; |
---|
77 | } |
---|
78 | break; |
---|
79 | case 'boolean': |
---|
80 | if (!f.isXType('checkbox')) { |
---|
81 | return; |
---|
82 | } |
---|
83 | } |
---|
84 | this.dropOK = true; |
---|
85 | Ext.fly(target.node).addClass('x-drop-target-active'); |
---|
86 | }, |
---|
87 | |
---|
88 | // Return the class name to add to the drag proxy. This provides a visual indication |
---|
89 | // of drop allowed or not allowed. |
---|
90 | onNodeOver: function(target, dd, e, dragData) { |
---|
91 | return this.dropOK ? this.dropAllowed : this.dropNotAllowed; |
---|
92 | }, |
---|
93 | |
---|
94 | // nhighlight the target node. |
---|
95 | onNodeOut: function(target, dd, e, dragData) { |
---|
96 | Ext.fly(target.node).removeClass('x-drop-target-active'); |
---|
97 | }, |
---|
98 | |
---|
99 | // Process the drop event if we have previously ascertained that a drop is OK. |
---|
100 | onNodeDrop: function(target, dd, e, dragData) { |
---|
101 | if (this.dropOK) { |
---|
102 | target.record.set(target.fieldName, dragData.field.getValue()); |
---|
103 | return true; |
---|
104 | } |
---|
105 | } |
---|
106 | }); |
---|
107 | |
---|
108 | // A class which makes Fields within a Panel draggable. |
---|
109 | // the dragData delivered to a coooperating DropZone's methods contains |
---|
110 | // the dragged Field in the property "field". |
---|
111 | Ext.ux.PanelFieldDragZone = Ext.extend(Ext.dd.DragZone, { |
---|
112 | constructor: function(){}, |
---|
113 | |
---|
114 | // Call the DRagZone's constructor. The Panel must have been rendered. |
---|
115 | init: function(panel) { |
---|
116 | if (panel.nodeType) { |
---|
117 | Ext.ux.PanelFieldDragZone.superclass.init.apply(this, arguments); |
---|
118 | } else { |
---|
119 | if (panel.rendered) { |
---|
120 | Ext.ux.PanelFieldDragZone.superclass.constructor.call(this, panel.getEl()); |
---|
121 | var i = Ext.fly(panel.getEl()).select('input'); |
---|
122 | i.unselectable(); |
---|
123 | } else { |
---|
124 | panel.on('afterlayout', this.init, this, {single: true}); |
---|
125 | } |
---|
126 | } |
---|
127 | }, |
---|
128 | |
---|
129 | scroll: false, |
---|
130 | |
---|
131 | // On mousedown, we ascertain whether it is on one of our draggable Fields. |
---|
132 | // If so, we collect data about the draggable object, and return a drag data |
---|
133 | // object which contains our own data, plus a "ddel" property which is a DOM |
---|
134 | // node which provides a "view" of the dragged data. |
---|
135 | getDragData: function(e) { |
---|
136 | var t = e.getTarget('input'); |
---|
137 | if (t) { |
---|
138 | e.stopEvent(); |
---|
139 | |
---|
140 | // Ugly code to "detach" the drag gesture from the input field. |
---|
141 | // Without this, Opera never changes the mouseover target from the input field |
---|
142 | // even when dragging outside of the field - it just keeps selecting. |
---|
143 | if (Ext.isOpera) { |
---|
144 | Ext.fly(t).on('mousemove', function(e1){ |
---|
145 | t.style.visibility = 'hidden'; |
---|
146 | (function(){ |
---|
147 | t.style.visibility = ''; |
---|
148 | }).defer(1); |
---|
149 | }, null, {single:true}); |
---|
150 | } |
---|
151 | |
---|
152 | // Get the data we are dragging: the Field |
---|
153 | // create a ddel for the drag proxy to display |
---|
154 | var f = Ext.getCmp(t.id); |
---|
155 | var d = document.createElement('div'); |
---|
156 | d.className = 'x-form-text'; |
---|
157 | d.appendChild(document.createTextNode(t.value)); |
---|
158 | Ext.fly(d).setWidth(f.getEl().getWidth()); |
---|
159 | return { |
---|
160 | field: f, |
---|
161 | ddel: d |
---|
162 | }; |
---|
163 | } |
---|
164 | }, |
---|
165 | |
---|
166 | // The coordinates to slide the drag proxy back to on failed drop. |
---|
167 | getRepairXY: function() { |
---|
168 | return this.dragData.field.getEl().getXY(); |
---|
169 | } |
---|
170 | }); |
---|
171 | |
---|
172 | Ext.onReady(function(){ |
---|
173 | |
---|
174 | var myData = [ |
---|
175 | ['3m Co',71.72,0.02,0.03,'9/1 12:00am'], |
---|
176 | ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'], |
---|
177 | ['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'], |
---|
178 | ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'], |
---|
179 | ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'], |
---|
180 | ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'], |
---|
181 | ['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'], |
---|
182 | ['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'], |
---|
183 | ['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'], |
---|
184 | ['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'], |
---|
185 | ['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'], |
---|
186 | ['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'], |
---|
187 | ['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'], |
---|
188 | ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'], |
---|
189 | ['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'], |
---|
190 | ['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'], |
---|
191 | ['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'], |
---|
192 | ['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'], |
---|
193 | ['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'], |
---|
194 | ['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am'], |
---|
195 | ['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'], |
---|
196 | ['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'], |
---|
197 | ['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'], |
---|
198 | ['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'], |
---|
199 | ['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'], |
---|
200 | ['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'], |
---|
201 | ['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'], |
---|
202 | ['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am'], |
---|
203 | ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am'] |
---|
204 | ]; |
---|
205 | |
---|
206 | // example of custom renderer function |
---|
207 | function change(val){ |
---|
208 | if(val > 0){ |
---|
209 | return '<span style="color:green;">' + val + '</span>'; |
---|
210 | }else if(val < 0){ |
---|
211 | return '<span style="color:red;">' + val + '</span>'; |
---|
212 | } |
---|
213 | return val; |
---|
214 | } |
---|
215 | |
---|
216 | // example of custom renderer function |
---|
217 | function pctChange(val){ |
---|
218 | if(val > 0){ |
---|
219 | return '<span style="color:green;">' + val + '%</span>'; |
---|
220 | }else if(val < 0){ |
---|
221 | return '<span style="color:red;">' + val + '%</span>'; |
---|
222 | } |
---|
223 | return val; |
---|
224 | } |
---|
225 | |
---|
226 | // create the data store |
---|
227 | var store = new Ext.data.ArrayStore({ |
---|
228 | fields: [ |
---|
229 | {name: 'company'}, |
---|
230 | {name: 'price', type: 'float'}, |
---|
231 | {name: 'change', type: 'float'}, |
---|
232 | {name: 'pctChange', type: 'float'}, |
---|
233 | {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'} |
---|
234 | ] |
---|
235 | }); |
---|
236 | store.loadData(myData); |
---|
237 | |
---|
238 | var helpWindow = new Ext.Window({ |
---|
239 | title: 'Source code', |
---|
240 | width: 920, |
---|
241 | height: 500, |
---|
242 | closeAction: 'hide', |
---|
243 | bodyCfg: {tag: 'textarea', readonly: true}, |
---|
244 | bodyStyle: { |
---|
245 | backgroundColor: 'white', |
---|
246 | margin: '0px', |
---|
247 | border: '0px none' |
---|
248 | }, |
---|
249 | listeners: { |
---|
250 | render: function(w) { |
---|
251 | Ext.Ajax.request({ |
---|
252 | url: 'field-to-grid-dd.js', |
---|
253 | success: function(r) { |
---|
254 | w.body.dom.value = r.responseText; |
---|
255 | } |
---|
256 | }); |
---|
257 | } |
---|
258 | } |
---|
259 | }); |
---|
260 | |
---|
261 | // create the Grid |
---|
262 | var grid = new Ext.grid.GridPanel({ |
---|
263 | store: store, |
---|
264 | columns: [ |
---|
265 | {id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'}, |
---|
266 | {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'}, |
---|
267 | {header: "Change", width: 75, sortable: true, renderer: change, dataIndex: 'change'}, |
---|
268 | {header: "% Change", width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'}, |
---|
269 | {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'} |
---|
270 | ], |
---|
271 | plugins: new Ext.ux.CellFieldDropZone(), |
---|
272 | stripeRows: true, |
---|
273 | autoExpandColumn: 'company', |
---|
274 | height:350, |
---|
275 | width:600, |
---|
276 | title:'Array Grid', |
---|
277 | bbar: new Ext.PagingToolbar({ |
---|
278 | buttons: [{ |
---|
279 | text: 'View Source', |
---|
280 | handler: function() { |
---|
281 | helpWindow.show(); |
---|
282 | } |
---|
283 | }], |
---|
284 | store: store, |
---|
285 | pageSize: 25 |
---|
286 | }) |
---|
287 | }); |
---|
288 | |
---|
289 | grid.render('grid-example'); |
---|
290 | grid.getSelectionModel().selectFirstRow(); |
---|
291 | |
---|
292 | var f = new Ext.Panel({ |
---|
293 | frame: true, |
---|
294 | layout: 'form', |
---|
295 | width: 600, |
---|
296 | plugins: new Ext.ux.PanelFieldDragZone(), |
---|
297 | style: { |
---|
298 | 'margin-top': '10px' |
---|
299 | }, |
---|
300 | labelWidth: 150, |
---|
301 | items: [{ |
---|
302 | xtype: 'textfield', |
---|
303 | fieldLabel: 'Drag this text', |
---|
304 | value: 'test' |
---|
305 | },{ |
---|
306 | xtype: 'numberfield', |
---|
307 | fieldLabel: 'Drag this number', |
---|
308 | value: '1.2' |
---|
309 | },{ |
---|
310 | xtype: 'datefield', |
---|
311 | fieldLabel: 'Drag this date', |
---|
312 | value: new Date() |
---|
313 | }], |
---|
314 | renderTo: Ext.getBody() |
---|
315 | }); |
---|
316 | }); |
---|