This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcontroller.js
237 lines (204 loc) · 6.54 KB
/
controller.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Collection from '../utils/collection.js';
import ControllerCollection from './controllercollection.js';
import CKEditorError from '../utils/ckeditorerror.js';
import EmitterMixin from '../utils/emittermixin.js';
import mix from '../utils/mix.js';
/**
* Basic Controller class.
*
* @memberOf ui
* @mixes utils.EmitterMixin
*/
export default class Controller {
/**
* Creates an instance of the {@link ui.Controller} class.
*
* @param {ui.Model} [model] Model of this Controller.
* @param {ui.View} [view] View instance of this Controller.
*/
constructor( model, view ) {
/**
* Model of this controller.
*
* @member {ui.Model} ui.Controller#model
*/
this.model = model || null;
/**
* Set `true` after {@link #init}.
*
* @member {Boolean} ui.Controller#ready
*/
this.ready = false;
/**
* View of this controller.
*
* @member {ui.View} ui.Controller#view
*/
this.view = view || null;
/**
* A collection of {@link ControllerCollection} instances containing
* child controllers.
*
* @member {utils.Collection} ui.Controller#collections
*/
this.collections = new Collection( {
idProperty: 'name'
} );
// Listen to {@link ControllerCollection#add} and {@link ControllerCollection#remove}
// of newly added Collection to synchronize this controller's view and children
// controllers' views in the future.
this.collections.on( 'add', ( evt, collection ) => {
// Set the {@link ControllerCollection#parent} to this controller.
// It allows the collection to determine the {@link #ready} state of this controller
// and accordingly initialize a child controller when added.
collection.parent = this;
this.listenTo( collection, 'add', ( evt, childController, index ) => {
// Child view is added to corresponding region in this controller's view
// when a new Controller joins the collection.
if ( this.ready && childController.view ) {
this.view.regions.get( collection.name ).views.add( childController.view, index );
}
} );
this.listenTo( collection, 'remove', ( evt, childController ) => {
// Child view is removed from corresponding region in this controller's view
// when a new Controller is removed from the the collection.
if ( this.ready && childController.view ) {
this.view.regions.get( collection.name ).views.remove( childController.view );
}
} );
} );
this.collections.on( 'remove', ( evt, collection ) => {
// Release the collection. Once removed from {@link #collections}, it can be
// moved to another controller.
collection.parent = null;
this.stopListening( collection );
} );
}
/**
* Initializes the controller instance. The process includes:
*
* 1. Initialization of the child {@link #view}.
* 2. Initialization of child controllers in {@link #collections}.
* 3. Setting {@link #ready} flag `true`.
*
* @returns {Promise} A Promise resolved when the initialization process is finished.
*/
init() {
if ( this.ready ) {
/**
* This Controller already been initialized.
*
* @error ui-controller-init-reinit
*/
throw new CKEditorError( 'ui-controller-init-reinit: This Controller already been initialized.' );
}
return Promise.resolve()
.then( this._initView.bind( this ) )
.then( this._initCollections.bind( this ) )
.then( () => {
this.ready = true;
this.fire( 'ready' );
} );
}
/**
* Destroys the controller instance. The process includes:
*
* 1. Destruction of the child {@link #view}.
* 2. Destruction of child controllers in {@link #collections}.
*
* @returns {Promise} A Promise resolved when the destruction process is finished.
*/
destroy() {
let promises = [];
let collection, childController;
for ( collection of this.collections ) {
for ( childController of collection ) {
promises.push( childController.destroy() );
}
collection.clear();
}
this.collections.clear();
if ( this.view ) {
promises.push( Promise.resolve().then( () => {
return this.view.destroy();
} ) );
}
promises.push( Promise.resolve().then( () => {
this.model = this.ready = this.view = this.collections = null;
} ) );
return Promise.all( promises );
}
/**
* Adds a new collection to {@link ui.Controller#collections}.
*
* @param {String} collectionName Name of the controller collection.
* @returns {ui.ControllerCollection} The new collection instance.
*/
addCollection( collectionName ) {
const collection = new ControllerCollection( collectionName, this.view && this.view.locale );
this.collections.add( collection );
return collection;
}
/**
* Adds a child {@link Controller} instance to {@link #collections} at given index.
*
* @param {String} collectionName Name of the Controller Collection.
* @param {ui.Controller} controller A controller instance to be added.
* @param {Number} [index] An index in the collection.
*/
add( collectionName, controller, index ) {
this.collections.get( collectionName ).add( controller, index );
}
/**
* Removes a child {@link ui.Controller} instance from one of {@link ui.Controller#collections}.
*
* @param {String} collectionName Name of the Controller Collection.
* @param {ui.Controller|Number} toRemove A Controller instance or index to be removed.
* @returns {Object} The removed item.
*/
remove( collectionName, toRemove ) {
return this.collections.get( collectionName ).remove( toRemove );
}
/**
* Initializes the {@link #view} of this controller instance.
*
* @protected
* @returns {Promise} A Promise resolved when initialization process is finished.
*/
_initView() {
let promise = Promise.resolve();
if ( this.view ) {
promise = promise.then( this.view.init.bind( this.view ) );
}
return promise;
}
/**
* Initializes the {@link #collections} of this controller instance.
*
* @protected
* @returns {Promise} A Promise resolved when initialization process is finished.
*/
_initCollections() {
const promises = [];
let collection, childController;
for ( collection of this.collections ) {
for ( childController of collection ) {
if ( this.view && childController.view ) {
this.view.regions.get( collection.name ).views.add( childController.view );
}
promises.push( childController.init() );
}
}
return Promise.all( promises );
}
}
mix( Controller, EmitterMixin );
/**
* Fired when the controller is fully initialized.
*
* @event ui.Controller#ready
*/