plugin.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or http://ckeditor.com/license
  4. */
  5. /**
  6. * @fileOverview The "Notification" plugin.
  7. *
  8. */
  9. 'use strict';
  10. CKEDITOR.plugins.add( 'notification', {
  11. lang: 'cs,da,de,en,eo,fr,gl,it,ko,ku,nb,nl,pl,pt-br,ru,sv,tr,zh,zh-cn', // %REMOVE_LINE_CORE%
  12. requires: 'toolbar',
  13. init: function( editor ) {
  14. editor._.notificationArea = new Area( editor );
  15. // Overwrites default `editor.showNotification`.
  16. editor.showNotification = function( message, type, progressOrDuration ) {
  17. var progress, duration;
  18. if ( type == 'progress' ) {
  19. progress = progressOrDuration;
  20. } else {
  21. duration = progressOrDuration;
  22. }
  23. var notification = new CKEDITOR.plugins.notification( editor, {
  24. message: message,
  25. type: type,
  26. progress: progress,
  27. duration: duration
  28. } );
  29. notification.show();
  30. return notification;
  31. };
  32. // Close the last notification on ESC.
  33. editor.on( 'key', function( evt ) {
  34. if ( evt.data.keyCode == 27 ) { /* ESC */
  35. var notifications = editor._.notificationArea.notifications;
  36. if ( !notifications.length ) {
  37. return;
  38. }
  39. // As long as this is not a common practice to inform screen-reader users about actions, in this case
  40. // this is the best solution (unfortunately there is no standard for accessibility for notifications).
  41. // Notification has an `alert` aria role what means that it does not get a focus nor is needed to be
  42. // closed (unlike `alertdialog`). However notification will capture ESC key so we need to inform user
  43. // why it does not do other actions.
  44. say( editor.lang.notification.closed );
  45. // Hide last.
  46. notifications[ notifications.length - 1 ].hide();
  47. evt.cancel();
  48. }
  49. } );
  50. // Send the message to the screen readers.
  51. function say( text ) {
  52. var message = new CKEDITOR.dom.element( 'div' );
  53. message.setStyles( {
  54. position: 'fixed',
  55. 'margin-left': '-9999px'
  56. } );
  57. message.setAttributes( {
  58. 'aria-live': 'assertive',
  59. 'aria-atomic': 'true'
  60. } );
  61. message.setText( text );
  62. CKEDITOR.document.getBody().append( message );
  63. setTimeout( function() {
  64. message.remove();
  65. }, 100 );
  66. }
  67. }
  68. } );
  69. /**
  70. * Notification class. Notifications are used to display short messages to the user. They might be used to show the result of
  71. * asynchronous actions or information about changes in the editor content. It is recommended to use them instead of
  72. * alert dialogs. They should **not** be used if a user response is required nor with dialog windows (e.g. in dialog validation).
  73. *
  74. * There are four types of notifications available, see the {@link #type} property.
  75. *
  76. * Note that the notification constructor only creates a notification instance. To show it, use the {@link #show} method:
  77. *
  78. * var notification = new CKEDITOR.plugins.notification( editor, { message: 'Foo' } );
  79. * notification.show();
  80. *
  81. * You can also use the {@link CKEDITOR.editor#showNotification} method:
  82. *
  83. * editor.showNotification( 'Foo' );
  84. *
  85. * All of the notification actions: ({@link #show}, {@link #update} and {@link #hide}) fire cancelable events
  86. * on the related {@link CKEDITOR.editor} instance so you can integrate editor notifications with your website notifications.
  87. *
  88. * Refer to the [Notifications](http://docs.ckeditor.com/#!/guide/dev_notifications) article for more information about this feature.
  89. *
  90. * @since 4.5
  91. * @class CKEDITOR.plugins.notification
  92. * @constructor Create a notification object. Call {@link #show} to show the created notification.
  93. * @param {CKEDITOR.editor} editor The editor instance.
  94. * @param {Object} options
  95. * @param {String} options.message The message displayed in the notification.
  96. * @param {String} [options.type='info'] Notification type, see {@link #type}.
  97. * @param {Number} [options.progress=0] If the type is `progress` this may be a progress from 0 to 1.
  98. * @param {Number} [options.duration] How long the notification will be visible, see {@link #duration}.
  99. */
  100. function Notification( editor, options ) {
  101. CKEDITOR.tools.extend( this, options, {
  102. editor: editor,
  103. id: 'cke-' + CKEDITOR.tools.getUniqueId(),
  104. area: editor._.notificationArea
  105. } );
  106. if ( !options.type ) {
  107. this.type = 'info';
  108. }
  109. this.element = this._createElement();
  110. // Don't allow dragging on notification (#13184).
  111. editor.plugins.clipboard && CKEDITOR.plugins.clipboard.preventDefaultDropOnElement( this.element );
  112. }
  113. /**
  114. * The editor instance.
  115. *
  116. * @readonly
  117. * @property {CKEDITOR.editor} editor
  118. */
  119. /**
  120. * Message displayed in the notification.
  121. *
  122. * @readonly
  123. * @property {String} message
  124. */
  125. /**
  126. * Notification type. There are four types available:
  127. *
  128. * * `info` (default) – Information for the user (e.g. "File is uploading.", "ACF modified content."),
  129. * * `warning` – Warning or error message (e.g. "This type of file is not supported.",
  130. * "You cannot paste the script."),
  131. * * `success` – Information that an operation finished successfully (e.g. "File uploaded.", "Data imported.").
  132. * * `progress` – Information about the progress of an operation. When the operation is done, the notification
  133. * type should be changed to `success`.
  134. *
  135. * @readonly
  136. * @property {String} type
  137. */
  138. /**
  139. * If the notification {@link #type} is `'progress'`, this is the progress from `0` to `1`.
  140. *
  141. * @readonly
  142. * @property {Number} progress
  143. */
  144. /**
  145. * Notification duration. Determines after how many milliseconds the notification should close automatically.
  146. * `0` means that the notification will not close automatically and that the user needs to close it manually.
  147. * The default value for `warning` and `progress` notifications is `0`. For `info` and `success` the value can
  148. * either be set through the {@link CKEDITOR.config#notification_duration} configuration option or equals `5000`
  149. * if the configuration option is not set.
  150. *
  151. * @readonly
  152. * @property {Number} duration
  153. */
  154. /**
  155. * Unique notification ID.
  156. *
  157. * @readonly
  158. * @property {Number} id
  159. */
  160. /**
  161. * Notification DOM element. There is one element per notification. It is created when the notification is created,
  162. * even if it is not shown. If the notification is hidden, the element is detached from the document but not deleted.
  163. * It will be reused if the notification is shown again.
  164. *
  165. * @readonly
  166. * @property {CKEDITOR.dom.element} element
  167. */
  168. /**
  169. * {@link CKEDITOR.plugins.notification.area Notification area} reference.
  170. *
  171. * @readonly
  172. * @property {CKEDITOR.plugins.notification.area} area
  173. */
  174. Notification.prototype = {
  175. /**
  176. * Adds the notification element to the notification area. The notification will be hidden automatically if
  177. * {@link #duration} is set.
  178. *
  179. * Fires the {@link CKEDITOR.editor#notificationShow} event.
  180. */
  181. show: function() {
  182. if ( this.editor.fire( 'notificationShow', { notification: this } ) === false ) {
  183. return;
  184. }
  185. this.area.add( this );
  186. this._hideAfterTimeout();
  187. },
  188. /**
  189. * Updates the notification object and element.
  190. *
  191. * Fires the {@link CKEDITOR.editor#notificationUpdate} event.
  192. *
  193. * @param {Object} options
  194. * @param {String} [options.message] {@link #message}
  195. * @param {String} [options.type] {@link #type}
  196. * @param {Number} [options.progress] {@link #progress}
  197. * @param {Number} [options.duration] {@link #duration}
  198. * @param {Boolean} [options.important=false] If the update is important, the notification will be shown
  199. * if it was hidden and read by screen readers.
  200. */
  201. update: function( options ) {
  202. var show = true;
  203. if ( this.editor.fire( 'notificationUpdate', { notification: this, options: options } ) === false ) {
  204. // The idea of cancelable event is to let user create his own way of displaying notification, so if
  205. // `notificationUpdate` event will be canceled there will be no interaction with notification area, but on
  206. // the other hand the logic should work anyway so object will be updated (including `element` property).
  207. // Note: we can safely update the element's attributes below, because this element is created inside
  208. // the constructor. If the notificatinShow event was canceled as well, the element is detached from DOM.
  209. show = false;
  210. }
  211. var element = this.element,
  212. messageElement = element.findOne( '.cke_notification_message' ),
  213. progressElement = element.findOne( '.cke_notification_progress' ),
  214. type = options.type;
  215. element.removeAttribute( 'role' );
  216. // Change type to progress if `options.progress` is set.
  217. if ( options.progress && this.type != 'progress' ) {
  218. type = 'progress';
  219. }
  220. if ( type ) {
  221. element.removeClass( this._getClass() );
  222. element.removeAttribute( 'aria-label' );
  223. this.type = type;
  224. element.addClass( this._getClass() );
  225. element.setAttribute( 'aria-label', this.type );
  226. if ( this.type == 'progress' && !progressElement ) {
  227. progressElement = this._createProgressElement();
  228. progressElement.insertBefore( messageElement );
  229. } else if ( this.type != 'progress' && progressElement ) {
  230. progressElement.remove();
  231. }
  232. }
  233. if ( options.message !== undefined ) {
  234. this.message = options.message;
  235. messageElement.setHtml( this.message );
  236. }
  237. if ( options.progress !== undefined ) {
  238. this.progress = options.progress;
  239. if ( progressElement ) {
  240. progressElement.setStyle( 'width', this._getPercentageProgress() );
  241. }
  242. }
  243. if ( show && options.important ) {
  244. element.setAttribute( 'role', 'alert' );
  245. if ( !this.isVisible() ) {
  246. this.area.add( this );
  247. }
  248. }
  249. // Overwrite even if it is undefined.
  250. this.duration = options.duration;
  251. this._hideAfterTimeout();
  252. },
  253. /**
  254. * Removes the notification element from the notification area.
  255. *
  256. * Fires the {@link CKEDITOR.editor#notificationHide} event.
  257. */
  258. hide: function() {
  259. if ( this.editor.fire( 'notificationHide', { notification: this } ) === false ) {
  260. return;
  261. }
  262. this.area.remove( this );
  263. },
  264. /**
  265. * Returns `true` if the notification is in the notification area.
  266. *
  267. * @returns {Boolean} `true` if the notification is in the notification area.
  268. */
  269. isVisible: function() {
  270. return CKEDITOR.tools.indexOf( this.area.notifications, this ) >= 0;
  271. },
  272. /**
  273. * Creates the notification DOM element.
  274. *
  275. * @private
  276. * @returns {CKEDITOR.dom.element} Notification DOM element.
  277. */
  278. _createElement: function() {
  279. var notification = this,
  280. notificationElement, notificationMessageElement, notificationCloseElement,
  281. close = this.editor.lang.common.close;
  282. notificationElement = new CKEDITOR.dom.element( 'div' );
  283. notificationElement.addClass( 'cke_notification' );
  284. notificationElement.addClass( this._getClass() );
  285. notificationElement.setAttributes( {
  286. id: this.id,
  287. role: 'alert',
  288. 'aria-label': this.type
  289. } );
  290. if ( this.type == 'progress' )
  291. notificationElement.append( this._createProgressElement() );
  292. notificationMessageElement = new CKEDITOR.dom.element( 'p' );
  293. notificationMessageElement.addClass( 'cke_notification_message' );
  294. notificationMessageElement.setHtml( this.message );
  295. notificationElement.append( notificationMessageElement );
  296. notificationCloseElement = CKEDITOR.dom.element.createFromHtml(
  297. '<a class="cke_notification_close" href="javascript:void(0)" title="' + close + '" role="button" tabindex="-1">' +
  298. '<span class="cke_label">X</span>' +
  299. '</a>' );
  300. notificationElement.append( notificationCloseElement );
  301. notificationCloseElement.on( 'click', function() {
  302. // Focus editor on close (#12865)
  303. notification.editor.focus();
  304. notification.hide();
  305. } );
  306. return notificationElement;
  307. },
  308. /**
  309. * Gets the notification CSS class.
  310. *
  311. * @private
  312. * @returns {String} Notification CSS class.
  313. */
  314. _getClass: function() {
  315. return ( this.type == 'progress' ) ?
  316. 'cke_notification_info' :
  317. ( 'cke_notification_' + this.type );
  318. },
  319. /**
  320. * Creates a progress element for the notification element.
  321. *
  322. * @private
  323. * @returns {CKEDITOR.dom.element} Progress element for the notification element.
  324. */
  325. _createProgressElement: function() {
  326. var element = new CKEDITOR.dom.element( 'span' );
  327. element.addClass( 'cke_notification_progress' );
  328. element.setStyle( 'width', this._getPercentageProgress() );
  329. return element;
  330. },
  331. /**
  332. * Gets the progress as a percentage (ex. `0.3` -> `30%`).
  333. *
  334. * @private
  335. * @returns {String} Progress as a percentage.
  336. */
  337. _getPercentageProgress: function() {
  338. return Math.round( ( this.progress || 0 ) * 100 ) + '%';
  339. },
  340. /**
  341. * Hides the notification after a timeout.
  342. *
  343. * @private
  344. */
  345. _hideAfterTimeout: function() {
  346. var notification = this,
  347. duration;
  348. if ( this._hideTimeoutId ) {
  349. clearTimeout( this._hideTimeoutId );
  350. }
  351. if ( typeof this.duration == 'number' ) {
  352. duration = this.duration;
  353. } else if ( this.type == 'info' || this.type == 'success' ) {
  354. duration = ( typeof this.editor.config.notification_duration == 'number' ) ?
  355. this.editor.config.notification_duration :
  356. 5000;
  357. }
  358. if ( duration ) {
  359. notification._hideTimeoutId = setTimeout( function() {
  360. notification.hide();
  361. }, duration );
  362. }
  363. }
  364. };
  365. /**
  366. * Notification area is an area where all notifications are put. The area is laid out dynamically.
  367. * When the first notification is added, the area is shown and all listeners are added.
  368. * When the last notification is removed, the area is hidden and all listeners are removed.
  369. *
  370. * @since 4.5
  371. * @private
  372. * @class CKEDITOR.plugins.notification.area
  373. * @constructor
  374. * @param {CKEDITOR.editor} editor The editor instance.
  375. */
  376. function Area( editor ) {
  377. var that = this;
  378. this.editor = editor;
  379. this.notifications = [];
  380. this.element = this._createElement();
  381. this._uiBuffer = CKEDITOR.tools.eventsBuffer( 10, this._layout, this );
  382. this._changeBuffer = CKEDITOR.tools.eventsBuffer( 500, this._layout, this );
  383. editor.on( 'destroy', function() {
  384. that._removeListeners();
  385. that.element.remove();
  386. } );
  387. }
  388. /**
  389. * The editor instance.
  390. *
  391. * @readonly
  392. * @property {CKEDITOR.editor} editor
  393. */
  394. /**
  395. * The array of added notifications.
  396. *
  397. * @readonly
  398. * @property {Array} notifications
  399. */
  400. /**
  401. * Notification area DOM element. This element is created when the area object is created. It will be attached to the document
  402. * when the first notification is added and removed when the last notification is removed.
  403. *
  404. * @readonly
  405. * @property {CKEDITOR.dom.element} element
  406. */
  407. /**
  408. * Notification width. Cached for performance reasons.
  409. *
  410. * @private
  411. * @property {CKEDITOR.dom.element} _notificationWidth
  412. */
  413. /**
  414. * Notification margin. Cached for performance reasons.
  415. *
  416. * @private
  417. * @property {CKEDITOR.dom.element} _notificationMargin
  418. */
  419. /**
  420. * Event buffer object for UI events to optimize performance.
  421. *
  422. * @private
  423. * @property {Object} _uiBuffer
  424. */
  425. /**
  426. * Event buffer object for editor change events to optimize performance.
  427. *
  428. * @private
  429. * @property {Object} _changeBuffer
  430. */
  431. Area.prototype = {
  432. /**
  433. * Adds the notification to the notification area. If it is the first notification, the area will also be attached to
  434. * the document and listeners will be attached.
  435. *
  436. * Note that the proper way to show a notification is to call the {@link CKEDITOR.plugins.notification#show} method.
  437. *
  438. * @param {CKEDITOR.plugins.notification} notification Notification to add.
  439. */
  440. add: function( notification ) {
  441. this.notifications.push( notification );
  442. this.element.append( notification.element );
  443. if ( this.element.getChildCount() == 1 ) {
  444. CKEDITOR.document.getBody().append( this.element );
  445. this._attachListeners();
  446. }
  447. this._layout();
  448. },
  449. /**
  450. * Removes the notification from the notification area. If it is the last notification, the area will also be
  451. * detached from the document and listeners will be detached.
  452. *
  453. * Note that the proper way to hide a notification is to call the {@link CKEDITOR.plugins.notification#hide} method.
  454. *
  455. * @param {CKEDITOR.plugins.notification} notification Notification to remove.
  456. */
  457. remove: function( notification ) {
  458. var i = CKEDITOR.tools.indexOf( this.notifications, notification );
  459. if ( i < 0 ) {
  460. return;
  461. }
  462. this.notifications.splice( i, 1 );
  463. notification.element.remove();
  464. if ( !this.element.getChildCount() ) {
  465. this._removeListeners();
  466. this.element.remove();
  467. }
  468. },
  469. /**
  470. * Creates the notification area element.
  471. *
  472. * @private
  473. * @returns {CKEDITOR.dom.element} Notification area element.
  474. */
  475. _createElement: function() {
  476. var editor = this.editor,
  477. config = editor.config,
  478. notificationArea = new CKEDITOR.dom.element( 'div' );
  479. notificationArea.addClass( 'cke_notifications_area' );
  480. notificationArea.setAttribute( 'id', 'cke_notifications_area_' + editor.name );
  481. notificationArea.setStyle( 'z-index', config.baseFloatZIndex - 2 );
  482. return notificationArea;
  483. },
  484. /**
  485. * Attaches listeners to the notification area.
  486. *
  487. * @private
  488. */
  489. _attachListeners: function() {
  490. var win = CKEDITOR.document.getWindow(),
  491. editor = this.editor;
  492. win.on( 'scroll', this._uiBuffer.input );
  493. win.on( 'resize', this._uiBuffer.input );
  494. editor.on( 'change', this._changeBuffer.input );
  495. editor.on( 'floatingSpaceLayout', this._layout, this, null, 20 );
  496. editor.on( 'blur', this._layout, this, null, 20 );
  497. },
  498. /**
  499. * Detaches listeners from the notification area.
  500. *
  501. * @private
  502. */
  503. _removeListeners: function() {
  504. var win = CKEDITOR.document.getWindow(),
  505. editor = this.editor;
  506. win.removeListener( 'scroll', this._uiBuffer.input );
  507. win.removeListener( 'resize', this._uiBuffer.input );
  508. editor.removeListener( 'change', this._changeBuffer.input );
  509. editor.removeListener( 'floatingSpaceLayout', this._layout );
  510. editor.removeListener( 'blur', this._layout );
  511. },
  512. /**
  513. * Sets the position of the notification area based on the editor content, toolbar as well as
  514. * viewport position and dimensions.
  515. *
  516. * @private
  517. */
  518. _layout: function() {
  519. var area = this.element,
  520. editor = this.editor,
  521. contentsRect = editor.ui.contentsElement.getClientRect(),
  522. contentsPos = editor.ui.contentsElement.getDocumentPosition(),
  523. top = editor.ui.space( 'top' ),
  524. topRect = top.getClientRect(),
  525. areaRect = area.getClientRect(),
  526. notification,
  527. notificationWidth = this._notificationWidth,
  528. notificationMargin = this._notificationMargin,
  529. win = CKEDITOR.document.getWindow(),
  530. scrollPos = win.getScrollPosition(),
  531. viewRect = win.getViewPaneSize(),
  532. body = CKEDITOR.document.getBody(),
  533. bodyPos = body.getDocumentPosition(),
  534. cssLength = CKEDITOR.tools.cssLength;
  535. // Cache for optimization
  536. if ( !notificationWidth || !notificationMargin ) {
  537. notification = this.element.getChild( 0 );
  538. notificationWidth = this._notificationWidth = notification.getClientRect().width;
  539. notificationMargin = this._notificationMargin =
  540. parseInt( notification.getComputedStyle( 'margin-left' ), 10 ) +
  541. parseInt( notification.getComputedStyle( 'margin-right' ), 10 );
  542. }
  543. // --------------------------------------- Horizontal layout ----------------------------------------
  544. // +---Viewport-------------------------------+ +---Viewport-------------------------------+
  545. // | | | |
  546. // | +---Toolbar----------------------------+ | | +---Content----------------------------+ |
  547. // | | | | | | | |
  548. // | +---Content----------------------------+ | | | | |
  549. // | | | | | +---Toolbar----------------------+ | |
  550. // | | +------Notification------+ | | | | | | |
  551. // | | | | OR | +--------------------------------+ | |
  552. // | | | | | | | |
  553. // | | | | | | +------Notification------+ | |
  554. // | | | | | | | |
  555. // | | | | | | | |
  556. // | +--------------------------------------+ | | +--------------------------------------+ |
  557. // +------------------------------------------+ +------------------------------------------+
  558. if ( top.isVisible() &&
  559. topRect.bottom > contentsRect.top &&
  560. topRect.bottom < contentsRect.bottom - areaRect.height ) {
  561. setBelowToolbar();
  562. // +---Viewport-------------------------------+
  563. // | |
  564. // | +---Content----------------------------+ |
  565. // | | | |
  566. // | | +------Notification------+ | |
  567. // | | | |
  568. // | | | |
  569. // | | | |
  570. // | +--------------------------------------+ |
  571. // | |
  572. // +------------------------------------------+
  573. } else if ( contentsRect.top > 0 ) {
  574. setTopStandard();
  575. // +---Content----------------------------+
  576. // | |
  577. // +---Viewport-------------------------------+
  578. // | | | |
  579. // | | +------Notification------+ | |
  580. // | | | |
  581. // | | | |
  582. // | | | |
  583. // | +--------------------------------------+ |
  584. // | |
  585. // +------------------------------------------+
  586. } else if ( contentsPos.y + contentsRect.height - areaRect.height > scrollPos.y ) {
  587. setTopFixed();
  588. // +---Content----------------------------+ +---Content----------------------------+
  589. // | | | |
  590. // | | | |
  591. // | | | +------Notification------+ |
  592. // | | | |
  593. // | | OR +--------------------------------------+
  594. // +---Viewport-------------------------------+
  595. // | | +------Notification------+ | | +---Viewport-------------------------------+
  596. // | | | | | |
  597. // | +--------------------------------------+ | | |
  598. // | | | |
  599. // +------------------------------------------+ +------------------------------------------+
  600. } else {
  601. setBottom();
  602. }
  603. function setTopStandard() {
  604. area.setStyles( {
  605. position: 'absolute',
  606. top: cssLength( contentsPos.y )
  607. } );
  608. }
  609. function setBelowToolbar() {
  610. area.setStyles( {
  611. position: 'fixed',
  612. top: cssLength( topRect.bottom )
  613. } );
  614. }
  615. function setTopFixed() {
  616. area.setStyles( {
  617. position: 'fixed',
  618. top: 0
  619. } );
  620. }
  621. function setBottom() {
  622. area.setStyles( {
  623. position: 'absolute',
  624. top: cssLength( contentsPos.y + contentsRect.height - areaRect.height )
  625. } );
  626. }
  627. // ---------------------------------------- Vertical layout -----------------------------------------
  628. var leftBase = area.getStyle( 'position' ) == 'fixed' ?
  629. contentsRect.left :
  630. body.getComputedStyle( 'position' ) != 'static' ? contentsPos.x - bodyPos.x : contentsPos.x;
  631. // Content is narrower than notification
  632. if ( contentsRect.width < notificationWidth + notificationMargin ) {
  633. // +---Viewport-------------------------------+
  634. // | |
  635. // | +---Content------------+ |
  636. // | | | |
  637. // | +------Notification------+ | |
  638. // | | | |
  639. // | +----------------------+ |
  640. // | |
  641. // +------------------------------------------+
  642. if ( contentsPos.x + notificationWidth + notificationMargin > scrollPos.x + viewRect.width ) {
  643. setRight();
  644. // +---Viewport-------------------------------+ +---Viewport--------------------------+
  645. // | | | |
  646. // | +---Content------------+ | +---Content------------+ |
  647. // | | | | | | | |
  648. // | | +------Notification------+ | OR | +------Notification------+ |
  649. // | | | | | | | |
  650. // | +----------------------+ | +----------------------+ |
  651. // | | | |
  652. // +------------------------------------------+ +-------------------------------------+
  653. } else {
  654. setLeft();
  655. }
  656. // Content is wider than notification.
  657. } else {
  658. // +--+Viewport+------------------------+
  659. // | |
  660. // | +---Content-----------------------------------------+
  661. // | | | |
  662. // | | +-----+Notification+-----+ |
  663. // | | | |
  664. // | | | |
  665. // | | | |
  666. // | +---------------------------------------------------+
  667. // | |
  668. // +------------------------------------+
  669. if ( contentsPos.x + notificationWidth + notificationMargin > scrollPos.x + viewRect.width ) {
  670. setLeft();
  671. // +---Viewport-------------------------+
  672. // | |
  673. // | +---Content----------------------------------------------+
  674. // | | | |
  675. // | | +------Notification------+ | |
  676. // | | | |
  677. // | | | |
  678. // | +--------------------------------------------------------+
  679. // | |
  680. // +------------------------------------+
  681. } else if ( contentsPos.x + contentsRect.width / 2 +
  682. notificationWidth / 2 + notificationMargin > scrollPos.x + viewRect.width ) {
  683. setRightFixed();
  684. // +---Viewport-------------------------+
  685. // | |
  686. // +---Content----------------------------+ |
  687. // | | | |
  688. // | +------Notification------+ | |
  689. // | | | |
  690. // | | | |
  691. // +--------------------------------------+ |
  692. // | |
  693. // +------------------------------------+
  694. } else if ( contentsRect.left + contentsRect.width - notificationWidth - notificationMargin < 0 ) {
  695. setRight();
  696. // +---Viewport-------------------------+
  697. // | |
  698. // +---Content---------------------------------------------+ |
  699. // | | | |
  700. // | | +------Notification------+ | |
  701. // | | | |
  702. // | | | |
  703. // +-------------------------------------------------------+ |
  704. // | |
  705. // +------------------------------------+
  706. } else if ( contentsRect.left + contentsRect.width / 2 - notificationWidth / 2 < 0 ) {
  707. setLeftFixed();
  708. // +---Viewport-------------------------+
  709. // | |
  710. // | +---Content----------------------+ |
  711. // | | | |
  712. // | | +-----Notification-----+ | |
  713. // | | | |
  714. // | | | |
  715. // | +--------------------------------+ |
  716. // | |
  717. // +------------------------------------+
  718. } else {
  719. setCenter();
  720. }
  721. }
  722. function setLeft() {
  723. area.setStyle( 'left', cssLength( leftBase ) );
  724. }
  725. function setLeftFixed() {
  726. area.setStyle( 'left', cssLength( leftBase - contentsPos.x + scrollPos.x ) );
  727. }
  728. function setCenter() {
  729. area.setStyle( 'left', cssLength( leftBase + contentsRect.width / 2 - notificationWidth / 2 - notificationMargin / 2 ) );
  730. }
  731. function setRight() {
  732. area.setStyle( 'left', cssLength( leftBase + contentsRect.width - notificationWidth - notificationMargin ) );
  733. }
  734. function setRightFixed() {
  735. area.setStyle( 'left', cssLength( leftBase - contentsPos.x + scrollPos.x + viewRect.width -
  736. notificationWidth - notificationMargin ) );
  737. }
  738. }
  739. };
  740. CKEDITOR.plugins.notification = Notification;
  741. /**
  742. * After how many milliseconds the notification of the `info` and `success`
  743. * {@link CKEDITOR.plugins.notification#type type} should close automatically.
  744. * `0` means that notifications will not close automatically.
  745. * Note that `warning` and `progress` notifications will never close automatically.
  746. *
  747. * Refer to the [Notifications](http://docs.ckeditor.com/#!/guide/dev_notifications) article
  748. * for more information about this feature.
  749. *
  750. * @since 4.5
  751. * @cfg {Number} [notification_duration=5000]
  752. * @member CKEDITOR.config
  753. */
  754. /**
  755. * Event fired when the {@link CKEDITOR.plugins.notification#show} method is called, before the
  756. * notification is shown. If this event is canceled, the notification will not be shown.
  757. *
  758. * Using this event allows you to fully customize how a notification will be shown. It may be used to integrate
  759. * the CKEditor notification system with your web page notifications.
  760. *
  761. * @since 4.5
  762. * @event notificationShow
  763. * @member CKEDITOR.editor
  764. * @param data
  765. * @param {CKEDITOR.plugins.notification} data.notification Notification which will be shown.
  766. * @param {CKEDITOR.editor} editor The editor instance.
  767. */
  768. /**
  769. * Event fired when the {@link CKEDITOR.plugins.notification#update} method is called, before the
  770. * notification is updated. If this event is canceled, the notification will not be shown even if the update was important,
  771. * but the object will be updated anyway. Note that canceling this event does not prevent updating {@link #element}
  772. * attributes, but if {@link #notificationShow} was canceled as well, this element is detached from the DOM.
  773. *
  774. * Using this event allows you to fully customize how a notification will be updated. It may be used to integrate
  775. * the CKEditor notification system with your web page notifications.
  776. *
  777. * @since 4.5
  778. * @event notificationUpdate
  779. * @member CKEDITOR.editor
  780. * @param data
  781. * @param {CKEDITOR.plugins.notification} data.notification Notification which will be updated.
  782. * Note that it contains the data that has not been updated yet.
  783. * @param {Object} data.options Update options, see {@link CKEDITOR.plugins.notification#update}.
  784. * @param {CKEDITOR.editor} editor The editor instance.
  785. */
  786. /**
  787. * Event fired when the {@link CKEDITOR.plugins.notification#hide} method is called, before the
  788. * notification is hidden. If this event is canceled, the notification will not be hidden.
  789. *
  790. * Using this event allows you to fully customize how a notification will be hidden. It may be used to integrate
  791. * the CKEditor notification system with your web page notifications.
  792. *
  793. * @since 4.5
  794. * @event notificationHide
  795. * @member CKEDITOR.editor
  796. * @param data
  797. * @param {CKEDITOR.plugins.notification} data.notification Notification which will be hidden.
  798. * @param {CKEDITOR.editor} editor The editor instance.
  799. */