root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_main_view.js @ 336ddd59
History | View | Annotate | Download (38.3 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", _.bind(this.handle_action_add, this, "vms")); |
235 |
storage.vms.bind("change:reboot_required", _.bind(this.handle_action_add, this, "reboots")); |
236 |
storage.networks.bind("change:actions", _.bind(this.handle_action_add, this, "nets")); |
237 |
}, |
238 |
|
239 |
handle_action_add: function(type, model, action) { |
240 |
var actions = this.actions[type]; |
241 |
|
242 |
// TODO: remove type specific addition code in its own namespace
|
243 |
if (type == "nets") { |
244 |
if (!action || action.is_empty()) {
|
245 |
delete actions[model.id];
|
246 |
} else {
|
247 |
actions[model.id] = {model: model, actions: action.actions}; |
248 |
} |
249 |
} |
250 |
|
251 |
if (type == "vms") { |
252 |
_.each(actions, function(action) {
|
253 |
if (action.model.id == model.id) {
|
254 |
delete actions[action]
|
255 |
} |
256 |
}); |
257 |
|
258 |
var actobject = {};
|
259 |
actobject[action] = [[]]; |
260 |
actions[model.id] = {model: model, actions: actobject}; |
261 |
if (typeof action == "undefined") { |
262 |
delete actions[model.id]
|
263 |
} |
264 |
} |
265 |
|
266 |
if (type == "reboots") { |
267 |
_.each(actions, function(action) {
|
268 |
if (action.model.id == model.id) {
|
269 |
delete actions[action]
|
270 |
} |
271 |
}); |
272 |
var actobject = {};
|
273 |
actobject['reboot'] = [[]];
|
274 |
actions[model.id] = {model: model, actions: actobject}; |
275 |
if (!action) {
|
276 |
delete actions[model.id]
|
277 |
} |
278 |
} |
279 |
|
280 |
this.update_layout();
|
281 |
}, |
282 |
|
283 |
update_actions_content: function(ns) { |
284 |
var conf = this.ns_config[ns]; |
285 |
conf.cont.find(".details").empty();
|
286 |
conf.cont.find(".msg p").text(""); |
287 |
|
288 |
var count = 0; |
289 |
var actionscount = 0; |
290 |
_.each(this.actions[ns], function(actions, model_id) { |
291 |
count++; |
292 |
_.each(actions.actions, function(params, act_name){
|
293 |
if (params && params.length) {
|
294 |
actionscount += params.length; |
295 |
} else {
|
296 |
actionscount++; |
297 |
} |
298 |
}) |
299 |
this.total_confirm_actions++;
|
300 |
}); |
301 |
|
302 |
var limit = conf.limit;
|
303 |
if (ui.main.current_view.view_id == "vm_list") { |
304 |
limit = 0;
|
305 |
} |
306 |
|
307 |
if (actionscount > limit) {
|
308 |
conf.cont.show(); |
309 |
this.confirm_ns_open++;
|
310 |
} else {
|
311 |
conf.cont.hide(); |
312 |
} |
313 |
|
314 |
var msg = count > 1 ? conf.msg_tpl_plural : conf.msg_tpl; |
315 |
conf.cont.find(".msg p").text(msg.format(count));
|
316 |
|
317 |
return conf.cont;
|
318 |
}, |
319 |
|
320 |
fix_position: function() { |
321 |
$('.confirm_multiple').removeClass('fixed'); |
322 |
if (($(this.el).offset().top +$(this.el).height())> ($(window).scrollTop() + $(window).height())) { |
323 |
$('.confirm_multiple').addClass('fixed'); |
324 |
} |
325 |
}, |
326 |
|
327 |
update_layout: function() { |
328 |
this.confirm_ns_open = 0; |
329 |
this.total_confirm_actions = 0; |
330 |
|
331 |
$(this.el).show(); |
332 |
$(this.el).find("#conirm_multiple_cont_template").hide(); |
333 |
$(this.el).find(".confirm-ns").show(); |
334 |
|
335 |
_.each(this.ns_config, _.bind(function(params, key) { |
336 |
this.update_actions_content(key);
|
337 |
}, this));
|
338 |
|
339 |
if (this.confirm_ns_open > 0) { |
340 |
$(this.el).show(); |
341 |
this.$(".confirm-all-cont").hide(); |
342 |
this.$(".ns-confirms-cont").show(); |
343 |
} else {
|
344 |
$(this.el).hide(); |
345 |
this.$(".confirm-all-cont").hide(); |
346 |
this.$(".ns-confirms-cont").hide(); |
347 |
} |
348 |
|
349 |
$(window).trigger("resize"); |
350 |
} |
351 |
}) |
352 |
|
353 |
// menu wrapper view
|
354 |
views.SelectView = views.View.extend({ |
355 |
|
356 |
initialize: function(view, router) { |
357 |
this.parent = view;
|
358 |
this.router = router;
|
359 |
this.pane_view_selector = $(".css-tabs"); |
360 |
this.machine_view_selector = $("#view-select"); |
361 |
this.el = $(".css-tabs"); |
362 |
this.title = $(".tab-name"); |
363 |
|
364 |
this.set_handlers();
|
365 |
this.update_layout();
|
366 |
|
367 |
views.SelectView.__super__.initialize.apply(this, arguments); |
368 |
}, |
369 |
|
370 |
clear_active: function() { |
371 |
this.pane_view_selector.find("a").removeClass("active"); |
372 |
this.machine_view_selector.find("a").removeClass("activelink"); |
373 |
}, |
374 |
|
375 |
// intercept menu links
|
376 |
set_handlers: function() { |
377 |
var self = this; |
378 |
this.pane_view_selector.find("a").hover(function(){ |
379 |
// FIXME: title from href ? omg
|
380 |
self.title.text($(this).attr("href")); |
381 |
}, function(){
|
382 |
self.title.text(self.parent.get_title()); |
383 |
}); |
384 |
|
385 |
this.pane_view_selector.find("a#machines_view_link").click(_.bind(function(ev){ |
386 |
ev.preventDefault(); |
387 |
this.router.vms_index();
|
388 |
}, this))
|
389 |
this.pane_view_selector.find("a#networks_view_link").click(_.bind(function(ev){ |
390 |
ev.preventDefault(); |
391 |
this.router.networks_view();
|
392 |
}, this))
|
393 |
this.pane_view_selector.find("a#disks_view_link").click(_.bind(function(ev){ |
394 |
ev.preventDefault(); |
395 |
this.router.disks_view();
|
396 |
}, this))
|
397 |
|
398 |
this.machine_view_selector.find("a#machines_view_icon_link").click(_.bind(function(ev){ |
399 |
ev.preventDefault(); |
400 |
var d = $.now(); |
401 |
this.router.vms_icon_view();
|
402 |
}, this))
|
403 |
this.machine_view_selector.find("a#machines_view_list_link").click(_.bind(function(ev){ |
404 |
ev.preventDefault(); |
405 |
this.router.vms_list_view();
|
406 |
}, this))
|
407 |
this.machine_view_selector.find("a#machines_view_single_link").click(_.bind(function(ev){ |
408 |
ev.preventDefault(); |
409 |
this.router.vms_single_view();
|
410 |
}, this))
|
411 |
}, |
412 |
|
413 |
update_layout: function() { |
414 |
this.clear_active();
|
415 |
|
416 |
var pane_index = this.parent.pane_ids[this.parent.current_view_id]; |
417 |
$(this.pane_view_selector.find("a")).removeClass("active"); |
418 |
$(this.pane_view_selector.find("a").get(pane_index)).addClass("active"); |
419 |
|
420 |
if (this.parent.current_view && this.parent.current_view.vms_view) { |
421 |
|
422 |
if (storage.vms.length > 0) { |
423 |
this.machine_view_selector.show();
|
424 |
var machine_index = this.parent.views_ids[this.parent.current_view_id]; |
425 |
$(this.machine_view_selector.find("a").get(machine_index)).addClass("activelink"); |
426 |
} else {
|
427 |
this.machine_view_selector.hide();
|
428 |
} |
429 |
} else {
|
430 |
this.machine_view_selector.hide();
|
431 |
} |
432 |
|
433 |
} |
434 |
}); |
435 |
|
436 |
views.MainView = views.View.extend({ |
437 |
el: 'body', |
438 |
view_id: 'main', |
439 |
|
440 |
// FIXME: titles belong to SelectView
|
441 |
views_titles: {
|
442 |
'icon': 'machines', 'single': 'machines', |
443 |
'list': 'machines', 'networks': 'networks', |
444 |
'disks': 'disks' |
445 |
}, |
446 |
|
447 |
// indexes registry
|
448 |
views_indexes: {0: 'icon', 2:'single', 1: 'list', 3:'networks'}, |
449 |
views_pane_indexes: {0:'single', 1:'networks', 2:'disks'}, |
450 |
|
451 |
// views classes registry
|
452 |
views_classes: {'icon': views.IconView, 'single': views.SingleView, |
453 |
'list': views.ListView, 'networks': views.NetworksView, 'disks': views.DisksView}, |
454 |
|
455 |
// view ids
|
456 |
views_ids: {'icon':0, 'single':2, 'list':1, 'networks':3, 'disks':4}, |
457 |
|
458 |
// on which pane id each view exists
|
459 |
// machine views (icon,single,list) are all on first pane
|
460 |
pane_ids: {'icon':0, 'single':0, 'list':0, 'networks':1, 'disks':2}, |
461 |
|
462 |
initialize: function(show_view) { |
463 |
if (!show_view) { show_view = 'icon' }; |
464 |
|
465 |
this.router = snf.router;
|
466 |
this.empty_hidden = true; |
467 |
// fallback to browser error reporting (true for debug)
|
468 |
this.skip_errors = true |
469 |
|
470 |
// reset views
|
471 |
this.views = {};
|
472 |
|
473 |
this.el = $("#app"); |
474 |
// reset main view status
|
475 |
this._loaded = false; |
476 |
this.status = "Initializing..."; |
477 |
|
478 |
// initialize handlers
|
479 |
this.init_handlers();
|
480 |
|
481 |
// identify initial view from user cookies
|
482 |
// this view will be visible after loading of
|
483 |
// main view
|
484 |
this.initial_view = this.session_view(); |
485 |
|
486 |
views.MainView.__super__.initialize.call(this);
|
487 |
|
488 |
$(window).focus(_.bind(this.handle_window_focus, this, "focus")); |
489 |
$(window).blur(_.bind(this.handle_window_focus, this, "out")); |
490 |
|
491 |
this.focused = true; |
492 |
}, |
493 |
|
494 |
handle_window_focus: function(focus) { |
495 |
if (!snf.config.delay_on_blur) { return }; |
496 |
|
497 |
if (focus === "focus") { |
498 |
this.focused = true; |
499 |
this.set_interval_timeouts();
|
500 |
} else {
|
501 |
this.focused = false; |
502 |
this.set_interval_timeouts();
|
503 |
} |
504 |
}, |
505 |
|
506 |
set_interval_timeouts: function(time) { |
507 |
_.each([this._networks, this._vms], _.bind(function(fetcher){ |
508 |
if (!fetcher) { return }; |
509 |
if (this.focused) { |
510 |
fetcher.interval = fetcher.normal_interval; |
511 |
fetcher.stop(false).start(true); |
512 |
} else {
|
513 |
fetcher.interval = fetcher.maximum_interval; |
514 |
fetcher.stop(false).start(false); |
515 |
} |
516 |
|
517 |
}, this));
|
518 |
}, |
519 |
|
520 |
vms_handlers_registered: false, |
521 |
|
522 |
// register event handlers
|
523 |
//
|
524 |
// vms storage events to identify if vms list
|
525 |
// is empty and display empty view if user viewing
|
526 |
// a machine view
|
527 |
//
|
528 |
// api/ui error event handlers
|
529 |
init_handlers: function() { |
530 |
// vm handlers
|
531 |
storage.vms.bind("remove", _.bind(this.check_empty, this)); |
532 |
storage.vms.bind("add", _.bind(this.check_empty, this)); |
533 |
storage.vms.bind("change:status", _.bind(this.check_empty, this)); |
534 |
storage.vms.bind("reset", _.bind(this.check_empty, this)); |
535 |
|
536 |
}, |
537 |
|
538 |
handle_api_error_state: function(state) { |
539 |
if (snf.api.error_state === snf.api.STATES.ERROR) {
|
540 |
this.stop_intervals();
|
541 |
} else {
|
542 |
if (this.intervals_stopped) { |
543 |
this.update_intervals();
|
544 |
} |
545 |
} |
546 |
}, |
547 |
|
548 |
handle_api_error: function(args) { |
549 |
if (arguments.length == 1) { arguments = _.toArray(arguments[0])}; |
550 |
|
551 |
if (!_.last(arguments).display) { |
552 |
return;
|
553 |
} |
554 |
|
555 |
this.error_state = true; |
556 |
|
557 |
var xhr = arguments[0]; |
558 |
var args = util.parse_api_error.apply(util, arguments); |
559 |
|
560 |
// force logout if UNAUTHORIZED request arrives
|
561 |
if (args.code == 401) { snf.auth_client.redirect_to_login(); return }; |
562 |
|
563 |
var error_entry = [args.ns, args.code, args.message, args.type, args.details, args];
|
564 |
this.error_view.show_error.apply(this.error_view, error_entry); |
565 |
}, |
566 |
|
567 |
handle_ui_error: function(data) { |
568 |
var msg = data.msg, code = data.code, err_obj = data.error;
|
569 |
error = msg + "<br /><br />" + snf.util.stacktrace().replace("at", "<br /><br />at"); |
570 |
params = { title: "UI error", extra_details: data.extra }; |
571 |
delete data.extra.allow_close;
|
572 |
params.allow_close = data.extra.allow_close === undefined ? true : data.extra.allow_close; |
573 |
this.error_view.show_error("UI", -1, msg, "JS Exception", error, params); |
574 |
}, |
575 |
|
576 |
init_overlays: function() { |
577 |
this.create_vm_view = new views.CreateVMView(); |
578 |
this.api_info_view = new views.ApiInfoView(); |
579 |
this.details_view = new views.DetailsView(); |
580 |
this.suspended_view = new views.SuspendedVMView(); |
581 |
//this.notice_view = new views.NoticeView();
|
582 |
}, |
583 |
|
584 |
show_loading_view: function() { |
585 |
$("#container #content").hide(); |
586 |
$("#loading-view").show(); |
587 |
}, |
588 |
|
589 |
hide_loading_view: function() { |
590 |
$("#container #content").show(); |
591 |
$("#loading-view").hide(); |
592 |
$(".css-panes").show(); |
593 |
}, |
594 |
|
595 |
items_to_load: 4, |
596 |
completed_items: 0, |
597 |
check_status: function(loaded) { |
598 |
this.completed_items++;
|
599 |
// images, flavors loaded
|
600 |
if (this.completed_items == 2) { |
601 |
this.load_nets_and_vms();
|
602 |
} |
603 |
if (this.completed_items == this.items_to_load) { |
604 |
this.update_status("layout", 1); |
605 |
var self = this; |
606 |
window.setTimeout(function(){
|
607 |
self.after_load(); |
608 |
}, 10)
|
609 |
} |
610 |
}, |
611 |
|
612 |
load_missing_images: function(cb) { |
613 |
synnefo.storage.vms.load_missing_images(cb); |
614 |
}, |
615 |
|
616 |
load_nets_and_vms: function() { |
617 |
var self = this; |
618 |
this.update_status("vms", 0); |
619 |
storage.vms.fetch({refresh:true, update:false, success: function(){ |
620 |
self.load_missing_images(function(){
|
621 |
self.update_status("vms", 1); |
622 |
self.update_status("layout", 0); |
623 |
self.check_status(); |
624 |
}); |
625 |
}}); |
626 |
|
627 |
this.update_status("networks", 0); |
628 |
storage.networks.fetch({refresh:true, update:false, success: function(){ |
629 |
self.update_status("networks", 1); |
630 |
self.check_status(); |
631 |
}}); |
632 |
}, |
633 |
|
634 |
init_intervals: function() { |
635 |
var fetcher_params = [snf.config.update_interval,
|
636 |
snf.config.update_interval_increase || 500,
|
637 |
snf.config.fast_interval || snf.config.update_interval/2,
|
638 |
snf.config.update_interval_increase_after_calls || 4,
|
639 |
snf.config.update_interval_max || 20000,
|
640 |
true,
|
641 |
{is_recurrent: true}] |
642 |
|
643 |
this._networks = storage.networks.get_fetcher.apply(storage.networks, _.clone(fetcher_params));
|
644 |
this._vms = storage.vms.get_fetcher.apply(storage.vms, _.clone(fetcher_params));
|
645 |
}, |
646 |
|
647 |
stop_intervals: function() { |
648 |
if (this._networks) { this._networks.stop(); } |
649 |
if (this._vms) { this._vms.stop(); } |
650 |
this.intervals_stopped = true; |
651 |
}, |
652 |
|
653 |
update_intervals: function() { |
654 |
if (this._networks) { |
655 |
this._networks.stop();
|
656 |
this._networks.start();
|
657 |
} else {
|
658 |
this.init_intervals();
|
659 |
} |
660 |
|
661 |
if (this._vms) { |
662 |
this._vms.stop();
|
663 |
this._vms.start();
|
664 |
} else {
|
665 |
this.init_intervals();
|
666 |
} |
667 |
|
668 |
this.intervals_stopped = false; |
669 |
}, |
670 |
|
671 |
after_load: function() { |
672 |
var self = this; |
673 |
this.init_intervals();
|
674 |
this.update_intervals();
|
675 |
this.update_status("layout", 0); |
676 |
|
677 |
// bypass update_hidden_views in initial view
|
678 |
// rendering to force all views to get render
|
679 |
// on their creation
|
680 |
var uhv = snf.config.update_hidden_views;
|
681 |
snf.config.update_hidden_views = true;
|
682 |
this.initialize_views();
|
683 |
snf.config.update_hidden_views = uhv; |
684 |
|
685 |
window.setTimeout(function() {
|
686 |
self.update_status("layout", 0); |
687 |
self.load_initialize_overlays(); |
688 |
}, 20);
|
689 |
}, |
690 |
|
691 |
load_initialize_overlays: function() { |
692 |
this.init_overlays();
|
693 |
// display initial view
|
694 |
this.loaded = true; |
695 |
|
696 |
// application start point
|
697 |
|
698 |
this.check_empty();
|
699 |
this.show_initial_view();
|
700 |
}, |
701 |
|
702 |
load: function() { |
703 |
if (synnefo.config.use_glance) {
|
704 |
synnefo.glance.register(); |
705 |
} |
706 |
this.error_view = new views.ErrorView(); |
707 |
// api request error handling
|
708 |
synnefo.api.bind("error", _.bind(this.handle_api_error, this)); |
709 |
synnefo.api.bind("change:error_state", _.bind(this.handle_api_error_state, this)); |
710 |
synnefo.ui.bind("error", _.bind(this.handle_ui_error, this)); |
711 |
|
712 |
this.feedback_view = new views.FeedbackView(); |
713 |
this.public_keys_view = new views.PublicKeysOverlay(); |
714 |
|
715 |
if (synnefo.config.use_glance) {
|
716 |
this.custom_images_view = new views.CustomImagesOverlay(); |
717 |
} |
718 |
|
719 |
var self = this; |
720 |
// initialize overlay views
|
721 |
|
722 |
// display loading message
|
723 |
this.show_loading_view();
|
724 |
// sync load initial data
|
725 |
this.update_status("images", 0); |
726 |
storage.images.fetch({refresh:true, update:false, success: function(){ |
727 |
self.update_status("images", 1); |
728 |
self.check_status() |
729 |
}}); |
730 |
this.update_status("flavors", 0); |
731 |
storage.flavors.fetch({refresh:true, update:false, success:function(){ |
732 |
self.update_status("flavors", 1); |
733 |
self.check_status() |
734 |
}}); |
735 |
}, |
736 |
|
737 |
update_status: function(ns, state) { |
738 |
var el = $("#loading-view .header."+ns); |
739 |
if (state == 0) { |
740 |
el.removeClass("off").addClass("on"); |
741 |
} |
742 |
if (state == 1) { |
743 |
el.removeClass("on").addClass("done"); |
744 |
} |
745 |
}, |
746 |
|
747 |
initialize_views: function() { |
748 |
this.select_view = new views.SelectView(this, this.router); |
749 |
this.empty_view = new views.EmptyView(); |
750 |
this.metadata_view = new views.MetadataView(); |
751 |
this.multiple_actions_view = new views.MultipleActionsView(); |
752 |
|
753 |
this.add_view("icon"); |
754 |
this.add_view("list"); |
755 |
this.add_view("single"); |
756 |
this.add_view("networks"); |
757 |
this.add_view("disks"); |
758 |
|
759 |
this.init_menu();
|
760 |
}, |
761 |
|
762 |
init_menu: function() { |
763 |
$(".usermenu .feedback").click(_.bind(function(e){ |
764 |
e.preventDefault(); |
765 |
this.feedback_view.show();
|
766 |
}, this));
|
767 |
$(".usermenu .public_keys").click(_.bind(function(e){ |
768 |
e.preventDefault(); |
769 |
this.public_keys_view.show();
|
770 |
}, this));
|
771 |
|
772 |
if (snf.glance) {
|
773 |
$(".usermenu .custom_images").click(_.bind(function(e){ |
774 |
e.preventDefault(); |
775 |
this.custom_images_view.show();
|
776 |
}, this));
|
777 |
} else {
|
778 |
$(".usermenu .custom_images").hide(); |
779 |
} |
780 |
}, |
781 |
|
782 |
quota_handlers_initialized: false, |
783 |
|
784 |
load_user_quotas: function() { |
785 |
var main_view = this; |
786 |
if (!snf.user.quota) {
|
787 |
snf.user.quota = new snf.quota.Quota("cyclades"); |
788 |
main_view.init_quotas_handlers(); |
789 |
|
790 |
} |
791 |
|
792 |
snf.api.sync('read', undefined, { |
793 |
url: synnefo.config.quota_url,
|
794 |
success: function(d) { |
795 |
snf.user.quota.load(d); |
796 |
}, |
797 |
complete: function() { |
798 |
setTimeout(function(){
|
799 |
main_view.load_user_quotas(); |
800 |
}, synnefo.config.quotas_update_interval || 10000);
|
801 |
} |
802 |
}); |
803 |
}, |
804 |
|
805 |
check_quotas: function(type) { |
806 |
var storage = synnefo.storage[type];
|
807 |
var consumed = storage.length;
|
808 |
var quotakey = {
|
809 |
'networks': 'cyclades.network.private', |
810 |
'vms': 'cyclades.vm' |
811 |
} |
812 |
if (type == "networks") { |
813 |
consumed = storage.filter(function(net){
|
814 |
return !net.is_public() && !net.is_deleted();
|
815 |
}).length; |
816 |
} |
817 |
|
818 |
var limit = snf.user.quota.get_limit(quotakey[type]);
|
819 |
if (snf.user.quota && snf.user.quota.data && consumed >= limit) {
|
820 |
storage.trigger("quota_reached");
|
821 |
} else {
|
822 |
storage.trigger("quota_free");
|
823 |
} |
824 |
}, |
825 |
|
826 |
init_quotas_handlers: function() { |
827 |
var self = this, event; |
828 |
snf.user.quota.bind("cyclades.vm.quota.changed", function() { |
829 |
this.check_quotas("vms"); |
830 |
}, this);
|
831 |
|
832 |
var event = "cyclades.network.private.quota.changed"; |
833 |
snf.user.quota.bind(event, function() {
|
834 |
this.check_quotas("networks"); |
835 |
}, this);
|
836 |
}, |
837 |
|
838 |
// initial view based on user cookie
|
839 |
show_initial_view: function() { |
840 |
this.set_vm_view_handlers();
|
841 |
this.load_user_quotas();
|
842 |
this.hide_loading_view();
|
843 |
|
844 |
bb.history.start(); |
845 |
|
846 |
this.trigger("ready"); |
847 |
}, |
848 |
|
849 |
show_vm_details: function(vm) { |
850 |
if (vm) {
|
851 |
this.router.vm_details_view(vm.id);
|
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 |
synnefo.storage.vms.bind("quota_reached", function(){ |
864 |
$("#createcontainer #create").addClass("disabled"); |
865 |
$("#createcontainer #create").attr("title", "Machines limit reached"); |
866 |
}); |
867 |
|
868 |
synnefo.storage.vms.bind("quota_free", function(){ |
869 |
$("#createcontainer #create").removeClass("disabled"); |
870 |
$("#createcontainer #create").attr("title", ""); |
871 |
}); |
872 |
|
873 |
}, |
874 |
|
875 |
check_empty: function() { |
876 |
if (!this.loaded) { return } |
877 |
if (storage.vms.length == 0) { |
878 |
this.show_view("machines"); |
879 |
this.router.show_welcome();
|
880 |
this.empty_hidden = false; |
881 |
} else {
|
882 |
this.hide_empty();
|
883 |
} |
884 |
}, |
885 |
|
886 |
show_empty: function() { |
887 |
if (!this.empty_hidden) { return }; |
888 |
$("#machines-pane-top").addClass("empty"); |
889 |
|
890 |
this.$(".panes").hide(); |
891 |
this.$("#machines-pane").show(); |
892 |
|
893 |
this.hide_views([]);
|
894 |
this.empty_hidden = false; |
895 |
this.empty_view.show();
|
896 |
this.select_view.update_layout();
|
897 |
this.empty_hidden = false; |
898 |
}, |
899 |
|
900 |
hide_empty: function() { |
901 |
if (this.empty_hidden) { return }; |
902 |
$("#machines-pane-top").removeClass("empty"); |
903 |
|
904 |
this.empty_view.hide(true); |
905 |
this.router.vms_index();
|
906 |
this.empty_hidden = true; |
907 |
this.select_view.update_layout();
|
908 |
}, |
909 |
|
910 |
get_title: function(view_id) { |
911 |
var view_id = view_id || this.current_view_id; |
912 |
return this.views_titles[view_id]; |
913 |
}, |
914 |
|
915 |
// return class object for the given view or false if
|
916 |
// the view is not registered
|
917 |
get_class_for_view: function (view_id) { |
918 |
if (!this.views_classes[view_id]) { |
919 |
return false; |
920 |
} |
921 |
return this.views_classes[view_id]; |
922 |
}, |
923 |
|
924 |
view: function(view_id) { |
925 |
return this.views[view_id]; |
926 |
}, |
927 |
|
928 |
add_view: function(view_id) { |
929 |
if (!this.views[view_id]) { |
930 |
var cls = this.get_class_for_view(view_id); |
931 |
if (this.skip_errors) { |
932 |
this.views[view_id] = new cls(); |
933 |
$(this.views[view_id]).bind("resize", _.bind(function() { |
934 |
window.forcePositionFooter(); |
935 |
this.multiple_actions_view.fix_position();
|
936 |
}, this));
|
937 |
} else {
|
938 |
// catch ui errors
|
939 |
try {
|
940 |
this.views[view_id] = new cls(); |
941 |
$(this.views[view_id]).bind("resize", _.bind(function() { |
942 |
window.positionFooter(); |
943 |
this.multiple_actions_view.fix_position();
|
944 |
}, this));
|
945 |
} catch (err) {snf.ui.trigger_error(-1, "Cannot add view", err)} |
946 |
} |
947 |
} else {
|
948 |
} |
949 |
|
950 |
if (this.views[view_id].vms_view) { |
951 |
this.views[view_id].metadata_view = this.metadata_view; |
952 |
} |
953 |
return this.views[view_id]; |
954 |
}, |
955 |
|
956 |
hide_views: function(skip) { |
957 |
_.each(this.views, function(view) { |
958 |
if (skip.indexOf(view) === -1) { |
959 |
$(view.el).hide();
|
960 |
} |
961 |
}, this)
|
962 |
}, |
963 |
|
964 |
get: function(view_id) { |
965 |
return this.views[view_id]; |
966 |
}, |
967 |
|
968 |
session_view: function() { |
969 |
if (this.pane_view_from_session() > 0) { |
970 |
return this.views_pane_indexes[this.pane_view_from_session()]; |
971 |
} else {
|
972 |
return this.views_indexes[this.machine_view_from_session()]; |
973 |
} |
974 |
}, |
975 |
|
976 |
pane_view_from_session: function() { |
977 |
return $.cookie("pane_view") || 0; |
978 |
}, |
979 |
|
980 |
machine_view_from_session: function() { |
981 |
return $.cookie("machine_view") || 0; |
982 |
}, |
983 |
|
984 |
update_session: function() { |
985 |
$.cookie("pane_view", this.pane_ids[this.current_view_id]); |
986 |
if (this.current_view.vms_view) { |
987 |
$.cookie("machine_view", this.views_ids[this.current_view_id]); |
988 |
} |
989 |
}, |
990 |
|
991 |
identify_view: function(view_id) { |
992 |
// machines view_id is an alias to
|
993 |
// one of the 3 machines view
|
994 |
// identify which one (if no cookie set defaults to icon)
|
995 |
if (view_id == "machines") { |
996 |
var index = this.machine_view_from_session(); |
997 |
view_id = this.views_indexes[index];
|
998 |
} |
999 |
return view_id;
|
1000 |
}, |
1001 |
|
1002 |
// switch to current view pane
|
1003 |
// if differs from the visible one
|
1004 |
show_view_pane: function() { |
1005 |
if (this.current_view.pane != this.current_pane) { |
1006 |
$(this.current_view.pane).show(); |
1007 |
$(this.current_pane).hide(); |
1008 |
this.current_pane = this.current_view.pane; |
1009 |
} |
1010 |
}, |
1011 |
|
1012 |
show_view: function(view_id) { |
1013 |
//var d = new Date;
|
1014 |
var ret = this._show_view(view_id); |
1015 |
//console.log((new Date)-d)
|
1016 |
return ret;
|
1017 |
}, |
1018 |
|
1019 |
_show_view: function(view_id) { |
1020 |
// same view, visible
|
1021 |
// get out of here asap
|
1022 |
if (this.current_view && |
1023 |
this.current_view.id == view_id &&
|
1024 |
this.current_view.visible()) {
|
1025 |
return;
|
1026 |
} |
1027 |
|
1028 |
// choose proper view_id
|
1029 |
view_id = this.identify_view(view_id);
|
1030 |
|
1031 |
// add/create view and update current view
|
1032 |
var view = this.add_view(view_id); |
1033 |
|
1034 |
// set current view
|
1035 |
this.current_view = view;
|
1036 |
this.current_view_id = view_id;
|
1037 |
|
1038 |
// hide all other views
|
1039 |
this.hide_views([this.current_view]); |
1040 |
|
1041 |
// FIXME: depricated
|
1042 |
$(".large-spinner").remove(); |
1043 |
|
1044 |
storage.vms.reset_pending_actions(); |
1045 |
storage.networks.reset_pending_actions(); |
1046 |
storage.vms.stop_stats_update(); |
1047 |
|
1048 |
// show current view
|
1049 |
this.show_view_pane();
|
1050 |
view.show(); |
1051 |
|
1052 |
// update menus
|
1053 |
if (this.select_view) { |
1054 |
this.select_view.update_layout();
|
1055 |
} |
1056 |
this.current_view.__update_layout();
|
1057 |
|
1058 |
// update cookies
|
1059 |
this.update_session();
|
1060 |
|
1061 |
// machines view subnav
|
1062 |
if (this.current_view.vms_view) { |
1063 |
$("#machines-pane").show(); |
1064 |
} else {
|
1065 |
$("#machines-pane").hide(); |
1066 |
} |
1067 |
|
1068 |
// fix footer position
|
1069 |
// TODO: move footer handlers in
|
1070 |
// main view (from home.html)
|
1071 |
if (window.positionFooter) {
|
1072 |
window.positionFooter(); |
1073 |
} |
1074 |
|
1075 |
// trigger view change event
|
1076 |
this.trigger("view:change", this.current_view.view_id); |
1077 |
this.select_view.title.text(this.get_title()); |
1078 |
$(window).trigger("view:change"); |
1079 |
return view;
|
1080 |
}, |
1081 |
|
1082 |
reset_vm_actions: function() { |
1083 |
|
1084 |
}, |
1085 |
|
1086 |
// identify current view
|
1087 |
// based on views element visibility
|
1088 |
current_view_id: function() { |
1089 |
var found = false; |
1090 |
_.each(this.views, function(key, value) { |
1091 |
if (value.visible()) {
|
1092 |
found = value; |
1093 |
} |
1094 |
}) |
1095 |
return found;
|
1096 |
} |
1097 |
|
1098 |
}); |
1099 |
|
1100 |
snf.ui.main = new views.MainView();
|
1101 |
|
1102 |
snf.ui.init = function() { |
1103 |
if (snf.config.handle_window_exceptions) {
|
1104 |
window.onerror = function(msg, file, line) { |
1105 |
snf.ui.trigger_error("CRITICAL", msg, {}, { file:file + ":" + line, allow_close: true }); |
1106 |
}; |
1107 |
} |
1108 |
snf.ui.main.load(); |
1109 |
} |
1110 |
|
1111 |
})(this);
|