PageRenderTime 34ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Trunk/WebEditor/editor/dialog/fck_link/fck_link.js

#
JavaScript | 290 lines | 228 code | 44 blank | 18 comment | 112 complexity | 068aa63ffe51906cb8fb3c3dd3b3ef29 MD5 | raw file
Possible License(s): LGPL-2.1
  1. /** FCKeditor - The text editor for internet
  2. * Copyright (C) 2003-2006 Frederico Caldeira Knabben
  3. *
  4. * Licensed under the terms of the GNU Lesser General Public License:
  5. * http:
  6. *
  7. * For further information visit:
  8. * http:
  9. *
  10. * "Support Open Source software. What about a donation today?"
  11. *
  12. * File Name: fck_link.js
  13. * Scripts related to the Link dialog window (see fck_link.html).
  14. *
  15. * File Authors:
  16. * Frederico Caldeira Knabben (fredck@fckeditor.net)
  17. * Dominik Pesch ?dom? (empty selection patch) (d.pesch@11com7.de)
  18. */
  19. var oEditor = window.parent.InnerDialogLoaded() ; var FCK = oEditor.FCK ; var FCKLang = oEditor.FCKLang ; var FCKConfig = oEditor.FCKConfig ;
  20. window.parent.AddTab( 'Info', FCKLang.DlgLnkInfoTab ) ;
  21. if ( !FCKConfig.LinkDlgHideTarget )
  22. window.parent.AddTab( 'Target', FCKLang.DlgLnkTargetTab, true ) ;
  23. if ( FCKConfig.LinkUpload )
  24. window.parent.AddTab( 'Upload', FCKLang.DlgLnkUpload, true ) ;
  25. if ( !FCKConfig.LinkDlgHideAdvanced )
  26. window.parent.AddTab( 'Advanced', FCKLang.DlgAdvancedTag ) ;
  27. function OnDialogTabChange( tabCode )
  28. { ShowE('divInfo' , ( tabCode == 'Info' ) ) ; ShowE('divTarget' , ( tabCode == 'Target' ) ) ; ShowE('divUpload' , ( tabCode == 'Upload' ) ) ; ShowE('divAttribs' , ( tabCode == 'Advanced' ) ) ;
  29. window.parent.SetAutoSize( true ) ; }
  30. var oRegex = new Object() ;
  31. oRegex.UriProtocol = new RegExp('') ; oRegex.UriProtocol.compile( '^(((http|https|ftp|news):\/\/)|mailto:)', 'gi' ) ;
  32. oRegex.UrlOnChangeProtocol = new RegExp('') ; oRegex.UrlOnChangeProtocol.compile( '^(http|https|ftp|news)://(?=.)', 'gi' ) ;
  33. oRegex.UrlOnChangeTestOther = new RegExp('') ; oRegex.UrlOnChangeTestOther.compile( '^((javascript:)|[#/\.])', 'gi' ) ;
  34. oRegex.ReserveTarget = new RegExp('') ; oRegex.ReserveTarget.compile( '^_(blank|self|top|parent)$', 'i' ) ;
  35. oRegex.PopupUri = new RegExp('') ; oRegex.PopupUri.compile( "^javascript:void\\(\\s*window.open\\(\\s*'([^']+)'\\s*,\\s*(?:'([^']*)'|null)\\s*,\\s*'([^']*)'\\s*\\)\\s*\\)\\s*$" ) ;
  36. oRegex.PopupFeatures = new RegExp('') ; oRegex.PopupFeatures.compile( '(?:^|,)([^=]+)=(\\d+|yes|no)', 'gi' ) ;
  37. var oParser = new Object() ;
  38. oParser.ParseEMailUrl = function( emailUrl )
  39. { var oEMailInfo = new Object() ; oEMailInfo.Address = '' ; oEMailInfo.Subject = '' ; oEMailInfo.Body = '' ;
  40. var oParts = emailUrl.match( /^([^\?]+)\??(.+)?/ ) ; if ( oParts )
  41. { oEMailInfo.Address = oParts[1] ;
  42. if ( oParts[2] )
  43. { var oMatch = oParts[2].match( /(^|&)subject=([^&]+)/i ) ; if ( oMatch ) oEMailInfo.Subject = unescape( oMatch[2] ) ;
  44. oMatch = oParts[2].match( /(^|&)body=([^&]+)/i ) ; if ( oMatch ) oEMailInfo.Body = unescape( oMatch[2] ) ; }
  45. }
  46. return oEMailInfo ; }
  47. oParser.CreateEMailUri = function( address, subject, body )
  48. { var sBaseUri = 'mailto:' + address ;
  49. var sParams = '' ;
  50. if ( subject.length > 0 )
  51. sParams = '?subject=' + escape( subject ) ;
  52. if ( body.length > 0 )
  53. { sParams += ( sParams.length == 0 ? '?' : '&' ) ; sParams += 'body=' + escape( body ) ; }
  54. return sBaseUri + sParams ; }
  55. var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ; if ( oLink )
  56. FCK.Selection.SelectNode( oLink ) ;
  57. window.onload = function()
  58. { oEditor.FCKLanguageManager.TranslatePage(document) ;
  59. LoadAnchorNamesAndIds() ;
  60. LoadSelection() ;
  61. SetLinkType( GetE('cmbLinkType').value ) ;
  62. GetE('divBrowseServer').style.display = FCKConfig.LinkBrowser ? '' : 'none' ;
  63. GetE('divInfo').style.display = '' ;
  64. if ( FCKConfig.LinkUpload )
  65. GetE('frmUpload').action = FCKConfig.LinkUploadURL ;
  66. window.parent.SetOkButton( true ) ; }
  67. var bHasAnchors ;
  68. function LoadAnchorNamesAndIds()
  69. { var aAnchors = new Array() ;
  70. var oImages = oEditor.FCK.EditorDocument.getElementsByTagName( 'IMG' ) ; for( var i = 0 ; i < oImages.length ; i++ )
  71. { if ( oImages[i].getAttribute('_fckanchor') )
  72. aAnchors[ aAnchors.length ] = oEditor.FCK.GetRealElement( oImages[i] ) ; }
  73. var aIds = oEditor.FCKTools.GetAllChildrenIds( oEditor.FCK.EditorDocument.body ) ;
  74. bHasAnchors = ( aAnchors.length > 0 || aIds.length > 0 ) ;
  75. for ( var i = 0 ; i < aAnchors.length ; i++ )
  76. { var sName = aAnchors[i].name ; if ( sName && sName.length > 0 )
  77. oEditor.FCKTools.AddSelectOption( GetE('cmbAnchorName'), sName, sName ) ; }
  78. for ( var i = 0 ; i < aIds.length ; i++ )
  79. { oEditor.FCKTools.AddSelectOption( GetE('cmbAnchorId'), aIds[i], aIds[i] ) ; }
  80. ShowE( 'divSelAnchor' , bHasAnchors ) ; ShowE( 'divNoAnchor' , !bHasAnchors ) ; }
  81. function LoadSelection()
  82. { if ( !oLink ) return ;
  83. var sType = 'url' ;
  84. var sHRef = oLink.getAttribute( '_fcksavedurl' ) ; if ( !sHRef || sHRef.length == 0 )
  85. sHRef = oLink.getAttribute( 'href' , 2 ) + '' ;
  86. var oPopupMatch = oRegex.PopupUri.exec( sHRef ) ; if( oPopupMatch )
  87. { GetE('cmbTarget').value = 'popup' ; sHRef = oPopupMatch[1] ; FillPopupFields( oPopupMatch[2], oPopupMatch[3] ) ; SetTarget( 'popup' ) ; }
  88. var sProtocol = oRegex.UriProtocol.exec( sHRef ) ;
  89. if ( sProtocol )
  90. { sProtocol = sProtocol[0].toLowerCase() ; GetE('cmbLinkProtocol').value = sProtocol ;
  91. var sUrl = sHRef.replace( oRegex.UriProtocol, '' ) ;
  92. if ( sProtocol == 'mailto:' )
  93. { sType = 'email' ;
  94. var oEMailInfo = oParser.ParseEMailUrl( sUrl ) ; GetE('txtEMailAddress').value = oEMailInfo.Address ; GetE('txtEMailSubject').value = oEMailInfo.Subject ; GetE('txtEMailBody').value = oEMailInfo.Body ; }
  95. else
  96. { sType = 'url' ; GetE('txtUrl').value = sUrl ; }
  97. }
  98. else if ( sHRef.substr(0,1) == '#' && sHRef.length > 1 )
  99. { sType = 'anchor' ; GetE('cmbAnchorName').value = GetE('cmbAnchorId').value = sHRef.substr(1) ; }
  100. else
  101. { sType = 'url' ;
  102. GetE('cmbLinkProtocol').value = '' ; GetE('txtUrl').value = sHRef ; }
  103. if ( !oPopupMatch )
  104. { var sTarget = oLink.target ;
  105. if ( sTarget && sTarget.length > 0 )
  106. { if ( oRegex.ReserveTarget.test( sTarget ) )
  107. { sTarget = sTarget.toLowerCase() ; GetE('cmbTarget').value = sTarget ; }
  108. else
  109. GetE('cmbTarget').value = 'frame' ; GetE('txtTargetFrame').value = sTarget ; }
  110. }
  111. GetE('txtAttId').value = oLink.id ; GetE('txtAttName').value = oLink.name ; GetE('cmbAttLangDir').value = oLink.dir ; GetE('txtAttLangCode').value = oLink.lang ; GetE('txtAttAccessKey').value = oLink.accessKey ; GetE('txtAttTabIndex').value = oLink.tabIndex <= 0 ? '' : oLink.tabIndex ; GetE('txtAttTitle').value = oLink.title ; GetE('txtAttContentType').value = oLink.type ; GetE('txtAttCharSet').value = oLink.charset ;
  112. if ( oEditor.FCKBrowserInfo.IsIE )
  113. { GetE('txtAttClasses').value = oLink.getAttribute('className',2) || '' ; GetE('txtAttStyle').value = oLink.style.cssText ; }
  114. else
  115. { GetE('txtAttClasses').value = oLink.getAttribute('class',2) || '' ; GetE('txtAttStyle').value = oLink.getAttribute('style',2) ; }
  116. GetE('cmbLinkType').value = sType ; }
  117. function SetLinkType( linkType )
  118. { ShowE('divLinkTypeUrl' , (linkType == 'url') ) ; ShowE('divLinkTypeAnchor' , (linkType == 'anchor') ) ; ShowE('divLinkTypeEMail' , (linkType == 'email') ) ;
  119. if ( !FCKConfig.LinkDlgHideTarget )
  120. window.parent.SetTabVisibility( 'Target' , (linkType == 'url') ) ;
  121. if ( FCKConfig.LinkUpload )
  122. window.parent.SetTabVisibility( 'Upload' , (linkType == 'url') ) ;
  123. if ( !FCKConfig.LinkDlgHideAdvanced )
  124. window.parent.SetTabVisibility( 'Advanced' , (linkType != 'anchor' || bHasAnchors) ) ;
  125. if ( linkType == 'email' )
  126. window.parent.SetAutoSize( true ) ; }
  127. function SetTarget( targetType )
  128. { GetE('tdTargetFrame').style.display = ( targetType == 'popup' ? 'none' : '' ) ; GetE('tdPopupName').style.display =
  129. GetE('tablePopupFeatures').style.display = ( targetType == 'popup' ? '' : 'none' ) ;
  130. switch ( targetType )
  131. { case "_blank" :
  132. case "_self" :
  133. case "_parent" :
  134. case "_top" :
  135. GetE('txtTargetFrame').value = targetType ; break ; case "" :
  136. GetE('txtTargetFrame').value = '' ; break ; }
  137. if ( targetType == 'popup' )
  138. window.parent.SetAutoSize( true ) ; }
  139. function OnUrlChange()
  140. { var sUrl = GetE('txtUrl').value ; var sProtocol = oRegex.UrlOnChangeProtocol.exec( sUrl ) ;
  141. if ( sProtocol )
  142. { sUrl = sUrl.substr( sProtocol[0].length ) ; GetE('txtUrl').value = sUrl ; GetE('cmbLinkProtocol').value = sProtocol[0].toLowerCase() ; }
  143. else if ( oRegex.UrlOnChangeTestOther.test( sUrl ) )
  144. { GetE('cmbLinkProtocol').value = '' ; }
  145. }
  146. function OnTargetNameChange()
  147. { var sFrame = GetE('txtTargetFrame').value ;
  148. if ( sFrame.length == 0 )
  149. GetE('cmbTarget').value = '' ; else if ( oRegex.ReserveTarget.test( sFrame ) )
  150. GetE('cmbTarget').value = sFrame.toLowerCase() ; else
  151. GetE('cmbTarget').value = 'frame' ; }
  152. function BuildPopupUri( uri )
  153. { var oReg = new RegExp( "'", "g" ) ; var sWindowName = "'" + GetE('txtPopupName').value.replace(oReg, "\\'") + "'" ;
  154. var sFeatures = '' ; var aChkFeatures = document.getElementsByName('chkFeature') ; for ( var i = 0 ; i < aChkFeatures.length ; i++ )
  155. { if ( i > 0 ) sFeatures += ',' ; sFeatures += aChkFeatures[i].value + '=' + ( aChkFeatures[i].checked ? 'yes' : 'no' ) ; }
  156. if ( GetE('txtPopupWidth').value.length > 0 ) sFeatures += ',width=' + GetE('txtPopupWidth').value ; if ( GetE('txtPopupHeight').value.length > 0 ) sFeatures += ',height=' + GetE('txtPopupHeight').value ; if ( GetE('txtPopupLeft').value.length > 0 ) sFeatures += ',left=' + GetE('txtPopupLeft').value ; if ( GetE('txtPopupTop').value.length > 0 ) sFeatures += ',top=' + GetE('txtPopupTop').value ;
  157. return ( "javascript:void(window.open('" + uri + "'," + sWindowName + ",'" + sFeatures + "'))" ) ; }
  158. function FillPopupFields( windowName, features )
  159. { if ( windowName )
  160. GetE('txtPopupName').value = windowName ;
  161. var oFeatures = new Object() ; var oFeaturesMatch ; while( ( oFeaturesMatch = oRegex.PopupFeatures.exec( features ) ) != null )
  162. { var sValue = oFeaturesMatch[2] ; if ( sValue == ( 'yes' || '1' ) )
  163. oFeatures[ oFeaturesMatch[1] ] = true ; else if ( ! isNaN( sValue ) && sValue != 0 )
  164. oFeatures[ oFeaturesMatch[1] ] = sValue ; }
  165. var aChkFeatures = document.getElementsByName('chkFeature') ; for ( var i = 0 ; i < aChkFeatures.length ; i++ )
  166. { if ( oFeatures[ aChkFeatures[i].value ] )
  167. aChkFeatures[i].checked = true ; }
  168. if ( oFeatures['width'] ) GetE('txtPopupWidth').value = oFeatures['width'] ; if ( oFeatures['height'] ) GetE('txtPopupHeight').value = oFeatures['height'] ; if ( oFeatures['left'] ) GetE('txtPopupLeft').value = oFeatures['left'] ; if ( oFeatures['top'] ) GetE('txtPopupTop').value = oFeatures['top'] ; }
  169. function Ok()
  170. { var sUri, sInnerHtml ;
  171. switch ( GetE('cmbLinkType').value )
  172. { case 'url' :
  173. sUri = GetE('txtUrl').value ;
  174. if ( sUri.length == 0 )
  175. { alert( FCKLang.DlnLnkMsgNoUrl ) ; return false ; }
  176. sUri = GetE('cmbLinkProtocol').value + sUri ;
  177. if( GetE('cmbTarget').value == 'popup' )
  178. sUri = BuildPopupUri( sUri ) ;
  179. break ;
  180. case 'email' :
  181. sUri = GetE('txtEMailAddress').value ;
  182. if ( sUri.length == 0 )
  183. { alert( FCKLang.DlnLnkMsgNoEMail ) ; return false ; }
  184. sUri = oParser.CreateEMailUri(
  185. sUri,
  186. GetE('txtEMailSubject').value,
  187. GetE('txtEMailBody').value ) ; break ;
  188. case 'anchor' :
  189. var sAnchor = GetE('cmbAnchorName').value ; if ( sAnchor.length == 0 ) sAnchor = GetE('cmbAnchorId').value ;
  190. if ( sAnchor.length == 0 )
  191. { alert( FCKLang.DlnLnkMsgNoAnchor ) ; return false ; }
  192. sUri = '#' + sAnchor ; break ; }
  193. if ( !oLink )
  194. oLink = oEditor.FCK.CreateLink( sUri ) ;
  195. if ( oLink )
  196. sInnerHtml = oLink.innerHTML ; else
  197. {
  198. sInnerHtml = sUri;
  199. switch (GetE('cmbLinkType').value)
  200. { case 'anchor':
  201. sInnerHtml = sInnerHtml.replace( /^#/, '' ) ; break;
  202. case 'url':
  203. var oLinkPathRegEx = new RegExp("//?([^?\"']+)([?].*)?$"); var asLinkPath = oLinkPathRegEx.exec( sUri ); if (asLinkPath != null)
  204. sInnerHtml = asLinkPath[1]; break;
  205. case 'email':
  206. sInnerHtml = GetE('txtEMailAddress').value
  207. break; }
  208. oLink = oEditor.FCK.CreateElement( 'a' ) ; }
  209. oEditor.FCKUndo.SaveUndoStep() ;
  210. oLink.href = sUri ; SetAttribute( oLink, '_fcksavedurl', sUri ) ;
  211. oLink.innerHTML = sInnerHtml ;
  212. if( GetE('cmbTarget').value != 'popup' )
  213. SetAttribute( oLink, 'target', GetE('txtTargetFrame').value ) ; else
  214. SetAttribute( oLink, 'target', null ) ;
  215. SetAttribute( oLink, 'id' , GetE('txtAttId').value ) ; SetAttribute( oLink, 'name' , GetE('txtAttName').value ) ; // No IE. Set but doesnt't update the outerHTML.
  216. SetAttribute( oLink, 'dir' , GetE('cmbAttLangDir').value ) ; SetAttribute( oLink, 'lang' , GetE('txtAttLangCode').value ) ; SetAttribute( oLink, 'accesskey', GetE('txtAttAccessKey').value ) ; SetAttribute( oLink, 'tabindex' , ( GetE('txtAttTabIndex').value > 0 ? GetE('txtAttTabIndex').value : null ) ) ; SetAttribute( oLink, 'title' , GetE('txtAttTitle').value ) ; SetAttribute( oLink, 'type' , GetE('txtAttContentType').value ) ; SetAttribute( oLink, 'charset' , GetE('txtAttCharSet').value ) ;
  217. if ( oEditor.FCKBrowserInfo.IsIE )
  218. { SetAttribute( oLink, 'className', GetE('txtAttClasses').value ) ; oLink.style.cssText = GetE('txtAttStyle').value ; }
  219. else
  220. { SetAttribute( oLink, 'class', GetE('txtAttClasses').value ) ; SetAttribute( oLink, 'style', GetE('txtAttStyle').value ) ; }
  221. oEditor.FCKSelection.SelectNode(oLink);
  222. return true ; }
  223. function BrowseServer()
  224. { OpenFileBrowser( FCKConfig.LinkBrowserURL, FCKConfig.LinkBrowserWindowWidth, FCKConfig.LinkBrowserWindowHeight ) ; }
  225. function SetUrl( url )
  226. { document.getElementById('txtUrl').value = url ; OnUrlChange() ; window.parent.SetSelectedTab( 'Info' ) ; }
  227. function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg )
  228. { switch ( errorNumber )
  229. { case 0 :
  230. alert( 'Your file has been successfully uploaded' ) ; break ; case 1 :
  231. alert( customMsg ) ; return ; case 101 :
  232. alert( customMsg ) ; break ; case 201 :
  233. alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ; break ; case 202 :
  234. alert( 'Invalid file type' ) ; return ; case 203 :
  235. alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ; return ; default :
  236. alert( 'Error on file upload. Error number: ' + errorNumber ) ; return ; }
  237. SetUrl( fileUrl ) ; GetE('frmUpload').reset() ; }
  238. var oUploadAllowedExtRegex = new RegExp( FCKConfig.LinkUploadAllowedExtensions, 'i' ) ; var oUploadDeniedExtRegex = new RegExp( FCKConfig.LinkUploadDeniedExtensions, 'i' ) ;
  239. function CheckUpload()
  240. { var sFile = GetE('txtUploadFile').value ;
  241. if ( sFile.length == 0 )
  242. { alert( 'Please select a file to upload' ) ; return false ; }
  243. if ( ( FCKConfig.LinkUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) ||
  244. ( FCKConfig.LinkUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) )
  245. { OnUploadCompleted( 202 ) ; return false ; }
  246. return true ; }