controller.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. CKEDITOR.define( [ 'collection', 'model' ], function( Collection, Model ) {
  7. class Controller extends Model {
  8. /**
  9. * @constructor
  10. */
  11. constructor( model, view ) {
  12. super();
  13. /**
  14. * Model of this controller.
  15. */
  16. this.model = model;
  17. /**
  18. * View of this controller.
  19. */
  20. this.view = view;
  21. /**
  22. * A collection of child controllers.
  23. */
  24. this.controllers = new Collection();
  25. }
  26. /**
  27. * @param
  28. * @returns
  29. */
  30. init() {
  31. // Note: Because this.view.init() can by sync as well as async,
  32. // this method is not returning this.view.init() directly.
  33. return Promise.resolve()
  34. .then( () => {
  35. return this.view.init();
  36. } );
  37. }
  38. /**
  39. * @param
  40. * @returns
  41. */
  42. append( controller, regionName ) {
  43. this.controllers.add( controller );
  44. // Note: Because controller.init() can by sync as well as async,
  45. // it is wrapped in promise.
  46. return Promise.resolve()
  47. .then( () => {
  48. return controller.init();
  49. } )
  50. .then( this.view.append.bind( this.view, controller.view, regionName ) )
  51. .then( () => {
  52. return controller;
  53. } );
  54. }
  55. /**
  56. * @param
  57. * @returns
  58. */
  59. destroy() {
  60. // Note: Because this.view.destroy() can by sync as well as async,
  61. // it is wrapped in promise.
  62. return Promise.resolve()
  63. .then( () => {
  64. return this.view.destroy();
  65. } )
  66. .then(
  67. Promise.all( this.controllers.filter( c => {
  68. return c.destroy();
  69. } ) )
  70. );
  71. }
  72. }
  73. return Controller;
  74. } );