8
0

controllercollection.js 1.8 KB

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