region.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. /**
  7. * Basic Region class.
  8. *
  9. * @class Region
  10. * @extends Model
  11. */
  12. CKEDITOR.define( [ 'collection', 'model' ], ( Collection, Model ) => {
  13. class Region extends Model {
  14. /**
  15. * Creates an instance of the {@link Region} class.
  16. *
  17. * @param {String} name The name of the Region.
  18. * @param {HTMLElement} [el] The element used for this region.
  19. * @constructor
  20. */
  21. constructor( name, el ) {
  22. super();
  23. /**
  24. * The name of the region.
  25. */
  26. this.name = name;
  27. /**
  28. * The element of the region.
  29. */
  30. this.el = el;
  31. /**
  32. * Views which belong to the region.
  33. */
  34. this.views = new Collection();
  35. this.views.on( 'add', ( evt, view ) => this.el && this.el.appendChild( view.el ) );
  36. this.views.on( 'remove', ( evt, view ) => view.el.remove() );
  37. }
  38. /**
  39. * Destroys the Region instance.
  40. */
  41. destroy() {
  42. // Drop the reference to HTMLElement but don't remove it from DOM.
  43. // Element comes as a parameter and it could be a part of the View.
  44. // Then it's up to the View what to do with it when the View is destroyed.
  45. this.el = null;
  46. // Remove and destroy views.
  47. for ( let view of this.views ) {
  48. this.views.remove( view ).destroy();
  49. }
  50. }
  51. }
  52. return Region;
  53. } );