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