Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_single_view.js @ 8d7e1e38

History | View | Annotate | Download (11 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-{0}',
42
        link_id_tpl: 'single-vm-at-{0}',
43

    
44
        hide_actions: false,
45

    
46
        selectors: {
47
            'vms': '.single-container',
48
            'vm': '.single-container#single-vm-{0}',
49
            'view': '#machinesview-single',
50
            'tpl': '.single-container#machine-container-template',
51
            'spinner': '.large-spinner',
52
            'vm_spinner': '.single-container#single-vm-{0} .state .spinner',
53
            'vm_wave': '.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
            this.update_current_vm();
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
        },
69

    
70
        // overload show function
71
        show_view: function() {
72
            this.log.debug("showing");
73
            this.$(".column3").show();
74
        },
75

    
76
        show_vm: function(vm) {
77
            this.current_vm_instance = vm;
78
            this.show_vm_menu();
79
            this.show_current();
80
            this.update_layout();
81
        },
82

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

    
96
            this.next_button.click(_.bind(function(ev){
97
                storage.vms.reset_pending_actions();
98
                ev.preventDefault();
99
                this.show_next();
100
            }, this));
101
        },  
102

    
103
        update_current_vm: function() {
104
            try {
105
                this.current_vm_instance = storage.vms.at(this.current_vm);
106
            } catch (err) {
107
                this.log.debug("Cannot select current vm instance for: {0}".format(this.current_vm));
108
            }
109
        },
110

    
111
        show_next: function() {
112
            this.current_vm++;
113
            if (this.current_vm >= storage.vms.length) {
114
                this.current_vm = 0;
115
            }
116
            this.update_current_vm();
117
            this.__update_layout();
118
        },
119

    
120
        show_prev: function() {
121
            this.current_vm--;
122
            if (this.current_vm < 0) {
123
                this.current_vm = storage.vms.length - 1;
124
            }
125
            this.update_current_vm();
126
            this.__update_layout();
127
        },
128

    
129
        post_remove_vm: function(vm) {
130
            // current vm removed or does not exist after an update
131
            this.show_vm_menu();
132
            if (!this.current_vm_instance || this.current_vm_instance.id == vm.id) {
133
                this.show_next();
134
            } else {
135
                this.show_current();
136
            }
137
        },
138
        
139
        // stuff to do when a new vm has been created.
140
        // - create vm subviews
141
        post_add: function(vm) {
142
            // rename views index
143
            this.stats_views = this.stats_views || {};
144
            this.connect_views = this.connect_views || {};
145
            this.tags_views = this.tags_views || {};
146
            this.details_views = this.details_views || {};
147
            this.action_views = this.action_views || {};
148

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

    
151
            // same as icon view
152
            this.action_views[vm.id] = new views.VMActionsView(vm, this, this.vm(vm), this.hide_actions);
153
            this.stats_views[vm.id] = new views.VMStatsView(vm, this, {stats_type: 'series'});
154
            this.connect_views[vm.id] = new views.VMConnectView(vm, this);
155
            this.tags_views[vm.id] = new views.VMTagsView(vm, this, true, 20, 10, 35);
156
            this.details_views[vm.id] = new views.SingleDetailsView(vm, this);
157
        },
158

    
159
        post_update_vm: function(vm) {
160
            vm.enable_stats_update();
161
        },
162
        
163
        // vm specific event handlers
164
        set_vm_handlers: function(vm) {
165
            var el = this.vm(vm);
166
        },
167
        
168
        // handle selected vm
169
        show_current: function() {
170
            var index = this.current_vm;
171
            
172
            this.$(".server-name").removeClass("column3-selected");
173
            
174
            _.each(storage.vms.models, function(vm){
175
                this.vm(vm).hide();
176
            }, this)
177

    
178
            vm = storage.vms.at(index);
179
            if (!vm) {
180
                // empty list
181
                this.$(".column3").hide();
182
                return;
183
            }
184
            this.$(".column3").show();
185

    
186
            if (vm) {
187
                this.vm(vm).show();
188
            };
189

    
190
            $("#" + this.link_id_tpl.format(this.current_vm)).addClass("column3-selected");
191
        },
192

    
193
        show_vm_menu: function() {
194
            this.menu.find(".server-name").remove();
195

    
196
            _.each(storage.vms.models, function(vm, index) {
197
                this.menu.append('<div class="server-name" id="'+this.link_id_tpl.format(index)+'">' + 
198
                               util.truncate(vm.get("name"),16)+'</div>');
199
                if (this.current_vm_instance && vm.id == this.current_vm_instance.id) {
200
                    this.current_vm = index;
201
                }
202
            }, this);
203
            
204
            var self = this;
205
            this.menu.find(".server-name").click(function(ev) {
206
                storage.vms.reset_pending_actions();
207

    
208
                ev.preventDefault();
209
                var id = $(this).attr("id").replace("single-vm-at-", "");
210
                self.current_vm = id;
211
                self.update_current_vm();
212
                self.show_current();
213
            })
214
        },
215

    
216
        // generic stuff to do on each view update
217
        // called once after each vm has been updated
218
        update_layout: function() {
219
            this.update_current_vm();
220
            this.show_vm_menu();
221
            this.show_current();
222
            fix_v6_addresses();
223
        },
224

    
225
        // update vm details
226
        update_details: function(vm) {
227
            var el = this.vm(vm);
228
            // truncate name
229
            el.find(".machine-detail.name").text(util.truncate(vm.get("name"), 35));
230
            // set ips
231
            el.find(".machine-detail.ipv4.ipv4-text").text(vm.get_addresses().ip4 || "undefined");
232
            // TODO: fix ipv6 truncates and tooltip handler
233
            el.find(".machine-detail.ipv6.ipv6-text").text(vm.get_addresses().ip6 || "undefined");
234
            // set the state (i18n ??)
235
            el.find(".state-label").text(STATE_TEXTS[vm.state()]);
236
            // set state class
237
            el.find(".state").removeClass().addClass(views.SingleView.STATE_CLASSES[vm.state()].join(" "));
238
            // os icon
239
            el.find(".single-image").css({'background-image': "url(" + this.get_vm_icon_path(vm, "medium") + ")"});
240
            
241
            el.removeClass("connectable");
242
            if (vm.is_connectable()) {
243
                el.addClass("connectable");
244
            }
245

    
246
            if (vm.get('status') == 'BUILD') {
247
                // update bulding progress
248
                var progress_details = get_progress_details(vm.id);
249
                el.find("span.build-progress").show().text(progress_details.msg);
250
            } else {
251
                // hide building progress
252
                el.find("span.build-progress").hide();
253
            }
254

    
255
            if (vm.state() == "DESTROY") {
256
                el.find("span.build-progress").show().text("Terminating...");
257
            }
258

    
259
            icon_state = vm.is_active() ? "on" : "off";
260
            set_machine_os_image(el, "single", icon_state, this.get_vm_icon_os(vm));
261
            
262
            // update subviews
263
            this.action_views[vm.id].update_layout();
264
            this.stats_views[vm.id].update_layout();
265
            this.connect_views[vm.id].update_layout();
266
            this.tags_views[vm.id].update_layout();
267
            this.details_views[vm.id].update_layout();
268
        },
269
            
270
        get_vm_icon_os: function(vm) {
271
            var os = vm.get_os();
272
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
273
            if (icons.indexOf(os) == -1) {
274
                os = "unknown";
275
            }
276
            return os;
277
        },
278

    
279
        // TODO: move to views.utils (the method and the VM_OS_ICON vars)
280
        get_vm_icon_path: function(vm, icon_type) {
281
            var os = vm.get_os();
282
            var icons = window.os_icons || views.SingleView.VM_OS_ICONS;
283

    
284
            if (icons.indexOf(os) == -1) {
285
                os = "unknown";
286
            }
287

    
288
            return views.SingleView.VM_OS_ICON_TPLS[icon_type].format(os);
289
        }
290
    })
291

    
292
    views.SingleView.VM_OS_ICON_TPLS = {
293
        "medium": "/static/icons/machines/large/{0}-sprite.png"
294
    }
295

    
296
    views.SingleView.VM_OS_ICONS = window.os_icons || [];
297

    
298
    views.SingleView.STATE_CLASSES = {
299
        'UNKNOWN':          ['state', 'error-state'],
300
        'BUILD':            ['state', 'build-state'],
301
        'REBOOT':           ['state', 'rebooting-state'],
302
        'STOPPED':          ['state', 'terminated-state'],
303
        'ACTIVE':           ['state', 'running-state'],
304
        'ERROR':            ['state', 'error-state'],
305
        'DELETE':           ['state', 'destroying-state'],
306
        'DESTROY':          ['state', 'destroying-state'],
307
        'BUILD_INIT':       ['state', 'build-state'], 
308
        'BUILD_COPY':       ['state', 'build-state'],
309
        'BUILD_FINAL':      ['state', 'build-state'],
310
        'SHUTDOWN':         ['state', 'shutting-state'],
311
        'START':            ['state', 'starting-state'],
312
        'CONNECT':          ['state', 'connecting-state'],
313
        'DISCONNECT':       ['state', 'disconnecting-state']
314
    };
315

    
316
})(this);