includes/clientside/tinymce/tiny_mce_popup.js
changeset 335 67bd3121a12e
parent 1 fe660c52c48f
child 395 fa4c5ecb7c9a
equal deleted inserted replaced
334:c72b545f1304 335:67bd3121a12e
     1 // Some global instances, this will be filled later
     1 // Some global instances
     2 var tinyMCE = null, tinyMCELang = null;
     2 var tinymce = null, tinyMCEPopup, tinyMCE;
     3 
     3 
     4 function TinyMCE_Popup() {
     4 tinyMCEPopup = {
     5 };
     5 	init : function() {
     6 
     6 		var t = this, w = t.getWin(), ti;
     7 TinyMCE_Popup.prototype = {
     7 
     8 	findWin : function(w) {
     8 		// Find API
     9 		var c;
     9 		tinymce = w.tinymce;
    10 
    10 		tinyMCE = w.tinyMCE;
    11 		// Check parents
    11 		t.editor = tinymce.EditorManager.activeEditor;
    12 		c = w;
    12 		t.params = t.editor.windowManager.params;
    13 		while (c && (c = c.parent) != null) {
    13 
    14 			if (typeof(c.tinyMCE) != "undefined")
    14 		// Setup local DOM
    15 				return c;
    15 		t.dom = t.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document);
       
    16 		t.dom.loadCSS(t.editor.settings.popup_css);
       
    17 
       
    18 		// Setup on init listeners
       
    19 		t.listeners = [];
       
    20 		t.onInit = {
       
    21 			add : function(f, s) {
       
    22 				t.listeners.push({func : f, scope : s});
       
    23 			}
       
    24 		};
       
    25 
       
    26 		t.isWindow = !t.getWindowArg('mce_inline');
       
    27 		t.id = t.getWindowArg('mce_window_id');
       
    28 		t.editor.windowManager.onOpen.dispatch(t.editor.windowManager, window);
       
    29 	},
       
    30 
       
    31 	getWin : function() {
       
    32 		return window.dialogArguments || opener || parent || top;
       
    33 	},
       
    34 
       
    35 	getWindowArg : function(n, dv) {
       
    36 		var v = this.params[n];
       
    37 
       
    38 		return tinymce.is(v) ? v : dv;
       
    39 	},
       
    40 
       
    41 	getParam : function(n, dv) {
       
    42 		return this.editor.getParam(n, dv);
       
    43 	},
       
    44 
       
    45 	getLang : function(n, dv) {
       
    46 		return this.editor.getLang(n, dv);
       
    47 	},
       
    48 
       
    49 	execCommand : function(cmd, ui, val) {
       
    50 		this.restoreSelection();
       
    51 		return this.editor.execCommand(cmd, ui, val);
       
    52 	},
       
    53 
       
    54 	resizeToInnerSize : function() {
       
    55 		var t = this, n, b = document.body, vp = t.dom.getViewPort(window), dw, dh;
       
    56 
       
    57 		dw = t.getWindowArg('mce_width') - vp.w;
       
    58 		dh = t.getWindowArg('mce_height') - vp.h;
       
    59 
       
    60 		if (t.isWindow)
       
    61 			window.resizeBy(dw, dh);
       
    62 		else
       
    63 			t.editor.windowManager.resizeBy(dw, dh, t.id);
       
    64 	},
       
    65 
       
    66 	executeOnLoad : function(s) {
       
    67 		this.onInit.add(function() {
       
    68 			eval(s);
       
    69 		});
       
    70 	},
       
    71 
       
    72 	storeSelection : function() {
       
    73 		this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark('simple');
       
    74 	},
       
    75 
       
    76 	restoreSelection : function() {
       
    77 		var t = tinyMCEPopup;
       
    78 
       
    79 		if (!t.isWindow && tinymce.isIE)
       
    80 			t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
       
    81 	},
       
    82 
       
    83 	requireLangPack : function() {
       
    84 		var u = this.getWindowArg('plugin_url') || this.getWindowArg('theme_url');
       
    85 
       
    86 		if (u)
       
    87 			document.write('<script type="text/javascript" src="' + u + '/langs/' + this.editor.settings.language + '_dlg.js' + '"></script>');
       
    88 	},
       
    89 
       
    90 	pickColor : function(e, element_id) {
       
    91 		this.execCommand('mceColorPicker', true, {
       
    92 			color : document.getElementById(element_id).value,
       
    93 			func : function(c) {
       
    94 				document.getElementById(element_id).value = c;
       
    95 
       
    96 				if (tinymce.is(document.getElementById(element_id).onchange, 'function'))
       
    97 					document.getElementById(element_id).onchange();
       
    98 			}
       
    99 		});
       
   100 	},
       
   101 
       
   102 	openBrowser : function(element_id, type, option) {
       
   103 		tinyMCEPopup.restoreSelection();
       
   104 		this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
       
   105 	},
       
   106 
       
   107 	close : function() {
       
   108 		var t = this;
       
   109 
       
   110 		t.dom = t.dom.doc = null; // Cleanup
       
   111 		t.editor.windowManager.close(window, t.id);
       
   112 	},
       
   113 
       
   114 	// Internal functions	
       
   115 
       
   116 	_restoreSelection : function() {
       
   117 		var e = window.event.srcElement;
       
   118 
       
   119 		if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
       
   120 			tinyMCEPopup.restoreSelection();
       
   121 	},
       
   122 
       
   123 /*	_restoreSelection : function() {
       
   124 		var e = window.event.srcElement;
       
   125 
       
   126 		// If user focus a non text input or textarea
       
   127 		if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
       
   128 			tinyMCEPopup.restoreSelection();
       
   129 	},*/
       
   130 
       
   131 	_onDOMLoaded : function() {
       
   132 		var t = this, ti = document.title, bm, h;
       
   133 
       
   134 		// Translate page
       
   135 		h = document.body.innerHTML;
       
   136 
       
   137 		// Replace a=x with a="x" in IE
       
   138 		if (tinymce.isIE)
       
   139 			h = h.replace(/ (value|title|alt)=([^\s>]+)/gi, ' $1="$2"');
       
   140 
       
   141 		document.body.innerHTML = t.editor.translate(h);
       
   142 		document.title = ti = t.editor.translate(ti);
       
   143 		document.body.style.display = '';
       
   144 
       
   145 		// Restore selection in IE when focus is placed on a non textarea or input element of the type text
       
   146 		if (tinymce.isIE)
       
   147 			document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
       
   148 
       
   149 		t.restoreSelection();
       
   150 
       
   151 		// Call onInit
       
   152 		tinymce.each(t.listeners, function(o) {
       
   153 			o.func.call(o.scope, t.editor);
       
   154 		});
       
   155 
       
   156 		t.resizeToInnerSize();
       
   157 
       
   158 		if (t.isWindow)
       
   159 			window.focus();
       
   160 		else
       
   161 			t.editor.windowManager.setTitle(ti, t.id);
       
   162 
       
   163 		if (!tinymce.isIE && !t.isWindow) {
       
   164 			tinymce.dom.Event._add(document, 'focus', function() {
       
   165 				t.editor.windowManager.focus(t.id)
       
   166 			});
    16 		}
   167 		}
    17 
   168 
    18 		// Check openers
   169 		// Patch for accessibility
    19 		c = w;
   170 		tinymce.each(t.dom.select('select'), function(e) {
    20 		while (c && (c = c.opener) != null) {
   171 			e.onkeydown = tinyMCEPopup._accessHandler;
    21 			if (typeof(c.tinyMCE) != "undefined")
   172 		});
    22 				return c;
   173 
       
   174 		// Move focus to window
       
   175 		window.focus();
       
   176 	},
       
   177 
       
   178 	_accessHandler : function(e) {
       
   179 		var e = e || window.event;
       
   180 
       
   181 		if (e.keyCode == 13 || e.keyCode == 32) {
       
   182 			e = e.target || e.srcElement;
       
   183 
       
   184 			if (e.onchange)
       
   185 				e.onchange();
       
   186 
       
   187 			return tinymce.dom.Event.cancel(e);
    23 		}
   188 		}
    24 
   189 	},
    25 		// Try top
   190 
    26 		if (typeof(top.tinyMCE) != "undefined")
   191 	_wait : function() {
    27 			return top;
   192 		var t = this, ti;
    28 
   193 
    29 		return null;
   194 		if (tinymce.isIE && document.location.protocol != 'https:') {
    30 	},
   195 			// Fake DOMContentLoaded on IE
    31 
   196 			document.write('<script id=__ie_onload defer src=\'javascript:""\';><\/script>');
    32 	init : function() {
   197 			document.getElementById("__ie_onload").onreadystatechange = function() {
    33 		var win = window.opener ? window.opener : window.dialogArguments, c;
   198 				if (this.readyState == "complete") {
    34 		var inst;
   199 					t._onDOMLoaded();
    35 
   200 					document.getElementById("__ie_onload").onreadystatechange = null; // Prevent leak
    36 		if (!win)
   201 				}
    37 			win = this.findWin(window);
   202 			};
    38 
   203 		} else {
    39 		if (!win) {
   204 			if (tinymce.isIE || tinymce.isWebKit) {
    40 			alert("tinyMCE object reference not found from popup.");
   205 				ti = setInterval(function() {
    41 			return;
   206 					if (/loaded|complete/.test(document.readyState)) {
    42 		}
   207 						clearInterval(ti);
    43 
   208 						t._onDOMLoaded();
    44 		window.opener = win;
   209 					}
    45 		this.windowOpener = win;
   210 				}, 10);
    46 		this.onLoadEval = "";
   211 			} else {
    47 
   212 				window.addEventListener('DOMContentLoaded', function() {
    48 		// Setup parent references
   213 					t._onDOMLoaded();
    49 		tinyMCE = win.tinyMCE;
   214 				}, false);
    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 			}
   215 			}
   108 		}
   216 		}
   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 	}
   217 	}
   282 
   218 };
   283 	};
       
   284 
       
   285 // Setup global instance
       
   286 var tinyMCEPopup = new TinyMCE_Popup();
       
   287 
   219 
   288 tinyMCEPopup.init();
   220 tinyMCEPopup.init();
       
   221 tinyMCEPopup._wait(); // Wait for DOM Content Loaded