region.js 1.7 KB

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