region.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 ) => {
  36. if ( this.el ) {
  37. this.el.appendChild( view.el );
  38. }
  39. } );
  40. this.views.on( 'remove', ( evt, view ) => {
  41. view.el.remove();
  42. } );
  43. }
  44. /**
  45. * Destroys the Region instance.
  46. */
  47. destroy() {
  48. // Drop the reference to HTMLElement but don't remove it from DOM.
  49. // Element comes as a parameter and it could be a part of the View.
  50. // Then it's up to the View what to do with it when the View is destroyed.
  51. this.el = null;
  52. // Remove and destroy views.
  53. for ( let view of this.views ) {
  54. this.views.remove( view ).destroy();
  55. }
  56. }
  57. add( view, index ) {
  58. this.views.add( view, index );
  59. }
  60. remove( view ) {
  61. this.views.remove( view );
  62. }
  63. }
  64. return Region;
  65. } );