componentrepository.js 996 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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 CKEditorError from '../ckeditorerror.js';
  7. /**
  8. * @class core.ui.ComponentRepository
  9. */
  10. export default class ComponentRepository {
  11. constructor( editor ) {
  12. /**
  13. * @readonly
  14. * @type {core.Editor}
  15. */
  16. this.editor = editor;
  17. this._components = new Map();
  18. }
  19. add( name, ControllerClass, ViewClass, model ) {
  20. if ( this._components.get( name ) ) {
  21. throw new CKEditorError(
  22. 'componentrepository-item-exists: The item already exists in the component registry', { name }
  23. );
  24. }
  25. this._components.set( name, {
  26. ControllerClass,
  27. ViewClass,
  28. model
  29. } );
  30. }
  31. create( name ) {
  32. const component = this._components.get( name );
  33. const model = component.model;
  34. const view = new component.ViewClass( model );
  35. const controller = new component.ControllerClass( this.editor, model, view );
  36. return controller;
  37. }
  38. }