annotate src/implab/components/_ActivatableMixin.js @ 0:fc2517695ee1

Initial commit, draft import of existing work
author cin
date Thu, 01 Jun 2017 13:20:03 +0300
parents
children 7d7059d2a810
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([ "dojo/_base/declare", "./guard", "./StateMachine", "./_LogMixin", ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 function(declare, guard, StateMachine, _LogMixin) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 var states = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 inactive : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 activate : "activating"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 activating : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 success : "active",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 failed : "inactive"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13 active : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 deactivate : "deactivating"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 deactivating : {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 success : "inactive",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18 failed : "active"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 return declare([ _LogMixin ], {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23 _controller : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 _active : null,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 constructor : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 this._active = new StateMachine({
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 states : states,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 initial : "inactive"
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 * @returns {Object} контроллер для активации текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 getController : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 return this._controller;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 * @param {Object}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 * v Контроллер для активации текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 setController : function(v) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 this._controller = v;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 * @returns {Boolean} текущая компонента активна
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 isActive : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 return this._active.current == "active";
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 assertActive : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 if (!this.isActive())
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 throw new Error("The object must be active to perform the operation");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 * Активирует текущую компоненту, если у текущей компоненты задан
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 * контроллер, то активация будет осуществляться через него
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 * @param{Boolean}
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 * direct вызов должен осуществится напрямую, без участия
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 * контроллера.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 * @return{Boolean} успешно/неуспешно
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 activate : function(direct) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 if (!direct && this._controller)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 return me._controller.activate(me).then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 me.onActivated();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 me._active.move("activate");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 return guard(me, "onActivating").then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 me.log("Activated");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 me._active.move("success");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 if (!me._controller)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 me.onActivated();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 }, function(err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 console.error(err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 me.error("Activation failed: {0}", err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 me._active.move("failed");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 throw err;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93 * Деактивирует текущую компоненту, если у компоненты задан контроллер,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 * то деактивация будет осуществляться через него.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 * @param{Boolean} direct вызов должен осуществится напрямую, без
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 * участия контроллера.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 deactivate : function(direct) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 if (!direct && me._controller)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 return me._controller.deactivate(me).then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 me.onDeactivated();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 me._active.move("deactivate");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 return guard(me, "onDeactivating").then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110 me.log("Deactivated");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 me._active.move("success");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 if (!me._controller)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113 me.onDeactivated();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 }, function(err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 console.error(err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 me.error("Deactivation failed: {0}", err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 me.move("failed");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 throw err;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 toogleActive : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 var me = this;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 return (me.isActive() ? me.deactivate() : me.activate()).then(function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 return me.isActive();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 * Событие вызывается перед активацией текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 * @returns{Boolean|undefined} если false - активация будет отменена
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 onActivating : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 * Событие вызывается перед деактивацией текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 * @returns {Boolean|undefined} если false - деактивация будет отменена
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 onDeactivating : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 * Событие вызывается после активации текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 onActivated : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 * Событие вызывается после деактивации текущей компоненты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 onDeactivated : function() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 });