PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/gui/tools/webmail/plugins/html_mail/fckeditor/editor/dialog/fck_image/fck_image.js

https://github.com/okbutton/ispCP-distributed
JavaScript | 442 lines | 320 code | 81 blank | 41 comment | 74 complexity | 33587042614bf7cb86bebcccc9402b28 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. /*
  2. * FCKeditor - The text editor for internet
  3. * Copyright (C) 2003-2005 Frederico Caldeira Knabben
  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. *
  11. * "Support Open Source software. What about a donation today?"
  12. *
  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. var oEditor = window.parent.InnerDialogLoaded() ;
  20. var FCK = oEditor.FCK ;
  21. var FCKLang = oEditor.FCKLang ;
  22. var FCKConfig = oEditor.FCKConfig ;
  23. var bImageButton = ( document.location.search.length > 0 && document.location.search.substr(1) == 'ImageButton' ) ;
  24. //#### Dialog Tabs
  25. // Set the dialog tabs.
  26. window.parent.AddTab( 'Info', FCKLang.DlgImgInfoTab ) ;
  27. if ( !bImageButton && !FCKConfig.ImageDlgHideLink )
  28. window.parent.AddTab( 'Link', FCKLang.DlgImgLinkTab ) ;
  29. if ( FCKConfig.ImageUpload )
  30. window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload ) ;
  31. if ( !FCKConfig.ImageDlgHideAdvanced )
  32. window.parent.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
  33. // Function called when a dialog tag is selected.
  34. function OnDialogTabChange( tabCode )
  35. {
  36. ShowE('divInfo' , ( tabCode == 'Info' ) ) ;
  37. ShowE('divLink' , ( tabCode == 'Link' ) ) ;
  38. ShowE('divUpload' , ( tabCode == 'Upload' ) ) ;
  39. ShowE('divAdvanced' , ( tabCode == 'Advanced' ) ) ;
  40. }
  41. // Get the selected image (if available).
  42. var oImage = FCK.Selection.GetSelectedElement() ;
  43. if ( oImage && oImage.tagName != 'IMG' && !( oImage.tagName == 'INPUT' && oImage.type == 'image' ) )
  44. oImage = null ;
  45. // Get the active link.
  46. var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ;
  47. var oImageOriginal ;
  48. function UpdateOriginal( resetSize )
  49. {
  50. oImageOriginal = document.createElement( 'IMG' ) ; // new Image() ;
  51. if ( resetSize )
  52. {
  53. oImageOriginal.onload = function()
  54. {
  55. this.onload = null ;
  56. ResetSizes() ;
  57. }
  58. }
  59. oImageOriginal.src = GetE('imgPreview').src ;
  60. }
  61. window.onload = function()
  62. {
  63. // Translate the dialog box texts.
  64. oEditor.FCKLanguageManager.TranslatePage(document) ;
  65. GetE('btnLockSizes').title = FCKLang.DlgImgLockRatio ;
  66. GetE('btnResetSize').title = FCKLang.DlgBtnResetSize ;
  67. // Load the selected element information (if any).
  68. LoadSelection() ;
  69. // Show/Hide the "Browse Server" button.
  70. GetE('tdBrowse').style.display = FCKConfig.ImageBrowser ? '' : 'none' ;
  71. GetE('divLnkBrowseServer').style.display = FCKConfig.LinkBrowser ? '' : 'none' ;
  72. UpdateOriginal() ;
  73. // Set the actual uploader URL.
  74. if ( FCKConfig.ImageUpload )
  75. GetE('frmUpload').action = FCKConfig.ImageUploadURL ;
  76. window.parent.SetAutoSize( true ) ;
  77. // Activate the "OK" button.
  78. window.parent.SetOkButton( true ) ;
  79. }
  80. function LoadSelection()
  81. {
  82. if ( ! oImage ) return ;
  83. var sUrl = GetAttribute( oImage, 'src', '' ) ;
  84. // TODO: Wait stable version and remove the following commented lines.
  85. // if ( sUrl.startsWith( FCK.BaseUrl ) )
  86. // sUrl = sUrl.remove( 0, FCK.BaseUrl.length ) ;
  87. GetE('txtUrl').value = sUrl ;
  88. GetE('txtAlt').value = GetAttribute( oImage, 'alt', '' ) ;
  89. GetE('txtVSpace').value = GetAttribute( oImage, 'vspace', '' ) ;
  90. GetE('txtHSpace').value = GetAttribute( oImage, 'hspace', '' ) ;
  91. GetE('txtBorder').value = GetAttribute( oImage, 'border', '' ) ;
  92. GetE('cmbAlign').value = GetAttribute( oImage, 'align', '' ) ;
  93. if ( oImage.style.pixelWidth > 0 )
  94. GetE('txtWidth').value = oImage.style.pixelWidth ;
  95. else
  96. GetE('txtWidth').value = GetAttribute( oImage, "width", '' ) ;
  97. if ( oImage.style.pixelHeight > 0 )
  98. GetE('txtHeight').value = oImage.style.pixelHeight ;
  99. else
  100. GetE('txtHeight').value = GetAttribute( oImage, "height", '' ) ;
  101. // Get Advances Attributes
  102. GetE('txtAttId').value = oImage.id ;
  103. GetE('cmbAttLangDir').value = oImage.dir ;
  104. GetE('txtAttLangCode').value = oImage.lang ;
  105. GetE('txtAttTitle').value = oImage.title ;
  106. GetE('txtAttClasses').value = oImage.getAttribute('class',2) || '' ;
  107. GetE('txtLongDesc').value = oImage.longDesc ;
  108. if ( oEditor.FCKBrowserInfo.IsIE )
  109. GetE('txtAttStyle').value = oImage.style.cssText ;
  110. else
  111. GetE('txtAttStyle').value = oImage.getAttribute('style',2) ;
  112. if ( oLink )
  113. {
  114. GetE('txtLnkUrl').value = oLink.getAttribute('href',2) ;
  115. GetE('cmbLnkTarget').value = oLink.target ;
  116. }
  117. UpdatePreview() ;
  118. }
  119. //#### The OK button was hit.
  120. function Ok()
  121. {
  122. if ( GetE('txtUrl').value.length == 0 )
  123. {
  124. window.parent.SetSelectedTab( 'Info' ) ;
  125. GetE('txtUrl').focus() ;
  126. alert( FCKLang.DlgImgAlertUrl ) ;
  127. return false ;
  128. }
  129. var bHasImage = ( oImage != null ) ;
  130. if ( bHasImage && bImageButton && oImage.tagName == 'IMG' )
  131. {
  132. if ( confirm( 'Do you want to transform the selected image on a image button?' ) )
  133. oImage = null ;
  134. }
  135. else if ( bHasImage && !bImageButton && oImage.tagName == 'INPUT' )
  136. {
  137. if ( confirm( 'Do you want to transform the selected image button on a simple image?' ) )
  138. oImage = null ;
  139. }
  140. if ( !bHasImage )
  141. {
  142. if ( bImageButton )
  143. {
  144. oImage = FCK.EditorDocument.createElement( 'INPUT' ) ;
  145. oImage.type = 'image' ;
  146. oImage = FCK.InsertElementAndGetIt( oImage ) ;
  147. }
  148. else
  149. oImage = FCK.CreateElement( 'IMG' ) ;
  150. }
  151. else
  152. oEditor.FCKUndo.SaveUndoStep() ;
  153. UpdateImage( oImage ) ;
  154. var sLnkUrl = GetE('txtLnkUrl').value.trim() ;
  155. if ( sLnkUrl.length == 0 )
  156. {
  157. if ( oLink )
  158. FCK.ExecuteNamedCommand( 'Unlink' ) ;
  159. }
  160. else
  161. {
  162. if ( oLink ) // Modifying an existent link.
  163. oLink.href = sLnkUrl ;
  164. else // Creating a new link.
  165. {
  166. if ( !bHasImage )
  167. oEditor.FCKSelection.SelectNode( oImage ) ;
  168. oLink = oEditor.FCK.CreateLink( sLnkUrl ) ;
  169. if ( !bHasImage )
  170. {
  171. oEditor.FCKSelection.SelectNode( oLink ) ;
  172. oEditor.FCKSelection.Collapse( false ) ;
  173. }
  174. }
  175. SetAttribute( oLink, 'target', GetE('cmbLnkTarget').value ) ;
  176. }
  177. return true ;
  178. }
  179. function UpdateImage( e, skipId )
  180. {
  181. e.src = GetE('txtUrl').value ;
  182. SetAttribute( e, "alt" , GetE('txtAlt').value ) ;
  183. SetAttribute( e, "width" , GetE('txtWidth').value ) ;
  184. SetAttribute( e, "height", GetE('txtHeight').value ) ;
  185. SetAttribute( e, "vspace", GetE('txtVSpace').value ) ;
  186. SetAttribute( e, "hspace", GetE('txtHSpace').value ) ;
  187. SetAttribute( e, "border", GetE('txtBorder').value ) ;
  188. SetAttribute( e, "align" , GetE('cmbAlign').value ) ;
  189. // Advances Attributes
  190. if ( ! skipId )
  191. SetAttribute( e, 'id', GetE('txtAttId').value ) ;
  192. SetAttribute( e, 'dir' , GetE('cmbAttLangDir').value ) ;
  193. SetAttribute( e, 'lang' , GetE('txtAttLangCode').value ) ;
  194. SetAttribute( e, 'title' , GetE('txtAttTitle').value ) ;
  195. SetAttribute( e, 'class' , GetE('txtAttClasses').value ) ;
  196. SetAttribute( e, 'longDesc' , GetE('txtLongDesc').value ) ;
  197. if ( oEditor.FCKBrowserInfo.IsIE )
  198. e.style.cssText = GetE('txtAttStyle').value ;
  199. else
  200. SetAttribute( e, 'style', GetE('txtAttStyle').value ) ;
  201. }
  202. function UpdatePreview()
  203. {
  204. if ( GetE('txtUrl').value.length == 0 )
  205. GetE('lnkPreview').style.display = 'none' ;
  206. else
  207. {
  208. UpdateImage( GetE('imgPreview'), true ) ;
  209. if ( GetE('txtLnkUrl').value.trim().length > 0 )
  210. GetE('lnkPreview').href = 'javascript:void(null);' ;
  211. else
  212. SetAttribute( GetE('lnkPreview'), 'href', '' ) ;
  213. GetE('lnkPreview').style.display = '' ;
  214. }
  215. }
  216. var bLockRatio = true ;
  217. function SwitchLock( lockButton )
  218. {
  219. bLockRatio = !bLockRatio ;
  220. lockButton.className = bLockRatio ? 'BtnLocked' : 'BtnUnlocked' ;
  221. lockButton.title = bLockRatio ? 'Lock sizes' : 'Unlock sizes' ;
  222. if ( bLockRatio )
  223. {
  224. if ( GetE('txtWidth').value.length > 0 )
  225. OnSizeChanged( 'Width', GetE('txtWidth').value ) ;
  226. else
  227. OnSizeChanged( 'Height', GetE('txtHeight').value ) ;
  228. }
  229. }
  230. // Fired when the width or height input texts change
  231. function OnSizeChanged( dimension, value )
  232. {
  233. // Verifies if the aspect ration has to be mantained
  234. if ( oImageOriginal && bLockRatio )
  235. {
  236. if ( value.length == 0 || isNaN( value ) )
  237. {
  238. GetE('txtHeight').value = GetE('txtWidth').value = '' ;
  239. return ;
  240. }
  241. if ( dimension == 'Width' )
  242. GetE('txtHeight').value = value == 0 ? 0 : Math.round( oImageOriginal.height * ( value / oImageOriginal.width ) ) ;
  243. else
  244. GetE('txtWidth').value = value == 0 ? 0 : Math.round( oImageOriginal.width * ( value / oImageOriginal.height ) ) ;
  245. }
  246. UpdatePreview() ;
  247. }
  248. // Fired when the Reset Size button is clicked
  249. function ResetSizes()
  250. {
  251. if ( ! oImageOriginal ) return ;
  252. GetE('txtWidth').value = oImageOriginal.width ;
  253. GetE('txtHeight').value = oImageOriginal.height ;
  254. UpdatePreview() ;
  255. }
  256. function BrowseServer()
  257. {
  258. OpenServerBrowser(
  259. 'Image',
  260. FCKConfig.ImageBrowserURL,
  261. FCKConfig.ImageBrowserWindowWidth,
  262. FCKConfig.ImageBrowserWindowHeight ) ;
  263. }
  264. function LnkBrowseServer()
  265. {
  266. OpenServerBrowser(
  267. 'Link',
  268. FCKConfig.LinkBrowserURL,
  269. FCKConfig.LinkBrowserWindowWidth,
  270. FCKConfig.LinkBrowserWindowHeight ) ;
  271. }
  272. function OpenServerBrowser( type, url, width, height )
  273. {
  274. sActualBrowser = type ;
  275. var iLeft = (FCKConfig.ScreenWidth - width) / 2 ;
  276. var iTop = (FCKConfig.ScreenHeight - height) / 2 ;
  277. var sOptions = "toolbar=no,status=no,resizable=yes,dependent=yes" ;
  278. sOptions += ",width=" + width ;
  279. sOptions += ",height=" + height ;
  280. sOptions += ",left=" + iLeft ;
  281. sOptions += ",top=" + iTop ;
  282. if ( oEditor.FCKBrowserInfo.IsIE )
  283. {
  284. // The following change has been made otherwise IE will open the file
  285. // browser on a different server session (on some cases):
  286. // http://support.microsoft.com/default.aspx?scid=kb;en-us;831678
  287. // by Simone Chiaretta.
  288. var oWindow = oEditor.window.open( url, "FCKBrowseWindow", sOptions ) ;
  289. oWindow.opener = window ;
  290. }
  291. else
  292. window.open( url, "FCKBrowseWindow", sOptions ) ;
  293. }
  294. var sActualBrowser ;
  295. function SetUrl( url, width, height, alt )
  296. {
  297. if ( sActualBrowser == 'Link' )
  298. {
  299. GetE('txtLnkUrl').value = url ;
  300. UpdatePreview() ;
  301. }
  302. else
  303. {
  304. GetE('txtUrl').value = url ;
  305. GetE('txtWidth').value = width ? width : '' ;
  306. GetE('txtHeight').value = height ? height : '' ;
  307. if ( alt )
  308. GetE('txtAlt').value = alt;
  309. UpdatePreview() ;
  310. UpdateOriginal( true ) ;
  311. }
  312. window.parent.SetSelectedTab( 'Info' ) ;
  313. }
  314. function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
  315. {
  316. switch ( errorNumber )
  317. {
  318. case 0 : // No errors
  319. alert( 'Your file has been successfully uploaded' ) ;
  320. break ;
  321. case 1 : // Custom error
  322. alert( customMsg ) ;
  323. return ;
  324. case 101 : // Custom warning
  325. alert( customMsg ) ;
  326. break ;
  327. case 201 :
  328. alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ;
  329. break ;
  330. case 202 :
  331. alert( 'Invalid file type' ) ;
  332. return ;
  333. case 203 :
  334. alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ;
  335. return ;
  336. default :
  337. alert( 'Error on file upload. Error number: ' + errorNumber ) ;
  338. return ;
  339. }
  340. sActualBrowser = ''
  341. SetUrl( fileUrl ) ;
  342. GetE('frmUpload').reset() ;
  343. }
  344. var oUploadAllowedExtRegex = new RegExp( FCKConfig.ImageUploadAllowedExtensions, 'i' ) ;
  345. var oUploadDeniedExtRegex = new RegExp( FCKConfig.ImageUploadDeniedExtensions, 'i' ) ;
  346. function CheckUpload()
  347. {
  348. var sFile = GetE('txtUploadFile').value ;
  349. if ( sFile.length == 0 )
  350. {
  351. alert( 'Please select a file to upload' ) ;
  352. return false ;
  353. }
  354. if ( ( FCKConfig.ImageUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
  355. ( FCKConfig.ImageUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
  356. {
  357. OnUploadCompleted( 202 ) ;
  358. return false ;
  359. }
  360. return true ;
  361. }