Project

General

Profile

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 Image dialog window (see fck_image.html).
22
 */
23

    
24
var dialog		= window.parent ;
25
var oEditor		= dialog.InnerDialogLoaded() ;
26
var FCK			= oEditor.FCK ;
27
var FCKLang		= oEditor.FCKLang ;
28
var FCKConfig	= oEditor.FCKConfig ;
29
var FCKDebug	= oEditor.FCKDebug ;
30
var FCKTools	= oEditor.FCKTools ;
31

    
32
var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
33

    
34
//#### Dialog Tabs
35

    
36
// Set the dialog tabs.
37
dialog.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
38

    
39
if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
40
	dialog.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
41

    
42
if ( FCKConfig.ImageUpload )
43
	dialog.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
44

    
45
if ( !FCKConfig.ImageDlgHideAdvanced )
46
	dialog.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
47

    
48
// Function called when a dialog tag is selected.
49
function OnDialogTabChange( tabCode )
50
{
51
	ShowE('divInfo'		, ( tabCode == 'Info' ) ) ;
52
	ShowE('divLink'		, ( tabCode == 'Link' ) ) ;
53
	ShowE('divUpload'	, ( tabCode == 'Upload' ) ) ;
54
	ShowE('divAdvanced'	, ( tabCode == 'Advanced' ) ) ;
55
}
56

    
57
// Get the selected image (if available).
58
var oImage = dialog.Selection.GetSelectedElement() ;
59

    
60
if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
61
	oImage = null ;
62

    
63
// Get the active link.
64
var oLink = dialog.Selection.GetSelection().MoveToAncestorNode( 'A' ) ;
65

    
66
var oImageOriginal ;
67

    
68
function UpdateOriginal( resetSize )
69
{
70
	if ( !eImgPreview )
71
		return ;
72

    
73
	if ( GetE('txtUrl').value.length == 0 )
74
	{
75
		oImageOriginal = null ;
76
		return ;
77
	}
78

    
79
	oImageOriginal = document.createElement( 'IMG' ) ;	// new Image() ;
80

    
81
	if ( resetSize )
82
	{
83
		oImageOriginal.onload = function()
84
		{
85
			this.onload = null ;
86
			ResetSizes() ;
87
		}
88
	}
89

    
90
	oImageOriginal.src = eImgPreview.src ;
91
}
92

    
93
var bPreviewInitialized ;
94

    
95
window.onload = function()
96
{
97
	// Translate the dialog box texts.
98
	oEditor.FCKLanguageManager.TranslatePage(document) ;
99

    
100
	GetE('btnLockSizes').title = FCKLang.DlgImgLockRatio ;
101
	GetE('btnResetSize').title = FCKLang.DlgBtnResetSize ;
102

    
103
	// Load the selected element information (if any).
104
	LoadSelection() ;
105

    
106
	// Show/Hide the "Browse Server" button.
107
	GetE('tdBrowse').style.display				= FCKConfig.ImageBrowser	? '' : 'none' ;
108
	GetE('divLnkBrowseServer').style.display	= FCKConfig.LinkBrowser		? '' : 'none' ;
109

    
110
	UpdateOriginal() ;
111

    
112
	// Set the actual uploader URL.
113
	if ( FCKConfig.ImageUpload )
114
		GetE('frmUpload').action = FCKConfig.ImageUploadURL ;
115

    
116
	dialog.SetAutoSize( true ) ;
117

    
118
	// Activate the "OK" button.
119
	dialog.SetOkButton( true ) ;
120

    
121
	SelectField( 'txtUrl' ) ;
122
}
123

    
124
function LoadSelection()
125
{
126
	if ( ! oImage ) return ;
127

    
128
	var sUrl = oImage.getAttribute( '_fcksavedurl' ) ;
129
	if ( sUrl == null )
130
		sUrl = GetAttribute( oImage, 'src', '' ) ;
131

    
132
	GetE('txtUrl').value    = sUrl ;
133
	GetE('txtAlt').value    = GetAttribute( oImage, 'alt', '' ) ;
134
	GetE('txtVSpace').value	= GetAttribute( oImage, 'vspace', '' ) ;
135
	GetE('txtHSpace').value	= GetAttribute( oImage, 'hspace', '' ) ;
136
	GetE('txtBorder').value	= GetAttribute( oImage, 'border', '' ) ;
137
	GetE('cmbAlign').value	= GetAttribute( oImage, 'align', '' ) ;
138

    
139
	var iWidth, iHeight ;
140

    
141
	var regexSize = /^\s*(\d+)px\s*$/i ;
142

    
143
	if ( oImage.style.width )
144
	{
145
		var aMatchW  = oImage.style.width.match( regexSize ) ;
146
		if ( aMatchW )
147
		{
148
			iWidth = aMatchW[1] ;
149
			oImage.style.width = '' ;
150
			SetAttribute( oImage, 'width' , iWidth ) ;
151
		}
152
	}
153

    
154
	if ( oImage.style.height )
155
	{
156
		var aMatchH  = oImage.style.height.match( regexSize ) ;
157
		if ( aMatchH )
158
		{
159
			iHeight = aMatchH[1] ;
160
			oImage.style.height = '' ;
161
			SetAttribute( oImage, 'height', iHeight ) ;
162
		}
163
	}
164

    
165
	GetE('txtWidth').value	= iWidth ? iWidth : GetAttribute( oImage, "width", '' ) ;
166
	GetE('txtHeight').value	= iHeight ? iHeight : GetAttribute( oImage, "height", '' ) ;
167

    
168
	// Get Advances Attributes
169
	GetE('txtAttId').value			= oImage.id ;
170
	GetE('cmbAttLangDir').value		= oImage.dir ;
171
	GetE('txtAttLangCode').value	= oImage.lang ;
172
	GetE('txtAttTitle').value		= oImage.title ;
173
	GetE('txtLongDesc').value		= oImage.longDesc ;
174

    
175
	if ( oEditor.FCKBrowserInfo.IsIE )
176
	{
177
		GetE('txtAttClasses').value = oImage.className || '' ;
178
		GetE('txtAttStyle').value = oImage.style.cssText ;
179
	}
180
	else
181
	{
182
		GetE('txtAttClasses').value = oImage.getAttribute('class',2) || '' ;
183
		GetE('txtAttStyle').value = oImage.getAttribute('style',2) ;
184
	}
185

    
186
	if ( oLink )
187
	{
188
		var sLinkUrl = oLink.getAttribute( '_fcksavedurl' ) ;
189
		if ( sLinkUrl == null )
190
			sLinkUrl = oLink.getAttribute('href',2) ;
191

    
192
		GetE('txtLnkUrl').value		= sLinkUrl ;
193
		GetE('cmbLnkTarget').value	= oLink.target ;
194
		GetE('cmbAttContentRel').value	= oLink.rel ;
195
		GetE('txtAttTitle').value		= oLink.title ;
196
		GetE('txtAttId').value			= oLink.id ;
197
	var sClass ;
198
	if ( oEditor.FCKBrowserInfo.IsIE )
199
	{
200
		sClass	= oLink.getAttribute('className',2) || '' ;
201
		// Clean up temporary classes for internal use:
202
		sClass = sClass.replace( FCKRegexLib.FCK_Class, '' ) ;
203

    
204
		GetE('txtAttStyle').value	= oLink.style.cssText ;
205
	}
206
	else
207
	{
208
		sClass	= oLink.getAttribute('class',2) || '' ;
209
		GetE('txtAttStyle').value	= oLink.getAttribute('style',2) || '' ;
210
	}
211
	GetE('txtAttClasses').value	= sClass ;
212

    
213
		}
214

    
215
	UpdatePreview() ;
216
}
217

    
218
//#### The OK button was hit.
219
function Ok()
220
{
221
	if ( GetE('txtUrl').value.length == 0 )
222
	{
223
		dialog.SetSelectedTab( 'Info' ) ;
224
		GetE('txtUrl').focus() ;
225

    
226
		alert( FCKLang.DlgImgAlertUrl ) ;
227

    
228
		return false ;
229
	}
230

    
231
	var bHasImage = ( oImage != null ) ;
232

    
233
	if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
234
	{
235
		if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
236
			oImage = null ;
237
	}
238
	else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
239
	{
240
		if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
241
			oImage = null ;
242
	}
243

    
244
	oEditor.FCKUndo.SaveUndoStep() ;
245
	if ( !bHasImage )
246
	{
247
		if ( bImageButton )
248
		{
249
			oImage = FCK.EditorDocument.createElement( 'input' ) ;
250
			oImage.type = 'image' ;
251
			oImage = FCK.InsertElement( oImage ) ;
252
		}
253
		else
254
			oImage = FCK.InsertElement( 'img' ) ;
255
	}
256

    
257
	UpdateImage( oImage ) ;
258

    
259
	var sLnkUrl = GetE('txtLnkUrl').value.Trim() ;
260

    
261
	if ( sLnkUrl.length == 0 )
262
	{
263
		if ( oLink )
264
			FCK.ExecuteNamedCommand( 'Unlink' ) ;
265
	}
266
	else
267
	{
268
		if ( oLink )	// Modifying an existent link.
269
			oLink.href = sLnkUrl ;
270
		else			// Creating a new link.
271
		{
272
			if ( !bHasImage )
273
				oEditor.FCKSelection.SelectNode( oImage ) ;
274

    
275
			oLink = oEditor.FCK.CreateLink( sLnkUrl )[0] ;
276

    
277
			if ( !bHasImage )
278
			{
279
				oEditor.FCKSelection.SelectNode( oLink ) ;
280
				oEditor.FCKSelection.Collapse( false ) ;
281
			}
282
		}
283

    
284
		SetAttribute( oLink, '_fcksavedurl', sLnkUrl ) ;
285
		SetAttribute( oLink, 'target', 	GetE('cmbLnkTarget').value ) ;
286
		SetAttribute( oLink, 'rel', 	GetE('cmbAttContentRel').value ) ;
287
		SetAttribute( oLink, 'id', 		GetE('txtAttId').value ) ;
288
		SetAttribute( oLink, 'title', 	GetE('txtAttTitle').value ) ;
289

    
290
		if ( oEditor.FCKBrowserInfo.IsIE )
291
		{
292
			var sClass = GetE('txtAttClasses').value ;
293
			// If it's also an anchor add an internal class
294
			if ( GetE('txtAttName').value.length != 0 )
295
				sClass += ' FCK__AnchorC' ;
296
			SetAttribute( oLink, 'className', sClass ) ;
297

    
298
			oLink.style.cssText = GetE('txtAttStyle').value ;
299
		}
300
		else
301
		{
302
			SetAttribute( oLink, 'class', GetE('txtAttClasses').value ) ;
303
			SetAttribute( oLink, 'style', GetE('txtAttStyle').value ) ;
304
		}
305
		
306
	}
307

    
308
	return true ;
309
}
310

    
311
function UpdateImage( e, skipId )
312
{
313
	e.src = GetE('txtUrl').value ;
314
	SetAttribute( e, "_fcksavedurl", GetE('txtUrl').value ) ;
315
	SetAttribute( e, "alt"   , GetE('txtAlt').value ) ;
316
	SetAttribute( e, "width" , GetE('txtWidth').value ) ;
317
	SetAttribute( e, "height", GetE('txtHeight').value ) ;
318
	SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
319
	SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
320
	SetAttribute( e, "border", GetE('txtBorder').value ) ;
321
	SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
322

    
323
	// Advances Attributes
324

    
325
	if ( ! skipId )
326
		SetAttribute( e, 'id', GetE('txtAttId').value ) ;
327

    
328
	SetAttribute( e, 'dir'		, GetE('cmbAttLangDir').value ) ;
329
	SetAttribute( e, 'lang'		, GetE('txtAttLangCode').value ) ;
330
	SetAttribute( e, 'title'	, GetE('txtAttTitle').value ) ;
331
	SetAttribute( e, 'longDesc'	, GetE('txtLongDesc').value ) ;
332

    
333
	if ( oEditor.FCKBrowserInfo.IsIE )
334
	{
335
		e.className = GetE('txtAttClasses').value ;
336
		e.style.cssText = GetE('txtAttStyle').value ;
337
	}
338
	else
339
	{
340
		SetAttribute( e, 'class'	, GetE('txtAttClasses').value ) ;
341
		SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
342
	}
343
}
344

    
345
var eImgPreview ;
346
var eImgPreviewLink ;
347

    
348
function SetPreviewElements( imageElement, linkElement )
349
{
350
	eImgPreview = imageElement ;
351
	eImgPreviewLink = linkElement ;
352

    
353
	UpdatePreview() ;
354
	UpdateOriginal() ;
355

    
356
	bPreviewInitialized = true ;
357
}
358

    
359
function UpdatePreview()
360
{
361
	if ( !eImgPreview || !eImgPreviewLink )
362
		return ;
363

    
364
	if ( GetE('txtUrl').value.length == 0 )
365
		eImgPreviewLink.style.display = 'none' ;
366
	else
367
	{
368
		UpdateImage( eImgPreview, true ) ;
369

    
370
		if ( GetE('txtLnkUrl').value.Trim().length > 0 )
371
			eImgPreviewLink.href = 'javascript:void(null);' ;
372
		else
373
			SetAttribute( eImgPreviewLink, 'href', '' ) ;
374

    
375
		eImgPreviewLink.style.display = '' ;
376
	}
377
}
378

    
379
var bLockRatio = true ;
380

    
381
function SwitchLock( lockButton )
382
{
383
	bLockRatio = !bLockRatio ;
384
	lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
385
	lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
386

    
387
	if ( bLockRatio )
388
	{
389
		if ( GetE('txtWidth').value.length > 0 )
390
			OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
391
		else
392
			OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
393
	}
394
}
395

    
396
// Fired when the width or height input texts change
397
function OnSizeChanged( dimension, value )
398
{
399
	// Verifies if the aspect ration has to be maintained
400
	if ( oImageOriginal && bLockRatio )
401
	{
402
		var e = dimension == 'Width' ? GetE('txtHeight') : GetE('txtWidth') ;
403

    
404
		if ( value.length == 0 || isNaN( value ) )
405
		{
406
			e.value = '' ;
407
			return ;
408
		}
409

    
410
		if ( dimension == 'Width' )
411
			value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value  / oImageOriginal.width ) ) ;
412
		else
413
			value = value == 0 ? 0 : Math.round( oImageOriginal.width  * ( value / oImageOriginal.height ) ) ;
414

    
415
		if ( !isNaN( value ) )
416
			e.value = value ;
417
	}
418

    
419
	UpdatePreview() ;
420
}
421

    
422
// Fired when the Reset Size button is clicked
423
function ResetSizes()
424
{
425
	if ( ! oImageOriginal ) return ;
426
	if ( oEditor.FCKBrowserInfo.IsGecko && !oImageOriginal.complete )
427
	{
428
		setTimeout( ResetSizes, 50 ) ;
429
		return ;
430
	}
431

    
432
	GetE('txtWidth').value  = oImageOriginal.width ;
433
	GetE('txtHeight').value = oImageOriginal.height ;
434

    
435
	UpdatePreview() ;
436
}
437

    
438
function BrowseServer()
439
{
440
	OpenServerBrowser(
441
		'Image',
442
		FCKConfig.ImageBrowserURL,
443
		FCKConfig.ImageBrowserWindowWidth,
444
		FCKConfig.ImageBrowserWindowHeight ) ;
445
}
446

    
447
function LnkBrowseServer()
448
{
449
	OpenServerBrowser(
450
		'Link',
451
		FCKConfig.LinkBrowserURL,
452
		FCKConfig.LinkBrowserWindowWidth,
453
		FCKConfig.LinkBrowserWindowHeight ) ;
454
}
455

    
456
function OpenServerBrowser( type, url, width, height )
457
{
458
	sActualBrowser = type ;
459
	OpenFileBrowser( url, width, height ) ;
460
}
461

    
462
var sActualBrowser ;
463

    
464
function SetUrl( url, width, height, alt )
465
{
466
	if ( sActualBrowser == 'Link' )
467
	{
468
		GetE('txtLnkUrl').value = url ;
469
		UpdatePreview() ;
470
	}
471
	else
472
	{
473
		GetE('txtUrl').value = url ;
474
		GetE('txtWidth').value = width ? width : '' ;
475
		GetE('txtHeight').value = height ? height : '' ;
476

    
477
		if ( alt )
478
			GetE('txtAlt').value = alt;
479

    
480
		UpdatePreview() ;
481
		UpdateOriginal( true ) ;
482
	}
483

    
484
	dialog.SetSelectedTab( 'Info' ) ;
485
}
486

    
487
function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
488
{
489
	// Remove animation
490
	window.parent.Throbber.Hide() ;
491
	GetE( 'divUpload' ).style.display  = '' ;
492

    
493
	switch ( errorNumber )
494
	{
495
		case 0 :	// No errors
496
			alert( 'Your file has been successfully uploaded' ) ;
497
			break ;
498
		case 1 :	// Custom error
499
			alert( customMsg ) ;
500
			return ;
501
		case 101 :	// Custom warning
502
			alert( customMsg ) ;
503
			break ;
504
		case 201 :
505
			alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
506
			break ;
507
		case 202 :
508
			alert( 'Invalid file type' ) ;
509
			return ;
510
		case 203 :
511
			alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
512
			return ;
513
		case 500 :
514
			alert( 'The connector is disabled' ) ;
515
			break ;
516
		default :
517
			alert( 'Error on file upload. Error number: ' + errorNumber ) ;
518
			return ;
519
	}
520

    
521
	sActualBrowser = '' ;
522
	SetUrl( fileUrl ) ;
523
	GetE('frmUpload').reset() ;
524
}
525

    
526
var oUploadAllowedExtRegex	= new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
527
var oUploadDeniedExtRegex	= new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
528

    
529
function CheckUpload()
530
{
531
	var sFile = GetE('txtUploadFile').value ;
532

    
533
	if ( sFile.length == 0 )
534
	{
535
		alert( 'Please select a file to upload' ) ;
536
		return false ;
537
	}
538

    
539
	if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
540
		( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
541
	{
542
		OnUploadCompleted( 202 ) ;
543
		return false ;
544
	}
545

    
546
	// Show animation
547
	window.parent.Throbber.Show( 100 ) ;
548
	GetE( 'divUpload' ).style.display  = 'none' ;
549

    
550
	return true ;
551
}
(1-1/3)