1/*
2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3 * Copyright (C) 2003-2009 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 * Scripts related to the Flash dialog window (see fck_flash.html).
22 */
23
24var dialog		= window.parent ;
25var oEditor		= dialog.InnerDialogLoaded() ;
26var FCK			= oEditor.FCK ;
27var FCKLang		= oEditor.FCKLang ;
28var FCKConfig	= oEditor.FCKConfig ;
29var FCKTools	= oEditor.FCKTools ;
30
31//#### Dialog Tabs
32
33// Set the dialog tabs.
34dialog.AddTab( 'Info', oEditor.FCKLang.DlgInfoTab ) ;
35
36if ( FCKConfig.FlashUpload )
37	dialog.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
38
39if ( !FCKConfig.FlashDlgHideAdvanced )
40	dialog.AddTab( 'Advanced', oEditor.FCKLang.DlgAdvancedTag ) ;
41
42// Function called when a dialog tag is selected.
43function OnDialogTabChange( tabCode )
44{
45	ShowE('divInfo'		, ( tabCode == 'Info' ) ) ;
46	ShowE('divUpload'	, ( tabCode == 'Upload' ) ) ;
47	ShowE('divAdvanced'	, ( tabCode == 'Advanced' ) ) ;
48}
49
50// Get the selected flash embed (if available).
51var oFakeImage = dialog.Selection.GetSelectedElement() ;
52var oEmbed ;
53
54if ( oFakeImage )
55{
56	if ( oFakeImage.tagName == 'IMG' && oFakeImage.getAttribute('_fckflash') )
57		oEmbed = FCK.GetRealElement( oFakeImage ) ;
58	else
59		oFakeImage = null ;
60}
61
62window.onload = function()
63{
64	// Translate the dialog box texts.
65	oEditor.FCKLanguageManager.TranslatePage(document) ;
66
67	// Load the selected element information (if any).
68	LoadSelection() ;
69
70	// Show/Hide the "Browse Server" button.
71	GetE('tdBrowse').style.display = FCKConfig.FlashBrowser	? '' : 'none' ;
72
73	// Set the actual uploader URL.
74	if ( FCKConfig.FlashUpload )
75		GetE('frmUpload').action = FCKConfig.FlashUploadURL ;
76
77	dialog.SetAutoSize( true ) ;
78
79	// Activate the "OK" button.
80	dialog.SetOkButton( true ) ;
81
82	SelectField( 'txtUrl' ) ;
83}
84
85function LoadSelection()
86{
87	if ( ! oEmbed ) return ;
88
89	GetE('txtUrl').value    = GetAttribute( oEmbed, 'src', '' ) ;
90	GetE('txtWidth').value  = GetAttribute( oEmbed, 'width', '' ) ;
91	GetE('txtHeight').value = GetAttribute( oEmbed, 'height', '' ) ;
92
93	// Get Advances Attributes
94	GetE('txtAttId').value		= oEmbed.id ;
95	GetE('chkAutoPlay').checked	= GetAttribute( oEmbed, 'play', 'true' ) == 'true' ;
96	GetE('chkLoop').checked		= GetAttribute( oEmbed, 'loop', 'true' ) == 'true' ;
97	GetE('chkMenu').checked		= GetAttribute( oEmbed, 'menu', 'true' ) == 'true' ;
98	GetE('cmbScale').value		= GetAttribute( oEmbed, 'scale', '' ).toLowerCase() ;
99
100	GetE('txtAttTitle').value		= oEmbed.title ;
101
102	if ( oEditor.FCKBrowserInfo.IsIE )
103	{
104		GetE('txtAttClasses').value = oEmbed.getAttribute('className') || '' ;
105		GetE('txtAttStyle').value = oEmbed.style.cssText ;
106	}
107	else
108	{
109		GetE('txtAttClasses').value = oEmbed.getAttribute('class',2) || '' ;
110		GetE('txtAttStyle').value = oEmbed.getAttribute('style',2) || '' ;
111	}
112
113	UpdatePreview() ;
114}
115
116//#### The OK button was hit.
117function Ok()
118{
119	if ( GetE('txtUrl').value.length == 0 )
120	{
121		dialog.SetSelectedTab( 'Info' ) ;
122		GetE('txtUrl').focus() ;
123
124		alert( oEditor.FCKLang.DlgAlertUrl ) ;
125
126		return false ;
127	}
128
129	oEditor.FCKUndo.SaveUndoStep() ;
130	if ( !oEmbed )
131	{
132		oEmbed		= FCK.EditorDocument.createElement( 'EMBED' ) ;
133		oFakeImage  = null ;
134	}
135	UpdateEmbed( oEmbed ) ;
136
137	if ( !oFakeImage )
138	{
139		oFakeImage	= oEditor.FCKDocumentProcessor_CreateFakeImage( 'FCK__Flash', oEmbed ) ;
140		oFakeImage.setAttribute( '_fckflash', 'true', 0 ) ;
141		oFakeImage	= FCK.InsertElement( oFakeImage ) ;
142	}
143
144	oEditor.FCKEmbedAndObjectProcessor.RefreshView( oFakeImage, oEmbed ) ;
145
146	return true ;
147}
148
149function UpdateEmbed( e )
150{
151	SetAttribute( e, 'type'			, 'application/x-shockwave-flash' ) ;
152	SetAttribute( e, 'pluginspage'	, 'http://www.macromedia.com/go/getflashplayer' ) ;
153
154	SetAttribute( e, 'src', GetE('txtUrl').value ) ;
155	SetAttribute( e, "width" , GetE('txtWidth').value ) ;
156	SetAttribute( e, "height", GetE('txtHeight').value ) ;
157
158	// Advances Attributes
159
160	SetAttribute( e, 'id'	, GetE('txtAttId').value ) ;
161	SetAttribute( e, 'scale', GetE('cmbScale').value ) ;
162
163	SetAttribute( e, 'play', GetE('chkAutoPlay').checked ? 'true' : 'false' ) ;
164	SetAttribute( e, 'loop', GetE('chkLoop').checked ? 'true' : 'false' ) ;
165	SetAttribute( e, 'menu', GetE('chkMenu').checked ? 'true' : 'false' ) ;
166
167	SetAttribute( e, 'title'	, GetE('txtAttTitle').value ) ;
168
169	if ( oEditor.FCKBrowserInfo.IsIE )
170	{
171		SetAttribute( e, 'className', GetE('txtAttClasses').value ) ;
172		e.style.cssText = GetE('txtAttStyle').value ;
173	}
174	else
175	{
176		SetAttribute( e, 'class', GetE('txtAttClasses').value ) ;
177		SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
178	}
179}
180
181var ePreview ;
182
183function SetPreviewElement( previewEl )
184{
185	ePreview = previewEl ;
186
187	if ( GetE('txtUrl').value.length > 0 )
188		UpdatePreview() ;
189}
190
191function UpdatePreview()
192{
193	if ( !ePreview )
194		return ;
195
196	while ( ePreview.firstChild )
197		ePreview.removeChild( ePreview.firstChild ) ;
198
199	if ( GetE('txtUrl').value.length == 0 )
200		ePreview.innerHTML = ' ' ;
201	else
202	{
203		var oDoc	= ePreview.ownerDocument || ePreview.document ;
204		var e		= oDoc.createElement( 'EMBED' ) ;
205
206		SetAttribute( e, 'src', GetE('txtUrl').value ) ;
207		SetAttribute( e, 'type', 'application/x-shockwave-flash' ) ;
208		SetAttribute( e, 'width', '100%' ) ;
209		SetAttribute( e, 'height', '100%' ) ;
210
211		ePreview.appendChild( e ) ;
212	}
213}
214
215// <embed id="ePreview" src="fck_flash/claims.swf" width="100%" height="100%" style="visibility:hidden" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
216
217function BrowseServer()
218{
219	OpenFileBrowser( FCKConfig.FlashBrowserURL, FCKConfig.FlashBrowserWindowWidth, FCKConfig.FlashBrowserWindowHeight ) ;
220}
221
222function SetUrl( url, width, height )
223{
224	GetE('txtUrl').value = url ;
225
226	if ( width )
227		GetE('txtWidth').value = width ;
228
229	if ( height )
230		GetE('txtHeight').value = height ;
231
232	UpdatePreview() ;
233
234	dialog.SetSelectedTab( 'Info' ) ;
235}
236
237function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
238{
239	// Remove animation
240	window.parent.Throbber.Hide() ;
241	GetE( 'divUpload' ).style.display  = '' ;
242
243	switch ( errorNumber )
244	{
245		case 0 :	// No errors
246			alert( 'Your file has been successfully uploaded' ) ;
247			break ;
248		case 1 :	// Custom error
249			alert( customMsg ) ;
250			return ;
251		case 101 :	// Custom warning
252			alert( customMsg ) ;
253			break ;
254		case 201 :
255			alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
256			break ;
257		case 202 :
258			alert( 'Invalid file type' ) ;
259			return ;
260		case 203 :
261			alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
262			return ;
263		case 500 :
264			alert( 'The connector is disabled' ) ;
265			break ;
266		default :
267			alert( 'Error on file upload. Error number: ' + errorNumber ) ;
268			return ;
269	}
270
271	SetUrl( fileUrl ) ;
272	GetE('frmUpload').reset() ;
273}
274
275var oUploadAllowedExtRegex	= new RegExp( FCKConfig.FlashUploadAllowedExtensions, 'i' ) ;
276var oUploadDeniedExtRegex	= new RegExp( FCKConfig.FlashUploadDeniedExtensions, 'i' ) ;
277
278function CheckUpload()
279{
280	var sFile = GetE('txtUploadFile').value ;
281
282	if ( sFile.length == 0 )
283	{
284		alert( 'Please select a file to upload' ) ;
285		return false ;
286	}
287
288	if ( ( FCKConfig.FlashUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
289		( FCKConfig.FlashUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
290	{
291		OnUploadCompleted( 202 ) ;
292		return false ;
293	}
294
295	// Show animation
296	window.parent.Throbber.Show( 100 ) ;
297	GetE( 'divUpload' ).style.display  = 'none' ;
298
299	return true ;
300}
301