Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_vms_base_view.js @ b042eb04

History | View | Annotate | Download (19 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
    
17
    // logging
18
    var logger = new snf.logging.logger("SNF-VIEWS");
19
    var debug = _.bind(logger.debug, logger);
20
    
21
    // base class for views that contain/handle VMS
22
    views.VMListView = views.View.extend({
23

    
24
        // just a flag to identify that
25
        // views of this type handle vms
26
        vms_view: true,
27

    
28
        selectors: {},
29
        hide_actions: true,
30
        pane: "#machines-pane",
31
        metadata_view: undefined,
32

    
33
        initialize: function() {
34
            views.VMListView.__super__.initialize.call(this);
35
            this.set_storage_handlers();
36
            this.set_handlers();
37
            this.vms_updated_handler();
38
            this.connect_overlay = new views.VMConnectView();
39
            this.vm_selector = this.selectors.vm;
40
        },
41

    
42
        // Helpers
43
        //
44
        // get element based on this.selectors key/value pairs
45
        sel: function(id, params) {
46
            if (!this.selectors[id]){ return };
47
            return $(this.selectors[id].format(params));
48
        },
49
        
50
        // vm element based on vm model instance provided
51
        vm: function(vm) {
52
            return $(this.vm_selector + vm.id);
53
        },
54
        
55
        // get vm model instance from DOM element
56
        vm_for_element: function(el) {
57
            return storage.vms.sel(this.vm_id_for_element(el));
58
        },
59
        
60

    
61
        // Event binding and stuff like that
62
        //
63
        set_storage_handlers: function() {
64
            storage.vms.bind("add", _.bind(this.vms_updated_handler, this, "add"));
65
            storage.vms.bind("change", _.bind(this.vms_updated_handler, this, "change"));
66
            storage.vms.bind("reset", _.bind(this.vms_updated_handler, this, "reset"));
67
            storage.vms.bind("remove", _.bind(this.vms_updated_handler, this, "remove"));
68
        },
69
        
70
        // vms updated triggered, update view vms
71
        vms_updated_handler: function (method, model, arg2, arg3) {
72
            var updated = storage.vms.models;
73
            if (method == "add") { updated = [model] };
74
            if (method == "change") { updated = [model] };
75
            if (method == "remove") { updated = [model] };
76

    
77
            if (method == "remove") {
78
                this.remove_vm(model)
79
                return;
80
            }
81
            
82
            this.update_vms(updated);
83
        },
84

    
85
        // create vm
86
        // append it on proper view container
87
        create_vm: function(vm) {
88
            // create dom element
89
            var vm_view = this.create_vm_element(vm);
90
            vm_view.find(".vm-actions").attr("id", this.view_id+"-actions-" + vm.id);
91
            var container = this.get_vm_container(vm);
92
            container.append(vm_view);
93
            vm_view.find(".action-indicator").text("");
94
            if (this.visible()) {
95
                container.show()
96
            }
97

    
98
            // initialize vm specific event handlers 
99
            this.__set_vm_handlers(vm);
100
        },
101
        
102
        // create vm dom element
103
        create_vm_element: function(vm) {
104
            // clone template
105
            return this.sel('tpl').clone().attr("id", this.id_tpl + vm.id)
106
        },
107

    
108
        // get proper vm container
109
        get_vm_container: function(vm) {
110
            if (vm.is_active()) {
111
                return this.sel("vm_cont_active");
112
            } else {
113
                return this.sel("vm_cont_terminated");
114
            }
115
        },
116

    
117
        // create and append inside the proper container the vm model
118
        // if it doesn't exist update vm data and make it visible
119
        add: function(vm) {
120
            // create if it does not exist
121
            if (this.vm(vm).length == 0) {
122
                this.create_vm(vm);
123
                this.vm(vm).show();
124
                this.post_add(vm);
125
            }
126

    
127
            return this.vm(vm);
128
        },
129
        
130
        // helpers for VMListView descendants
131
        post_add: function(vm) { throw "Not implemented" },
132
        set_vm_handlers: function(vm) { throw "Not implemented" },
133
        set_handlers: function() { throw "Not implemented" },
134
        update_layout: function() { throw "Not implemented" },
135
        post_update_vm: function(vm) { throw "Not implemented" },
136
        update_details: function(vm) {},
137
        
138
        // remove vm
139
        remove_vm: function(vm) {
140
            // FIXME: some kind of transiton ??? effect maybe ???
141
            this.vm(vm).remove();
142
            this.post_remove_vm(vm);
143
        },
144
        
145
        // remove all vms from view
146
        clear: function() {
147
            this.sel('vms').remove();
148
            this.__update_layout();
149
        },
150

    
151
        show: function() {
152
            views.VMListView.__super__.show.apply(this, arguments);
153
            if (storage.vms.length == 0) { this.hide() };
154
            if (!snf.config.update_hidden_views) {
155
                this.update_vms(storage.vms.models);
156
            }
157
        },
158

    
159
        // do update for provided vms, then update the view layout
160
        update_vms: function(vms) {
161
            if (!this.visible() && !snf.config.update_hidden_views) { return };
162

    
163
            _.each(vms, _.bind(function(vm){
164
                // vm will be removed
165
                // no need to update
166
                if (vm.get("status") == "DELETED") {
167
                    return;
168
                }
169

    
170
                // this won't add it additional times
171
                this.add(vm);
172
                this.update_vm(vm);
173
            }, this))
174
            
175
            // update view stuff
176
            this.__update_layout();
177
        },
178
        
179
        // update ui for the given vm
180
        update_vm: function(vm) {
181
            // do not update deleted state vms
182
            if (!vm || vm.get("status") == 'DELETED') { return };
183
            this.check_vm_container(vm);
184

    
185
            this.update_details(vm);
186
            this.update_transition_state(vm);
187

    
188
            if (this.action_views) {
189
                this.action_views[vm.id].update();
190
                this.action_views[vm.id].update_layout();
191
            }
192
            
193
            try {
194
                this.post_update_vm(vm);
195
            } catch (err) {};
196
        },
197

    
198
        // check if vm is placed properly within the view
199
        // container (e.g. some views might have different
200
        // containers for terminated or running machines
201
        check_vm_container: function(vm){
202
            var el = this.vm(vm);
203
            if (!el.length) { return };
204
            var self = this;
205
            var selector = vm.is_active() ? 'vm_cont_active' : 'vm_cont_terminated';
206
            if (el.parent()[0] != this.sel(selector)[0]) {
207
                var cont = this.sel(selector);
208
                var self = this;
209

    
210
                el.hide().appendTo(cont).fadeIn(300);
211
                $(window).trigger('resize');
212

    
213
                //el.fadeOut(200, function() {
214
                    //el.appendTo(cont); 
215
                    //el.fadeIn(200);
216
                    //self.sel(selector).show(function(){
217
                        //$(window).trigger("resize");
218
                    //});
219
                //});
220
            }
221
        },
222

    
223
        __update_layout: function() {
224
            this.update_layout();
225
        },
226
        
227
        // append handlers for vm specific events
228
        __set_vm_handlers: function(vm) {
229
            // show transition on vm status transit
230
            vm.bind('transition', _.bind(function(){this.show_transition(vm)}, this));
231
            this.set_vm_handlers(vm);
232
        },
233
        
234
        // is vm in transition ??? show the progress spinner
235
        update_transition_state: function(vm) {
236
            if (vm.in_transition() && !vm.pending_action){
237
                this.sel('vm_spinner', vm.id).show();
238
            } else {
239
                this.sel('vm_spinner', vm.id).hide();
240
            }
241
        },
242
        
243
        show_indicator: function(vm, action) {
244
            var action = action || vm.pending_action;
245
            this.sel('vm_wave', vm.id).hide();
246
            this.sel('vm_spinner', vm.id).hide();
247
            this.vm(vm).find(".action-indicator").removeClass().addClass(action + " action-indicator").show();
248
        },
249

    
250
        hide_indicator: function(vm) {
251
            this.vm(vm).find(".action-indicator").removeClass().addClass("action-indicator").hide();
252
            this.update_transition_state(vm);
253
        },
254

    
255
        // display transition animations
256
        show_transition: function(vm) {
257
            var wave = this.sel('vm_wave', vm.id);
258
            if (!wave || !wave.length) { return }
259

    
260
            var src = wave.attr('src');
261
            // change src to force gif play from the first frame
262
            // animate for 500 ms then hide
263
            wave.attr('src', "").show();
264
            wave.attr('src', src).fadeIn(200).delay(700).fadeOut(300, function() {
265
                wave.hide();
266
            });
267
        },
268

    
269
        connect_to_console: function(vm) {
270
            vm.call("console", function(console_data) {
271
                var url = vm.get_console_url(console_data);
272
                snf.util.open_window(url, "Console", {});
273
            })
274
        }
275

    
276
    });
277
    
278
    // empty message view (just a wrapper to the element containing 
279
    // the empty information message)
280
    views.EmptyView = views.View.extend({
281
        el: '#emptymachineslist'
282
    })
283

    
284
    views.VMActionsView = views.View.extend({
285
        
286
        initialize: function(vm, parent, el, hide) {
287
            this.hide = hide || false;
288
            this.view = parent;
289
            this.vm = vm;
290
            this.vm_el = el;
291
            this.el = $("#" + parent.view_id + "-actions-" + vm.id);
292
            this.set_handlers();
293
            this.all_action_names = _.keys(views.VMActionsView.STATUS_ACTIONS);
294
            
295
            // state params
296
            this.selected_action = false;
297

    
298
            _.bindAll(this);
299
            window.acts = this;
300
            this.view_id = "vm_" + vm.id + "_actions";
301
            views.VMActionsView.__super__.initialize.call(this);
302

    
303
            this.hovered = false;
304
        },
305

    
306
        action: function(name) {
307
            return $(this.el).find(".action-container." + name);
308
        },
309

    
310
        action_link: function(name) {
311
            return this.action(name).find("a");
312
        },
313
        
314
        action_confirm_cont: function(name) {
315
            return this.action_confirm(name).parent();
316
        },
317

    
318
        action_confirm: function(name) {
319
            return this.action(name).find("button.yes");
320
        },
321

    
322
        action_cancel: function(name) {
323
            return this.action(name).find("button.no");
324
        },
325

    
326
        hide_actions: function() {
327
            $(this.el).find("a").css("visibility", "hidden");
328
        },
329

    
330
        // update the actions layout, depending on the selected actions
331
        update_layout: function() {
332
            try {
333
                // it doesn't seem to work without this
334
                // some serious debugging is needed to 
335
                // find out what is going on
336
                this.vm = storage.vms.get(this.vm.id);
337
                this.init_vm_handlers();
338
            } catch (err) { console.error(err); return }
339

    
340
            if (!this.vm) { return }
341
            
342
            // update selected action
343
            if (this.vm.pending_action) {
344
                this.selected_action = this.vm.pending_action;
345
            } else {
346
                this.selected_action = false;
347
            }
348
            
349
            // vm actions tha can be performed
350
            var actions = this.vm.get_available_actions();
351
            
352
            // had pending action but actions changed and now selected action is
353
            // not available, hide it from user
354
            if (this.selected_action && actions.indexOf(this.selected_action) == -1) {
355
                this.reset();
356
            }
357
            
358
            this.el.show();
359
            
360
            if ((this.selected_action || this.hovered) && !this.vm.action_error) {
361
                // show selected action
362
                $(this.el).show();
363
                $(this.el).find("a").css("visibility", "visible");
364
                // show action icon
365
                this.view.show_indicator(this.vm);
366
            } else {
367
                if (this.hide || this.vm.action_error) {
368
                    // view shows actions on machine hover
369
                    $(this.el).find("a").css("visibility", "hidden");
370
                } else {
371
                    if (!this.vm.action_error) {
372
                        // view shows actions always
373
                        $(this.el).find("a").css("visibility", "visible");
374
                        $(this.el).show();
375
                    }
376
                }
377
                
378
                this.view.hide_indicator(this.vm);
379
            }
380
            
381
            // update action link styles and shit
382
            _.each(models.VM.ACTIONS, function(action, index) {
383
                if (actions.indexOf(action) > -1) {
384
                    this.action(action).removeClass("disabled");
385
                    if (this.selected_action == action) {
386
                        this.action_confirm_cont(action).css('display', 'block');
387
                        this.action_confirm(action).show();
388
                        this.action(action).removeClass("disabled");
389
                        this.action_link(action).addClass("selected");
390
                    } else {
391
                        this.action_confirm_cont(action).hide();
392
                        this.action_confirm(action).hide();
393
                        this.action_link(action).removeClass("selected");
394
                    }
395
                } else {
396
                    this.action().hide();
397
                    this.action(action).addClass("disabled");
398
                    this.action_confirm(action).hide();
399
                }
400

    
401
            }, this);
402
        },
403
        
404
        init_vm_handlers: function() {
405
            try {
406
                this.vm.unbind("action:fail", this.update_layout)
407
                this.vm.unbind("action:fail:reset", this.update_layout)
408
            } catch (err) {};
409

    
410
            this.vm.bind("action:fail", this.update_layout)
411
            this.vm.bind("action:fail:reset", this.update_layout)
412
        },
413

    
414
        // bind event handlers
415
        set_handlers: function() {
416
            var self = this;
417
            var vm = this.vm;
418
            
419
            // initial hide
420
            if (this.hide) { $(this.el).hide() };
421
            
422
            // vm container hover (icon view)
423
            this.view.vm(this.vm).hover(_.bind(function() {
424
                this.hovered = true;
425
                this.update_layout();
426

    
427
            }, this),  _.bind(function() {
428
                this.hovered = false;
429
                this.update_layout();
430
            }, this));
431

    
432
            
433
            // action links events
434
            _.each(models.VM.ACTIONS, function(action) {
435
                var action = action;
436
                // indicator hovers
437
                this.view.vm(this.vm).find(".action-container."+action+" a").hover(function() {
438
                    self.view.show_indicator(self.vm, action);
439
                }, function() {
440
                    // clear or show selected action indicator
441
                    if (self.vm.pending_action) {
442
                        self.view.show_indicator(self.vm);
443
                    } else {
444
                        self.view.hide_indicator(self.vm);
445
                    }
446
                })
447
                
448
                // action links click events
449
                $(this.el).find(".action-container."+action+" a").click(function(ev) {
450
                    ev.preventDefault();
451
                    self.set(action);
452
                }).data("action", action);
453

    
454
                // confirms
455
                $(this.el).find(".action-container."+action+" button.no").click(function(ev) {
456
                    ev.preventDefault();
457
                    self.reset();
458
                });
459

    
460
                // cancels
461
                $(this.el).find(".action-container."+action+" button.yes").click(function(ev) {
462
                    ev.preventDefault();
463
                    // override console
464
                    // ui needs to act (open the console window)
465
                    if (action == "console") {
466
                        self.view.connect_to_console(self.vm);
467
                    } else {
468
                        self.vm.call(action);
469
                    }
470
                    self.reset();
471
                });
472
            }, this);
473
        },
474
        
475
        // reset actions
476
        reset: function() {
477
            var prev_action = this.selected_action;
478
            this.selected_action = false;
479
            this.vm.clear_pending_action();
480
            this.trigger("change", {'action': prev_action, 'vm': this.vm, 'view': this, remove: true});
481
            this.trigger("remove", {'action': prev_action, 'vm': this.vm, 'view': this, remove: true});
482
        },
483
        
484
        // set selected action
485
        set: function(action_name) {
486
            this.selected_action = action_name;
487
            this.vm.update_pending_action(this.selected_action);
488
            this.view.vm(this.vm).find(".action-indicator").show().removeClass().addClass(action_name + " action-indicator");
489
            this.trigger("change", {'action': this.selected_action, 'vm': this.vm, 'view': this});
490
        },
491

    
492
        update: function() {
493
        }
494
    })
495

    
496

    
497
    views.VMActionsView.STATUS_ACTIONS = { 
498
        'reboot':        ['UNKOWN', 'ACTIVE', 'REBOOT'],
499
        'shutdown':      ['UNKOWN', 'ACTIVE', 'REBOOT'],
500
        'console':       ['ACTIVE'],
501
        'start':         ['UNKOWN', 'STOPPED'],
502
        'destroy':       ['UNKOWN', 'ACTIVE', 'STOPPED', 'REBOOT', 'ERROR', 'BUILD']
503
    };
504

    
505
    // UI helpers
506
    var uihelpers = snf.ui.helpers = {};
507
    
508
    // OS icon helpers
509
    var os_icon = uihelpers.os_icon = function(os) {
510
        var icons = window.os_icons;
511
        if (icons.indexOf(os) == -1) {
512
            os = "unknown";
513
        }
514
        return os;
515
    }
516

    
517
    var os_icon_path = uihelpers.os_icon_path = function(os, size, active) {
518
        size = size || "small";
519
        if (active == undefined) { active = true };
520

    
521
        var icon = os_icon(os);
522
        if (active) {
523
            icon = icon + "-on";
524
        } else {
525
            icon = icon + "-off";
526
        }
527

    
528
        return "/static/icons/machines/{0}/{1}.png".format(size, icon)
529
    }
530

    
531
    var os_icon_tag = uihelpers.os_icon_tag = function (os, size, active, attrs) {
532
        attrs = attrs || {};
533
        return '<img src="{0}" />'.format(os_icon_path(os, size, active));
534
    }
535

    
536
    // VM Icon helpers
537
    //
538
    // identify icon
539
    var vm_icon = uihelpers.vm_icon = function(vm) {
540
        return os_icon(vm.get_os());
541
    }
542
    
543
    // get icon url
544
    var vm_icon_path = uihelpers.vm_icon_path = function(vm, size) {
545
        return os_icon_path(vm.get_os(), size, vm.is_active());
546
    }
547
    
548
    // get icon IMG tag
549
    var vm_icon_tag = uihelpers.vm_icon_tag = function (vm, size, attrs) {
550
       return os_icon_tag(vm.get_os(), size, vm.is_active(), attrs);
551
    }
552
    
553
    snf.ui = _.extend(snf.ui, bb.Events);
554

    
555
})(this);