mcwindows.js
13.7 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/**
* $Id: mcwindows.js 18 2006-06-29 14:11:23Z spocke $
*
* Moxiecode DHTML Windows script.
*
* @author Moxiecode
* @copyright Copyright 2004, Moxiecode Systems AB, All rights reserved.
*/
// Windows handler
function MCWindows() {
this.settings = new Array();
this.windows = new Array();
this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
this.isMac = navigator.userAgent.indexOf('Mac') != -1;
this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
this.action = "none";
this.selectedWindow = null;
this.zindex = 100;
this.mouseDownScreenX = 0;
this.mouseDownScreenY = 0;
this.mouseDownLayerX = 0;
this.mouseDownLayerY = 0;
this.mouseDownWidth = 0;
this.mouseDownHeight = 0;
};
MCWindows.prototype.init = function(settings) {
this.settings = settings;
if (this.isMSIE)
this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
else
this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
};
MCWindows.prototype.getParam = function(name, default_value) {
var value = null;
value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
// Fix bool values
if (value == "true" || value == "false")
return (value == "true");
return value;
};
MCWindows.prototype.eventDispatcher = function(e) {
e = typeof(e) == "undefined" ? window.event : e;
if (mcWindows.selectedWindow == null)
return;
// Switch focus
if (mcWindows.isGecko && e.type == "mousedown") {
var elm = e.currentTarget;
for (var n in mcWindows.windows) {
var win = mcWindows.windows[n];
if (typeof(win) == 'function')
continue;
if (win.headElement == elm || win.resizeElement == elm) {
win.focus();
break;
}
}
}
switch (e.type) {
case "mousemove":
mcWindows.selectedWindow.onMouseMove(e);
break;
case "mouseup":
mcWindows.selectedWindow.onMouseUp(e);
break;
case "mousedown":
mcWindows.selectedWindow.onMouseDown(e);
break;
case "focus":
mcWindows.selectedWindow.onFocus(e);
break;
}
}
MCWindows.prototype.addEvent = function(obj, name, handler) {
if (this.isMSIE)
obj.attachEvent("on" + name, handler);
else
obj.addEventListener(name, handler, true);
};
MCWindows.prototype.cancelEvent = function(e) {
if (this.isMSIE) {
e.returnValue = false;
e.cancelBubble = true;
} else
e.preventDefault();
};
MCWindows.prototype.parseFeatures = function(opts) {
// Cleanup the options
opts = opts.toLowerCase();
opts = opts.replace(/;/g, ",");
opts = opts.replace(/[^0-9a-z=,]/g, "");
var optionChunks = opts.split(',');
var options = new Array();
options['left'] = 10;
options['top'] = 10;
options['width'] = 300;
options['height'] = 300;
options['resizable'] = true;
options['minimizable'] = true;
options['maximizable'] = true;
options['close'] = true;
options['movable'] = true;
if (opts == "")
return options;
for (var i=0; i<optionChunks.length; i++) {
var parts = optionChunks[i].split('=');
if (parts.length == 2)
options[parts[0]] = parts[1];
}
return options;
};
MCWindows.prototype.open = function(url, name, features) {
var win = new MCWindow();
var winDiv, html = "", id;
features = this.parseFeatures(features);
// Create div
id = "mcWindow_" + name;
width = parseInt(features['width']);
height = parseInt(features['height'])-12-19;
if (this.isMSIE)
width -= 2;
// Setup first part of window
win.id = id;
win.url = url;
win.name = name;
win.features = features;
this.windows[name] = win;
iframeWidth = width;
iframeHeight = height;
// Create inner content
html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
html += '<html>';
html += '<head>';
html += '<title>Wrapper iframe</title>';
html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
html += '<link href="../jscripts/tiny_mce/themes/advanced/css/editor_ui.css" rel="stylesheet" type="text/css" />';
html += '</head>';
html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
html += '<div id="' + id + '_container" class="mceWindow">';
html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
html += ' <div id="' + id + '_title" class="mceWindowTitle"';
html += ' onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;">No name window</div>';
html += ' <div class="mceWindowHeadTools">';
html += ' <a href="javascript:parent.mcWindows.windows[\'' + name + '\'].close();" onmousedown="return false;" class="mceWindowClose"><img border="0" src="../jscripts/tiny_mce/themes/advanced/images/window_close.gif" /></a>';
// html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].maximize();" onmousedown="return false;" class="mceWindowMaximize"></a>';
// html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].minimize();" onmousedown="return false;" class="mceWindowMinimize"></a>';
html += ' </div>';
html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
html += '<iframe id="' + id + '_iframe" name="' + id + '_iframe" onfocus="parent.mcWindows.windows[\'' + name + '\'].focus();" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe"></iframe></div>';
html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
html += '<div id="' + id + '_resize" class="mceWindowResize"><img onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();" border="0" src="../jscripts/tiny_mce/themes/advanced/images/window_resize.gif" /></div>';
html += '</div>';
html += '</div>';
html += '</body>';
html += '</html>';
// Create iframe
this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
};
// Gets called when wrapper iframe is initialized
MCWindows.prototype.onLoad = function(name) {
var win = mcWindows.windows[name];
var id = "mcWindow_" + name;
var wrapperIframe = window.frames[id + "_iframe"].frames[0];
var wrapperDoc = window.frames[id + "_iframe"].document;
var doc = window.frames[id + "_iframe"].document;
var winDiv = document.getElementById("mcWindow_" + name + "_div");
var realIframe = window.frames[id + "_iframe"].frames[0];
// Set window data
win.id = "mcWindow_" + name + "_iframe";
win.winElement = winDiv;
win.bodyElement = doc.getElementById(id + '_body');
win.iframeElement = doc.getElementById(id + '_iframe');
win.headElement = doc.getElementById(id + '_head');
win.titleElement = doc.getElementById(id + '_title');
win.resizeElement = doc.getElementById(id + '_resize');
win.containerElement = doc.getElementById(id + '_container');
win.left = win.features['left'];
win.top = win.features['top'];
win.frame = window.frames[id + '_iframe'].frames[0];
win.wrapperFrame = window.frames[id + '_iframe'];
win.wrapperIFrameElement = document.getElementById(id + "_iframe");
// Add event handlers
mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
if (mcWindows.isMSIE) {
mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
} else {
mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
}
for (var i=0; i<window.frames.length; i++) {
if (!window.frames[i]._hasMouseHandlers) {
if (mcWindows.isMSIE) {
mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
} else {
mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
}
window.frames[i]._hasMouseHandlers = true;
}
}
if (mcWindows.isMSIE) {
mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
} else {
mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
}
this.selectedWindow = win;
};
MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
var iframe = document.createElement("iframe");
var div = document.createElement("div");
width = parseInt(width);
height = parseInt(height)+1;
// Create wrapper div
div.setAttribute("id", id_prefix + "_div");
div.setAttribute("width", width);
div.setAttribute("height", (height));
div.style.position = "absolute";
div.style.left = left + "px";
div.style.top = top + "px";
div.style.width = width + "px";
div.style.height = (height) + "px";
div.style.backgroundColor = "white";
div.style.display = "none";
if (this.isGecko) {
iframeWidth = width + 2;
iframeHeight = height + 2;
} else {
iframeWidth = width;
iframeHeight = height + 1;
}
// Create iframe
iframe.setAttribute("id", id_prefix + "_iframe");
iframe.setAttribute("name", id_prefix + "_iframe");
iframe.setAttribute("border", "0");
iframe.setAttribute("frameBorder", "0");
iframe.setAttribute("marginWidth", "0");
iframe.setAttribute("marginHeight", "0");
iframe.setAttribute("leftMargin", "0");
iframe.setAttribute("topMargin", "0");
iframe.setAttribute("width", iframeWidth);
iframe.setAttribute("height", iframeHeight);
// iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
// iframe.setAttribute("allowtransparency", "false");
iframe.setAttribute("scrolling", "no");
iframe.style.width = iframeWidth + "px";
iframe.style.height = iframeHeight + "px";
iframe.style.backgroundColor = "white";
div.appendChild(iframe);
document.body.appendChild(div);
// Fixed MSIE 5.0 issue
div.innerHTML = div.innerHTML;
if (this.isSafari) {
// Give Safari some time to setup
window.setTimeout(function() {
doc = window.frames[id_prefix + '_iframe'].document;
doc.open();
doc.write(html);
doc.close();
}, 10);
} else {
doc = window.frames[id_prefix + '_iframe'].window.document
doc.open();
doc.write(html);
doc.close();
}
div.style.display = "block";
return div;
};
// Window instance
function MCWindow() {
};
MCWindow.prototype.focus = function() {
this.winElement.style.zIndex = mcWindows.zindex++;
mcWindows.selectedWindow = this;
};
MCWindow.prototype.minimize = function() {
};
MCWindow.prototype.maximize = function() {
};
MCWindow.prototype.startResize = function() {
mcWindows.action = "resize";
};
MCWindow.prototype.startMove = function(e) {
mcWindows.action = "move";
};
MCWindow.prototype.close = function() {
document.body.removeChild(this.winElement);
mcWindows.windows[this.name] = null;
};
MCWindow.prototype.onMouseMove = function(e) {
var scrollX = 0;//this.doc.body.scrollLeft;
var scrollY = 0;//this.doc.body.scrollTop;
// Calculate real X, Y
var dx = e.screenX - mcWindows.mouseDownScreenX;
var dy = e.screenY - mcWindows.mouseDownScreenY;
switch (mcWindows.action) {
case "resize":
width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
width = width < 100 ? 100 : width;
height = height < 100 ? 100 : height;
this.wrapperIFrameElement.style.width = width+2;
this.wrapperIFrameElement.style.height = height+2;
this.wrapperIFrameElement.width = width+2;
this.wrapperIFrameElement.height = height+2;
this.winElement.style.width = width;
this.winElement.style.height = height;
height = height-12-19;
this.containerElement.style.width = width;
this.iframeElement.style.width = width;
this.iframeElement.style.height = height;
this.bodyElement.style.width = width;
this.bodyElement.style.height = height;
this.headElement.style.width = width;
//this.statusElement.style.width = width;
mcWindows.cancelEvent(e);
break;
case "move":
this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
this.winElement.style.left = this.left + "px";
this.winElement.style.top = this.top + "px";
mcWindows.cancelEvent(e);
break;
}
};
MCWindow.prototype.onMouseUp = function(e) {
mcWindows.action = "none";
};
MCWindow.prototype.onFocus = function(e) {
// Gecko only handler
var winRef = e.currentTarget;
for (var n in mcWindows.windows) {
var win = mcWindows.windows[n];
if (typeof(win) == 'function')
continue;
if (winRef.name == win.id) {
win.focus();
return;
}
}
};
MCWindow.prototype.onMouseDown = function(e) {
var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
var scrollX = 0;//this.doc.body.scrollLeft;
var scrollY = 0;//this.doc.body.scrollTop;
mcWindows.mouseDownScreenX = e.screenX;
mcWindows.mouseDownScreenY = e.screenY;
mcWindows.mouseDownLayerX = this.left;
mcWindows.mouseDownLayerY = this.top;
mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
if (elm == this.resizeElement.firstChild)
this.startResize(e);
else
this.startMove(e);
mcWindows.cancelEvent(e);
};
// Global instance
var mcWindows = new MCWindows();