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