-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathaction.js
89 lines (73 loc) · 2.19 KB
/
action.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var createSpec = require('spec-js'),
Property = require('./property'),
statham = require('statham'),
ViewItem = require('./viewItem');
function triggerAction(action, parent, scope, event) {
// clone
if(action instanceof ViewItem){
action = action._clone();
}
action = parent.gaffa.initialiseAction(action);
action.on('bind', function(parent, scope){
action.path = action.getPath();
scope || (scope = {});
if(action.condition.value){
action.trigger(parent, scope, event);
}
if(!action._async){
action.complete();
}
});
action.bind(parent, scope);
}
function triggerActions(actions, parent, scope, event) {
if(Array.isArray(actions)){
for(var i = 0; i < actions.length; i++) {
triggerAction(actions[i], parent, scope, event);
}
}else if(actions instanceof Action){
triggerAction(actions, parent, scope, event);
}
}
function Action(actionDescription){
}
Action = createSpec(Action, ViewItem);
Action.trigger = triggerActions;
Action.prototype.trigger = function(){
throw 'Nothing is implemented for this action (' + this.constructor.name + ')';
};
Action.prototype.condition = new Property({
value: true
});
// Because actions shouldn't neccisarily debind untill they are complete,
// They have an odd debind impementation.
Action.prototype.debind = function(){
if(!this._complete && !this._destroyed){
return;
}
this.complete();
};
Action.prototype.complete = function(){
if(this._complete){
return;
}
this._complete = true;
var action = this;
this.on('debind', function(){
action.destroy();
});
this.emit('complete');
ViewItem.prototype.debind.call(this);
};
// Only actually destroy if either the actions parent was not an Action,
// Or if its Action parent was explicitly destroyed.
Action.prototype.destroy = function(){
if(!this._complete && (this.parent instanceof Action ? this.parent._canceled : true)){
this._canceled = true;
}
if(!this._complete && !this._canceled){
return;
}
ViewItem.prototype.destroy.call(this);
};
module.exports = Action;