controller.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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( [
  7. 'collection',
  8. 'model',
  9. 'ckeditorerror',
  10. ], function( Collection, Model, CKEditorError ) {
  11. class Controller extends Model {
  12. /**
  13. * @constructor
  14. */
  15. constructor( model, view ) {
  16. super();
  17. /**
  18. * Model of this controller.
  19. *
  20. * @property {Model}
  21. */
  22. this.model = model;
  23. /**
  24. * View of this controller.
  25. *
  26. * @property {View}
  27. */
  28. this.view = view;
  29. /**
  30. * Set `true` after {@link #init}.
  31. *
  32. * @property {Boolean}
  33. */
  34. this.ready = false;
  35. /**
  36. * Collections of child controllers.
  37. *
  38. * @private
  39. * @property {Collection}
  40. */
  41. this._collections = new Collection( {
  42. idProperty: 'name'
  43. } );
  44. }
  45. /**
  46. * Initializes the controller instance. The process includes:
  47. * * initialization of the child {@link #view}.
  48. * * initialization of child controllers in {@link #_collections}.
  49. * * setting {@link #ready} flag `true`.
  50. *
  51. * @returns {Promise} A Promise resolved when the initialization process is finished.
  52. */
  53. init() {
  54. if ( this.ready ) {
  55. throw new CKEditorError( 'ui-controller-init-reinit: This Controller already been initialized.' );
  56. }
  57. return Promise.resolve()
  58. .then( this._initView.bind( this ) )
  59. .then( this._initCollections.bind( this ) )
  60. .then( () => {
  61. this.ready = true;
  62. } );
  63. }
  64. /**
  65. * Initializes the {@link #view} of this controller instance.
  66. *
  67. * @protected
  68. * @returns {Promise} A Promise resolved when initialization process is finished.
  69. */
  70. _initView() {
  71. let promise = Promise.resolve();
  72. if ( this.view ) {
  73. promise = promise.then( this.view.init.bind( this.view ) );
  74. }
  75. return promise;
  76. }
  77. /**
  78. * Initializes the {@link #_collections} of this controller instance.
  79. *
  80. * @protected
  81. * @returns {Promise} A Promise resolved when initialization process is finished.
  82. */
  83. _initCollections() {
  84. const promises = [];
  85. let collection, childController;
  86. for ( collection of this._collections ) {
  87. for ( childController of collection ) {
  88. if ( this.view, childController.view ) {
  89. this.view.addChild( collection.name, childController.view );
  90. }
  91. promises.push( childController.init() );
  92. }
  93. }
  94. return Promise.all( promises );
  95. }
  96. /**
  97. * Adds a child controller to one of the {@link #_collections}.
  98. * If this controller instance is ready, the child view will be initialized when added.
  99. * If this controller and child controller have views, the child view will be added
  100. * to corresponding region in this controller's view.
  101. *
  102. * @param {String} collectionName One of {@link _collections} the child should be added to.
  103. * @param {Controller} childController A child controller.
  104. * @param {Number} [index] Index at which the child will be added to the collection.
  105. * @returns {Promise} A Promise resolved when the child is added.
  106. */
  107. addChild( collectionName, childController, index ) {
  108. if ( !collectionName ) {
  109. throw new CKEditorError( 'ui-controller-addchild-badcname' );
  110. }
  111. const collection = this._collections.get( collectionName );
  112. if ( !collection ) {
  113. throw new CKEditorError( 'ui-controller-addchild-nocol' );
  114. }
  115. if ( !childController || !( childController instanceof Controller ) ) {
  116. throw new CKEditorError( 'ui-controller-addchild-badtype' );
  117. }
  118. // ChildController.init() returns Promise.
  119. let promise = Promise.resolve();
  120. collection.add( childController, index );
  121. if ( this.ready ) {
  122. if ( childController.view ) {
  123. this.view.addChild( collectionName, childController.view, index );
  124. }
  125. if ( !childController.ready ) {
  126. promise = promise.then( () => {
  127. return childController.init();
  128. } );
  129. }
  130. }
  131. return promise;
  132. }
  133. /**
  134. * Removes a child controller from one of the {@link #_collections}.
  135. * If this controller and child controller have views, the child view will be removed
  136. * from corresponding region in this controller's view.
  137. *
  138. * @param {String} collectionName One of {@link _collections} the child should be removed from.
  139. * @param {Controller} childController A child controller.
  140. * @returns {Controller} A child controller instance after removal.
  141. */
  142. removeChild( collectionName, childController ) {
  143. if ( !collectionName ) {
  144. throw new CKEditorError( 'ui-controller-removechild-badcname' );
  145. }
  146. const collection = this._collections.get( collectionName );
  147. if ( !collection ) {
  148. throw new CKEditorError( 'ui-controller-removechild-nocol' );
  149. }
  150. if ( !childController || !( childController instanceof Controller ) ) {
  151. throw new CKEditorError( 'ui-controller-removechild-badtype' );
  152. }
  153. collection.remove( childController );
  154. if ( this.ready && childController.view ) {
  155. this.view.removeChild( collectionName, childController.view );
  156. }
  157. return childController;
  158. }
  159. /**
  160. * Returns a child controller from one of the {@link #_collections} at given `index`.
  161. *
  162. * @param {String} collectionName One of {@link _collections} the child should be retrieved from.
  163. * @param {Number} [index] An index of desired controller.
  164. * @returns {Controller} A child controller instance.
  165. */
  166. getChild( collectionName, index ) {
  167. const collection = this._collections.get( collectionName );
  168. if ( !collection ) {
  169. throw new CKEditorError( 'ui-controller-getchild-nocol' );
  170. }
  171. return collection.get( index );
  172. }
  173. /**
  174. * Registers a collection in {@link #_collections}.
  175. *
  176. * @param {String} collectionName The name of the collection to be registered.
  177. * @param {Collection} collection Collection to be registered.
  178. * @param {Boolean} [override] When set `true` it will allow overriding of registered collections.
  179. */
  180. register( collectionName, collection, override ) {
  181. const registered = this._collections.get( collectionName );
  182. const that = this;
  183. if ( !( collection instanceof Collection ) ) {
  184. throw new CKEditorError( 'ui-controller-register-badtype' );
  185. }
  186. if ( !registered ) {
  187. add( collection );
  188. } else {
  189. if ( registered !== collection ) {
  190. if ( !override ) {
  191. throw new CKEditorError( 'ui-controller-register-noverride' );
  192. }
  193. that._collections.remove( registered );
  194. add( collection );
  195. }
  196. }
  197. function add() {
  198. collection.name = collectionName;
  199. that._collections.add( collection );
  200. }
  201. }
  202. /**
  203. * Destroys the controller instance. The process includes:
  204. * * destruction of the child {@link #view}.
  205. * * destruction of child controllers in {@link #_collections}.
  206. *
  207. * @returns {Promise} A Promise resolved when the destruction process is finished.
  208. */
  209. destroy() {
  210. let promises = [];
  211. let collection, childController;
  212. for ( collection of this._collections ) {
  213. for ( childController of collection ) {
  214. if ( this.view && childController.view ) {
  215. this.view.removeChild( collection.name, childController.view );
  216. }
  217. promises.push( childController.destroy() );
  218. collection.remove( childController );
  219. }
  220. this._collections.remove( collection );
  221. }
  222. if ( this.view ) {
  223. promises.push( Promise.resolve().then( () => {
  224. return this.view.destroy();
  225. } ) );
  226. }
  227. promises.push( Promise.resolve().then( () => {
  228. this.model = this.view = this._collections = null;
  229. } ) );
  230. return Promise.all( promises );
  231. }
  232. }
  233. return Controller;
  234. } );