2 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
\r
3 For licensing, see LICENSE.html or http://ckeditor.com/license
\r
7 * @fileOverview The "filebrowser" plugin, it adds support for file uploads and
\r
10 * When file is selected inside of the file browser or uploaded, its url is
\r
11 * inserted automatically to a field, which is described in the 'filebrowser'
\r
12 * attribute. To specify field that should be updated, pass the tab id and
\r
13 * element id, separated with a colon.
\r
15 * Example 1: (Browse)
\r
21 * filebrowser : 'tabId:elementId',
\r
22 * label : editor.lang.common.browseServer
\r
26 * If you set the 'filebrowser' attribute on any element other than
\r
27 * 'fileButton', the 'Browse' action will be triggered.
\r
29 * Example 2: (Quick Upload)
\r
33 * type : 'fileButton',
\r
34 * id : 'uploadButton',
\r
35 * filebrowser : 'tabId:elementId',
\r
36 * label : editor.lang.common.uploadSubmit,
\r
37 * 'for' : [ 'upload', 'upload' ]
\r
41 * If you set the 'filebrowser' attribute on a fileButton element, the
\r
42 * 'QuickUpload' action will be executed.
\r
44 * Filebrowser plugin also supports more advanced configuration (through
\r
45 * javascript object).
\r
47 * The following settings are supported:
\r
50 * [action] - Browse or QuickUpload
\r
51 * [target] - field to update, tabId:elementId
\r
52 * [params] - additional arguments to be passed to the server connector (optional)
\r
53 * [onSelect] - function to execute when file is selected/uploaded (optional)
\r
54 * [url] - the URL to be called (optional)
\r
57 * Example 3: (Quick Upload)
\r
61 * type : 'fileButton',
\r
62 * label : editor.lang.common.uploadSubmit,
\r
66 * action : 'QuickUpload', //required
\r
67 * target : 'tab1:elementId', //required
\r
68 * params : //optional
\r
71 * currentFolder : '/folder/'
\r
73 * onSelect : function( fileUrl, errorMessage ) //optional
\r
75 * // Do not call the built-in selectFuntion
\r
79 * 'for' : [ 'tab1', 'myFile' ]
\r
83 * Suppose we have a file element with id 'myFile', text field with id
\r
84 * 'elementId' and a fileButton. If filebowser.url is not specified explicitly,
\r
85 * form action will be set to 'filebrowser[DialogName]UploadUrl' or, if not
\r
86 * specified, to 'filebrowserUploadUrl'. Additional parameters from 'params'
\r
87 * object will be added to the query string. It is possible to create your own
\r
88 * uploadHandler and cancel the built-in updateTargetElement command.
\r
90 * Example 4: (Browse)
\r
96 * label : editor.lang.common.browseServer,
\r
99 * action : 'Browse',
\r
100 * url : '/ckfinder/ckfinder.html&type=Images',
\r
101 * target : 'tab1:elementId'
\r
106 * In this example, after pressing a button, file browser will be opened in a
\r
107 * popup. If we don't specify filebrowser.url attribute,
\r
108 * 'filebrowser[DialogName]BrowseUrl' or 'filebrowserBrowseUrl' will be used.
\r
109 * After selecting a file in a file browser, an element with id 'elementId' will
\r
110 * be updated. Just like in the third example, a custom 'onSelect' function may be
\r
116 * Adds (additional) arguments to given url.
\r
121 * params Additional parameters.
\r
123 function addQueryString( url, params )
\r
125 var queryString = [];
\r
131 for ( var i in params )
\r
132 queryString.push( i + "=" + encodeURIComponent( params[ i ] ) );
\r
135 return url + ( ( url.indexOf( "?" ) != -1 ) ? "&" : "?" ) + queryString.join( "&" );
\r
139 * Make a string's first character uppercase.
\r
144 function ucFirst( str )
\r
147 var f = str.charAt( 0 ).toUpperCase();
\r
148 return f + str.substr( 1 );
\r
152 * The onlick function assigned to the 'Browse Server' button. Opens the
\r
153 * file browser and updates target field when file is selected.
\r
155 * @param {CKEDITOR.event}
\r
156 * evt The event object.
\r
158 function browseServer( evt )
\r
160 var dialog = this.getDialog();
\r
161 var editor = dialog.getParentEditor();
\r
163 editor._.filebrowserSe = this;
\r
165 var width = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowWidth' ]
\r
166 || editor.config.filebrowserWindowWidth || '80%';
\r
167 var height = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowHeight' ]
\r
168 || editor.config.filebrowserWindowHeight || '70%';
\r
170 var params = this.filebrowser.params || {};
\r
171 params.CKEditor = editor.name;
\r
172 params.CKEditorFuncNum = editor._.filebrowserFn;
\r
173 if ( !params.langCode )
\r
174 params.langCode = editor.langCode;
\r
176 var url = addQueryString( this.filebrowser.url, params );
\r
177 editor.popup( url, width, height, editor.config.fileBrowserWindowFeatures );
\r
181 * The onlick function assigned to the 'Upload' button. Makes the final
\r
182 * decision whether form is really submitted and updates target field when
\r
183 * file is uploaded.
\r
185 * @param {CKEDITOR.event}
\r
186 * evt The event object.
\r
188 function uploadFile( evt )
\r
190 var dialog = this.getDialog();
\r
191 var editor = dialog.getParentEditor();
\r
193 editor._.filebrowserSe = this;
\r
195 // If user didn't select the file, stop the upload.
\r
196 if ( !dialog.getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement().$.value )
\r
199 if ( !dialog.getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getAction() )
\r
206 * Setups the file element.
\r
208 * @param {CKEDITOR.ui.dialog.file}
\r
209 * fileInput The file element used during file upload.
\r
211 * filebrowser Object containing filebrowser settings assigned to
\r
212 * the fileButton associated with this file element.
\r
214 function setupFileElement( editor, fileInput, filebrowser )
\r
216 var params = filebrowser.params || {};
\r
217 params.CKEditor = editor.name;
\r
218 params.CKEditorFuncNum = editor._.filebrowserFn;
\r
219 if ( !params.langCode )
\r
220 params.langCode = editor.langCode;
\r
222 fileInput.action = addQueryString( filebrowser.url, params );
\r
223 fileInput.filebrowser = filebrowser;
\r
227 * Traverse through the content definition and attach filebrowser to
\r
228 * elements with 'filebrowser' attribute.
\r
231 * dialogName Dialog name.
\r
232 * @param {CKEDITOR.dialog.definitionObject}
\r
233 * definition Dialog definition.
\r
235 * elements Array of {@link CKEDITOR.dialog.definition.content}
\r
238 function attachFileBrowser( editor, dialogName, definition, elements )
\r
240 var element, fileInput;
\r
242 for ( var i in elements )
\r
244 element = elements[ i ];
\r
246 if ( element.type == 'hbox' || element.type == 'vbox' )
\r
247 attachFileBrowser( editor, dialogName, definition, element.children );
\r
249 if ( !element.filebrowser )
\r
252 if ( typeof element.filebrowser == 'string' )
\r
256 action : ( element.type == 'fileButton' ) ? 'QuickUpload' : 'Browse',
\r
257 target : element.filebrowser
\r
259 element.filebrowser = fb;
\r
262 if ( element.filebrowser.action == 'Browse' )
\r
264 var url = element.filebrowser.url;
\r
265 if ( url === undefined )
\r
267 url = editor.config[ 'filebrowser' + ucFirst( dialogName ) + 'BrowseUrl' ];
\r
268 if ( url === undefined )
\r
269 url = editor.config.filebrowserBrowseUrl;
\r
274 element.onClick = browseServer;
\r
275 element.filebrowser.url = url;
\r
276 element.hidden = false;
\r
279 else if ( element.filebrowser.action == 'QuickUpload' && element[ 'for' ] )
\r
281 url = element.filebrowser.url;
\r
282 if ( url === undefined )
\r
284 url = editor.config[ 'filebrowser' + ucFirst( dialogName ) + 'UploadUrl' ];
\r
285 if ( url === undefined )
\r
286 url = editor.config.filebrowserUploadUrl;
\r
291 var onClick = element.onClick;
\r
292 element.onClick = function( evt )
\r
294 // "element" here means the definition object, so we need to find the correct
\r
295 // button to scope the event call
\r
296 var sender = evt.sender;
\r
297 if ( onClick && onClick.call( sender, evt ) === false )
\r
300 return uploadFile.call( sender, evt );
\r
303 element.filebrowser.url = url;
\r
304 element.hidden = false;
\r
305 setupFileElement( editor, definition.getContents( element[ 'for' ][ 0 ] ).get( element[ 'for' ][ 1 ] ), element.filebrowser );
\r
312 * Updates the target element with the url of uploaded/selected file.
\r
315 * url The url of a file.
\r
317 function updateTargetElement( url, sourceElement )
\r
319 var dialog = sourceElement.getDialog();
\r
320 var targetElement = sourceElement.filebrowser.target || null;
\r
321 url = url.replace( /#/g, '%23' );
\r
323 // If there is a reference to targetElement, update it.
\r
324 if ( targetElement )
\r
326 var target = targetElement.split( ':' );
\r
327 var element = dialog.getContentElement( target[ 0 ], target[ 1 ] );
\r
330 element.setValue( url );
\r
331 dialog.selectPage( target[ 0 ] );
\r
337 * Returns true if filebrowser is configured in one of the elements.
\r
339 * @param {CKEDITOR.dialog.definitionObject}
\r
340 * definition Dialog definition.
\r
342 * tabId The tab id where element(s) can be found.
\r
344 * elementId The element id (or ids, separated with a semicolon) to check.
\r
346 function isConfigured( definition, tabId, elementId )
\r
348 if ( elementId.indexOf( ";" ) !== -1 )
\r
350 var ids = elementId.split( ";" );
\r
351 for ( var i = 0 ; i < ids.length ; i++ )
\r
353 if ( isConfigured( definition, tabId, ids[i] ) )
\r
359 var elementFileBrowser = definition.getContents( tabId ).get( elementId ).filebrowser;
\r
360 return ( elementFileBrowser && elementFileBrowser.url );
\r
363 function setUrl( fileUrl, data )
\r
365 var dialog = this._.filebrowserSe.getDialog(),
\r
366 targetInput = this._.filebrowserSe[ 'for' ],
\r
367 onSelect = this._.filebrowserSe.filebrowser.onSelect;
\r
370 dialog.getContentElement( targetInput[ 0 ], targetInput[ 1 ] ).reset();
\r
372 if ( typeof data == 'function' && data.call( this._.filebrowserSe ) === false )
\r
375 if ( onSelect && onSelect.call( this._.filebrowserSe, fileUrl, data ) === false )
\r
378 // The "data" argument may be used to pass the error message to the editor.
\r
379 if ( typeof data == 'string' && data )
\r
383 updateTargetElement( fileUrl, this._.filebrowserSe );
\r
386 CKEDITOR.plugins.add( 'filebrowser',
\r
388 init : function( editor, pluginPath )
\r
390 editor._.filebrowserFn = CKEDITOR.tools.addFunction( setUrl, editor );
\r
391 editor.on( 'destroy', function () { CKEDITOR.tools.removeFunction( this._.filebrowserFn ); } );
\r
395 CKEDITOR.on( 'dialogDefinition', function( evt )
\r
397 var definition = evt.data.definition,
\r
399 // Associate filebrowser to elements with 'filebrowser' attribute.
\r
400 for ( var i in definition.contents )
\r
402 if ( ( element = definition.contents[ i ] ) )
\r
404 attachFileBrowser( evt.editor, evt.data.name, definition, element.elements );
\r
405 if ( element.hidden && element.filebrowser )
\r
407 element.hidden = !isConfigured( definition, element[ 'id' ], element.filebrowser );
\r
416 * The location of an external file browser, that should be launched when "Browse Server" button is pressed.
\r
417 * If configured, the "Browse Server" button will appear in Link, Image and Flash dialogs.
\r
418 * @see The <a href="http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)">File Browser/Uploader</a> documentation.
\r
419 * @name CKEDITOR.config.filebrowserBrowseUrl
\r
422 * @default '' (empty string = disabled)
\r
424 * config.filebrowserBrowseUrl = '/browser/browse.php';
\r
428 * The location of a script that handles file uploads.
\r
429 * If set, the "Upload" tab will appear in "Link", "Image" and "Flash" dialogs.
\r
430 * @name CKEDITOR.config.filebrowserUploadUrl
\r
431 * @see The <a href="http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)">File Browser/Uploader</a> documentation.
\r
434 * @default '' (empty string = disabled)
\r
436 * config.filebrowserUploadUrl = '/uploader/upload.php';
\r
440 * The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Image dialog.
\r
441 * If not set, CKEditor will use {@link CKEDITOR.config.filebrowserBrowseUrl}.
\r
442 * @name CKEDITOR.config.filebrowserImageBrowseUrl
\r
445 * @default '' (empty string = disabled)
\r
447 * config.filebrowserImageBrowseUrl = '/browser/browse.php?type=Images';
\r
451 * The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Flash dialog.
\r
452 * If not set, CKEditor will use {@link CKEDITOR.config.filebrowserBrowseUrl}.
\r
453 * @name CKEDITOR.config.filebrowserFlashBrowseUrl
\r
456 * @default '' (empty string = disabled)
\r
458 * config.filebrowserFlashBrowseUrl = '/browser/browse.php?type=Flash';
\r
462 * The location of a script that handles file uploads in the Image dialog.
\r
463 * If not set, CKEditor will use {@link CKEDITOR.config.filebrowserUploadUrl}.
\r
464 * @name CKEDITOR.config.filebrowserImageUploadUrl
\r
467 * @default '' (empty string = disabled)
\r
469 * config.filebrowserImageUploadUrl = '/uploader/upload.php?type=Images';
\r
473 * The location of a script that handles file uploads in the Flash dialog.
\r
474 * If not set, CKEditor will use {@link CKEDITOR.config.filebrowserUploadUrl}.
\r
475 * @name CKEDITOR.config.filebrowserFlashUploadUrl
\r
478 * @default '' (empty string = disabled)
\r
480 * config.filebrowserFlashUploadUrl = '/uploader/upload.php?type=Flash';
\r
484 * The location of an external file browser, that should be launched when "Browse Server" button is pressed in the Link tab of Image dialog.
\r
485 * If not set, CKEditor will use {@link CKEDITOR.config.filebrowserBrowseUrl}.
\r
486 * @name CKEDITOR.config.filebrowserImageBrowseLinkUrl
\r
489 * @default '' (empty string = disabled)
\r
491 * config.filebrowserImageBrowseLinkUrl = '/browser/browse.php';
\r
495 * The "features" to use in the file browser popup window.
\r
496 * @name CKEDITOR.config.filebrowserWindowFeatures
\r
499 * @default 'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes,scrollbars=yes'
\r
501 * config.filebrowserWindowFeatures = 'resizable=yes,scrollbars=no';
\r
505 * The width of the file browser popup window. It can be a number or a percent string.
\r
506 * @name CKEDITOR.config.filebrowserWindowWidth
\r
507 * @type Number|String
\r
510 * config.filebrowserWindowWidth = 750;
\r
512 * config.filebrowserWindowWidth = '50%';
\r
516 * The height of the file browser popup window. It can be a number or a percent string.
\r
517 * @name CKEDITOR.config.filebrowserWindowHeight
\r
518 * @type Number|String
\r
521 * config.filebrowserWindowHeight = 580;
\r
523 * config.filebrowserWindowHeight = '50%';
\r