wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
dd.js.php
Go to the documentation of this file.
1 // Code from http://yui-ext.com/forum/showthread.php?t=21913
2 //
3 Ext.namespace('Ext.ux.dd');
4 
5 // constructor parameters:
6 // grid (required): GridPanel or EditorGridPanel (with enableDragDrop set to true and optionally a value specified for ddGroup, which defaults to 'GridDD')
7 // config (optional): config object
8 // valid config params:
9 // anything accepted by DropTarget
10 // listeners: listeners object. There are 4 valid listeners, all listed below
11 // copy: boolean. Determines whether to move (false) or copy (true) the row(s) (defaults to false for move)
12 Ext.ux.dd.GridReorderDropTarget = function(grid, config) {
13  this.target = new Ext.dd.DropTarget(grid.getEl(), {
14  ddGroup: grid.ddGroup || 'GridDD'
15  ,grid: grid
16  ,gridDropTarget: this
17  ,notifyDrop: function(dd, e, data){
18  // determine the row
19  var t = Ext.lib.Event.getTarget(e);
20  var rindex = this.grid.getView().findRowIndex(t);
21  if (rindex === false) return false;
22  if (rindex == data.rowIndex) return false;
23 
24  // fire the before move/copy event
25  if (this.gridDropTarget.fireEvent(this.copy?'beforerowcopy':'beforerowmove', this.gridDropTarget, data.rowIndex, rindex, data.selections) === false) return false;
26 
27  // update the store
28  var ds = this.grid.getStore();
29  if (!this.copy) {
30  for(i = 0; i < data.selections.length; i++) {
31  ds.remove(ds.getById(data.selections[i].id));
32  }
33  }
34  ds.insert(rindex,data.selections);
35 
36  // re-select the row(s)
37  var sm = this.grid.getSelectionModel();
38  if (sm) sm.selectRecords(data.selections);
39 
40  // fire the after move/copy event
41  this.gridDropTarget.fireEvent(this.copy?'afterrowcopy':'afterrowmove', this.gridDropTarget, data.rowIndex, rindex, data.selections);
42 
43  return true;
44  }
45  ,notifyOver: function(dd, e, data) {
46  var t = Ext.lib.Event.getTarget(e);
47  var rindex = this.grid.getView().findRowIndex(t);
48  if (rindex == data.rowIndex) rindex = false;
49 
50  return (rindex === false)? this.dropNotAllowed : this.dropAllowed;
51  }
52  });
53  if (config) {
54  Ext.apply(this.target, config);
55  if (config.listeners) Ext.apply(this,{listeners: config.listeners});
56  }
57 
58  this.addEvents({
59  "beforerowmove": true
60  ,"afterrowmove": true
61  ,"beforerowcopy": true
62  ,"afterrowcopy": true
63  });
64 
65  Ext.ux.dd.GridReorderDropTarget.superclass.constructor.call(this);
66 };
67 
68 Ext.extend(Ext.ux.dd.GridReorderDropTarget, Ext.util.Observable, {
69  getTarget: function() {
70  return this.target;
71  }
72  ,getGrid: function() {
73  return this.target.grid;
74  }
75  ,getCopy: function() {
76  return this.target.copy?true:false;
77  }
78  ,setCopy: function(b) {
79  this.target.copy = b?true:false;
80  }
81 });