comparison src/implab/components/ActivationController.js @ 0:fc2517695ee1

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