6 * Moxiecode DHTML Windows script.
9 * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
12 // Patch openWindow, closeWindow TinyMCE functions
14 function TinyMCE_inlinepopups_getInfo() {
16 longname : 'Inline Popups',
17 author : 'Moxiecode Systems',
18 authorurl : 'http://tinymce.moxiecode.com',
19 infourl : 'http://tinymce.moxiecode.com/tinymce/docs/plugin_inlinepopups.html',
20 version : tinyMCE.majorVersion + "." + tinyMCE.minorVersion
24 TinyMCE.prototype.orgOpenWindow = TinyMCE.prototype.openWindow;
26 TinyMCE.prototype.openWindow = function(template, args) {
27 // Does the caller support inline
28 if (args['inline'] != "yes") {
29 mcWindows.selectedWindow = null;
30 args['mce_inside_iframe'] = false;
31 this.orgOpenWindow(template, args);
35 var url, resizable, scrollbars;
37 args['mce_inside_iframe'] = true;
38 tinyMCE.windowArgs = args;
40 if (template['file'].charAt(0) != '/' && template['file'].indexOf('://') == -1)
41 url = tinyMCE.baseURL + "/themes/" + tinyMCE.getParam("theme") + "/" + template['file'];
43 url = template['file'];
45 if (!(width = parseInt(template['width'])))
48 if (!(height = parseInt(template['height'])))
51 resizable = (args && args['resizable']) ? args['resizable'] : "no";
52 scrollbars = (args && args['scrollbars']) ? args['scrollbars'] : "no";
56 // Replace all args as variables in URL
57 for (var name in args)
58 url = tinyMCE.replaceVar(url, name, escape(args[name]));
60 var elm = document.getElementById(this.selectedInstance.editorId + '_parent');
61 var pos = tinyMCE.getAbsPosition(elm);
63 // Center div in editor area
64 pos.absLeft += Math.round((elm.firstChild.clientWidth / 2) - (width / 2));
65 pos.absTop += Math.round((elm.firstChild.clientHeight / 2) - (height / 2));
67 mcWindows.open(url, mcWindows.idCounter++, "modal=yes,width=" + width+ ",height=" + height + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",statusbar=" + resizable + ",left=" + pos.absLeft + ",top=" + pos.absTop);
70 TinyMCE.prototype.orgCloseWindow = TinyMCE.prototype.closeWindow;
72 TinyMCE.prototype.closeWindow = function(win) {
73 if (mcWindows.selectedWindow != null)
74 mcWindows.selectedWindow.close();
76 this.orgCloseWindow(win);
79 TinyMCE.prototype.setWindowTitle = function(win_ref, title) {
80 for (var n in mcWindows.windows) {
81 var win = mcWindows.windows[n];
83 if (win_ref.name == win.id + "_iframe")
84 window.frames[win.id + "_iframe"].document.getElementById(win.id + '_title').innerHTML = title;
88 // * * * * * MCWindows classes below
91 function MCWindows() {
92 this.settings = new Array();
93 this.windows = new Array();
94 this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
95 this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
96 this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
97 this.isMac = navigator.userAgent.indexOf('Mac') != -1;
98 this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
100 this.selectedWindow = null;
101 this.lastSelectedWindow = null;
103 this.mouseDownScreenX = 0;
104 this.mouseDownScreenY = 0;
105 this.mouseDownLayerX = 0;
106 this.mouseDownLayerY = 0;
107 this.mouseDownWidth = 0;
108 this.mouseDownHeight = 0;
112 MCWindows.prototype.init = function(settings) {
113 this.settings = settings;
116 this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
118 this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
120 this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
125 MCWindows.prototype.getParam = function(name, default_value) {
128 value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
131 if (value == "true" || value == "false")
132 return (value == "true");
137 MCWindows.prototype.eventDispatcher = function(e) {
138 e = typeof(e) == "undefined" ? window.event : e;
140 if (mcWindows.selectedWindow == null)
144 if (mcWindows.isGecko && e.type == "mousedown") {
145 var elm = e.currentTarget;
147 for (var n in mcWindows.windows) {
148 var win = mcWindows.windows[n];
150 if (win.headElement == elm || win.resizeElement == elm) {
159 mcWindows.selectedWindow.onMouseMove(e);
163 mcWindows.selectedWindow.onMouseUp(e);
167 mcWindows.selectedWindow.onMouseDown(e);
171 mcWindows.selectedWindow.onFocus(e);
176 MCWindows.prototype.addEvent = function(obj, name, handler) {
178 obj.attachEvent("on" + name, handler);
180 obj.addEventListener(name, handler, true);
183 MCWindows.prototype.cancelEvent = function(e) {
185 e.returnValue = false;
186 e.cancelBubble = true;
191 MCWindows.prototype.parseFeatures = function(opts) {
192 // Cleanup the options
193 opts = opts.toLowerCase();
194 opts = opts.replace(/;/g, ",");
195 opts = opts.replace(/[^0-9a-z=,]/g, "");
197 var optionChunks = opts.split(',');
198 var options = new Array();
200 options['left'] = "10";
201 options['top'] = "10";
202 options['width'] = "300";
203 options['height'] = "300";
204 options['resizable'] = "yes";
205 options['minimizable'] = "yes";
206 options['maximizable'] = "yes";
207 options['close'] = "yes";
208 options['movable'] = "yes";
209 options['statusbar'] = "yes";
210 options['scrollbars'] = "auto";
211 options['modal'] = "no";
216 for (var i=0; i<optionChunks.length; i++) {
217 var parts = optionChunks[i].split('=');
219 if (parts.length == 2)
220 options[parts[0]] = parts[1];
223 options['left'] = parseInt(options['left']);
224 options['top'] = parseInt(options['top']);
225 options['width'] = parseInt(options['width']);
226 options['height'] = parseInt(options['height']);
231 MCWindows.prototype.open = function(url, name, features) {
232 this.lastSelectedWindow = this.selectedWindow;
234 var win = new MCWindow();
235 var winDiv, html = "", id;
236 var imgPath = this.getParam("images_path");
238 features = this.parseFeatures(features);
241 id = "mcWindow_" + name;
242 win.deltaHeight = 18;
244 if (features['statusbar'] == "yes") {
245 win.deltaHeight += 13;
248 win.deltaHeight += 1;
251 width = parseInt(features['width']);
252 height = parseInt(features['height'])-win.deltaHeight;
257 // Setup first part of window
261 win.features = features;
262 this.windows[name] = win;
265 iframeHeight = height;
267 // Create inner content
268 html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
271 html += '<title>Wrapper iframe</title>';
272 html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
273 html += '<link href="' + this.getParam("css_file") + '" rel="stylesheet" type="text/css" />';
275 html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
277 html += '<div id="' + id + '_container" class="mceWindow">';
278 html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
279 html += ' <div id="' + id + '_title" class="mceWindowTitle"';
280 html += ' onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;"></div>';
281 html += ' <div class="mceWindowHeadTools">';
282 html += ' <a href="javascript:parent.mcWindows.windows[\'' + name + '\'].close();" onmousedown="return false;" class="mceWindowClose"><img border="0" src="' + imgPath + '/window_close.gif" /></a>';
283 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].maximize();" onmousedown="return false;" class="mceWindowMaximize"></a>';
284 // html += ' <a href="javascript:mcWindows.windows[\'' + name + '\'].minimize();" onmousedown="return false;" class="mceWindowMinimize"></a>';
286 html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
287 html += '<iframe id="' + id + '_iframe" name="' + id + '_iframe" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe" scrolling="' + features['scrollbars'] + '"></iframe></div>';
289 if (features['statusbar'] == "yes") {
290 html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
292 if (features['resizable'] == "yes") {
294 html += '<div id="' + id + '_resize" class="mceWindowResize"><div style="background-image: url(\'' + imgPath + '/window_resize.gif\'); width: 12px; height: 12px;"></div></div>';
296 html += '<div id="' + id + '_resize" class="mceWindowResize"><img onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();" border="0" src="' + imgPath + '/window_resize.gif" /></div>';
308 this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
311 // Blocks the document events by placing a image over the whole document
312 MCWindows.prototype.setDocumentLock = function(state) {
314 var elm = document.getElementById('mcWindowEventBlocker');
316 elm = document.createElement("div");
318 elm.id = "mcWindowEventBlocker";
319 elm.style.position = "absolute";
320 elm.style.left = "0px";
321 elm.style.top = "0px";
323 document.body.appendChild(elm);
326 elm.style.display = "none";
328 var imgPath = this.getParam("images_path");
329 var width = document.body.clientWidth;
330 var height = document.body.clientHeight;
332 elm.style.width = width;
333 elm.style.height = height;
334 elm.innerHTML = '<img src="' + imgPath + '/spacer.gif" width="' + width + '" height="' + height + '" />';
336 elm.style.zIndex = mcWindows.zindex-1;
337 elm.style.display = "block";
339 var elm = document.getElementById('mcWindowEventBlocker');
341 if (mcWindows.windows.length == 0)
342 elm.parentNode.removeChild(elm);
344 elm.style.zIndex = mcWindows.zindex-1;
348 // Gets called when wrapper iframe is initialized
349 MCWindows.prototype.onLoad = function(name) {
350 var win = mcWindows.windows[name];
351 var id = "mcWindow_" + name;
352 var wrapperIframe = window.frames[id + "_iframe"].frames[0];
353 var wrapperDoc = window.frames[id + "_iframe"].document;
354 var doc = window.frames[id + "_iframe"].document;
355 var winDiv = document.getElementById("mcWindow_" + name + "_div");
356 var realIframe = window.frames[id + "_iframe"].frames[0];
359 win.id = "mcWindow_" + name;
360 win.winElement = winDiv;
361 win.bodyElement = doc.getElementById(id + '_body');
362 win.iframeElement = doc.getElementById(id + '_iframe');
363 win.headElement = doc.getElementById(id + '_head');
364 win.titleElement = doc.getElementById(id + '_title');
365 win.resizeElement = doc.getElementById(id + '_resize');
366 win.containerElement = doc.getElementById(id + '_container');
367 win.left = win.features['left'];
368 win.top = win.features['top'];
369 win.frame = window.frames[id + '_iframe'].frames[0];
370 win.wrapperFrame = window.frames[id + '_iframe'];
371 win.wrapperIFrameElement = document.getElementById(id + "_iframe");
373 // Add event handlers
374 mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
376 if (win.resizeElement != null)
377 mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
379 if (mcWindows.isMSIE) {
380 mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
381 mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
383 mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
384 mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
385 mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
388 for (var i=0; i<window.frames.length; i++) {
389 if (!window.frames[i]._hasMouseHandlers) {
390 if (mcWindows.isMSIE) {
391 mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
392 mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
394 mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
395 mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
398 window.frames[i]._hasMouseHandlers = true;
402 if (mcWindows.isMSIE) {
403 mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
404 mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
406 mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
407 mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
408 mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
411 // Dispatch open window event
412 var func = this.getParam("on_open_window", "");
414 eval(func + "(win);");
418 if (win.features['modal'] == "yes")
419 mcWindows.setDocumentLock(true);
422 MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
423 var iframe = document.createElement("iframe");
424 var div = document.createElement("div");
426 width = parseInt(width);
427 height = parseInt(height)+1;
429 // Create wrapper div
430 div.setAttribute("id", id_prefix + "_div");
431 div.setAttribute("width", width);
432 div.setAttribute("height", (height));
433 div.style.position = "absolute";
434 div.style.left = left + "px";
435 div.style.top = top + "px";
436 div.style.width = width + "px";
437 div.style.height = (height) + "px";
438 div.style.backgroundColor = "white";
439 div.style.display = "none";
442 iframeWidth = width + 2;
443 iframeHeight = height + 2;
446 iframeHeight = height + 1;
450 iframe.setAttribute("id", id_prefix + "_iframe");
451 iframe.setAttribute("name", id_prefix + "_iframe");
452 iframe.setAttribute("border", "0");
453 iframe.setAttribute("frameBorder", "0");
454 iframe.setAttribute("marginWidth", "0");
455 iframe.setAttribute("marginHeight", "0");
456 iframe.setAttribute("leftMargin", "0");
457 iframe.setAttribute("topMargin", "0");
458 iframe.setAttribute("width", iframeWidth);
459 iframe.setAttribute("height", iframeHeight);
460 // iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
461 // iframe.setAttribute("allowtransparency", "false");
462 iframe.setAttribute("scrolling", "no");
463 iframe.style.width = iframeWidth + "px";
464 iframe.style.height = iframeHeight + "px";
465 iframe.style.backgroundColor = "white";
466 div.appendChild(iframe);
468 document.body.appendChild(div);
470 // Fixed MSIE 5.0 issue
471 div.innerHTML = div.innerHTML;
474 // Give Safari some time to setup
475 window.setTimeout(function() {
476 doc = window.frames[id_prefix + '_iframe'].document;
482 doc = window.frames[id_prefix + '_iframe'].window.document;
488 div.style.display = "block";
494 function MCWindow() {
497 MCWindow.prototype.focus = function() {
498 if (this != mcWindows.selectedWindow) {
499 this.winElement.style.zIndex = ++mcWindows.zindex;
500 mcWindows.lastSelectedWindow = mcWindows.selectedWindow;
501 mcWindows.selectedWindow = this;
505 MCWindow.prototype.minimize = function() {
508 MCWindow.prototype.maximize = function() {
512 MCWindow.prototype.startResize = function() {
513 mcWindows.action = "resize";
516 MCWindow.prototype.startMove = function(e) {
517 mcWindows.action = "move";
520 MCWindow.prototype.close = function() {
521 if (mcWindows.lastSelectedWindow != null)
522 mcWindows.lastSelectedWindow.focus();
524 var mcWindowsNew = new Array();
525 for (var n in mcWindows.windows) {
526 var win = mcWindows.windows[n];
528 if (win.name != this.name)
529 mcWindowsNew[n] = win;
532 mcWindows.windows = mcWindowsNew;
534 // alert(mcWindows.doc.getElementById(this.id + "_iframe"));
536 var e = mcWindows.doc.getElementById(this.id + "_iframe");
537 e.parentNode.removeChild(e);
539 var e = mcWindows.doc.getElementById(this.id + "_div");
540 e.parentNode.removeChild(e);
542 mcWindows.setDocumentLock(false);
545 MCWindow.prototype.onMouseMove = function(e) {
546 var scrollX = 0;//this.doc.body.scrollLeft;
547 var scrollY = 0;//this.doc.body.scrollTop;
549 // Calculate real X, Y
550 var dx = e.screenX - mcWindows.mouseDownScreenX;
551 var dy = e.screenY - mcWindows.mouseDownScreenY;
553 switch (mcWindows.action) {
555 width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
556 height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
558 width = width < 100 ? 100 : width;
559 height = height < 100 ? 100 : height;
561 this.wrapperIFrameElement.style.width = width+2;
562 this.wrapperIFrameElement.style.height = height+2;
563 this.wrapperIFrameElement.width = width+2;
564 this.wrapperIFrameElement.height = height+2;
565 this.winElement.style.width = width;
566 this.winElement.style.height = height;
568 height = height - this.deltaHeight;
570 this.containerElement.style.width = width;
572 this.iframeElement.style.width = width;
573 this.iframeElement.style.height = height;
574 this.bodyElement.style.width = width;
575 this.bodyElement.style.height = height;
576 this.headElement.style.width = width;
577 //this.statusElement.style.width = width;
579 mcWindows.cancelEvent(e);
583 this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
584 this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
585 this.winElement.style.left = this.left + "px";
586 this.winElement.style.top = this.top + "px";
588 mcWindows.cancelEvent(e);
593 function debug(msg) {
594 document.getElementById('debug').value += msg + "\n";
597 MCWindow.prototype.onMouseUp = function(e) {
598 mcWindows.action = "none";
601 MCWindow.prototype.onFocus = function(e) {
602 // Gecko only handler
603 var winRef = e.currentTarget;
605 for (var n in mcWindows.windows) {
606 var win = mcWindows.windows[n];
608 if (winRef.name == win.id + "_iframe") {
615 MCWindow.prototype.onMouseDown = function(e) {
616 var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
618 var scrollX = 0;//this.doc.body.scrollLeft;
619 var scrollY = 0;//this.doc.body.scrollTop;
621 mcWindows.mouseDownScreenX = e.screenX;
622 mcWindows.mouseDownScreenY = e.screenY;
623 mcWindows.mouseDownLayerX = this.left;
624 mcWindows.mouseDownLayerY = this.top;
625 mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
626 mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
628 if (this.resizeElement != null && elm == this.resizeElement.firstChild)
633 mcWindows.cancelEvent(e);
637 var mcWindows = new MCWindows();
639 // Initialize windows
641 images_path : tinyMCE.baseURL + "/plugins/inlinepopups/images",
642 css_file : tinyMCE.baseURL + "/plugins/inlinepopups/css/inlinepopup.css"