2
* FCKeditor - The text editor for Internet - http://www.fckeditor.net
3
* Copyright (C) 2003-2007 Frederico Caldeira Knabben
7
* Licensed under the terms of any of the following licenses at your
10
* - GNU General Public License Version 2 or later (the "GPL")
11
* http://www.gnu.org/licenses/gpl.html
13
* - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14
* http://www.gnu.org/licenses/lgpl.html
16
* - Mozilla Public License Version 1.1 or later (the "MPL")
17
* http://www.mozilla.org/MPL/MPL-1.1.html
21
* Scripts related to the Image dialog window (see fck_image.html).
24
var oEditor = window.parent.InnerDialogLoaded() ;
25
var FCK = oEditor.FCK ;
26
var FCKLang = oEditor.FCKLang ;
27
var FCKConfig = oEditor.FCKConfig ;
28
var FCKDebug = oEditor.FCKDebug ;
30
var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
34
// Set the dialog tabs.
35
window.parent.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
37
if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
38
window.parent.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
40
if ( FCKConfig.ImageUpload )
41
window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
43
if ( !FCKConfig.ImageDlgHideAdvanced )
44
window.parent.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
46
// Function called when a dialog tag is selected.
47
function OnDialogTabChange( tabCode )
49
ShowE('divInfo' , ( tabCode == 'Info' ) ) ;
50
ShowE('divLink' , ( tabCode == 'Link' ) ) ;
51
ShowE('divUpload' , ( tabCode == 'Upload' ) ) ;
52
ShowE('divAdvanced' , ( tabCode == 'Advanced' ) ) ;
55
// Get the selected image (if available).
56
var oImage = FCK.Selection.GetSelectedElement() ;
58
if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
61
// Get the active link.
62
var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
66
function UpdateOriginal( resetSize )
71
if ( GetE('txtUrl').value.length == 0 )
73
oImageOriginal = null ;
77
oImageOriginal = document.createElement( 'IMG' ) ; // new Image() ;
81
oImageOriginal.onload = function()
88
oImageOriginal.src = eImgPreview.src ;
91
var bPreviewInitialized ;
93
window.onload = function()
95
// Translate the dialog box texts.
96
oEditor.FCKLanguageManager.TranslatePage(document) ;
98
GetE('btnLockSizes').title = FCKLang.DlgImgLockRatio ;
99
GetE('btnResetSize').title = FCKLang.DlgBtnResetSize ;
101
// Load the selected element information (if any).
104
// Show/Hide the "Browse Server" button.
105
GetE('tdBrowse').style.display = FCKConfig.ImageBrowser ? '' : 'none' ;
106
GetE('divLnkBrowseServer').style.display = FCKConfig.LinkBrowser ? '' : 'none' ;
110
// Set the actual uploader URL.
111
if ( FCKConfig.ImageUpload )
112
GetE('frmUpload').action = FCKConfig.ImageUploadURL ;
114
window.parent.SetAutoSize( true ) ;
116
// Activate the "OK" button.
117
window.parent.SetOkButton( true ) ;
120
function LoadSelection()
122
if ( ! oImage ) return ;
124
var sUrl = oImage.getAttribute( '_fcksavedurl' ) ;
126
sUrl = GetAttribute( oImage, 'src', '' ) ;
128
GetE('txtUrl').value = sUrl ;
129
GetE('txtAlt').value = GetAttribute( oImage, 'alt', '' ) ;
130
GetE('txtVSpace').value = GetAttribute( oImage, 'vspace', '' ) ;
131
GetE('txtHSpace').value = GetAttribute( oImage, 'hspace', '' ) ;
132
GetE('txtBorder').value = GetAttribute( oImage, 'border', '' ) ;
133
GetE('cmbAlign').value = GetAttribute( oImage, 'align', '' ) ;
135
var iWidth, iHeight ;
137
var regexSize = /^\s*(\d+)px\s*$/i ;
139
if ( oImage.style.width )
141
var aMatchW = oImage.style.width.match( regexSize ) ;
144
iWidth = aMatchW[1] ;
145
oImage.style.width = '' ;
146
SetAttribute( oImage, 'width' , iWidth ) ;
150
if ( oImage.style.height )
152
var aMatchH = oImage.style.height.match( regexSize ) ;
155
iHeight = aMatchH[1] ;
156
oImage.style.height = '' ;
157
SetAttribute( oImage, 'height', iHeight ) ;
161
GetE('txtWidth').value = iWidth ? iWidth : GetAttribute( oImage, "width", '' ) ;
162
GetE('txtHeight').value = iHeight ? iHeight : GetAttribute( oImage, "height", '' ) ;
164
// Get Advances Attributes
165
GetE('txtAttId').value = oImage.id ;
166
GetE('cmbAttLangDir').value = oImage.dir ;
167
GetE('txtAttLangCode').value = oImage.lang ;
168
GetE('txtAttTitle').value = oImage.title ;
169
GetE('txtLongDesc').value = oImage.longDesc ;
171
if ( oEditor.FCKBrowserInfo.IsIE )
173
GetE('txtAttClasses').value = oImage.className || '' ;
174
GetE('txtAttStyle').value = oImage.style.cssText ;
178
GetE('txtAttClasses').value = oImage.getAttribute('class',2) || '' ;
179
GetE('txtAttStyle').value = oImage.getAttribute('style',2) ;
184
var sLinkUrl = oLink.getAttribute( '_fcksavedurl' ) ;
185
if ( sLinkUrl == null )
186
sLinkUrl = oLink.getAttribute('href',2) ;
188
GetE('txtLnkUrl').value = sLinkUrl ;
189
GetE('cmbLnkTarget').value = oLink.target ;
195
//#### The OK button was hit.
198
if ( GetE('txtUrl').value.length == 0 )
200
window.parent.SetSelectedTab( 'Info' ) ;
201
GetE('txtUrl').focus() ;
203
alert( FCKLang.DlgImgAlertUrl ) ;
208
var bHasImage = ( oImage != null ) ;
210
if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
212
if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
215
else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
217
if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
221
oEditor.FCKUndo.SaveUndoStep() ;
226
oImage = FCK.EditorDocument.createElement( 'input' ) ;
227
oImage.type = 'image' ;
228
oImage = FCK.InsertElement( oImage ) ;
231
oImage = FCK.InsertElement( 'img' ) ;
234
UpdateImage( oImage ) ;
236
var sLnkUrl = GetE('txtLnkUrl').value.Trim() ;
238
if ( sLnkUrl.length == 0 )
241
FCK.ExecuteNamedCommand( 'Unlink' ) ;
245
if ( oLink ) // Modifying an existent link.
246
oLink.href = sLnkUrl ;
247
else // Creating a new link.
250
oEditor.FCKSelection.SelectNode( oImage ) ;
252
oLink = oEditor.FCK.CreateLink( sLnkUrl )[0] ;
256
oEditor.FCKSelection.SelectNode( oLink ) ;
257
oEditor.FCKSelection.Collapse( false ) ;
261
SetAttribute( oLink, '_fcksavedurl', sLnkUrl ) ;
262
SetAttribute( oLink, 'target', GetE('cmbLnkTarget').value ) ;
268
function UpdateImage( e, skipId )
270
e.src = GetE('txtUrl').value ;
271
SetAttribute( e, "_fcksavedurl", GetE('txtUrl').value ) ;
272
SetAttribute( e, "alt" , GetE('txtAlt').value ) ;
273
SetAttribute( e, "width" , GetE('txtWidth').value ) ;
274
SetAttribute( e, "height", GetE('txtHeight').value ) ;
275
SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
276
SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
277
SetAttribute( e, "border", GetE('txtBorder').value ) ;
278
SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
280
// Advances Attributes
283
SetAttribute( e, 'id', GetE('txtAttId').value ) ;
285
SetAttribute( e, 'dir' , GetE('cmbAttLangDir').value ) ;
286
SetAttribute( e, 'lang' , GetE('txtAttLangCode').value ) ;
287
SetAttribute( e, 'title' , GetE('txtAttTitle').value ) ;
288
SetAttribute( e, 'longDesc' , GetE('txtLongDesc').value ) ;
290
if ( oEditor.FCKBrowserInfo.IsIE )
292
e.className = GetE('txtAttClasses').value ;
293
e.style.cssText = GetE('txtAttStyle').value ;
297
SetAttribute( e, 'class' , GetE('txtAttClasses').value ) ;
298
SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
303
var eImgPreviewLink ;
305
function SetPreviewElements( imageElement, linkElement )
307
eImgPreview = imageElement ;
308
eImgPreviewLink = linkElement ;
313
bPreviewInitialized = true ;
316
function UpdatePreview()
318
if ( !eImgPreview || !eImgPreviewLink )
321
if ( GetE('txtUrl').value.length == 0 )
322
eImgPreviewLink.style.display = 'none' ;
325
UpdateImage( eImgPreview, true ) ;
327
if ( GetE('txtLnkUrl').value.Trim().length > 0 )
328
eImgPreviewLink.href = 'javascript:void(null);' ;
330
SetAttribute( eImgPreviewLink, 'href', '' ) ;
332
eImgPreviewLink.style.display = '' ;
336
var bLockRatio = true ;
338
function SwitchLock( lockButton )
340
bLockRatio = !bLockRatio ;
341
lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
342
lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
346
if ( GetE('txtWidth').value.length > 0 )
347
OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
349
OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
353
// Fired when the width or height input texts change
354
function OnSizeChanged( dimension, value )
356
// Verifies if the aspect ration has to be maintained
357
if ( oImageOriginal && bLockRatio )
359
var e = dimension == 'Width' ? GetE('txtHeight') : GetE('txtWidth') ;
361
if ( value.length == 0 || isNaN( value ) )
367
if ( dimension == 'Width' )
368
value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value / oImageOriginal.width ) ) ;
370
value = value == 0 ? 0 : Math.round( oImageOriginal.width * ( value / oImageOriginal.height ) ) ;
372
if ( !isNaN( value ) )
379
// Fired when the Reset Size button is clicked
380
function ResetSizes()
382
if ( ! oImageOriginal ) return ;
384
GetE('txtWidth').value = oImageOriginal.width ;
385
GetE('txtHeight').value = oImageOriginal.height ;
390
function BrowseServer()
394
FCKConfig.ImageBrowserURL,
395
FCKConfig.ImageBrowserWindowWidth,
396
FCKConfig.ImageBrowserWindowHeight ) ;
399
function LnkBrowseServer()
403
FCKConfig.LinkBrowserURL,
404
FCKConfig.LinkBrowserWindowWidth,
405
FCKConfig.LinkBrowserWindowHeight ) ;
408
function OpenServerBrowser( type, url, width, height )
410
sActualBrowser = type ;
411
OpenFileBrowser( url, width, height ) ;
416
function SetUrl( url, width, height, alt )
418
if ( sActualBrowser == 'Link' )
420
GetE('txtLnkUrl').value = url ;
425
GetE('txtUrl').value = url ;
426
GetE('txtWidth').value = width ? width : '' ;
427
GetE('txtHeight').value = height ? height : '' ;
430
GetE('txtAlt').value = alt;
433
UpdateOriginal( true ) ;
436
window.parent.SetSelectedTab( 'Info' ) ;
439
function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
441
switch ( errorNumber )
443
case 0 : // No errors
444
alert( 'Your file has been successfully uploaded' ) ;
446
case 1 : // Custom error
449
case 101 : // Custom warning
453
alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
456
alert( 'Invalid file type' ) ;
459
alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
462
alert( 'Error on file upload. Error number: ' + errorNumber ) ;
466
sActualBrowser = '' ;
468
GetE('frmUpload').reset() ;
471
var oUploadAllowedExtRegex = new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
472
var oUploadDeniedExtRegex = new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
474
function CheckUpload()
476
var sFile = GetE('txtUploadFile').value ;
478
if ( sFile.length == 0 )
480
alert( 'Please select a file to upload' ) ;
484
if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
485
( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
487
OnUploadCompleted( 202 ) ;