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