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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
|
// JavaScript Document
//<![CDATA[
// ==== First some helper functions ====
// Nice, handy strprintf for javascript
function jstrprintf() {
len = arguments.length;
if (len == 0) { return; }
if (len == 1) { return arguments[0]; }
var result;
var regexstr;
var replstr;
var formatstr;
var re;
result = "";
regexstr = "";
replstr = "";
formatstr = arguments[0];
for (var i=1; i<arguments.length; i++) {
replstr += String(i+100) + arguments[i] + String(i + 100);
regexstr += String(i+100) + "(.*)" + String(i+100);
}
re = new RegExp(regexstr);
var result;
result = replstr.replace(re, formatstr);
return result;
}
function AddPx(num) {
return String(num) + "px";
}
function findParentDiv(obj) {
while (obj) {
if (obj.tagName.toUpperCase() == "DIV") {
return obj;
}
if (obj.parentElement) {
obj = obj.parentElement;
}
else {
return null;
}
}
return null;
}
function findParentTagById(obj, parentname) {
while (obj) {
if (obj.id.match(parentname)) {
return obj;
}
if (obj.parentElement) {
obj = obj.parentElement;
}
else {
return null;
}
}
return null;
}
// Now for the real thing
var topZ = 1;
var startX;
var startY;
nextID = 1;
function CreateDropdownWindow( caption, theWidth, canMove, contentSource, startX, startY) {
var newdiv;
newdiv = document.createElement("div");
newdiv.id = "dragTitle" + String(nextID);
newdiv.className = "divDragTitle";
newdiv.style.width = theWidth;
newdiv.style.left = AddPx(startX);
newdiv.style.top = AddPx(startY);
newdiv.style.zIndex = topZ;
newdiv.innerHTML = jstrprintf(
'<table><tr><td>$1</td>' +
'<td style="text-align:right">' +
'<img src="/static/buttontop.gif" class="divTitleButton" id="dragButton$2" ' +
'onmousedown="javascript:toggleContentWin($2)" /></td>' +
'</tr></table>',
caption, nextID);
// If canMove is false, don't register event handlers
if (canMove) {
// IE doesn't support addEventListener, so check for its presence
if (newdiv.addEventListener) {
// firefox, etc.
newdiv.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
newdiv.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
newdiv.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
}
else {
// IE
newdiv.attachEvent("onmousemove", function(e) { return mouseMove(e) });
newdiv.attachEvent("onmousedown", function(e) { return mouseDown(e) });
newdiv.attachEvent("onmouseup", function(e) { return mouseUp(e) });
}
}
document.body.appendChild(newdiv);
var newdiv2;
newdiv2 = document.createElement("div");
newdiv2.id = "dragContent" + String(nextID);
newdiv2.className = "divDragContent";
newdiv2.style.width = theWidth;
newdiv2.style.left = AddPx(startX);
newdiv2.style.top = AddPx(startY + 20);
newdiv2.style.zIndex = topZ;
if (contentSource) {
newdiv2.innerHTML = document.getElementById(contentSource).innerHTML;
}
if (canMove) {
if (newdiv2.addEventListener) {
// firefox, etc.
newdiv2.addEventListener("mousedown", function(e) { return contentMouseDown(e) }, true);
}
else {
// IE
newdiv2.attachEvent("onmousedown", function(e) { return contentMouseDown(e) });
}
}
document.body.appendChild(newdiv2);
// Save away the content DIV into the title DIV for
// later access, and vice versa
newdiv.content = newdiv2;
newdiv2.titlediv = newdiv;
topZ += 1;
startX += 20;
startY += 20;
// If you want you can check when these two are greater than
// a certain number and then rotate them back to 100,100...
nextID++;
}
function toggleContentWin(id) {
var elem = document.getElementById("dragContent" + String(id));
var img = document.getElementById("dragButton" + String(id));
if (elem.style.display == "none") {
// hidden, so unhide
elem.style.display = "block";
// Change the button's image
img.src = "/static/buttontop.gif";
}
else {
// showing, so hide
elem.style.display = "none";
// Change the button's image
img.src = "/static/buttonbottom.gif";
}
}
// Drag methods
var dragObjTitle = null;
var dragOffsetX = 0;
var dragOffsetY = 0;
function contentMouseDown(e) {
// Move the window to the front
// Use a handy trick for IE vs FF
var dragContent = e.srcElement || e.currentTarget;
if ( ! dragContent.id.match("dragContent")) {
dragContent = findParentTagById(dragContent, "dragContent");
}
if (dragContent) {
dragContent.style.zIndex = topZ;
dragContent.titlediv.style.zIndex = topZ;
topZ++;
}
}
function mouseDown(e) {
// These first two lines are written to handle both FF and IE
var curElem = e.srcElement || e.target;
var dragTitle = e.currentTarget || findParentDiv(curElem);
if (dragTitle) {
if (dragTitle.className != 'divDragTitle') {
return;
}
}
// Start the drag, but first make sure neither is null
if (curElem && dragTitle) {
// Attach the document handlers. We don't want these running all the time.
addDocumentHandlers(true);
// Move this window to the front.
dragTitle.style.zIndex = topZ;
dragTitle.content.style.zIndex = topZ;
topZ++;
// Check if it's the button. If so, don't drag.
if (curElem.className != "divTitleButton") {
// Save away the two objects
dragObjTitle = dragTitle;
// Calculate the offset
dragOffsetX = e.clientX -
dragTitle.offsetLeft;
dragOffsetY = e.clientY -
dragTitle.offsetTop;
// Don't let the default actions take place
if (e.preventDefault) {
e.preventDefault();
}
else {
document.onselectstart = function () { return false; };
e.cancelBubble = true;
return false;
}
}
}
}
function mouseMove(e) {
// If not null, then we're in a drag
if (dragObjTitle) {
if (!e.preventDefault) {
// This is the IE version for handling a strange
// problem when you quickly move the mouse
// out of the window and let go of the button.
if (e.button == 0) {
finishDrag(e);
return;
}
}
dragObjTitle.style.left = AddPx(e.clientX - dragOffsetX);
dragObjTitle.style.top = AddPx(e.clientY - dragOffsetY);
dragObjTitle.content.style.left = AddPx(e.clientX - dragOffsetX);
dragObjTitle.content.style.top = AddPx(e.clientY - dragOffsetY + 20);
if (e.preventDefault) {
e.preventDefault();
}
else {
e.cancelBubble = true;
return false;
}
}
}
function mouseUp(e) {
if (dragObjTitle) {
finishDrag(e);
}
}
function finishDrag(e) {
var finalX = e.clientX - dragOffsetX;
var finalY = e.clientY - dragOffsetY;
if (finalX < 0) { finalX = 0 };
if (finalY < 0) { finalY = 0 };
dragObjTitle.style.left = AddPx(finalX);
dragObjTitle.style.top = AddPx(finalY);
dragObjTitle.content.style.left = AddPx(finalX);
dragObjTitle.content.style.top = AddPx(finalY + 20);
// Done, so reset to null
dragObjTitle = null;
addDocumentHandlers(false);
if (e.preventDefault) {
e.preventDefault();
}
else {
document.onselectstart = null;
e.cancelBubble = true;
return false;
}
}
function addDocumentHandlers(addOrRemove) {
if (addOrRemove) {
if (document.body.addEventListener) {
// firefox, etc.
document.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
document.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
document.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
}
else {
// IE
document.onmousedown = function() { mouseDown(window.event) } ;
document.onmousemove = function() { mouseMove(window.event) } ;
document.onmouseup = function() { mouseUp(window.event) } ;
}
}
else {
if (document.body.addEventListener) {
// firefox, etc.
remove.addEventListener("mousedown", function(e) { return mouseDown(e) }, true);
remove.addEventListener("mousemove", function(e) { return mouseMove(e) }, true);
remove.addEventListener("mouseup", function(e) { return mouseUp(e) }, true);
}
else {
// IE
// Be careful here. If you have other code that sets these events,
// you'll want this code here to restore the values to your other handlers,
// rather than just clear them out.
document.onmousedown = null;
document.onmousemove = null;
document.onmouseup = null;
}
}
}
//]]>
|