Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.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
    
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
                ev.preventDefault();
92
                this.show_prev();
93
            }, this));
94

    
95
            this.next_button.click(_.bind(function(ev){
96
                ev.preventDefault();
97
                this.show_next();
98
            }, this));
99
        },  
100

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

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

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

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

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

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

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

    
176
            vm = storage.vms.at(index);
177
            if (!vm) {
178
                // empty list
179
                return;
180
            }
181

    
182
            if (vm) {
183
                this.vm(vm).show();
184
            };
185

    
186
            $("#" + this.link_id_tpl.format(this.current_vm)).addClass("column3-selected");
187

    
188
            //if (this.vm(vm).find(".column3").length == 0) {
189
                //this.$(".column3").appendTo(this.vm(vm).find(".vms-menu-cont"));
190
            //}
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
                ev.preventDefault();
208
                var id = $(this).attr("id").replace("single-vm-at-", "");
209
                self.current_vm = id;
210
                self.update_current_vm();
211
                self.show_current();
212
            })
213
        },
214

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

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

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

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

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

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

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

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

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

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

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

    
315
})(this);