source: sandbox/filemanager/tp/fckeditor/editor/dialog/fck_image/fck_image.js @ 1575

Revision 1575, 12.8 KB checked in by amuller, 14 years ago (diff)

Ticket #597 - Implentação, melhorias do modulo gerenciador de arquivos

  • Property svn:executable set to *
Line 
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
24var dialog              = window.parent ;
25var oEditor             = dialog.InnerDialogLoaded() ;
26var FCK                 = oEditor.FCK ;
27var FCKLang             = oEditor.FCKLang ;
28var FCKConfig   = oEditor.FCKConfig ;
29var FCKDebug    = oEditor.FCKDebug ;
30var FCKTools    = oEditor.FCKTools ;
31
32var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
33
34//#### Dialog Tabs
35
36// Set the dialog tabs.
37dialog.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
38
39if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
40        dialog.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
41
42if ( FCKConfig.ImageUpload )
43        dialog.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
44
45if ( !FCKConfig.ImageDlgHideAdvanced )
46        dialog.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
47
48// Function called when a dialog tag is selected.
49function 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).
58var oImage = dialog.Selection.GetSelectedElement() ;
59
60if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
61        oImage = null ;
62
63// Get the active link.
64var oLink = dialog.Selection.GetSelection().MoveToAncestorNode( 'A' ) ;
65
66var oImageOriginal ;
67
68function 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
93var bPreviewInitialized ;
94
95window.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
124function 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        }
195
196        UpdatePreview() ;
197}
198
199//#### The OK button was hit.
200function Ok()
201{
202        if ( GetE('txtUrl').value.length == 0 )
203        {
204                dialog.SetSelectedTab( 'Info' ) ;
205                GetE('txtUrl').focus() ;
206
207                alert( FCKLang.DlgImgAlertUrl ) ;
208
209                return false ;
210        }
211
212        var bHasImage = ( oImage != null ) ;
213
214        if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
215        {
216                if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
217                        oImage = null ;
218        }
219        else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
220        {
221                if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
222                        oImage = null ;
223        }
224
225        oEditor.FCKUndo.SaveUndoStep() ;
226        if ( !bHasImage )
227        {
228                if ( bImageButton )
229                {
230                        oImage = FCK.EditorDocument.createElement( 'input' ) ;
231                        oImage.type = 'image' ;
232                        oImage = FCK.InsertElement( oImage ) ;
233                }
234                else
235                        oImage = FCK.InsertElement( 'img' ) ;
236        }
237
238        UpdateImage( oImage ) ;
239
240        var sLnkUrl = GetE('txtLnkUrl').value.Trim() ;
241
242        if ( sLnkUrl.length == 0 )
243        {
244                if ( oLink )
245                        FCK.ExecuteNamedCommand( 'Unlink' ) ;
246        }
247        else
248        {
249                if ( oLink )    // Modifying an existent link.
250                        oLink.href = sLnkUrl ;
251                else                    // Creating a new link.
252                {
253                        if ( !bHasImage )
254                                oEditor.FCKSelection.SelectNode( oImage ) ;
255
256                        oLink = oEditor.FCK.CreateLink( sLnkUrl )[0] ;
257
258                        if ( !bHasImage )
259                        {
260                                oEditor.FCKSelection.SelectNode( oLink ) ;
261                                oEditor.FCKSelection.Collapse( false ) ;
262                        }
263                }
264
265                SetAttribute( oLink, '_fcksavedurl', sLnkUrl ) ;
266                SetAttribute( oLink, 'target', GetE('cmbLnkTarget').value ) ;
267        }
268
269        return true ;
270}
271
272function UpdateImage( e, skipId )
273{
274        e.src = GetE('txtUrl').value ;
275        SetAttribute( e, "_fcksavedurl", GetE('txtUrl').value ) ;
276        SetAttribute( e, "alt"   , GetE('txtAlt').value ) ;
277        SetAttribute( e, "width" , GetE('txtWidth').value ) ;
278        SetAttribute( e, "height", GetE('txtHeight').value ) ;
279        SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
280        SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
281        SetAttribute( e, "border", GetE('txtBorder').value ) ;
282        SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
283
284        // Advances Attributes
285
286        if ( ! skipId )
287                SetAttribute( e, 'id', GetE('txtAttId').value ) ;
288
289        SetAttribute( e, 'dir'          , GetE('cmbAttLangDir').value ) ;
290        SetAttribute( e, 'lang'         , GetE('txtAttLangCode').value ) ;
291        SetAttribute( e, 'title'        , GetE('txtAttTitle').value ) ;
292        SetAttribute( e, 'longDesc'     , GetE('txtLongDesc').value ) ;
293
294        if ( oEditor.FCKBrowserInfo.IsIE )
295        {
296                e.className = GetE('txtAttClasses').value ;
297                e.style.cssText = GetE('txtAttStyle').value ;
298        }
299        else
300        {
301                SetAttribute( e, 'class'        , GetE('txtAttClasses').value ) ;
302                SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
303        }
304}
305
306var eImgPreview ;
307var eImgPreviewLink ;
308
309function SetPreviewElements( imageElement, linkElement )
310{
311        eImgPreview = imageElement ;
312        eImgPreviewLink = linkElement ;
313
314        UpdatePreview() ;
315        UpdateOriginal() ;
316
317        bPreviewInitialized = true ;
318}
319
320function UpdatePreview()
321{
322        if ( !eImgPreview || !eImgPreviewLink )
323                return ;
324
325        if ( GetE('txtUrl').value.length == 0 )
326                eImgPreviewLink.style.display = 'none' ;
327        else
328        {
329                UpdateImage( eImgPreview, true ) ;
330
331                if ( GetE('txtLnkUrl').value.Trim().length > 0 )
332                        eImgPreviewLink.href = 'javascript:void(null);' ;
333                else
334                        SetAttribute( eImgPreviewLink, 'href', '' ) ;
335
336                eImgPreviewLink.style.display = '' ;
337        }
338}
339
340var bLockRatio = true ;
341
342function SwitchLock( lockButton )
343{
344        bLockRatio = !bLockRatio ;
345        lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
346        lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
347
348        if ( bLockRatio )
349        {
350                if ( GetE('txtWidth').value.length > 0 )
351                        OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
352                else
353                        OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
354        }
355}
356
357// Fired when the width or height input texts change
358function OnSizeChanged( dimension, value )
359{
360        // Verifies if the aspect ration has to be maintained
361        if ( oImageOriginal && bLockRatio )
362        {
363                var e = dimension == 'Width' ? GetE('txtHeight') : GetE('txtWidth') ;
364
365                if ( value.length == 0 || isNaN( value ) )
366                {
367                        e.value = '' ;
368                        return ;
369                }
370
371                if ( dimension == 'Width' )
372                        value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value  / oImageOriginal.width ) ) ;
373                else
374                        value = value == 0 ? 0 : Math.round( oImageOriginal.width  * ( value / oImageOriginal.height ) ) ;
375
376                if ( !isNaN( value ) )
377                        e.value = value ;
378        }
379
380        UpdatePreview() ;
381}
382
383// Fired when the Reset Size button is clicked
384function ResetSizes()
385{
386        if ( ! oImageOriginal ) return ;
387        if ( oEditor.FCKBrowserInfo.IsGecko && !oImageOriginal.complete )
388        {
389                setTimeout( ResetSizes, 50 ) ;
390                return ;
391        }
392
393        GetE('txtWidth').value  = oImageOriginal.width ;
394        GetE('txtHeight').value = oImageOriginal.height ;
395
396        UpdatePreview() ;
397}
398
399function BrowseServer()
400{
401        OpenServerBrowser(
402                'Image',
403                FCKConfig.ImageBrowserURL,
404                FCKConfig.ImageBrowserWindowWidth,
405                FCKConfig.ImageBrowserWindowHeight ) ;
406}
407
408function LnkBrowseServer()
409{
410        OpenServerBrowser(
411                'Link',
412                FCKConfig.LinkBrowserURL,
413                FCKConfig.LinkBrowserWindowWidth,
414                FCKConfig.LinkBrowserWindowHeight ) ;
415}
416
417function OpenServerBrowser( type, url, width, height )
418{
419        sActualBrowser = type ;
420        OpenFileBrowser( url, width, height ) ;
421}
422
423var sActualBrowser ;
424
425function SetUrl( url, width, height, alt )
426{
427        if ( sActualBrowser == 'Link' )
428        {
429                GetE('txtLnkUrl').value = url ;
430                UpdatePreview() ;
431        }
432        else
433        {
434                GetE('txtUrl').value = url ;
435                GetE('txtWidth').value = width ? width : '' ;
436                GetE('txtHeight').value = height ? height : '' ;
437
438                if ( alt )
439                        GetE('txtAlt').value = alt;
440
441                UpdatePreview() ;
442                UpdateOriginal( true ) ;
443        }
444
445        dialog.SetSelectedTab( 'Info' ) ;
446}
447
448function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
449{
450        // Remove animation
451        window.parent.Throbber.Hide() ;
452        GetE( 'divUpload' ).style.display  = '' ;
453
454        switch ( errorNumber )
455        {
456                case 0 :        // No errors
457                        alert( 'Your file has been successfully uploaded' ) ;
458                        break ;
459                case 1 :        // Custom error
460                        alert( customMsg ) ;
461                        return ;
462                case 101 :      // Custom warning
463                        alert( customMsg ) ;
464                        break ;
465                case 201 :
466                        alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
467                        break ;
468                case 202 :
469                        alert( 'Invalid file type' ) ;
470                        return ;
471                case 203 :
472                        alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
473                        return ;
474                case 500 :
475                        alert( 'The connector is disabled' ) ;
476                        break ;
477                default :
478                        alert( 'Error on file upload. Error number: ' + errorNumber ) ;
479                        return ;
480        }
481
482        sActualBrowser = '' ;
483        SetUrl( fileUrl ) ;
484        GetE('frmUpload').reset() ;
485}
486
487var oUploadAllowedExtRegex      = new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
488var oUploadDeniedExtRegex       = new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
489
490function CheckUpload()
491{
492        var sFile = GetE('txtUploadFile').value ;
493
494        if ( sFile.length == 0 )
495        {
496                alert( 'Please select a file to upload' ) ;
497                return false ;
498        }
499
500        if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
501                ( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
502        {
503                OnUploadCompleted( 202 ) ;
504                return false ;
505        }
506
507        // Show animation
508        window.parent.Throbber.Show( 100 ) ;
509        GetE( 'divUpload' ).style.display  = 'none' ;
510
511        return true ;
512}
Note: See TracBrowser for help on using the repository browser.