3
|
1 define(["dojo/_base/declare", "../guard", "../safe", "../log/_LogMixin"], function (declare, guard, safe, _LogMixin) {
|
0
|
2 "use strict";
|
3
|
3 return declare([_LogMixin], {
|
0
|
4
|
3
|
5 _current: null,
|
0
|
6
|
3
|
7 _pending: false,
|
0
|
8
|
3
|
9 getCurrent: function () {
|
0
|
10 return this._current;
|
|
11 },
|
|
12
|
3
|
13 _start: function () {
|
0
|
14 if (this._pending)
|
|
15 throw new Error("The activation/decativation is already pending");
|
|
16 this._pending = true;
|
|
17 },
|
|
18
|
3
|
19 _await: function (d) {
|
0
|
20 var me = this;
|
3
|
21 return d.then(function (x) {
|
0
|
22 me._pending = false;
|
|
23 return x;
|
3
|
24 }, function (e) {
|
0
|
25 me._pending = false;
|
|
26 throw e;
|
|
27 });
|
|
28 },
|
|
29
|
3
|
30 activate: function (component) {
|
0
|
31 safe.argumentNotNull(component, "component");
|
|
32 var me = this;
|
|
33 if (component.getController() !== this)
|
|
34 throw new Error("The specified component doesn't belong to this controller");
|
|
35
|
3
|
36 return me._await(guard(me, "_start").then(function () {
|
0
|
37 me._activate(component);
|
|
38 }));
|
|
39 },
|
|
40
|
3
|
41 _activate: function (component) {
|
0
|
42 var me = this;
|
|
43 if (me._current === component)
|
|
44 return guard(false);
|
|
45
|
|
46 // before activation hook
|
3
|
47 return guard(me, "onActivating", [component]).then(function () {
|
0
|
48 // deactivate curent
|
|
49 if (me._current)
|
3
|
50 return me._current.deactivate(true).then(function () {
|
0
|
51 try {
|
|
52 me._current.onDeactivated();
|
|
53 } catch (err) {
|
|
54 me.error(err);
|
|
55 }
|
|
56 // HACK raise deactivated event
|
|
57 try {
|
|
58 me.onDeactivated(me._current, component);
|
|
59 } catch (err) {
|
|
60 // deactivated shouldn't affect the process
|
|
61 me.error(err);
|
|
62 }
|
|
63 me._current = null;
|
|
64
|
|
65 });
|
3
|
66 }).then(function () {
|
0
|
67 return component.activate(true);
|
3
|
68 }).then(function () {
|
0
|
69 me._current = component;
|
|
70 try {
|
|
71 me.onActivated(component);
|
|
72 } catch (err) {
|
|
73 me.error(err);
|
|
74 }
|
|
75
|
|
76 });
|
|
77
|
|
78 },
|
|
79
|
|
80 /**
|
|
81 * Деактивирует текущую компоненту.
|
|
82 *
|
|
83 * @async
|
|
84 * @returns true - компонента была деактивирована, либо нет активной
|
|
85 * компоненты. false - запрос на деактивацию - отклонен.
|
|
86 */
|
3
|
87 deactivate: function () {
|
0
|
88 var me = this;
|
3
|
89 return me._await(guard(me, "_start").then(function () {
|
0
|
90 return me._deactivate();
|
|
91 }));
|
|
92 },
|
|
93
|
3
|
94 _deactivate: function () {
|
0
|
95 var me = this;
|
|
96 if (!me._current)
|
|
97 return guard(false);
|
|
98
|
3
|
99 return guard(me, "onDeactivating").then(function () {
|
0
|
100 return me._current.deactivate(true);
|
3
|
101 }).then(function () {
|
0
|
102 // HACK raise deactivated event
|
|
103 try {
|
|
104 me.onDeactivated(me._current);
|
|
105 } catch (err) {
|
|
106 me.error(err);
|
|
107 }
|
|
108 me._current = null;
|
|
109 });
|
|
110 },
|
|
111
|
3
|
112 onActivating: function (component) {},
|
0
|
113
|
3
|
114 onDeactivating: function (component) {},
|
0
|
115
|
3
|
116 onDeactivated: function (component, next) {},
|
0
|
117
|
3
|
118 onActivated: function (component) {}
|
0
|
119 });
|
|
120 }); |