* Removed the Kevin Roth rich text editor and replaced it with TinyMCE.
[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                 url = tinyMCE.replaceVar(url, name, escape(args[name]));
59
60         var elm = document.getElementById(this.selectedInstance.editorId + '_parent');
61         var pos = tinyMCE.getAbsPosition(elm);
62
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));
66
67         mcWindows.open(url, mcWindows.idCounter++, "modal=yes,width=" + width+ ",height=" + height + ",resizable=" + resizable + ",scrollbars=" + scrollbars + ",statusbar=" + resizable + ",left=" + pos.absLeft + ",top=" + pos.absTop);
68 };
69
70 TinyMCE.prototype.orgCloseWindow = TinyMCE.prototype.closeWindow;
71
72 TinyMCE.prototype.closeWindow = function(win) {
73         if (mcWindows.selectedWindow != null)
74                 mcWindows.selectedWindow.close();
75         else
76                 this.orgCloseWindow(win);
77 };
78
79 TinyMCE.prototype.setWindowTitle = function(win_ref, title) {
80         for (var n in mcWindows.windows) {
81                 var win = mcWindows.windows[n];
82
83                 if (win_ref.name == win.id + "_iframe")
84                         window.frames[win.id + "_iframe"].document.getElementById(win.id + '_title').innerHTML = title;
85         }
86 };
87
88 // * * * * * MCWindows classes below
89
90 // Windows handler
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);
99         this.action = "none";
100         this.selectedWindow = null;
101         this.lastSelectedWindow = null;
102         this.zindex = 100;
103         this.mouseDownScreenX = 0;
104         this.mouseDownScreenY = 0;
105         this.mouseDownLayerX = 0;
106         this.mouseDownLayerY = 0;
107         this.mouseDownWidth = 0;
108         this.mouseDownHeight = 0;
109         this.idCounter = 0;
110 };
111
112 MCWindows.prototype.init = function(settings) {
113         this.settings = settings;
114
115         if (this.isMSIE)
116                 this.addEvent(document, "mousemove", mcWindows.eventDispatcher);
117         else
118                 this.addEvent(window, "mousemove", mcWindows.eventDispatcher);
119
120         this.addEvent(document, "mouseup", mcWindows.eventDispatcher);
121
122         this.doc = document;
123 };
124
125 MCWindows.prototype.getParam = function(name, default_value) {
126         var value = null;
127
128         value = (typeof(this.settings[name]) == "undefined") ? default_value : this.settings[name];
129
130         // Fix bool values
131         if (value == "true" || value == "false")
132                 return (value == "true");
133
134         return value;
135 };
136
137 MCWindows.prototype.eventDispatcher = function(e) {
138         e = typeof(e) == "undefined" ? window.event : e;
139
140         if (mcWindows.selectedWindow == null)
141                 return;
142
143         // Switch focus
144         if (mcWindows.isGecko && e.type == "mousedown") {
145                 var elm = e.currentTarget;
146
147                 for (var n in mcWindows.windows) {
148                         var win = mcWindows.windows[n];
149
150                         if (win.headElement == elm || win.resizeElement == elm) {
151                                 win.focus();
152                                 break;
153                         }
154                 }
155         }
156
157         switch (e.type) {
158                 case "mousemove":
159                         mcWindows.selectedWindow.onMouseMove(e);
160                         break;
161
162                 case "mouseup":
163                         mcWindows.selectedWindow.onMouseUp(e);
164                         break;
165
166                 case "mousedown":
167                         mcWindows.selectedWindow.onMouseDown(e);
168                         break;
169
170                 case "focus":
171                         mcWindows.selectedWindow.onFocus(e);
172                         break;
173         }
174 };
175
176 MCWindows.prototype.addEvent = function(obj, name, handler) {
177         if (this.isMSIE)
178                 obj.attachEvent("on" + name, handler);
179         else
180                 obj.addEventListener(name, handler, true);
181 };
182
183 MCWindows.prototype.cancelEvent = function(e) {
184         if (this.isMSIE) {
185                 e.returnValue = false;
186                 e.cancelBubble = true;
187         } else
188                 e.preventDefault();
189 };
190
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, "");
196
197         var optionChunks = opts.split(',');
198         var options = new Array();
199
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";
212
213         if (opts == "")
214                 return options;
215
216         for (var i=0; i<optionChunks.length; i++) {
217                 var parts = optionChunks[i].split('=');
218
219                 if (parts.length == 2)
220                         options[parts[0]] = parts[1];
221         }
222
223         options['left'] = parseInt(options['left']);
224         options['top'] = parseInt(options['top']);
225         options['width'] = parseInt(options['width']);
226         options['height'] = parseInt(options['height']);
227
228         return options;
229 };
230
231 MCWindows.prototype.open = function(url, name, features) {
232         this.lastSelectedWindow = this.selectedWindow;
233
234         var win = new MCWindow();
235         var winDiv, html = "", id;
236         var imgPath = this.getParam("images_path");
237
238         features = this.parseFeatures(features);
239
240         // Create div
241         id = "mcWindow_" + name;
242         win.deltaHeight = 18;
243
244         if (features['statusbar'] == "yes") {
245                 win.deltaHeight += 13;
246
247                 if (this.isMSIE)
248                         win.deltaHeight += 1;
249         }
250
251         width = parseInt(features['width']);
252         height = parseInt(features['height'])-win.deltaHeight;
253
254         if (this.isMSIE)
255                 width -= 2;
256
257         // Setup first part of window
258         win.id = id;
259         win.url = url;
260         win.name = name;
261         win.features = features;
262         this.windows[name] = win;
263
264         iframeWidth = width;
265         iframeHeight = height;
266
267         // Create inner content
268         html += '<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">';
269         html += '<html>';
270         html += '<head>';
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" />';
274         html += '</head>';
275         html += '<body onload="parent.mcWindows.onLoad(\'' + name + '\');">';
276
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>';
285         html += '    </div>';
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>';
288
289         if (features['statusbar'] == "yes") {
290                 html += '<div id="' + id + '_statusbar" class="mceWindowStatusbar" onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();">';
291
292                 if (features['resizable'] == "yes") {
293                         if (this.isGecko)
294                                 html += '<div id="' + id + '_resize" class="mceWindowResize"><div style="background-image: url(\'' + imgPath + '/window_resize.gif\'); width: 12px; height: 12px;"></div></div>';
295                         else
296                                 html += '<div id="' + id + '_resize" class="mceWindowResize"><img onmousedown="parent.mcWindows.windows[\'' + name + '\'].focus();" border="0" src="' + imgPath + '/window_resize.gif" /></div>';
297                 }
298
299                 html += '</div>';
300         }
301
302         html += '</div>';
303
304         html += '</body>';
305         html += '</html>';
306
307         // Create iframe
308         this.createFloatingIFrame(id, features['left'], features['top'], features['width'], features['height'], html);
309 };
310
311 // Blocks the document events by placing a image over the whole document
312 MCWindows.prototype.setDocumentLock = function(state) {
313         if (state) {
314                 var elm = document.getElementById('mcWindowEventBlocker');
315                 if (elm == null) {
316                         elm = document.createElement("div");
317
318                         elm.id = "mcWindowEventBlocker";
319                         elm.style.position = "absolute";
320                         elm.style.left = "0px";
321                         elm.style.top = "0px";
322
323                         document.body.appendChild(elm);
324                 }
325
326                 elm.style.display = "none";
327
328                 var imgPath = this.getParam("images_path");
329                 var width = document.body.clientWidth;
330                 var height = document.body.clientHeight;
331
332                 elm.style.width = width;
333                 elm.style.height = height;
334                 elm.innerHTML = '<img src="' + imgPath + '/spacer.gif" width="' + width + '" height="' + height + '" />';
335
336                 elm.style.zIndex = mcWindows.zindex-1;
337                 elm.style.display = "block";
338         } else {
339                 var elm = document.getElementById('mcWindowEventBlocker');
340
341                 if (mcWindows.windows.length == 0)
342                         elm.parentNode.removeChild(elm);
343                 else
344                         elm.style.zIndex = mcWindows.zindex-1;
345         }
346 };
347
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];
357
358         // Set window data
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");
372
373         // Add event handlers
374         mcWindows.addEvent(win.headElement, "mousedown", mcWindows.eventDispatcher);
375
376         if (win.resizeElement != null)
377                 mcWindows.addEvent(win.resizeElement, "mousedown", mcWindows.eventDispatcher);
378
379         if (mcWindows.isMSIE) {
380                 mcWindows.addEvent(realIframe.document, "mousemove", mcWindows.eventDispatcher);
381                 mcWindows.addEvent(realIframe.document, "mouseup", mcWindows.eventDispatcher);
382         } else {
383                 mcWindows.addEvent(realIframe, "mousemove", mcWindows.eventDispatcher);
384                 mcWindows.addEvent(realIframe, "mouseup", mcWindows.eventDispatcher);
385                 mcWindows.addEvent(realIframe, "focus", mcWindows.eventDispatcher);
386         }
387
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);
393                         } else {
394                                 mcWindows.addEvent(window.frames[i], "mousemove", mcWindows.eventDispatcher);
395                                 mcWindows.addEvent(window.frames[i], "mouseup", mcWindows.eventDispatcher);
396                         }
397
398                         window.frames[i]._hasMouseHandlers = true;
399                 }
400         }
401
402         if (mcWindows.isMSIE) {
403                 mcWindows.addEvent(win.frame.document, "mousemove", mcWindows.eventDispatcher);
404                 mcWindows.addEvent(win.frame.document, "mouseup", mcWindows.eventDispatcher);
405         } else {
406                 mcWindows.addEvent(win.frame, "mousemove", mcWindows.eventDispatcher);
407                 mcWindows.addEvent(win.frame, "mouseup", mcWindows.eventDispatcher);
408                 mcWindows.addEvent(win.frame, "focus", mcWindows.eventDispatcher);
409         }
410
411         // Dispatch open window event
412         var func = this.getParam("on_open_window", "");
413         if (func != "")
414                 eval(func + "(win);");
415
416         win.focus();
417
418         if (win.features['modal'] == "yes")
419                 mcWindows.setDocumentLock(true);
420 };
421
422 MCWindows.prototype.createFloatingIFrame = function(id_prefix, left, top, width, height, html) {
423         var iframe = document.createElement("iframe");
424         var div = document.createElement("div");
425
426         width = parseInt(width);
427         height = parseInt(height)+1;
428
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";
440
441         if (this.isGecko) {
442                 iframeWidth = width + 2;
443                 iframeHeight = height + 2;
444         } else {
445                 iframeWidth = width;
446                 iframeHeight = height + 1;
447         }
448
449         // Create iframe
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);
467
468         document.body.appendChild(div);
469
470         // Fixed MSIE 5.0 issue
471         div.innerHTML = div.innerHTML;
472
473         if (this.isSafari) {
474                 // Give Safari some time to setup
475                 window.setTimeout(function() {
476                         doc = window.frames[id_prefix + '_iframe'].document;
477                         doc.open();
478                         doc.write(html);
479                         doc.close();
480                 }, 10);
481         } else {
482                 doc = window.frames[id_prefix + '_iframe'].window.document;
483                 doc.open();
484                 doc.write(html);
485                 doc.close();
486         }
487
488         div.style.display = "block";
489
490         return div;
491 };
492
493 // Window instance
494 function MCWindow() {
495 };
496
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;
502         }
503 };
504
505 MCWindow.prototype.minimize = function() {
506 };
507
508 MCWindow.prototype.maximize = function() {
509         
510 };
511
512 MCWindow.prototype.startResize = function() {
513         mcWindows.action = "resize";
514 };
515
516 MCWindow.prototype.startMove = function(e) {
517         mcWindows.action = "move";
518 };
519
520 MCWindow.prototype.close = function() {
521         if (mcWindows.lastSelectedWindow != null)
522                 mcWindows.lastSelectedWindow.focus();
523
524         var mcWindowsNew = new Array();
525         for (var n in mcWindows.windows) {
526                 var win = mcWindows.windows[n];
527
528                 if (win.name != this.name)
529                         mcWindowsNew[n] = win;
530         }
531
532         mcWindows.windows = mcWindowsNew;
533
534 //      alert(mcWindows.doc.getElementById(this.id + "_iframe"));
535
536         var e = mcWindows.doc.getElementById(this.id + "_iframe");
537         e.parentNode.removeChild(e);
538
539         var e = mcWindows.doc.getElementById(this.id + "_div");
540         e.parentNode.removeChild(e);
541
542         mcWindows.setDocumentLock(false);
543 };
544
545 MCWindow.prototype.onMouseMove = function(e) {
546         var scrollX = 0;//this.doc.body.scrollLeft;
547         var scrollY = 0;//this.doc.body.scrollTop;
548
549         // Calculate real X, Y
550         var dx = e.screenX - mcWindows.mouseDownScreenX;
551         var dy = e.screenY - mcWindows.mouseDownScreenY;
552
553         switch (mcWindows.action) {
554                 case "resize":
555                         width = mcWindows.mouseDownWidth + (e.screenX - mcWindows.mouseDownScreenX);
556                         height = mcWindows.mouseDownHeight + (e.screenY - mcWindows.mouseDownScreenY);
557
558                         width = width < 100 ? 100 : width;
559                         height = height < 100 ? 100 : height;
560
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;
567
568                         height = height - this.deltaHeight;
569
570                         this.containerElement.style.width = width;
571
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;
578
579                         mcWindows.cancelEvent(e);
580                         break;
581
582                 case "move":
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";
587
588                         mcWindows.cancelEvent(e);
589                         break;
590         }
591 };
592
593 function debug(msg) {
594         document.getElementById('debug').value += msg + "\n";
595 }
596
597 MCWindow.prototype.onMouseUp = function(e) {
598         mcWindows.action = "none";
599 };
600
601 MCWindow.prototype.onFocus = function(e) {
602         // Gecko only handler
603         var winRef = e.currentTarget;
604
605         for (var n in mcWindows.windows) {
606                 var win = mcWindows.windows[n];
607
608                 if (winRef.name == win.id + "_iframe") {
609                         win.focus();
610                         return;
611                 }
612         }
613 };
614
615 MCWindow.prototype.onMouseDown = function(e) {
616         var elm = mcWindows.isMSIE ? this.wrapperFrame.event.srcElement : e.target;
617
618         var scrollX = 0;//this.doc.body.scrollLeft;
619         var scrollY = 0;//this.doc.body.scrollTop;
620
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);
627
628         if (this.resizeElement != null && elm == this.resizeElement.firstChild)
629                 this.startResize(e);
630         else
631                 this.startMove(e);
632
633         mcWindows.cancelEvent(e);
634 };
635
636 // Global instance
637 var mcWindows = new MCWindows();
638
639 // Initialize windows
640 mcWindows.init({
641         images_path : tinyMCE.baseURL + "/plugins/inlinepopups/images",
642         css_file : tinyMCE.baseURL + "/plugins/inlinepopups/css/inlinepopup.css"
643 });