app/tiny_mce/tiny_mce_popup.js
changeset 154 d2377425d3f2
parent 126 6186c115a210
equal deleted inserted replaced
153:79d52c2d50a2 154:d2377425d3f2
       
     1 // Some global instances
       
     2 var tinymce = null, tinyMCEPopup, tinyMCE;
       
     3 
       
     4 tinyMCEPopup = {
       
     5 	init : function() {
       
     6 		var t = this, w, ti, li, q, i, it;
       
     7 
       
     8 		li = ('' + document.location.search).replace(/^\?/, '').split('&');
       
     9 		q = {};
       
    10 		for (i=0; i<li.length; i++) {
       
    11 			it = li[i].split('=');
       
    12 			q[unescape(it[0])] = unescape(it[1]);
       
    13 		}
       
    14 
       
    15 		if (q.mce_rdomain)
       
    16 			document.domain = q.mce_rdomain;
       
    17 
       
    18 		// Find window & API
       
    19 		w = t.getWin();
       
    20 		tinymce = w.tinymce;
       
    21 		tinyMCE = w.tinyMCE;
       
    22 		t.editor = tinymce.EditorManager.activeEditor;
       
    23 		t.params = t.editor.windowManager.params;
       
    24 		t.features = t.editor.windowManager.features;
       
    25 
       
    26 		// Setup local DOM
       
    27 		t.dom = t.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document);
       
    28 		t.dom.loadCSS(t.features.popup_css || t.editor.settings.popup_css);
       
    29 
       
    30 		// Setup on init listeners
       
    31 		t.listeners = [];
       
    32 		t.onInit = {
       
    33 			add : function(f, s) {
       
    34 				t.listeners.push({func : f, scope : s});
       
    35 			}
       
    36 		};
       
    37 
       
    38 		t.isWindow = !t.getWindowArg('mce_inline');
       
    39 		t.id = t.getWindowArg('mce_window_id');
       
    40 		t.editor.windowManager.onOpen.dispatch(t.editor.windowManager, window);
       
    41 	},
       
    42 
       
    43 	getWin : function() {
       
    44 		return window.dialogArguments || opener || parent || top;
       
    45 	},
       
    46 
       
    47 	getWindowArg : function(n, dv) {
       
    48 		var v = this.params[n];
       
    49 
       
    50 		return tinymce.is(v) ? v : dv;
       
    51 	},
       
    52 
       
    53 	getParam : function(n, dv) {
       
    54 		return this.editor.getParam(n, dv);
       
    55 	},
       
    56 
       
    57 	getLang : function(n, dv) {
       
    58 		return this.editor.getLang(n, dv);
       
    59 	},
       
    60 
       
    61 	execCommand : function(cmd, ui, val, a) {
       
    62 		a = a || {};
       
    63 		a.skip_focus = 1;
       
    64 
       
    65 		this.restoreSelection();
       
    66 		return this.editor.execCommand(cmd, ui, val, a);
       
    67 	},
       
    68 
       
    69 	resizeToInnerSize : function() {
       
    70 		var t = this, n, b = document.body, vp = t.dom.getViewPort(window), dw, dh;
       
    71 
       
    72 		dw = t.getWindowArg('mce_width') - vp.w;
       
    73 		dh = t.getWindowArg('mce_height') - vp.h;
       
    74 
       
    75 		if (t.isWindow)
       
    76 			window.resizeBy(dw, dh);
       
    77 		else
       
    78 			t.editor.windowManager.resizeBy(dw, dh, t.id);
       
    79 	},
       
    80 
       
    81 	executeOnLoad : function(s) {
       
    82 		this.onInit.add(function() {
       
    83 			eval(s);
       
    84 		});
       
    85 	},
       
    86 
       
    87 	storeSelection : function() {
       
    88 		this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark('simple');
       
    89 	},
       
    90 
       
    91 	restoreSelection : function() {
       
    92 		var t = tinyMCEPopup;
       
    93 
       
    94 		if (!t.isWindow && tinymce.isIE)
       
    95 			t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
       
    96 	},
       
    97 
       
    98 	requireLangPack : function() {
       
    99 		var u = this.getWindowArg('plugin_url') || this.getWindowArg('theme_url');
       
   100 
       
   101 		if (u && this.editor.settings.language) {
       
   102 			u += '/langs/' + this.editor.settings.language + '_dlg.js';
       
   103 
       
   104 			if (!tinymce.ScriptLoader.isDone(u)) {
       
   105 				document.write('<script type="text/javascript" src="' + tinymce._addVer(u) + '"></script>');
       
   106 				tinymce.ScriptLoader.markDone(u);
       
   107 			}
       
   108 		}
       
   109 	},
       
   110 
       
   111 	pickColor : function(e, element_id) {
       
   112 		this.execCommand('mceColorPicker', true, {
       
   113 			color : document.getElementById(element_id).value,
       
   114 			func : function(c) {
       
   115 				document.getElementById(element_id).value = c;
       
   116 
       
   117 				try {
       
   118 					document.getElementById(element_id).onchange();
       
   119 				} catch (ex) {
       
   120 					// Try fire event, ignore errors
       
   121 				}
       
   122 			}
       
   123 		});
       
   124 	},
       
   125 
       
   126 	openBrowser : function(element_id, type, option) {
       
   127 		tinyMCEPopup.restoreSelection();
       
   128 		this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
       
   129 	},
       
   130 
       
   131 	confirm : function(t, cb, s) {
       
   132 		this.editor.windowManager.confirm(t, cb, s, window);
       
   133 	},
       
   134 
       
   135 	alert : function(tx, cb, s) {
       
   136 		this.editor.windowManager.alert(tx, cb, s, window);
       
   137 	},
       
   138 
       
   139 	close : function() {
       
   140 		var t = this;
       
   141 
       
   142 		// To avoid domain relaxing issue in Opera
       
   143 		function close() {
       
   144 			t.editor.windowManager.close(window);
       
   145 			tinymce = tinyMCE = t.editor = t.params = t.dom = t.dom.doc = null; // Cleanup
       
   146 		};
       
   147 
       
   148 		if (tinymce.isOpera)
       
   149 			t.getWin().setTimeout(close, 0);
       
   150 		else
       
   151 			close();
       
   152 	},
       
   153 
       
   154 	// Internal functions	
       
   155 
       
   156 	_restoreSelection : function() {
       
   157 		var e = window.event.srcElement;
       
   158 
       
   159 		if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
       
   160 			tinyMCEPopup.restoreSelection();
       
   161 	},
       
   162 
       
   163 /*	_restoreSelection : function() {
       
   164 		var e = window.event.srcElement;
       
   165 
       
   166 		// If user focus a non text input or textarea
       
   167 		if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
       
   168 			tinyMCEPopup.restoreSelection();
       
   169 	},*/
       
   170 
       
   171 	_onDOMLoaded : function() {
       
   172 		var t = this, ti = document.title, bm, h;
       
   173 
       
   174 		// Translate page
       
   175 		h = document.body.innerHTML;
       
   176 
       
   177 		// Replace a=x with a="x" in IE
       
   178 		if (tinymce.isIE)
       
   179 			h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"')
       
   180 
       
   181 		document.dir = t.editor.getParam('directionality','');
       
   182 		document.body.innerHTML = t.editor.translate(h);
       
   183 		document.title = ti = t.editor.translate(ti);
       
   184 		document.body.style.display = '';
       
   185 
       
   186 		// Restore selection in IE when focus is placed on a non textarea or input element of the type text
       
   187 		if (tinymce.isIE)
       
   188 			document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
       
   189 
       
   190 		t.restoreSelection();
       
   191 		t.resizeToInnerSize();
       
   192 
       
   193 		// Set inline title
       
   194 		if (!t.isWindow)
       
   195 			t.editor.windowManager.setTitle(window, ti);
       
   196 		else
       
   197 			window.focus();
       
   198 
       
   199 		if (!tinymce.isIE && !t.isWindow) {
       
   200 			tinymce.dom.Event._add(document, 'focus', function() {
       
   201 				t.editor.windowManager.focus(t.id)
       
   202 			});
       
   203 		}
       
   204 
       
   205 		// Patch for accessibility
       
   206 		tinymce.each(t.dom.select('select'), function(e) {
       
   207 			e.onkeydown = tinyMCEPopup._accessHandler;
       
   208 		});
       
   209 
       
   210 		// Call onInit
       
   211 		// Init must be called before focus so the selection won't get lost by the focus call
       
   212 		tinymce.each(t.listeners, function(o) {
       
   213 			o.func.call(o.scope, t.editor);
       
   214 		});
       
   215 
       
   216 		// Move focus to window
       
   217 		if (t.getWindowArg('mce_auto_focus', true)) {
       
   218 			window.focus();
       
   219 
       
   220 			// Focus element with mceFocus class
       
   221 			tinymce.each(document.forms, function(f) {
       
   222 				tinymce.each(f.elements, function(e) {
       
   223 					if (t.dom.hasClass(e, 'mceFocus') && !e.disabled) {
       
   224 						e.focus();
       
   225 						return false; // Break loop
       
   226 					}
       
   227 				});
       
   228 			});
       
   229 		}
       
   230 
       
   231 		document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
       
   232 	},
       
   233 
       
   234 	_accessHandler : function(e) {
       
   235 		e = e || window.event;
       
   236 
       
   237 		if (e.keyCode == 13 || e.keyCode == 32) {
       
   238 			e = e.target || e.srcElement;
       
   239 
       
   240 			if (e.onchange)
       
   241 				e.onchange();
       
   242 
       
   243 			return tinymce.dom.Event.cancel(e);
       
   244 		}
       
   245 	},
       
   246 
       
   247 	_closeWinKeyHandler : function(e) {
       
   248 		e = e || window.event;
       
   249 
       
   250 		if (e.keyCode == 27)
       
   251 			tinyMCEPopup.close();
       
   252 	},
       
   253 
       
   254 	_wait : function() {
       
   255 		var t = this, ti;
       
   256 
       
   257 		if (tinymce.isIE && document.location.protocol != 'https:') {
       
   258 			// Fake DOMContentLoaded on IE
       
   259 			document.write('<script id=__ie_onload defer src=\'javascript:""\';><\/script>');
       
   260 			document.getElementById("__ie_onload").onreadystatechange = function() {
       
   261 				if (this.readyState == "complete") {
       
   262 					t._onDOMLoaded();
       
   263 					document.getElementById("__ie_onload").onreadystatechange = null; // Prevent leak
       
   264 				}
       
   265 			};
       
   266 		} else {
       
   267 			if (tinymce.isIE || tinymce.isWebKit) {
       
   268 				ti = setInterval(function() {
       
   269 					if (/loaded|complete/.test(document.readyState)) {
       
   270 						clearInterval(ti);
       
   271 						t._onDOMLoaded();
       
   272 					}
       
   273 				}, 10);
       
   274 			} else {
       
   275 				window.addEventListener('DOMContentLoaded', function() {
       
   276 					t._onDOMLoaded();
       
   277 				}, false);
       
   278 			}
       
   279 		}
       
   280 	}
       
   281 };
       
   282 
       
   283 tinyMCEPopup.init();
       
   284 tinyMCEPopup._wait(); // Wait for DOM Content Loaded