| 1 | /* |
| 2 | Copyright (c) 2003-2009, CKSource - Frederico Knabben. All rights reserved. |
| 3 | For licensing, see LICENSE.html or http://ckeditor.com/license |
| 4 | */ |
| 5 | |
| 6 | /** |
| 7 | * @fileOverview The "filebrowser" plugin, it adds support for file uploads and browsing. |
| 8 | * |
| 9 | * File Browser attaches itself automatically to elements with id: 'browse' and 'uploadButton'. |
| 10 | * When file is selected inside of file browser or uploaded, its url is inserted automatically to |
| 11 | * a field with id 'txtUrl'. |
| 12 | * |
| 13 | * Example 1: (Browse) |
| 14 | * { |
| 15 | * type : 'button', |
| 16 | * id : 'browse', |
| 17 | * label : editor.lang.common.browseServer |
| 18 | * } |
| 19 | * Because of the special id: 'browse' given to this button, file browser will be opened after pressing it. |
| 20 | * |
| 21 | * Example 2: (Quick Upload) |
| 22 | * { |
| 23 | * type : 'fileButton', |
| 24 | * id : 'uploadButton', |
| 25 | * label : editor.lang.common.uploadSubmit, |
| 26 | * 'for' : [ 'upload', 'upload' ] |
| 27 | * } |
| 28 | * Because of the special id: 'uploadButton' given to this button, file browser will handle the file upload. |
| 29 | * |
| 30 | * |
| 31 | * It is possible to attach filebrowser to any element inside of a plugin, regardless of assigned id. |
| 32 | * Two actions are supported: 'Browse' and 'QuickUpload'. |
| 33 | * |
| 34 | * Example 3: (Quick Upload) |
| 35 | * { |
| 36 | * type : 'fileButton', |
| 37 | * label : editor.lang.common.uploadSubmit, |
| 38 | * id : 'buttonId', |
| 39 | * filebrowser : |
| 40 | * { |
| 41 | * action : 'QuickUpload', //required |
| 42 | * updateOnUpload : ['tab1', 'someTextFieldId'], //required |
| 43 | * params : { type : 'Files', currentFolder : '/folder/' }, //optional |
| 44 | * onUpload : function( error, fileUrl ) //optional |
| 45 | * { |
| 46 | * // Do not call the built-in onUpload command |
| 47 | * // return false; |
| 48 | * } |
| 49 | * }, |
| 50 | * 'for' : ['tab1', 'myFile'] |
| 51 | * } |
| 52 | * |
| 53 | * Suppose we have a file element with id 'myFile', text field with id 'someTextFieldId' and a fileButton. |
| 54 | * If filebowser.url is not specified explicitly, form action will be set to 'filebrowser_upload_url', |
| 55 | * additional parameters from 'params' object will be added to the query string. |
| 56 | * File uploader expects to receive error message/number from the server connector and fileUrl. |
| 57 | * It is possible to create your own uploadHandler and call the built-in onUpload command. |
| 58 | * |
| 59 | * Example 4: (Browse) |
| 60 | * { |
| 61 | * type : 'button', |
| 62 | * id : 'buttonId', |
| 63 | * label : editor.lang.common.browseServer, |
| 64 | * filebrowser : |
| 65 | * { |
| 66 | * action : 'Browse', |
| 67 | * url : '/ckfinder/ckfinder.html?action=js&func=SetUrl&thumbFunc=SetUrl&type=Images', |
| 68 | * updateOnSelect : ['tab1', 'textFieldId'] |
| 69 | * } |
| 70 | * } |
| 71 | * |
| 72 | * In this example, after pressing a button, file browser will be opened in a popup. |
| 73 | * If we don't specify filebrowser.url attribute, 'filebrowser_browse_url' setting will be used. |
| 74 | * After selecting a file in a file browser, an element with id 'textFieldId' will be updated. |
| 75 | */ |
| 76 | CKEDITOR.plugins.add( 'filebrowser', |
| 77 | { |
| 78 | /** |
| 79 | * Reference to the last opened dialog. Required by 'selectFunction' and 'uploadCompletedFunction'. |
| 80 | * @type {CKEDITOR.dialog} |
| 81 | */ |
| 82 | _dialog : null, |
| 83 | /** |
| 84 | * The "action" attribute that will be passed to the {@see CKEDITOR.ui.dialog.file#reset} function. |
| 85 | * The "action" attribute is generated automatically in some cases. |
| 86 | * @type String |
| 87 | */ |
| 88 | _formAction : null, |
| 89 | /** |
| 90 | * Holds the reference to the file input element to reset after file is uploaded. |
| 91 | * Syntax: [ pageId, elementId ] |
| 92 | * @type Array |
| 93 | */ |
| 94 | _targetInput : null, |
| 95 | /** |
| 96 | * Holds the reference to the UI element that will be updated when file is selected/uploaded. |
| 97 | * Syntax: [ pageId, elementId ] |
| 98 | * @type Array |
| 99 | */ |
| 100 | _targetElement : null, |
| 101 | /** |
| 102 | * The user defined function (if defined) that will be called when file is uploaded. |
| 103 | * @type Function |
| 104 | */ |
| 105 | _onUpload : null, |
| 106 | |
| 107 | init : function( editor, pluginPath ) |
| 108 | { |
| 109 | /** |
| 110 | * Updates the target element with the url of uploaded/selected file. |
| 111 | * @param {String} url The url of a file. |
| 112 | */ |
| 113 | var updateTargetElement = function( url ) |
| 114 | { |
| 115 | url = url.replace( /#/g, '%23' ); |
| 116 | var plugin = editor.plugins.filebrowser; |
| 117 | |
| 118 | // If there is a reference to targetElement, update it. |
| 119 | if ( plugin._targetElement ) |
| 120 | { |
| 121 | var element = plugin._dialog.getContentElement( plugin._targetElement[0], plugin._targetElement[1] ); |
| 122 | if ( element ) |
| 123 | element.setValue( url ); |
| 124 | } |
| 125 | // If targetElement is empty, find the first element with id 'txtUrl' and update it. |
| 126 | else |
| 127 | { |
| 128 | var widgetFound = false; |
| 129 | plugin._dialog.foreach( function( widget ) |
| 130 | { |
| 131 | if ( !widgetFound && widget.id == 'txtUrl' ) |
| 132 | { |
| 133 | widget.setValue( url ); |
| 134 | widget.getDialog().selectPage( widget.getParentTab() ); |
| 135 | widgetFound = true; |
| 136 | } |
| 137 | }); |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | /** |
| 142 | * Opens Browser in a popup. The "width" and "height" parameters accept |
| 143 | * numbers (pixels) or percent (of screen size) values. |
| 144 | * @param {String} url The url of the external file browser. |
| 145 | * @param {String} width Popup window width. |
| 146 | * @param {String} height Popup window height. |
| 147 | */ |
| 148 | var popup = function( url, width, height ) |
| 149 | { |
| 150 | width = width || '80%'; |
| 151 | height = height || '70%'; |
| 152 | |
| 153 | if ( typeof width == 'string' && width.length > 1 && width.substr( width.length - 1, 1 ) == '%' ) |
| 154 | width = parseInt( window.screen.width * parseInt( width, 10 ) / 100, 10 ); |
| 155 | |
| 156 | if ( typeof height == 'string' && height.length > 1 && height.substr( height.length - 1, 1 ) == '%' ) |
| 157 | height = parseInt( window.screen.height * parseInt( height, 10 ) / 100, 10 ); |
| 158 | |
| 159 | if ( width < 640 ) |
| 160 | width = 640; |
| 161 | |
| 162 | if ( height < 420 ) |
| 163 | height = 420; |
| 164 | |
| 165 | var top = parseInt( ( window.screen.height - height ) / 2, 10 ), |
| 166 | left = parseInt( ( window.screen.width - width ) / 2, 10 ), |
| 167 | options = 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes' + |
| 168 | ',width=' + width + |
| 169 | ',height=' + height + |
| 170 | ',top=' + top + |
| 171 | ',left=' + left; |
| 172 | |
| 173 | var popupWindow = window.open( '', null, options, true ); |
| 174 | |
| 175 | // Blocked by a popup blocker. |
| 176 | if ( !popupWindow ) |
| 177 | return false; |
| 178 | |
| 179 | try |
| 180 | { |
| 181 | popupWindow.moveTo( left, top ); |
| 182 | popupWindow.resizeTo( width, height ); |
| 183 | popupWindow.focus(); |
| 184 | popupWindow.location.href = url; |
| 185 | } |
| 186 | catch (e) |
| 187 | { |
| 188 | popupWindow = window.open( url, null, options, true ); |
| 189 | } |
| 190 | |
| 191 | return true ; |
| 192 | }; |
| 193 | |
| 194 | /** |
| 195 | * The onlick function assigned to the 'Browse Server' button. |
| 196 | * Opens the file browser and updates target field when file is selected. |
| 197 | * @param {CKEDITOR.event} evt The event object. |
| 198 | */ |
| 199 | var browseServer = function( evt ) |
| 200 | { |
| 201 | var dialog = this.getDialog(); |
| 202 | var editor = dialog.getParentEditor(); |
| 203 | var plugin = editor.plugins.filebrowser; |
| 204 | var params = this.filebrowser.params || {}; |
| 205 | var url; |
| 206 | |
| 207 | // Save references to fields required by the 'selectFunction'. |
| 208 | plugin._dialog = dialog; |
| 209 | |
| 210 | if ( this.filebrowser.updateOnSelect ) |
| 211 | { |
| 212 | plugin._targetElement = this.filebrowser.updateOnSelect; |
| 213 | } |
| 214 | else |
| 215 | { |
| 216 | var parentTab = this.getParentTab(); |
| 217 | if ( dialog.getContentElement( parentTab, 'txtUrl' ) ) |
| 218 | plugin._targetElement = [ parentTab, 'txtUrl' ]; |
| 219 | else |
| 220 | plugin._targetElement = null; |
| 221 | } |
| 222 | |
| 223 | // Function that is called when the user selects a file in external file browser. |
| 224 | var selectFunction = this.filebrowser.selectFunction || 'SetUrl'; |
| 225 | |
| 226 | this.getElement().getWindow().$[selectFunction] = function( fileUrl, data ) |
| 227 | { |
| 228 | updateTargetElement( fileUrl ); |
| 229 | }; |
| 230 | |
| 231 | var width = editor.config['filebrowser_' + dialog.getName() + '_window_width'] || editor.config.filebrowser_window_width; |
| 232 | var height = editor.config['filebrowser_' + dialog.getName() + '_window_height'] || editor.config.filebrowser_window_height; |
| 233 | |
| 234 | if ( this.filebrowser.url ) |
| 235 | url = this.filebrowser.url; |
| 236 | else |
| 237 | url = editor.config['filebrowser_' + dialog.getName() + '_browse_url'] || editor.config.filebrowser_browse_url; |
| 238 | |
| 239 | if ( !url ) |
| 240 | { |
| 241 | alert( editor.lang.filebrowser.browserNotConfigured ); |
| 242 | return; |
| 243 | } |
| 244 | |
| 245 | if ( !params.langCode ) |
| 246 | params.langCode = editor.langCode; |
| 247 | |
| 248 | url = addQueryString( url, params ); |
| 249 | |
| 250 | popup( url, width, height ); |
| 251 | }; |
| 252 | |
| 253 | /** |
| 254 | * The onlick function assigned to the 'Upload' button. |
| 255 | * Makes the final decision whether form is really submitted and updates target field when file is uploaded. |
| 256 | * @param {CKEDITOR.event} evt The event object. |
| 257 | */ |
| 258 | var uploadFile = function( evt ) |
| 259 | { |
| 260 | var dialog = this.getDialog(); |
| 261 | var editor = dialog.getParentEditor(); |
| 262 | var plugin = editor.plugins.filebrowser; |
| 263 | |
| 264 | // Update variables that will be used by the 'OnUploadCompleted' function. |
| 265 | plugin._onUpload = this.filebrowser.onUpload; |
| 266 | plugin._dialog = dialog; |
| 267 | plugin._targetInput = this['for']; |
| 268 | plugin._formAction = dialog.getContentElement( this['for'][0], this['for'][1] ).getAction(); |
| 269 | |
| 270 | // If user didn't select the file, stop the upload. |
| 271 | if ( !dialog.getContentElement( this['for'][0], this['for'][1] ).getInputElement().$.value ) |
| 272 | { |
| 273 | alert( editor.lang.filebrowser.fileNotSelected ); |
| 274 | return; |
| 275 | } |
| 276 | |
| 277 | if ( !plugin._formAction ) |
| 278 | { |
| 279 | alert( editor.lang.filebrowser.uploaderNotConfigured ); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | // targetElement will be updated with the url of uploaded file. |
| 284 | if ( this.filebrowser.updateOnUpload ) |
| 285 | plugin._targetElement = this.filebrowser.updateOnUpload; |
| 286 | else |
| 287 | { |
| 288 | var parentTab = this.getParentTab(); |
| 289 | if ( dialog.getContentElement( parentTab, 'url' ) ) |
| 290 | plugin._targetElement = [ parentTab, 'url' ]; |
| 291 | else |
| 292 | plugin._targetElement = null; |
| 293 | } |
| 294 | |
| 295 | // Function to be called by the server connector after file is uploaded. |
| 296 | var uploadCompletedFunction = this.filebrowser.uploadCompletedFunction || 'OnUploadCompleted'; |
| 297 | |
| 298 | this.getElement().getWindow().$[uploadCompletedFunction] = function( errorMessage, fileUrl ) |
| 299 | { |
| 300 | var errorNumberRegex = /^[0-9]+$/; |
| 301 | |
| 302 | plugin._dialog.getContentElement( plugin._targetInput[0], plugin._targetInput[1] ).reset( plugin._formAction ); |
| 303 | |
| 304 | if ( plugin._onUpload && plugin._onUpload( errorMessage, fileUrl ) === false ) |
| 305 | return; |
| 306 | |
| 307 | if ( errorMessage ) |
| 308 | { |
| 309 | if ( errorNumberRegex.test( errorMessage ) ) |
| 310 | { |
| 311 | if ( editor.lang.filebrowser.errors[errorMessage] ) |
| 312 | { |
| 313 | var fileName = fileUrl.replace( /^.*[\/\\]/g, '' ); |
| 314 | alert( editor.lang.filebrowser.errors[errorMessage].replace( '%filename', fileName ) ); |
| 315 | } |
| 316 | else |
| 317 | alert( editor.lang.filebrowser.errorUnknown.replace( /%1/, errorMessage ) ); |
| 318 | } |
| 319 | else |
| 320 | alert( errorMessage ); |
| 321 | } |
| 322 | |
| 323 | if ( fileUrl ) |
| 324 | updateTargetElement( fileUrl ); |
| 325 | }; |
| 326 | }; |
| 327 | |
| 328 | /** |
| 329 | * Adds (additional) arguments to given url. |
| 330 | * @param {String} url The url. |
| 331 | * @param {Object} params Additional parameters. |
| 332 | */ |
| 333 | var addQueryString = function( url, params ) |
| 334 | { |
| 335 | var queryString = []; |
| 336 | |
| 337 | if ( !params ) |
| 338 | return url; |
| 339 | else |
| 340 | { |
| 341 | for ( var i in params ) |
| 342 | queryString.push( i + "=" + encodeURIComponent( params[i] ) ); |
| 343 | } |
| 344 | |
| 345 | return url + ( ( url.indexOf( "?" ) != -1 ) ? "&" : "?" ) + queryString.join( "&" ); |
| 346 | }; |
| 347 | |
| 348 | /** |
| 349 | * Setups the file element. |
| 350 | * @param {CKEDITOR.ui.dialog.file} fileInput The file element used during file upload. |
| 351 | * @param {Object} filebrowser Object containing filebrowser settings assigned to the fileButton |
| 352 | * associated with this file element. |
| 353 | */ |
| 354 | var setupFileElement = function( dialogName, fileInput, filebrowser ) |
| 355 | { |
| 356 | var params = filebrowser.params || {}; |
| 357 | var url; |
| 358 | |
| 359 | if ( !params.langCode ) |
| 360 | params.langCode = editor.langCode; |
| 361 | |
| 362 | if ( filebrowser.url ) |
| 363 | url = filebrowser.url; |
| 364 | else |
| 365 | url = editor.config['filebrowser_' + dialogName + '_upload_url'] || editor.config.filebrowser_upload_url; |
| 366 | |
| 367 | if ( url ) |
| 368 | fileInput.action = addQueryString( url, params ); |
| 369 | else |
| 370 | fileInput.action = ''; |
| 371 | |
| 372 | fileInput.filebrowser = filebrowser; |
| 373 | }; |
| 374 | |
| 375 | /** |
| 376 | * Traverse through the content definition and attach filebrowser |
| 377 | * to elements with 'filebrowser' attribute. |
| 378 | * @param {CKEDITOR.dialog.dialogDefinitionObject} definition |
| 379 | * @param {Array} elements Array of {@link CKEDITOR.dialog.contentDefinition} objects. |
| 380 | */ |
| 381 | var attachFileBrowser = function( dialogName, definition, elements ) |
| 382 | { |
| 383 | var element, fileInput; |
| 384 | |
| 385 | for ( var i in elements ) |
| 386 | { |
| 387 | element = elements[i]; |
| 388 | |
| 389 | if ( element.type == 'hbox' || element.type == 'vbox' ) |
| 390 | attachFileBrowser( dialogName, definition, element.children ); |
| 391 | |
| 392 | if ( !element.filebrowser ) |
| 393 | continue; |
| 394 | |
| 395 | if ( element.filebrowser.action == 'Browse' ) |
| 396 | { |
| 397 | element.onClick = browseServer; |
| 398 | } |
| 399 | else if ( element.filebrowser.action == 'QuickUpload' && element['for'] ) |
| 400 | { |
| 401 | setupFileElement( dialogName, definition.getContents( element['for'][0] ).get( element['for'][1] ), element.filebrowser ); |
| 402 | element.onClick = uploadFile; |
| 403 | } |
| 404 | } |
| 405 | }; |
| 406 | |
| 407 | CKEDITOR.on( 'dialogDefinition', function( evt ) |
| 408 | { |
| 409 | var i, browseButton, fileButton; |
| 410 | |
| 411 | // Attach 'filebrowser' attributes to elements with special id. |
| 412 | for ( i in evt.data.definition.contents ) |
| 413 | { |
| 414 | browseButton = evt.data.definition.contents[i].get( 'browse' ); |
| 415 | if ( browseButton ) |
| 416 | browseButton.filebrowser = { action : 'Browse', params : {} }; |
| 417 | |
| 418 | fileButton = evt.data.definition.contents[i].get( 'uploadButton' ); |
| 419 | if ( fileButton && evt.data.definition.getContents( fileButton['for'][0] ).get( fileButton['for'][1] ).action == 'filebrowser' ) |
| 420 | fileButton.filebrowser = { action : 'QuickUpload', params : {} }; |
| 421 | } |
| 422 | |
| 423 | // Associate filebrowser to elements with 'filebrowser' attribute. |
| 424 | for ( i in evt.data.definition.contents ) |
| 425 | attachFileBrowser( evt.data.name, evt.data.definition, evt.data.definition.contents[i].elements ); |
| 426 | }); |
| 427 | } |
| 428 | } ); |
| 429 | |
| 430 | CKEDITOR.tools.extend( CKEDITOR.config, { |
| 431 | /** |
| 432 | * File Browser window width. |
| 433 | * @type String |
| 434 | */ |
| 435 | filebrowser_window_width : '80%', |
| 436 | /** |
| 437 | * File Browser window height. |
| 438 | * @type String |
| 439 | */ |
| 440 | filebrowser_window_height : '70%', |
| 441 | /** |
| 442 | * Default File Browser url. |
| 443 | * @type String |
| 444 | */ |
| 445 | filebrowser_browse_url : '', |
| 446 | /** |
| 447 | * File Browser url (Image dialog). |
| 448 | * @type String |
| 449 | */ |
| 450 | filebrowser_image_browse_url : '', |
| 451 | /** |
| 452 | * File Browser url (Flash dialog). |
| 453 | * @type String |
| 454 | */ |
| 455 | filebrowser_flash_browse_url : '', |
| 456 | /** |
| 457 | * Default upload action. |
| 458 | * @type String |
| 459 | */ |
| 460 | filebrowser_upload_url : '', |
| 461 | /** |
| 462 | * Upload action (Image dialog). |
| 463 | * @type String |
| 464 | */ |
| 465 | filebrowser_image_upload_url : '', |
| 466 | /** |
| 467 | * Upload action (Flash dialog). |
| 468 | * @type String |
| 469 | */ |
| 470 | filebrowser_flash_upload_url : '' |
| 471 | } ); |