root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_main_view.js @ 7dff818c
History | View | Annotate | Download (38 kB)
1 |
// Copyright 2011 GRNET S.A. All rights reserved.
|
---|---|
2 |
//
|
3 |
// Redistribution and use in source and binary forms, with or
|
4 |
// without modification, are permitted provided that the following
|
5 |
// conditions are met:
|
6 |
//
|
7 |
// 1. Redistributions of source code must retain the above
|
8 |
// copyright notice, this list of conditions and the following
|
9 |
// disclaimer.
|
10 |
//
|
11 |
// 2. Redistributions in binary form must reproduce the above
|
12 |
// copyright notice, this list of conditions and the following
|
13 |
// disclaimer in the documentation and/or other materials
|
14 |
// provided with the distribution.
|
15 |
//
|
16 |
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
17 |
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18 |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
19 |
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
20 |
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
21 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
23 |
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
24 |
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
25 |
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
26 |
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
27 |
// POSSIBILITY OF SUCH DAMAGE.
|
28 |
//
|
29 |
// The views and conclusions contained in the software and
|
30 |
// documentation are those of the authors and should not be
|
31 |
// interpreted as representing official policies, either expressed
|
32 |
// or implied, of GRNET S.A.
|
33 |
//
|
34 |
|
35 |
;(function(root){
|
36 |
|
37 |
// root
|
38 |
var root = root;
|
39 |
|
40 |
// setup namepsaces
|
41 |
var snf = root.synnefo = root.synnefo || {};
|
42 |
var models = snf.models = snf.models || {}
|
43 |
var storage = snf.storage = snf.storage || {};
|
44 |
var ui = snf.ui = snf.ui || {};
|
45 |
|
46 |
var views = snf.views = snf.views || {}
|
47 |
|
48 |
// shortcuts
|
49 |
var bb = root.Backbone;
|
50 |
var util = snf.util;
|
51 |
|
52 |
// generic details overlay view.
|
53 |
views.DetailsView = views.Overlay.extend({ |
54 |
view_id: "details_view", |
55 |
|
56 |
content_selector: "#details-overlay", |
57 |
css_class: 'overlay-api-info overlay-info', |
58 |
overlay_id: "overlay-details", |
59 |
|
60 |
subtitle: "", |
61 |
title: "Details", |
62 |
|
63 |
show: function(title, msg, content) { |
64 |
this.title = title;
|
65 |
this.msg = msg;
|
66 |
this.content = content;
|
67 |
views.DetailsView.__super__.show.apply(this);
|
68 |
}, |
69 |
|
70 |
beforeOpen: function() { |
71 |
this.set_title(this.title); |
72 |
if (!this.msg) { |
73 |
this.$(".description.intro").hide() |
74 |
} else {
|
75 |
this.$(".description.intro").html(this.msg).show(); |
76 |
} |
77 |
|
78 |
if (!this.content) { |
79 |
this.$(".description.subinfo").hide() |
80 |
} else {
|
81 |
this.$(".description.subinfo").html(this.content).show(); |
82 |
}; |
83 |
} |
84 |
|
85 |
}); |
86 |
|
87 |
views.SuspendedVMView = views.FeedbackView.extend({ |
88 |
view_id: "suspended_info_view", |
89 |
|
90 |
css_class: 'overlay-api-info overlay-error non-critical', |
91 |
overlay_id: "overlay-api-info", |
92 |
|
93 |
subtitle: "", |
94 |
title: "VM Suspended", |
95 |
|
96 |
beforeOpen: function() { |
97 |
views.SuspendedVMView.__super__.beforeOpen.apply(this);
|
98 |
$(this.$(".description p")[0]).html($("#suspended-vm-overlay .description").html()) |
99 |
}, |
100 |
|
101 |
show: function(vm, data, collect_data, extra_data, cb) { |
102 |
this.vm = vm;
|
103 |
data = "Suspended VM Details";
|
104 |
data += "\n====================";
|
105 |
data += "\nID: " + vm.id;
|
106 |
data += "\nName: " + vm.get('name'); |
107 |
data += "\nPublic IP: " + vm.get_public_nic().get('ipv4'); |
108 |
data += "\n\n";
|
109 |
views.SuspendedVMView.__super__.show.call(this, data, collect_data, extra_data, cb);
|
110 |
} |
111 |
|
112 |
}); |
113 |
|
114 |
views.ApiInfoView = views.Overlay.extend({ |
115 |
view_id: "api_info_view", |
116 |
|
117 |
content_selector: "#api-info-overlay", |
118 |
css_class: 'overlay-api-info overlay-info', |
119 |
overlay_id: "overlay-api-info", |
120 |
|
121 |
subtitle: "", |
122 |
title: "API Access", |
123 |
|
124 |
beforeOpen: function() { |
125 |
var cont = this.$(".copy-content p"); |
126 |
var token = snf.user.get_token();
|
127 |
|
128 |
cont.html("");
|
129 |
cont.text(token); |
130 |
|
131 |
this.cont = cont;
|
132 |
this.token = token;
|
133 |
try { delete this.clip; } catch (err) {}; |
134 |
}, |
135 |
|
136 |
onOpen: function() { |
137 |
views.ApiInfoView.__super__.onOpen(this, arguments); |
138 |
this.clip = new snf.util.ClipHelper(this.cont.parent(), this.token); |
139 |
}, |
140 |
|
141 |
onClose: function() { |
142 |
var cont = this.$(".copy-content p"); |
143 |
var token = snf.user.token;
|
144 |
cont.html("");
|
145 |
} |
146 |
}); |
147 |
|
148 |
// TODO: implement me
|
149 |
views.NoticeView = views.Overlay.extend({}); |
150 |
|
151 |
views.MultipleActionsView = views.View.extend({ |
152 |
view_id: "multiple_actions", |
153 |
|
154 |
_actions: {},
|
155 |
el: '#multiple_actions_container', |
156 |
|
157 |
initialize: function() { |
158 |
this.actions = {};
|
159 |
this.ns_config = {};
|
160 |
|
161 |
views.MultipleActionsView.__super__.initialize.call(this);
|
162 |
|
163 |
this.ns_tpl = this.$(".confirm_multiple_actions-template").clone() |
164 |
|
165 |
this.init_handlers();
|
166 |
this.update_layout();
|
167 |
|
168 |
// for heavy resize/scroll window events
|
169 |
// do it `like a boss`
|
170 |
this.fix_position = _.throttle(this.fix_position, 100); |
171 |
this.update_layout = _.throttle(this.update_layout, 100); |
172 |
this.show_limit = 1; |
173 |
|
174 |
this.init_ns("vms", { |
175 |
msg_tpl:"Your actions will affect 1 machine", |
176 |
msg_tpl_plural:"Your actions will affect {0} machines", |
177 |
actions_msg: {confirm: "Confirm all", cancel: "Cancel all"}, |
178 |
limit: 1, |
179 |
cancel_all: function() { snf.storage.vms.reset_pending_actions(); }, |
180 |
do_all: function() { snf.storage.vms.do_all_pending_actions(); } |
181 |
}); |
182 |
|
183 |
this.init_ns("nets", { |
184 |
msg_tpl:"Your actions will affect 1 private network", |
185 |
msg_tpl_plural:"Your actions will affect {0} private networks", |
186 |
actions_msg: {confirm: "Confirm all", cancel: "Cancel all"}, |
187 |
limit: 1, |
188 |
cancel_all: function() { snf.storage.networks.reset_pending_actions(); }, |
189 |
do_all: function() { snf.storage.networks.do_all_pending_actions(); } |
190 |
}); |
191 |
|
192 |
this.init_ns("reboots", { |
193 |
msg_tpl:"1 machine needs to be rebooted for changes to apply.", |
194 |
msg_tpl_plural:"{0} machines needs to be rebooted for changes to apply.", |
195 |
actions_msg: {confirm: "Reboot all", cancel: "Cancel all"}, |
196 |
limit: 0, |
197 |
cancel_all: function() { snf.storage.vms.reset_reboot_required(); }, |
198 |
do_all: function() { snf.storage.vms.do_all_reboots(); } |
199 |
}); |
200 |
}, |
201 |
|
202 |
init_ns: function(ns, params) { |
203 |
this.actions[ns] = {};
|
204 |
var nsconf = this.ns_config[ns] = params || {}; |
205 |
nsconf.cont = $(this.$("#conirm_multiple_cont_template").clone()); |
206 |
nsconf.cont.attr("id", "confirm_multiple_cont_" + ns); |
207 |
$(this.el).find(".ns-confirms-cont").append(nsconf.cont).addClass(ns); |
208 |
$(this.el).find(".ns-confirms-cont").append(nsconf.cont).addClass("confirm-ns"); |
209 |
nsconf.cont.find(".msg button.yes").text(
|
210 |
nsconf.actions_msg.confirm).click(_.bind(this.do_all, this, ns)); |
211 |
nsconf.cont.find(".msg button.no").text(
|
212 |
nsconf.actions_msg.cancel).click(_.bind(this.cancel_all, this, ns)); |
213 |
}, |
214 |
|
215 |
do_all: function(ns) { |
216 |
this.ns_config[ns].do_all();
|
217 |
}, |
218 |
|
219 |
cancel_all: function(ns) { |
220 |
this.ns_config[ns].cancel_all();
|
221 |
}, |
222 |
|
223 |
init_handlers: function() { |
224 |
var self = this; |
225 |
|
226 |
$(window).resize(_.bind(function(){ |
227 |
this.fix_position();
|
228 |
}, this));
|
229 |
|
230 |
$(window).scroll(_.bind(function(){ |
231 |
this.fix_position();
|
232 |
}, this));
|
233 |
|
234 |
storage.vms.bind("change:pending_action",
|
235 |
_.bind(this.handle_action_add, this, "vms")); |
236 |
storage.vms.bind("change:reboot_required",
|
237 |
_.bind(this.handle_action_add, this, "reboots")); |
238 |
storage.networks.bind("change:actions",
|
239 |
_.bind(this.handle_action_add, this, "nets")); |
240 |
}, |
241 |
|
242 |
handle_action_add: function(type, model, action) { |
243 |
var actions = this.actions[type]; |
244 |
|
245 |
// TODO: remove type specific addition code in its own namespace
|
246 |
if (type == "nets") { |
247 |
if (!action || action.is_empty()) {
|
248 |
delete actions[model.id];
|
249 |
} else {
|
250 |
actions[model.id] = {model: model, actions: action.actions}; |
251 |
} |
252 |
} |
253 |
|
254 |
if (type == "vms") { |
255 |
_.each(actions, function(action) {
|
256 |
if (action.model.id == model.id) {
|
257 |
delete actions[action]
|
258 |
} |
259 |
}); |
260 |
|
261 |
var actobject = {};
|
262 |
actobject[action] = [[]]; |
263 |
actions[model.id] = {model: model, actions: actobject}; |
264 |
if (typeof action == "undefined") { |
265 |
delete actions[model.id]
|
266 |
} |
267 |
} |
268 |
|
269 |
if (type == "reboots") { |
270 |
_.each(actions, function(action) {
|
271 |
if (action.model.id == model.id) {
|
272 |
delete actions[action]
|
273 |
} |
274 |
}); |
275 |
var actobject = {};
|
276 |
actobject['reboot'] = [[]];
|
277 |
actions[model.id] = {model: model, actions: actobject}; |
278 |
if (!action) {
|
279 |
delete actions[model.id]
|
280 |
} |
281 |
} |
282 |
|
283 |
this.update_layout();
|
284 |
}, |
285 |
|
286 |
update_actions_content: function(ns) { |
287 |
var conf = this.ns_config[ns]; |
288 |
conf.cont.find(".details").empty();
|
289 |
conf.cont.find(".msg p").text(""); |
290 |
|
291 |
var count = 0; |
292 |
var actionscount = 0; |
293 |
_.each(this.actions[ns], function(actions, model_id) { |
294 |
count++; |
295 |
_.each(actions.actions, function(params, act_name){
|
296 |
if (params && params.length) {
|
297 |
actionscount += params.length; |
298 |
} else {
|
299 |
actionscount++; |
300 |
} |
301 |
}) |
302 |
this.total_confirm_actions++;
|
303 |
}); |
304 |
|
305 |
var limit = conf.limit;
|
306 |
if (ui.main.current_view.view_id == "vm_list") { |
307 |
limit = 0;
|
308 |
} |
309 |
|
310 |
if (actionscount > limit) {
|
311 |
conf.cont.show(); |
312 |
this.confirm_ns_open++;
|
313 |
} else {
|
314 |
conf.cont.hide(); |
315 |
} |
316 |
|
317 |
var msg = count > 1 ? conf.msg_tpl_plural : conf.msg_tpl; |
318 |
conf.cont.find(".msg p").text(msg.format(count));
|
319 |
|
320 |
return conf.cont;
|
321 |
}, |
322 |
|
323 |
fix_position: function() { |
324 |
$('.confirm_multiple').removeClass('fixed'); |
325 |
if (($(this.el).offset().top +$(this.el).height())> ($(window).scrollTop() + $(window).height())) { |
326 |
$('.confirm_multiple').addClass('fixed'); |
327 |
} |
328 |
}, |
329 |
|
330 |
update_layout: function() { |
331 |
this.confirm_ns_open = 0; |
332 |
this.total_confirm_actions = 0; |
333 |
|
334 |
$(this.el).show(); |
335 |
$(this.el).find("#conirm_multiple_cont_template").hide(); |
336 |
$(this.el).find(".confirm-ns").show(); |
337 |
|
338 |
_.each(this.ns_config, _.bind(function(params, key) { |
339 |
this.update_actions_content(key);
|
340 |
}, this));
|
341 |
|
342 |
if (this.confirm_ns_open > 0) { |
343 |
$(this.el).show(); |
344 |
this.$(".confirm-all-cont").hide(); |
345 |
this.$(".ns-confirms-cont").show(); |
346 |
} else {
|
347 |
$(this.el).hide(); |
348 |
this.$(".confirm-all-cont").hide(); |
349 |
this.$(".ns-confirms-cont").hide(); |
350 |
} |
351 |
|
352 |
$(window).trigger("resize"); |
353 |
} |
354 |
}) |
355 |
|
356 |
// menu wrapper view
|
357 |
views.SelectView = views.View.extend({ |
358 |
|
359 |
initialize: function(view, router) { |
360 |
this.parent = view;
|
361 |
this.router = router;
|
362 |
this.pane_view_selector = $(".css-tabs"); |
363 |
this.machine_view_selector = $("#view-select"); |
364 |
this.el = $(".css-tabs"); |
365 |
this.title = $(".tab-name"); |
366 |
|
367 |
this.set_handlers();
|
368 |
this.update_layout();
|
369 |
|
370 |
views.SelectView.__super__.initialize.apply(this, arguments); |
371 |
}, |
372 |
|
373 |
clear_active: function() { |
374 |
this.pane_view_selector.find("a").removeClass("active"); |
375 |
this.machine_view_selector.find("a").removeClass("activelink"); |
376 |
}, |
377 |
|
378 |
// intercept menu links
|
379 |
set_handlers: function() { |
380 |
var self = this; |
381 |
this.pane_view_selector.find("a").hover(function(){ |
382 |
self.title.text($(this).data("hover-title")); |
383 |
}, function(){
|
384 |
self.title.text(self.parent.get_title()); |
385 |
}); |
386 |
|
387 |
this.pane_view_selector.find("a#machines_view_link").click(_.bind(function(ev){ |
388 |
ev.preventDefault(); |
389 |
this.router.vms_index();
|
390 |
}, this))
|
391 |
this.pane_view_selector.find("a#networks_view_link").click(_.bind(function(ev){ |
392 |
ev.preventDefault(); |
393 |
this.router.networks_view();
|
394 |
}, this))
|
395 |
this.pane_view_selector.find("a#ips_view_link").click(_.bind(function(ev){ |
396 |
ev.preventDefault(); |
397 |
this.router.ips_view();
|
398 |
}, this))
|
399 |
this.pane_view_selector.find("a#public_keys_view_link").click(_.bind(function(ev){ |
400 |
ev.preventDefault(); |
401 |
this.router.public_keys_view();
|
402 |
}, this))
|
403 |
|
404 |
this.machine_view_selector.find("a#machines_view_icon_link").click(_.bind(function(ev){ |
405 |
ev.preventDefault(); |
406 |
var d = $.now(); |
407 |
this.router.vms_icon_view();
|
408 |
}, this))
|
409 |
this.machine_view_selector.find("a#machines_view_list_link").click(_.bind(function(ev){ |
410 |
ev.preventDefault(); |
411 |
this.router.vms_list_view();
|
412 |
}, this))
|
413 |
this.machine_view_selector.find("a#machines_view_single_link").click(_.bind(function(ev){ |
414 |
ev.preventDefault(); |
415 |
this.router.vms_single_view();
|
416 |
}, this))
|
417 |
}, |
418 |
|
419 |
update_layout: function() { |
420 |
this.clear_active();
|
421 |
|
422 |
var pane_index = this.parent.pane_ids[this.parent.current_view_id]; |
423 |
$(this.pane_view_selector.find("a")).removeClass("active"); |
424 |
$(this.pane_view_selector.find("a").get(pane_index)).addClass("active"); |
425 |
|
426 |
if (this.parent.current_view && this.parent.current_view.vms_view) { |
427 |
|
428 |
if (storage.vms.length > 0) { |
429 |
this.machine_view_selector.show();
|
430 |
var machine_index = this.parent.views_ids[this.parent.current_view_id]; |
431 |
$(this.machine_view_selector.find("a").get(machine_index)).addClass("activelink"); |
432 |
} else {
|
433 |
this.machine_view_selector.hide();
|
434 |
} |
435 |
} else {
|
436 |
this.machine_view_selector.hide();
|
437 |
} |
438 |
|
439 |
} |
440 |
}); |
441 |
|
442 |
views.MainView = views.View.extend({ |
443 |
el: 'body', |
444 |
view_id: 'main', |
445 |
|
446 |
// FIXME: titles belong to SelectView
|
447 |
views_titles: {
|
448 |
'icon': 'machines', 'single': 'machines', |
449 |
'list': 'machines', 'networks': 'networks', |
450 |
'ips': 'ips', |
451 |
'public-keys': 'public keys' |
452 |
}, |
453 |
|
454 |
// indexes registry
|
455 |
views_indexes: {
|
456 |
0: 'icon', |
457 |
2: 'single', |
458 |
1: 'list', |
459 |
3: 'networks', |
460 |
4: 'ips', |
461 |
5: 'public-keys' |
462 |
}, |
463 |
|
464 |
views_pane_indexes: {
|
465 |
0: 'single', |
466 |
1: 'networks', |
467 |
2: 'ips', |
468 |
3: 'public-keys' |
469 |
}, |
470 |
|
471 |
// views classes registry
|
472 |
views_classes: {
|
473 |
'icon': views.IconView,
|
474 |
'single': views.SingleView,
|
475 |
'list': views.ListView,
|
476 |
'networks': views.NetworksPaneView,
|
477 |
'ips': views.IpsPaneView,
|
478 |
'public-keys': views.PublicKeysPaneView
|
479 |
}, |
480 |
|
481 |
// view ids
|
482 |
views_ids: {'icon':0, 'single':2, 'list':1, 'networks':3, 'ips':4, 'public-keys': 5}, |
483 |
|
484 |
// on which pane id each view exists
|
485 |
// machine views (icon,single,list) are all on first pane
|
486 |
pane_ids: {'icon':0, 'single':0, 'list':0, 'networks':1, 'ips':2, 'public-keys': 3}, |
487 |
|
488 |
initialize: function(show_view) { |
489 |
if (!show_view) { show_view = 'icon' }; |
490 |
|
491 |
this.router = snf.router;
|
492 |
this.empty_hidden = true; |
493 |
// fallback to browser error reporting (true for debug)
|
494 |
this.skip_errors = true |
495 |
|
496 |
// reset views
|
497 |
this.views = {};
|
498 |
|
499 |
this.el = $("#app"); |
500 |
// reset main view status
|
501 |
this._loaded = false; |
502 |
this.status = "Initializing..."; |
503 |
|
504 |
// initialize handlers
|
505 |
this.init_handlers();
|
506 |
|
507 |
// identify initial view from user cookies
|
508 |
// this view will be visible after loading of
|
509 |
// main view
|
510 |
this.initial_view = this.session_view(); |
511 |
|
512 |
views.MainView.__super__.initialize.call(this);
|
513 |
|
514 |
$(window).focus(_.bind(this.handle_window_focus, this, "focus")); |
515 |
$(window).blur(_.bind(this.handle_window_focus, this, "out")); |
516 |
|
517 |
this.focused = true; |
518 |
}, |
519 |
|
520 |
handle_window_focus: function(focus) { |
521 |
if (!snf.config.delay_on_blur) { return }; |
522 |
|
523 |
if (focus === "focus") { |
524 |
this.focused = true; |
525 |
this.set_interval_timeouts();
|
526 |
} else {
|
527 |
this.focused = false; |
528 |
this.set_interval_timeouts();
|
529 |
} |
530 |
}, |
531 |
|
532 |
set_interval_timeouts: function(time) { |
533 |
_.each(this._fetchers, _.bind(function(fetcher){ |
534 |
if (!fetcher) { return }; |
535 |
if (this.focused) { |
536 |
fetcher.interval = fetcher.normal_interval; |
537 |
fetcher.stop(false).start(true); |
538 |
} else {
|
539 |
fetcher.interval = fetcher.maximum_interval; |
540 |
fetcher.stop(false).start(false); |
541 |
} |
542 |
|
543 |
}, this));
|
544 |
}, |
545 |
|
546 |
vms_handlers_registered: false, |
547 |
|
548 |
// register event handlers
|
549 |
//
|
550 |
// vms storage events to identify if vms list
|
551 |
// is empty and display empty view if user viewing
|
552 |
// a machine view
|
553 |
//
|
554 |
// api/ui error event handlers
|
555 |
init_handlers: function() { |
556 |
// vm handlers
|
557 |
storage.vms.bind("remove", _.bind(this.check_empty, this)); |
558 |
storage.vms.bind("add", _.bind(this.check_empty, this)); |
559 |
storage.vms.bind("change:status", _.bind(this.check_empty, this)); |
560 |
storage.vms.bind("reset", _.bind(this.check_empty, this)); |
561 |
storage.quotas.bind("change", _.bind(this.update_create_buttons_status, this)); |
562 |
// additionally check quotas the first time they get fetched
|
563 |
storage.quotas.bind("add", _.bind(this.update_create_buttons_status, this)); |
564 |
|
565 |
}, |
566 |
|
567 |
handle_api_error_state: function(state) { |
568 |
if (snf.api.error_state === snf.api.STATES.ERROR) {
|
569 |
this.stop_intervals();
|
570 |
} else {
|
571 |
if (this.intervals_stopped) { |
572 |
this.update_intervals();
|
573 |
} |
574 |
} |
575 |
}, |
576 |
|
577 |
handle_api_error: function(args) { |
578 |
if (arguments.length == 1) { arguments = _.toArray(arguments[0])}; |
579 |
|
580 |
if (!_.last(arguments).display) { |
581 |
return;
|
582 |
} |
583 |
|
584 |
this.error_state = true; |
585 |
|
586 |
var xhr = arguments[0]; |
587 |
var args = util.parse_api_error.apply(util, arguments); |
588 |
|
589 |
// force logout if UNAUTHORIZED request arrives
|
590 |
if (args.code == 401) { snf.auth_client.redirect_to_login(); return }; |
591 |
|
592 |
var error_entry = [args.ns, args.code, args.message, args.type, args.details, args];
|
593 |
this.error_view.show_error.apply(this.error_view, error_entry); |
594 |
}, |
595 |
|
596 |
handle_ui_error: function(data) { |
597 |
var msg = data.msg, code = data.code, err_obj = data.error;
|
598 |
error = msg + "<br /><br />" + snf.util.stacktrace().replace("at", "<br /><br />at"); |
599 |
params = { title: "UI error", extra_details: data.extra }; |
600 |
delete data.extra.allow_close;
|
601 |
params.allow_close = data.extra.allow_close === undefined ? true : data.extra.allow_close; |
602 |
this.error_view.show_error("UI", -1, msg, "JS Exception", error, params); |
603 |
}, |
604 |
|
605 |
init_overlays: function() { |
606 |
this.create_vm_view = new views.CreateVMView(); |
607 |
this.api_info_view = new views.ApiInfoView(); |
608 |
this.details_view = new views.DetailsView(); |
609 |
this.suspended_view = new views.SuspendedVMView(); |
610 |
//this.notice_view = new views.NoticeView();
|
611 |
}, |
612 |
|
613 |
show_loading_view: function() { |
614 |
$("#container #content").hide(); |
615 |
$("#loading-view").show(); |
616 |
}, |
617 |
|
618 |
hide_loading_view: function() { |
619 |
$("#container #content").show(); |
620 |
$("#loading-view").hide(); |
621 |
$(".css-panes").show(); |
622 |
}, |
623 |
|
624 |
items_to_load: 6, |
625 |
completed_items: 0, |
626 |
check_status: function(loaded) { |
627 |
this.completed_items++;
|
628 |
// images, flavors loaded
|
629 |
if (this.completed_items == this.items_to_load) { |
630 |
this.update_status("layout", 1); |
631 |
var self = this; |
632 |
window.setTimeout(function(){
|
633 |
self.after_load(); |
634 |
}, 10)
|
635 |
} |
636 |
}, |
637 |
|
638 |
load_missing_images: function(cb) { |
639 |
synnefo.storage.vms.load_missing_images(cb); |
640 |
}, |
641 |
|
642 |
load_nets_and_vms: function() { |
643 |
var self = this; |
644 |
this.update_status("vms", 0); |
645 |
storage.vms.fetch({refresh:true, update:false, success: function(){ |
646 |
self.load_missing_images(function(){
|
647 |
self.update_status("vms", 1); |
648 |
self.update_status("layout", 0); |
649 |
self.check_status(); |
650 |
}); |
651 |
}}); |
652 |
|
653 |
this.update_status("networks", 0); |
654 |
$.when([
|
655 |
storage.networks.fetch({refresh: true}), |
656 |
storage.floating_ips.fetch({refresh: true}), |
657 |
storage.subnets.fetch({refresh: true}), |
658 |
storage.ports.fetch({refresh: true}) |
659 |
]).done(function() {
|
660 |
self.update_status("networks", 1); |
661 |
self.check_status(); |
662 |
}) |
663 |
}, |
664 |
|
665 |
_fetchers: {},
|
666 |
init_interval: function(key, collection) { |
667 |
if (this._fetchers[key]) { return } |
668 |
var fetcher_params = [snf.config.update_interval,
|
669 |
snf.config.update_interval_increase || 500,
|
670 |
snf.config.fast_interval || snf.config.update_interval/2,
|
671 |
snf.config.update_interval_increase_after_calls || 4,
|
672 |
snf.config.update_interval_max || 20000,
|
673 |
true,
|
674 |
{is_recurrent: true}]; |
675 |
var fetcher = collection.get_fetcher.apply(collection, _.clone(fetcher_params));
|
676 |
this._fetchers[key] = fetcher;
|
677 |
collection.fetch(); |
678 |
|
679 |
}, |
680 |
|
681 |
init_intervals: function() { |
682 |
_.each({ |
683 |
'networks': storage.networks,
|
684 |
'vms': storage.vms,
|
685 |
'quotas': storage.quotas,
|
686 |
'ips': storage.floating_ips,
|
687 |
'subnets': storage.subnets,
|
688 |
'ports': storage.ports,
|
689 |
'keys': storage.keys
|
690 |
}, function(col, name) {
|
691 |
this.init_interval(name, col)
|
692 |
}, this);
|
693 |
}, |
694 |
|
695 |
stop_intervals: function() { |
696 |
_.each(this._fetchers, function(fetcher) { |
697 |
fetcher.stop(); |
698 |
}); |
699 |
this.intervals_stopped = true; |
700 |
}, |
701 |
|
702 |
update_intervals: function() { |
703 |
_.each(this._fetchers, function(fetcher) { |
704 |
fetcher.stop(); |
705 |
fetcher.start(); |
706 |
}) |
707 |
this.intervals_stopped = false; |
708 |
}, |
709 |
|
710 |
after_load: function() { |
711 |
var self = this; |
712 |
this.init_intervals();
|
713 |
this.update_intervals();
|
714 |
this.update_status("layout", 0); |
715 |
|
716 |
// bypass update_hidden_views in initial view
|
717 |
// rendering to force all views to get render
|
718 |
// on their creation
|
719 |
var uhv = snf.config.update_hidden_views;
|
720 |
snf.config.update_hidden_views = true;
|
721 |
this.initialize_views();
|
722 |
snf.config.update_hidden_views = uhv; |
723 |
|
724 |
window.setTimeout(function() {
|
725 |
self.load_initialize_overlays(); |
726 |
}, 20);
|
727 |
}, |
728 |
|
729 |
load_initialize_overlays: function() { |
730 |
this.init_overlays();
|
731 |
// display initial view
|
732 |
this.loaded = true; |
733 |
|
734 |
// application start point
|
735 |
|
736 |
this.check_empty();
|
737 |
this.show_initial_view();
|
738 |
}, |
739 |
|
740 |
load: function() { |
741 |
if (synnefo.config.use_glance) {
|
742 |
synnefo.glance.register(); |
743 |
} |
744 |
this.error_view = new views.ErrorView(); |
745 |
this.vm_resize_view = new views.VmResizeView(); |
746 |
|
747 |
// api request error handling
|
748 |
synnefo.api.bind("error", _.bind(this.handle_api_error, this)); |
749 |
synnefo.api.bind("change:error_state", _.bind(this.handle_api_error_state, this)); |
750 |
synnefo.ui.bind("error", _.bind(this.handle_ui_error, this)); |
751 |
|
752 |
this.feedback_view = new views.FeedbackView(); |
753 |
|
754 |
if (synnefo.config.use_glance) {
|
755 |
this.custom_images_view = new views.CustomImagesOverlay(); |
756 |
} |
757 |
|
758 |
var self = this; |
759 |
// initialize overlay views
|
760 |
|
761 |
// display loading message
|
762 |
this.show_loading_view();
|
763 |
// sync load initial data
|
764 |
this.update_status("images", 0); |
765 |
storage.images.fetch({refresh:true, update:false, success: function(){ |
766 |
self.update_status("images", 1); |
767 |
self.check_status(); |
768 |
self.load_nets_and_vms(); |
769 |
}}); |
770 |
this.update_status("flavors", 0); |
771 |
storage.flavors.fetch({refresh:true, update:false, success:function(){ |
772 |
self.update_status("flavors", 1); |
773 |
self.check_status() |
774 |
}}); |
775 |
|
776 |
this.update_status("resources", 0); |
777 |
storage.resources.fetch({refresh:true, update:false, success: function(){ |
778 |
self.update_status("resources", 1); |
779 |
self.update_status("quotas", 0); |
780 |
self.check_status(); |
781 |
storage.quotas.fetch({refresh:true, update:true, success: function() { |
782 |
self.update_status("quotas", 1); |
783 |
self.update_status("layout", 1); |
784 |
self.check_status() |
785 |
}}) |
786 |
}}) |
787 |
}, |
788 |
|
789 |
update_status: function(ns, state) { |
790 |
var el = $("#loading-view .header."+ns); |
791 |
if (state == 0) { |
792 |
el.removeClass("off").addClass("on"); |
793 |
} |
794 |
if (state == 1) { |
795 |
el.removeClass("on").addClass("done"); |
796 |
} |
797 |
}, |
798 |
|
799 |
initialize_views: function() { |
800 |
this.select_view = new views.SelectView(this, this.router); |
801 |
this.empty_view = new views.EmptyView(); |
802 |
this.metadata_view = new views.MetadataView(); |
803 |
this.multiple_actions_view = new views.MultipleActionsView(); |
804 |
|
805 |
this.add_view("icon"); |
806 |
this.add_view("list"); |
807 |
this.add_view("single"); |
808 |
this.add_view("networks"); |
809 |
this.add_view("ips"); |
810 |
this.add_view("public-keys"); |
811 |
|
812 |
this.init_menu();
|
813 |
}, |
814 |
|
815 |
init_menu: function() { |
816 |
$(".usermenu .feedback").click(_.bind(function(e){ |
817 |
e.preventDefault(); |
818 |
this.feedback_view.show();
|
819 |
}, this));
|
820 |
}, |
821 |
|
822 |
// initial view based on user cookie
|
823 |
show_initial_view: function() { |
824 |
this.set_vm_view_handlers();
|
825 |
this.hide_loading_view();
|
826 |
bb.history.start(); |
827 |
this.trigger("ready"); |
828 |
}, |
829 |
|
830 |
show_vm_details: function(vm) { |
831 |
if (vm) {
|
832 |
this.router.vm_details_view(vm.id);
|
833 |
} |
834 |
}, |
835 |
|
836 |
update_create_buttons_status: function() { |
837 |
var nets = storage.quotas.get('cyclades.network.private'); |
838 |
var vms = storage.quotas.get('cyclades.vm'); |
839 |
|
840 |
if (!nets || !vms) { return } |
841 |
|
842 |
if (!nets.can_consume()) {
|
843 |
$("#networks-pane a.createbutton").addClass("disabled"); |
844 |
} else {
|
845 |
$("#networks-pane a.createbutton").removeClass("disabled"); |
846 |
} |
847 |
|
848 |
if (!vms.can_consume()) {
|
849 |
$("#createcontainer #create").addClass("disabled"); |
850 |
} else {
|
851 |
$("#createcontainer #create").removeClass("disabled"); |
852 |
} |
853 |
}, |
854 |
|
855 |
set_vm_view_handlers: function() { |
856 |
var self = this; |
857 |
$("#createcontainer #create").click(function(e){ |
858 |
e.preventDefault(); |
859 |
if ($(this).hasClass("disabled")) { return } |
860 |
self.router.vm_create_view(); |
861 |
}); |
862 |
}, |
863 |
|
864 |
check_empty: function() { |
865 |
if (!this.loaded) { return } |
866 |
if (storage.vms.length == 0) { |
867 |
this.show_view("machines"); |
868 |
this.router.show_welcome();
|
869 |
this.empty_hidden = false; |
870 |
} else {
|
871 |
this.hide_empty();
|
872 |
} |
873 |
}, |
874 |
|
875 |
show_empty: function() { |
876 |
if (!this.empty_hidden) { return }; |
877 |
$("#machines-pane-top").addClass("empty"); |
878 |
|
879 |
this.$(".panes").hide(); |
880 |
this.$("#machines-pane").show(); |
881 |
|
882 |
this.hide_views([]);
|
883 |
this.empty_hidden = false; |
884 |
this.empty_view.show();
|
885 |
this.select_view.update_layout();
|
886 |
this.empty_hidden = false; |
887 |
}, |
888 |
|
889 |
hide_empty: function() { |
890 |
if (this.empty_hidden) { return }; |
891 |
$("#machines-pane-top").removeClass("empty"); |
892 |
|
893 |
this.empty_view.hide(true); |
894 |
this.router.vms_index();
|
895 |
this.empty_hidden = true; |
896 |
this.select_view.update_layout();
|
897 |
}, |
898 |
|
899 |
get_title: function(view_id) { |
900 |
var view_id = view_id || this.current_view_id; |
901 |
return this.views_titles[view_id]; |
902 |
}, |
903 |
|
904 |
// return class object for the given view or false if
|
905 |
// the view is not registered
|
906 |
get_class_for_view: function (view_id) { |
907 |
if (!this.views_classes[view_id]) { |
908 |
return false; |
909 |
} |
910 |
return this.views_classes[view_id]; |
911 |
}, |
912 |
|
913 |
view: function(view_id) { |
914 |
return this.views[view_id]; |
915 |
}, |
916 |
|
917 |
add_view: function(view_id) { |
918 |
if (!this.views[view_id]) { |
919 |
var cls = this.get_class_for_view(view_id); |
920 |
if (this.skip_errors) { |
921 |
this.views[view_id] = new cls(); |
922 |
$(this.views[view_id]).bind("resize", _.bind(function() { |
923 |
window.forcePositionFooter(); |
924 |
this.multiple_actions_view.fix_position();
|
925 |
}, this));
|
926 |
} else {
|
927 |
// catch ui errors
|
928 |
try {
|
929 |
this.views[view_id] = new cls(); |
930 |
$(this.views[view_id]).bind("resize", _.bind(function() { |
931 |
window.positionFooter(); |
932 |
this.multiple_actions_view.fix_position();
|
933 |
}, this));
|
934 |
} catch (err) {snf.ui.trigger_error(-1, "Cannot add view", err)} |
935 |
} |
936 |
} else {
|
937 |
} |
938 |
|
939 |
if (this.views[view_id].vms_view) { |
940 |
this.views[view_id].metadata_view = this.metadata_view; |
941 |
} |
942 |
return this.views[view_id]; |
943 |
}, |
944 |
|
945 |
hide_views: function(skip) { |
946 |
_.each(this.views, function(view) { |
947 |
if (skip.indexOf(view) === -1) { |
948 |
view.hide(); |
949 |
} |
950 |
}, this)
|
951 |
}, |
952 |
|
953 |
get: function(view_id) { |
954 |
return this.views[view_id]; |
955 |
}, |
956 |
|
957 |
session_view: function() { |
958 |
if (this.pane_view_from_session() > 0) { |
959 |
return this.views_pane_indexes[this.pane_view_from_session()]; |
960 |
} else {
|
961 |
return this.views_indexes[this.machine_view_from_session()]; |
962 |
} |
963 |
}, |
964 |
|
965 |
pane_view_from_session: function() { |
966 |
return $.cookie("pane_view") || 0; |
967 |
}, |
968 |
|
969 |
machine_view_from_session: function() { |
970 |
return $.cookie("machine_view") || 0; |
971 |
}, |
972 |
|
973 |
update_session: function() { |
974 |
$.cookie("pane_view", this.pane_ids[this.current_view_id]); |
975 |
if (this.current_view.vms_view) { |
976 |
$.cookie("machine_view", this.views_ids[this.current_view_id]); |
977 |
} |
978 |
}, |
979 |
|
980 |
identify_view: function(view_id) { |
981 |
// machines view_id is an alias to
|
982 |
// one of the 3 machines view
|
983 |
// identify which one (if no cookie set defaults to icon)
|
984 |
if (view_id == "machines") { |
985 |
var index = this.machine_view_from_session(); |
986 |
view_id = this.views_indexes[index];
|
987 |
} |
988 |
return view_id;
|
989 |
}, |
990 |
|
991 |
// switch to current view pane
|
992 |
// if differs from the visible one
|
993 |
show_view_pane: function() { |
994 |
if (this.current_view.pane != this.current_pane) { |
995 |
$(this.current_view.pane).show(); |
996 |
$(this.current_pane).hide(); |
997 |
this.current_pane = this.current_view.pane; |
998 |
} |
999 |
}, |
1000 |
|
1001 |
show_view: function(view_id) { |
1002 |
//var d = new Date;
|
1003 |
var ret = this._show_view(view_id); |
1004 |
//console.log((new Date)-d)
|
1005 |
return ret;
|
1006 |
}, |
1007 |
|
1008 |
_show_view: function(view_id) { |
1009 |
// same view, visible
|
1010 |
// get out of here asap
|
1011 |
if (this.current_view && |
1012 |
this.current_view.id == view_id &&
|
1013 |
this.current_view.visible()) {
|
1014 |
return;
|
1015 |
} |
1016 |
|
1017 |
// choose proper view_id
|
1018 |
view_id = this.identify_view(view_id);
|
1019 |
|
1020 |
// add/create view and update current view
|
1021 |
var view = this.add_view(view_id); |
1022 |
|
1023 |
// set current view
|
1024 |
this.current_view = view;
|
1025 |
this.current_view_id = view_id;
|
1026 |
|
1027 |
// hide all other views
|
1028 |
this.hide_views([this.current_view]); |
1029 |
|
1030 |
// FIXME: depricated
|
1031 |
$(".large-spinner").remove(); |
1032 |
|
1033 |
storage.vms.reset_pending_actions(); |
1034 |
//storage.networks.reset_pending_actions();
|
1035 |
storage.vms.stop_stats_update(); |
1036 |
|
1037 |
// show current view
|
1038 |
this.show_view_pane();
|
1039 |
view.show(true);
|
1040 |
|
1041 |
// update menus
|
1042 |
if (this.select_view) { |
1043 |
this.select_view.update_layout();
|
1044 |
} |
1045 |
|
1046 |
var update_layout = this.current_view.__update_layout; |
1047 |
update_layout && update_layout.call(this.current_view);
|
1048 |
|
1049 |
// update cookies
|
1050 |
this.update_session();
|
1051 |
|
1052 |
// machines view subnav
|
1053 |
if (this.current_view.vms_view) { |
1054 |
$("#machines-pane").show(); |
1055 |
} else {
|
1056 |
$("#machines-pane").hide(); |
1057 |
} |
1058 |
|
1059 |
// fix footer position
|
1060 |
// TODO: move footer handlers in
|
1061 |
// main view (from home.html)
|
1062 |
if (window.positionFooter) {
|
1063 |
window.positionFooter(); |
1064 |
} |
1065 |
|
1066 |
// trigger view change event
|
1067 |
this.trigger("view:change", this.current_view.view_id); |
1068 |
this.select_view.title.text(this.get_title()); |
1069 |
$(window).trigger("view:change"); |
1070 |
return view;
|
1071 |
}, |
1072 |
|
1073 |
reset_vm_actions: function() { |
1074 |
|
1075 |
}, |
1076 |
|
1077 |
// identify current view
|
1078 |
// based on views element visibility
|
1079 |
current_view_id: function() { |
1080 |
var found = false; |
1081 |
_.each(this.views, function(key, value) { |
1082 |
if (value.visible()) {
|
1083 |
found = value; |
1084 |
} |
1085 |
}) |
1086 |
return found;
|
1087 |
} |
1088 |
|
1089 |
}); |
1090 |
|
1091 |
snf.ui.main = new views.MainView();
|
1092 |
|
1093 |
snf.ui.init = function() { |
1094 |
if (snf.config.handle_window_exceptions) {
|
1095 |
window.onerror = function(msg, file, line) { |
1096 |
snf.ui.trigger_error("CRITICAL", msg, {}, { file:file + ":" + line, allow_close: true }); |
1097 |
}; |
1098 |
} |
1099 |
snf.ui.main.load(); |
1100 |
} |
1101 |
|
1102 |
})(this);
|