3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
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(){},
13 // Call the DropZone constructor using the View's scrolling element
14 // only after the grid has been rendered.
15 init: function(grid) {
18 this.view = grid.getView();
19 this.store = grid.getStore();
20 Ext.ux.CellFieldDropZone.superclass.constructor.call(this, this.view.scroller);
22 grid.on('render', this.init, this);
26 // Scroll the main configured Element when we drag close to the edge
27 containerScroll: true,
29 getTargetFromEvent: function(e) {
30 // Ascertain whether the mousemove is within a grid cell
31 var t = e.getTarget(this.view.cellSelector);
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)) {
44 record: this.store.getAt(rowIndex),
45 fieldName: this.grid.getColumnModel().getDataIndex(columnIndex)
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) {
58 // Check that a field is being dragged.
59 var f = dragData.field;
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 types = Ext.data.Types;
71 if (!f.isXType('numberfield')) {
76 if (!f.isXType('datefield')) {
81 if (!f.isXType('checkbox')) {
86 Ext.fly(target.node).addClass('x-drop-target-active');
89 // Return the class name to add to the drag proxy. This provides a visual indication
90 // of drop allowed or not allowed.
91 onNodeOver: function(target, dd, e, dragData) {
92 return this.dropOK ? this.dropAllowed : this.dropNotAllowed;
95 // nhighlight the target node.
96 onNodeOut: function(target, dd, e, dragData) {
97 Ext.fly(target.node).removeClass('x-drop-target-active');
100 // Process the drop event if we have previously ascertained that a drop is OK.
101 onNodeDrop: function(target, dd, e, dragData) {
103 target.record.set(target.fieldName, dragData.field.getValue());
109 // A class which makes Fields within a Panel draggable.
110 // the dragData delivered to a coooperating DropZone's methods contains
111 // the dragged Field in the property "field".
112 Ext.ux.PanelFieldDragZone = Ext.extend(Ext.dd.DragZone, {
113 constructor: function(){},
115 // Call the DRagZone's constructor. The Panel must have been rendered.
116 init: function(panel) {
117 if (panel.nodeType) {
118 Ext.ux.PanelFieldDragZone.superclass.init.apply(this, arguments);
120 if (panel.rendered) {
121 Ext.ux.PanelFieldDragZone.superclass.constructor.call(this, panel.getEl());
122 var i = Ext.fly(panel.getEl()).select('input');
125 panel.on('afterlayout', this.init, this, {single: true});
132 // On mousedown, we ascertain whether it is on one of our draggable Fields.
133 // If so, we collect data about the draggable object, and return a drag data
134 // object which contains our own data, plus a "ddel" property which is a DOM
135 // node which provides a "view" of the dragged data.
136 getDragData: function(e) {
137 var t = e.getTarget('input');
141 // Ugly code to "detach" the drag gesture from the input field.
142 // Without this, Opera never changes the mouseover target from the input field
143 // even when dragging outside of the field - it just keeps selecting.
145 Ext.fly(t).on('mousemove', function(e1){
146 t.style.visibility = 'hidden';
148 t.style.visibility = '';
150 }, null, {single:true});
153 // Get the data we are dragging: the Field
154 // create a ddel for the drag proxy to display
155 var f = Ext.getCmp(t.id);
156 var d = document.createElement('div');
157 d.className = 'x-form-text';
158 d.appendChild(document.createTextNode(t.value));
159 Ext.fly(d).setWidth(f.getEl().getWidth());
167 // The coordinates to slide the drag proxy back to on failed drop.
168 getRepairXY: function() {
169 return this.dragData.field.getEl().getXY();
173 Ext.onReady(function(){
176 ['3m Co',71.72,0.02,0.03,'9/1 12:00am'],
177 ['Alcoa Inc',29.01,0.42,1.47,'9/1 12:00am'],
178 ['Altria Group Inc',83.81,0.28,0.34,'9/1 12:00am'],
179 ['American Express Company',52.55,0.01,0.02,'9/1 12:00am'],
180 ['American International Group, Inc.',64.13,0.31,0.49,'9/1 12:00am'],
181 ['AT&T Inc.',31.61,-0.48,-1.54,'9/1 12:00am'],
182 ['Boeing Co.',75.43,0.53,0.71,'9/1 12:00am'],
183 ['Caterpillar Inc.',67.27,0.92,1.39,'9/1 12:00am'],
184 ['Citigroup, Inc.',49.37,0.02,0.04,'9/1 12:00am'],
185 ['E.I. du Pont de Nemours and Company',40.48,0.51,1.28,'9/1 12:00am'],
186 ['Exxon Mobil Corp',68.1,-0.43,-0.64,'9/1 12:00am'],
187 ['General Electric Company',34.14,-0.08,-0.23,'9/1 12:00am'],
188 ['General Motors Corporation',30.27,1.09,3.74,'9/1 12:00am'],
189 ['Hewlett-Packard Co.',36.53,-0.03,-0.08,'9/1 12:00am'],
190 ['Honeywell Intl Inc',38.77,0.05,0.13,'9/1 12:00am'],
191 ['Intel Corporation',19.88,0.31,1.58,'9/1 12:00am'],
192 ['International Business Machines',81.41,0.44,0.54,'9/1 12:00am'],
193 ['Johnson & Johnson',64.72,0.06,0.09,'9/1 12:00am'],
194 ['JP Morgan & Chase & Co',45.73,0.07,0.15,'9/1 12:00am'],
195 ['McDonald\'s Corporation',36.76,0.86,2.40,'9/1 12:00am'],
196 ['Merck & Co., Inc.',40.96,0.41,1.01,'9/1 12:00am'],
197 ['Microsoft Corporation',25.84,0.14,0.54,'9/1 12:00am'],
198 ['Pfizer Inc',27.96,0.4,1.45,'9/1 12:00am'],
199 ['The Coca-Cola Company',45.07,0.26,0.58,'9/1 12:00am'],
200 ['The Home Depot, Inc.',34.64,0.35,1.02,'9/1 12:00am'],
201 ['The Procter & Gamble Company',61.91,0.01,0.02,'9/1 12:00am'],
202 ['United Technologies Corporation',63.26,0.55,0.88,'9/1 12:00am'],
203 ['Verizon Communications',35.57,0.39,1.11,'9/1 12:00am'],
204 ['Wal-Mart Stores, Inc.',45.45,0.73,1.63,'9/1 12:00am']
207 // example of custom renderer function
208 function change(val){
210 return '<span style="color:green;">' + val + '</span>';
212 return '<span style="color:red;">' + val + '</span>';
217 // example of custom renderer function
218 function pctChange(val){
220 return '<span style="color:green;">' + val + '%</span>';
222 return '<span style="color:red;">' + val + '%</span>';
227 // create the data store
228 var store = new Ext.data.ArrayStore({
231 {name: 'price', type: 'float'},
232 {name: 'change', type: 'float'},
233 {name: 'pctChange', type: 'float'},
234 {name: 'lastChange', type: 'date', dateFormat: 'n/j h:ia'}
237 store.loadData(myData);
239 var helpWindow = new Ext.Window({
240 title: 'Source code',
244 bodyCfg: {tag: 'textarea', readonly: true},
246 backgroundColor: 'white',
251 render: function(w) {
253 url: 'field-to-grid-dd.js',
254 success: function(r) {
255 w.body.dom.value = r.responseText;
263 var grid = new Ext.grid.GridPanel({
266 {id:'company',header: "Company", width: 160, sortable: true, dataIndex: 'company'},
267 {header: "Price", width: 75, sortable: true, renderer: 'usMoney', dataIndex: 'price'},
268 {header: "Change", width: 75, sortable: true, renderer: change, dataIndex: 'change'},
269 {header: "% Change", width: 75, sortable: true, renderer: pctChange, dataIndex: 'pctChange'},
270 {header: "Last Updated", width: 85, sortable: true, renderer: Ext.util.Format.dateRenderer('m/d/Y'), dataIndex: 'lastChange'}
272 plugins: new Ext.ux.CellFieldDropZone(),
274 autoExpandColumn: 'company',
278 bbar: new Ext.PagingToolbar({
281 handler: function() {
290 grid.render('grid-example');
291 grid.getSelectionModel().selectFirstRow();
293 var f = new Ext.Panel({
297 plugins: new Ext.ux.PanelFieldDragZone(),
304 fieldLabel: 'Drag this text',
307 xtype: 'numberfield',
308 fieldLabel: 'Drag this number',
312 fieldLabel: 'Drag this date',
315 renderTo: Ext.getBody()