region.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Collection from '../utils/collection.js';
  6. /**
  7. * Basic Region class.
  8. *
  9. * @memberOf ui
  10. */
  11. export default class Region {
  12. /**
  13. * Creates an instance of the {@link ui.Region} class.
  14. *
  15. * @param {String} name The name of the Region.
  16. */
  17. constructor( name ) {
  18. /**
  19. * The name of the region.
  20. *
  21. * @member {String} ui.Region#name
  22. */
  23. this.name = name;
  24. /**
  25. * Views which belong to the region.
  26. *
  27. * @member {utils.Collection} ui.Region#views
  28. */
  29. this.views = new Collection();
  30. /**
  31. * Element of this region (see {@link #init}).
  32. *
  33. * @member {HTMLElement} ui.Region#element
  34. */
  35. this.element = null;
  36. }
  37. /**
  38. * Initializes region instance with an element. Usually it comes from {@link View#init}.
  39. *
  40. * @param {HTMLElement} regionElement Element of this region.
  41. */
  42. init( regionElement ) {
  43. this.element = regionElement;
  44. if ( regionElement ) {
  45. this.views.on( 'add', ( evt, childView, index ) => {
  46. regionElement.insertBefore( childView.element, regionElement.childNodes[ index ] );
  47. } );
  48. this.views.on( 'remove', ( evt, childView ) => {
  49. childView.element.remove();
  50. } );
  51. }
  52. }
  53. /**
  54. * Destroys region instance.
  55. */
  56. destroy() {
  57. if ( this.element ) {
  58. for ( let view of this.views ) {
  59. view.element.remove();
  60. this.views.remove( view );
  61. }
  62. }
  63. // Drop the reference to HTMLElement but don't remove it from DOM.
  64. // Element comes as a parameter and it could be a part of the View.
  65. // Then it's up to the View what to do with it when the View is destroyed.
  66. this.element = this.views = null;
  67. }
  68. }