root / ui / static / snf / js / ui / web / ui_single_view.js @ 44dd6b92
History | View | Annotate | Download (11.4 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var models = snf.models = snf.models || {}
|
9 |
var storage = snf.storage = snf.storage || {};
|
10 |
var ui = snf.ui = snf.ui || {};
|
11 |
var util = snf.util = snf.util || {};
|
12 |
|
13 |
var views = snf.views = snf.views || {}
|
14 |
|
15 |
// shortcuts
|
16 |
var bb = root.Backbone;
|
17 |
|
18 |
views.SingleDetailsView = views.VMDetailsView.extend({ |
19 |
|
20 |
view_id: "vm_details_single", |
21 |
el_sel: '.machine-details', |
22 |
|
23 |
selectors: {
|
24 |
'cpu': '.machine-detail.cpus', |
25 |
'ram': '.machine-detail.ram', |
26 |
'disk': '.machine-detail.disk', |
27 |
'image_name': '.machine-detail.image-name', |
28 |
'image_size': '.machine-detail.image-size' |
29 |
} |
30 |
|
31 |
}) |
32 |
|
33 |
// VMs single view
|
34 |
views.SingleView = views.VMListView.extend({ |
35 |
|
36 |
// view id (this could be used to identify
|
37 |
// the view object from global context
|
38 |
view_id: 'vm_single', |
39 |
|
40 |
el: '#machinesview-single', |
41 |
id_tpl: 'single-vm-', |
42 |
link_id_tpl: 'single-vm-at-', |
43 |
|
44 |
hide_actions: false, |
45 |
|
46 |
selectors: {
|
47 |
'vms': '.single-container', |
48 |
'vm': 'div#single-vm-{0}', |
49 |
'view': '#machinesview-single', |
50 |
'tpl': 'div.single-container#machine-container-template', |
51 |
'spinner': '.large-spinner', |
52 |
'vm_spinner': 'div.single-container#single-vm-{0} .state .spinner', |
53 |
'vm_wave': 'div.single-container#single-vm-{0} img.wave', |
54 |
'vm_cont_active': '#machinesview-single', |
55 |
'vm_cont_terminated': '#machinesview-single' |
56 |
}, |
57 |
|
58 |
initialize: function() { |
59 |
this.current_vm = 0; |
60 |
|
61 |
// button selectors
|
62 |
this.prev_button = this.$(".controls .previous"); |
63 |
this.next_button = this.$(".controls .next"); |
64 |
this.menu = $("#single-servers-list"); |
65 |
|
66 |
views.SingleView.__super__.initialize.apply(this, arguments); |
67 |
this.update_current_vm();
|
68 |
}, |
69 |
|
70 |
// overload show function
|
71 |
show: function() { |
72 |
views.SingleView.__super__.show.apply(this, arguments); |
73 |
this.log.debug("showing"); |
74 |
this.$(".column3").show(); |
75 |
this.show_vm_menu();
|
76 |
this.show_current();
|
77 |
}, |
78 |
|
79 |
show_vm: function(vm) { |
80 |
this.current_vm_instance = vm;
|
81 |
this.show_vm_menu();
|
82 |
this.show_current();
|
83 |
this.update_layout();
|
84 |
}, |
85 |
|
86 |
// identify vm model instance id based on DOM element
|
87 |
vm_id_for_element: function(el) { |
88 |
return el.attr('id').replace("single-vm-", ""); |
89 |
}, |
90 |
|
91 |
// set generic view handlers
|
92 |
set_handlers: function() { |
93 |
this.prev_button.click(_.bind(function(ev){ |
94 |
storage.vms.reset_pending_actions(); |
95 |
ev.preventDefault(); |
96 |
this.show_prev();
|
97 |
}, this));
|
98 |
|
99 |
this.next_button.click(_.bind(function(ev){ |
100 |
storage.vms.reset_pending_actions(); |
101 |
ev.preventDefault(); |
102 |
this.show_next();
|
103 |
}, this));
|
104 |
}, |
105 |
|
106 |
update_current_vm: function() { |
107 |
storage.vms.reset_stats_update(); |
108 |
|
109 |
try {
|
110 |
this.current_vm_instance = storage.vms.at(this.current_vm); |
111 |
this.current_vm_instance.do_update_stats = true; |
112 |
} catch (err) {
|
113 |
console.trace(); |
114 |
this.log.debug("Cannot select current vm instance for: {0}".format(this.current_vm)); |
115 |
} |
116 |
}, |
117 |
|
118 |
show_next: function() { |
119 |
this.current_vm++;
|
120 |
if (this.current_vm >= storage.vms.models.length) { |
121 |
this.current_vm = 0; |
122 |
} |
123 |
|
124 |
this.update_current_vm();
|
125 |
this.show_current();
|
126 |
this.__update_layout();
|
127 |
}, |
128 |
|
129 |
show_prev: function() { |
130 |
this.current_vm--;
|
131 |
if (this.current_vm < 0) { |
132 |
this.current_vm = storage.vms.length - 1; |
133 |
} |
134 |
this.update_current_vm();
|
135 |
this.show_current();
|
136 |
this.__update_layout();
|
137 |
}, |
138 |
|
139 |
post_remove_vm: function(vm) { |
140 |
// current vm removed or does not exist after an update
|
141 |
this.show_vm_menu();
|
142 |
if (!this.current_vm_instance || this.current_vm_instance.id == vm.id) { |
143 |
this.show_next();
|
144 |
} else {
|
145 |
this.show_current();
|
146 |
} |
147 |
}, |
148 |
|
149 |
// stuff to do when a new vm has been created.
|
150 |
// - create vm subviews
|
151 |
post_add: function(vm) { |
152 |
this.show_vm_menu();
|
153 |
this.show_current();
|
154 |
|
155 |
// rename views index
|
156 |
this.stats_views = this.stats_views || {}; |
157 |
this.connect_views = this.connect_views || {}; |
158 |
this.tags_views = this.tags_views || {}; |
159 |
this.details_views = this.details_views || {}; |
160 |
this.action_views = this.action_views || {}; |
161 |
this.action_error_views = this.action_error_views || {}; |
162 |
|
163 |
//this.stats_views[vm.id] = new views.IconStatsView(vm, this);
|
164 |
|
165 |
// same as icon view
|
166 |
this.action_views[vm.id] = new views.VMActionsView(vm, this, this.vm(vm), this.hide_actions); |
167 |
this.stats_views[vm.id] = new views.VMStatsView(vm, this, {stats_type: 'series'}); |
168 |
this.connect_views[vm.id] = new views.IconVMConnectView(vm, this); |
169 |
this.tags_views[vm.id] = new views.VMTagsView(vm, this, true, 20, 10, 35); |
170 |
this.details_views[vm.id] = new views.SingleDetailsView(vm, this); |
171 |
this.action_error_views[vm.id] = new views.VMActionErrorView(vm, this); |
172 |
|
173 |
if (storage.vms.models.length > 1) { this.vm(vm).hide(); }; |
174 |
}, |
175 |
|
176 |
post_update_vm: function(vm) { |
177 |
}, |
178 |
|
179 |
// vm specific event handlers
|
180 |
set_vm_handlers: function(vm) { |
181 |
var el = this.vm(vm); |
182 |
}, |
183 |
|
184 |
// handle selected vm
|
185 |
show_current: function() { |
186 |
var index = this.current_vm; |
187 |
var vm = storage.vms.at(index);
|
188 |
|
189 |
this.$(".server-name").removeClass("column3-selected"); |
190 |
|
191 |
if (vm) {
|
192 |
this.vm(vm).show();
|
193 |
}; |
194 |
|
195 |
_.each(storage.vms.models, function(vmo){
|
196 |
if (vm && (vm.id != vmo.id)) {
|
197 |
this.vm(vmo).hide();
|
198 |
} |
199 |
}, this)
|
200 |
|
201 |
if (!vm) {
|
202 |
// empty list
|
203 |
this.$(".column3").hide(); |
204 |
return;
|
205 |
} |
206 |
this.$(".column3").show(); |
207 |
|
208 |
|
209 |
$("#" + this.link_id_tpl + this.current_vm).addClass("column3-selected"); |
210 |
}, |
211 |
|
212 |
show_vm_menu: function() { |
213 |
this.menu.find(".server-name").remove(); |
214 |
|
215 |
_.each(storage.vms.models, function(vm, index) {
|
216 |
this.menu.append('<div class="server-name" id="'+this.link_id_tpl + index +'">' + |
217 |
util.truncate(vm.get("name"),16)+'</div>'); |
218 |
if (this.current_vm_instance && vm.id == this.current_vm_instance.id) { |
219 |
this.current_vm = index;
|
220 |
} |
221 |
}, this);
|
222 |
|
223 |
var self = this; |
224 |
this.menu.find(".server-name").click(function(ev) { |
225 |
ev.preventDefault(); |
226 |
var id = $(this).attr("id").replace("single-vm-at-", ""); |
227 |
self.current_vm = id; |
228 |
self.update_current_vm(); |
229 |
self.show_current(); |
230 |
}) |
231 |
}, |
232 |
|
233 |
// generic stuff to do on each view update
|
234 |
// called once after each vm has been updated
|
235 |
update_layout: function() { |
236 |
this.update_current_vm();
|
237 |
fix_v6_addresses(); |
238 |
}, |
239 |
|
240 |
// update vm details
|
241 |
update_details: function(vm) { |
242 |
var el = this.vm(vm); |
243 |
// truncate name
|
244 |
el.find(".machine-detail.name").text(util.truncate(vm.get("name"), 35)); |
245 |
// set ips
|
246 |
el.find(".machine-detail.ipv4.ipv4-text").text(vm.get_addresses().ip4 || "undefined"); |
247 |
// TODO: fix ipv6 truncates and tooltip handler
|
248 |
el.find(".machine-detail.ipv6.ipv6-text").text(vm.get_addresses().ip6 || "undefined"); |
249 |
// set the state (i18n ??)
|
250 |
el.find(".state-label").text(STATE_TEXTS[vm.state()]);
|
251 |
// set state class
|
252 |
el.find(".state").removeClass().addClass(views.SingleView.STATE_CLASSES[vm.state()].join(" ")); |
253 |
// os icon
|
254 |
el.find(".single-image").css({'background-image': "url(" + this.get_vm_icon_path(vm, "medium") + ")"}); |
255 |
|
256 |
el.removeClass("connectable");
|
257 |
if (vm.is_connectable()) {
|
258 |
el.addClass("connectable");
|
259 |
} |
260 |
|
261 |
if (vm.get('status') == 'BUILD') { |
262 |
// update bulding progress
|
263 |
el.find("span.build-progress").show().text(vm.get("progress_message")); |
264 |
} else {
|
265 |
// hide building progress
|
266 |
el.find("span.build-progress").hide();
|
267 |
} |
268 |
|
269 |
if (vm.state() == "DESTROY") { |
270 |
el.find("span.build-progress").show().text("Terminating..."); |
271 |
} |
272 |
|
273 |
icon_state = vm.is_active() ? "on" : "off"; |
274 |
set_machine_os_image(el, "single", icon_state, this.get_vm_icon_os(vm)); |
275 |
|
276 |
// update subviews
|
277 |
this.action_views[vm.id].update_layout();
|
278 |
this.stats_views[vm.id].update_layout();
|
279 |
this.connect_views[vm.id].update_layout();
|
280 |
this.tags_views[vm.id].update_layout();
|
281 |
this.details_views[vm.id].update_layout();
|
282 |
}, |
283 |
|
284 |
get_vm_icon_os: function(vm) { |
285 |
var os = vm.get_os();
|
286 |
var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
|
287 |
if (icons.indexOf(os) == -1) { |
288 |
os = "unknown";
|
289 |
} |
290 |
return os;
|
291 |
}, |
292 |
|
293 |
// TODO: move to views.utils (the method and the VM_OS_ICON vars)
|
294 |
get_vm_icon_path: function(vm, icon_type) { |
295 |
var os = vm.get_os();
|
296 |
var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
|
297 |
|
298 |
if (icons.indexOf(os) == -1) { |
299 |
os = "unknown";
|
300 |
} |
301 |
|
302 |
return views.SingleView.VM_OS_ICON_TPLS[icon_type].format(os);
|
303 |
} |
304 |
}) |
305 |
|
306 |
views.SingleView.VM_OS_ICON_TPLS = { |
307 |
"medium": "/static/icons/machines/large/{0}-sprite.png" |
308 |
} |
309 |
|
310 |
views.SingleView.VM_OS_ICONS = window.os_icons || []; |
311 |
|
312 |
views.SingleView.STATE_CLASSES = { |
313 |
'UNKNOWN': ['state', 'error-state'], |
314 |
'BUILD': ['state', 'build-state'], |
315 |
'REBOOT': ['state', 'rebooting-state'], |
316 |
'STOPPED': ['state', 'terminated-state'], |
317 |
'ACTIVE': ['state', 'running-state'], |
318 |
'ERROR': ['state', 'error-state'], |
319 |
'DELETE': ['state', 'destroying-state'], |
320 |
'DESTROY': ['state', 'destroying-state'], |
321 |
'BUILD_INIT': ['state', 'build-state'], |
322 |
'BUILD_COPY': ['state', 'build-state'], |
323 |
'BUILD_FINAL': ['state', 'build-state'], |
324 |
'SHUTDOWN': ['state', 'shutting-state'], |
325 |
'START': ['state', 'starting-state'], |
326 |
'CONNECT': ['state', 'connecting-state'], |
327 |
'DISCONNECT': ['state', 'disconnecting-state'] |
328 |
}; |
329 |
|
330 |
})(this);
|