2 * $RCSfile: mcwindows.js,v $
\r
4 * $Date: 2005/10/18 13:59:43 $
\r
6 * Moxiecode DHTML Windows script.
\r
9 * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
\r
13 function MCWindows() {
\r
14 this.settings = new Array();
\r
15 this.windows = new Array();
\r
16 this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
\r
17 this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
\r
18 this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
\r
19 this.isMac = navigator.userAgent.indexOf('Mac') != -1;
\r
20 this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
\r
21 this.action = "none";
\r
22 this.selectedWindow = null;
\r
24 this.mouseDownScreenX = 0;
\r
25 this.mouseDownScreenY = 0;
\r
26 this.mouseDownLayerX = 0;
\r
27 this.mouseDownLayerY = 0;
\r
28 this.mouseDownWidth = 0;
\r
29 this.mouseDownHeight = 0;
\r
32 MCWindows.prototype.init = function(settings) {
\r
33 this.settings = settings;
\r
36 this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
\r
38 this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
\r
40 this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
\r
43 MCWindows.prototype.getParam = function(name, default_value) {
\r
46 value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
\r
49 if (value == "true" || value == "false")
\r
50 return (value == "true");
\r
55 MCWindows.prototype.eventDispatcher = function(e) {
\r
56 e = typeof(e) == "undefined" ? window.event : e;
\r
58 if (mcWindows.selectedWindow == null)
\r
62 if (mcWindows.isGecko && e.type == "mousedown") {
\r
63 var elm = e.currentTarget;
\r
65 for (var n in mcWindows.windows) {
\r
66 var win = mcWindows.windows[n];
\r
67 if (typeof(win) == 'function')
\r
70 if (win.headElement == elm || win.resizeElement == elm) {
\r
79 mcWindows.selectedWindow.onMouseMove(e);
\r
83 mcWindows.selectedWindow.onMouseUp(e);
\r
87 mcWindows.selectedWindow.onMouseDown(e);
\r
91 mcWindows.selectedWindow.onFocus(e);
\r
96 MCWindows.prototype.addEvent = function(obj, name, handler) {
\r
98 obj.attachEvent("on" + name, handler);
\r
100 obj.addEventListener(name, handler, true);
\r
103 MCWindows.prototype.cancelEvent = function(e) {
\r
105 e.returnValue = false;
\r
106 e.cancelBubble = true;
\r
108 e.preventDefault();
\r
111 MCWindows.prototype.parseFeatures = function(opts) {
\r
112 // Cleanup the options
\r
113 opts = opts.toLowerCase();
\r
114 opts = opts.replace(/;/g, ",");
\r
115 opts = opts.replace(/[^0-9a-z=,]/g, "");
\r
117 var optionChunks = opts.split(',');
\r
118 var options = new Array();
\r
120 options['left'] = 10;
\r
121 options['top'] = 10;
\r
122 options['width'] = 300;
\r
123 options['height'] = 300;
\r
124 options['resizable'] = true;
\r
125 options['minimizable'] = true;
\r
126 options['maximizable'] = true;
\r
127 options['close'] = true;
\r
128 options['movable'] = true;
\r
133 for (var i=0; i<optionChunks.length; i++) {
\r
134 var parts = optionChunks[i].split('=');
\r
136 if (parts.length == 2)
\r
137 options[parts[0]] = parts[1];
\r
143 MCWindows.prototype.open = function(url, name, features) {
\r
144 var win = new MCWindow();
\r
145 var winDiv, html = "", id;
\r
147 features = this.parseFeatures(features);
\r
150 id = "mcWindow_" + name;
\r
152 width = parseInt(features['width']);
\r
153 height = parseInt(features['height'])-12-19;
\r
158 // Setup first part of window
\r
162 win.features = features;
\r
163 this.windows[name] = win;
\r
165 iframeWidth = width;
\r
166 iframeHeight = height;
\r
168 // Create inner content
\r
169 html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
\r
172 html += '<title>Wrapper iframe</title>';
\r
173 html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
\r
174 html += '<link href="../jscripts/tiny_mce/themes/advanced/css/editor_ui.css" rel="stylesheet" type="text/css" />';
\r
176 html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
\r
178 html += '<div id="' + id + '_container" class="mceWindow">';
\r
179 html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
\r
180 html += ' <div id="' + id + '_title" class="mceWindowTitle"';
\r
181 html += ' onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;">No name window</div>';
\r
182 html += ' <div class="mceWindowHeadTools">';
\r
183 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>';
\r
184 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].maximize();" onmousedown="return false;" class="mceWindowMaximize"></a>';
\r
185 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].minimize();" onmousedown="return false;" class="mceWindowMinimize"></a>';
\r
187 html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
\r
188 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>';
\r
189 html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
\r
190 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>';
\r
198 this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
\r
201 // Gets called when wrapper iframe is initialized
\r
202 MCWindows.prototype.onLoad = function(name) {
\r
203 var win = mcWindows.windows[name];
\r
204 var id = "mcWindow_" + name;
\r
205 var wrapperIframe = window.frames[id + "_iframe"].frames[0];
\r
206 var wrapperDoc = window.frames[id + "_iframe"].document;
\r
207 var doc = window.frames[id + "_iframe"].document;
\r
208 var winDiv = document.getElementById("mcWindow_" + name + "_div");
\r
209 var realIframe = window.frames[id + "_iframe"].frames[0];
\r
212 win.id = "mcWindow_" + name + "_iframe";
\r
213 win.winElement = winDiv;
\r
214 win.bodyElement = doc.getElementById(id + '_body');
\r
215 win.iframeElement = doc.getElementById(id + '_iframe');
\r
216 win.headElement = doc.getElementById(id + '_head');
\r
217 win.titleElement = doc.getElementById(id + '_title');
\r
218 win.resizeElement = doc.getElementById(id + '_resize');
\r
219 win.containerElement = doc.getElementById(id + '_container');
\r
220 win.left = win.features['left'];
\r
221 win.top = win.features['top'];
\r
222 win.frame = window.frames[id + '_iframe'].frames[0];
\r
223 win.wrapperFrame = window.frames[id + '_iframe'];
\r
224 win.wrapperIFrameElement = document.getElementById(id + "_iframe");
\r
226 // Add event handlers
\r
227 mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
\r
228 mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
\r
230 if (mcWindows.isMSIE) {
\r
231 mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
\r
232 mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
\r
234 mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
\r
235 mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
\r
236 mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
\r
239 for (var i=0; i<window.frames.length; i++) {
\r
240 if (!window.frames[i]._hasMouseHandlers) {
\r
241 if (mcWindows.isMSIE) {
\r
242 mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
\r
243 mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
\r
245 mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
\r
246 mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
\r
249 window.frames[i]._hasMouseHandlers = true;
\r
253 if (mcWindows.isMSIE) {
\r
254 mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
\r
255 mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
\r
257 mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
\r
258 mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
\r
259 mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
\r
262 this.selectedWindow = win;
\r
265 MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
\r
266 var iframe = document.createElement("iframe");
\r
267 var div = document.createElement("div");
\r
269 width = parseInt(width);
\r
270 height = parseInt(height)+1;
\r
272 // Create wrapper div
\r
273 div.setAttribute("id", id_prefix + "_div");
\r
274 div.setAttribute("width", width);
\r
275 div.setAttribute("height", (height));
\r
276 div.style.position = "absolute";
\r
277 div.style.left = left + "px";
\r
278 div.style.top = top + "px";
\r
279 div.style.width = width + "px";
\r
280 div.style.height = (height) + "px";
\r
281 div.style.backgroundColor = "white";
\r
282 div.style.display = "none";
\r
284 if (this.isGecko) {
\r
285 iframeWidth = width + 2;
\r
286 iframeHeight = height + 2;
\r
288 iframeWidth = width;
\r
289 iframeHeight = height + 1;
\r
293 iframe.setAttribute("id", id_prefix + "_iframe");
\r
294 iframe.setAttribute("name", id_prefix + "_iframe");
\r
295 iframe.setAttribute("border", "0");
\r
296 iframe.setAttribute("frameBorder", "0");
\r
297 iframe.setAttribute("marginWidth", "0");
\r
298 iframe.setAttribute("marginHeight", "0");
\r
299 iframe.setAttribute("leftMargin", "0");
\r
300 iframe.setAttribute("topMargin", "0");
\r
301 iframe.setAttribute("width", iframeWidth);
\r
302 iframe.setAttribute("height", iframeHeight);
\r
303 // iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
\r
304 // iframe.setAttribute("allowtransparency", "false");
\r
305 iframe.setAttribute("scrolling", "no");
\r
306 iframe.style.width = iframeWidth + "px";
\r
307 iframe.style.height = iframeHeight + "px";
\r
308 iframe.style.backgroundColor = "white";
\r
309 div.appendChild(iframe);
\r
311 document.body.appendChild(div);
\r
313 // Fixed MSIE 5.0 issue
\r
314 div.innerHTML = div.innerHTML;
\r
316 if (this.isSafari) {
\r
317 // Give Safari some time to setup
\r
318 window.setTimeout(function() {
\r
319 doc = window.frames[id_prefix + '_iframe'].document;
\r
325 doc = window.frames[id_prefix + '_iframe'].window.document
\r
331 div.style.display = "block";
\r
337 function MCWindow() {
\r
340 MCWindow.prototype.focus = function() {
\r
341 this.winElement.style.zIndex = mcWindows.zindex++;
\r
342 mcWindows.selectedWindow = this;
\r
345 MCWindow.prototype.minimize = function() {
\r
348 MCWindow.prototype.maximize = function() {
\r
352 MCWindow.prototype.startResize = function() {
\r
353 mcWindows.action = "resize";
\r
356 MCWindow.prototype.startMove = function(e) {
\r
357 mcWindows.action = "move";
\r
360 MCWindow.prototype.close = function() {
\r
361 document.body.removeChild(this.winElement);
\r
362 mcWindows.windows[this.name] = null;
\r
365 MCWindow.prototype.onMouseMove = function(e) {
\r
366 var scrollX = 0;//this.doc.body.scrollLeft;
\r
367 var scrollY = 0;//this.doc.body.scrollTop;
\r
369 // Calculate real X, Y
\r
370 var dx = e.screenX - mcWindows.mouseDownScreenX;
\r
371 var dy = e.screenY - mcWindows.mouseDownScreenY;
\r
373 switch (mcWindows.action) {
\r
375 width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
\r
376 height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
\r
378 width = width < 100 ? 100 : width;
\r
379 height = height < 100 ? 100 : height;
\r
381 this.wrapperIFrameElement.style.width = width+2;
\r
382 this.wrapperIFrameElement.style.height = height+2;
\r
383 this.wrapperIFrameElement.width = width+2;
\r
384 this.wrapperIFrameElement.height = height+2;
\r
385 this.winElement.style.width = width;
\r
386 this.winElement.style.height = height;
\r
388 height = height-12-19;
\r
390 this.containerElement.style.width = width;
\r
392 this.iframeElement.style.width = width;
\r
393 this.iframeElement.style.height = height;
\r
394 this.bodyElement.style.width = width;
\r
395 this.bodyElement.style.height = height;
\r
396 this.headElement.style.width = width;
\r
397 //this.statusElement.style.width = width;
\r
399 mcWindows.cancelEvent(e);
\r
403 this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
\r
404 this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
\r
405 this.winElement.style.left = this.left + "px";
\r
406 this.winElement.style.top = this.top + "px";
\r
408 mcWindows.cancelEvent(e);
\r
413 MCWindow.prototype.onMouseUp = function(e) {
\r
414 mcWindows.action = "none";
\r
417 MCWindow.prototype.onFocus = function(e) {
\r
418 // Gecko only handler
\r
419 var winRef = e.currentTarget;
\r
421 for (var n in mcWindows.windows) {
\r
422 var win = mcWindows.windows[n];
\r
423 if (typeof(win) == 'function')
\r
426 if (winRef.name == win.id) {
\r
433 MCWindow.prototype.onMouseDown = function(e) {
\r
434 var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
\r
436 var scrollX = 0;//this.doc.body.scrollLeft;
\r
437 var scrollY = 0;//this.doc.body.scrollTop;
\r
439 mcWindows.mouseDownScreenX = e.screenX;
\r
440 mcWindows.mouseDownScreenY = e.screenY;
\r
441 mcWindows.mouseDownLayerX = this.left;
\r
442 mcWindows.mouseDownLayerY = this.top;
\r
443 mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
\r
444 mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
\r
446 if (elm == this.resizeElement.firstChild)
\r
447 this.startResize(e);
\r
451 mcWindows.cancelEvent(e);
\r
455 var mcWindows = new MCWindows();
\r