1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2007 Frederico Caldeira Knabben
4 *
5 * == BEGIN LICENSE ==
6 *
7 * Licensed under the terms of any of the following licenses at your
8 * choice:
9 *
10 *  - GNU General Public License Version 2 or later (the "GPL")
11 *    http://www.gnu.org/licenses/gpl.html
12 *
13 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14 *    http://www.gnu.org/licenses/lgpl.html
15 *
16 *  - Mozilla Public License Version 1.1 or later (the "MPL")
17 *    http://www.mozilla.org/MPL/MPL-1.1.html
18 *
19 * == END LICENSE ==
20 *
21 * Create the FCKeditorAPI object that is available as a global object in
22 * the page where the editor is placed in.
23 */
24
25var FCKeditorAPI ;
26
27function InitializeAPI()
28{
29	var oParentWindow = window.parent ;
30
31	if ( !( FCKeditorAPI = oParentWindow.FCKeditorAPI ) )
32	{
33		// Make the FCKeditorAPI object available in the parent window. Use
34		// eval so this core runs in the parent's scope and so it will still be
35		// available if the editor instance is removed ("Can't execute code
36		// from a freed script" error).
37
38		// Note: we check the existence of oEditor.GetParentForm because some external
39		// code (like JSON) can extend the Object prototype and we get then extra oEditor
40		// objects that aren't really FCKeditor instances.
41		var sScript =
42			'var FCKeditorAPI = {' +
43				'Version : "2.5 Beta",' +
44				'VersionBuild : "16842",' +
45				'__Instances : new Object(),' +
46
47				'GetInstance : function( name )' +
48				'{' +
49					'return this.__Instances[ name ];' +
50				'},' +
51
52				'_FormSubmit : function()' +
53				'{' +
54					'for ( var name in FCKeditorAPI.__Instances )' +
55					'{' +
56						'var oEditor = FCKeditorAPI.__Instances[ name ] ;' +
57						'if ( oEditor.GetParentForm && oEditor.GetParentForm() == this )' +
58							'oEditor.UpdateLinkedField() ;' +
59					'}' +
60					'this._FCKOriginalSubmit() ;' +
61				'},' +
62
63				'_FunctionQueue	: {' +
64					'Functions : new Array(),' +
65					'IsRunning : false,' +
66
67					'Add : function( f )' +
68					'{' +
69						'this.Functions.push( f );' +
70						'if ( !this.IsRunning )' +
71							'this.StartNext();' +
72					'},' +
73
74					'StartNext : function()' +
75					'{' +
76						'var aQueue = this.Functions ;' +
77						'if ( aQueue.length > 0 )' +
78						'{' +
79							'this.IsRunning = true;' +
80							'aQueue[0].call();' +
81						'}' +
82						'else ' +
83							'this.IsRunning = false;' +
84					'},' +
85
86					'Remove : function( f )' +
87					'{' +
88						'var aQueue = this.Functions;' +
89						'var i = 0, fFunc;' +
90						'while( (fFunc = aQueue[ i ]) )' +
91						'{' +
92							'if ( fFunc == f )' +
93								'aQueue.splice( i,1 );' +
94							'i++ ;' +
95						'}' +
96						'this.StartNext();' +
97					'}' +
98				'}' +
99			'}' ;
100
101		// In IE, the "eval" function is not always available (it works with
102		// the JavaScript samples, but not with the ASP ones, for example).
103		// So, let's use the execScript instead.
104		if ( oParentWindow.execScript )
105			oParentWindow.execScript( sScript, 'JavaScript' ) ;
106		else
107		{
108			if ( FCKBrowserInfo.IsGecko10 )
109			{
110				// FF 1.0.4 gives an error with the request bellow. The
111				// following seams to work well.
112				eval.call( oParentWindow, sScript ) ;
113			}
114			else if ( FCKBrowserInfo.IsSafari || FCKBrowserInfo.IsGecko19 )
115			{
116				// oParentWindow.eval in Safari and Gran Paradiso executes in the calling window
117				// environment, instead of the parent one. The following should make it work.
118				var oParentDocument = oParentWindow.document ;
119				var eScript = oParentDocument.createElement('script') ;
120				eScript.appendChild( oParentDocument.createTextNode( sScript ) ) ;
121				oParentDocument.documentElement.appendChild( eScript ) ;
122			}
123			else
124				oParentWindow.eval( sScript ) ;
125		}
126
127		FCKeditorAPI = oParentWindow.FCKeditorAPI ;
128	}
129
130	// Add the current instance to the FCKeditorAPI's instances collection.
131	FCKeditorAPI.__Instances[ FCK.Name ] = FCK ;
132}
133
134// Attach to the form onsubmit event and to the form.submit().
135function _AttachFormSubmitToAPI()
136{
137	// Get the linked field form.
138	var oForm = FCK.GetParentForm() ;
139
140	if ( oForm )
141	{
142		// Attach to the onsubmit event.
143		FCKTools.AddEventListener( oForm, 'submit', FCK.UpdateLinkedField ) ;
144
145		// IE sees oForm.submit function as an 'object'.
146		if ( !oForm._FCKOriginalSubmit && ( typeof( oForm.submit ) == 'function' || ( !oForm.submit.tagName && !oForm.submit.length ) ) )
147		{
148			// Save the original submit.
149			oForm._FCKOriginalSubmit = oForm.submit ;
150
151			// Create our replacement for the submit.
152			oForm.submit = FCKeditorAPI._FormSubmit ;
153		}
154	}
155}
156
157function FCKeditorAPI_Cleanup()
158{
159	if ( ! window.FCKUnloadFlag )
160		return ;
161	delete FCKeditorAPI.__Instances[ FCK.Name ] ;
162}
163function FCKeditorAPI_ConfirmCleanup()
164{
165	window.FCKUnloadFlag = true ;
166}
167FCKTools.AddEventListener( window, 'unload', FCKeditorAPI_Cleanup ) ;
168FCKTools.AddEventListener( window, 'beforeunload', FCKeditorAPI_ConfirmCleanup) ;
169