4 * An altered version of tinyMCEPopup to work in the same window as tinymce.
6 * ------------------------------------------------------------------
8 * Copyright 2009, Moxiecode Systems AB
11 * License: http://tinymce.moxiecode.com/license
12 * Contributing: http://tinymce.moxiecode.com/contributing
15 // Some global instances
18 * TinyMCE popup/dialog helper class. This gives you easy access to the
19 * parent editor instance and a bunch of other things. It's higly recommended
20 * that you load this script into your dialogs.
27 * Initializes the popup this will be called automatically.
38 t.editor = tinymce.EditorManager.activeEditor;
39 t.params = t.editor.windowManager.params;
40 t.features = t.editor.windowManager.features;
43 // Setup on init listeners
46 add : function(f, s) {
47 t.listeners.push({func : f, scope : s});
53 t.editor.windowManager.onOpen.dispatch(t.editor.windowManager, window);
57 * Returns the reference to the parent window that opened the dialog.
60 * @return {Window} Reference to the parent window that opened the dialog.
67 * Returns a window argument/parameter by name.
69 * @method getWindowArg
70 * @param {String} n Name of the window argument to retrieve.
71 * @param {String} dv Optional default value to return.
72 * @return {String} Argument value or default value if it wasn't found.
74 getWindowArg : function(n, dv) {
75 var v = this.params[n];
77 return tinymce.is(v) ? v : dv;
81 * Returns a editor parameter/config option value.
84 * @param {String} n Name of the editor config option to retrieve.
85 * @param {String} dv Optional default value to return.
86 * @return {String} Parameter value or default value if it wasn't found.
88 getParam : function(n, dv) {
89 return this.editor.getParam(n, dv);
93 * Returns a language item by key.
96 * @param {String} n Language item like mydialog.something.
97 * @param {String} dv Optional default value to return.
98 * @return {String} Language value for the item like "my string" or the default value if it wasn't found.
100 getLang : function(n, dv) {
101 return this.editor.getLang(n, dv);
105 * Executed a command on editor that opened the dialog/popup.
107 * @method execCommand
108 * @param {String} cmd Command to execute.
109 * @param {Boolean} ui Optional boolean value if the UI for the command should be presented or not.
110 * @param {Object} val Optional value to pass with the comman like an URL.
111 * @param {Object} a Optional arguments object.
113 execCommand : function(cmd, ui, val, a) {
117 this.restoreSelection();
118 return this.editor.execCommand(cmd, ui, val, a);
122 * Resizes the dialog to the inner size of the window. This is needed since various browsers
123 * have different border sizes on windows.
125 * @method resizeToInnerSize
127 resizeToInnerSize : function() {
130 // Detach it to workaround a Chrome specific bug
131 // https://sourceforge.net/tracker/?func=detail&atid=635682&aid=2926339&group_id=103281
132 setTimeout(function() {
133 var vp = t.dom.getViewPort(window);
135 t.editor.windowManager.resizeBy(
136 t.getWindowArg('mce_width') - vp.w,
137 t.getWindowArg('mce_height') - vp.h,
144 * Will executed the specified string when the page has been loaded. This function
145 * was added for compatibility with the 2.x branch.
147 * @method executeOnLoad
148 * @param {String} s String to evalutate on init.
150 executeOnLoad : function(s) {
151 this.onInit.add(function() {
157 * Stores the current editor selection for later restoration. This can be useful since some browsers
158 * loses its selection if a control element is selected/focused inside the dialogs.
160 * @method storeSelection
162 storeSelection : function() {
163 this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark(1);
167 * Restores any stored selection. This can be useful since some browsers
168 * loses its selection if a control element is selected/focused inside the dialogs.
170 * @method restoreSelection
172 restoreSelection : function() {
173 var t = tinyMCEPopup;
175 if (!t.isWindow && tinymce.isIE)
176 t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
180 * Loads a specific dialog language pack. If you pass in plugin_url as a arugment
181 * when you open the window it will load the <plugin url>/langs/<code>_dlg.js lang pack file.
183 * @method requireLangPack
185 requireLangPack : function() {
186 var t = this, u = t.getWindowArg('plugin_url') || t.getWindowArg('theme_url');
188 if (u && t.editor.settings.language && t.features.translate_i18n !== false) {
189 u += '/langs/' + t.editor.settings.language + '_dlg.js';
191 if (!tinymce.ScriptLoader.isDone(u)) {
192 document.write('<script type="text/javascript" src="' + tinymce._addVer(u) + '"></script>');
193 tinymce.ScriptLoader.markDone(u);
199 * Executes a color picker on the specified element id. When the user
200 * then selects a color it will be set as the value of the specified element.
203 * @param {DOMEvent} e DOM event object.
204 * @param {string} element_id Element id to be filled with the color value from the picker.
206 pickColor : function(e, element_id) {
207 this.execCommand('mceColorPicker', true, {
208 color : document.getElementById(element_id).value,
210 document.getElementById(element_id).value = c;
213 document.getElementById(element_id).onchange();
215 // Try fire event, ignore errors
222 * Opens a filebrowser/imagebrowser this will set the output value from
223 * the browser as a value on the specified element.
225 * @method openBrowser
226 * @param {string} element_id Id of the element to set value in.
227 * @param {string} type Type of browser to open image/file/flash.
228 * @param {string} option Option name to get the file_broswer_callback function name from.
230 openBrowser : function(element_id, type, option) {
231 tinyMCEPopup.restoreSelection();
232 this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
236 * Creates a confirm dialog. Please don't use the blocking behavior of this
237 * native version use the callback method instead then it can be extended.
240 * @param {String} t Title for the new confirm dialog.
241 * @param {function} cb Callback function to be executed after the user has selected ok or cancel.
242 * @param {Object} s Optional scope to execute the callback in.
244 confirm : function(t, cb, s) {
245 this.editor.windowManager.confirm(t, cb, s, window);
249 * Creates an alert dialog. Please don't use the blocking behavior of this
250 * native version use the callback method instead then it can be extended.
253 * @param {String} t Title for the new alert dialog.
254 * @param {function} cb Callback function to be executed after the user has selected ok.
255 * @param {Object} s Optional scope to execute the callback in.
257 alert : function(tx, cb, s) {
258 this.editor.windowManager.alert(tx, cb, s, window);
262 * Closes the current window.
269 // To avoid domain relaxing issue in Opera
271 t.editor.windowManager.close(window);
276 t.getWin().setTimeout(close, 0);
281 // Internal functions
283 _restoreSelection : function() {
284 var e = window.event.srcElement;
286 if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
287 tinyMCEPopup.restoreSelection();
290 /* _restoreSelection : function() {
291 var e = window.event.srcElement;
293 // If user focus a non text input or textarea
294 if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
295 tinyMCEPopup.restoreSelection();
298 _onDOMLoaded : function() {
299 var t = tinyMCEPopup, ti = document.title, bm, h, nv;
309 if (t.features.translate_i18n !== false) {
310 h = document.body.innerHTML;
312 // Replace a=x with a="x" in IE
314 h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"')
316 document.dir = t.editor.getParam('directionality','');
318 if ((nv = t.editor.translate(h)) && nv != h)
319 document.body.innerHTML = nv;
321 if ((nv = t.editor.translate(ti)) && nv != ti)
322 document.title = ti = nv;
325 document.body.style.display = '';
327 // Restore selection in IE when focus is placed on a non textarea or input element of the type text
329 document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
331 // Add base target element for it since it would fail with modal dialogs
332 t.dom.add(t.dom.select('head')[0], 'base', {target : '_self'});
335 t.restoreSelection();
339 t.editor.windowManager.setTitle(window, ti);
343 if (!tinymce.isIE && !t.isWindow) {
344 tinymce.dom.Event._add(document, 'focus', function() {
345 t.editor.windowManager.focus(t.id);
349 // Patch for accessibility
350 tinymce.each(t.dom.select('select'), function(e) {
351 e.onkeydown = tinyMCEPopup._accessHandler;
355 // Init must be called before focus so the selection won't get lost by the focus call
356 tinymce.each(t.listeners, function(o) {
357 o.func.call(o.scope, t.editor);
360 // Move focus to window
361 if (t.getWindowArg('mce_auto_focus', true)) {
364 // Focus element with mceFocus class
365 tinymce.each(document.forms, function(f) {
366 tinymce.each(f.elements, function(e) {
367 if (t.dom.hasClass(e, 'mceFocus') && !e.disabled) {
369 return false; // Break loop
375 document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
378 _accessHandler : function(e) {
379 e = e || window.event;
381 if (e.keyCode == 13 || e.keyCode == 32) {
382 e = e.target || e.srcElement;
387 return tinymce.dom.Event.cancel(e);
391 _closeWinKeyHandler : function(e) {
392 e = e || window.event;
395 tinyMCEPopup.close();
400 if (document.attachEvent) {
401 document.attachEvent("onreadystatechange", function() {
402 if (document.readyState === "complete") {
403 document.detachEvent("onreadystatechange", arguments.callee);
404 tinyMCEPopup._onDOMLoaded();
408 if (document.documentElement.doScroll && window == window.top) {
410 if (tinyMCEPopup.domLoaded)
414 // If IE is used, use the trick by Diego Perini
415 // http://javascript.nwbox.com/IEContentLoaded/
416 document.documentElement.doScroll("left");
418 setTimeout(arguments.callee, 0);
422 tinyMCEPopup._onDOMLoaded();
426 document.attachEvent('onload', tinyMCEPopup._onDOMLoaded);
427 } else if (document.addEventListener) {
428 window.addEventListener('DOMContentLoaded', tinyMCEPopup._onDOMLoaded, false);
429 window.addEventListener('load', tinyMCEPopup._onDOMLoaded, false);