* Upgraded TinyMCE to version 2.0RC3. This fixes a conflict with
[citadel.git] / webcit / tiny_mce / plugins / inlinepopups / editor_plugin_src.js
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * Moxiecode DHTML Windows script.
7  *
8  * @author Moxiecode
9  * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
10  */
11
12 // Patch openWindow, closeWindow TinyMCE functions
13
14 function TinyMCE_inlinepopups_getInfo() {
15         return {
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
21         };
22 };
23
24 TinyMCE.prototype.orgOpenWindow = TinyMCE.prototype.openWindow;
25
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);
32                 return;
33         }
34
35         var url, resizable, scrollbars;
36
37         args['mce_inside_iframe'] = true;
38         tinyMCE.windowArgs = args;
39
40         if (template['file'].charAt(0) != '/' && template['file'].indexOf('://') == -1)
41                 url = tinyMCE.baseURL + "/themes/" + tinyMCE.getParam("theme") + "/" + template['file'];
42         else
43                 url = template['file'];
44
45         if (!(width = parseInt(template['width'])))
46                 width = 320;
47
48         if (!(height = parseInt(template['height'])))
49                 height = 200;
50
51         resizable = (args && args['resizable']) ? args['resizable'] : "no";
52         scrollbars = (args && args['scrollbars']) ? args['scrollbars'] : "no";
53
54         height += 18;
55
56         // Replace all args as variables in URL
57         for (var name in args) {
58                 if (typeof(args[name]) == 'function')
59                         continue;
60
61                 url = tinyMCE.replaceVar(url, name, escape(args[name]));
62         }
63
64         var elm = document.getElementById(this.selectedInstance.editorId + '_parent');
65         var pos = tinyMCE.getAbsPosition(elm);
66
67         // Center div in editor area
68         pos.absLeft += Math.round((elm.firstChild.clientWidth / 2) - (width / 2));
69         pos.absTop += Math.round((elm.firstChild.clientHeight / 2) - (height / 2));
70
71         mcWindows.open(url, mcWindows.idCounter++, "modal=yes,width=" + width+ ",height=" + height + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",statusbar=" + resizable + ",left=" + pos.absLeft + ",top=" + pos.absTop);
72 };
73
74 TinyMCE.prototype.orgCloseWindow = TinyMCE.prototype.closeWindow;
75
76 TinyMCE.prototype.closeWindow = function(win) {
77         if (mcWindows.selectedWindow != null)
78                 mcWindows.selectedWindow.close();
79         else
80                 this.orgCloseWindow(win);
81 };
82
83 TinyMCE.prototype.setWindowTitle = function(win_ref, title) {
84         for (var n in mcWindows.windows) {
85                 var win = mcWindows.windows[n];
86                 if (typeof(win) == 'function')
87                         continue;
88
89                 if (win_ref.name == win.id + "_iframe")
90                         window.frames[win.id + "_iframe"].document.getElementById(win.id + '_title').innerHTML = title;
91         }
92 };
93
94 // * * * * * MCWindows classes below
95
96 // Windows handler
97 function MCWindows() {
98         this.settings = new Array();
99         this.windows = new Array();
100         this.isMSIE = (navigator.appName == "Microsoft Internet Explorer");
101         this.isGecko = navigator.userAgent.indexOf('Gecko') != -1;
102         this.isSafari = navigator.userAgent.indexOf('Safari') != -1;
103         this.isMac = navigator.userAgent.indexOf('Mac') != -1;
104         this.isMSIE5_0 = this.isMSIE && (navigator.userAgent.indexOf('MSIE 5.0') != -1);
105         this.action = "none";
106         this.selectedWindow = null;
107         this.lastSelectedWindow = null;
108         this.zindex = 100;
109         this.mouseDownScreenX = 0;
110         this.mouseDownScreenY = 0;
111         this.mouseDownLayerX = 0;
112         this.mouseDownLayerY = 0;
113         this.mouseDownWidth = 0;
114         this.mouseDownHeight = 0;
115         this.idCounter = 0;
116 };
117
118 MCWindows.prototype.init = function(settings) {
119         this.settings = settings;
120
121         if (this.isMSIE)
122                 this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
123         else
124                 this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
125
126         this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
127
128         this.doc = document;
129 };
130
131 MCWindows.prototype.getParam = function(name, default_value) {
132         var value = null;
133
134         value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
135
136         // Fix bool values
137         if (value == "true" || value == "false")
138                 return (value == "true");
139
140         return value;
141 };
142
143 MCWindows.prototype.eventDispatcher = function(e) {
144         e = typeof(e) == "undefined" ? window.event : e;
145
146         if (mcWindows.selectedWindow == null)
147                 return;
148
149         // Switch focus
150         if (mcWindows.isGecko && e.type == "mousedown") {
151                 var elm = e.currentTarget;
152
153                 for (var n in mcWindows.windows) {
154                         var win = mcWindows.windows[n];
155
156                         if (win.headElement == elm || win.resizeElement == elm) {
157                                 win.focus();
158                                 break;
159                         }
160                 }
161         }
162
163         switch (e.type) {
164                 case "mousemove":
165                         mcWindows.selectedWindow.onMouseMove(e);
166                         break;
167
168                 case "mouseup":
169                         mcWindows.selectedWindow.onMouseUp(e);
170                         break;
171
172                 case "mousedown":
173                         mcWindows.selectedWindow.onMouseDown(e);
174                         break;
175
176                 case "focus":
177                         mcWindows.selectedWindow.onFocus(e);
178                         break;
179         }
180 };
181
182 MCWindows.prototype.addEvent = function(obj, name, handler) {
183         if (this.isMSIE)
184                 obj.attachEvent("on" + name, handler);
185         else
186                 obj.addEventListener(name, handler, true);
187 };
188
189 MCWindows.prototype.cancelEvent = function(e) {
190         if (this.isMSIE) {
191                 e.returnValue = false;
192                 e.cancelBubble = true;
193         } else
194                 e.preventDefault();
195 };
196
197 MCWindows.prototype.parseFeatures = function(opts) {
198         // Cleanup the options
199         opts = opts.toLowerCase();
200         opts = opts.replace(/;/g, ",");
201         opts = opts.replace(/[^0-9a-z=,]/g, "");
202
203         var optionChunks = opts.split(',');
204         var options = new Array();
205
206         options['left'] = "10";
207         options['top'] = "10";
208         options['width'] = "300";
209         options['height'] = "300";
210         options['resizable'] = "yes";
211         options['minimizable'] = "yes";
212         options['maximizable'] = "yes";
213         options['close'] = "yes";
214         options['movable'] = "yes";
215         options['statusbar'] = "yes";
216         options['scrollbars'] = "auto";
217         options['modal'] = "no";
218
219         if (opts == "")
220                 return options;
221
222         for (var i=0; i<optionChunks.length; i++) {
223                 var parts = optionChunks[i].split('=');
224
225                 if (parts.length == 2)
226                         options[parts[0]] = parts[1];
227         }
228
229         options['left'] = parseInt(options['left']);
230         options['top'] = parseInt(options['top']);
231         options['width'] = parseInt(options['width']);
232         options['height'] = parseInt(options['height']);
233
234         return options;
235 };
236
237 MCWindows.prototype.open = function(url, name, features) {
238         this.lastSelectedWindow = this.selectedWindow;
239
240         var win = new MCWindow();
241         var winDiv, html = "", id;
242         var imgPath = this.getParam("images_path");
243
244         features = this.parseFeatures(features);
245
246         // Create div
247         id = "mcWindow_" + name;
248         win.deltaHeight = 18;
249
250         if (features['statusbar'] == "yes") {
251                 win.deltaHeight += 13;
252
253                 if (this.isMSIE)
254                         win.deltaHeight += 1;
255         }
256
257         width = parseInt(features['width']);
258         height = parseInt(features['height'])-win.deltaHeight;
259
260         if (this.isMSIE)
261                 width -= 2;
262
263         // Setup first part of window
264         win.id = id;
265         win.url = url;
266         win.name = name;
267         win.features = features;
268         this.windows[name] = win;
269
270         iframeWidth = width;
271         iframeHeight = height;
272
273         // Create inner content
274         html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
275         html += '<html>';
276         html += '<head>';
277         html += '<title>Wrapper iframe</title>';
278         html += '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">';
279         html += '<link href="' + this.getParam("css_file") + '" rel="stylesheet" type="text/css" />';
280         html += '</head>';
281         html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
282
283         html += '<div id="' + id + '_container" class="mceWindow">';
284         html += '<div id="' + id + '_head" class="mceWindowHead" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
285         html += '  <div id="' + id + '_title" class="mceWindowTitle"';
286         html += '  onselectstart="return false;" unselectable="on" style="-moz-user-select: none !important;"></div>';
287         html += '    <div class="mceWindowHeadTools">';
288         html += '      <a href="javascript:parent.mcWindows.windows[\'' + name + '\'].close();" target="_self" onmousedown="return false;" class="mceWindowClose"><img border="0" src="' + imgPath + '/window_close.gif" /></a>';
289 //      html += '      <a href="javascript:mcWindows.windows[\'' + name + '\'].maximize();" target="_self" onmousedown="return false;" class="mceWindowMaximize"></a>';
290 //      html += '      <a href="javascript:mcWindows.windows[\'' + name + '\'].minimize();" target="_self" onmousedown="return false;" class="mceWindowMinimize"></a>';
291         html += '    </div>';
292         html += '</div><div id="' + id + '_body" class="mceWindowBody" style="width: ' + width + 'px; height: ' + height + 'px;">';
293         html += '<iframe id="' + id + '_iframe" name="' + id + '_iframe" frameborder="0" width="' + iframeWidth + '" height="' + iframeHeight + '" src="' + url + '" class="mceWindowBodyIframe" scrolling="' + features['scrollbars'] + '"></iframe></div>';
294
295         if (features['statusbar'] == "yes") {
296                 html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
297
298                 if (features['resizable'] == "yes") {
299                         if (this.isGecko)
300                                 html += '<div id="' + id + '_resize" class="mceWindowResize"><div style="background-image: url(\'' + imgPath + '/window_resize.gif\'); width: 12px; height: 12px;"></div></div>';
301                         else
302                                 html += '<div id="' + id + '_resize" class="mceWindowResize"><img onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();" border="0" src="' + imgPath + '/window_resize.gif" /></div>';
303                 }
304
305                 html += '</div>';
306         }
307
308         html += '</div>';
309
310         html += '</body>';
311         html += '</html>';
312
313         // Create iframe
314         this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
315 };
316
317 // Blocks the document events by placing a image over the whole document
318 MCWindows.prototype.setDocumentLock = function(state) {
319         if (state) {
320                 var elm = document.getElementById('mcWindowEventBlocker');
321                 if (elm == null) {
322                         elm = document.createElement("div");
323
324                         elm.id = "mcWindowEventBlocker";
325                         elm.style.position = "absolute";
326                         elm.style.left = "0px";
327                         elm.style.top = "0px";
328
329                         document.body.appendChild(elm);
330                 }
331
332                 elm.style.display = "none";
333
334                 var imgPath = this.getParam("images_path");
335                 var width = document.body.clientWidth;
336                 var height = document.body.clientHeight;
337
338                 elm.style.width = width;
339                 elm.style.height = height;
340                 elm.innerHTML = '<img src="' + imgPath + '/spacer.gif" width="' + width + '" height="' + height + '" />';
341
342                 elm.style.zIndex = mcWindows.zindex-1;
343                 elm.style.display = "block";
344         } else {
345                 var elm = document.getElementById('mcWindowEventBlocker');
346
347                 if (mcWindows.windows.length == 0)
348                         elm.parentNode.removeChild(elm);
349                 else
350                         elm.style.zIndex = mcWindows.zindex-1;
351         }
352 };
353
354 // Gets called when wrapper iframe is initialized
355 MCWindows.prototype.onLoad = function(name) {
356         var win = mcWindows.windows[name];
357         var id = "mcWindow_" + name;
358         var wrapperIframe = window.frames[id + "_iframe"].frames[0];
359         var wrapperDoc = window.frames[id + "_iframe"].document;
360         var doc = window.frames[id + "_iframe"].document;
361         var winDiv = document.getElementById("mcWindow_" + name + "_div");
362         var realIframe = window.frames[id + "_iframe"].frames[0];
363
364         // Set window data
365         win.id = "mcWindow_" + name;
366         win.winElement = winDiv;
367         win.bodyElement = doc.getElementById(id + '_body');
368         win.iframeElement = doc.getElementById(id + '_iframe');
369         win.headElement = doc.getElementById(id + '_head');
370         win.titleElement = doc.getElementById(id + '_title');
371         win.resizeElement = doc.getElementById(id + '_resize');
372         win.containerElement = doc.getElementById(id + '_container');
373         win.left = win.features['left'];
374         win.top = win.features['top'];
375         win.frame = window.frames[id + '_iframe'].frames[0];
376         win.wrapperFrame = window.frames[id + '_iframe'];
377         win.wrapperIFrameElement = document.getElementById(id + "_iframe");
378
379         // Add event handlers
380         mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
381
382         if (win.resizeElement != null)
383                 mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
384
385         if (mcWindows.isMSIE) {
386                 mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
387                 mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
388         } else {
389                 mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
390                 mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
391                 mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
392         }
393
394         for (var i=0; i<window.frames.length; i++) {
395                 if (!window.frames[i]._hasMouseHandlers) {
396                         if (mcWindows.isMSIE) {
397                                 mcWindows.addEvent(window.frames[i].document, "mousemove", mcWindows.eventDispatcher);
398                                 mcWindows.addEvent(window.frames[i].document, "mouseup", mcWindows.eventDispatcher);
399                         } else {
400                                 mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
401                                 mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
402                         }
403
404                         window.frames[i]._hasMouseHandlers = true;
405                 }
406         }
407
408         if (mcWindows.isMSIE) {
409                 mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
410                 mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
411         } else {
412                 mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
413                 mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
414                 mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
415         }
416
417         // Dispatch open window event
418         var func = this.getParam("on_open_window", "");
419         if (func != "")
420                 eval(func + "(win);");
421
422         win.focus();
423
424         if (win.features['modal'] == "yes")
425                 mcWindows.setDocumentLock(true);
426 };
427
428 MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
429         var iframe = document.createElement("iframe");
430         var div = document.createElement("div");
431
432         width = parseInt(width);
433         height = parseInt(height)+1;
434
435         // Create wrapper div
436         div.setAttribute("id", id_prefix + "_div");
437         div.setAttribute("width", width);
438         div.setAttribute("height", (height));
439         div.style.position = "absolute";
440         div.style.left = left + "px";
441         div.style.top = top + "px";
442         div.style.width = width + "px";
443         div.style.height = (height) + "px";
444         div.style.backgroundColor = "white";
445         div.style.display = "none";
446
447         if (this.isGecko) {
448                 iframeWidth = width + 2;
449                 iframeHeight = height + 2;
450         } else {
451                 iframeWidth = width;
452                 iframeHeight = height + 1;
453         }
454
455         // Create iframe
456         iframe.setAttribute("id", id_prefix + "_iframe");
457         iframe.setAttribute("name", id_prefix + "_iframe");
458         iframe.setAttribute("border", "0");
459         iframe.setAttribute("frameBorder", "0");
460         iframe.setAttribute("marginWidth", "0");
461         iframe.setAttribute("marginHeight", "0");
462         iframe.setAttribute("leftMargin", "0");
463         iframe.setAttribute("topMargin", "0");
464         iframe.setAttribute("width", iframeWidth);
465         iframe.setAttribute("height", iframeHeight);
466 //      iframe.setAttribute("src", "../jscripts/tiny_mce/blank.htm");
467         // iframe.setAttribute("allowtransparency", "false");
468         iframe.setAttribute("scrolling", "no");
469         iframe.style.width = iframeWidth + "px";
470         iframe.style.height = iframeHeight + "px";
471         iframe.style.backgroundColor = "white";
472         div.appendChild(iframe);
473
474         document.body.appendChild(div);
475
476         // Fixed MSIE 5.0 issue
477         div.innerHTML = div.innerHTML;
478
479         if (this.isSafari) {
480                 // Give Safari some time to setup
481                 window.setTimeout(function() {
482                         doc = window.frames[id_prefix + '_iframe'].document;
483                         doc.open();
484                         doc.write(html);
485                         doc.close();
486                 }, 10);
487         } else {
488                 doc = window.frames[id_prefix + '_iframe'].window.document;
489                 doc.open();
490                 doc.write(html);
491                 doc.close();
492         }
493
494         div.style.display = "block";
495
496         return div;
497 };
498
499 // Window instance
500 function MCWindow() {
501 };
502
503 MCWindow.prototype.focus = function() {
504         if (this != mcWindows.selectedWindow) {
505                 this.winElement.style.zIndex = ++mcWindows.zindex;
506                 mcWindows.lastSelectedWindow = mcWindows.selectedWindow;
507                 mcWindows.selectedWindow = this;
508         }
509 };
510
511 MCWindow.prototype.minimize = function() {
512 };
513
514 MCWindow.prototype.maximize = function() {
515         
516 };
517
518 MCWindow.prototype.startResize = function() {
519         mcWindows.action = "resize";
520 };
521
522 MCWindow.prototype.startMove = function(e) {
523         mcWindows.action = "move";
524 };
525
526 MCWindow.prototype.close = function() {
527         if (mcWindows.lastSelectedWindow != null)
528                 mcWindows.lastSelectedWindow.focus();
529
530         var mcWindowsNew = new Array();
531         for (var n in mcWindows.windows) {
532                 var win = mcWindows.windows[n];
533                 if (typeof(win) == 'function')
534                         continue;
535
536                 if (win.name != this.name)
537                         mcWindowsNew[n] = win;
538         }
539
540         mcWindows.windows = mcWindowsNew;
541
542 //      alert(mcWindows.doc.getElementById(this.id + "_iframe"));
543
544         var e = mcWindows.doc.getElementById(this.id + "_iframe");
545         e.parentNode.removeChild(e);
546
547         var e = mcWindows.doc.getElementById(this.id + "_div");
548         e.parentNode.removeChild(e);
549
550         mcWindows.setDocumentLock(false);
551 };
552
553 MCWindow.prototype.onMouseMove = function(e) {
554         var scrollX = 0;//this.doc.body.scrollLeft;
555         var scrollY = 0;//this.doc.body.scrollTop;
556
557         // Calculate real X, Y
558         var dx = e.screenX - mcWindows.mouseDownScreenX;
559         var dy = e.screenY - mcWindows.mouseDownScreenY;
560
561         switch (mcWindows.action) {
562                 case "resize":
563                         width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
564                         height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
565
566                         width = width < 100 ? 100 : width;
567                         height = height < 100 ? 100 : height;
568
569                         this.wrapperIFrameElement.style.width = width+2;
570                         this.wrapperIFrameElement.style.height = height+2;
571                         this.wrapperIFrameElement.width = width+2;
572                         this.wrapperIFrameElement.height = height+2;
573                         this.winElement.style.width = width;
574                         this.winElement.style.height = height;
575
576                         height = height - this.deltaHeight;
577
578                         this.containerElement.style.width = width;
579
580                         this.iframeElement.style.width = width;
581                         this.iframeElement.style.height = height;
582                         this.bodyElement.style.width = width;
583                         this.bodyElement.style.height = height;
584                         this.headElement.style.width = width;
585                         //this.statusElement.style.width = width;
586
587                         mcWindows.cancelEvent(e);
588                         break;
589
590                 case "move":
591                         this.left = mcWindows.mouseDownLayerX + (e.screenX - mcWindows.mouseDownScreenX);
592                         this.top = mcWindows.mouseDownLayerY + (e.screenY - mcWindows.mouseDownScreenY);
593                         this.winElement.style.left = this.left + "px";
594                         this.winElement.style.top = this.top + "px";
595
596                         mcWindows.cancelEvent(e);
597                         break;
598         }
599 };
600
601 function debug(msg) {
602         document.getElementById('debug').value += msg + "\n";
603 }
604
605 MCWindow.prototype.onMouseUp = function(e) {
606         mcWindows.action = "none";
607 };
608
609 MCWindow.prototype.onFocus = function(e) {
610         // Gecko only handler
611         var winRef = e.currentTarget;
612
613         for (var n in mcWindows.windows) {
614                 var win = mcWindows.windows[n];
615                 if (typeof(win) == 'function')
616                         continue;
617
618                 if (winRef.name == win.id + "_iframe") {
619                         win.focus();
620                         return;
621                 }
622         }
623 };
624
625 MCWindow.prototype.onMouseDown = function(e) {
626         var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
627
628         var scrollX = 0;//this.doc.body.scrollLeft;
629         var scrollY = 0;//this.doc.body.scrollTop;
630
631         mcWindows.mouseDownScreenX = e.screenX;
632         mcWindows.mouseDownScreenY = e.screenY;
633         mcWindows.mouseDownLayerX = this.left;
634         mcWindows.mouseDownLayerY = this.top;
635         mcWindows.mouseDownWidth = parseInt(this.winElement.style.width);
636         mcWindows.mouseDownHeight = parseInt(this.winElement.style.height);
637
638         if (this.resizeElement != null && elm == this.resizeElement.firstChild)
639                 this.startResize(e);
640         else
641                 this.startMove(e);
642
643         mcWindows.cancelEvent(e);
644 };
645
646 // Global instance
647 var mcWindows = new MCWindows();
648
649 // Initialize windows
650 mcWindows.init({
651         images_path : tinyMCE.baseURL + "/plugins/inlinepopups/images",
652         css_file : tinyMCE.baseURL + "/plugins/inlinepopups/css/inlinepopup.css"
653 });