[ Index ]
 

Code source de FCKeditor 2.4

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/editor/dialog/fck_image/ -> fck_image.js (source)

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


Généré le : Sun Feb 25 15:28:05 2007 par Balluche grâce à PHPXref 0.7