contextualballoon.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module ui/contextualballoon
  7. */
  8. import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
  9. import BalloonPanelView from './panel/balloon/balloonpanelview';
  10. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  11. import nth from '@ckeditor/ckeditor5-utils/src/nth';
  12. /**
  13. * Provides the common contextual balloon panel for the editor.
  14. *
  15. * This plugin allows reusing a single {module:ui/panel/balloon/balloonpanelview~BalloonPanelView} instance
  16. * to display multiple contextual balloon panels in the editor.
  17. *
  18. * Child views of such a panel are stored in the stack and the last one in the stack is visible. When the
  19. * visible view is removed from the stack, the previous view becomes visible, etc. If there are no more
  20. * views in the stack, the balloon panel will hide.
  21. *
  22. * It simplifies managing the views and helps
  23. * avoid the unnecessary complexity of handling multiple {module:ui/panel/balloon/balloonpanelview~BalloonPanelView}
  24. * instances in the editor.
  25. *
  26. * @extends module:core/plugin~Plugin
  27. */
  28. export default class ContextualBalloon extends Plugin {
  29. /**
  30. * @inheritDoc
  31. */
  32. static get pluginName() {
  33. return 'contextualballoon';
  34. }
  35. /**
  36. * @inheritDoc
  37. */
  38. init() {
  39. /**
  40. * The common balloon panel view.
  41. *
  42. * @readonly
  43. * @member {module:ui/panel/balloon/balloonpanelview~BalloonPanelView} #view
  44. */
  45. this.view = new BalloonPanelView();
  46. /**
  47. * Stack of the views injected into the balloon. Last one in the stack is displayed
  48. * as a content of {@link module:ui/contextualballoon~ContextualBalloon#view}.
  49. *
  50. * @private
  51. * @member {Map} #_stack
  52. */
  53. this._stack = new Map();
  54. // Add balloon panel view to editor `body` collection.
  55. this.editor.ui.view.body.add( this.view );
  56. }
  57. /**
  58. * Returns the currently visible view or `null` when there are no
  59. * views in the stack.
  60. *
  61. * @returns {module:ui/view~View|null}
  62. */
  63. get visibleView() {
  64. const item = this._stack.get( this.view.content.get( 0 ) );
  65. return item ? item.view : null;
  66. }
  67. /**
  68. * Returns `true` when the given view is in the stack. Otherwise returns `false`.
  69. *
  70. * @param {module:ui/view~View} view
  71. * @returns {Boolean}
  72. */
  73. hasView( view ) {
  74. return this._stack.has( view );
  75. }
  76. /**
  77. * Adds a new view to the stack and makes it visible.
  78. *
  79. * @param {Object} data Configuration of the view.
  80. * @param {module:ui/view~View} [data.view] Content of the balloon.
  81. * @param {module:utils/dom/position~Options} [data.position] Positioning options.
  82. * @param {String} [data.balloonClassName] Additional css class for {@link #view} added when given view is visible.
  83. */
  84. add( data ) {
  85. if ( this.hasView( data.view ) ) {
  86. /**
  87. * Trying to add configuration of the same view more than once.
  88. *
  89. * @error contextualballoon-add-view-exist
  90. */
  91. throw new CKEditorError( 'contextualballoon-add-view-exist: Cannot add configuration of the same view twice.' );
  92. }
  93. // When adding view to the not empty balloon.
  94. if ( this.visibleView ) {
  95. // Remove displayed content from the view.
  96. this.view.content.remove( this.visibleView );
  97. }
  98. // Add new view to the stack.
  99. this._stack.set( data.view, data );
  100. // And display it.
  101. this._show( data );
  102. }
  103. /**
  104. * Removes the given view from the stack. If the removed view was visible,
  105. * then the view preceding it in the stack will become visible instead.
  106. * When there is no view in the stack then balloon will hide.
  107. *
  108. * @param {module:ui/view~View} view A view to be removed from the balloon.
  109. */
  110. remove( view ) {
  111. if ( !this.hasView( view ) ) {
  112. /**
  113. * Trying to remove configuration of the view not defined in the stack.
  114. *
  115. * @error contextualballoon-remove-view-not-exist
  116. */
  117. throw new CKEditorError( 'contextualballoon-remove-view-not-exist: Cannot remove configuration of not existing view.' );
  118. }
  119. // When visible view is being removed.
  120. if ( this.visibleView === view ) {
  121. // We need to remove it from the view content.
  122. this.view.content.remove( view );
  123. // And then remove from the stack.
  124. this._stack.delete( view );
  125. // Next we need to check if there is other view in stack to show.
  126. const last = Array.from( this._stack.values() ).pop();
  127. // If it is some other view.
  128. if ( last ) {
  129. // Just show it.
  130. this._show( last );
  131. } else {
  132. // Hide the balloon panel.
  133. this.view.hide();
  134. }
  135. } else {
  136. // Just remove given view from the stack.
  137. this._stack.delete( view );
  138. }
  139. }
  140. /**
  141. * Updates the position of the balloon panel according to the given position data
  142. * or position data of the first view in the stack.
  143. *
  144. * @param {module:utils/dom/position~Options} [position] position options.
  145. */
  146. updatePosition( position ) {
  147. if ( position ) {
  148. nth( 0, this._stack )[ 1 ].position = position;
  149. }
  150. this.view.attachTo( this._getBalloonPosition() );
  151. }
  152. /**
  153. * Sets the view as a content of the balloon and attaches balloon using position
  154. * options of the first view.
  155. *
  156. * @private
  157. * @param {Object} data Configuration.
  158. * @param {module:ui/view~View} [data.view] View to show in the balloon.
  159. * @param {String} [data.balloonClassName=''] View to show in the balloon.
  160. */
  161. _show( { view, balloonClassName = '' } ) {
  162. this.view.content.add( view );
  163. this.view.className = balloonClassName;
  164. // When view is not rendered we need to wait for it. See: https://github.com/ckeditor/ckeditor5-ui/issues/187.
  165. if ( !view.ready ) {
  166. view.once( 'change:ready', () => this.view.attachTo( this._getBalloonPosition() ) );
  167. } else {
  168. this.view.attachTo( this._getBalloonPosition() );
  169. }
  170. }
  171. /**
  172. * Returns position options of the first view in the stack.
  173. * This keeps the balloon in the same position when view is changed.
  174. *
  175. * @private
  176. * @returns {module:utils/dom/position~Options}
  177. */
  178. _getBalloonPosition() {
  179. return nth( 0, this._stack )[ 1 ].position;
  180. }
  181. /**
  182. * @inheritDoc
  183. */
  184. destroy() {
  185. this.editor.ui.view.body.remove( this.view );
  186. this.view.destroy();
  187. super.destroy();
  188. }
  189. }