3 * Copyright(c) 2006-2009 Ext JS, LLC
5 * http://www.extjs.com/license
7 Ext.onReady(function() {
\r
10 insuranceCode: '11111',
\r
11 name: 'Fred Bloggs',
\r
12 address: 'Main Street',
\r
13 telephone: '555 1234 123'
\r
15 insuranceCode: '22222',
\r
17 address: 'Cromwell Street',
\r
18 telephone: '666 666 666'
\r
20 insuranceCode: '33333',
\r
21 name: 'Fred Mercury',
\r
22 address: 'Over The Rainbow',
\r
23 telephone: '555 321 0987'
\r
25 insuranceCode: '44444',
\r
26 name: 'Fred Forsyth',
\r
27 address: 'Blimp Street',
\r
28 telephone: '555 111 2222'
\r
30 insuranceCode: '55555',
\r
31 name: 'Fred Douglass',
\r
32 address: 'Talbot County, Maryland',
\r
36 var PatientRecord = Ext.data.Record.create([{
\r
44 var patientStore = new Ext.data.Store({
\r
46 reader: new Ext.data.JsonReader({
\r
53 name: 'Saint Thomas',
\r
54 address: 'Westminster Bridge Road, SE1 7EH',
\r
55 telephone: '020 7188 7188'
\r
58 name: 'Queen\'s Medical Centre',
\r
59 address: 'Derby Road, NG7 2UH',
\r
60 telephone: '0115 924 9924'
\r
63 name: 'Saint Bartholomew',
\r
64 address: 'West Smithfield, EC1A 7BE',
\r
65 telephone: '020 7377 7000'
\r
68 name: 'Royal London',
\r
69 address: 'Whitechapel, E1 1BB',
\r
70 telephone: '020 7377 7000'
\r
73 var HospitalRecord = Ext.data.Record.create([{
\r
81 var hospitalStore = new Ext.data.Store({
\r
83 reader: new Ext.data.JsonReader({
\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
97 itemSelector: 'div.patient-source',
\r
98 store: patientStore,
\r
100 render: initializePatientDragZone
\r
104 var helpWindow = new Ext.Window({
\r
105 title: 'Source code',
\r
108 closeAction: 'hide',
\r
109 bodyCfg: {tag: 'textarea', readonly: true},
\r
111 backgroundColor: 'white',
\r
116 render: function(w) {
\r
118 url: 'dragdropzones.js',
\r
119 success: function(r) {
\r
120 w.body.dom.value = r.responseText;
\r
127 var hospitalGrid = new Ext.grid.GridPanel({
\r
128 title: 'Hospitals',
\r
130 margins: '0 5 5 0',
\r
132 text: 'View Source',
\r
133 handler: function() {
\r
142 dataIndex: 'address',
\r
146 dataIndex: 'telephone',
\r
147 header: 'Telephone',
\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
157 store: hospitalStore,
\r
159 render: initializeHospitalDropZone
\r
169 html: '<h1>Patient Hospital Assignment</h1>',
\r
175 margins: '0 5 5 5',
\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
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
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
196 function initializePatientDragZone(v) {
\r
197 v.dragZone = new Ext.dd.DragZone(v.getEl(), {
\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
206 d = sourceEl.cloneNode(true);
\r
208 return v.dragData = {
\r
209 sourceEl: sourceEl,
\r
210 repairXY: Ext.fly(sourceEl).getXY(),
\r
212 patientData: v.getRecord(sourceEl).data
\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
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
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
240 * We provide implementations of each of these to provide behaviour for these events.
\r
242 function initializeHospitalDropZone(g) {
\r
243 g.dropZone = new Ext.dd.DropZone(g.getView().scroller, {
\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
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
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
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
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