Subversion Repositories Applications.papyrus

Rev

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