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