* Removed the Kevin Roth rich text editor and replaced it with TinyMCE.
[citadel.git] / webcit / tiny_mce / tiny_mce_popup.js
1 /**
2  * $RCSfile$
3  * $Revision$
4  * $Date$
5  *
6  * @author Moxiecode
7  * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
8  */
9
10 var tinyMCE = null, tinyMCELang = null;
11
12 function TinyMCEPopup() {
13 };
14
15 TinyMCEPopup.prototype.init = function() {
16         var win = window.opener ? window.opener : window.dialogArguments;
17
18         if (!win)
19                 win = top;
20
21         window.opener = win;
22         this.windowOpener = win;
23
24         // Setup parent references
25         tinyMCE = win.tinyMCE;
26         tinyMCELang = win.tinyMCELang;
27
28         if (!tinyMCE) {
29                 alert("tinyMCE object reference not found from popup.");
30                 return;
31         }
32
33         this.isWindow = tinyMCE.getWindowArg('mce_inside_iframe', false) == false;
34         this.storeSelection = tinyMCE.isMSIE && !this.isWindow && tinyMCE.getWindowArg('mce_store_selection', true);
35
36         // Store selection
37         if (this.storeSelection)
38                 tinyMCE.selectedInstance.execCommand('mceStoreSelection');
39
40         // Setup dir
41         if (tinyMCELang['lang_dir'])
42                 document.dir = tinyMCELang['lang_dir'];
43
44         // Setup title
45         var re = new RegExp('{|\\\$|}', 'g');
46         var title = document.title.replace(re, "");
47         if (typeof tinyMCELang[title] != "undefined") {
48                 var divElm = document.createElement("div");
49                 divElm.innerHTML = tinyMCELang[title];
50                 document.title = divElm.innerHTML;
51
52                 if (tinyMCE.setWindowTitle != null)
53                         tinyMCE.setWindowTitle(window, divElm.innerHTML);
54         }
55
56         // Output Popup CSS class
57         document.write('<link href="' + tinyMCE.getParam("popups_css") + '" rel="stylesheet" type="text/css">');
58
59         tinyMCE.addEvent(window, "load", this.onLoad);
60 };
61
62 TinyMCEPopup.prototype.onLoad = function() {
63         var body = document.body;
64
65         if (tinyMCE.getWindowArg('mce_replacevariables', true))
66                 body.innerHTML = tinyMCE.applyTemplate(body.innerHTML, tinyMCE.windowArgs);
67
68         var dir = tinyMCE.selectedInstance.settings['directionality'];
69         if (dir == "rtl") {
70                 var elms = document.forms[0].elements;
71                 for (var i=0; i<elms.length; i++) {
72                         if ((elms[i].type == "text" || elms[i].type == "textarea") && elms[i].getAttribute("dir") != "ltr")
73                                 elms[i].dir = dir;
74                 }
75         }
76
77         if (body.style.display == 'none')
78                 body.style.display = 'block';
79 };
80
81 TinyMCEPopup.prototype.resizeToInnerSize = function() {
82         if (this.isWindow) {
83                 var doc = document;
84                 var body = doc.body;
85                 var oldMargin, wrapper, iframe, nodes, dx, dy;
86
87                 if (body.style.display == 'none')
88                         body.style.display = 'block';
89
90                 // Remove margin
91                 oldMargin = body.style.margin;
92                 body.style.margin = '0px';
93
94                 // Create wrapper
95                 wrapper = doc.createElement("div");
96                 wrapper.id = 'mcBodyWrapper';
97                 wrapper.style.display = 'none';
98                 wrapper.style.margin = '0px';
99
100                 // Wrap body elements
101                 nodes = doc.body.childNodes;
102                 for (var i=nodes.length-1; i>=0; i--) {
103                         if (wrapper.hasChildNodes())
104                                 wrapper.insertBefore(nodes[i].cloneNode(true), wrapper.firstChild);
105                         else
106                                 wrapper.appendChild(nodes[i].cloneNode(true));
107
108                         nodes[i].parentNode.removeChild(nodes[i]);
109                 }
110
111                 // Add wrapper
112                 doc.body.appendChild(wrapper);
113
114                 // Create iframe
115                 iframe = document.createElement("iframe");
116                 iframe.id = "mcWinIframe";
117                 iframe.src = "about:blank";
118                 iframe.width = "100%";
119                 iframe.height = "100%";
120                 iframe.style.margin = '0px';
121
122                 // Add iframe
123                 doc.body.appendChild(iframe);
124
125                 // Measure iframe
126                 iframe = document.getElementById('mcWinIframe');
127                 dx = tinyMCE.getWindowArg('mce_width') - iframe.clientWidth;
128                 dy = tinyMCE.getWindowArg('mce_height') - iframe.clientHeight;
129
130                 // Resize window
131                 // tinyMCE.debug(tinyMCE.getWindowArg('mce_width') + "," + tinyMCE.getWindowArg('mce_height') + " - " + dx + "," + dy);
132                 window.resizeBy(dx, dy);
133
134                 // Hide iframe and show wrapper
135                 body.style.margin = oldMargin;
136                 iframe.style.display = 'none';
137                 wrapper.style.display = 'block';
138         }
139 };
140
141 TinyMCEPopup.prototype.resizeToContent = function() {
142         var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
143         var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
144
145         if (isOpera)
146                 return;
147
148         if (isMSIE) {
149                 try { window.resizeTo(10, 10); } catch (e) {}
150
151                 var elm = document.body;
152                 var width = elm.offsetWidth;
153                 var height = elm.offsetHeight;
154                 var dx = (elm.scrollWidth - width) + 4;
155                 var dy = elm.scrollHeight - height;
156
157                 try { window.resizeBy(dx, dy); } catch (e) {}
158         } else {
159                 window.scrollBy(1000, 1000);
160                 if (window.scrollX > 0 || window.scrollY > 0) {
161                         window.resizeBy(window.innerWidth * 2, window.innerHeight * 2);
162                         window.sizeToContent();
163                         window.scrollTo(0, 0);
164                         var x = parseInt(screen.width / 2.0) - (window.outerWidth / 2.0);
165                         var y = parseInt(screen.height / 2.0) - (window.outerHeight / 2.0);
166                         window.moveTo(x, y);
167                 }
168         }
169 };
170
171 TinyMCEPopup.prototype.getWindowArg = function(name, default_value) {
172         return tinyMCE.getWindowArg(name, default_value);
173 };
174
175 TinyMCEPopup.prototype.execCommand = function(command, user_interface, value) {
176         var inst = tinyMCE.selectedInstance;
177
178         // Restore selection
179         if (this.storeSelection) {
180                 inst.getWin().focus();
181                 inst.execCommand('mceRestoreSelection');
182         }
183
184         inst.execCommand(command, user_interface, value);
185
186         // Store selection
187         if (this.storeSelection)
188                 inst.execCommand('mceStoreSelection');
189 };
190
191 TinyMCEPopup.prototype.close = function() {
192         tinyMCE.closeWindow(window);
193 };
194
195 TinyMCEPopup.prototype.pickColor = function(e, element_id) {
196         tinyMCE.selectedInstance.execCommand('mceColorPicker', true, {
197                 element_id : element_id,
198                 document : document,
199                 window : window,
200                 store_selection : false
201         });
202 };
203
204 TinyMCEPopup.prototype.openBrowser = function(element_id, type, option) {
205         var cb = tinyMCE.getParam(option, tinyMCE.getParam("file_browser_callback"));
206         var url = document.getElementById(element_id).value;
207
208         tinyMCE.setWindowArg("window", window);
209         tinyMCE.setWindowArg("document", document);
210
211         // Call to external callback
212         if (eval('typeof(tinyMCEPopup.windowOpener.' + cb + ')') == "undefined")
213                 alert("Callback function: " + cb + " could not be found.");
214         else
215                 eval("tinyMCEPopup.windowOpener." + cb + "(element_id, url, type, window);");
216 };
217
218 // Setup global instance
219 var tinyMCEPopup = new TinyMCEPopup();
220
221 tinyMCEPopup.init();