| 1 | /* |
|---|
| 2 | * File Authors: |
|---|
| 3 | * Alfonso Martínez de Lizarrondo |
|---|
| 4 | * |
|---|
| 5 | * Based on fck_link.js |
|---|
| 6 | * |
|---|
| 7 | */ |
|---|
| 8 | |
|---|
| 9 | function Import(aSrc) { |
|---|
| 10 | document.write('<scr'+'ipt type="text/javascript" src="' + aSrc + '"></sc' + 'ript>'); |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | var oEditor = window.parent.InnerDialogLoaded() ; |
|---|
| 14 | var FCK = oEditor.FCK ; |
|---|
| 15 | var FCKLang = oEditor.FCKLang ; |
|---|
| 16 | var FCKConfig = oEditor.FCKConfig ; |
|---|
| 17 | |
|---|
| 18 | Import(FCKConfig.FullBasePath + 'dialog/common/fck_dialog_common.js'); |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | //#### Initialization Code |
|---|
| 22 | |
|---|
| 23 | // oLink: The actual selected link in the editor. |
|---|
| 24 | var oLink = FCK.Selection.MoveToAncestorNode( 'A' ) ; |
|---|
| 25 | if ( oLink ) |
|---|
| 26 | FCK.Selection.SelectNode( oLink ) ; |
|---|
| 27 | |
|---|
| 28 | window.onload = function() |
|---|
| 29 | { |
|---|
| 30 | // Translate the dialog box texts. |
|---|
| 31 | oEditor.FCKLanguageManager.TranslatePage(document) ; |
|---|
| 32 | |
|---|
| 33 | // Load the selected link information (if any). |
|---|
| 34 | LoadSelection() ; |
|---|
| 35 | |
|---|
| 36 | |
|---|
| 37 | GetE('frmUpload').action = FCKConfig.LinkUploadURL ; |
|---|
| 38 | |
|---|
| 39 | // Activate the "OK" button. |
|---|
| 40 | window.parent.SetOkButton( true ) ; |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | function LoadSelection() |
|---|
| 45 | { |
|---|
| 46 | if ( !oLink ) { |
|---|
| 47 | //set the focus on the file selector |
|---|
| 48 | GetE("txtUploadFile").focus(); |
|---|
| 49 | return ; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | // Get the actual Link href. |
|---|
| 53 | var sHRef = oLink.getAttribute( '_fcksavedurl' ) ; |
|---|
| 54 | if ( !sHRef || sHRef.length == 0 ) |
|---|
| 55 | sHRef = oLink.getAttribute( 'href' , 2 ) + '' ; |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | GetE('txtUrl').value = sHRef ; |
|---|
| 59 | |
|---|
| 60 | GetE('txtAttTitle').value = oLink.title ; |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | //#### The OK button was hit. |
|---|
| 65 | function Ok() |
|---|
| 66 | { |
|---|
| 67 | var sUri, sInnerHtml ; |
|---|
| 68 | |
|---|
| 69 | //check if the user has selected a file to upload (we're overiding the default behaviour of the link dialog) |
|---|
| 70 | var sFile = GetE('txtUploadFile').value ; |
|---|
| 71 | if (sFile.length > 0) |
|---|
| 72 | { |
|---|
| 73 | //upload the file |
|---|
| 74 | if ( CheckUpload() ) |
|---|
| 75 | GetE('frmUpload').submit(); |
|---|
| 76 | |
|---|
| 77 | return false ; // We'll finish once the file is at the server |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | sUri = GetE('txtUrl').value ; |
|---|
| 81 | if ( sUri.length == 0 ) |
|---|
| 82 | { |
|---|
| 83 | // If the url is empty check to see if they have already selected a file |
|---|
| 84 | CheckUpload(); |
|---|
| 85 | return false ; |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | // Workaround to make IE happy |
|---|
| 89 | window.focus(); |
|---|
| 90 | oEditor.FCKUndo.SaveUndoStep() ; |
|---|
| 91 | |
|---|
| 92 | // If no link is selected, create a new one (it may result in more than one link creation - #220). |
|---|
| 93 | var aLinks = oLink ? [ oLink ] : oEditor.FCK.CreateLink( sUri, true ) ; |
|---|
| 94 | |
|---|
| 95 | // If no selection, no links are created, so use the uri as the link text (by dom, 2006-05-26) |
|---|
| 96 | var aHasSelection = ( aLinks.length > 0 ) ; |
|---|
| 97 | if ( !aHasSelection ) |
|---|
| 98 | { |
|---|
| 99 | sInnerHtml = sUri; |
|---|
| 100 | |
|---|
| 101 | // Try to built better text for empty link |
|---|
| 102 | // Adjusted to get only the file name and exclude the path: |
|---|
| 103 | var oLinkPathRegEx = new RegExp("(^.*\\/|^)([^\\/]*?)(\\?.*$|$)"); |
|---|
| 104 | var asLinkPath = oLinkPathRegEx.exec( sUri ); |
|---|
| 105 | if (asLinkPath != null) |
|---|
| 106 | sInnerHtml = asLinkPath[2]; // use matched path |
|---|
| 107 | |
|---|
| 108 | // Create a new (empty) anchor. |
|---|
| 109 | aLinks = [ oEditor.FCK.InsertElement( 'a' ) ] ; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // sUri = encodeURI( sUri ).replace( '#', '%23' ); |
|---|
| 113 | for ( var i = 0 ; i < aLinks.length ; i++ ) |
|---|
| 114 | { |
|---|
| 115 | oLink = aLinks[i] ; |
|---|
| 116 | |
|---|
| 117 | if ( aHasSelection ) |
|---|
| 118 | sInnerHtml = oLink.innerHTML ; // Save the innerHTML (IE changes it if it is like an URL). |
|---|
| 119 | |
|---|
| 120 | oLink.href = sUri ; |
|---|
| 121 | SetAttribute( oLink, '_fcksavedurl', sUri ) ; |
|---|
| 122 | |
|---|
| 123 | // Target |
|---|
| 124 | SetAttribute( oLink, 'target', '_blank' ) ; |
|---|
| 125 | |
|---|
| 126 | // Advances Attributes |
|---|
| 127 | SetAttribute( oLink, 'title', GetE('txtAttTitle').value ) ; |
|---|
| 128 | |
|---|
| 129 | oLink.innerHTML = sInnerHtml ; // Set (or restore) the innerHTML |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | // Select the (first) link. |
|---|
| 133 | oEditor.FCKSelection.SelectNode( aLinks[0] ); |
|---|
| 134 | |
|---|
| 135 | return true ; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | function SetUrl( url ) |
|---|
| 140 | { |
|---|
| 141 | document.getElementById('txtUrl').value = url ; |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | function OnUploadCompleted( errorNumber, fileUrl, fileName, customMsg ) |
|---|
| 145 | { |
|---|
| 146 | |
|---|
| 147 | switch ( errorNumber ) |
|---|
| 148 | { |
|---|
| 149 | case 0 : // No errors |
|---|
| 150 | // alert( 'Your file has been successfully uploaded' ) ; |
|---|
| 151 | break ; |
|---|
| 152 | case 1 : // Custom error |
|---|
| 153 | alert( customMsg ) ; |
|---|
| 154 | return ; |
|---|
| 155 | case 101 : // Custom warning |
|---|
| 156 | alert( customMsg ) ; |
|---|
| 157 | break ; |
|---|
| 158 | case 201 : |
|---|
| 159 | // alert( 'A file with the same name is already available. The uploaded file has been renamed to "' + fileName + '"' ) ; |
|---|
| 160 | break ; |
|---|
| 161 | case 202 : |
|---|
| 162 | alert( 'Invalid file type' ) ; |
|---|
| 163 | return ; |
|---|
| 164 | case 203 : |
|---|
| 165 | alert( "Security error. You probably don't have enough permissions to upload. Please check your server." ) ; |
|---|
| 166 | return ; |
|---|
| 167 | default : |
|---|
| 168 | alert( 'Error on file upload. Error number: ' + errorNumber ) ; |
|---|
| 169 | return ; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | SetUrl( fileUrl ) ; |
|---|
| 173 | GetE('frmUpload').reset() ; |
|---|
| 174 | |
|---|
| 175 | // Press the Ok button if we had only a warning or it was succesful |
|---|
| 176 | if (errorNumber == 0 || errorNumber == 101 || errorNumber == 201 ) |
|---|
| 177 | window.parent.Ok(); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | var oUploadAllowedExtRegex = new RegExp( FCKConfig.LinkUploadAllowedExtensions, 'i' ) ; |
|---|
| 181 | var oUploadDeniedExtRegex = new RegExp( FCKConfig.LinkUploadDeniedExtensions, 'i' ) ; |
|---|
| 182 | |
|---|
| 183 | function CheckUpload() |
|---|
| 184 | { |
|---|
| 185 | var sFile = GetE('txtUploadFile').value ; |
|---|
| 186 | |
|---|
| 187 | if ( sFile.length == 0 ) |
|---|
| 188 | { |
|---|
| 189 | alert( 'Please select a file to upload' ) ; |
|---|
| 190 | return false ; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | if ( ( FCKConfig.LinkUploadAllowedExtensions.length > 0 && !oUploadAllowedExtRegex.test( sFile ) ) || |
|---|
| 194 | ( FCKConfig.LinkUploadDeniedExtensions.length > 0 && oUploadDeniedExtRegex.test( sFile ) ) ) |
|---|
| 195 | { |
|---|
| 196 | OnUploadCompleted( 202 ) ; |
|---|
| 197 | return false ; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | return true ; |
|---|
| 201 | } |
|---|