comparison core/src/js/components/ActivationController.js @ 34:27e8e9e38e07 default tip

Слияние
author nickolay
date Wed, 05 Jun 2019 20:44:15 +0300
parents acdcdf1a8d21
children
comparison
equal deleted inserted replaced
33:8af8e840dd49 34:27e8e9e38e07
1 define(["dojo/_base/declare", "../guard", "../safe", "../log/_LogMixin"], function (declare, guard, safe, _LogMixin) {
2 "use strict";
3 return declare([_LogMixin], {
4
5 _current: null,
6
7 _pending: false,
8
9 getCurrent: function () {
10 return this._current;
11 },
12
13 _start: function () {
14 if (this._pending)
15 throw new Error("The activation/decativation is already pending");
16 this._pending = true;
17 },
18
19 _await: function (d) {
20 var me = this;
21 return d.then(function (x) {
22 me._pending = false;
23 return x;
24 }, function (e) {
25 me._pending = false;
26 throw e;
27 });
28 },
29
30 activate: function (component) {
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
36 return me._await(guard(me, "_start").then(function () {
37 me._activate(component);
38 }));
39 },
40
41 _activate: function (component) {
42 var me = this;
43 if (me._current === component)
44 return guard(false);
45
46 // before activation hook
47 return guard(me, "onActivating", [component]).then(function () {
48 // deactivate curent
49 if (me._current)
50 return me._current.deactivate(true).then(function () {
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 });
66 }).then(function () {
67 return component.activate(true);
68 }).then(function () {
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 */
87 deactivate: function () {
88 var me = this;
89 return me._await(guard(me, "_start").then(function () {
90 return me._deactivate();
91 }));
92 },
93
94 _deactivate: function () {
95 var me = this;
96 if (!me._current)
97 return guard(false);
98
99 return guard(me, "onDeactivating").then(function () {
100 return me._current.deactivate(true);
101 }).then(function () {
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
112 onActivating: function (component) {},
113
114 onDeactivating: function (component) {},
115
116 onDeactivated: function (component, next) {},
117
118 onActivated: function (component) {}
119 });
120 });