fck_image.js
上传用户:li2971742
上传日期:2021-11-18
资源大小:39096k
文件大小:12k
源码类别:

OA系统

开发平台:

C#

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