PortalDropZone.js
5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**
* @class Ext.app.PortalDropZone
* @extends Ext.dd.DropTarget
* Internal class that manages drag/drop for {@link Ext.app.PortalPanel}.
*/
Ext.define('Ext.app.PortalDropZone', {
extend: 'Ext.dd.DropTarget',
constructor: function(portal, cfg) {
this.portal = portal;
Ext.dd.ScrollManager.register(portal.body);
Ext.app.PortalDropZone.superclass.constructor.call(this, portal.body, cfg);
portal.body.ddScrollConfig = this.ddScrollConfig;
},
ddScrollConfig: {
vthresh: 50,
hthresh: -1,
animate: true,
increment: 200
},
createEvent: function(dd, e, data, col, c, pos) {
return {
portal: this.portal,
panel: data.panel,
columnIndex: col,
column: c,
position: pos,
data: data,
source: dd,
rawEvent: e,
status: this.dropAllowed
};
},
notifyOver: function(dd, e, data) {
var xy = e.getXY(),
portal = this.portal,
proxy = dd.proxy;
// case column widths
if (!this.grid) {
this.grid = this.getGrid();
}
// handle case scroll where scrollbars appear during drag
var cw = portal.body.dom.clientWidth;
if (!this.lastCW) {
// set initial client width
this.lastCW = cw;
} else if (this.lastCW != cw) {
// client width has changed, so refresh layout & grid calcs
this.lastCW = cw;
//portal.doLayout();
this.grid = this.getGrid();
}
// determine column
var colIndex = 0,
colRight = 0,
cols = this.grid.columnX,
len = cols.length,
cmatch = false;
for (len; colIndex < len; colIndex++) {
colRight = cols[colIndex].x + cols[colIndex].w;
if (xy[0] < colRight) {
cmatch = true;
break;
}
}
// no match, fix last index
if (!cmatch) {
colIndex--;
}
// find insert position
var overPortlet, pos = 0,
h = 0,
match = false,
overColumn = portal.items.getAt(colIndex),
portlets = overColumn.items.items,
overSelf = false;
len = portlets.length;
for (len; pos < len; pos++) {
overPortlet = portlets[pos];
h = overPortlet.el.getHeight();
if (h === 0) {
overSelf = true;
} else if ((overPortlet.el.getY() + (h / 2)) > xy[1]) {
match = true;
break;
}
}
pos = (match && overPortlet ? pos : overColumn.items.getCount()) + (overSelf ? -1 : 0);
var overEvent = this.createEvent(dd, e, data, colIndex, overColumn, pos);
if (portal.fireEvent('validatedrop', overEvent) !== false && portal.fireEvent('beforedragover', overEvent) !== false) {
// make sure proxy width is fluid in different width columns
proxy.getProxy().setWidth('auto');
if (overPortlet) {
dd.panelProxy.moveProxy(overPortlet.el.dom.parentNode, match ? overPortlet.el.dom : null);
} else {
dd.panelProxy.moveProxy(overColumn.el.dom, null);
}
this.lastPos = {
c: overColumn,
col: colIndex,
p: overSelf || (match && overPortlet) ? pos : false
};
this.scrollPos = portal.body.getScroll();
portal.fireEvent('dragover', overEvent);
return overEvent.status;
} else {
return overEvent.status;
}
},
notifyOut: function() {
delete this.grid;
},
notifyDrop: function(dd, e, data) {
delete this.grid;
if (!this.lastPos) {
return;
}
var c = this.lastPos.c,
col = this.lastPos.col,
pos = this.lastPos.p,
panel = dd.panel,
dropEvent = this.createEvent(dd, e, data, col, c, pos !== false ? pos : c.items.getCount());
if (this.portal.fireEvent('validatedrop', dropEvent) !== false &&
this.portal.fireEvent('beforedrop', dropEvent) !== false) {
Ext.suspendLayouts();
// make sure panel is visible prior to inserting so that the layout doesn't ignore it
panel.el.dom.style.display = '';
dd.panelProxy.hide();
dd.proxy.hide();
if (pos !== false) {
c.insert(pos, panel);
} else {
c.add(panel);
}
Ext.resumeLayouts(true);
this.portal.fireEvent('drop', dropEvent);
// scroll position is lost on drop, fix it
var st = this.scrollPos.top;
if (st) {
var d = this.portal.body.dom;
setTimeout(function() {
d.scrollTop = st;
},
10);
}
}
delete this.lastPos;
return true;
},
// internal cache of body and column coords
getGrid: function() {
var box = this.portal.body.getBox();
box.columnX = [];
this.portal.items.each(function(c) {
box.columnX.push({
x: c.el.getX(),
w: c.el.getWidth()
});
});
return box;
},
// unregister the dropzone from ScrollManager
unreg: function() {
Ext.dd.ScrollManager.unregister(this.portal.body);
Ext.app.PortalDropZone.superclass.unreg.call(this);
}
});