-
Notifications
You must be signed in to change notification settings - Fork 6
/
viewItem.js
159 lines (123 loc) · 4.24 KB
/
viewItem.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var createSpec = require('spec-js'),
Bindable = require('./bindable'),
jsonConverter = require('./jsonConverter'),
Property = require('./property'),
merge = require('./flatMerge');
function copyProperties(source, target){
if(
!source || typeof source !== 'object' ||
!target || typeof target !== 'object'
){
return;
}
Object.keys(source).forEach(function(key){
target[key] = source[key];
});
}
function inflateViewItem(viewItem, description){
var ViewContainer = require('./viewContainer');
for(var key in viewItem){
if(viewItem[key] instanceof Property){
viewItem[key] = new viewItem[key].constructor(viewItem[key]);
}
}
/**
## .actions
All ViewItems have an actions object which can be overriden.
The actions object looks like this:
viewItem.actions = {
click: [action1, action2],
hover: [action3, action4]
}
eg:
// Some ViewItems
var someButton = new views.button(),
removeItem = new actions.remove();
// Set removeItem as a child of someButton.
someButton.actions.click = [removeItem];
If a Views action.[name] matches a DOM event name, it will be automatically _bound.
myView.actions.click = [
// actions to trigger when a 'click' event is raised by the views renderedElement
];
*/
viewItem.actions = merge(viewItem.actions);
if(description == null || typeof description !== 'object'){
return;
}
var keys = Object.keys(description);
for(var i = 0; i < keys.length; i++){
var key = keys[i],
prop = viewItem[key];
if(prop instanceof Property || prop instanceof ViewContainer){
copyProperties(description[key], prop);
}else{
viewItem[key] = description[key];
}
}
}
/**
## ViewItem
The base constructor for all gaffa ViewItems.
Views, Behaviours, and Actions inherrit from ViewItem.
*/
function ViewItem(viewItemDescription){
inflateViewItem(this, viewItemDescription);
this.on('bind', function(parent, scope){
var viewItem = this;
for(var key in this.scopeBindings){
this.scope[key] = this.gaffa.model.get(this.scopeBindings[key], this, this.scope);
}
// Only set up properties that were on the prototype.
// Faster and 'safer'
for(var propertyKey in this.constructor.prototype){
var property = this[propertyKey];
if(property instanceof Property && (property.binding || 'value' in property)){
property.bind(this, this.scope);
}
}
});
}
ViewItem = createSpec(ViewItem, Bindable);
/**
## .path
the base path for a viewItem.
Any bindings on a ViewItem will recursivly resolve through the ViewItems parent's paths.
Eg:
// Some ViewItems
var viewItem1 = new views.button(),
viewItem2 = new actions.set();
// Give viewItem1 a path.
viewItem1.path = '[things]';
// Set viewItem2 as a child of viewItem1.
viewItem1.actions.click = [viewItem2];
// Give viewItem2 a path.
viewItem2.path = '[stuff]';
// Set viewItem2s target binding.
viewItem2.target.binding = '[majigger]';
viewItem2.target.binding will resolve to:
'[/things/stuff/majigger]'
*/
ViewItem.prototype.path = '[]';
ViewItem.prototype.remove = function(){
if(this.parentContainer){
var index = this.parentContainer.indexOf(this);
if(index >= 0){
this.parentContainer.splice(index, 1);
}
this.parentContainer = null;
}
this.emit('remove');
this.debind();
this.destroy();
};
ViewItem.prototype.triggerActions = function(actionName, scope, event){
if(!this._bound){
return;
}
if(!this.actions[actionName] || !this.actions[actionName].length){
return;
}
scope = merge(this.scope, scope);
this.gaffa.actions.trigger(this.actions[actionName], this, scope, event);
};
module.exports = ViewItem;