4 * Copyright 2009, Moxiecode Systems AB
5 * Released under LGPL License.
7 * License: http://tinymce.moxiecode.com/license
8 * Contributing: http://tinymce.moxiecode.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.
35 t.editor = tinymce.EditorManager.activeEditor;
36 t.params = t.editor.windowManager.params;
37 t.features = t.editor.windowManager.features;
40 t.dom = t.editor.windowManager.createInstance('tinymce.dom.DOMUtils', document);
42 // Enables you to skip loading the default css
43 if (t.features.popup_css !== false)
44 t.dom.loadCSS(t.features.popup_css || t.editor.settings.popup_css);
46 // Setup on init listeners
50 * Fires when the popup is initialized.
53 * @param {tinymce.Editor} editor Editor instance.
55 * // Alerts the selected contents when the dialog is loaded
56 * tinyMCEPopup.onInit.add(function(ed) {
57 * alert(ed.selection.getContent());
60 * // Executes the init method on page load in some object using the SomeObject scope
61 * tinyMCEPopup.onInit.add(SomeObject.init, SomeObject);
64 add : function(f, s) {
65 t.listeners.push({func : f, scope : s});
69 t.isWindow = !t.getWindowArg('mce_inline');
70 t.id = t.getWindowArg('mce_window_id');
71 t.editor.windowManager.onOpen.dispatch(t.editor.windowManager, window);
75 * Returns the reference to the parent window that opened the dialog.
78 * @return {Window} Reference to the parent window that opened the dialog.
81 // Added frameElement check to fix bug: #2817583
82 return (!window.frameElement && window.dialogArguments) || opener || parent || top;
86 * Returns a window argument/parameter by name.
88 * @method getWindowArg
89 * @param {String} n Name of the window argument to retrive.
90 * @param {String} dv Optional default value to return.
91 * @return {String} Argument value or default value if it wasn't found.
93 getWindowArg : function(n, dv) {
94 var v = this.params[n];
96 return tinymce.is(v) ? v : dv;
100 * Returns a editor parameter/config option value.
103 * @param {String} n Name of the editor config option to retrive.
104 * @param {String} dv Optional default value to return.
105 * @return {String} Parameter value or default value if it wasn't found.
107 getParam : function(n, dv) {
108 return this.editor.getParam(n, dv);
112 * Returns a language item by key.
115 * @param {String} n Language item like mydialog.something.
116 * @param {String} dv Optional default value to return.
117 * @return {String} Language value for the item like "my string" or the default value if it wasn't found.
119 getLang : function(n, dv) {
120 return this.editor.getLang(n, dv);
124 * Executed a command on editor that opened the dialog/popup.
126 * @method execCommand
127 * @param {String} cmd Command to execute.
128 * @param {Boolean} ui Optional boolean value if the UI for the command should be presented or not.
129 * @param {Object} val Optional value to pass with the comman like an URL.
130 * @param {Object} a Optional arguments object.
132 execCommand : function(cmd, ui, val, a) {
136 this.restoreSelection();
137 return this.editor.execCommand(cmd, ui, val, a);
141 * Resizes the dialog to the inner size of the window. This is needed since various browsers
142 * have different border sizes on windows.
144 * @method resizeToInnerSize
146 resizeToInnerSize : function() {
149 // Detach it to workaround a Chrome specific bug
150 // https://sourceforge.net/tracker/?func=detail&atid=635682&aid=2926339&group_id=103281
151 setTimeout(function() {
152 var vp = t.dom.getViewPort(window);
154 t.editor.windowManager.resizeBy(
155 t.getWindowArg('mce_width') - vp.w,
156 t.getWindowArg('mce_height') - vp.h,
163 * Will executed the specified string when the page has been loaded. This function
164 * was added for compatibility with the 2.x branch.
166 * @method executeOnLoad
167 * @param {String} s String to evalutate on init.
169 executeOnLoad : function(s) {
170 this.onInit.add(function() {
176 * Stores the current editor selection for later restoration. This can be useful since some browsers
177 * looses it's selection if a control element is selected/focused inside the dialogs.
179 * @method storeSelection
181 storeSelection : function() {
182 this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark(1);
186 * Restores any stored selection. This can be useful since some browsers
187 * looses it's selection if a control element is selected/focused inside the dialogs.
189 * @method restoreSelection
191 restoreSelection : function() {
192 var t = tinyMCEPopup;
194 if (!t.isWindow && tinymce.isIE)
195 t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
199 * Loads a specific dialog language pack. If you pass in plugin_url as a arugment
200 * when you open the window it will load the <plugin url>/langs/<code>_dlg.js lang pack file.
202 * @method requireLangPack
204 requireLangPack : function() {
205 var t = this, u = t.getWindowArg('plugin_url') || t.getWindowArg('theme_url');
207 if (u && t.editor.settings.language && t.features.translate_i18n !== false && t.editor.settings.language_load !== false) {
208 u += '/langs/' + t.editor.settings.language + '_dlg.js';
210 if (!tinymce.ScriptLoader.isDone(u)) {
211 document.write('<script type="text/javascript" src="' + tinymce._addVer(u) + '"></script>');
212 tinymce.ScriptLoader.markDone(u);
218 * Executes a color picker on the specified element id. When the user
219 * then selects a color it will be set as the value of the specified element.
222 * @param {DOMEvent} e DOM event object.
223 * @param {string} element_id Element id to be filled with the color value from the picker.
225 pickColor : function(e, element_id) {
226 this.execCommand('mceColorPicker', true, {
227 color : document.getElementById(element_id).value,
229 document.getElementById(element_id).value = c;
232 document.getElementById(element_id).onchange();
234 // Try fire event, ignore errors
241 * Opens a filebrowser/imagebrowser this will set the output value from
242 * the browser as a value on the specified element.
244 * @method openBrowser
245 * @param {string} element_id Id of the element to set value in.
246 * @param {string} type Type of browser to open image/file/flash.
247 * @param {string} option Option name to get the file_broswer_callback function name from.
249 openBrowser : function(element_id, type, option) {
250 tinyMCEPopup.restoreSelection();
251 this.editor.execCallback('file_browser_callback', element_id, document.getElementById(element_id).value, type, window);
255 * Creates a confirm dialog. Please don't use the blocking behavior of this
256 * native version use the callback method instead then it can be extended.
259 * @param {String} t Title for the new confirm dialog.
260 * @param {function} cb Callback function to be executed after the user has selected ok or cancel.
261 * @param {Object} s Optional scope to execute the callback in.
263 confirm : function(t, cb, s) {
264 this.editor.windowManager.confirm(t, cb, s, window);
268 * Creates a alert dialog. Please don't use the blocking behavior of this
269 * native version use the callback method instead then it can be extended.
272 * @param {String} t Title for the new alert dialog.
273 * @param {function} cb Callback function to be executed after the user has selected ok.
274 * @param {Object} s Optional scope to execute the callback in.
276 alert : function(tx, cb, s) {
277 this.editor.windowManager.alert(tx, cb, s, window);
281 * Closes the current window.
288 // To avoid domain relaxing issue in Opera
290 t.editor.windowManager.close(window);
291 tinymce = tinyMCE = t.editor = t.params = t.dom = t.dom.doc = null; // Cleanup
295 t.getWin().setTimeout(close, 0);
300 // Internal functions
302 _restoreSelection : function() {
303 var e = window.event.srcElement;
305 if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
306 tinyMCEPopup.restoreSelection();
309 /* _restoreSelection : function() {
310 var e = window.event.srcElement;
312 // If user focus a non text input or textarea
313 if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
314 tinyMCEPopup.restoreSelection();
317 _onDOMLoaded : function() {
318 var t = tinyMCEPopup, ti = document.title, bm, h, nv;
326 if (t.features.translate_i18n !== false) {
327 h = document.body.innerHTML;
329 // Replace a=x with a="x" in IE
331 h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"')
333 document.dir = t.editor.getParam('directionality','');
335 if ((nv = t.editor.translate(h)) && nv != h)
336 document.body.innerHTML = nv;
338 if ((nv = t.editor.translate(ti)) && nv != ti)
339 document.title = ti = nv;
342 if (!t.editor.getParam('browser_preferred_colors', false) || !t.isWindow)
343 t.dom.addClass(document.body, 'forceColors');
345 document.body.style.display = '';
347 // Restore selection in IE when focus is placed on a non textarea or input element of the type text
349 document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
351 // Add base target element for it since it would fail with modal dialogs
352 t.dom.add(t.dom.select('head')[0], 'base', {target : '_self'});
355 t.restoreSelection();
356 t.resizeToInnerSize();
360 t.editor.windowManager.setTitle(window, ti);
364 if (!tinymce.isIE && !t.isWindow) {
365 tinymce.dom.Event._add(document, 'focus', function() {
366 t.editor.windowManager.focus(t.id);
370 // Patch for accessibility
371 tinymce.each(t.dom.select('select'), function(e) {
372 e.onkeydown = tinyMCEPopup._accessHandler;
376 // Init must be called before focus so the selection won't get lost by the focus call
377 tinymce.each(t.listeners, function(o) {
378 o.func.call(o.scope, t.editor);
381 // Move focus to window
382 if (t.getWindowArg('mce_auto_focus', true)) {
385 // Focus element with mceFocus class
386 tinymce.each(document.forms, function(f) {
387 tinymce.each(f.elements, function(e) {
388 if (t.dom.hasClass(e, 'mceFocus') && !e.disabled) {
390 return false; // Break loop
396 document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
399 _accessHandler : function(e) {
400 e = e || window.event;
402 if (e.keyCode == 13 || e.keyCode == 32) {
403 e = e.target || e.srcElement;
408 return tinymce.dom.Event.cancel(e);
412 _closeWinKeyHandler : function(e) {
413 e = e || window.event;
416 tinyMCEPopup.close();
421 if (document.attachEvent) {
422 document.attachEvent("onreadystatechange", function() {
423 if (document.readyState === "complete") {
424 document.detachEvent("onreadystatechange", arguments.callee);
425 tinyMCEPopup._onDOMLoaded();
429 if (document.documentElement.doScroll && window == window.top) {
431 if (tinyMCEPopup.domLoaded)
435 // If IE is used, use the trick by Diego Perini licensed under MIT by request to the author.
436 // http://javascript.nwbox.com/IEContentLoaded/
437 document.documentElement.doScroll("left");
439 setTimeout(arguments.callee, 0);
443 tinyMCEPopup._onDOMLoaded();
447 document.attachEvent('onload', tinyMCEPopup._onDOMLoaded);
448 } else if (document.addEventListener) {
449 window.addEventListener('DOMContentLoaded', tinyMCEPopup._onDOMLoaded, false);
450 window.addEventListener('load', tinyMCEPopup._onDOMLoaded, false);
456 tinyMCEPopup._wait(); // Wait for DOM Content Loaded