3 <title>The source code</title>
\r
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
7 <body onload="prettyPrint();">
\r
8 <pre class="prettyprint lang-js">Ext.onReady(function() {
\r
11 insuranceCode: '11111',
\r
12 name: 'Fred Bloggs',
\r
13 address: 'Main Street',
\r
14 telephone: '555 1234 123'
\r
16 insuranceCode: '22222',
\r
18 address: 'Cromwell Street',
\r
19 telephone: '666 666 666'
\r
21 insuranceCode: '33333',
\r
22 name: 'Fred Mercury',
\r
23 address: 'Over The Rainbow',
\r
24 telephone: '555 321 0987'
\r
26 insuranceCode: '44444',
\r
27 name: 'Fred Forsyth',
\r
28 address: 'Blimp Street',
\r
29 telephone: '555 111 2222'
\r
31 insuranceCode: '55555',
\r
32 name: 'Fred Douglass',
\r
33 address: 'Talbot County, Maryland',
\r
37 var PatientRecord = Ext.data.Record.create([{
\r
45 var patientStore = new Ext.data.Store({
\r
47 reader: new Ext.data.JsonReader({
\r
54 name: 'Saint Thomas',
\r
55 address: 'Westminster Bridge Road, SE1 7EH',
\r
56 telephone: '020 7188 7188'
\r
59 name: 'Queen\'s Medical Centre',
\r
60 address: 'Derby Road, NG7 2UH',
\r
61 telephone: '0115 924 9924'
\r
64 name: 'Saint Bartholomew',
\r
65 address: 'West Smithfield, EC1A 7BE',
\r
66 telephone: '020 7377 7000'
\r
69 name: 'Royal London',
\r
70 address: 'Whitechapel, E1 1BB',
\r
71 telephone: '020 7377 7000'
\r
74 var HospitalRecord = Ext.data.Record.create([{
\r
82 var hospitalStore = new Ext.data.Store({
\r
84 reader: new Ext.data.JsonReader({
\r
89 var patientView = new Ext.DataView({
\r
90 cls: 'patient-view',
\r
91 tpl: '<tpl for=".">' +
\r
92 '<div class="patient-source"><table><tbody>' +
\r
93 '<tr><td class="patient-label">Name</td><td class="patient-name">{name}</td></tr>' +
\r
94 '<tr><td class="patient-label">Address</td><td class="patient-name">{address}</td></tr>' +
\r
95 '<tr><td class="patient-label">Telephone</td><td class="patient-name">{telephone}</td></tr>' +
\r
96 '</tbody></table></div>' +
\r
98 itemSelector: 'div.patient-source',
\r
99 store: patientStore,
\r
101 render: initializePatientDragZone
\r
105 var helpWindow = new Ext.Window({
\r
106 title: 'Source code',
\r
109 closeAction: 'hide',
\r
110 bodyCfg: {tag: 'textarea', readonly: true},
\r
112 backgroundColor: 'white',
\r
117 render: function(w) {
\r
119 url: 'dragdropzones.js',
\r
120 success: function(r) {
\r
121 w.body.dom.value = r.responseText;
\r
128 var hospitalGrid = new Ext.grid.GridPanel({
\r
129 title: 'Hospitals',
\r
131 margins: '0 5 5 0',
\r
133 text: 'View Source',
\r
134 handler: function() {
\r
143 dataIndex: 'address',
\r
147 dataIndex: 'telephone',
\r
148 header: 'Telephone',
\r
152 tpl: new Ext.XTemplate('<div class="hospital-target"></div>'),
\r
153 enableRowBody: true,
\r
154 getRowClass: function(rec, idx, p, store) {
\r
155 p.body = this.tpl.apply(rec.data);
\r
158 store: hospitalStore,
\r
160 render: initializeHospitalDropZone
\r
170 html: '<h1>Patient Hospital Assignment</h1>',
\r
176 margins: '0 5 5 5',
\r
183 * Here is where we "activate" the DataView.
\r
184 * We have decided that each node with the class "patient-source" encapsulates a single draggable
\r
187 * So we inject code into the DragZone which, when passed a mousedown event, interrogates
\r
188 * the event to see if it was within an element with the class "patient-source". If so, we
\r
189 * return non-null drag data.
\r
191 * Returning non-null drag data indicates that the mousedown event has begun a dragging process.
\r
192 * The data must contain a property called "ddel" which is a DOM element which provides an image
\r
193 * of the data being dragged. The actual node clicked on is not dragged, a proxy element is dragged.
\r
194 * We can insert any other data into the data object, and this will be used by a cooperating DropZone
\r
195 * to perform the drop operation.
\r
197 function initializePatientDragZone(v) {
\r
198 v.dragZone = new Ext.dd.DragZone(v.getEl(), {
\r
200 // On receipt of a mousedown event, see if it is within a draggable element.
\r
201 // Return a drag data object if so. The data object can contain arbitrary application
\r
202 // data, but it should also contain a DOM element in the ddel property to provide
\r
203 // a proxy to drag.
\r
204 getDragData: function(e) {
\r
205 var sourceEl = e.getTarget(v.itemSelector, 10);
\r
207 d = sourceEl.cloneNode(true);
\r
209 return v.dragData = {
\r
210 sourceEl: sourceEl,
\r
211 repairXY: Ext.fly(sourceEl).getXY(),
\r
213 patientData: v.getRecord(sourceEl).data
\r
218 // Provide coordinates for the proxy to slide back to on failed drag.
\r
219 // This is the original XY coordinates of the draggable element.
\r
220 getRepairXY: function() {
\r
221 return this.dragData.repairXY;
\r
227 * Here is where we "activate" the GridPanel.
\r
228 * We have decided that the element with class "hospital-target" is the element which can receieve
\r
229 * drop gestures. So we inject a method "getTargetFromEvent" into the DropZone. This is constantly called
\r
230 * while the mouse is moving over the DropZone, and it returns the target DOM element if it detects that
\r
231 * the mouse if over an element which can receieve drop gestures.
\r
233 * Once the DropZone has been informed by getTargetFromEvent that it is over a target, it will then
\r
234 * call several "onNodeXXXX" methods at various points. These include:
\r
241 * We provide implementations of each of these to provide behaviour for these events.
\r
243 function initializeHospitalDropZone(g) {
\r
244 g.dropZone = new Ext.dd.DropZone(g.getView().scroller, {
\r
246 // If the mouse is over a target node, return that node. This is
\r
247 // provided as the "target" parameter in all "onNodeXXXX" node event handling functions
\r
248 getTargetFromEvent: function(e) {
\r
249 return e.getTarget('.hospital-target');
\r
252 // On entry into a target node, highlight that node.
\r
253 onNodeEnter : function(target, dd, e, data){
\r
254 Ext.fly(target).addClass('hospital-target-hover');
\r
257 // On exit from a target node, unhighlight that node.
\r
258 onNodeOut : function(target, dd, e, data){
\r
259 Ext.fly(target).removeClass('hospital-target-hover');
\r
262 // While over a target node, return the default drop allowed class which
\r
263 // places a "tick" icon into the drag proxy.
\r
264 onNodeOver : function(target, dd, e, data){
\r
265 return Ext.dd.DropZone.prototype.dropAllowed;
\r
268 // On node drop, we can interrogate the target node to find the underlying
\r
269 // application object that is the real target of the dragged data.
\r
270 // In this case, it is a Record in the GridPanel's Store.
\r
271 // We can use the data set up by the DragZone's getDragData method to read
\r
272 // any data we decided to attach.
\r
273 onNodeDrop : function(target, dd, e, data){
\r
274 var rowIndex = g.getView().findRowIndex(target);
\r
275 var h = g.getStore().getAt(rowIndex);
\r
276 Ext.Msg.alert('Drop gesture', 'Dropped patient ' + data.patientData.name +
\r
277 ' on hospital ' + h.data.name);
\r