region.js 1.7 KB

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