root / snf-cyclades-app / synnefo / ui / static / snf / js / lib / rivets.conf.js @ 198b546d
History | View | Annotate | Download (7.7 kB)
1 |
function _resolve_keypath(obj, keypath) { |
---|---|
2 |
_obj = obj; |
3 |
_key = keypath; |
4 |
|
5 |
var map = [];
|
6 |
|
7 |
keypath = keypath.replace(/^model\./, ''); |
8 |
keypath = keypath.replace(/^item\./, ''); |
9 |
|
10 |
_.each(keypath.split("."), function(key) { |
11 |
// key = vm, name
|
12 |
var is_last = (keypath.indexOf(key) == (keypath.length-key.length))
|
13 |
if (is_last) {
|
14 |
map.push([_obj, key]); |
15 |
return;
|
16 |
} |
17 |
|
18 |
map.push([_obj, key]); |
19 |
_key = key; |
20 |
_obj = _obj.get(key); |
21 |
}); |
22 |
|
23 |
return map
|
24 |
} |
25 |
|
26 |
COLLECTION_EVENTS = ['add', 'remove', 'update', 'reset'] |
27 |
|
28 |
_.extend(rivets.formatters, { |
29 |
|
30 |
prefix: function(value, prefix) { |
31 |
return prefix + value.toString();
|
32 |
}, |
33 |
|
34 |
ip_type: function(value) { |
35 |
if (value.indexOf(":") > -1) { |
36 |
return "IPv6" |
37 |
} |
38 |
return "IPv4" |
39 |
}, |
40 |
|
41 |
truncate: function(value, size) { |
42 |
return synnefo.util.truncate(value, parseInt(size));
|
43 |
}, |
44 |
|
45 |
list_truncate: function(value, size) { |
46 |
size = size === undefined ? 38 : size; |
47 |
return synnefo.util.truncate(value, size);
|
48 |
}, |
49 |
|
50 |
collection_size: function(col) { |
51 |
return col.models.length;
|
52 |
}, |
53 |
|
54 |
collection_machines_size: function(col) { |
55 |
var items = {};
|
56 |
col.each(function(m) {
|
57 |
if (!items[m.get('device_id')]) { |
58 |
items[m.get('device_id')] = 1 |
59 |
} |
60 |
}); |
61 |
return _.keys(items).length
|
62 |
}, |
63 |
|
64 |
lower: function(value) { |
65 |
return value.toString().toLowerCase();
|
66 |
}, |
67 |
|
68 |
parenthesize: function(value) { |
69 |
return "({0})".format(value); |
70 |
}, |
71 |
|
72 |
intEq: function(value, cmp) { |
73 |
return parseInt(value) == parseInt(cmp);
|
74 |
} |
75 |
|
76 |
}); |
77 |
|
78 |
_.extend(rivets.binders, { |
79 |
'collection-view': {
|
80 |
block: true, |
81 |
bind: function(el) { |
82 |
if (this.bound) { return } |
83 |
}, |
84 |
|
85 |
unbind: function(el) { |
86 |
}, |
87 |
|
88 |
update: function(el, value) { |
89 |
}, |
90 |
|
91 |
routine: function(el, value) { |
92 |
if (!value) {
|
93 |
try {
|
94 |
if (this.keypath) { |
95 |
value = this.view.models.model.get(this.keypath); |
96 |
} else {
|
97 |
value = this.view.models.model;
|
98 |
} |
99 |
} catch (err) {
|
100 |
console.log("value error");
|
101 |
} |
102 |
} |
103 |
|
104 |
if (!value || this._bind_value != value) { |
105 |
if (this.bound) { |
106 |
this._bind_view.hide();
|
107 |
this.view.models.view.remove_view(this._bind_view); |
108 |
delete this._bind_view; |
109 |
delete this._bind_value; |
110 |
} |
111 |
this.bound = false; |
112 |
} |
113 |
|
114 |
if (!this.bound && value) { |
115 |
var specs = this.options.formatters[0].split(","); |
116 |
var cls_name = specs[0]; |
117 |
var params = specs[1]; |
118 |
if (params && this.view.models && |
119 |
this.view.models.view &&
|
120 |
this.view.models.view[params]) {
|
121 |
params = this.view.models.view[params](this, specs); |
122 |
} else {
|
123 |
if (params) { params = JSON.parse(params) }
|
124 |
} |
125 |
var view_cls = synnefo.views[cls_name];
|
126 |
var view_params = {collection: value}; |
127 |
if (params) { _.extend(view_params, params); }
|
128 |
var view = this.view.models.view.create_view(view_cls, view_params); |
129 |
this.view.models.view.add_subview(view);
|
130 |
view.show(true);
|
131 |
this._bind_view = view;
|
132 |
this._bind_value = value;
|
133 |
|
134 |
this.bound = true; |
135 |
$(el).append(view.el);
|
136 |
} |
137 |
} |
138 |
}, |
139 |
'model-view': {
|
140 |
block: true, |
141 |
bind: function(el) { |
142 |
if (this.bound) { return } |
143 |
}, |
144 |
|
145 |
unbind: function(el) { |
146 |
}, |
147 |
|
148 |
update: function(el, value) { |
149 |
}, |
150 |
|
151 |
routine: function(el, value) { |
152 |
if (!value) {
|
153 |
try {
|
154 |
if (this.keypath) { |
155 |
value = this.view.models.model.get(this.keypath); |
156 |
} else {
|
157 |
value = this.view.models.model;
|
158 |
} |
159 |
} catch (err) {
|
160 |
console.log("value error");
|
161 |
} |
162 |
} |
163 |
|
164 |
if (!value || this._bind_value != value) { |
165 |
if (this.bound) { |
166 |
this._bind_view.hide();
|
167 |
this.view.models.view.remove_view(this._bind_view); |
168 |
delete this._bind_view; |
169 |
delete this._bind_value; |
170 |
} |
171 |
this.bound = false; |
172 |
} |
173 |
|
174 |
if (!this.bound && value) { |
175 |
var specs = this.options.formatters[0].split(","); |
176 |
var cls_name = specs[0]; |
177 |
var params = specs[1]; |
178 |
var view_cls = synnefo.views[cls_name];
|
179 |
var view_params = {model: value}; |
180 |
if (params) {
|
181 |
_.extend(view_params, JSON.parse(params)); |
182 |
} |
183 |
var view = this.view.models.view.create_view(view_cls, view_params); |
184 |
this.view.models.view.add_subview(view);
|
185 |
view.show(true);
|
186 |
this._bind_view = view;
|
187 |
this._bind_value = value;
|
188 |
|
189 |
this.bound = true; |
190 |
$(el).append(view.el);
|
191 |
} |
192 |
} |
193 |
} |
194 |
}); |
195 |
|
196 |
rivets.configure({ |
197 |
prefix: 'rv', |
198 |
preloadData: true, |
199 |
|
200 |
handler: function(target, event, binding) { |
201 |
var func = binding.model[binding.keypath];
|
202 |
func.call(binding.model, binding.view.models.model, event); |
203 |
}, |
204 |
|
205 |
adapter: {
|
206 |
|
207 |
subscribe: function(root_obj, keypath, callback) { |
208 |
if (!(root_obj instanceof Backbone.Model) && |
209 |
!(root_obj instanceof Backbone.Collection)) {
|
210 |
return;
|
211 |
} |
212 |
|
213 |
var bind_map = _resolve_keypath(root_obj, keypath);
|
214 |
var last_key, last_obj;
|
215 |
last_key = bind_map[0][1]; |
216 |
last_obj = bind_map[0][0]; |
217 |
|
218 |
// TODO: Clean up :)
|
219 |
_.each(bind_map, function(data) {
|
220 |
var obj, key;
|
221 |
obj = data[0]; key = data[1]; |
222 |
|
223 |
var collection = last_obj[key] || last_obj.get && last_obj.get(key);
|
224 |
if (collection instanceof Backbone.Collection) { |
225 |
obj = collection; |
226 |
_.each(COLLECTION_EVENTS, function(e) {
|
227 |
obj.bind(e, callback); |
228 |
}); |
229 |
last_obj = obj; |
230 |
last_key = key; |
231 |
return;
|
232 |
} |
233 |
|
234 |
if (!obj) {
|
235 |
var cb = function() { |
236 |
obj.bind('change:' + key, callback);
|
237 |
} |
238 |
function reg_handler(last_obj, lkey, obj, callback) { |
239 |
var last_key = lkey;
|
240 |
var resolve_obj = function(model, value, key) { |
241 |
if (value) {
|
242 |
last_obj.unbind('change:' + key, resolve_obj);
|
243 |
delete last_obj['__pending_rivet_bind']; |
244 |
} |
245 |
var key = last_key;
|
246 |
} |
247 |
last_obj.bind('change:' + last_key, resolve_obj);
|
248 |
} |
249 |
last_obj.__pending_rivet_bind = [last_key, reg_handler]; |
250 |
reg_handler(last_obj, last_key, obj, callback); |
251 |
} else {
|
252 |
obj.bind('change:' + key, callback);
|
253 |
} |
254 |
last_key = key; |
255 |
last_obj = obj; |
256 |
}); |
257 |
}, |
258 |
|
259 |
unsubscribe: function(obj, keypath, callback) { |
260 |
if (!(obj instanceof Backbone.Model) && |
261 |
!(obj instanceof Backbone.Collection)) {
|
262 |
return;
|
263 |
} |
264 |
var bind_map = _resolve_keypath(obj, keypath);
|
265 |
_.each(bind_map, function(data) {
|
266 |
var obj, key;
|
267 |
obj = data[0]; key = data[1]; |
268 |
if (!obj) {
|
269 |
return
|
270 |
} |
271 |
if ('__pending_rivet_bind' in obj) { |
272 |
var opts = obj.__pending_rivet_bind;
|
273 |
obj.unbind('change:'+opts[0], opts[1]); |
274 |
} |
275 |
if (obj instanceof Backbone.Collection) { |
276 |
_.each(COLLECTION_EVENTS, function(e) {
|
277 |
obj.unbind(e, callback); |
278 |
}); |
279 |
} else {
|
280 |
obj.unbind('change:' + key, callback);
|
281 |
} |
282 |
}); |
283 |
}, |
284 |
|
285 |
read: function(obj, keypath) { |
286 |
if (!(obj instanceof Backbone.Model) && !(obj instanceof Backbone.Collection)) { |
287 |
return;
|
288 |
} |
289 |
var result;
|
290 |
var bind_map = _resolve_keypath(obj, keypath);
|
291 |
var last = _.last(bind_map);
|
292 |
if (!last[0]) { |
293 |
return ''; |
294 |
} |
295 |
result = last[0].get(last[1]); |
296 |
// array of models or collection ????
|
297 |
//if (result instanceof Backbone.Collection) {
|
298 |
//return result
|
299 |
//}
|
300 |
return result
|
301 |
}, |
302 |
|
303 |
publish: function(obj, keypath, value) { |
304 |
throw "Publish not available" |
305 |
}, |
306 |
|
307 |
} |
308 |
}); |