PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/184.168.182.1/admin/old/ckeditor/_source/plugins/filebrowser/plugin.js

https://gitlab.com/endomorphosis/falkenstein
JavaScript | 533 lines | 190 code | 49 blank | 294 comment | 60 complexity | 1411c40c0f1d455d47e5d7ceee8b5aba MD5 | raw file
  1. /*
  2. Copyright (c) 2003-2012, CKSource - Frederico Knabben. All rights reserved.
  3. For licensing, see LICENSE.html or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The "filebrowser" plugin that adds support for file uploads and
  7. * browsing.
  8. *
  9. * When a file is uploaded or selected inside the file browser, its URL is
  10. * inserted automatically into a field defined in the <code>filebrowser</code>
  11. * attribute. In order to specify a field that should be updated, pass the tab ID and
  12. * the element ID, separated with a colon.<br /><br />
  13. *
  14. * <strong>Example 1: (Browse)</strong>
  15. *
  16. * <pre>
  17. * {
  18. * type : 'button',
  19. * id : 'browse',
  20. * filebrowser : 'tabId:elementId',
  21. * label : editor.lang.common.browseServer
  22. * }
  23. * </pre>
  24. *
  25. * If you set the <code>filebrowser</code> attribute for an element other than
  26. * the <code>fileButton</code>, the <code>Browse</code> action will be triggered.<br /><br />
  27. *
  28. * <strong>Example 2: (Quick Upload)</strong>
  29. *
  30. * <pre>
  31. * {
  32. * type : 'fileButton',
  33. * id : 'uploadButton',
  34. * filebrowser : 'tabId:elementId',
  35. * label : editor.lang.common.uploadSubmit,
  36. * 'for' : [ 'upload', 'upload' ]
  37. * }
  38. * </pre>
  39. *
  40. * If you set the <code>filebrowser</code> attribute for a <code>fileButton</code>
  41. * element, the <code>QuickUpload</code> action will be executed.<br /><br />
  42. *
  43. * The filebrowser plugin also supports more advanced configuration performed through
  44. * a JavaScript object.
  45. *
  46. * The following settings are supported:
  47. *
  48. * <ul>
  49. * <li><code>action</code> &ndash; <code>Browse</code> or <code>QuickUpload</code>.</li>
  50. * <li><code>target</code> &ndash; the field to update in the <code><em>tabId:elementId</em></code> format.</li>
  51. * <li><code>params</code> &ndash; additional arguments to be passed to the server connector (optional).</li>
  52. * <li><code>onSelect</code> &ndash; a function to execute when the file is selected/uploaded (optional).</li>
  53. * <li><code>url</code> &ndash; the URL to be called (optional).</li>
  54. * </ul>
  55. *
  56. * <strong>Example 3: (Quick Upload)</strong>
  57. *
  58. * <pre>
  59. * {
  60. * type : 'fileButton',
  61. * label : editor.lang.common.uploadSubmit,
  62. * id : 'buttonId',
  63. * filebrowser :
  64. * {
  65. * action : 'QuickUpload', // required
  66. * target : 'tab1:elementId', // required
  67. * params : // optional
  68. * {
  69. * type : 'Files',
  70. * currentFolder : '/folder/'
  71. * },
  72. * onSelect : function( fileUrl, errorMessage ) // optional
  73. * {
  74. * // Do not call the built-in selectFuntion.
  75. * // return false;
  76. * }
  77. * },
  78. * 'for' : [ 'tab1', 'myFile' ]
  79. * }
  80. * </pre>
  81. *
  82. * Suppose you have a file element with an ID of <code>myFile</code>, a text
  83. * field with an ID of <code>elementId</code> and a <code>fileButton</code>.
  84. * If the <code>filebowser.url</code> attribute is not specified explicitly,
  85. * the form action will be set to <code>filebrowser[<em>DialogWindowName</em>]UploadUrl</code>
  86. * or, if not specified, to <code>filebrowserUploadUrl</code>. Additional parameters
  87. * from the <code>params</code> object will be added to the query string. It is
  88. * possible to create your own <code>uploadHandler</code> and cancel the built-in
  89. * <code>updateTargetElement</code> command.<br /><br />
  90. *
  91. * <strong>Example 4: (Browse)</strong>
  92. *
  93. * <pre>
  94. * {
  95. * type : 'button',
  96. * id : 'buttonId',
  97. * label : editor.lang.common.browseServer,
  98. * filebrowser :
  99. * {
  100. * action : 'Browse',
  101. * url : '/ckfinder/ckfinder.html&amp;type=Images',
  102. * target : 'tab1:elementId'
  103. * }
  104. * }
  105. * </pre>
  106. *
  107. * In this example, when the button is pressed, the file browser will be opened in a
  108. * popup window. If you do not specify the <code>filebrowser.url</code> attribute,
  109. * <code>filebrowser[<em>DialogName</em>]BrowseUrl</code> or
  110. * <code>filebrowserBrowseUrl</code> will be used. After selecting a file in the file
  111. * browser, an element with an ID of <code>elementId</code> will be updated. Just
  112. * like in the third example, a custom <code>onSelect</code> function may be defined.
  113. */
  114. ( function()
  115. {
  116. /*
  117. * Adds (additional) arguments to given url.
  118. *
  119. * @param {String}
  120. * url The url.
  121. * @param {Object}
  122. * params Additional parameters.
  123. */
  124. function addQueryString( url, params )
  125. {
  126. var queryString = [];
  127. if ( !params )
  128. return url;
  129. else
  130. {
  131. for ( var i in params )
  132. queryString.push( i + "=" + encodeURIComponent( params[ i ] ) );
  133. }
  134. return url + ( ( url.indexOf( "?" ) != -1 ) ? "&" : "?" ) + queryString.join( "&" );
  135. }
  136. /*
  137. * Make a string's first character uppercase.
  138. *
  139. * @param {String}
  140. * str String.
  141. */
  142. function ucFirst( str )
  143. {
  144. str += '';
  145. var f = str.charAt( 0 ).toUpperCase();
  146. return f + str.substr( 1 );
  147. }
  148. /*
  149. * The onlick function assigned to the 'Browse Server' button. Opens the
  150. * file browser and updates target field when file is selected.
  151. *
  152. * @param {CKEDITOR.event}
  153. * evt The event object.
  154. */
  155. function browseServer( evt )
  156. {
  157. var dialog = this.getDialog();
  158. var editor = dialog.getParentEditor();
  159. editor._.filebrowserSe = this;
  160. var width = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowWidth' ]
  161. || editor.config.filebrowserWindowWidth || '80%';
  162. var height = editor.config[ 'filebrowser' + ucFirst( dialog.getName() ) + 'WindowHeight' ]
  163. || editor.config.filebrowserWindowHeight || '70%';
  164. var params = this.filebrowser.params || {};
  165. params.CKEditor = editor.name;
  166. params.CKEditorFuncNum = editor._.filebrowserFn;
  167. if ( !params.langCode )
  168. params.langCode = editor.langCode;
  169. var url = addQueryString( this.filebrowser.url, params );
  170. // TODO: V4: Remove backward compatibility (#8163).
  171. editor.popup( url, width, height, editor.config.filebrowserWindowFeatures || editor.config.fileBrowserWindowFeatures );
  172. }
  173. /*
  174. * The onlick function assigned to the 'Upload' button. Makes the final
  175. * decision whether form is really submitted and updates target field when
  176. * file is uploaded.
  177. *
  178. * @param {CKEDITOR.event}
  179. * evt The event object.
  180. */
  181. function uploadFile( evt )
  182. {
  183. var dialog = this.getDialog();
  184. var editor = dialog.getParentEditor();
  185. editor._.filebrowserSe = this;
  186. // If user didn't select the file, stop the upload.
  187. if ( !dialog.getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getInputElement().$.value )
  188. return false;
  189. if ( !dialog.getContentElement( this[ 'for' ][ 0 ], this[ 'for' ][ 1 ] ).getAction() )
  190. return false;
  191. return true;
  192. }
  193. /*
  194. * Setups the file element.
  195. *
  196. * @param {CKEDITOR.ui.dialog.file}
  197. * fileInput The file element used during file upload.
  198. * @param {Object}
  199. * filebrowser Object containing filebrowser settings assigned to
  200. * the fileButton associated with this file element.
  201. */
  202. function setupFileElement( editor, fileInput, filebrowser )
  203. {
  204. var params = filebrowser.params || {};
  205. params.CKEditor = editor.name;
  206. params.CKEditorFuncNum = editor._.filebrowserFn;
  207. if ( !params.langCode )
  208. params.langCode = editor.langCode;
  209. fileInput.action = addQueryString( filebrowser.url, params );
  210. fileInput.filebrowser = filebrowser;
  211. }
  212. /*
  213. * Traverse through the content definition and attach filebrowser to
  214. * elements with 'filebrowser' attribute.
  215. *
  216. * @param String
  217. * dialogName Dialog name.
  218. * @param {CKEDITOR.dialog.definitionObject}
  219. * definition Dialog definition.
  220. * @param {Array}
  221. * elements Array of {@link CKEDITOR.dialog.definition.content}
  222. * objects.
  223. */
  224. function attachFileBrowser( editor, dialogName, definition, elements )
  225. {
  226. var element, fileInput;
  227. for ( var i in elements )
  228. {
  229. element = elements[ i ];
  230. if ( element.type == 'hbox' || element.type == 'vbox' || element.type == 'fieldset' )
  231. attachFileBrowser( editor, dialogName, definition, element.children );
  232. if ( !element.filebrowser )
  233. continue;
  234. if ( typeof element.filebrowser == 'string' )
  235. {
  236. var fb =
  237. {
  238. action : ( element.type == 'fileButton' ) ? 'QuickUpload' : 'Browse',
  239. target : element.filebrowser
  240. };
  241. element.filebrowser = fb;
  242. }
  243. if ( element.filebrowser.action == 'Browse' )
  244. {
  245. var url = element.filebrowser.url;
  246. if ( url === undefined )
  247. {
  248. url = editor.config[ 'filebrowser' + ucFirst( dialogName ) + 'BrowseUrl' ];
  249. if ( url === undefined )
  250. url = editor.config.filebrowserBrowseUrl;
  251. }
  252. if ( url )
  253. {
  254. element.onClick = browseServer;
  255. element.filebrowser.url = url;
  256. element.hidden = false;
  257. }
  258. }
  259. else if ( element.filebrowser.action == 'QuickUpload' && element[ 'for' ] )
  260. {
  261. url = element.filebrowser.url;
  262. if ( url === undefined )
  263. {
  264. url = editor.config[ 'filebrowser' + ucFirst( dialogName ) + 'UploadUrl' ];
  265. if ( url === undefined )
  266. url = editor.config.filebrowserUploadUrl;
  267. }
  268. if ( url )
  269. {
  270. var onClick = element.onClick;
  271. element.onClick = function( evt )
  272. {
  273. // "element" here means the definition object, so we need to find the correct
  274. // button to scope the event call
  275. var sender = evt.sender;
  276. if ( onClick && onClick.call( sender, evt ) === false )
  277. return false;
  278. return uploadFile.call( sender, evt );
  279. };
  280. element.filebrowser.url = url;
  281. element.hidden = false;
  282. setupFileElement( editor, definition.getContents( element[ 'for' ][ 0 ] ).get( element[ 'for' ][ 1 ] ), element.filebrowser );
  283. }
  284. }
  285. }
  286. }
  287. /*
  288. * Updates the target element with the url of uploaded/selected file.
  289. *
  290. * @param {String}
  291. * url The url of a file.
  292. */
  293. function updateTargetElement( url, sourceElement )
  294. {
  295. var dialog = sourceElement.getDialog();
  296. var targetElement = sourceElement.filebrowser.target || null;
  297. // If there is a reference to targetElement, update it.
  298. if ( targetElement )
  299. {
  300. var target = targetElement.split( ':' );
  301. var element = dialog.getContentElement( target[ 0 ], target[ 1 ] );
  302. if ( element )
  303. {
  304. element.setValue( url );
  305. dialog.selectPage( target[ 0 ] );
  306. }
  307. }
  308. }
  309. /*
  310. * Returns true if filebrowser is configured in one of the elements.
  311. *
  312. * @param {CKEDITOR.dialog.definitionObject}
  313. * definition Dialog definition.
  314. * @param String
  315. * tabId The tab id where element(s) can be found.
  316. * @param String
  317. * elementId The element id (or ids, separated with a semicolon) to check.
  318. */
  319. function isConfigured( definition, tabId, elementId )
  320. {
  321. if ( elementId.indexOf( ";" ) !== -1 )
  322. {
  323. var ids = elementId.split( ";" );
  324. for ( var i = 0 ; i < ids.length ; i++ )
  325. {
  326. if ( isConfigured( definition, tabId, ids[i] ) )
  327. return true;
  328. }
  329. return false;
  330. }
  331. var elementFileBrowser = definition.getContents( tabId ).get( elementId ).filebrowser;
  332. return ( elementFileBrowser && elementFileBrowser.url );
  333. }
  334. function setUrl( fileUrl, data )
  335. {
  336. var dialog = this._.filebrowserSe.getDialog(),
  337. targetInput = this._.filebrowserSe[ 'for' ],
  338. onSelect = this._.filebrowserSe.filebrowser.onSelect;
  339. if ( targetInput )
  340. dialog.getContentElement( targetInput[ 0 ], targetInput[ 1 ] ).reset();
  341. if ( typeof data == 'function' && data.call( this._.filebrowserSe ) === false )
  342. return;
  343. if ( onSelect && onSelect.call( this._.filebrowserSe, fileUrl, data ) === false )
  344. return;
  345. // The "data" argument may be used to pass the error message to the editor.
  346. if ( typeof data == 'string' && data )
  347. alert( data );
  348. if ( fileUrl )
  349. updateTargetElement( fileUrl, this._.filebrowserSe );
  350. }
  351. CKEDITOR.plugins.add( 'filebrowser',
  352. {
  353. init : function( editor, pluginPath )
  354. {
  355. editor._.filebrowserFn = CKEDITOR.tools.addFunction( setUrl, editor );
  356. editor.on( 'destroy', function () { CKEDITOR.tools.removeFunction( this._.filebrowserFn ); } );
  357. }
  358. } );
  359. CKEDITOR.on( 'dialogDefinition', function( evt )
  360. {
  361. var definition = evt.data.definition,
  362. element;
  363. // Associate filebrowser to elements with 'filebrowser' attribute.
  364. for ( var i in definition.contents )
  365. {
  366. if ( ( element = definition.contents[ i ] ) )
  367. {
  368. attachFileBrowser( evt.editor, evt.data.name, definition, element.elements );
  369. if ( element.hidden && element.filebrowser )
  370. {
  371. element.hidden = !isConfigured( definition, element[ 'id' ], element.filebrowser );
  372. }
  373. }
  374. }
  375. } );
  376. } )();
  377. /**
  378. * The location of an external file browser that should be launched when the <strong>Browse Server</strong>
  379. * button is pressed. If configured, the <strong>Browse Server</strong> button will appear in the
  380. * <strong>Link</strong>, <strong>Image</strong>, and <strong>Flash</strong> dialog windows.
  381. * @see The <a href="http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)">File Browser/Uploader</a> documentation.
  382. * @name CKEDITOR.config.filebrowserBrowseUrl
  383. * @since 3.0
  384. * @type String
  385. * @default <code>''</code> (empty string = disabled)
  386. * @example
  387. * config.filebrowserBrowseUrl = '/browser/browse.php';
  388. */
  389. /**
  390. * The location of the script that handles file uploads.
  391. * If set, the <strong>Upload</strong> tab will appear in the <strong>Link</strong>, <strong>Image</strong>,
  392. * and <strong>Flash</strong> dialog windows.
  393. * @name CKEDITOR.config.filebrowserUploadUrl
  394. * @see The <a href="http://docs.cksource.com/CKEditor_3.x/Developers_Guide/File_Browser_(Uploader)">File Browser/Uploader</a> documentation.
  395. * @since 3.0
  396. * @type String
  397. * @default <code>''</code> (empty string = disabled)
  398. * @example
  399. * config.filebrowserUploadUrl = '/uploader/upload.php';
  400. */
  401. /**
  402. * The location of an external file browser that should be launched when the <strong>Browse Server</strong>
  403. * button is pressed in the <strong>Image</strong> dialog window.
  404. * If not set, CKEditor will use <code>{@link CKEDITOR.config.filebrowserBrowseUrl}</code>.
  405. * @name CKEDITOR.config.filebrowserImageBrowseUrl
  406. * @since 3.0
  407. * @type String
  408. * @default <code>''</code> (empty string = disabled)
  409. * @example
  410. * config.filebrowserImageBrowseUrl = '/browser/browse.php?type=Images';
  411. */
  412. /**
  413. * The location of an external file browser that should be launched when the <strong>Browse Server</strong>
  414. * button is pressed in the <strong>Flash</strong> dialog window.
  415. * If not set, CKEditor will use <code>{@link CKEDITOR.config.filebrowserBrowseUrl}</code>.
  416. * @name CKEDITOR.config.filebrowserFlashBrowseUrl
  417. * @since 3.0
  418. * @type String
  419. * @default <code>''</code> (empty string = disabled)
  420. * @example
  421. * config.filebrowserFlashBrowseUrl = '/browser/browse.php?type=Flash';
  422. */
  423. /**
  424. * The location of the script that handles file uploads in the <strong>Image</strong> dialog window.
  425. * If not set, CKEditor will use <code>{@link CKEDITOR.config.filebrowserUploadUrl}</code>.
  426. * @name CKEDITOR.config.filebrowserImageUploadUrl
  427. * @since 3.0
  428. * @type String
  429. * @default <code>''</code> (empty string = disabled)
  430. * @example
  431. * config.filebrowserImageUploadUrl = '/uploader/upload.php?type=Images';
  432. */
  433. /**
  434. * The location of the script that handles file uploads in the <strong>Flash</strong> dialog window.
  435. * If not set, CKEditor will use <code>{@link CKEDITOR.config.filebrowserUploadUrl}</code>.
  436. * @name CKEDITOR.config.filebrowserFlashUploadUrl
  437. * @since 3.0
  438. * @type String
  439. * @default <code>''</code> (empty string = disabled)
  440. * @example
  441. * config.filebrowserFlashUploadUrl = '/uploader/upload.php?type=Flash';
  442. */
  443. /**
  444. * The location of an external file browser that should be launched when the <strong>Browse Server</strong>
  445. * button is pressed in the <strong>Link</strong> tab of the <strong>Image</strong> dialog window.
  446. * If not set, CKEditor will use <code>{@link CKEDITOR.config.filebrowserBrowseUrl}</code>.
  447. * @name CKEDITOR.config.filebrowserImageBrowseLinkUrl
  448. * @since 3.2
  449. * @type String
  450. * @default <code>''</code> (empty string = disabled)
  451. * @example
  452. * config.filebrowserImageBrowseLinkUrl = '/browser/browse.php';
  453. */
  454. /**
  455. * The features to use in the file browser popup window.
  456. * @name CKEDITOR.config.filebrowserWindowFeatures
  457. * @since 3.4.1
  458. * @type String
  459. * @default <code>'location=no,menubar=no,toolbar=no,dependent=yes,minimizable=no,modal=yes,alwaysRaised=yes,resizable=yes,scrollbars=yes'</code>
  460. * @example
  461. * config.filebrowserWindowFeatures = 'resizable=yes,scrollbars=no';
  462. */
  463. /**
  464. * The width of the file browser popup window. It can be a number denoting a value in
  465. * pixels or a percent string.
  466. * @name CKEDITOR.config.filebrowserWindowWidth
  467. * @type Number|String
  468. * @default <code>'80%'</code>
  469. * @example
  470. * config.filebrowserWindowWidth = 750;
  471. * @example
  472. * config.filebrowserWindowWidth = '50%';
  473. */
  474. /**
  475. * The height of the file browser popup window. It can be a number denoting a value in
  476. * pixels or a percent string.
  477. * @name CKEDITOR.config.filebrowserWindowHeight
  478. * @type Number|String
  479. * @default <code>'70%'</code>
  480. * @example
  481. * config.filebrowserWindowHeight = 580;
  482. * @example
  483. * config.filebrowserWindowHeight = '50%';
  484. */