4 * Copyright, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://www.tinymce.com/license
8 * Contributing: http://www.tinymce.com/contributing
11 // Some global instances
12 var tinymce = null, tinyMCEPopup, tinyMCE;
15 * TinyMCE popup/dialog helper class. This gives you easy access to the
16 * parent editor instance and a bunch of other things. It's higly recommended
17 * that you load this script into your dialogs.
24 * Initializes the popup this will be called automatically.
29 var t = this, w, ti, settings;
35 t.editor = tinymce.EditorManager.activeEditor;
36 t.params = t.editor.windowManager.params;
37 t.features = t.editor.windowManager.features;
38 settings = t.editor.settings;
40 // Setup popup CSS path(s)
41 if (settings.popup_css !== false) {
42 if (settings.popup_css) {
43 settings.popup_css = t.documentBaseURI.toAbsolute(settings.popup_css);
45 settings.popup_css = t.baseURI.toAbsolute("themes/" + settings.theme + "/skins/" + settings.skin + "/dialog.css");
49 if (settings.popup_css_add) {
50 settings.popup_css += ',' + t.documentBaseURI.toAbsolute(settings.popup_css_add);
54 t.dom = t.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document, {ownEvents: true, proxy: tinyMCEPopup._eventProxy});
55 t.dom.bind(window, 'ready', t._onDOMLoaded, t);
57 // Enables you to skip loading the default css
58 if (t.features.popup_css !== false)
59 t.dom.loadCSS(t.features.popup_css || t.editor.settings.popup_css);
61 // Setup on init listeners
65 * Fires when the popup is initialized.
68 * @param {tinymce.Editor} editor Editor instance.
70 * // Alerts the selected contents when the dialog is loaded
71 * tinyMCEPopup.onInit.add(function(ed) {
72 * alert(ed.selection.getContent());
75 * // Executes the init method on page load in some object using the SomeObject scope
76 * tinyMCEPopup.onInit.add(SomeObject.init, SomeObject);
79 add : function(f, s) {
80 t.listeners.push({func : f, scope : s});
84 t.isWindow = !t.getWindowArg('mce_inline');
85 t.id = t.getWindowArg('mce_window_id');
89 * Returns the reference to the parent window that opened the dialog.
92 * @return {Window} Reference to the parent window that opened the dialog.
95 // Added frameElement check to fix bug: #2817583
96 return (!window.frameElement && window.dialogArguments) || opener || parent || top;
100 * Returns a window argument/parameter by name.
102 * @method getWindowArg
103 * @param {String} n Name of the window argument to retrive.
104 * @param {String} dv Optional default value to return.
105 * @return {String} Argument value or default value if it wasn't found.
107 getWindowArg : function(n, dv) {
108 var v = this.params[n];
110 return tinymce.is(v) ? v : dv;
114 * Returns a editor parameter/config option value.
117 * @param {String} n Name of the editor config option to retrive.
118 * @param {String} dv Optional default value to return.
119 * @return {String} Parameter value or default value if it wasn't found.
121 getParam : function(n, dv) {
122 return this.editor.getParam(n, dv);
126 * Returns a language item by key.
129 * @param {String} n Language item like mydialog.something.
130 * @param {String} dv Optional default value to return.
131 * @return {String} Language value for the item like "my string" or the default value if it wasn't found.
133 getLang : function(n, dv) {
134 return this.editor.getLang(n, dv);
138 * Executed a command on editor that opened the dialog/popup.
140 * @method execCommand
141 * @param {String} cmd Command to execute.
142 * @param {Boolean} ui Optional boolean value if the UI for the command should be presented or not.
143 * @param {Object} val Optional value to pass with the comman like an URL.
144 * @param {Object} a Optional arguments object.
146 execCommand : function(cmd, ui, val, a) {
150 this.restoreSelection();
151 return this.editor.execCommand(cmd, ui, val, a);
155 * Resizes the dialog to the inner size of the window. This is needed since various browsers
156 * have different border sizes on windows.
158 * @method resizeToInnerSize
160 resizeToInnerSize : function() {
163 // Detach it to workaround a Chrome specific bug
164 // https://sourceforge.net/tracker/?func=detail&atid=635682&aid=2926339&group_id=103281
165 setTimeout(function() {
166 var vp = t.dom.getViewPort(window);
168 t.editor.windowManager.resizeBy(
169 t.getWindowArg('mce_width') - vp.w,
170 t.getWindowArg('mce_height') - vp.h,
177 * Will executed the specified string when the page has been loaded. This function
178 * was added for compatibility with the 2.x branch.
180 * @method executeOnLoad
181 * @param {String} s String to evalutate on init.
183 executeOnLoad : function(s) {
184 this.onInit.add(function() {
190 * Stores the current editor selection for later restoration. This can be useful since some browsers
191 * looses it's selection if a control element is selected/focused inside the dialogs.
193 * @method storeSelection
195 storeSelection : function() {
196 this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark(1);
200 * Restores any stored selection. This can be useful since some browsers
201 * looses it's selection if a control element is selected/focused inside the dialogs.
203 * @method restoreSelection
205 restoreSelection : function() {
206 var t = tinyMCEPopup;
208 if (!t.isWindow && tinymce.isIE)
209 t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
213 * Loads a specific dialog language pack. If you pass in plugin_url as a argument
214 * when you open the window it will load the <plugin url>/langs/<code>_dlg.js lang pack file.
216 * @method requireLangPack
218 requireLangPack : function() {
219 var t = this, u = t.getWindowArg('plugin_url') || t.getWindowArg('theme_url');
221 if (u && t.editor.settings.language && t.features.translate_i18n !== false && t.editor.settings.language_load !== false) {
222 u += '/langs/' + t.editor.settings.language + '_dlg.js';
224 if (!tinymce.ScriptLoader.isDone(u)) {
225 document.write('<script type="text/javascript" src="' + u + '"></script>');
226 tinymce.ScriptLoader.markDone(u);
232 * Executes a color picker on the specified element id. When the user
233 * then selects a color it will be set as the value of the specified element.
236 * @param {DOMEvent} e DOM event object.
237 * @param {string} element_id Element id to be filled with the color value from the picker.
239 pickColor : function(e, element_id) {
240 this.execCommand('mceColorPicker', true, {
241 color : document.getElementById(element_id).value,
243 document.getElementById(element_id).value = c;
246 document.getElementById(element_id).onchange();
248 // Try fire event, ignore errors
255 * Opens a filebrowser/imagebrowser this will set the output value from
256 * the browser as a value on the specified element.
258 * @method openBrowser
259 * @param {string} element_id Id of the element to set value in.
260 * @param {string} type Type of browser to open image/file/flash.
261 * @param {string} option Option name to get the file_broswer_callback function name from.
263 openBrowser : function(element_id, type, option) {
264 tinyMCEPopup.restoreSelection();
265 this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
269 * Creates a confirm dialog. Please don't use the blocking behavior of this
270 * native version use the callback method instead then it can be extended.
273 * @param {String} t Title for the new confirm dialog.
274 * @param {function} cb Callback function to be executed after the user has selected ok or cancel.
275 * @param {Object} s Optional scope to execute the callback in.
277 confirm : function(t, cb, s) {
278 this.editor.windowManager.confirm(t, cb, s, window);
282 * Creates a alert dialog. Please don't use the blocking behavior of this
283 * native version use the callback method instead then it can be extended.
286 * @param {String} t Title for the new alert dialog.
287 * @param {function} cb Callback function to be executed after the user has selected ok.
288 * @param {Object} s Optional scope to execute the callback in.
290 alert : function(tx, cb, s) {
291 this.editor.windowManager.alert(tx, cb, s, window);
295 * Closes the current window.
302 // To avoid domain relaxing issue in Opera
304 t.editor.windowManager.close(window);
305 tinymce = tinyMCE = t.editor = t.params = t.dom = t.dom.doc = null; // Cleanup
309 t.getWin().setTimeout(close, 0);
314 // Internal functions
316 _restoreSelection : function() {
317 var e = window.event.srcElement;
319 if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
320 tinyMCEPopup.restoreSelection();
323 /* _restoreSelection : function() {
324 var e = window.event.srcElement;
326 // If user focus a non text input or textarea
327 if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
328 tinyMCEPopup.restoreSelection();
331 _onDOMLoaded : function() {
332 var t = tinyMCEPopup, ti = document.title, bm, h, nv;
335 if (t.features.translate_i18n !== false) {
336 h = document.body.innerHTML;
338 // Replace a=x with a="x" in IE
340 h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"')
342 document.dir = t.editor.getParam('directionality','');
344 if ((nv = t.editor.translate(h)) && nv != h)
345 document.body.innerHTML = nv;
347 if ((nv = t.editor.translate(ti)) && nv != ti)
348 document.title = ti = nv;
351 if (!t.editor.getParam('browser_preferred_colors', false) || !t.isWindow)
352 t.dom.addClass(document.body, 'forceColors');
354 document.body.style.display = '';
356 // Restore selection in IE when focus is placed on a non textarea or input element of the type text
358 document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
360 // Add base target element for it since it would fail with modal dialogs
361 t.dom.add(t.dom.select('head')[0], 'base', {target : '_self'});
364 t.restoreSelection();
365 t.resizeToInnerSize();
369 t.editor.windowManager.setTitle(window, ti);
373 if (!tinymce.isIE && !t.isWindow) {
374 t.dom.bind(document, 'focus', function() {
375 t.editor.windowManager.focus(t.id);
379 // Patch for accessibility
380 tinymce.each(t.dom.select('select'), function(e) {
381 e.onkeydown = tinyMCEPopup._accessHandler;
385 // Init must be called before focus so the selection won't get lost by the focus call
386 tinymce.each(t.listeners, function(o) {
387 o.func.call(o.scope, t.editor);
390 // Move focus to window
391 if (t.getWindowArg('mce_auto_focus', true)) {
394 // Focus element with mceFocus class
395 tinymce.each(document.forms, function(f) {
396 tinymce.each(f.elements, function(e) {
397 if (t.dom.hasClass(e, 'mceFocus') && !e.disabled) {
399 return false; // Break loop
405 document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
408 _accessHandler : function(e) {
409 e = e || window.event;
411 if (e.keyCode == 13 || e.keyCode == 32) {
412 var elm = e.target || e.srcElement;
417 return tinymce.dom.Event.cancel(e);
421 _closeWinKeyHandler : function(e) {
422 e = e || window.event;
425 tinyMCEPopup.close();
428 _eventProxy: function(id) {
429 return function(evt) {
430 tinyMCEPopup.dom.events.callNativeHandler(id, evt);