Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_single_view.js @ b4372e4d

History | View | Annotate | Download (11.3 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
    
18
    views.SingleDetailsView = views.VMDetailsView.extend({
19
    
20
        view_id: "vm_details_single",
21
        el_sel: '.machine-details',
22
        
23
        selectors: {
24
            'cpu': '.machine-detail.cpus',
25
            'ram': '.machine-detail.ram',
26
            'disk': '.machine-detail.disk',
27
            'image_name': '.machine-detail.image-name',
28
            'image_size': '.machine-detail.image-size'
29
        }
30
    
31
    })
32

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

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

    
44
        hide_actions: false,
45

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

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

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

    
79
        show_vm: function(vm) {
80
            this.current_vm_instance = vm;
81
            this.show_vm_menu();
82
            this.show_current();
83
            this.update_layout();
84
        },
85

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

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

    
106
        update_current_vm: function() {
107
            storage.vms.reset_stats_update();
108

    
109
            try {
110
                this.current_vm_instance = storage.vms.at(this.current_vm);
111
                this.current_vm_instance.do_update_stats = true;
112
            } catch (err) {
113
                console.trace();
114
                this.log.debug("Cannot select current vm instance for: {0}".format(this.current_vm));
115
            }
116
        },
117

    
118
        show_next: function() {
119
            this.current_vm++;
120
            if (this.current_vm >= storage.vms.length) {
121
                this.current_vm = 0;
122
            }
123
            this.update_current_vm();
124
            this.__update_layout();
125
        },
126

    
127
        show_prev: function() {
128
            this.current_vm--;
129
            if (this.current_vm < 0) {
130
                this.current_vm = storage.vms.length - 1;
131
            }
132
            this.update_current_vm();
133
            this.__update_layout();
134
        },
135

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

    
152
            // rename views index
153
            this.stats_views = this.stats_views || {};
154
            this.connect_views = this.connect_views || {};
155
            this.tags_views = this.tags_views || {};
156
            this.details_views = this.details_views || {};
157
            this.action_views = this.action_views || {};
158
            this.action_error_views = this.action_error_views || {};
159

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

    
162
            // same as icon view
163
            this.action_views[vm.id] = new views.VMActionsView(vm, this, this.vm(vm), this.hide_actions);
164
            this.stats_views[vm.id] = new views.VMStatsView(vm, this, {stats_type: 'series'});
165
            this.connect_views[vm.id] = new views.IconVMConnectView(vm, this);
166
            this.tags_views[vm.id] = new views.VMTagsView(vm, this, true, 20, 10, 35);
167
            this.details_views[vm.id] = new views.SingleDetailsView(vm, this);
168
            this.action_error_views[vm.id] = new views.VMActionErrorView(vm, this);
169

    
170
            this.vm(vm).hide();
171
        },
172

    
173
        post_update_vm: function(vm) {
174
        },
175
        
176
        // vm specific event handlers
177
        set_vm_handlers: function(vm) {
178
            var el = this.vm(vm);
179
        },
180
        
181
        // handle selected vm
182
        show_current: function() {
183
            var index = this.current_vm;
184
            var vm = storage.vms.at(index);
185

    
186
            this.$(".server-name").removeClass("column3-selected");
187
            
188
            if (vm) {
189
                this.vm(vm).show();
190
            };
191

    
192
            _.each(storage.vms.models, function(vmo){
193
                if (vm && (vm.id != vmo.id)) {
194
                    this.vm(vmo).hide();
195
                }
196
            }, this)
197

    
198
            if (!vm) {
199
                // empty list
200
                this.$(".column3").hide();
201
                return;
202
            }
203
            this.$(".column3").show();
204

    
205

    
206
            $("#" + this.link_id_tpl + this.current_vm).addClass("column3-selected");
207
        },
208

    
209
        show_vm_menu: function() {
210
            this.menu.find(".server-name").remove();
211

    
212
            _.each(storage.vms.models, function(vm, index) {
213
                this.menu.append('<div class="server-name" id="'+this.link_id_tpl + index +'">' + 
214
                               util.truncate(vm.get("name"),16)+'</div>');
215
                if (this.current_vm_instance && vm.id == this.current_vm_instance.id) {
216
                    this.current_vm = index;
217
                }
218
            }, this);
219
            
220
            var self = this;
221
            this.menu.find(".server-name").click(function(ev) {
222
                ev.preventDefault();
223
                var id = $(this).attr("id").replace("single-vm-at-", "");
224
                self.current_vm = id;
225
                self.update_current_vm();
226
                self.show_current();
227
            })
228
        },
229

    
230
        // generic stuff to do on each view update
231
        // called once after each vm has been updated
232
        update_layout: function() {
233
            this.update_current_vm();
234
            fix_v6_addresses();
235
        },
236

    
237
        // update vm details
238
        update_details: function(vm) {
239
            var el = this.vm(vm);
240
            // truncate name
241
            el.find(".machine-detail.name").text(util.truncate(vm.get("name"), 35));
242
            // set ips
243
            el.find(".machine-detail.ipv4.ipv4-text").text(vm.get_addresses().ip4 || "undefined");
244
            // TODO: fix ipv6 truncates and tooltip handler
245
            el.find(".machine-detail.ipv6.ipv6-text").text(vm.get_addresses().ip6 || "undefined");
246
            // set the state (i18n ??)
247
            el.find(".state-label").text(STATE_TEXTS[vm.state()]);
248
            // set state class
249
            el.find(".state").removeClass().addClass(views.SingleView.STATE_CLASSES[vm.state()].join(" "));
250
            // os icon
251
            el.find(".single-image").css({'background-image': "url(" + this.get_vm_icon_path(vm, "medium") + ")"});
252
            
253
            el.removeClass("connectable");
254
            if (vm.is_connectable()) {
255
                el.addClass("connectable");
256
            }
257

    
258
            if (vm.get('status') == 'BUILD') {
259
                // update bulding progress
260
                el.find("span.build-progress").show().text(vm.get("progress_message"));
261
            } else {
262
                // hide building progress
263
                el.find("span.build-progress").hide();
264
            }
265

    
266
            if (vm.state() == "DESTROY") {
267
                el.find("span.build-progress").show().text("Terminating...");
268
            }
269

    
270
            icon_state = vm.is_active() ? "on" : "off";
271
            set_machine_os_image(el, "single", icon_state, this.get_vm_icon_os(vm));
272
            
273
            // update subviews
274
            this.action_views[vm.id].update_layout();
275
            this.stats_views[vm.id].update_layout();
276
            this.connect_views[vm.id].update_layout();
277
            this.tags_views[vm.id].update_layout();
278
            this.details_views[vm.id].update_layout();
279
        },
280
            
281
        get_vm_icon_os: function(vm) {
282
            var os = vm.get_os();
283
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
284
            if (icons.indexOf(os) == -1) {
285
                os = "unknown";
286
            }
287
            return os;
288
        },
289

    
290
        // TODO: move to views.utils (the method and the VM_OS_ICON vars)
291
        get_vm_icon_path: function(vm, icon_type) {
292
            var os = vm.get_os();
293
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
294

    
295
            if (icons.indexOf(os) == -1) {
296
                os = "unknown";
297
            }
298

    
299
            return views.SingleView.VM_OS_ICON_TPLS[icon_type].format(os);
300
        }
301
    })
302

    
303
    views.SingleView.VM_OS_ICON_TPLS = {
304
        "medium": "/static/icons/machines/large/{0}-sprite.png"
305
    }
306

    
307
    views.SingleView.VM_OS_ICONS = window.os_icons || [];
308

    
309
    views.SingleView.STATE_CLASSES = {
310
        'UNKNOWN':          ['state', 'error-state'],
311
        'BUILD':            ['state', 'build-state'],
312
        'REBOOT':           ['state', 'rebooting-state'],
313
        'STOPPED':          ['state', 'terminated-state'],
314
        'ACTIVE':           ['state', 'running-state'],
315
        'ERROR':            ['state', 'error-state'],
316
        'DELETE':           ['state', 'destroying-state'],
317
        'DESTROY':          ['state', 'destroying-state'],
318
        'BUILD_INIT':       ['state', 'build-state'], 
319
        'BUILD_COPY':       ['state', 'build-state'],
320
        'BUILD_FINAL':      ['state', 'build-state'],
321
        'SHUTDOWN':         ['state', 'shutting-state'],
322
        'START':            ['state', 'starting-state'],
323
        'CONNECT':          ['state', 'connecting-state'],
324
        'DISCONNECT':       ['state', 'disconnecting-state']
325
    };
326

    
327
})(this);