yllar
2006-12-16 77c28206a14b5bee3f3091f10cffd531bce5649c
commit | author | age
a0109c 1 // Some global instances, this will be filled later
S 2 var tinyMCE = null, tinyMCELang = null;
3
4 function TinyMCE_Popup() {
5 };
6
f0ea59 7 TinyMCE_Popup.prototype = {
S 8     findWin : function(w) {
9         var c;
a0109c 10
f0ea59 11         // Check parents
S 12         c = w;
13         while (c && (c = c.parent) != null) {
14             if (typeof(c.tinyMCE) != "undefined")
15                 return c;
16         }
a0109c 17
f0ea59 18         // Check openers
S 19         c = w;
20         while (c && (c = c.opener) != null) {
21             if (typeof(c.tinyMCE) != "undefined")
22                 return c;
23         }
a0109c 24
S 25         // Try top
f0ea59 26         if (typeof(top.tinyMCE) != "undefined")
S 27             return top;
a0109c 28
f0ea59 29         return null;
S 30     },
a0109c 31
f0ea59 32     init : function() {
S 33         var win = window.opener ? window.opener : window.dialogArguments, c;
34         var inst;
a0109c 35
f0ea59 36         if (!win)
S 37             win = this.findWin(window);
a0109c 38
f0ea59 39         if (!win) {
S 40             alert("tinyMCE object reference not found from popup.");
41             return;
a0109c 42         }
S 43
f0ea59 44         window.opener = win;
S 45         this.windowOpener = win;
46         this.onLoadEval = "";
a0109c 47
f0ea59 48         // Setup parent references
S 49         tinyMCE = win.tinyMCE;
50         tinyMCELang = win.tinyMCELang;
a0109c 51
f0ea59 52         inst = tinyMCE.selectedInstance;
S 53         this.isWindow = tinyMCE.getWindowArg('mce_inside_iframe', false) == false;
54         this.storeSelection = (tinyMCE.isRealIE) && !this.isWindow && tinyMCE.getWindowArg('mce_store_selection', true);
a0109c 55
f0ea59 56         if (this.isWindow)
S 57             window.focus();
a0109c 58
f0ea59 59         // Store selection
S 60         if (this.storeSelection)
61             inst.selectionBookmark = inst.selection.getBookmark(true);
a0109c 62
f0ea59 63         // Setup dir
S 64         if (tinyMCELang['lang_dir'])
65             document.dir = tinyMCELang['lang_dir'];
a0109c 66
f0ea59 67         // Setup title
S 68         var re = new RegExp('{|\\\$|}', 'g');
69         var title = document.title.replace(re, "");
70         if (typeof tinyMCELang[title] != "undefined") {
71             var divElm = document.createElement("div");
72             divElm.innerHTML = tinyMCELang[title];
73             document.title = divElm.innerHTML;
74
75             if (tinyMCE.setWindowTitle != null)
76                 tinyMCE.setWindowTitle(window, divElm.innerHTML);
77         }
78
79         // Output Popup CSS class
80         document.write('<link href="' + tinyMCE.getParam("popups_css") + '" rel="stylesheet" type="text/css">');
81
82         if (tinyMCE.getParam("popups_css_add")) {
83             c = tinyMCE.getParam("popups_css_add");
84
85             // Is relative
86             if (c.indexOf('://') == -1 && c.charAt(0) != '/')
87                 c = tinyMCE.documentBasePath + "/" + c;
88
89             document.write('<link href="' + c + '" rel="stylesheet" type="text/css">');
90         }
91
92         tinyMCE.addEvent(window, "load", this.onLoad);
93     },
94
95     onLoad : function() {
96         var dir, i, elms, body = document.body;
97
98         if (tinyMCE.getWindowArg('mce_replacevariables', true))
99             body.innerHTML = tinyMCE.applyTemplate(body.innerHTML, tinyMCE.windowArgs);
100
101         dir = tinyMCE.selectedInstance.settings['directionality'];
102         if (dir == "rtl" && document.forms && document.forms.length > 0) {
103             elms = document.forms[0].elements;
104             for (i=0; i<elms.length; i++) {
105                 if ((elms[i].type == "text" || elms[i].type == "textarea") && elms[i].getAttribute("dir") != "ltr")
106                     elms[i].dir = dir;
107             }
108         }
a0109c 109
S 110         if (body.style.display == 'none')
111             body.style.display = 'block';
112
f0ea59 113         // Execute real onload (Opera fix)
S 114         if (tinyMCEPopup.onLoadEval != "")
115             eval(tinyMCEPopup.onLoadEval);
116     },
a0109c 117
f0ea59 118     executeOnLoad : function(str) {
S 119         if (tinyMCE.isOpera)
120             this.onLoadEval = str;
121         else
122             eval(str);
123     },
a0109c 124
f0ea59 125     resizeToInnerSize : function() {
S 126         // Netscape 7.1 workaround
127         if (this.isWindow && tinyMCE.isNS71) {
128             window.resizeBy(0, 10);
129             return;
a0109c 130         }
S 131
f0ea59 132         if (this.isWindow) {
S 133             var doc = document;
134             var body = doc.body;
135             var oldMargin, wrapper, iframe, nodes, dx, dy;
a0109c 136
f0ea59 137             if (body.style.display == 'none')
S 138                 body.style.display = 'block';
a0109c 139
f0ea59 140             // Remove margin
S 141             oldMargin = body.style.margin;
142             body.style.margin = '0';
a0109c 143
f0ea59 144             // Create wrapper
S 145             wrapper = doc.createElement("div");
146             wrapper.id = 'mcBodyWrapper';
147             wrapper.style.display = 'none';
148             wrapper.style.margin = '0';
a0109c 149
f0ea59 150             // Wrap body elements
S 151             nodes = doc.body.childNodes;
152             for (var i=nodes.length-1; i>=0; i--) {
153                 if (wrapper.hasChildNodes())
154                     wrapper.insertBefore(nodes[i].cloneNode(true), wrapper.firstChild);
155                 else
156                     wrapper.appendChild(nodes[i].cloneNode(true));
a0109c 157
f0ea59 158                 nodes[i].parentNode.removeChild(nodes[i]);
S 159             }
a0109c 160
f0ea59 161             // Add wrapper
S 162             doc.body.appendChild(wrapper);
a0109c 163
f0ea59 164             // Create iframe
S 165             iframe = document.createElement("iframe");
166             iframe.id = "mcWinIframe";
167             iframe.src = document.location.href.toLowerCase().indexOf('https') == -1 ? "about:blank" : tinyMCE.settings['default_document'];
168             iframe.width = "100%";
169             iframe.height = "100%";
170             iframe.style.margin = '0';
a0109c 171
f0ea59 172             // Add iframe
S 173             doc.body.appendChild(iframe);
a0109c 174
f0ea59 175             // Measure iframe
S 176             iframe = document.getElementById('mcWinIframe');
177             dx = tinyMCE.getWindowArg('mce_width') - iframe.clientWidth;
178             dy = tinyMCE.getWindowArg('mce_height') - iframe.clientHeight;
a0109c 179
f0ea59 180             // Resize window
S 181             // tinyMCE.debug(tinyMCE.getWindowArg('mce_width') + "," + tinyMCE.getWindowArg('mce_height') + " - " + dx + "," + dy);
182             window.resizeBy(dx, dy);
a0109c 183
f0ea59 184             // Hide iframe and show wrapper
S 185             body.style.margin = oldMargin;
186             iframe.style.display = 'none';
187             wrapper.style.display = 'block';
a0109c 188         }
f0ea59 189     },
a0109c 190
f0ea59 191     resizeToContent : function() {
S 192         var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
193         var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
a0109c 194
f0ea59 195         if (isOpera)
S 196             return;
a0109c 197
f0ea59 198         if (isMSIE) {
S 199             try { window.resizeTo(10, 10); } catch (e) {}
a0109c 200
f0ea59 201             var elm = document.body;
S 202             var width = elm.offsetWidth;
203             var height = elm.offsetHeight;
204             var dx = (elm.scrollWidth - width) + 4;
205             var dy = elm.scrollHeight - height;
206
207             try { window.resizeBy(dx, dy); } catch (e) {}
208         } else {
209             window.scrollBy(1000, 1000);
210             if (window.scrollX > 0 || window.scrollY > 0) {
211                 window.resizeBy(window.innerWidth * 2, window.innerHeight * 2);
212                 window.sizeToContent();
213                 window.scrollTo(0, 0);
214                 var x = parseInt(screen.width / 2.0) - (window.outerWidth / 2.0);
215                 var y = parseInt(screen.height / 2.0) - (window.outerHeight / 2.0);
216                 window.moveTo(x, y);
217             }
218         }
219     },
220
221     getWindowArg : function(name, default_value) {
222         return tinyMCE.getWindowArg(name, default_value);
223     },
224
225     restoreSelection : function() {
226         if (this.storeSelection) {
227             var inst = tinyMCE.selectedInstance;
228
229             inst.getWin().focus();
230
231             if (inst.selectionBookmark)
232                 inst.selection.moveToBookmark(inst.selectionBookmark);
233         }
234     },
235
236     execCommand : function(command, user_interface, value) {
a0109c 237         var inst = tinyMCE.selectedInstance;
S 238
f0ea59 239         this.restoreSelection();
S 240         inst.execCommand(command, user_interface, value);
a0109c 241
f0ea59 242         // Store selection
S 243         if (this.storeSelection)
244             inst.selectionBookmark = inst.selection.getBookmark(true);
245     },
246
247     close : function() {
248         tinyMCE.closeWindow(window);
249     },
250
251     pickColor : function(e, element_id) {
252         tinyMCE.selectedInstance.execCommand('mceColorPicker', true, {
253             element_id : element_id,
254             document : document,
255             window : window,
256             store_selection : false
257         });
258     },
259
260     openBrowser : function(element_id, type, option) {
261         var cb = tinyMCE.getParam(option, tinyMCE.getParam("file_browser_callback"));
262         var url = document.getElementById(element_id).value;
263
264         tinyMCE.setWindowArg("window", window);
265         tinyMCE.setWindowArg("document", document);
266
267         // Call to external callback
268         if (eval('typeof(tinyMCEPopup.windowOpener.' + cb + ')') == "undefined")
269             alert("Callback function: " + cb + " could not be found.");
270         else
271             eval("tinyMCEPopup.windowOpener." + cb + "(element_id, url, type, window);");
272     },
273
274     importClass : function(c) {
275         window[c] = function() {};
276
277         for (var n in window.opener[c].prototype)
278             window[c].prototype[n] = window.opener[c].prototype[n];
279
280         window[c].constructor = window.opener[c].constructor;
a0109c 281     }
S 282
f0ea59 283     };
a0109c 284
S 285 // Setup global instance
286 var tinyMCEPopup = new TinyMCE_Popup();
287
288 tinyMCEPopup.init();