Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_single_view.js @ 1c1f838e

History | View | Annotate | Download (11.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
    var util = snf.util = snf.util || {};
12

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

    
15
    // shortcuts
16
    var bb = root.Backbone;
17
    var hasKey = Object.prototype.hasOwnProperty;
18

    
19
    views.SingleDetailsView = views.VMDetailsView.extend({
20
    
21
        view_id: "vm_details_single",
22
        el_sel: '.machine-details',
23
        
24
        selectors: {
25
            'cpu': '.machine-detail.cpus',
26
            'ram': '.machine-detail.ram',
27
            'disk': '.machine-detail.disk',
28
            'image_name': '.machine-detail.image-name',
29
            'image_size': '.machine-detail.image-size'
30
        }
31
    
32
    })
33

    
34
    // VMs single view
35
    views.SingleView = views.VMListView.extend({
36
        
37
        // view id (this could be used to identify 
38
        // the view object from global context
39
        view_id: 'vm_single',
40

    
41
        el: '#machinesview-single',
42
        id_tpl: 'single-vm-',
43
        link_id_tpl: 'single-vm-at-',
44

    
45
        hide_actions: false,
46

    
47
        selectors: {
48
            'vms': '.single-container',
49
            'vm': '#single-vm-',
50
            'view': '#machinesview-single',
51
            'tpl': 'div.single-container#machine-container-template',
52
            'spinner': '.large-spinner',
53
            'vm_spinner': '#single-vm-{0} .state .spinner',
54
            'vm_wave': '#single-vm-{0} img.wave',
55
            'vm_cont_active': '#machinesview-single',
56
            'vm_cont_terminated': '#machinesview-single'
57
        },
58
        
59
        initialize: function() {
60
            this.current_vm = 0;
61
            
62
            // button selectors
63
            this.prev_button = this.$(".controls .previous");
64
            this.next_button = this.$(".controls .next");
65
            this.menu = $("#single-servers-list");
66

    
67
            views.SingleView.__super__.initialize.apply(this, arguments);
68
            this.update_current_vm();
69
        },
70

    
71
        // overload show function
72
        show: function() {
73
            views.SingleView.__super__.show.apply(this, arguments);
74
            this.log.debug("showing");
75
            this.$(".column3").show();
76
            this.show_vm_menu();
77
            this.show_current();
78
        },
79

    
80
        show_vm: function(vm) {
81
            if (!vm) { return };
82
            this.current_vm_instance = vm;
83
            this.show_vm_menu();
84
            this.show_current();
85
            this.update_layout();
86
        },
87

    
88
        // identify vm model instance id based on DOM element
89
        vm_id_for_element: function(el) {
90
            return el.attr('id').replace("single-vm-", "");
91
        },
92
        
93
        // set generic view handlers
94
        set_handlers: function() {
95
            this.prev_button.click(_.bind(function(ev){
96
                storage.vms.reset_pending_actions();
97
                ev.preventDefault();
98
                this.show_prev();
99
            }, this));
100

    
101
            this.next_button.click(_.bind(function(ev){
102
                storage.vms.reset_pending_actions();
103
                ev.preventDefault();
104
                this.show_next();
105
            }, this));
106
        },  
107

    
108
        update_current_vm: function() {
109
            try {
110
                this.current_vm_instance = storage.vms.at(this.current_vm);
111
                this.current_vm_instance.start_stats_update(true);
112
                storage.vms.stop_stats_update([this.current_vm_instance]);
113
            } catch (err) {
114
                this.log.debug("Cannot select current vm instance for: {0}".format(this.current_vm));
115
                this.current_vm_instance = undefined;
116
                this.current_vm = 0;
117
            }
118
        },
119

    
120
        show_next: function() {
121
            this.current_vm++;
122
            if (this.current_vm >= storage.vms.models.length) {
123
                this.current_vm = 0;
124
            }
125

    
126
            this.update_current_vm();
127
            this.show_current();
128
            this.__update_layout();
129
        },
130

    
131
        show_prev: function() {
132
            this.current_vm--;
133
            if (this.current_vm < 0) {
134
                this.current_vm = storage.vms.length - 1;
135
            }
136
            this.update_current_vm();
137
            this.show_current();
138
            this.__update_layout();
139
        },
140

    
141
        post_remove_vm: function(vm) {
142
            // current vm removed or does not exist after an update
143
            this.show_vm_menu();
144
            if (!this.current_vm_instance || this.current_vm_instance.id == vm.id) {
145
                this.show_next();
146
            } else {
147
                this.show_current();
148
            }
149
        },
150
        
151
        // stuff to do when a new vm has been created.
152
        // - create vm subviews
153
        post_add: function(vm) {
154
            this.show_vm_menu();
155
            this.show_current();
156

    
157
            // rename views index
158
            this.stats_views = this.stats_views || {};
159
            this.connect_views = this.connect_views || {};
160
            this.tags_views = this.tags_views || {};
161
            this.details_views = this.details_views || {};
162
            this.action_views = this.action_views || {};
163
            this.action_error_views = this.action_error_views || {};
164

    
165
            //this.stats_views[vm.id] = new views.IconStatsView(vm, this);
166

    
167
            // same as icon view
168
            this.action_views[vm.id] = new views.VMActionsView(vm, this, this.vm(vm), this.hide_actions);
169
            this.stats_views[vm.id] = new views.VMStatsView(vm, this, {stats_type: 'series'});
170
            this.connect_views[vm.id] = new views.IconVMConnectView(vm, this);
171
            this.tags_views[vm.id] = new views.VMTagsView(vm, this, true, 20, 10, 35);
172
            this.details_views[vm.id] = new views.SingleDetailsView(vm, this);
173
            this.action_error_views[vm.id] = new views.VMActionErrorView(vm, this);
174
            
175
            if (storage.vms.models.length > 1) { this.vm(vm).hide(); };
176
        },
177

    
178
        post_update_vm: function(vm) {
179
        },
180
        
181
        // vm specific event handlers
182
        set_vm_handlers: function(vm) {
183
            var el = this.vm(vm);
184
        },
185
        
186
        // handle selected vm
187
        show_current: function() {
188
            var index = this.current_vm;
189
            var vm = storage.vms.at(index);
190

    
191
            this.$(".server-name").removeClass("column3-selected");
192
            
193
            if (vm) {
194
                this.vm(vm).show();
195
            };
196

    
197
            _.each(storage.vms.models, function(vmo){
198
                if (vm && (vm.id != vmo.id)) {
199
                    if (!hasKey.call(this._vm_els, vmo.id)) { return };
200
                    this.vm(vmo).hide();
201
                }
202
            }, this)
203

    
204
            if (!vm) {
205
                // empty list
206
                this.$(".column3").hide();
207
                return;
208
            }
209
            this.$(".column3").show();
210

    
211

    
212
            $("#" + this.link_id_tpl + this.current_vm).addClass("column3-selected");
213
            try {
214
                this.update_details(vm);
215
            } catch (err) {};
216
        },
217

    
218
        show_vm_menu: function() {
219
            this.menu.find(".server-name").remove();
220

    
221
            _.each(storage.vms.models, function(vm, index) {
222
                this.menu.append('<div class="server-name" id="'+this.link_id_tpl + index +'">' + 
223
                               util.truncate(vm.get("name"),16)+'</div>');
224
                if (this.current_vm_instance && vm.id == this.current_vm_instance.id) {
225
                    this.current_vm = index;
226
                }
227
            }, this);
228
            
229
            var self = this;
230
            this.menu.find(".server-name").click(function(ev) {
231
                ev.preventDefault();
232
                var id = $(this).attr("id").replace("single-vm-at-", "");
233
                if (self.current_vm != id) {
234
                    storage.vms.reset_pending_actions();
235
                }
236
                self.current_vm = id;
237
                self.update_current_vm();
238
                self.show_current();
239
            })
240
        },
241

    
242
        // generic stuff to do on each view update
243
        // called once after each vm has been updated
244
        update_layout: function() {
245
            this.update_current_vm();
246
            fix_v6_addresses();
247
        },
248

    
249
        // update vm details
250
        update_details: function(vm) {
251
            var el = this.vm(vm);
252
            if (vm != this.current_vm_instance) { return };
253

    
254
            // truncate name
255
            el.find(".machine-detail.name").text(util.truncate(vm.get("name"), 35));
256
            // set ips
257
            el.find(".machine-detail.ipv4.ipv4-text").text(vm.get_addresses().ip4 || "not set");
258
            // TODO: fix ipv6 truncates and tooltip handler
259
            el.find(".machine-detail.ipv6.ipv6-text").text(vm.get_addresses().ip6 || "not set");
260
            // set the state (i18n ??)
261
            el.find(".state-label").text(STATE_TEXTS[vm.state()]);
262
            // set state class
263
            el.find(".state").removeClass().addClass(views.SingleView.STATE_CLASSES[vm.state()].join(" "));
264
            // os icon
265
            el.find(".single-image").css({'background-image': "url(" + this.get_vm_icon_path(vm, "medium") + ")"});
266
            
267
            el.removeClass("connectable");
268
            if (vm.is_connectable()) {
269
                el.addClass("connectable");
270
            }
271

    
272
            if (vm.get('status') == 'BUILD') {
273
                // update bulding progress
274
                el.find("span.build-progress").show().text(vm.get("progress_message"));
275
            } else {
276
                // hide building progress
277
                el.find("span.build-progress").hide();
278
            }
279

    
280
            if (vm.state() == "DESTROY") {
281
                el.find("span.build-progress").show().text("Terminating...");
282
            }
283

    
284
            icon_state = vm.is_active() ? "on" : "off";
285
            set_machine_os_image(el, "single", icon_state, this.get_vm_icon_os(vm));
286
            
287
            // update subviews
288
            this.action_views[vm.id].update_layout();
289
            this.stats_views[vm.id].update_layout();
290
            this.connect_views[vm.id].update_layout();
291
            this.tags_views[vm.id].update_layout();
292
            this.details_views[vm.id].update_layout();
293
        },
294
            
295
        get_vm_icon_os: function(vm) {
296
            var os = vm.get_os();
297
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
298
            if (icons.indexOf(os) == -1) {
299
                os = "unknown";
300
            }
301
            return os;
302
        },
303

    
304
        // TODO: move to views.utils (the method and the VM_OS_ICON vars)
305
        get_vm_icon_path: function(vm, icon_type) {
306
            var os = vm.get_os();
307
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
308

    
309
            if (icons.indexOf(os) == -1) {
310
                os = "unknown";
311
            }
312

    
313
            return views.SingleView.VM_OS_ICON_TPLS()[icon_type].format(os);
314
        }
315
    })
316

    
317
    views.SingleView.VM_OS_ICON_TPLS = function() {
318
        return {
319
            "medium": snf.config.machines_icons_url + "large/{0}-sprite.png"
320
        }
321
    }
322

    
323
    views.SingleView.VM_OS_ICONS = window.os_icons || [];
324

    
325
    views.SingleView.STATE_CLASSES = {
326
        'UNKNOWN':          ['state', 'error-state'],
327
        'BUILD':            ['state', 'build-state'],
328
        'REBOOT':           ['state', 'rebooting-state'],
329
        'STOPPED':          ['state', 'terminated-state'],
330
        'ACTIVE':           ['state', 'running-state'],
331
        'ERROR':            ['state', 'error-state'],
332
        'DELETED':           ['state', 'destroying-state'],
333
        'DESTROY':          ['state', 'destroying-state'],
334
        'BUILD_INIT':       ['state', 'build-state'], 
335
        'BUILD_COPY':       ['state', 'build-state'],
336
        'BUILD_FINAL':      ['state', 'build-state'],
337
        'SHUTDOWN':         ['state', 'shutting-state'],
338
        'START':            ['state', 'starting-state'],
339
        'CONNECT':          ['state', 'connecting-state'],
340
        'DISCONNECT':       ['state', 'disconnecting-state']
341
    };
342

    
343
})(this);