2 * $Id: mcwindows.js 18 2006-06-29 14:11:23Z spocke $
4 * Moxiecode DHTML Windows script.
7 * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
11 function MCWindows() {
12 this.settings = new Array();
13 this.windows = new Array();
14 this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
15 this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
16 this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
17 this.isMac = navigator.userAgent.indexOf('Mac') != -1;
18 this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
20 this.selectedWindow = null;
22 this.mouseDownScreenX = 0;
23 this.mouseDownScreenY = 0;
24 this.mouseDownLayerX = 0;
25 this.mouseDownLayerY = 0;
26 this.mouseDownWidth = 0;
27 this.mouseDownHeight = 0;
30 MCWindows.prototype.init = function(settings) {
31 this.settings = settings;
34 this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
36 this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
38 this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
41 MCWindows.prototype.getParam = function(name, default_value) {
44 value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
47 if (value == "true" || value == "false")
48 return (value == "true");
53 MCWindows.prototype.eventDispatcher = function(e) {
54 e = typeof(e) == "undefined" ? window.event : e;
56 if (mcWindows.selectedWindow == null)
60 if (mcWindows.isGecko && e.type == "mousedown") {
61 var elm = e.currentTarget;
63 for (var n in mcWindows.windows) {
64 var win = mcWindows.windows[n];
65 if (typeof(win) == 'function')
68 if (win.headElement == elm || win.resizeElement == elm) {
77 mcWindows.selectedWindow.onMouseMove(e);
81 mcWindows.selectedWindow.onMouseUp(e);
85 mcWindows.selectedWindow.onMouseDown(e);
89 mcWindows.selectedWindow.onFocus(e);
94 MCWindows.prototype.addEvent = function(obj, name, handler) {
96 obj.attachEvent("on" + name, handler);
98 obj.addEventListener(name, handler, true);
101 MCWindows.prototype.cancelEvent = function(e) {
103 e.returnValue = false;
104 e.cancelBubble = true;
109 MCWindows.prototype.parseFeatures = function(opts) {
110 // Cleanup the options
111 opts = opts.toLowerCase();
112 opts = opts.replace(/;/g, ",");
113 opts = opts.replace(/[^0-9a-z=,]/g, "");
115 var optionChunks = opts.split(',');
116 var options = new Array();
118 options['left'] = 10;
120 options['width'] = 300;
121 options['height'] = 300;
122 options['resizable'] = true;
123 options['minimizable'] = true;
124 options['maximizable'] = true;
125 options['close'] = true;
126 options['movable'] = true;
131 for (var i=0; i<optionChunks.length; i++) {
132 var parts = optionChunks[i].split('=');
134 if (parts.length == 2)
135 options[parts[0]] = parts[1];
141 MCWindows.prototype.open = function(url, name, features) {
142 var win = new MCWindow();
143 var winDiv, html = "", id;
145 features = this.parseFeatures(features);
148 id = "mcWindow_" + name;
150 width = parseInt(features['width']);
151 height = parseInt(features['height'])-12-19;
156 // Setup first part of window
160 win.features = features;
161 this.windows[name] = win;
164 iframeHeight = height;
166 // Create inner content
167 html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
170 html += '<title>Wrapper iframe</title>';
171 html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
172 html += '<link href="../jscripts/tiny_mce/themes/advanced/css/editor_ui.css" rel="stylesheet" type="text/css" />';
174 html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
176 html += '<div id="' + id + '_container" class="mceWindow">';
177 html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
178 html += ' <div id="' + id + '_title" class="mceWindowTitle"';
179 html += ' onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;">No name window</div>';
180 html += ' <div class="mceWindowHeadTools">';
181 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>';
182 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].maximize();" onmousedown="return false;" class="mceWindowMaximize"></a>';
183 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].minimize();" onmousedown="return false;" class="mceWindowMinimize"></a>';
185 html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
186 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>';
187 html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
188 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>';
196 this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
199 // Gets called when wrapper iframe is initialized
200 MCWindows.prototype.onLoad = function(name) {
201 var win = mcWindows.windows[name];
202 var id = "mcWindow_" + name;
203 var wrapperIframe = window.frames[id + "_iframe"].frames[0];
204 var wrapperDoc = window.frames[id + "_iframe"].document;
205 var doc = window.frames[id + "_iframe"].document;
206 var winDiv = document.getElementById("mcWindow_" + name + "_div");
207 var realIframe = window.frames[id + "_iframe"].frames[0];
210 win.id = "mcWindow_" + name + "_iframe";
211 win.winElement = winDiv;
212 win.bodyElement = doc.getElementById(id + '_body');
213 win.iframeElement = doc.getElementById(id + '_iframe');
214 win.headElement = doc.getElementById(id + '_head');
215 win.titleElement = doc.getElementById(id + '_title');
216 win.resizeElement = doc.getElementById(id + '_resize');
217 win.containerElement = doc.getElementById(id + '_container');
218 win.left = win.features['left'];
219 win.top = win.features['top'];
220 win.frame = window.frames[id + '_iframe'].frames[0];
221 win.wrapperFrame = window.frames[id + '_iframe'];
222 win.wrapperIFrameElement = document.getElementById(id + "_iframe");
224 // Add event handlers
225 mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
226 mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
228 if (mcWindows.isMSIE) {
229 mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
230 mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
232 mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
233 mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
234 mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
237 for (var i=0; i<window.frames.length; i++) {
238 if (!window.frames[i]._hasMouseHandlers) {
239 if (mcWindows.isMSIE) {
240 mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
241 mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
243 mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
244 mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
247 window.frames[i]._hasMouseHandlers = true;
251 if (mcWindows.isMSIE) {
252 mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
253 mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
255 mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
256 mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
257 mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
260 this.selectedWindow = win;
263 MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
264 var iframe = document.createElement("iframe");
265 var div = document.createElement("div");
267 width = parseInt(width);
268 height = parseInt(height)+1;
270 // Create wrapper div
271 div.setAttribute("id", id_prefix + "_div");
272 div.setAttribute("width", width);
273 div.setAttribute("height", (height));
274 div.style.position = "absolute";
275 div.style.left = left + "px";
276 div.style.top = top + "px";
277 div.style.width = width + "px";
278 div.style.height = (height) + "px";
279 div.style.backgroundColor = "white";
280 div.style.display = "none";
283 iframeWidth = width + 2;
284 iframeHeight = height + 2;
287 iframeHeight = height + 1;
291 iframe.setAttribute("id", id_prefix + "_iframe");
292 iframe.setAttribute("name", id_prefix + "_iframe");
293 iframe.setAttribute("border", "0");
294 iframe.setAttribute("frameBorder", "0");
295 iframe.setAttribute("marginWidth", "0");
296 iframe.setAttribute("marginHeight", "0");
297 iframe.setAttribute("leftMargin", "0");
298 iframe.setAttribute("topMargin", "0");
299 iframe.setAttribute("width", iframeWidth);
300 iframe.setAttribute("height", iframeHeight);
301 // iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
302 // iframe.setAttribute("allowtransparency", "false");
303 iframe.setAttribute("scrolling", "no");
304 iframe.style.width = iframeWidth + "px";
305 iframe.style.height = iframeHeight + "px";
306 iframe.style.backgroundColor = "white";
307 div.appendChild(iframe);
309 document.body.appendChild(div);
311 // Fixed MSIE 5.0 issue
312 div.innerHTML = div.innerHTML;
315 // Give Safari some time to setup
316 window.setTimeout(function() {
317 doc = window.frames[id_prefix + '_iframe'].document;
323 doc = window.frames[id_prefix + '_iframe'].window.document
329 div.style.display = "block";
335 function MCWindow() {
338 MCWindow.prototype.focus = function() {
339 this.winElement.style.zIndex = mcWindows.zindex++;
340 mcWindows.selectedWindow = this;
343 MCWindow.prototype.minimize = function() {
346 MCWindow.prototype.maximize = function() {
350 MCWindow.prototype.startResize = function() {
351 mcWindows.action = "resize";
354 MCWindow.prototype.startMove = function(e) {
355 mcWindows.action = "move";
358 MCWindow.prototype.close = function() {
359 document.body.removeChild(this.winElement);
360 mcWindows.windows[this.name] = null;
363 MCWindow.prototype.onMouseMove = function(e) {
364 var scrollX = 0;//this.doc.body.scrollLeft;
365 var scrollY = 0;//this.doc.body.scrollTop;
367 // Calculate real X, Y
368 var dx = e.screenX - mcWindows.mouseDownScreenX;
369 var dy = e.screenY - mcWindows.mouseDownScreenY;
371 switch (mcWindows.action) {
373 width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
374 height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
376 width = width < 100 ? 100 : width;
377 height = height < 100 ? 100 : height;
379 this.wrapperIFrameElement.style.width = width+2;
380 this.wrapperIFrameElement.style.height = height+2;
381 this.wrapperIFrameElement.width = width+2;
382 this.wrapperIFrameElement.height = height+2;
383 this.winElement.style.width = width;
384 this.winElement.style.height = height;
386 height = height-12-19;
388 this.containerElement.style.width = width;
390 this.iframeElement.style.width = width;
391 this.iframeElement.style.height = height;
392 this.bodyElement.style.width = width;
393 this.bodyElement.style.height = height;
394 this.headElement.style.width = width;
395 //this.statusElement.style.width = width;
397 mcWindows.cancelEvent(e);
401 this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
402 this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
403 this.winElement.style.left = this.left + "px";
404 this.winElement.style.top = this.top + "px";
406 mcWindows.cancelEvent(e);
411 MCWindow.prototype.onMouseUp = function(e) {
412 mcWindows.action = "none";
415 MCWindow.prototype.onFocus = function(e) {
416 // Gecko only handler
417 var winRef = e.currentTarget;
419 for (var n in mcWindows.windows) {
420 var win = mcWindows.windows[n];
421 if (typeof(win) == 'function')
424 if (winRef.name == win.id) {
431 MCWindow.prototype.onMouseDown = function(e) {
432 var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
434 var scrollX = 0;//this.doc.body.scrollLeft;
435 var scrollY = 0;//this.doc.body.scrollTop;
437 mcWindows.mouseDownScreenX = e.screenX;
438 mcWindows.mouseDownScreenY = e.screenY;
439 mcWindows.mouseDownLayerX = this.left;
440 mcWindows.mouseDownLayerY = this.top;
441 mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
442 mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
444 if (elm == this.resizeElement.firstChild)
449 mcWindows.cancelEvent(e);
453 var mcWindows = new MCWindows();