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