8
0

controllercollection.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. import CKEditorError from '../ckeditorerror.js';
  8. /**
  9. * Manages UI Controllers.
  10. *
  11. * @class ControllerCollection
  12. * @extends Collection
  13. */
  14. export default class ControllerCollection extends Collection {
  15. /**
  16. * Creates an instance of the ControllerCollection class, initializing it with a name.
  17. *
  18. * @constructor
  19. */
  20. constructor( name ) {
  21. super();
  22. if ( !name ) {
  23. /**
  24. * ControllerCollection must be initialized with a name.
  25. *
  26. * @error ui-controllercollection-no-name
  27. */
  28. throw new CKEditorError( 'ui-controllercollection-no-name: ControllerCollection must be initialized with a name.' );
  29. }
  30. /**
  31. * Name of this collection.
  32. *
  33. * @property {String}
  34. */
  35. this.name = name;
  36. /**
  37. * Parent controller of this collection.
  38. *
  39. * @property {Controller}
  40. */
  41. this.parent = null;
  42. }
  43. /**
  44. * Adds a child controller to the collection. If {@link #parent} {@link Controller}
  45. * instance is ready, the child view is initialized when added.
  46. *
  47. * @param {Controller} controller A child controller.
  48. * @param {Number} [index] Index at which the child will be added to the collection.
  49. * @returns {Promise} A Promise resolved when the child {@link Controller#init} is done.
  50. */
  51. add( controller, index ) {
  52. super.add( controller, index );
  53. // ChildController.init() returns Promise.
  54. let promise = Promise.resolve();
  55. if ( this.parent && this.parent.ready && !controller.ready ) {
  56. promise = promise.then( () => {
  57. return controller.init();
  58. } );
  59. }
  60. return promise;
  61. }
  62. }