1 | import { EventEmitter, Output } from '@angular/core';
|
2 | import { isPresent } from '../util/util';
|
3 | import { STATE_DESTROYED, STATE_NEW } from './nav-util';
|
4 | import { NavParams } from './nav-params';
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var ViewController = (function () {
|
23 | function ViewController(component, data, rootCssClass) {
|
24 | if (rootCssClass === void 0) { rootCssClass = DEFAULT_CSS_CLASS; }
|
25 | this.component = component;
|
26 | this._isHidden = false;
|
27 | this._state = STATE_NEW;
|
28 | |
29 |
|
30 |
|
31 |
|
32 | this.willEnter = new EventEmitter();
|
33 | |
34 |
|
35 |
|
36 |
|
37 | this.didEnter = new EventEmitter();
|
38 | |
39 |
|
40 |
|
41 |
|
42 | this.willLeave = new EventEmitter();
|
43 | |
44 |
|
45 |
|
46 |
|
47 | this.didLeave = new EventEmitter();
|
48 | |
49 |
|
50 |
|
51 |
|
52 | this.willUnload = new EventEmitter();
|
53 | |
54 |
|
55 |
|
56 | this.readReady = new EventEmitter();
|
57 | |
58 |
|
59 |
|
60 | this.writeReady = new EventEmitter();
|
61 |
|
62 | this.isOverlay = false;
|
63 |
|
64 | this._emitter = new EventEmitter();
|
65 |
|
66 | this.data = (data instanceof NavParams ? data.data : (isPresent(data) ? data : {}));
|
67 | this._cssClass = rootCssClass;
|
68 | this._ts = Date.now();
|
69 | this._bindHandler = this.handleOrientationChange.bind(this);
|
70 | window.addEventListener('orientationchange', this._bindHandler);
|
71 | }
|
72 | ViewController.prototype.handleOrientationChange = function () {
|
73 | if (this.getContent()) {
|
74 | this.getContent().resize();
|
75 | }
|
76 | };
|
77 | |
78 |
|
79 |
|
80 | ViewController.prototype.init = function (componentRef) {
|
81 | (void 0) ;
|
82 | this._ts = Date.now();
|
83 | this._cmp = componentRef;
|
84 | this.instance = this.instance || componentRef.instance;
|
85 | this._detached = false;
|
86 | };
|
87 | ViewController.prototype._setNav = function (navCtrl) {
|
88 | this._nav = navCtrl;
|
89 | };
|
90 | ViewController.prototype._setInstance = function (instance) {
|
91 | this.instance = instance;
|
92 | };
|
93 | |
94 |
|
95 |
|
96 | ViewController.prototype.subscribe = function (generatorOrNext) {
|
97 | return this._emitter.subscribe(generatorOrNext);
|
98 | };
|
99 | |
100 |
|
101 |
|
102 | ViewController.prototype.emit = function (data) {
|
103 | this._emitter.emit(data);
|
104 | };
|
105 | |
106 |
|
107 |
|
108 | ViewController.prototype.onDidDismiss = function (callback) {
|
109 | this._onDidDismiss = callback;
|
110 | };
|
111 | |
112 |
|
113 |
|
114 | ViewController.prototype.onWillDismiss = function (callback) {
|
115 | this._onWillDismiss = callback;
|
116 | };
|
117 | |
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | ViewController.prototype.dismiss = function (data, role, navOptions) {
|
125 | if (navOptions === void 0) { navOptions = {}; }
|
126 | if (!this._nav) {
|
127 | (void 0) ;
|
128 | return Promise.resolve(false);
|
129 | }
|
130 | if (this.isOverlay && !navOptions.minClickBlockDuration) {
|
131 |
|
132 |
|
133 |
|
134 | navOptions.minClickBlockDuration = 400;
|
135 | }
|
136 | this._dismissData = data;
|
137 | this._dismissRole = role;
|
138 | var options = Object.assign({}, this._leavingOpts, navOptions);
|
139 | return this._nav.removeView(this, options).then(function () { return data; });
|
140 | };
|
141 | |
142 |
|
143 |
|
144 | ViewController.prototype.getNav = function () {
|
145 | return this._nav;
|
146 | };
|
147 | |
148 |
|
149 |
|
150 | ViewController.prototype.getTransitionName = function (_direction) {
|
151 | return this._nav && this._nav.config.get('pageTransition');
|
152 | };
|
153 | |
154 |
|
155 |
|
156 | ViewController.prototype.getNavParams = function () {
|
157 | return new NavParams(this.data);
|
158 | };
|
159 | |
160 |
|
161 |
|
162 | ViewController.prototype.setLeavingOpts = function (opts) {
|
163 | this._leavingOpts = opts;
|
164 | };
|
165 | |
166 |
|
167 |
|
168 |
|
169 | ViewController.prototype.enableBack = function () {
|
170 |
|
171 | if (!this._nav) {
|
172 | return false;
|
173 | }
|
174 |
|
175 |
|
176 | var previousItem = this._nav.getPrevious(this);
|
177 | return !!(previousItem);
|
178 | };
|
179 | Object.defineProperty(ViewController.prototype, "name", {
|
180 | |
181 |
|
182 |
|
183 | get: function () {
|
184 | return (this.component ? this.component.name : '');
|
185 | },
|
186 | enumerable: true,
|
187 | configurable: true
|
188 | });
|
189 | Object.defineProperty(ViewController.prototype, "index", {
|
190 | |
191 |
|
192 |
|
193 |
|
194 | get: function () {
|
195 | return (this._nav ? this._nav.indexOf(this) : -1);
|
196 | },
|
197 | enumerable: true,
|
198 | configurable: true
|
199 | });
|
200 | |
201 |
|
202 |
|
203 | ViewController.prototype.isFirst = function () {
|
204 | return (this._nav ? this._nav.first() === this : false);
|
205 | };
|
206 | |
207 |
|
208 |
|
209 | ViewController.prototype.isLast = function () {
|
210 | return (this._nav ? this._nav.last() === this : false);
|
211 | };
|
212 | |
213 |
|
214 |
|
215 |
|
216 | ViewController.prototype._domShow = function (shouldShow, renderer) {
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 | if (this._cmp && shouldShow === this._isHidden) {
|
223 | this._isHidden = !shouldShow;
|
224 | var value = (shouldShow ? null : '');
|
225 |
|
226 | renderer.setElementAttribute(this.pageRef().nativeElement, 'hidden', value);
|
227 | }
|
228 | };
|
229 | |
230 |
|
231 |
|
232 | ViewController.prototype.getZIndex = function () {
|
233 | return this._zIndex;
|
234 | };
|
235 | |
236 |
|
237 |
|
238 |
|
239 | ViewController.prototype._setZIndex = function (zIndex, renderer) {
|
240 | if (zIndex !== this._zIndex) {
|
241 | this._zIndex = zIndex;
|
242 | var pageRef = this.pageRef();
|
243 | if (pageRef) {
|
244 |
|
245 | renderer.setElementStyle(pageRef.nativeElement, 'z-index', zIndex);
|
246 | }
|
247 | }
|
248 | };
|
249 | |
250 |
|
251 |
|
252 | ViewController.prototype.pageRef = function () {
|
253 | return this._cmp && this._cmp.location;
|
254 | };
|
255 | ViewController.prototype._setContent = function (directive) {
|
256 | this._cntDir = directive;
|
257 | };
|
258 | |
259 |
|
260 |
|
261 | ViewController.prototype.getContent = function () {
|
262 | return this._cntDir;
|
263 | };
|
264 | ViewController.prototype._setContentRef = function (elementRef) {
|
265 | this._cntRef = elementRef;
|
266 | };
|
267 | |
268 |
|
269 |
|
270 | ViewController.prototype.contentRef = function () {
|
271 | return this._cntRef;
|
272 | };
|
273 | ViewController.prototype._setIONContent = function (content) {
|
274 | this._setContent(content);
|
275 | this._ionCntDir = content;
|
276 | };
|
277 | |
278 |
|
279 |
|
280 | ViewController.prototype.getIONContent = function () {
|
281 | return this._ionCntDir;
|
282 | };
|
283 | ViewController.prototype._setIONContentRef = function (elementRef) {
|
284 | this._setContentRef(elementRef);
|
285 | this._ionCntRef = elementRef;
|
286 | };
|
287 | |
288 |
|
289 |
|
290 | ViewController.prototype.getIONContentRef = function () {
|
291 | return this._ionCntRef;
|
292 | };
|
293 | ViewController.prototype._setHeader = function (directive) {
|
294 | this._hdrDir = directive;
|
295 | };
|
296 | |
297 |
|
298 |
|
299 | ViewController.prototype.getHeader = function () {
|
300 | return this._hdrDir;
|
301 | };
|
302 | ViewController.prototype._setFooter = function (directive) {
|
303 | this._ftrDir = directive;
|
304 | };
|
305 | |
306 |
|
307 |
|
308 | ViewController.prototype.getFooter = function () {
|
309 | return this._ftrDir;
|
310 | };
|
311 | ViewController.prototype._setNavbar = function (directive) {
|
312 | this._nb = directive;
|
313 | };
|
314 | |
315 |
|
316 |
|
317 | ViewController.prototype.getNavbar = function () {
|
318 | return this._nb;
|
319 | };
|
320 | |
321 |
|
322 |
|
323 |
|
324 |
|
325 |
|
326 | ViewController.prototype.hasNavbar = function () {
|
327 | return !!this._nb;
|
328 | };
|
329 | |
330 |
|
331 |
|
332 |
|
333 |
|
334 | ViewController.prototype.setBackButtonText = function (val) {
|
335 | this._nb && this._nb.setBackButtonText(val);
|
336 | };
|
337 | |
338 |
|
339 |
|
340 |
|
341 |
|
342 | ViewController.prototype.showBackButton = function (shouldShow) {
|
343 | if (this._nb) {
|
344 | this._nb.hideBackButton = !shouldShow;
|
345 | }
|
346 | };
|
347 | ViewController.prototype._preLoad = function () {
|
348 | (void 0) ;
|
349 | this._lifecycle('PreLoad');
|
350 | };
|
351 | |
352 |
|
353 |
|
354 |
|
355 |
|
356 | ViewController.prototype._willLoad = function () {
|
357 | (void 0) ;
|
358 | this._lifecycle('WillLoad');
|
359 | };
|
360 | |
361 |
|
362 |
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | ViewController.prototype._didLoad = function () {
|
369 | (void 0) ;
|
370 | this._lifecycle('DidLoad');
|
371 | };
|
372 | |
373 |
|
374 |
|
375 |
|
376 | ViewController.prototype._willEnter = function () {
|
377 | this.handleOrientationChange();
|
378 | (void 0) ;
|
379 | if (this._detached && this._cmp) {
|
380 |
|
381 | this._cmp.changeDetectorRef.reattach();
|
382 | this._detached = false;
|
383 | }
|
384 | this.willEnter.emit(null);
|
385 | this._lifecycle('WillEnter');
|
386 | };
|
387 | |
388 |
|
389 |
|
390 |
|
391 |
|
392 | ViewController.prototype._didEnter = function () {
|
393 | (void 0) ;
|
394 | this._nb && this._nb.didEnter();
|
395 | this.didEnter.emit(null);
|
396 | this._lifecycle('DidEnter');
|
397 | };
|
398 | |
399 |
|
400 |
|
401 |
|
402 | ViewController.prototype._willLeave = function (willUnload) {
|
403 | this.willLeave.emit(null);
|
404 | this._lifecycle('WillLeave');
|
405 | if (willUnload && this._onWillDismiss) {
|
406 | this._onWillDismiss(this._dismissData, this._dismissRole);
|
407 | this._onWillDismiss = null;
|
408 | }
|
409 | };
|
410 | |
411 |
|
412 |
|
413 |
|
414 |
|
415 | ViewController.prototype._didLeave = function () {
|
416 | this.didLeave.emit(null);
|
417 | this._lifecycle('DidLeave');
|
418 |
|
419 |
|
420 | if (!this._detached && this._cmp) {
|
421 | this._cmp.changeDetectorRef.detach();
|
422 | this._detached = true;
|
423 | }
|
424 | };
|
425 | |
426 |
|
427 |
|
428 | ViewController.prototype._willUnload = function () {
|
429 | this.willUnload.emit(null);
|
430 | this._lifecycle('WillUnload');
|
431 | this._onDidDismiss && this._onDidDismiss(this._dismissData, this._dismissRole);
|
432 | this._onDidDismiss = null;
|
433 | this._dismissData = null;
|
434 | this._dismissRole = null;
|
435 | };
|
436 | |
437 |
|
438 |
|
439 |
|
440 | ViewController.prototype._destroy = function (renderer) {
|
441 | (void 0) ;
|
442 | if (this._cmp) {
|
443 | if (renderer) {
|
444 |
|
445 |
|
446 | var cmpEle = this._cmp.location.nativeElement;
|
447 | renderer.setElementAttribute(cmpEle, 'class', null);
|
448 | renderer.setElementAttribute(cmpEle, 'style', null);
|
449 | }
|
450 | window.removeEventListener('orientationchange', this._bindHandler);
|
451 |
|
452 | this._cmp.destroy();
|
453 | }
|
454 | this._nav = this._cmp = this.instance = this._cntDir = this._cntRef = this._leavingOpts = this._hdrDir = this._ftrDir = this._nb = this._onDidDismiss = this._onWillDismiss = null;
|
455 | this._state = STATE_DESTROYED;
|
456 | };
|
457 | |
458 |
|
459 |
|
460 | ViewController.prototype._lifecycleTest = function (lifecycle) {
|
461 | var instance = this.instance;
|
462 | var methodName = 'ionViewCan' + lifecycle;
|
463 | if (instance && instance[methodName]) {
|
464 | try {
|
465 | var result = instance[methodName]();
|
466 | if (result instanceof Promise) {
|
467 | return result;
|
468 | }
|
469 | else {
|
470 |
|
471 | return Promise.resolve(result !== false);
|
472 | }
|
473 | }
|
474 | catch (e) {
|
475 | return Promise.reject(this.name + " " + methodName + " error: " + e.message);
|
476 | }
|
477 | }
|
478 | return Promise.resolve(true);
|
479 | };
|
480 | |
481 |
|
482 |
|
483 | ViewController.prototype._lifecycle = function (lifecycle) {
|
484 | var instance = this.instance;
|
485 | var methodName = 'ionView' + lifecycle;
|
486 | if (instance && instance[methodName]) {
|
487 | instance[methodName]();
|
488 | }
|
489 | };
|
490 | ViewController.propDecorators = {
|
491 | '_emitter': [{ type: Output },],
|
492 | };
|
493 | return ViewController;
|
494 | }());
|
495 | export { ViewController };
|
496 | export function isViewController(viewCtrl) {
|
497 | return !!(viewCtrl && viewCtrl._didLoad && viewCtrl._willUnload);
|
498 | }
|
499 | var DEFAULT_CSS_CLASS = 'ion-page';
|
500 |
|
\ | No newline at end of file |