2 * $RCSfile: tiny_mce_popup.js,v $
4 * $Date: 2005/10/29 19:13:20 $
7 * @copyright Copyright © 2004, Moxiecode Systems AB, All rights reserved.
10 var tinyMCE = null, tinyMCELang = null;
12 function TinyMCEPopup() {
15 TinyMCEPopup.prototype.init = function() {
16 var win = window.opener ? window.opener : window.dialogArguments;
22 this.windowOpener = win;
25 // Setup parent references
26 tinyMCE = win.tinyMCE;
27 tinyMCELang = win.tinyMCELang;
30 alert("tinyMCE object reference not found from popup.");
34 this.isWindow = tinyMCE.getWindowArg('mce_inside_iframe', false) == false;
35 this.storeSelection = tinyMCE.isMSIE && !this.isWindow && tinyMCE.getWindowArg('mce_store_selection', true);
41 if (this.storeSelection)
42 tinyMCE.selectedInstance.execCommand('mceStoreSelection');
45 if (tinyMCELang['lang_dir'])
46 document.dir = tinyMCELang['lang_dir'];
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;
56 if (tinyMCE.setWindowTitle != null)
57 tinyMCE.setWindowTitle(window, divElm.innerHTML);
60 // Output Popup CSS class
61 document.write('<link href="' + tinyMCE.getParam("popups_css") + '" rel="stylesheet" type="text/css">');
63 tinyMCE.addEvent(window, "load", this.onLoad);
66 TinyMCEPopup.prototype.onLoad = function() {
67 var body = document.body;
69 body.onkeydown = function (e) {
70 e = e ? e : window.event;
71 if ( e.keyCode == 27 && !e.shiftKey && !e.controlKey && !e.altKey ) {
76 if (tinyMCE.getWindowArg('mce_replacevariables', true))
77 body.innerHTML = tinyMCE.applyTemplate(body.innerHTML, tinyMCE.windowArgs);
79 var dir = tinyMCE.selectedInstance.settings['directionality'];
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")
88 if (body.style.display == 'none')
89 body.style.display = 'block';
91 // Execute real onload (Opera fix)
92 if (tinyMCEPopup.onLoadEval != "") {
93 eval(tinyMCEPopup.onLoadEval);
97 TinyMCEPopup.prototype.executeOnLoad = function(str) {
99 this.onLoadEval = str;
104 TinyMCEPopup.prototype.resizeToInnerSize = function() {
105 // Netscape 7.1 workaround
106 if (this.isWindow && tinyMCE.isNS71) {
107 window.resizeBy(0, 10);
114 var oldMargin, wrapper, iframe, nodes, dx, dy;
116 if (body.style.display == 'none')
117 body.style.display = 'block';
120 oldMargin = body.style.margin;
121 body.style.margin = '0px';
124 wrapper = doc.createElement("div");
125 wrapper.id = 'mcBodyWrapper';
126 wrapper.style.display = 'none';
127 wrapper.style.margin = '0px';
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);
135 wrapper.appendChild(nodes[i].cloneNode(true));
137 nodes[i].parentNode.removeChild(nodes[i]);
141 doc.body.appendChild(wrapper);
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';
152 doc.body.appendChild(iframe);
155 iframe = document.getElementById('mcWinIframe');
156 dx = tinyMCE.getWindowArg('mce_width') - iframe.clientWidth;
157 dy = tinyMCE.getWindowArg('mce_height') - iframe.clientHeight;
160 // tinyMCE.debug(tinyMCE.getWindowArg('mce_width') + "," + tinyMCE.getWindowArg('mce_height') + " - " + dx + "," + dy);
161 window.resizeBy(dx, dy);
163 // Hide iframe and show wrapper
164 body.style.margin = oldMargin;
165 iframe.style.display = 'none';
166 wrapper.style.display = 'block';
170 TinyMCEPopup.prototype.resizeToContent = function() {
171 var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
172 var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
178 try { window.resizeTo(10, 10); } catch (e) {}
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;
186 try { window.resizeBy(dx, dy); } catch (e) {}
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);
200 TinyMCEPopup.prototype.getWindowArg = function(name, default_value) {
201 return tinyMCE.getWindowArg(name, default_value);
204 TinyMCEPopup.prototype.execCommand = function(command, user_interface, value) {
205 var inst = tinyMCE.selectedInstance;
208 if (this.storeSelection) {
209 inst.getWin().focus();
210 inst.execCommand('mceRestoreSelection');
213 inst.execCommand(command, user_interface, value);
216 if (this.storeSelection)
217 inst.execCommand('mceStoreSelection');
220 TinyMCEPopup.prototype.close = function() {
221 tinyMCE.closeWindow(window);
224 TinyMCEPopup.prototype.pickColor = function(e, element_id) {
225 tinyMCE.selectedInstance.execCommand('mceColorPicker', true, {
226 element_id : element_id,
229 store_selection : false
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;
237 tinyMCE.setWindowArg("window", window);
238 tinyMCE.setWindowArg("document", document);
240 // Call to external callback
241 if (eval('typeof(tinyMCEPopup.windowOpener.' + cb + ')') == "undefined")
242 alert("Callback function: " + cb + " could not be found.");
244 eval("tinyMCEPopup.windowOpener." + cb + "(element_id, url, type, window);");
247 // Setup global instance
248 var tinyMCEPopup = new TinyMCEPopup();