Upgrade to ExtJS 3.0.0 - Released 07/06/2009
[extjs.git] / examples / dd / dragdropzones.js
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.onReady(function() {\r
8 \r
9     var patients = [{\r
10         insuranceCode: '11111',\r
11         name: 'Fred Bloggs',\r
12         address: 'Main Street',\r
13         telephone: '555 1234 123'\r
14     }, {\r
15         insuranceCode: '22222',\r
16         name: 'Fred West',\r
17         address: 'Cromwell Street',\r
18         telephone: '666 666 666'\r
19     }, {\r
20         insuranceCode: '33333',\r
21         name: 'Fred Mercury',\r
22         address: 'Over The Rainbow',\r
23         telephone: '555 321 0987'\r
24     }, {\r
25         insuranceCode: '44444',\r
26         name: 'Fred Forsyth',\r
27         address: 'Blimp Street',\r
28         telephone: '555 111 2222'\r
29     }, {\r
30         insuranceCode: '55555',\r
31         name: 'Fred Douglass',\r
32         address: 'Talbot County, Maryland',\r
33         telephone: 'N/A'\r
34     }];\r
35 \r
36     var PatientRecord = Ext.data.Record.create([{\r
37         name: 'name'\r
38     }, {\r
39         name: 'address'\r
40     }, {\r
41         name: 'telephone'\r
42     }]);\r
43 \r
44     var patientStore = new Ext.data.Store({\r
45         data: patients,\r
46         reader: new Ext.data.JsonReader({\r
47             id: 'insuranceCode'\r
48         }, PatientRecord)\r
49     });\r
50 \r
51     var hospitals = [{\r
52         code: 'AAAAA',\r
53         name: 'Saint Thomas',\r
54         address: 'Westminster Bridge Road, SE1 7EH',\r
55         telephone: '020 7188 7188'\r
56     }, {\r
57         code: 'BBBBB',\r
58         name: 'Queen\'s Medical Centre',\r
59         address: 'Derby Road, NG7 2UH',\r
60         telephone: '0115 924 9924'\r
61     }, {\r
62         code: 'CCCCC',\r
63         name: 'Saint Bartholomew',\r
64         address: 'West Smithfield, EC1A 7BE',\r
65         telephone: '020 7377 7000'\r
66     }, {\r
67         code: 'DDDDD',\r
68         name: 'Royal London',\r
69         address: 'Whitechapel, E1 1BB',\r
70         telephone: '020 7377 7000'\r
71     }];\r
72     \r
73     var HospitalRecord = Ext.data.Record.create([{\r
74         name: 'name'\r
75     }, {\r
76         name: 'address'\r
77     }, {\r
78         name: 'telephone'\r
79     }]);\r
80 \r
81     var hospitalStore = new Ext.data.Store({\r
82         data: hospitals,\r
83         reader: new Ext.data.JsonReader({\r
84             id: 'code'\r
85         }, HospitalRecord)\r
86     });\r
87     \r
88     var patientView = new Ext.DataView({\r
89         cls: 'patient-view',\r
90         tpl: '<tpl for=".">' +\r
91                 '<div class="patient-source"><table><tbody>' +\r
92                     '<tr><td class="patient-label">Name</td><td class="patient-name">{name}</td></tr>' +\r
93                     '<tr><td class="patient-label">Address</td><td class="patient-name">{address}</td></tr>' +\r
94                     '<tr><td class="patient-label">Telephone</td><td class="patient-name">{telephone}</td></tr>' +\r
95                 '</tbody></table></div>' +\r
96              '</tpl>',\r
97         itemSelector: 'div.patient-source',\r
98         store: patientStore,\r
99         listeners: {\r
100             render: initializePatientDragZone\r
101         }\r
102     });\r
103 \r
104     var helpWindow = new Ext.Window({\r
105         title: 'Source code',\r
106         width: 920,\r
107         height: 500,\r
108         closeAction: 'hide',\r
109         bodyCfg: {tag: 'textarea', readonly: true},\r
110         bodyStyle: {\r
111             backgroundColor: 'white',\r
112             margin: '0px',\r
113             border: '0px none'\r
114         },\r
115         listeners: {\r
116             render: function(w) {\r
117                 Ext.Ajax.request({\r
118                     url: 'dragdropzones.js',\r
119                     success: function(r) {\r
120                         w.body.dom.value = r.responseText;\r
121                     }\r
122                 });\r
123             }\r
124         }\r
125     });\r
126 \r
127     var hospitalGrid = new Ext.grid.GridPanel({\r
128         title: 'Hospitals',\r
129         region: 'center',\r
130         margins: '0 5 5 0',\r
131         bbar: [{\r
132             text: 'View Source',\r
133             handler: function() {\r
134                 helpWindow.show();\r
135             }\r
136         }],\r
137         columns: [{\r
138             dataIndex: 'name',\r
139             header: 'Name',\r
140             width: 200\r
141         }, {\r
142             dataIndex: 'address',\r
143             header: 'Address',\r
144             width: 300\r
145         }, {\r
146             dataIndex: 'telephone',\r
147             header: 'Telephone',\r
148             width: 100\r
149         }],\r
150         viewConfig: {\r
151             tpl: new Ext.XTemplate('<div class="hospital-target"></div>'),\r
152             enableRowBody: true,\r
153             getRowClass: function(rec, idx, p, store) {\r
154                 p.body = this.tpl.apply(rec.data);\r
155             }\r
156         },\r
157         store: hospitalStore,\r
158         listeners: {\r
159             render: initializeHospitalDropZone\r
160         }\r
161     });\r
162 \r
163     new Ext.Viewport({\r
164         layout: 'border',\r
165         items: [{\r
166             cls: 'app-header',\r
167             region: 'north',\r
168             height: 100,\r
169             html: '<h1>Patient Hospital Assignment</h1>',\r
170             margins: '5 5 5 5'\r
171         }, {\r
172             title: 'Patients',\r
173             region: 'west',\r
174             width: 300,\r
175             margins: '0 5 5 5',\r
176             items: patientView\r
177         }, hospitalGrid ]\r
178     });\r
179 });\r
180 \r
181 /*\r
182  * Here is where we "activate" the DataView.\r
183  * We have decided that each node with the class "patient-source" encapsulates a single draggable\r
184  * object.\r
185  *\r
186  * So we inject code into the DragZone which, when passed a mousedown event, interrogates\r
187  * the event to see if it was within an element with the class "patient-source". If so, we\r
188  * return non-null drag data.\r
189  *\r
190  * Returning non-null drag data indicates that the mousedown event has begun a dragging process.\r
191  * The data must contain a property called "ddel" which is a DOM element which provides an image\r
192  * of the data being dragged. The actual node clicked on is not dragged, a proxy element is dragged.\r
193  * We can insert any other data into the data object, and this will be used by a cooperating DropZone\r
194  * to perform the drop operation.\r
195  */\r
196 function initializePatientDragZone(v) {\r
197     v.dragZone = new Ext.dd.DragZone(v.getEl(), {\r
198 \r
199 //      On receipt of a mousedown event, see if it is within a draggable element.\r
200 //      Return a drag data object if so. The data object can contain arbitrary application\r
201 //      data, but it should also contain a DOM element in the ddel property to provide\r
202 //      a proxy to drag.\r
203         getDragData: function(e) {\r
204             var sourceEl = e.getTarget(v.itemSelector, 10);\r
205             if (sourceEl) {\r
206                 d = sourceEl.cloneNode(true);\r
207                 d.id = Ext.id();\r
208                 return v.dragData = {\r
209                     sourceEl: sourceEl,\r
210                     repairXY: Ext.fly(sourceEl).getXY(),\r
211                     ddel: d,\r
212                     patientData: v.getRecord(sourceEl).data\r
213                 }\r
214             }\r
215         },\r
216 \r
217 //      Provide coordinates for the proxy to slide back to on failed drag.\r
218 //      This is the original XY coordinates of the draggable element.\r
219         getRepairXY: function() {\r
220             return this.dragData.repairXY;\r
221         }\r
222     });\r
223 }\r
224 \r
225 /*\r
226  * Here is where we "activate" the GridPanel.\r
227  * We have decided that the element with class "hospital-target" is the element which can receieve\r
228  * drop gestures. So we inject a method "getTargetFromEvent" into the DropZone. This is constantly called\r
229  * while the mouse is moving over the DropZone, and it returns the target DOM element if it detects that\r
230  * the mouse if over an element which can receieve drop gestures.\r
231  *\r
232  * Once the DropZone has been informed by getTargetFromEvent that it is over a target, it will then\r
233  * call several "onNodeXXXX" methods at various points. These include:\r
234  *\r
235  * onNodeEnter\r
236  * onNodeOut\r
237  * onNodeOver\r
238  * onNodeDrop\r
239  *\r
240  * We provide implementations of each of these to provide behaviour for these events.\r
241  */\r
242 function initializeHospitalDropZone(g) {\r
243     g.dropZone = new Ext.dd.DropZone(g.getView().scroller, {\r
244 \r
245 //      If the mouse is over a target node, return that node. This is\r
246 //      provided as the "target" parameter in all "onNodeXXXX" node event handling functions\r
247         getTargetFromEvent: function(e) {\r
248             return e.getTarget('.hospital-target');\r
249         },\r
250 \r
251 //      On entry into a target node, highlight that node.\r
252         onNodeEnter : function(target, dd, e, data){ \r
253             Ext.fly(target).addClass('hospital-target-hover');\r
254         },\r
255 \r
256 //      On exit from a target node, unhighlight that node.\r
257         onNodeOut : function(target, dd, e, data){ \r
258             Ext.fly(target).removeClass('hospital-target-hover');\r
259         },\r
260 \r
261 //      While over a target node, return the default drop allowed class which\r
262 //      places a "tick" icon into the drag proxy.\r
263         onNodeOver : function(target, dd, e, data){ \r
264             return Ext.dd.DropZone.prototype.dropAllowed;\r
265         },\r
266 \r
267 //      On node drop, we can interrogate the target node to find the underlying\r
268 //      application object that is the real target of the dragged data.\r
269 //      In this case, it is a Record in the GridPanel's Store.\r
270 //      We can use the data set up by the DragZone's getDragData method to read\r
271 //      any data we decided to attach.\r
272         onNodeDrop : function(target, dd, e, data){\r
273             var rowIndex = g.getView().findRowIndex(target);\r
274             var h = g.getStore().getAt(rowIndex);\r
275             Ext.Msg.alert('Drop gesture', 'Dropped patient ' + data.patientData.name +\r
276                 ' on hospital ' + h.data.name);\r
277             return true;\r
278         }\r
279     });\r
280 }