Subversion-Projekte lars-tiefland.webanos.zeldi.de

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
4 lars 1
/**
2
 * Popup.js
3
 *
4
 * Copyright, Moxiecode Systems AB
5
 * Released under LGPL License.
6
 *
7
 * License: http://www.tinymce.com/license
8
 * Contributing: http://www.tinymce.com/contributing
9
 */
10
 
11
// Some global instances
12
var tinymce = null, tinyMCEPopup, tinyMCE;
13
 
14
/**
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.
18
 *
19
 * @static
20
 * @class tinyMCEPopup
21
 */
22
tinyMCEPopup = {
23
	/**
24
	 * Initializes the popup this will be called automatically.
25
	 *
26
	 * @method init
27
	 */
28
	init : function() {
29
		var t = this, w, ti, settings;
30
 
31
		// Find window & API
32
		w = t.getWin();
33
		tinymce = w.tinymce;
34
		tinyMCE = w.tinyMCE;
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;
39
 
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);
44
			} else {
45
				settings.popup_css = t.baseURI.toAbsolute("themes/" + settings.theme + "/skins/" + settings.skin + "/dialog.css");
46
			}
47
		}
48
 
49
		if (settings.popup_css_add) {
50
			settings.popup_css += ',' + t.documentBaseURI.toAbsolute(settings.popup_css_add);
51
		}
52
 
53
		// Setup local DOM
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);
56
 
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);
60
 
61
		// Setup on init listeners
62
		t.listeners = [];
63
 
64
		/**
65
		 * Fires when the popup is initialized.
66
		 *
67
		 * @event onInit
68
		 * @param {tinymce.Editor} editor Editor instance.
69
		 * @example
70
		 * // Alerts the selected contents when the dialog is loaded
71
		 * tinyMCEPopup.onInit.add(function(ed) {
72
		 *     alert(ed.selection.getContent());
73
		 * });
74
		 *
75
		 * // Executes the init method on page load in some object using the SomeObject scope
76
		 * tinyMCEPopup.onInit.add(SomeObject.init, SomeObject);
77
		 */
78
		t.onInit = {
79
			add : function(f, s) {
80
				t.listeners.push({func : f, scope : s});
81
			}
82
		};
83
 
84
		t.isWindow = !t.getWindowArg('mce_inline');
85
		t.id = t.getWindowArg('mce_window_id');
86
	},
87
 
88
	/**
89
	 * Returns the reference to the parent window that opened the dialog.
90
	 *
91
	 * @method getWin
92
	 * @return {Window} Reference to the parent window that opened the dialog.
93
	 */
94
	getWin : function() {
95
		// Added frameElement check to fix bug: #2817583
96
		return (!window.frameElement && window.dialogArguments) || opener || parent || top;
97
	},
98
 
99
	/**
100
	 * Returns a window argument/parameter by name.
101
	 *
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.
106
	 */
107
	getWindowArg : function(n, dv) {
108
		var v = this.params[n];
109
 
110
		return tinymce.is(v) ? v : dv;
111
	},
112
 
113
	/**
114
	 * Returns a editor parameter/config option value.
115
	 *
116
	 * @method getParam
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.
120
	 */
121
	getParam : function(n, dv) {
122
		return this.editor.getParam(n, dv);
123
	},
124
 
125
	/**
126
	 * Returns a language item by key.
127
	 *
128
	 * @method getLang
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.
132
	 */
133
	getLang : function(n, dv) {
134
		return this.editor.getLang(n, dv);
135
	},
136
 
137
	/**
138
	 * Executed a command on editor that opened the dialog/popup.
139
	 *
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.
145
	 */
146
	execCommand : function(cmd, ui, val, a) {
147
		a = a || {};
148
		a.skip_focus = 1;
149
 
150
		this.restoreSelection();
151
		return this.editor.execCommand(cmd, ui, val, a);
152
	},
153
 
154
	/**
155
	 * Resizes the dialog to the inner size of the window. This is needed since various browsers
156
	 * have different border sizes on windows.
157
	 *
158
	 * @method resizeToInnerSize
159
	 */
160
	resizeToInnerSize : function() {
161
		var t = this;
162
 
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);
167
 
168
			t.editor.windowManager.resizeBy(
169
				t.getWindowArg('mce_width') - vp.w,
170
				t.getWindowArg('mce_height') - vp.h,
171
				t.id || window
172
			);
173
		}, 10);
174
	},
175
 
176
	/**
177
	 * Will executed the specified string when the page has been loaded. This function
178
	 * was added for compatibility with the 2.x branch.
179
	 *
180
	 * @method executeOnLoad
181
	 * @param {String} s String to evalutate on init.
182
	 */
183
	executeOnLoad : function(s) {
184
		this.onInit.add(function() {
185
			eval(s);
186
		});
187
	},
188
 
189
	/**
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.
192
	 *
193
	 * @method storeSelection
194
	 */
195
	storeSelection : function() {
196
		this.editor.windowManager.bookmark = tinyMCEPopup.editor.selection.getBookmark(1);
197
	},
198
 
199
	/**
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.
202
	 *
203
	 * @method restoreSelection
204
	 */
205
	restoreSelection : function() {
206
		var t = tinyMCEPopup;
207
 
208
		if (!t.isWindow && tinymce.isIE)
209
			t.editor.selection.moveToBookmark(t.editor.windowManager.bookmark);
210
	},
211
 
212
	/**
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.
215
	 *
216
	 * @method requireLangPack
217
	 */
218
	requireLangPack : function() {
219
		var t = this, u = t.getWindowArg('plugin_url') || t.getWindowArg('theme_url');
220
 
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';
223
 
224
			if (!tinymce.ScriptLoader.isDone(u)) {
225
				document.write('<script type="text/javascript" src="' + u + '"></script>');
226
				tinymce.ScriptLoader.markDone(u);
227
			}
228
		}
229
	},
230
 
231
	/**
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.
234
	 *
235
	 * @method pickColor
236
	 * @param {DOMEvent} e DOM event object.
237
	 * @param {string} element_id Element id to be filled with the color value from the picker.
238
	 */
239
	pickColor : function(e, element_id) {
240
		this.execCommand('mceColorPicker', true, {
241
			color : document.getElementById(element_id).value,
242
			func : function(c) {
243
				document.getElementById(element_id).value = c;
244
 
245
				try {
246
					document.getElementById(element_id).onchange();
247
				} catch (ex) {
248
					// Try fire event, ignore errors
249
				}
250
			}
251
		});
252
	},
253
 
254
	/**
255
	 * Opens a filebrowser/imagebrowser this will set the output value from
256
	 * the browser as a value on the specified element.
257
	 *
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.
262
	 */
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);
266
	},
267
 
268
	/**
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.
271
	 *
272
	 * @method confirm
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.
276
	 */
277
	confirm : function(t, cb, s) {
278
		this.editor.windowManager.confirm(t, cb, s, window);
279
	},
280
 
281
	/**
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.
284
	 *
285
	 * @method alert
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.
289
	 */
290
	alert : function(tx, cb, s) {
291
		this.editor.windowManager.alert(tx, cb, s, window);
292
	},
293
 
294
	/**
295
	 * Closes the current window.
296
	 *
297
	 * @method close
298
	 */
299
	close : function() {
300
		var t = this;
301
 
302
		// To avoid domain relaxing issue in Opera
303
		function close() {
304
			t.editor.windowManager.close(window);
305
			tinymce = tinyMCE = t.editor = t.params = t.dom = t.dom.doc = null; // Cleanup
306
		};
307
 
308
		if (tinymce.isOpera)
309
			t.getWin().setTimeout(close, 0);
310
		else
311
			close();
312
	},
313
 
314
	// Internal functions
315
 
316
	_restoreSelection : function() {
317
		var e = window.event.srcElement;
318
 
319
		if (e.nodeName == 'INPUT' && (e.type == 'submit' || e.type == 'button'))
320
			tinyMCEPopup.restoreSelection();
321
	},
322
 
323
/*	_restoreSelection : function() {
324
		var e = window.event.srcElement;
325
 
326
		// If user focus a non text input or textarea
327
		if ((e.nodeName != 'INPUT' && e.nodeName != 'TEXTAREA') || e.type != 'text')
328
			tinyMCEPopup.restoreSelection();
329
	},*/
330
 
331
	_onDOMLoaded : function() {
332
		var t = tinyMCEPopup, ti = document.title, bm, h, nv;
333
 
334
		// Translate page
335
		if (t.features.translate_i18n !== false) {
336
			h = document.body.innerHTML;
337
 
338
			// Replace a=x with a="x" in IE
339
			if (tinymce.isIE)
340
				h = h.replace(/ (value|title|alt)=([^"][^\s>]+)/gi, ' $1="$2"')
341
 
342
			document.dir = t.editor.getParam('directionality','');
343
 
344
			if ((nv = t.editor.translate(h)) && nv != h)
345
				document.body.innerHTML = nv;
346
 
347
			if ((nv = t.editor.translate(ti)) && nv != ti)
348
				document.title = ti = nv;
349
		}
350
 
351
		if (!t.editor.getParam('browser_preferred_colors', false) || !t.isWindow)
352
			t.dom.addClass(document.body, 'forceColors');
353
 
354
		document.body.style.display = '';
355
 
356
		// Restore selection in IE when focus is placed on a non textarea or input element of the type text
357
		if (tinymce.isIE) {
358
			document.attachEvent('onmouseup', tinyMCEPopup._restoreSelection);
359
 
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'});
362
		}
363
 
364
		t.restoreSelection();
365
		t.resizeToInnerSize();
366
 
367
		// Set inline title
368
		if (!t.isWindow)
369
			t.editor.windowManager.setTitle(window, ti);
370
		else
371
			window.focus();
372
 
373
		if (!tinymce.isIE && !t.isWindow) {
374
			t.dom.bind(document, 'focus', function() {
375
				t.editor.windowManager.focus(t.id);
376
			});
377
		}
378
 
379
		// Patch for accessibility
380
		tinymce.each(t.dom.select('select'), function(e) {
381
			e.onkeydown = tinyMCEPopup._accessHandler;
382
		});
383
 
384
		// Call onInit
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);
388
		});
389
 
390
		// Move focus to window
391
		if (t.getWindowArg('mce_auto_focus', true)) {
392
			window.focus();
393
 
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) {
398
						e.focus();
399
						return false; // Break loop
400
					}
401
				});
402
			});
403
		}
404
 
405
		document.onkeyup = tinyMCEPopup._closeWinKeyHandler;
406
	},
407
 
408
	_accessHandler : function(e) {
409
		e = e || window.event;
410
 
411
		if (e.keyCode == 13 || e.keyCode == 32) {
412
			var elm = e.target || e.srcElement;
413
 
414
			if (elm.onchange)
415
				elm.onchange();
416
 
417
			return tinymce.dom.Event.cancel(e);
418
		}
419
	},
420
 
421
	_closeWinKeyHandler : function(e) {
422
		e = e || window.event;
423
 
424
		if (e.keyCode == 27)
425
			tinyMCEPopup.close();
426
	},
427
 
428
	_eventProxy: function(id) {
429
		return function(evt) {
430
			tinyMCEPopup.dom.events.callNativeHandler(id, evt);
431
		};
432
	}
433
};
434
 
435
tinyMCEPopup.init();