includes/clientside/tinymce/tiny_mce_popup.js
changeset 1 fe660c52c48f
equal deleted inserted replaced
0:902822492a68 1:fe660c52c48f
       
     1 // Some global instances, this will be filled later
       
     2 var tinyMCE = null, tinyMCELang = null;
       
     3 
       
     4 function TinyMCE_Popup() {
       
     5 };
       
     6 
       
     7 TinyMCE_Popup.prototype = {
       
     8 	findWin : function(w) {
       
     9 		var c;
       
    10 
       
    11 		// Check parents
       
    12 		c = w;
       
    13 		while (c && (c = c.parent) != null) {
       
    14 			if (typeof(c.tinyMCE) != "undefined")
       
    15 				return c;
       
    16 		}
       
    17 
       
    18 		// Check openers
       
    19 		c = w;
       
    20 		while (c && (c = c.opener) != null) {
       
    21 			if (typeof(c.tinyMCE) != "undefined")
       
    22 				return c;
       
    23 		}
       
    24 
       
    25 		// Try top
       
    26 		if (typeof(top.tinyMCE) != "undefined")
       
    27 			return top;
       
    28 
       
    29 		return null;
       
    30 	},
       
    31 
       
    32 	init : function() {
       
    33 		var win = window.opener ? window.opener : window.dialogArguments, c;
       
    34 		var inst;
       
    35 
       
    36 		if (!win)
       
    37 			win = this.findWin(window);
       
    38 
       
    39 		if (!win) {
       
    40 			alert("tinyMCE object reference not found from popup.");
       
    41 			return;
       
    42 		}
       
    43 
       
    44 		window.opener = win;
       
    45 		this.windowOpener = win;
       
    46 		this.onLoadEval = "";
       
    47 
       
    48 		// Setup parent references
       
    49 		tinyMCE = win.tinyMCE;
       
    50 		tinyMCELang = win.tinyMCELang;
       
    51 
       
    52 		inst = tinyMCE.selectedInstance;
       
    53 		this.isWindow = tinyMCE.getWindowArg('mce_inside_iframe', false) == false;
       
    54 		this.storeSelection = (tinyMCE.isRealIE) && !this.isWindow && tinyMCE.getWindowArg('mce_store_selection', true);
       
    55 
       
    56 		if (this.isWindow)
       
    57 			window.focus();
       
    58 
       
    59 		// Store selection
       
    60 		if (this.storeSelection)
       
    61 			inst.selectionBookmark = inst.selection.getBookmark(true);
       
    62 
       
    63 		// Setup dir
       
    64 		if (tinyMCELang['lang_dir'])
       
    65 			document.dir = tinyMCELang['lang_dir'];
       
    66 
       
    67 		// Setup title
       
    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 		}
       
   109 
       
   110 		if (body.style.display == 'none')
       
   111 			body.style.display = 'block';
       
   112 
       
   113 		// Execute real onload (Opera fix)
       
   114 		if (tinyMCEPopup.onLoadEval != "")
       
   115 			eval(tinyMCEPopup.onLoadEval);
       
   116 	},
       
   117 
       
   118 	executeOnLoad : function(str) {
       
   119 		if (tinyMCE.isOpera)
       
   120 			this.onLoadEval = str;
       
   121 		else
       
   122 			eval(str);
       
   123 	},
       
   124 
       
   125 	resizeToInnerSize : function() {
       
   126 		// Netscape 7.1 workaround
       
   127 		if (this.isWindow && tinyMCE.isNS71) {
       
   128 			window.resizeBy(0, 10);
       
   129 			return;
       
   130 		}
       
   131 
       
   132 		if (this.isWindow) {
       
   133 			var doc = document;
       
   134 			var body = doc.body;
       
   135 			var oldMargin, wrapper, iframe, nodes, dx, dy;
       
   136 
       
   137 			if (body.style.display == 'none')
       
   138 				body.style.display = 'block';
       
   139 
       
   140 			// Remove margin
       
   141 			oldMargin = body.style.margin;
       
   142 			body.style.margin = '0';
       
   143 
       
   144 			// Create wrapper
       
   145 			wrapper = doc.createElement("div");
       
   146 			wrapper.id = 'mcBodyWrapper';
       
   147 			wrapper.style.display = 'none';
       
   148 			wrapper.style.margin = '0';
       
   149 
       
   150 			// Wrap body elements
       
   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));
       
   157 
       
   158 				nodes[i].parentNode.removeChild(nodes[i]);
       
   159 			}
       
   160 
       
   161 			// Add wrapper
       
   162 			doc.body.appendChild(wrapper);
       
   163 
       
   164 			// Create iframe
       
   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';
       
   171 
       
   172 			// Add iframe
       
   173 			doc.body.appendChild(iframe);
       
   174 
       
   175 			// Measure iframe
       
   176 			iframe = document.getElementById('mcWinIframe');
       
   177 			dx = tinyMCE.getWindowArg('mce_width') - iframe.clientWidth;
       
   178 			dy = tinyMCE.getWindowArg('mce_height') - iframe.clientHeight;
       
   179 
       
   180 			// Resize window
       
   181 			// tinyMCE.debug(tinyMCE.getWindowArg('mce_width') + "," + tinyMCE.getWindowArg('mce_height') + " - " + dx + "," + dy);
       
   182 			window.resizeBy(dx, dy);
       
   183 
       
   184 			// Hide iframe and show wrapper
       
   185 			body.style.margin = oldMargin;
       
   186 			iframe.style.display = 'none';
       
   187 			wrapper.style.display = 'block';
       
   188 		}
       
   189 	},
       
   190 
       
   191 	resizeToContent : function() {
       
   192 		var isMSIE = (navigator.appName == "Microsoft Internet Explorer");
       
   193 		var isOpera = (navigator.userAgent.indexOf("Opera") != -1);
       
   194 
       
   195 		if (isOpera)
       
   196 			return;
       
   197 
       
   198 		if (isMSIE) {
       
   199 			try { window.resizeTo(10, 10); } catch (e) {}
       
   200 
       
   201 			var elm = document.body;
       
   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) {
       
   237 		var inst = tinyMCE.selectedInstance;
       
   238 
       
   239 		this.restoreSelection();
       
   240 		inst.execCommand(command, user_interface, value);
       
   241 
       
   242 		// Store selection
       
   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;
       
   281 	}
       
   282 
       
   283 	};
       
   284 
       
   285 // Setup global instance
       
   286 var tinyMCEPopup = new TinyMCE_Popup();
       
   287 
       
   288 tinyMCEPopup.init();