Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_main_view.js @ 2506f741

History | View | Annotate | Download (24.9 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

    
12
    var views = snf.views = snf.views || {}
13

    
14
    // shortcuts
15
    var bb = root.Backbone;
16
    var util = snf.util;
17
    
18
    // TODO: implement me
19
    views.NoticeView = views.Overlay.extend({});
20

    
21
    views.MultipleActionsView = views.View.extend({
22
        view_id: "multiple_actions",
23

    
24
        _actions: {},
25
        el: '#multiple_actions_container',
26
        
27
        initialize: function() {
28
            this.actions = {};
29
            views.MultipleActionsView.__super__.initialize.call(this);
30
            
31
            // view elements
32
            this.confirm_actions = this.$(".confirm_multiple_actions");
33
            this.confirm_actions_yes = this.$(".confirm_multiple_actions button.yes");
34
            this.confirm_actions_no = this.$(".confirm_multiple_actions button.no");
35
            this.confirm_reboot = this.$(".confirm_reboot_required");
36
            this.confirm_reboot_yes = this.$(".confirm_reboot_required button.yes");
37
            this.confirm_reboot_no = this.$(".confirm_reboot_required button.no");
38
            this.confirm_reboot_list = this.confirm_reboot.find(".reboot-machines-list");
39

    
40
            this.init_handlers();
41
            this.update_layout();
42
            
43
            // for heavy resize/scroll window events
44
            // do it `like a boss` 
45
            this.fix_position = _.throttle(this.fix_position, 100);
46
            this.update_layout = _.throttle(this.update_layout, 100);
47
            this.show_limit = 1;
48
        },
49

    
50
        init_handlers: function() {
51
            var self = this;
52

    
53
            $(window).resize(_.bind(function(){
54
                this.fix_position();
55
            }, this));
56

    
57
            $(window).scroll(_.bind(function(){
58
                this.fix_position();
59
            }, this));
60
            
61
            // confirm/cancel button handlers
62
            var self = this;
63
            this.confirm_actions_yes.click(function(){ self.do_all(); })
64
            this.confirm_actions_no.click(function(){
65
                self.reset_actions();
66
            });
67

    
68
            this.confirm_reboot_yes.click(function(){ self.do_reboot_all(); })
69
            this.confirm_reboot_no.click(function(){
70
                self.reset_reboots();
71
            });
72

    
73
            storage.vms.bind("change:pending_action", _.bind(this.handle_vm_change, this));
74
            storage.vms.bind("change:reboot_required", _.bind(this.handle_vm_change, this));
75
        },
76
    
77
        handle_vm_change: function(vm) {
78
            if (vm.has_pending_action()) {
79
                var action = vm.get("pending_action");
80
                this.add_action(vm, action);
81
            } else {
82
                this.remove_action(vm);
83
            }
84
            this.update_layout();
85
        },
86

    
87
        add_action: function(vm, action) {
88
            this._actions[vm.id] = {'vm': vm, 'action': action};
89
        },
90

    
91
        remove_action: function(vm) {
92
            delete this._actions[vm.id];
93
        },
94

    
95
        reset: function() {
96
            this._actions = {};
97
            this.update_layout();
98
        },
99
        
100
        reboot_vm: function(vm) {
101
            vm.call("reboot");
102
        },
103

    
104
        do_reboot_all: function() {
105
            _.each(storage.vms.get_reboot_required(), function(vm){
106
                this.reboot_vm(vm)
107
            }, this)  
108
        },
109

    
110
        do_all: function() {
111
            _.each(this._actions, function(action){
112
                action.vm.call(action.action);
113
            }, this)  
114
            this.reset_actions();
115
        },
116

    
117
        reset_reboots: function () {
118
            _.each(storage.vms.get_reboot_required(), function(vm) {vm.set({'reboot_required': false})}, this);
119
            this.update_layout();
120
        },
121

    
122
        reset_actions: function() {
123
            _.each(this._actions, _.bind(function(action){
124
                try {
125
                    action.vm.clear_pending_action();
126
                    this.remove_action(action.vm);
127
                } catch(err) {
128
                    console.error("vm " + action.vm.id + " failed to reset", err);
129
                }
130
            }, this))  
131
        },
132
        
133
        fix_position: function() {
134
            $('.confirm_multiple').removeClass('fixed');
135
            if (($(this.el).offset().top +$(this.el).height())> ($(window).scrollTop() + $(window).height())) {
136
                $('.confirm_multiple').addClass('fixed');
137
            }
138
        },
139
        
140
        check_notify_limit: function() {
141
            this.show_limit = 1;
142
            if (ui.main.current_view && ['networks', 'vm_list'].indexOf(ui.main.current_view.view_id) > -1) {
143
                this.show_limit = 0;
144
            }
145
        },
146
        
147
        update_reboot_required_list: function(vms) {
148
            this.confirm_reboot_list.empty();
149
        },
150

    
151
        update_reboot_required: function() {
152
            var vms = storage.vms.get_reboot_required();
153
            if (vms.length) {
154
                this.confirm_reboot.find(".actionLen").text(vms.length);
155
                this.update_reboot_required_list();
156
                this.confirm_reboot.show();
157
                $(this.el).show();
158
            } else {
159
                if (!this.actions_visible) {
160
                   $(this.el).hide();
161
                }
162
                this.confirm_reboot.hide();
163
            }
164
        },
165

    
166
        update_layout: function() {
167
            this.check_notify_limit();
168
            this.actions_visible = false;
169

    
170
            if (_.size(this._actions) > this.show_limit) {
171
                this.actions_visible = true;
172
                $(this.el).show();
173
                this.confirm_actions.show();
174
            } else {
175
                $(this.el).hide();
176
                this.confirm_actions.hide();
177
            }
178

    
179
            this.update_reboot_required();
180
            this.confirm_actions.find(".actionLen").text(_.size(this._actions));
181
            $(window).trigger("resize");
182
        }
183
    })
184
    
185
    // menu wrapper view
186
    views.SelectView = views.View.extend({
187
        
188
        initialize: function(view) {
189
            this.parent = view;
190

    
191
            this.pane_view_selector = $(".css-tabs");
192
            this.machine_view_selector = $("#view-select");
193
            this.el = $(".css-tabs");
194
            this.title = $(".tab-name");
195

    
196
            this.set_handlers();
197
            this.update_layout();
198

    
199
            views.SelectView.__super__.initialize.apply(this, arguments);
200
        },
201
        
202
        clear_active: function() {
203
            this.pane_view_selector.find("a").removeClass("active");
204
            this.machine_view_selector.find("a").removeClass("activelink");
205
        },
206
        
207
        // intercept menu links
208
        set_handlers: function() {
209
            var self = this;
210
            this.pane_view_selector.find("a").hover(function(){
211
                // FIXME: title from href ? omg
212
                self.title.text($(this).attr("href"));
213
            }, function(){
214
                self.title.text(self.parent.get_title());
215
            });
216

    
217
            this.pane_view_selector.find("a#machines_view_link").click(_.bind(function(ev){
218
                ev.preventDefault();
219
                this.parent.show_view("machines");
220
            }, this))
221
            this.pane_view_selector.find("a#networks_view_link").click(_.bind(function(ev){
222
                ev.preventDefault();
223
                this.parent.show_view("networks");
224
            }, this))
225
            this.pane_view_selector.find("a#disks_view_link").click(_.bind(function(ev){
226
                ev.preventDefault();
227
                this.parent.show_view("disks");
228
            }, this))
229
            
230
            this.machine_view_selector.find("a#machines_view_icon_link").click(_.bind(function(ev){
231
                ev.preventDefault();
232
                var d = $.now();
233
                this.parent.show_view("icon");
234
            }, this))
235
            this.machine_view_selector.find("a#machines_view_list_link").click(_.bind(function(ev){
236
                ev.preventDefault();
237
                this.parent.show_view("list");
238
            }, this))
239
            this.machine_view_selector.find("a#machines_view_single_link").click(_.bind(function(ev){
240
                ev.preventDefault();
241
                this.parent.show_view("single");
242
            }, this))
243
        },
244

    
245
        update_layout: function() {
246
            this.clear_active();
247

    
248
            var pane_index = this.parent.pane_ids[this.parent.current_view_id];
249
            $(this.pane_view_selector.find("a")).removeClass("active");
250
            $(this.pane_view_selector.find("a").get(pane_index)).addClass("active");
251
            
252
            if (this.parent.current_view && this.parent.current_view.vms_view) {
253

    
254
                if (storage.vms.length > 0) {
255
                    this.machine_view_selector.show();
256
                    var machine_index = this.parent.views_ids[this.parent.current_view_id];
257
                    $(this.machine_view_selector.find("a").get(machine_index)).addClass("activelink");
258
                } else {
259
                    this.machine_view_selector.hide();
260
                }
261
            } else {
262
                this.machine_view_selector.hide();
263
            }
264

    
265
        }
266
    });
267

    
268
    views.MainView = views.View.extend({
269
        el: 'body',
270
        view_id: 'main',
271
        
272
        // FIXME: titles belong to SelectView
273
        views_titles: {
274
            'icon': 'machines', 'single': 'machines', 
275
            'list': 'machines', 'networks': 'networks',
276
            'disks': 'disks'
277
        },
278

    
279
        // indexes registry
280
        views_indexes: {0: 'icon', 2:'single', 1: 'list', 3:'networks'},
281
        views_pane_indexes: {0:'single', 1:'networks', 2:'disks'},
282

    
283
        // views classes registry
284
        views_classes: {'icon': views.IconView, 'single': views.SingleView, 
285
            'list': views.ListView, 'networks': views.NetworksView},
286

    
287
        // view ids
288
        views_ids: {'icon':0, 'single':2, 'list':1, 'networks':3},
289

    
290
        // on which pane id each view exists
291
        // machine views (icon,single,list) are all on first pane
292
        pane_ids: {'icon':0, 'single':0, 'list':0, 'networks':1, 'disks':2},
293
    
294
        initialize: function(show_view) {
295
            if (!show_view) { show_view = 'icon' };
296
            
297
            // fallback to browser error reporting (true for debug)
298
            this.skip_errors = true
299

    
300
            // reset views
301
            this.views = {};
302

    
303
            this.el = $("#app");
304
            // reset main view status
305
            this._loaded = false;
306
            this.status = "Initializing...";
307

    
308
            // initialize handlers
309
            this.init_handlers();
310

    
311
            // identify initial view from user cookies
312
            // this view will be visible after loading of
313
            // main view
314
            this.initial_view = this.session_view();
315

    
316
            views.MainView.__super__.initialize.call(this);
317
        },
318
        
319
        vms_handlers_registered: false,
320

    
321
        // register event handlers
322
        // 
323
        // vms storage events to identify if vms list 
324
        // is empty and display empty view if user viewing
325
        // a machine view
326
        //
327
        // api/ui error event handlers
328
        init_handlers: function() {
329
            // vm handlers
330
            storage.vms.bind("remove", _.bind(this.check_empty, this));
331
            storage.vms.bind("add", _.bind(this.check_empty, this));
332
            storage.vms.bind("change", _.bind(this.check_empty, this));
333
            storage.vms.bind("reset", _.bind(this.check_empty, this));
334
            
335
            // api calls handlers
336
            synnefo.api.bind("error", _.bind(this.handle_api_error, this));
337
            synnefo.api.bind("change:error_state", _.bind(this.handle_api_error_state, this));
338
            synnefo.ui.bind("error", _.bind(this.handle_ui_error, this));
339
        },
340
        
341
        handle_api_error_state: function(state) {
342
            if (snf.api.error_state) {
343
                this.stop_intervals();
344
            } else {
345
                if (this.intervals_stopped) {
346
                    this.update_intervals();
347
                }
348
            }
349
        },
350
        
351
        handle_api_error: function(xhr, type, message) {
352
            this.error_state = true;
353
            this.log.error("API ERRROR", arguments);
354
            
355
            var xhr = arguments[0];
356
            var args = util.parse_api_error(arguments);
357
            
358
            // force logout if UNAUTHORIZED request arrives
359
            if (args.code == 401) { snf.ui.logout(); return };
360

    
361
            var error_entry = [args.ns, args.code, args.message, args.type, args.details, args];
362
            this.error_view.show_error.apply(this.error_view, error_entry);
363
        },
364

    
365
        handle_ui_error: function(error) {
366
            error = error + "<br /><br />" + snf.util.stacktrace().replace("at", "<br /><br />at");
367
            this.error_view.show_error("Application", -1, "Something went wrong", "JS Exception", error);
368
        },
369

    
370
        init_overlays: function() {
371
            this.create_vm_view = new views.CreateVMView();
372
            //this.notice_view = new views.NoticeView();
373
        },
374
        
375
        show_loading_view: function() {
376
            $("#container #content").hide();
377
            $("#loading-view").show();
378
        },
379

    
380
        hide_loading_view: function() {
381
            $("#container #content").show();
382
            $("#loading-view").hide();
383
            $(".css-panes").show();
384
        },
385
        
386
        items_to_load: 4,
387
        completed_items: 0,
388
        check_status: function(loaded) {
389
            this.completed_items++;
390
            // images, flavors loaded
391
            if (this.completed_items == 2) {
392
                this.load_nets_and_vms();
393
            }
394
            if (this.completed_items == this.items_to_load) {
395
                this.after_load();
396
            }
397
        },
398

    
399
        load_nets_and_vms: function() {
400
            var self = this;
401
            this.update_status("Loading vms...");
402
            storage.vms.fetch({refresh:true, update:false, success: function(){
403
                self.update_status("VMS Loaded.");
404
                self.check_status()
405
            }});
406
            this.update_status("Loading networks...");
407
            storage.networks.fetch({refresh:true, update:false, success: function(){
408
                self.update_status("Networks loaded.");
409
                self.check_status()
410
            }});
411
        },  
412

    
413
        init_intervals: function() {
414
            this._networks = storage.networks.get_fetcher(snf.config.update_interval, snf.config.update_interval/2, 2, true, undefined);
415
            this._vms = storage.vms.get_fetcher(snf.config.update_interval, snf.config.update_interval/2, 2, true, undefined);
416
        },
417

    
418
        stop_intervals: function() {
419
            if (this._networks) { this._networks.stop(); }
420
            if (this._vms) { this._vms.stop(); }
421
            this.intervals_stopped = true;
422
        },
423

    
424
        update_intervals: function() {
425
            if (this._networks) {
426
                this._networks.stop();
427
                this._networks.start();
428
            } else {
429
                this.init_intervals();
430
            }
431

    
432
            if (this._vms) {
433
                this._vms.stop();
434
                this._vms.start();
435
            } else {
436
                this.init_intervals();
437
            }
438

    
439
            this.intervals_stopped = false;
440
        },
441

    
442
        after_load: function() {
443
            this.update_status("Setting vms update interval...");
444
            this.init_intervals();
445
            this.update_intervals();
446
            this.update_status("Loaded");
447
            // FIXME: refactor needed
448
            // initialize views
449
            this.initialize_views()
450
            this.update_status("Initializing overlays...");
451
            this.init_overlays();
452
            // display initial view
453
            this.loaded = true;
454
            this.show_initial_view();
455
            this.check_empty();
456
        },
457

    
458
        load: function() {
459
            this.error_view = new views.ErrorView();
460
            var self = this;
461
            // initialize overlay views
462
            
463
            // display loading message
464
            this.show_loading_view();
465
            // sync load initial data
466
            this.update_status("Loading images...");
467
            storage.images.fetch({refresh:true, update:false, success: function(){
468
                self.check_status()
469
            }});
470
            this.update_status("Loading flavors...");
471
            storage.flavors.fetch({refresh:true, update:false, success:function(){
472
                self.check_status()
473
            }});
474
        },
475

    
476
        update_status: function(msg) {
477
            this.log.debug(msg)
478
            this.status = msg;
479
            $("#loading-view .info").removeClass("hidden")
480
            $("#loading-view .info").text(this.status);
481
        },
482

    
483
        initialize_views: function() {
484
            this.empty_view = new views.EmptyView();
485
            this.select_view = new views.SelectView(this);
486
            this.metadata_view = new views.MetadataView();
487
            this.multiple_actions_view = new views.MultipleActionsView();
488
            this.feedback_view = new views.FeedbackView();
489
            
490
            this.add_view("icon");
491
            this.add_view("list");
492
            this.add_view("single");
493
            this.add_view("networks");
494

    
495
            this.init_menu();
496
        },
497

    
498
        init_menu: function() {
499
            $(".usermenu .feedback").click(_.bind(function(){
500
                this.feedback_view.show();
501
            }, this));
502
        },
503
        
504
        // initial view based on user cookie
505
        show_initial_view: function() {
506
          this.set_vm_view_handlers();
507
          this.hide_loading_view();
508
          this.show_view(this.initial_view);
509
          this.trigger("initial");
510
        },
511

    
512
        show_vm_details: function(vm) {
513
            snf.ui.main.show_view("single")
514
            snf.ui.main.current_view.show_vm(vm);
515
        },
516

    
517
        set_vm_view_handlers: function() {
518
            $("#createcontainer #create").click(_.bind(function(){
519
                this.create_vm_view.show();
520
            }, this))
521
        },
522

    
523
        check_empty: function() {
524
            if (!this.loaded) { return }
525
            if (storage.vms.length == 0) {
526
                this.show_view("machines");
527
                this.show_empty();
528
            } else {
529
                this.hide_empty();
530
            }
531
            this.select_view.update_layout();
532
        },
533

    
534
        show_empty: function() {
535
            $("#machines-pane-top").addClass("empty");
536

    
537
            this.$(".panes").hide();
538
            this.$("#machines-pane").show();
539

    
540
            this.hide_views([]);
541
            this.empty_view.show();
542
        },
543

    
544
        hide_empty: function() {
545
            $("#machines-pane-top").removeClass("empty");
546

    
547
            this.empty_view = new views.EmptyView();
548
            this.empty_view.hide();
549
            if (this.current_view && !this.current_view.visible()) { 
550
                this.current_view.show(); 
551
            }
552
        },
553
        
554
        get_title: function(view_id) {
555
            var view_id = view_id || this.current_view_id;
556
            return this.views_titles[view_id];
557
        },
558

    
559
        // return class object for the given view or false if
560
        // the view is not registered
561
        get_class_for_view: function (view_id) {
562
            if (!this.views_classes[view_id]) {
563
                return false;
564
            }
565
            return this.views_classes[view_id];
566
        },
567

    
568
        view: function(view_id) {
569
            return this.views[view_id];
570
        },
571

    
572
        add_view: function(view_id) {
573
            if (!this.views[view_id]) {
574
                var cls = this.get_class_for_view(view_id);
575
                if (this.skip_errors) {
576
                    this.views[view_id] = new cls();
577
                    $(this.views[view_id]).bind("resize", _.bind(function() {
578
                        window.positionFooter();
579
                        this.multiple_actions_view.fix_position();
580
                    }, this));
581
                } else {
582
                    // catch ui errors
583
                    try {
584
                        this.views[view_id] = new cls();
585
                        $(this.views[view_id]).bind("resize", _.bind(function() {
586
                            window.positionFooter();
587
                            this.multiple_actions_view.fix_position();
588
                        }, this));
589
                    } catch (err) {snf.ui.trigger("error", err)}
590
                }
591
            } else {
592
            }
593

    
594
            if (this.views[view_id].vms_view) {
595
                this.views[view_id].metadata_view = this.metadata_view;
596
            }
597
            return this.views[view_id];
598
        },
599
            
600
        hide_views: function(skip) {
601
            _.each(this.views, function(view) {
602
                if (skip.indexOf(view) === -1) {
603
                    $(view.el).hide();
604
                }
605
            }, this)
606
        },
607
        
608
        get: function(view_id) {
609
            return this.views[view_id];
610
        },
611
        
612
        session_view: function() {
613
            if (this.pane_view_from_session() > 0) {
614
                return this.views_pane_indexes[this.pane_view_from_session()];
615
            } else {
616
                return this.views_indexes[this.machine_view_from_session()];
617
            }
618
        },
619

    
620
        pane_view_from_session: function() {
621
            return $.cookie("pane_view") || 0;
622
        },
623

    
624
        machine_view_from_session: function() {
625
            return $.cookie("machine_view") || 0;
626
        },
627

    
628
        update_session: function() {
629
            $.cookie("pane_view", this.pane_ids[this.current_view_id]);
630
            if (this.current_view.vms_view) {
631
                $.cookie("machine_view", this.views_ids[this.current_view_id]);
632
            }
633
        },
634

    
635
        identify_view: function(view_id) {
636
            // machines view_id is an alias to
637
            // one of the 3 machines view
638
            // identify which one (if no cookie set defaults to icon)
639
            if (view_id == "machines") {
640
                var index = this.machine_view_from_session();
641
                view_id = this.views_indexes[index];
642
            }
643
            return view_id;
644
        },
645
        
646
        // switch to current view pane
647
        // if differs from the visible one
648
        show_view_pane: function() {
649
            if (this.current_view.pane != this.current_pane) {
650
                $(this.current_view.pane).show();
651
                $(this.current_pane).hide();
652
                this.current_pane = this.current_view.pane;
653
            }
654
        },
655

    
656
        show_view: function(view_id) {
657
            // same view, visible
658
            // get out of here asap
659
            if (this.current_view && 
660
                this.current_view.id == view_id && 
661
                this.current_view.visible()) {
662
                return;
663
            }
664
            
665
            // choose proper view_id
666
            view_id = this.identify_view(view_id);
667

    
668
            // add/create view and update current view
669
            var view = this.add_view(view_id);
670
            
671
            // set current view
672
            this.current_view = view;
673
            this.current_view_id = view_id;
674

    
675
            // hide all other views
676
            this.hide_views([this.current_view]);
677
            
678
            // FIXME: depricated
679
            $(".large-spinner").remove();
680

    
681
            storage.vms.reset_pending_actions();
682
            storage.vms.reset_stats_update();
683

    
684
            // show current view
685
            this.show_view_pane();
686
            view.show();
687
            
688
            // update menus
689
            if (this.select_view) {
690
                this.select_view.update_layout();
691
            }
692
            this.current_view.__update_layout();
693

    
694
            // update cookies
695
            this.update_session();
696
            
697
            // machines view subnav
698
            if (this.current_view.vms_view) {
699
                $("#machines-pane").show();
700
            } else {
701
                $("#machines-pane").hide();
702
            }
703
            
704
            // fix footer position
705
            // TODO: move footer handlers in
706
            // main view (from home.html)
707
            if (window.positionFooter) {
708
                window.positionFooter();
709
            }
710
            
711
            // trigger view change event
712
            this.trigger("view:change", this.current_view.view_id);
713
            $(window).trigger("view:change");
714
            return view;
715
        },
716

    
717
        reset_vm_actions: function() {
718
        
719
        },
720
        
721
        // identify current view
722
        // based on views element visibility
723
        current_view_id: function() {
724
            var found = false;
725
            _.each(this.views, function(key, value) {
726
                if (value.visible()) {
727
                    found = value;
728
                }
729
            })
730
            return found;
731
        }
732

    
733
    });
734

    
735
    snf.ui.main = new views.MainView();
736
    
737
    snf.ui.logout = function() {
738
        $.cookie("X-Auth-Token", null);
739
        if (window.LOGOUT_REDIRECT !== undefined)
740
        {
741
            window.location = window.LOGOUT_REDIRECT;
742
        } else {
743
            window.location.reload();
744
        }
745
    }
746

    
747
    snf.ui.init = function() {
748
        snf.ui.main.load();
749
    }
750

    
751
})(this);