Subversion Repositories Applications.papyrus

Rev

Rev 431 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
431 ddelon 1
/*
2
 * FCKeditor - The text editor for internet
3
 * Copyright (C) 2003-2005 Frederico Caldeira Knabben
4
 *
5
 * Licensed under the terms of the GNU Lesser General Public License:
6
 * 		http://www.opensource.org/licenses/lgpl-license.php
7
 *
8
 * For further information visit:
9
 * 		http://www.fckeditor.net/
10
 *
521 ddelon 11
 * "Support Open Source software. What about a donation today?"
12
 *
431 ddelon 13
 * File Name: fck_image.js
14
 * 	Scripts related to the Image dialog window (see fck_image.html).
15
 *
16
 * File Authors:
17
 * 		Frederico Caldeira Knabben (fredck@fckeditor.net)
18
 */
19
 
20
var oEditor		= window.parent.InnerDialogLoaded() ;
21
var FCK			= oEditor.FCK ;
22
var FCKLang		= oEditor.FCKLang ;
23
var FCKConfig	= oEditor.FCKConfig ;
24
 
25
var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
26
 
27
//#### Dialog Tabs
28
 
29
// Set the dialog tabs.
30
window.parent.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
31
 
32
if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
33
	window.parent.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
34
 
35
if ( FCKConfig.ImageUpload )
36
	window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
37
 
38
if ( !FCKConfig.ImageDlgHideAdvanced )
39
	window.parent.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
40
 
41
// Function called when a dialog tag is selected.
42
function OnDialogTabChange( tabCode )
43
{
44
	ShowE('divInfo'		, ( tabCode == 'Info' ) ) ;
45
	ShowE('divLink'		, ( tabCode == 'Link' ) ) ;
46
	ShowE('divUpload'	, ( tabCode == 'Upload' ) ) ;
47
	ShowE('divAdvanced'	, ( tabCode == 'Advanced' ) ) ;
48
}
49
 
50
// Get the selected image (if available).
51
var oImage = FCK.Selection.GetSelectedElement() ;
52
 
53
if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
54
	oImage = null ;
55
 
56
// Get the active link.
57
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
58
 
59
var oImageOriginal ;
60
 
61
function UpdateOriginal( resetSize )
62
{
63
	oImageOriginal = document.createElement( 'IMG' ) ;	// new Image() ;
64
 
65
	if ( resetSize )
66
	{
67
		oImageOriginal.onload = function()
68
		{
69
			this.onload = null ;
70
			ResetSizes() ;
71
		}
72
	}
73
 
74
	oImageOriginal.src = GetE('imgPreview').src ;
75
}
76
 
77
window.onload = function()
78
{
79
	// Translate the dialog box texts.
80
	oEditor.FCKLanguageManager.TranslatePage(document) ;
81
 
82
	GetE('btnLockSizes').title = FCKLang.DlgImgLockRatio ;
83
	GetE('btnResetSize').title = FCKLang.DlgBtnResetSize ;
84
 
85
	// Load the selected element information (if any).
86
	LoadSelection() ;
87
 
88
	// Show/Hide the "Browse Server" button.
89
	GetE('tdBrowse').style.display				= FCKConfig.ImageBrowser	? '' : 'none' ;
90
	GetE('divLnkBrowseServer').style.display	= FCKConfig.LinkBrowser		? '' : 'none' ;
91
 
92
	UpdateOriginal() ;
93
 
94
	// Set the actual uploader URL.
95
	if ( FCKConfig.ImageUpload )
96
		GetE('frmUpload').action = FCKConfig.ImageUploadURL ;
97
 
98
	window.parent.SetAutoSize( true ) ;
99
 
100
	// Activate the "OK" button.
101
	window.parent.SetOkButton( true ) ;
102
}
103
 
104
function LoadSelection()
105
{
106
	if ( ! oImage ) return ;
107
 
108
	var sUrl = GetAttribute( oImage, 'src', '' ) ;
109
 
110
	// TODO: Wait stable version and remove the following commented lines.
111
//	if ( sUrl.startsWith( FCK.BaseUrl ) )
112
//		sUrl = sUrl.remove( 0, FCK.BaseUrl.length ) ;
113
 
114
	GetE('txtUrl').value    = sUrl ;
115
	GetE('txtAlt').value    = GetAttribute( oImage, 'alt', '' ) ;
116
	GetE('txtVSpace').value	= GetAttribute( oImage, 'vspace', '' ) ;
117
	GetE('txtHSpace').value	= GetAttribute( oImage, 'hspace', '' ) ;
118
	GetE('txtBorder').value	= GetAttribute( oImage, 'border', '' ) ;
119
	GetE('cmbAlign').value	= GetAttribute( oImage, 'align', '' ) ;
120
 
121
	if ( oImage.style.pixelWidth > 0 )
122
		GetE('txtWidth').value  = oImage.style.pixelWidth ;
123
	else
124
		GetE('txtWidth').value  = GetAttribute( oImage, "width", '' ) ;
125
 
126
	if ( oImage.style.pixelHeight > 0 )
127
		GetE('txtHeight').value  = oImage.style.pixelHeight ;
128
	else
129
		GetE('txtHeight').value = GetAttribute( oImage, "height", '' ) ;
130
 
131
	// Get Advances Attributes
132
	GetE('txtAttId').value			= oImage.id ;
133
	GetE('cmbAttLangDir').value		= oImage.dir ;
134
	GetE('txtAttLangCode').value	= oImage.lang ;
135
	GetE('txtAttTitle').value		= oImage.title ;
136
	GetE('txtAttClasses').value		= oImage.getAttribute('class',2) || '' ;
137
	GetE('txtLongDesc').value		= oImage.longDesc ;
138
 
139
	if ( oEditor.FCKBrowserInfo.IsIE )
140
		GetE('txtAttStyle').value	= oImage.style.cssText ;
141
	else
142
		GetE('txtAttStyle').value	= oImage.getAttribute('style',2) ;
143
 
144
	if ( oLink )
145
	{
146
		GetE('txtLnkUrl').value		= oLink.getAttribute('href',2) ;
147
		GetE('cmbLnkTarget').value	= oLink.target ;
148
	}
149
 
150
	UpdatePreview() ;
151
}
152
 
153
//#### The OK button was hit.
154
function Ok()
155
{
156
	if ( GetE('txtUrl').value.length == 0 )
157
	{
158
		window.parent.SetSelectedTab( 'Info' ) ;
159
		GetE('txtUrl').focus() ;
160
 
161
		alert( FCKLang.DlgImgAlertUrl ) ;
162
 
163
		return false ;
164
	}
165
 
166
	var bHasImage = ( oImage != null ) ;
167
 
168
	if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
169
	{
170
		if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
171
			oImage = null ;
172
	}
173
	else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
174
	{
175
		if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
176
			oImage = null ;
177
	}
178
 
179
	if ( !bHasImage )
180
	{
181
		if ( bImageButton )
182
		{
183
			oImage = FCK.EditorDocument.createElement( 'INPUT' ) ;
184
			oImage.type = 'image' ;
185
			oImage = FCK.InsertElementAndGetIt( oImage ) ;
186
		}
187
		else
188
			oImage = FCK.CreateElement( 'IMG' ) ;
189
	}
190
	else
191
		oEditor.FCKUndo.SaveUndoStep() ;
192
 
193
	UpdateImage( oImage ) ;
194
 
195
	var sLnkUrl = GetE('txtLnkUrl').value.trim() ;
196
 
197
	if ( sLnkUrl.length == 0 )
198
	{
199
		if ( oLink )
200
			FCK.ExecuteNamedCommand( 'Unlink' ) ;
201
	}
202
	else
203
	{
204
		if ( oLink )	// Modifying an existent link.
205
			oLink.href = sLnkUrl ;
206
		else			// Creating a new link.
207
		{
208
			if ( !bHasImage )
209
				oEditor.FCKSelection.SelectNode( oImage ) ;
210
 
211
			oLink = oEditor.FCK.CreateLink( sLnkUrl ) ;
212
 
213
			if ( !bHasImage )
214
			{
215
				oEditor.FCKSelection.SelectNode( oLink ) ;
216
				oEditor.FCKSelection.Collapse( false ) ;
217
			}
218
		}
219
 
220
		SetAttribute( oLink, 'target', GetE('cmbLnkTarget').value ) ;
221
	}
222
 
223
	return true ;
224
}
225
 
226
function UpdateImage( e, skipId )
227
{
228
	e.src = GetE('txtUrl').value ;
229
	SetAttribute( e, "alt"   , GetE('txtAlt').value ) ;
230
	SetAttribute( e, "width" , GetE('txtWidth').value ) ;
231
	SetAttribute( e, "height", GetE('txtHeight').value ) ;
232
	SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
233
	SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
234
	SetAttribute( e, "border", GetE('txtBorder').value ) ;
235
	SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
236
 
237
	// Advances Attributes
238
 
239
	if ( ! skipId )
240
		SetAttribute( e, 'id', GetE('txtAttId').value ) ;
241
 
242
	SetAttribute( e, 'dir'		, GetE('cmbAttLangDir').value ) ;
243
	SetAttribute( e, 'lang'		, GetE('txtAttLangCode').value ) ;
244
	SetAttribute( e, 'title'	, GetE('txtAttTitle').value ) ;
245
	SetAttribute( e, 'class'	, GetE('txtAttClasses').value ) ;
246
	SetAttribute( e, 'longDesc'	, GetE('txtLongDesc').value ) ;
247
 
248
	if ( oEditor.FCKBrowserInfo.IsIE )
249
		e.style.cssText = GetE('txtAttStyle').value ;
250
	else
251
		SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
252
}
253
 
254
function UpdatePreview()
255
{
256
	if ( GetE('txtUrl').value.length == 0 )
257
		GetE('lnkPreview').style.display = 'none' ;
258
	else
259
	{
260
		UpdateImage( GetE('imgPreview'), true ) ;
261
 
262
		if ( GetE('txtLnkUrl').value.trim().length > 0 )
263
			GetE('lnkPreview').href = 'javascript:void(null);' ;
264
		else
265
			SetAttribute( GetE('lnkPreview'), 'href', '' ) ;
266
 
267
		GetE('lnkPreview').style.display = '' ;
268
	}
269
}
270
 
271
var bLockRatio = true ;
272
 
273
function SwitchLock( lockButton )
274
{
275
	bLockRatio = !bLockRatio ;
276
	lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
277
	lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
278
 
279
	if ( bLockRatio )
280
	{
281
		if ( GetE('txtWidth').value.length > 0 )
282
			OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
283
		else
284
			OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
285
	}
286
}
287
 
288
// Fired when the width or height input texts change
289
function OnSizeChanged( dimension, value )
290
{
291
	// Verifies if the aspect ration has to be mantained
292
	if ( oImageOriginal && bLockRatio )
293
	{
294
		if ( value.length == 0 || isNaN( value ) )
295
		{
296
			GetE('txtHeight').value = GetE('txtWidth').value = '' ;
297
			return ;
298
		}
299
 
300
		if ( dimension == 'Width' )
301
			GetE('txtHeight').value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value  / oImageOriginal.width ) ) ;
302
		else
303
			GetE('txtWidth').value  = value == 0 ? 0 : Math.round( oImageOriginal.width  * ( value / oImageOriginal.height ) ) ;
304
	}
305
 
306
	UpdatePreview() ;
307
}
308
 
309
// Fired when the Reset Size button is clicked
310
function ResetSizes()
311
{
312
	if ( ! oImageOriginal ) return ;
313
 
314
	GetE('txtWidth').value  = oImageOriginal.width ;
315
	GetE('txtHeight').value = oImageOriginal.height ;
316
 
317
	UpdatePreview() ;
318
}
319
 
320
function BrowseServer()
321
{
322
	OpenServerBrowser(
323
		'Image',
324
		FCKConfig.ImageBrowserURL,
325
		FCKConfig.ImageBrowserWindowWidth,
326
		FCKConfig.ImageBrowserWindowHeight ) ;
327
}
328
 
329
function LnkBrowseServer()
330
{
331
	OpenServerBrowser(
332
		'Link',
333
		FCKConfig.LinkBrowserURL,
334
		FCKConfig.LinkBrowserWindowWidth,
335
		FCKConfig.LinkBrowserWindowHeight ) ;
336
}
337
 
338
function OpenServerBrowser( type, url, width, height )
339
{
340
	sActualBrowser = type ;
341
 
521 ddelon 342
	var iLeft = (FCKConfig.ScreenWidth  - width) / 2 ;
343
	var iTop  = (FCKConfig.ScreenHeight - height) / 2 ;
431 ddelon 344
 
345
	var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
346
	sOptions += ",width=" + width ;
347
	sOptions += ",height=" + height ;
348
	sOptions += ",left=" + iLeft ;
349
	sOptions += ",top=" + iTop ;
350
 
521 ddelon 351
	if ( oEditor.FCKBrowserInfo.IsIE )
352
	{
353
		// The following change has been made otherwise IE will open the file
354
		// browser on a different server session (on some cases):
355
		// http://support.microsoft.com/default.aspx?scid=kb;en-us;831678
356
		// by Simone Chiaretta.
357
		var oWindow = oEditor.window.open( url, "FCKBrowseWindow", sOptions ) ;
358
		oWindow.opener = window ;
359
    }
360
    else
361
		window.open( url, "FCKBrowseWindow", sOptions ) ;
431 ddelon 362
}
363
 
364
var sActualBrowser ;
365
 
366
function SetUrl( url, width, height, alt )
367
{
368
	if ( sActualBrowser == 'Link' )
369
	{
370
		GetE('txtLnkUrl').value = url ;
371
		UpdatePreview() ;
372
	}
373
	else
374
	{
375
		GetE('txtUrl').value = url ;
376
		GetE('txtWidth').value = width ? width : '' ;
377
		GetE('txtHeight').value = height ? height : '' ;
378
 
379
		if ( alt )
380
			GetE('txtAlt').value = alt;
381
 
382
		UpdatePreview() ;
383
		UpdateOriginal( true ) ;
384
	}
385
 
386
	window.parent.SetSelectedTab( 'Info' ) ;
387
}
388
 
389
function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
390
{
391
	switch ( errorNumber )
392
	{
393
		case 0 :	// No errors
394
			alert( 'Your file has been successfully uploaded' ) ;
395
			break ;
396
		case 1 :	// Custom error
397
			alert( customMsg ) ;
398
			return ;
399
		case 101 :	// Custom warning
400
			alert( customMsg ) ;
401
			break ;
402
		case 201 :
403
			alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
404
			break ;
405
		case 202 :
406
			alert( 'Invalid file type' ) ;
407
			return ;
408
		case 203 :
409
			alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
410
			return ;
411
		default :
412
			alert( 'Error on file upload. Error number: ' + errorNumber ) ;
413
			return ;
414
	}
415
 
416
	sActualBrowser = ''
417
	SetUrl( fileUrl ) ;
418
	GetE('frmUpload').reset() ;
419
}
420
 
421
var oUploadAllowedExtRegex	= new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
422
var oUploadDeniedExtRegex	= new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
423
 
424
function CheckUpload()
425
{
426
	var sFile = GetE('txtUploadFile').value ;
427
 
428
	if ( sFile.length == 0 )
429
	{
430
		alert( 'Please select a file to upload' ) ;
431
		return false ;
432
	}
433
 
434
	if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
435
		( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
436
	{
437
		OnUploadCompleted( 202 ) ;
438
		return false ;
439
	}
440
 
441
	return true ;
442
}