Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_main_view.js @ 2d485d37

History | View | Annotate | Download (27 kB)

1 8d08f18a Kostas Papadimitriou
;(function(root){
2 8d08f18a Kostas Papadimitriou
3 8d08f18a Kostas Papadimitriou
    // root
4 8d08f18a Kostas Papadimitriou
    var root = root;
5 8d08f18a Kostas Papadimitriou
    
6 8d08f18a Kostas Papadimitriou
    // setup namepsaces
7 8d08f18a Kostas Papadimitriou
    var snf = root.synnefo = root.synnefo || {};
8 8d08f18a Kostas Papadimitriou
    var models = snf.models = snf.models || {}
9 8d08f18a Kostas Papadimitriou
    var storage = snf.storage = snf.storage || {};
10 8d08f18a Kostas Papadimitriou
    var ui = snf.ui = snf.ui || {};
11 8d08f18a Kostas Papadimitriou
12 8d08f18a Kostas Papadimitriou
    var views = snf.views = snf.views || {}
13 8d08f18a Kostas Papadimitriou
14 8d08f18a Kostas Papadimitriou
    // shortcuts
15 8d08f18a Kostas Papadimitriou
    var bb = root.Backbone;
16 8d08f18a Kostas Papadimitriou
    var util = snf.util;
17 8d08f18a Kostas Papadimitriou
    
18 dc020cf6 Kostas Papadimitriou
    // TODO: implement me
19 dc020cf6 Kostas Papadimitriou
    views.NoticeView = views.Overlay.extend({});
20 8d08f18a Kostas Papadimitriou
21 8d08f18a Kostas Papadimitriou
    views.MultipleActionsView = views.View.extend({
22 8d08f18a Kostas Papadimitriou
        view_id: "multiple_actions",
23 8d08f18a Kostas Papadimitriou
24 8d08f18a Kostas Papadimitriou
        _actions: {},
25 8d08f18a Kostas Papadimitriou
        el: '#multiple_actions_container',
26 8d08f18a Kostas Papadimitriou
        
27 8d08f18a Kostas Papadimitriou
        initialize: function() {
28 8d08f18a Kostas Papadimitriou
            this.actions = {};
29 8d08f18a Kostas Papadimitriou
            views.MultipleActionsView.__super__.initialize.call(this);
30 122850c5 Kostas Papadimitriou
            
31 122850c5 Kostas Papadimitriou
            // view elements
32 122850c5 Kostas Papadimitriou
            this.confirm_actions = this.$(".confirm_multiple_actions");
33 122850c5 Kostas Papadimitriou
            this.confirm_actions_yes = this.$(".confirm_multiple_actions button.yes");
34 122850c5 Kostas Papadimitriou
            this.confirm_actions_no = this.$(".confirm_multiple_actions button.no");
35 122850c5 Kostas Papadimitriou
            this.confirm_reboot = this.$(".confirm_reboot_required");
36 122850c5 Kostas Papadimitriou
            this.confirm_reboot_yes = this.$(".confirm_reboot_required button.yes");
37 122850c5 Kostas Papadimitriou
            this.confirm_reboot_no = this.$(".confirm_reboot_required button.no");
38 122850c5 Kostas Papadimitriou
            this.confirm_reboot_list = this.confirm_reboot.find(".reboot-machines-list");
39 122850c5 Kostas Papadimitriou
40 8d08f18a Kostas Papadimitriou
            this.init_handlers();
41 8d08f18a Kostas Papadimitriou
            this.update_layout();
42 8d08f18a Kostas Papadimitriou
            
43 8d08f18a Kostas Papadimitriou
            // for heavy resize/scroll window events
44 8d08f18a Kostas Papadimitriou
            // do it `like a boss` 
45 122850c5 Kostas Papadimitriou
            this.fix_position = _.throttle(this.fix_position, 100);
46 2506f741 Kostas Papadimitriou
            this.update_layout = _.throttle(this.update_layout, 100);
47 8d08f18a Kostas Papadimitriou
            this.show_limit = 1;
48 8d08f18a Kostas Papadimitriou
        },
49 8d08f18a Kostas Papadimitriou
50 8d08f18a Kostas Papadimitriou
        init_handlers: function() {
51 122850c5 Kostas Papadimitriou
            var self = this;
52 122850c5 Kostas Papadimitriou
53 8d08f18a Kostas Papadimitriou
            $(window).resize(_.bind(function(){
54 8d08f18a Kostas Papadimitriou
                this.fix_position();
55 8d08f18a Kostas Papadimitriou
            }, this));
56 122850c5 Kostas Papadimitriou
57 8d08f18a Kostas Papadimitriou
            $(window).scroll(_.bind(function(){
58 8d08f18a Kostas Papadimitriou
                this.fix_position();
59 8d08f18a Kostas Papadimitriou
            }, this));
60 122850c5 Kostas Papadimitriou
            
61 122850c5 Kostas Papadimitriou
            // confirm/cancel button handlers
62 8d08f18a Kostas Papadimitriou
            var self = this;
63 122850c5 Kostas Papadimitriou
            this.confirm_actions_yes.click(function(){ self.do_all(); })
64 122850c5 Kostas Papadimitriou
            this.confirm_actions_no.click(function(){
65 122850c5 Kostas Papadimitriou
                self.reset_actions();
66 8d08f18a Kostas Papadimitriou
            });
67 8d08f18a Kostas Papadimitriou
68 122850c5 Kostas Papadimitriou
            this.confirm_reboot_yes.click(function(){ self.do_reboot_all(); })
69 122850c5 Kostas Papadimitriou
            this.confirm_reboot_no.click(function(){
70 122850c5 Kostas Papadimitriou
                self.reset_reboots();
71 122850c5 Kostas Papadimitriou
            });
72 122850c5 Kostas Papadimitriou
73 122850c5 Kostas Papadimitriou
            storage.vms.bind("change:pending_action", _.bind(this.handle_vm_change, this));
74 122850c5 Kostas Papadimitriou
            storage.vms.bind("change:reboot_required", _.bind(this.handle_vm_change, this));
75 b1e6a2de Kostas Papadimitriou
76 8d08f18a Kostas Papadimitriou
        },
77 b1e6a2de Kostas Papadimitriou
78 122850c5 Kostas Papadimitriou
        handle_vm_change: function(vm) {
79 8d08f18a Kostas Papadimitriou
            if (vm.has_pending_action()) {
80 8d08f18a Kostas Papadimitriou
                var action = vm.get("pending_action");
81 122850c5 Kostas Papadimitriou
                this.add_action(vm, action);
82 8d08f18a Kostas Papadimitriou
            } else {
83 8d08f18a Kostas Papadimitriou
                this.remove_action(vm);
84 8d08f18a Kostas Papadimitriou
            }
85 122850c5 Kostas Papadimitriou
            this.update_layout();
86 8d08f18a Kostas Papadimitriou
        },
87 8d08f18a Kostas Papadimitriou
88 122850c5 Kostas Papadimitriou
        add_action: function(vm, action) {
89 122850c5 Kostas Papadimitriou
            this._actions[vm.id] = {'vm': vm, 'action': action};
90 8d08f18a Kostas Papadimitriou
        },
91 8d08f18a Kostas Papadimitriou
92 8d08f18a Kostas Papadimitriou
        remove_action: function(vm) {
93 8d08f18a Kostas Papadimitriou
            delete this._actions[vm.id];
94 8d08f18a Kostas Papadimitriou
        },
95 8d08f18a Kostas Papadimitriou
96 8d08f18a Kostas Papadimitriou
        reset: function() {
97 8d08f18a Kostas Papadimitriou
            this._actions = {};
98 8d08f18a Kostas Papadimitriou
            this.update_layout();
99 8d08f18a Kostas Papadimitriou
        },
100 122850c5 Kostas Papadimitriou
        
101 122850c5 Kostas Papadimitriou
        reboot_vm: function(vm) {
102 122850c5 Kostas Papadimitriou
            vm.call("reboot");
103 122850c5 Kostas Papadimitriou
        },
104 122850c5 Kostas Papadimitriou
105 122850c5 Kostas Papadimitriou
        do_reboot_all: function() {
106 122850c5 Kostas Papadimitriou
            _.each(storage.vms.get_reboot_required(), function(vm){
107 122850c5 Kostas Papadimitriou
                this.reboot_vm(vm)
108 122850c5 Kostas Papadimitriou
            }, this)  
109 122850c5 Kostas Papadimitriou
        },
110 8d08f18a Kostas Papadimitriou
111 8d08f18a Kostas Papadimitriou
        do_all: function() {
112 8d08f18a Kostas Papadimitriou
            _.each(this._actions, function(action){
113 8d08f18a Kostas Papadimitriou
                action.vm.call(action.action);
114 8d08f18a Kostas Papadimitriou
            }, this)  
115 122850c5 Kostas Papadimitriou
            this.reset_actions();
116 8d08f18a Kostas Papadimitriou
        },
117 8d08f18a Kostas Papadimitriou
118 122850c5 Kostas Papadimitriou
        reset_reboots: function () {
119 122850c5 Kostas Papadimitriou
            _.each(storage.vms.get_reboot_required(), function(vm) {vm.set({'reboot_required': false})}, this);
120 122850c5 Kostas Papadimitriou
            this.update_layout();
121 8d08f18a Kostas Papadimitriou
        },
122 8d08f18a Kostas Papadimitriou
123 122850c5 Kostas Papadimitriou
        reset_actions: function() {
124 122850c5 Kostas Papadimitriou
            _.each(this._actions, _.bind(function(action){
125 122850c5 Kostas Papadimitriou
                try {
126 122850c5 Kostas Papadimitriou
                    action.vm.clear_pending_action();
127 122850c5 Kostas Papadimitriou
                    this.remove_action(action.vm);
128 122850c5 Kostas Papadimitriou
                } catch(err) {
129 122850c5 Kostas Papadimitriou
                    console.error("vm " + action.vm.id + " failed to reset", err);
130 122850c5 Kostas Papadimitriou
                }
131 122850c5 Kostas Papadimitriou
            }, this))  
132 8d08f18a Kostas Papadimitriou
        },
133 8d08f18a Kostas Papadimitriou
        
134 8d08f18a Kostas Papadimitriou
        fix_position: function() {
135 8d08f18a Kostas Papadimitriou
            $('.confirm_multiple').removeClass('fixed');
136 8d08f18a Kostas Papadimitriou
            if (($(this.el).offset().top +$(this.el).height())> ($(window).scrollTop() + $(window).height())) {
137 8d08f18a Kostas Papadimitriou
                $('.confirm_multiple').addClass('fixed');
138 8d08f18a Kostas Papadimitriou
            }
139 8d08f18a Kostas Papadimitriou
        },
140 8d08f18a Kostas Papadimitriou
        
141 122850c5 Kostas Papadimitriou
        check_notify_limit: function() {
142 122850c5 Kostas Papadimitriou
            this.show_limit = 1;
143 122850c5 Kostas Papadimitriou
            if (ui.main.current_view && ['networks', 'vm_list'].indexOf(ui.main.current_view.view_id) > -1) {
144 122850c5 Kostas Papadimitriou
                this.show_limit = 0;
145 122850c5 Kostas Papadimitriou
            }
146 8d08f18a Kostas Papadimitriou
        },
147 122850c5 Kostas Papadimitriou
        
148 122850c5 Kostas Papadimitriou
        update_reboot_required_list: function(vms) {
149 122850c5 Kostas Papadimitriou
            this.confirm_reboot_list.empty();
150 8d08f18a Kostas Papadimitriou
        },
151 8d08f18a Kostas Papadimitriou
152 122850c5 Kostas Papadimitriou
        update_reboot_required: function() {
153 122850c5 Kostas Papadimitriou
            var vms = storage.vms.get_reboot_required();
154 122850c5 Kostas Papadimitriou
            if (vms.length) {
155 122850c5 Kostas Papadimitriou
                this.confirm_reboot.find(".actionLen").text(vms.length);
156 122850c5 Kostas Papadimitriou
                this.update_reboot_required_list();
157 122850c5 Kostas Papadimitriou
                this.confirm_reboot.show();
158 122850c5 Kostas Papadimitriou
                $(this.el).show();
159 122850c5 Kostas Papadimitriou
            } else {
160 122850c5 Kostas Papadimitriou
                if (!this.actions_visible) {
161 122850c5 Kostas Papadimitriou
                   $(this.el).hide();
162 8d08f18a Kostas Papadimitriou
                }
163 122850c5 Kostas Papadimitriou
                this.confirm_reboot.hide();
164 122850c5 Kostas Papadimitriou
            }
165 8d08f18a Kostas Papadimitriou
        },
166 8d08f18a Kostas Papadimitriou
167 8d08f18a Kostas Papadimitriou
        update_layout: function() {
168 122850c5 Kostas Papadimitriou
            this.check_notify_limit();
169 122850c5 Kostas Papadimitriou
            this.actions_visible = false;
170 122850c5 Kostas Papadimitriou
171 8d08f18a Kostas Papadimitriou
            if (_.size(this._actions) > this.show_limit) {
172 122850c5 Kostas Papadimitriou
                this.actions_visible = true;
173 8d08f18a Kostas Papadimitriou
                $(this.el).show();
174 122850c5 Kostas Papadimitriou
                this.confirm_actions.show();
175 8d08f18a Kostas Papadimitriou
            } else {
176 8d08f18a Kostas Papadimitriou
                $(this.el).hide();
177 122850c5 Kostas Papadimitriou
                this.confirm_actions.hide();
178 8d08f18a Kostas Papadimitriou
            }
179 122850c5 Kostas Papadimitriou
180 122850c5 Kostas Papadimitriou
            this.update_reboot_required();
181 122850c5 Kostas Papadimitriou
            this.confirm_actions.find(".actionLen").text(_.size(this._actions));
182 8d08f18a Kostas Papadimitriou
            $(window).trigger("resize");
183 8d08f18a Kostas Papadimitriou
        }
184 8d08f18a Kostas Papadimitriou
    })
185 8d08f18a Kostas Papadimitriou
    
186 8d08f18a Kostas Papadimitriou
    // menu wrapper view
187 8d08f18a Kostas Papadimitriou
    views.SelectView = views.View.extend({
188 8d08f18a Kostas Papadimitriou
        
189 8d08f18a Kostas Papadimitriou
        initialize: function(view) {
190 8d08f18a Kostas Papadimitriou
            this.parent = view;
191 8d08f18a Kostas Papadimitriou
192 8d08f18a Kostas Papadimitriou
            this.pane_view_selector = $(".css-tabs");
193 8d08f18a Kostas Papadimitriou
            this.machine_view_selector = $("#view-select");
194 8d08f18a Kostas Papadimitriou
            this.el = $(".css-tabs");
195 8d08f18a Kostas Papadimitriou
            this.title = $(".tab-name");
196 8d08f18a Kostas Papadimitriou
197 8d08f18a Kostas Papadimitriou
            this.set_handlers();
198 8d08f18a Kostas Papadimitriou
            this.update_layout();
199 8d08f18a Kostas Papadimitriou
200 8d08f18a Kostas Papadimitriou
            views.SelectView.__super__.initialize.apply(this, arguments);
201 8d08f18a Kostas Papadimitriou
        },
202 8d08f18a Kostas Papadimitriou
        
203 8d08f18a Kostas Papadimitriou
        clear_active: function() {
204 8d08f18a Kostas Papadimitriou
            this.pane_view_selector.find("a").removeClass("active");
205 8d08f18a Kostas Papadimitriou
            this.machine_view_selector.find("a").removeClass("activelink");
206 8d08f18a Kostas Papadimitriou
        },
207 8d08f18a Kostas Papadimitriou
        
208 8d08f18a Kostas Papadimitriou
        // intercept menu links
209 8d08f18a Kostas Papadimitriou
        set_handlers: function() {
210 8d08f18a Kostas Papadimitriou
            var self = this;
211 8d08f18a Kostas Papadimitriou
            this.pane_view_selector.find("a").hover(function(){
212 8d08f18a Kostas Papadimitriou
                // FIXME: title from href ? omg
213 8d08f18a Kostas Papadimitriou
                self.title.text($(this).attr("href"));
214 8d08f18a Kostas Papadimitriou
            }, function(){
215 8d08f18a Kostas Papadimitriou
                self.title.text(self.parent.get_title());
216 8d08f18a Kostas Papadimitriou
            });
217 8d08f18a Kostas Papadimitriou
218 8d08f18a Kostas Papadimitriou
            this.pane_view_selector.find("a#machines_view_link").click(_.bind(function(ev){
219 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
220 8d08f18a Kostas Papadimitriou
                this.parent.show_view("machines");
221 8d08f18a Kostas Papadimitriou
            }, this))
222 8d08f18a Kostas Papadimitriou
            this.pane_view_selector.find("a#networks_view_link").click(_.bind(function(ev){
223 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
224 8d08f18a Kostas Papadimitriou
                this.parent.show_view("networks");
225 8d08f18a Kostas Papadimitriou
            }, this))
226 8d08f18a Kostas Papadimitriou
            this.pane_view_selector.find("a#disks_view_link").click(_.bind(function(ev){
227 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
228 8d08f18a Kostas Papadimitriou
                this.parent.show_view("disks");
229 8d08f18a Kostas Papadimitriou
            }, this))
230 8d08f18a Kostas Papadimitriou
            
231 8d08f18a Kostas Papadimitriou
            this.machine_view_selector.find("a#machines_view_icon_link").click(_.bind(function(ev){
232 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
233 8d08f18a Kostas Papadimitriou
                var d = $.now();
234 8d08f18a Kostas Papadimitriou
                this.parent.show_view("icon");
235 8d08f18a Kostas Papadimitriou
            }, this))
236 8d08f18a Kostas Papadimitriou
            this.machine_view_selector.find("a#machines_view_list_link").click(_.bind(function(ev){
237 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
238 8d08f18a Kostas Papadimitriou
                this.parent.show_view("list");
239 8d08f18a Kostas Papadimitriou
            }, this))
240 8d08f18a Kostas Papadimitriou
            this.machine_view_selector.find("a#machines_view_single_link").click(_.bind(function(ev){
241 8d08f18a Kostas Papadimitriou
                ev.preventDefault();
242 8d08f18a Kostas Papadimitriou
                this.parent.show_view("single");
243 8d08f18a Kostas Papadimitriou
            }, this))
244 8d08f18a Kostas Papadimitriou
        },
245 8d08f18a Kostas Papadimitriou
246 8d08f18a Kostas Papadimitriou
        update_layout: function() {
247 8d08f18a Kostas Papadimitriou
            this.clear_active();
248 8d08f18a Kostas Papadimitriou
249 8d08f18a Kostas Papadimitriou
            var pane_index = this.parent.pane_ids[this.parent.current_view_id];
250 8d08f18a Kostas Papadimitriou
            $(this.pane_view_selector.find("a")).removeClass("active");
251 8d08f18a Kostas Papadimitriou
            $(this.pane_view_selector.find("a").get(pane_index)).addClass("active");
252 8d08f18a Kostas Papadimitriou
            
253 8d08f18a Kostas Papadimitriou
            if (this.parent.current_view && this.parent.current_view.vms_view) {
254 8d08f18a Kostas Papadimitriou
255 8d08f18a Kostas Papadimitriou
                if (storage.vms.length > 0) {
256 8d08f18a Kostas Papadimitriou
                    this.machine_view_selector.show();
257 8d08f18a Kostas Papadimitriou
                    var machine_index = this.parent.views_ids[this.parent.current_view_id];
258 8d08f18a Kostas Papadimitriou
                    $(this.machine_view_selector.find("a").get(machine_index)).addClass("activelink");
259 8d08f18a Kostas Papadimitriou
                } else {
260 8d08f18a Kostas Papadimitriou
                    this.machine_view_selector.hide();
261 8d08f18a Kostas Papadimitriou
                }
262 8d08f18a Kostas Papadimitriou
            } else {
263 8d08f18a Kostas Papadimitriou
                this.machine_view_selector.hide();
264 8d08f18a Kostas Papadimitriou
            }
265 8d08f18a Kostas Papadimitriou
266 8d08f18a Kostas Papadimitriou
        }
267 8d08f18a Kostas Papadimitriou
    });
268 8d08f18a Kostas Papadimitriou
269 8d08f18a Kostas Papadimitriou
    views.MainView = views.View.extend({
270 8d08f18a Kostas Papadimitriou
        el: 'body',
271 8d08f18a Kostas Papadimitriou
        view_id: 'main',
272 8d08f18a Kostas Papadimitriou
        
273 8d08f18a Kostas Papadimitriou
        // FIXME: titles belong to SelectView
274 8d08f18a Kostas Papadimitriou
        views_titles: {
275 8d08f18a Kostas Papadimitriou
            'icon': 'machines', 'single': 'machines', 
276 8d08f18a Kostas Papadimitriou
            'list': 'machines', 'networks': 'networks',
277 8d08f18a Kostas Papadimitriou
            'disks': 'disks'
278 8d08f18a Kostas Papadimitriou
        },
279 8d08f18a Kostas Papadimitriou
280 8d08f18a Kostas Papadimitriou
        // indexes registry
281 8d08f18a Kostas Papadimitriou
        views_indexes: {0: 'icon', 2:'single', 1: 'list', 3:'networks'},
282 8d08f18a Kostas Papadimitriou
        views_pane_indexes: {0:'single', 1:'networks', 2:'disks'},
283 8d08f18a Kostas Papadimitriou
284 8d08f18a Kostas Papadimitriou
        // views classes registry
285 8d08f18a Kostas Papadimitriou
        views_classes: {'icon': views.IconView, 'single': views.SingleView, 
286 8d08f18a Kostas Papadimitriou
            'list': views.ListView, 'networks': views.NetworksView},
287 8d08f18a Kostas Papadimitriou
288 8d08f18a Kostas Papadimitriou
        // view ids
289 8d08f18a Kostas Papadimitriou
        views_ids: {'icon':0, 'single':2, 'list':1, 'networks':3},
290 8d08f18a Kostas Papadimitriou
291 8d08f18a Kostas Papadimitriou
        // on which pane id each view exists
292 8d08f18a Kostas Papadimitriou
        // machine views (icon,single,list) are all on first pane
293 8d08f18a Kostas Papadimitriou
        pane_ids: {'icon':0, 'single':0, 'list':0, 'networks':1, 'disks':2},
294 8d08f18a Kostas Papadimitriou
    
295 8d08f18a Kostas Papadimitriou
        initialize: function(show_view) {
296 8d08f18a Kostas Papadimitriou
            if (!show_view) { show_view = 'icon' };
297 8d08f18a Kostas Papadimitriou
            
298 8d08f18a Kostas Papadimitriou
            // fallback to browser error reporting (true for debug)
299 8d08f18a Kostas Papadimitriou
            this.skip_errors = true
300 8d08f18a Kostas Papadimitriou
301 8d08f18a Kostas Papadimitriou
            // reset views
302 8d08f18a Kostas Papadimitriou
            this.views = {};
303 8d08f18a Kostas Papadimitriou
304 23a3bb8e Kostas Papadimitriou
            this.el = $("#app");
305 8d08f18a Kostas Papadimitriou
            // reset main view status
306 8d08f18a Kostas Papadimitriou
            this._loaded = false;
307 8d08f18a Kostas Papadimitriou
            this.status = "Initializing...";
308 8d08f18a Kostas Papadimitriou
309 8d08f18a Kostas Papadimitriou
            // initialize handlers
310 8d08f18a Kostas Papadimitriou
            this.init_handlers();
311 8d08f18a Kostas Papadimitriou
312 8d08f18a Kostas Papadimitriou
            // identify initial view from user cookies
313 8d08f18a Kostas Papadimitriou
            // this view will be visible after loading of
314 8d08f18a Kostas Papadimitriou
            // main view
315 8d08f18a Kostas Papadimitriou
            this.initial_view = this.session_view();
316 8d08f18a Kostas Papadimitriou
317 8d08f18a Kostas Papadimitriou
            views.MainView.__super__.initialize.call(this);
318 b1e6a2de Kostas Papadimitriou
319 b1e6a2de Kostas Papadimitriou
            $(window).focus(_.bind(this.handle_window_focus, this, "focus"));
320 b1e6a2de Kostas Papadimitriou
            $(window).blur(_.bind(this.handle_window_focus, this, "out"));
321 b1e6a2de Kostas Papadimitriou
322 b1e6a2de Kostas Papadimitriou
            this.focused = true;
323 b1e6a2de Kostas Papadimitriou
        },
324 b1e6a2de Kostas Papadimitriou
325 b1e6a2de Kostas Papadimitriou
        handle_window_focus: function(focus) {
326 b1e6a2de Kostas Papadimitriou
            if (!snf.config.delay_on_blur) { return };
327 b1e6a2de Kostas Papadimitriou
328 b1e6a2de Kostas Papadimitriou
            if (focus === "focus") {
329 b1e6a2de Kostas Papadimitriou
                this.focused = true;
330 b1e6a2de Kostas Papadimitriou
                this.set_interval_timeouts(snf.config.update_interval);
331 b1e6a2de Kostas Papadimitriou
            } else {
332 b1e6a2de Kostas Papadimitriou
                this.focused = false;
333 66be390b Kostas Papadimitriou
                this.set_interval_timeouts(snf.config.blur_delay);
334 b1e6a2de Kostas Papadimitriou
            }
335 b1e6a2de Kostas Papadimitriou
        },
336 b1e6a2de Kostas Papadimitriou
337 b1e6a2de Kostas Papadimitriou
        set_interval_timeouts: function(time) {
338 b1e6a2de Kostas Papadimitriou
            _.each([this._networks, this._vms], function(fetcher){
339 b1e6a2de Kostas Papadimitriou
                if (!fetcher) { return };
340 b1e6a2de Kostas Papadimitriou
                fetcher.timeout = time;
341 b1e6a2de Kostas Papadimitriou
                fetcher.stop().start();
342 b1e6a2de Kostas Papadimitriou
            })
343 8d08f18a Kostas Papadimitriou
        },
344 8d08f18a Kostas Papadimitriou
        
345 8d08f18a Kostas Papadimitriou
        vms_handlers_registered: false,
346 8d08f18a Kostas Papadimitriou
347 8d08f18a Kostas Papadimitriou
        // register event handlers
348 8d08f18a Kostas Papadimitriou
        // 
349 8d08f18a Kostas Papadimitriou
        // vms storage events to identify if vms list 
350 8d08f18a Kostas Papadimitriou
        // is empty and display empty view if user viewing
351 8d08f18a Kostas Papadimitriou
        // a machine view
352 8d08f18a Kostas Papadimitriou
        //
353 8d08f18a Kostas Papadimitriou
        // api/ui error event handlers
354 8d08f18a Kostas Papadimitriou
        init_handlers: function() {
355 8d08f18a Kostas Papadimitriou
            // vm handlers
356 8d08f18a Kostas Papadimitriou
            storage.vms.bind("remove", _.bind(this.check_empty, this));
357 8d08f18a Kostas Papadimitriou
            storage.vms.bind("add", _.bind(this.check_empty, this));
358 8d08f18a Kostas Papadimitriou
            storage.vms.bind("change", _.bind(this.check_empty, this));
359 8d08f18a Kostas Papadimitriou
            storage.vms.bind("reset", _.bind(this.check_empty, this));
360 8d08f18a Kostas Papadimitriou
            
361 8d08f18a Kostas Papadimitriou
            // api calls handlers
362 e1599ff8 Kostas Papadimitriou
            synnefo.api.bind("error", _.bind(this.handle_api_error, this));
363 edd1d565 Kostas Papadimitriou
            synnefo.api.bind("change:error_state", _.bind(this.handle_api_error_state, this));
364 8d08f18a Kostas Papadimitriou
            synnefo.ui.bind("error", _.bind(this.handle_ui_error, this));
365 8d08f18a Kostas Papadimitriou
        },
366 8d08f18a Kostas Papadimitriou
        
367 edd1d565 Kostas Papadimitriou
        handle_api_error_state: function(state) {
368 9ce969a7 Kostas Papadimitriou
            if (snf.api.error_state === snf.api.STATES.ERROR) {
369 edd1d565 Kostas Papadimitriou
                this.stop_intervals();
370 edd1d565 Kostas Papadimitriou
            } else {
371 edd1d565 Kostas Papadimitriou
                if (this.intervals_stopped) {
372 edd1d565 Kostas Papadimitriou
                    this.update_intervals();
373 edd1d565 Kostas Papadimitriou
                }
374 edd1d565 Kostas Papadimitriou
            }
375 8d08f18a Kostas Papadimitriou
        },
376 6a3a5bf7 Kostas Papadimitriou
        
377 9ce969a7 Kostas Papadimitriou
        handle_api_error: function(args) {
378 9ce969a7 Kostas Papadimitriou
            if (arguments.length == 1) { arguments = _.toArray(arguments[0])};
379 9ce969a7 Kostas Papadimitriou
380 9ce969a7 Kostas Papadimitriou
            if (!_.last(arguments).display) {
381 9ce969a7 Kostas Papadimitriou
                return;
382 9ce969a7 Kostas Papadimitriou
            }
383 9ce969a7 Kostas Papadimitriou
384 8d08f18a Kostas Papadimitriou
            this.error_state = true;
385 8d08f18a Kostas Papadimitriou
            
386 8d08f18a Kostas Papadimitriou
            var xhr = arguments[0];
387 9ce969a7 Kostas Papadimitriou
            var args = util.parse_api_error.apply(util, arguments);
388 8d08f18a Kostas Papadimitriou
            
389 108dd5bd Kostas Papadimitriou
            // force logout if UNAUTHORIZED request arrives
390 108dd5bd Kostas Papadimitriou
            if (args.code == 401) { snf.ui.logout(); return };
391 108dd5bd Kostas Papadimitriou
392 dc020cf6 Kostas Papadimitriou
            var error_entry = [args.ns, args.code, args.message, args.type, args.details, args];
393 dc020cf6 Kostas Papadimitriou
            this.error_view.show_error.apply(this.error_view, error_entry);
394 8d08f18a Kostas Papadimitriou
        },
395 8d08f18a Kostas Papadimitriou
396 9ffd10ce Kostas Papadimitriou
        handle_ui_error: function(data) {
397 9ffd10ce Kostas Papadimitriou
            var msg = data.msg, code = data.code, err_obj = data.error;
398 9ffd10ce Kostas Papadimitriou
            error = msg + "<br /><br />" + snf.util.stacktrace().replace("at", "<br /><br />at");
399 9ffd10ce Kostas Papadimitriou
            params = { title: "UI error", extra_details: data.extra };
400 9ffd10ce Kostas Papadimitriou
            params.allow_close = data.extra.allow_close === undefined ? true : data.extra.allow_close;
401 9ffd10ce Kostas Papadimitriou
            this.error_view.show_error("UI", -1, msg, "JS Exception", error, params);
402 8d08f18a Kostas Papadimitriou
        },
403 8d08f18a Kostas Papadimitriou
404 8d08f18a Kostas Papadimitriou
        init_overlays: function() {
405 8d08f18a Kostas Papadimitriou
            this.create_vm_view = new views.CreateVMView();
406 8d08f18a Kostas Papadimitriou
            //this.notice_view = new views.NoticeView();
407 8d08f18a Kostas Papadimitriou
        },
408 8d08f18a Kostas Papadimitriou
        
409 8d08f18a Kostas Papadimitriou
        show_loading_view: function() {
410 8d08f18a Kostas Papadimitriou
            $("#container #content").hide();
411 8d08f18a Kostas Papadimitriou
            $("#loading-view").show();
412 8d08f18a Kostas Papadimitriou
        },
413 8d08f18a Kostas Papadimitriou
414 8d08f18a Kostas Papadimitriou
        hide_loading_view: function() {
415 8d08f18a Kostas Papadimitriou
            $("#container #content").show();
416 8d08f18a Kostas Papadimitriou
            $("#loading-view").hide();
417 8d08f18a Kostas Papadimitriou
            $(".css-panes").show();
418 8d08f18a Kostas Papadimitriou
        },
419 8d08f18a Kostas Papadimitriou
        
420 8d08f18a Kostas Papadimitriou
        items_to_load: 4,
421 8d08f18a Kostas Papadimitriou
        completed_items: 0,
422 8d08f18a Kostas Papadimitriou
        check_status: function(loaded) {
423 8d08f18a Kostas Papadimitriou
            this.completed_items++;
424 8d08f18a Kostas Papadimitriou
            // images, flavors loaded
425 8d08f18a Kostas Papadimitriou
            if (this.completed_items == 2) {
426 8d08f18a Kostas Papadimitriou
                this.load_nets_and_vms();
427 8d08f18a Kostas Papadimitriou
            }
428 8d08f18a Kostas Papadimitriou
            if (this.completed_items == this.items_to_load) {
429 8d08f18a Kostas Papadimitriou
                this.after_load();
430 8d08f18a Kostas Papadimitriou
            }
431 8d08f18a Kostas Papadimitriou
        },
432 8d08f18a Kostas Papadimitriou
433 8d08f18a Kostas Papadimitriou
        load_nets_and_vms: function() {
434 8d08f18a Kostas Papadimitriou
            var self = this;
435 8d08f18a Kostas Papadimitriou
            this.update_status("Loading vms...");
436 8d08f18a Kostas Papadimitriou
            storage.vms.fetch({refresh:true, update:false, success: function(){
437 8d08f18a Kostas Papadimitriou
                self.update_status("VMS Loaded.");
438 9ce969a7 Kostas Papadimitriou
                self.check_status();
439 8d08f18a Kostas Papadimitriou
            }});
440 9ce969a7 Kostas Papadimitriou
441 8d08f18a Kostas Papadimitriou
            this.update_status("Loading networks...");
442 8d08f18a Kostas Papadimitriou
            storage.networks.fetch({refresh:true, update:false, success: function(){
443 8d08f18a Kostas Papadimitriou
                self.update_status("Networks loaded.");
444 9ce969a7 Kostas Papadimitriou
                self.check_status();
445 8d08f18a Kostas Papadimitriou
            }});
446 8d08f18a Kostas Papadimitriou
        },  
447 8d08f18a Kostas Papadimitriou
448 8d08f18a Kostas Papadimitriou
        init_intervals: function() {
449 9ce969a7 Kostas Papadimitriou
            this._networks = storage.networks.get_fetcher(snf.config.update_interval, 
450 35855ff5 Kostas Papadimitriou
                                                          snf.config.update_interval / 2, 
451 9ce969a7 Kostas Papadimitriou
                                                          1, true, undefined);
452 9ce969a7 Kostas Papadimitriou
            this._vms = storage.vms.get_fetcher(snf.config.update_interval, 
453 35855ff5 Kostas Papadimitriou
                                                snf.config.update_interval / 2, 
454 9ce969a7 Kostas Papadimitriou
                                                1, true, undefined);
455 8d08f18a Kostas Papadimitriou
        },
456 8d08f18a Kostas Papadimitriou
457 8d08f18a Kostas Papadimitriou
        stop_intervals: function() {
458 b15a0cc4 Kostas Papadimitriou
            if (this._networks) { this._networks.stop(); }
459 b15a0cc4 Kostas Papadimitriou
            if (this._vms) { this._vms.stop(); }
460 edd1d565 Kostas Papadimitriou
            this.intervals_stopped = true;
461 8d08f18a Kostas Papadimitriou
        },
462 8d08f18a Kostas Papadimitriou
463 8d08f18a Kostas Papadimitriou
        update_intervals: function() {
464 b15a0cc4 Kostas Papadimitriou
            if (this._networks) {
465 b15a0cc4 Kostas Papadimitriou
                this._networks.stop();
466 b15a0cc4 Kostas Papadimitriou
                this._networks.start();
467 b15a0cc4 Kostas Papadimitriou
            } else {
468 b15a0cc4 Kostas Papadimitriou
                this.init_intervals();
469 b15a0cc4 Kostas Papadimitriou
            }
470 b15a0cc4 Kostas Papadimitriou
471 b15a0cc4 Kostas Papadimitriou
            if (this._vms) {
472 b15a0cc4 Kostas Papadimitriou
                this._vms.stop();
473 b15a0cc4 Kostas Papadimitriou
                this._vms.start();
474 b15a0cc4 Kostas Papadimitriou
            } else {
475 b15a0cc4 Kostas Papadimitriou
                this.init_intervals();
476 b15a0cc4 Kostas Papadimitriou
            }
477 b15a0cc4 Kostas Papadimitriou
478 edd1d565 Kostas Papadimitriou
            this.intervals_stopped = false;
479 8d08f18a Kostas Papadimitriou
        },
480 8d08f18a Kostas Papadimitriou
481 8d08f18a Kostas Papadimitriou
        after_load: function() {
482 8d08f18a Kostas Papadimitriou
            this.update_status("Setting vms update interval...");
483 8d08f18a Kostas Papadimitriou
            this.init_intervals();
484 8d08f18a Kostas Papadimitriou
            this.update_intervals();
485 8d08f18a Kostas Papadimitriou
            this.update_status("Loaded");
486 8d08f18a Kostas Papadimitriou
            // FIXME: refactor needed
487 8d08f18a Kostas Papadimitriou
            // initialize views
488 8d08f18a Kostas Papadimitriou
            this.initialize_views()
489 8d08f18a Kostas Papadimitriou
            this.update_status("Initializing overlays...");
490 8d08f18a Kostas Papadimitriou
            this.init_overlays();
491 8d08f18a Kostas Papadimitriou
            // display initial view
492 8d08f18a Kostas Papadimitriou
            this.loaded = true;
493 8d08f18a Kostas Papadimitriou
            this.show_initial_view();
494 122850c5 Kostas Papadimitriou
            this.check_empty();
495 8d08f18a Kostas Papadimitriou
        },
496 8d08f18a Kostas Papadimitriou
497 8d08f18a Kostas Papadimitriou
        load: function() {
498 8d08f18a Kostas Papadimitriou
            this.error_view = new views.ErrorView();
499 81cfb7ee Kostas Papadimitriou
            this.feedback_view = new views.FeedbackView();
500 8d08f18a Kostas Papadimitriou
            var self = this;
501 8d08f18a Kostas Papadimitriou
            // initialize overlay views
502 8d08f18a Kostas Papadimitriou
            
503 8d08f18a Kostas Papadimitriou
            // display loading message
504 8d08f18a Kostas Papadimitriou
            this.show_loading_view();
505 8d08f18a Kostas Papadimitriou
            // sync load initial data
506 8d08f18a Kostas Papadimitriou
            this.update_status("Loading images...");
507 8d08f18a Kostas Papadimitriou
            storage.images.fetch({refresh:true, update:false, success: function(){
508 8d08f18a Kostas Papadimitriou
                self.check_status()
509 8d08f18a Kostas Papadimitriou
            }});
510 8d08f18a Kostas Papadimitriou
            this.update_status("Loading flavors...");
511 8d08f18a Kostas Papadimitriou
            storage.flavors.fetch({refresh:true, update:false, success:function(){
512 8d08f18a Kostas Papadimitriou
                self.check_status()
513 8d08f18a Kostas Papadimitriou
            }});
514 8d08f18a Kostas Papadimitriou
        },
515 8d08f18a Kostas Papadimitriou
516 8d08f18a Kostas Papadimitriou
        update_status: function(msg) {
517 8d08f18a Kostas Papadimitriou
            this.log.debug(msg)
518 8d08f18a Kostas Papadimitriou
            this.status = msg;
519 8d08f18a Kostas Papadimitriou
            $("#loading-view .info").removeClass("hidden")
520 8d08f18a Kostas Papadimitriou
            $("#loading-view .info").text(this.status);
521 8d08f18a Kostas Papadimitriou
        },
522 8d08f18a Kostas Papadimitriou
523 8d08f18a Kostas Papadimitriou
        initialize_views: function() {
524 8d08f18a Kostas Papadimitriou
            this.empty_view = new views.EmptyView();
525 8d08f18a Kostas Papadimitriou
            this.select_view = new views.SelectView(this);
526 8d08f18a Kostas Papadimitriou
            this.metadata_view = new views.MetadataView();
527 8d08f18a Kostas Papadimitriou
            this.multiple_actions_view = new views.MultipleActionsView();
528 8d08f18a Kostas Papadimitriou
            
529 8d08f18a Kostas Papadimitriou
            this.add_view("icon");
530 8d08f18a Kostas Papadimitriou
            this.add_view("list");
531 8d08f18a Kostas Papadimitriou
            this.add_view("single");
532 8d08f18a Kostas Papadimitriou
            this.add_view("networks");
533 8d08f18a Kostas Papadimitriou
534 8d08f18a Kostas Papadimitriou
            this.init_menu();
535 8d08f18a Kostas Papadimitriou
        },
536 8d08f18a Kostas Papadimitriou
537 8d08f18a Kostas Papadimitriou
        init_menu: function() {
538 8d08f18a Kostas Papadimitriou
            $(".usermenu .feedback").click(_.bind(function(){
539 8d08f18a Kostas Papadimitriou
                this.feedback_view.show();
540 8d08f18a Kostas Papadimitriou
            }, this));
541 8d08f18a Kostas Papadimitriou
        },
542 8d08f18a Kostas Papadimitriou
        
543 8d08f18a Kostas Papadimitriou
        // initial view based on user cookie
544 8d08f18a Kostas Papadimitriou
        show_initial_view: function() {
545 8d08f18a Kostas Papadimitriou
          this.set_vm_view_handlers();
546 8d08f18a Kostas Papadimitriou
          this.hide_loading_view();
547 8d08f18a Kostas Papadimitriou
          this.show_view(this.initial_view);
548 8d08f18a Kostas Papadimitriou
          this.trigger("initial");
549 8d08f18a Kostas Papadimitriou
        },
550 8d08f18a Kostas Papadimitriou
551 8d08f18a Kostas Papadimitriou
        show_vm_details: function(vm) {
552 8d08f18a Kostas Papadimitriou
            snf.ui.main.show_view("single")
553 8d08f18a Kostas Papadimitriou
            snf.ui.main.current_view.show_vm(vm);
554 8d08f18a Kostas Papadimitriou
        },
555 8d08f18a Kostas Papadimitriou
556 8d08f18a Kostas Papadimitriou
        set_vm_view_handlers: function() {
557 8d08f18a Kostas Papadimitriou
            $("#createcontainer #create").click(_.bind(function(){
558 8d08f18a Kostas Papadimitriou
                this.create_vm_view.show();
559 8d08f18a Kostas Papadimitriou
            }, this))
560 8d08f18a Kostas Papadimitriou
        },
561 8d08f18a Kostas Papadimitriou
562 8d08f18a Kostas Papadimitriou
        check_empty: function() {
563 8d08f18a Kostas Papadimitriou
            if (!this.loaded) { return }
564 8d08f18a Kostas Papadimitriou
            if (storage.vms.length == 0) {
565 df8fe961 Kostas Papadimitriou
                this.show_view("machines");
566 8d08f18a Kostas Papadimitriou
                this.show_empty();
567 8d08f18a Kostas Papadimitriou
            } else {
568 8d08f18a Kostas Papadimitriou
                this.hide_empty();
569 8d08f18a Kostas Papadimitriou
            }
570 8d08f18a Kostas Papadimitriou
            this.select_view.update_layout();
571 8d08f18a Kostas Papadimitriou
        },
572 8d08f18a Kostas Papadimitriou
573 8d08f18a Kostas Papadimitriou
        show_empty: function() {
574 8d08f18a Kostas Papadimitriou
            $("#machines-pane-top").addClass("empty");
575 8d08f18a Kostas Papadimitriou
576 8d08f18a Kostas Papadimitriou
            this.$(".panes").hide();
577 8d08f18a Kostas Papadimitriou
            this.$("#machines-pane").show();
578 8d08f18a Kostas Papadimitriou
579 8d08f18a Kostas Papadimitriou
            this.hide_views([]);
580 8d08f18a Kostas Papadimitriou
            this.empty_view.show();
581 8d08f18a Kostas Papadimitriou
        },
582 8d08f18a Kostas Papadimitriou
583 8d08f18a Kostas Papadimitriou
        hide_empty: function() {
584 8d08f18a Kostas Papadimitriou
            $("#machines-pane-top").removeClass("empty");
585 8d08f18a Kostas Papadimitriou
586 8d08f18a Kostas Papadimitriou
            this.empty_view = new views.EmptyView();
587 8d08f18a Kostas Papadimitriou
            this.empty_view.hide();
588 8d08f18a Kostas Papadimitriou
            if (this.current_view && !this.current_view.visible()) { 
589 8d08f18a Kostas Papadimitriou
                this.current_view.show(); 
590 8d08f18a Kostas Papadimitriou
            }
591 8d08f18a Kostas Papadimitriou
        },
592 8d08f18a Kostas Papadimitriou
        
593 8d08f18a Kostas Papadimitriou
        get_title: function(view_id) {
594 8d08f18a Kostas Papadimitriou
            var view_id = view_id || this.current_view_id;
595 8d08f18a Kostas Papadimitriou
            return this.views_titles[view_id];
596 8d08f18a Kostas Papadimitriou
        },
597 8d08f18a Kostas Papadimitriou
598 8d08f18a Kostas Papadimitriou
        // return class object for the given view or false if
599 8d08f18a Kostas Papadimitriou
        // the view is not registered
600 8d08f18a Kostas Papadimitriou
        get_class_for_view: function (view_id) {
601 8d08f18a Kostas Papadimitriou
            if (!this.views_classes[view_id]) {
602 8d08f18a Kostas Papadimitriou
                return false;
603 8d08f18a Kostas Papadimitriou
            }
604 8d08f18a Kostas Papadimitriou
            return this.views_classes[view_id];
605 8d08f18a Kostas Papadimitriou
        },
606 8d08f18a Kostas Papadimitriou
607 8d08f18a Kostas Papadimitriou
        view: function(view_id) {
608 8d08f18a Kostas Papadimitriou
            return this.views[view_id];
609 8d08f18a Kostas Papadimitriou
        },
610 8d08f18a Kostas Papadimitriou
611 8d08f18a Kostas Papadimitriou
        add_view: function(view_id) {
612 8d08f18a Kostas Papadimitriou
            if (!this.views[view_id]) {
613 8d08f18a Kostas Papadimitriou
                var cls = this.get_class_for_view(view_id);
614 8d08f18a Kostas Papadimitriou
                if (this.skip_errors) {
615 8d08f18a Kostas Papadimitriou
                    this.views[view_id] = new cls();
616 8d08f18a Kostas Papadimitriou
                    $(this.views[view_id]).bind("resize", _.bind(function() {
617 8d08f18a Kostas Papadimitriou
                        window.positionFooter();
618 8d08f18a Kostas Papadimitriou
                        this.multiple_actions_view.fix_position();
619 8d08f18a Kostas Papadimitriou
                    }, this));
620 8d08f18a Kostas Papadimitriou
                } else {
621 8d08f18a Kostas Papadimitriou
                    // catch ui errors
622 8d08f18a Kostas Papadimitriou
                    try {
623 8d08f18a Kostas Papadimitriou
                        this.views[view_id] = new cls();
624 8d08f18a Kostas Papadimitriou
                        $(this.views[view_id]).bind("resize", _.bind(function() {
625 8d08f18a Kostas Papadimitriou
                            window.positionFooter();
626 8d08f18a Kostas Papadimitriou
                            this.multiple_actions_view.fix_position();
627 8d08f18a Kostas Papadimitriou
                        }, this));
628 9ffd10ce Kostas Papadimitriou
                    } catch (err) {snf.ui.trigger_error(-1, "Cannot add view", err)}
629 8d08f18a Kostas Papadimitriou
                }
630 8d08f18a Kostas Papadimitriou
            } else {
631 8d08f18a Kostas Papadimitriou
            }
632 8d08f18a Kostas Papadimitriou
633 8d08f18a Kostas Papadimitriou
            if (this.views[view_id].vms_view) {
634 8d08f18a Kostas Papadimitriou
                this.views[view_id].metadata_view = this.metadata_view;
635 8d08f18a Kostas Papadimitriou
            }
636 8d08f18a Kostas Papadimitriou
            return this.views[view_id];
637 8d08f18a Kostas Papadimitriou
        },
638 8d08f18a Kostas Papadimitriou
            
639 8d08f18a Kostas Papadimitriou
        hide_views: function(skip) {
640 8d08f18a Kostas Papadimitriou
            _.each(this.views, function(view) {
641 8d08f18a Kostas Papadimitriou
                if (skip.indexOf(view) === -1) {
642 8d08f18a Kostas Papadimitriou
                    $(view.el).hide();
643 8d08f18a Kostas Papadimitriou
                }
644 8d08f18a Kostas Papadimitriou
            }, this)
645 8d08f18a Kostas Papadimitriou
        },
646 8d08f18a Kostas Papadimitriou
        
647 8d08f18a Kostas Papadimitriou
        get: function(view_id) {
648 8d08f18a Kostas Papadimitriou
            return this.views[view_id];
649 8d08f18a Kostas Papadimitriou
        },
650 8d08f18a Kostas Papadimitriou
        
651 8d08f18a Kostas Papadimitriou
        session_view: function() {
652 8d08f18a Kostas Papadimitriou
            if (this.pane_view_from_session() > 0) {
653 8d08f18a Kostas Papadimitriou
                return this.views_pane_indexes[this.pane_view_from_session()];
654 8d08f18a Kostas Papadimitriou
            } else {
655 8d08f18a Kostas Papadimitriou
                return this.views_indexes[this.machine_view_from_session()];
656 8d08f18a Kostas Papadimitriou
            }
657 8d08f18a Kostas Papadimitriou
        },
658 8d08f18a Kostas Papadimitriou
659 8d08f18a Kostas Papadimitriou
        pane_view_from_session: function() {
660 8d08f18a Kostas Papadimitriou
            return $.cookie("pane_view") || 0;
661 8d08f18a Kostas Papadimitriou
        },
662 8d08f18a Kostas Papadimitriou
663 8d08f18a Kostas Papadimitriou
        machine_view_from_session: function() {
664 8d08f18a Kostas Papadimitriou
            return $.cookie("machine_view") || 0;
665 8d08f18a Kostas Papadimitriou
        },
666 8d08f18a Kostas Papadimitriou
667 8d08f18a Kostas Papadimitriou
        update_session: function() {
668 8d08f18a Kostas Papadimitriou
            $.cookie("pane_view", this.pane_ids[this.current_view_id]);
669 8d08f18a Kostas Papadimitriou
            if (this.current_view.vms_view) {
670 8d08f18a Kostas Papadimitriou
                $.cookie("machine_view", this.views_ids[this.current_view_id]);
671 8d08f18a Kostas Papadimitriou
            }
672 8d08f18a Kostas Papadimitriou
        },
673 8d08f18a Kostas Papadimitriou
674 8d08f18a Kostas Papadimitriou
        identify_view: function(view_id) {
675 8d08f18a Kostas Papadimitriou
            // machines view_id is an alias to
676 8d08f18a Kostas Papadimitriou
            // one of the 3 machines view
677 8d08f18a Kostas Papadimitriou
            // identify which one (if no cookie set defaults to icon)
678 8d08f18a Kostas Papadimitriou
            if (view_id == "machines") {
679 8d08f18a Kostas Papadimitriou
                var index = this.machine_view_from_session();
680 8d08f18a Kostas Papadimitriou
                view_id = this.views_indexes[index];
681 8d08f18a Kostas Papadimitriou
            }
682 8d08f18a Kostas Papadimitriou
            return view_id;
683 8d08f18a Kostas Papadimitriou
        },
684 8d08f18a Kostas Papadimitriou
        
685 8d08f18a Kostas Papadimitriou
        // switch to current view pane
686 8d08f18a Kostas Papadimitriou
        // if differs from the visible one
687 8d08f18a Kostas Papadimitriou
        show_view_pane: function() {
688 8d08f18a Kostas Papadimitriou
            if (this.current_view.pane != this.current_pane) {
689 8d08f18a Kostas Papadimitriou
                $(this.current_view.pane).show();
690 8d08f18a Kostas Papadimitriou
                $(this.current_pane).hide();
691 8d08f18a Kostas Papadimitriou
                this.current_pane = this.current_view.pane;
692 8d08f18a Kostas Papadimitriou
            }
693 8d08f18a Kostas Papadimitriou
        },
694 8d08f18a Kostas Papadimitriou
695 8d08f18a Kostas Papadimitriou
        show_view: function(view_id) {
696 9ffd10ce Kostas Papadimitriou
            try {
697 9ffd10ce Kostas Papadimitriou
                // same view, visible
698 9ffd10ce Kostas Papadimitriou
                // get out of here asap
699 9ffd10ce Kostas Papadimitriou
                if (this.current_view && 
700 9ffd10ce Kostas Papadimitriou
                    this.current_view.id == view_id && 
701 9ffd10ce Kostas Papadimitriou
                    this.current_view.visible()) {
702 9ffd10ce Kostas Papadimitriou
                    return;
703 9ffd10ce Kostas Papadimitriou
                }
704 9ffd10ce Kostas Papadimitriou
                
705 9ffd10ce Kostas Papadimitriou
                // choose proper view_id
706 9ffd10ce Kostas Papadimitriou
                view_id = this.identify_view(view_id);
707 9ffd10ce Kostas Papadimitriou
708 9ffd10ce Kostas Papadimitriou
                // add/create view and update current view
709 9ffd10ce Kostas Papadimitriou
                var view = this.add_view(view_id);
710 9ffd10ce Kostas Papadimitriou
                
711 9ffd10ce Kostas Papadimitriou
                // set current view
712 9ffd10ce Kostas Papadimitriou
                this.current_view = view;
713 9ffd10ce Kostas Papadimitriou
                this.current_view_id = view_id;
714 9ffd10ce Kostas Papadimitriou
715 9ffd10ce Kostas Papadimitriou
                // hide all other views
716 9ffd10ce Kostas Papadimitriou
                this.hide_views([this.current_view]);
717 9ffd10ce Kostas Papadimitriou
                
718 9ffd10ce Kostas Papadimitriou
                // FIXME: depricated
719 9ffd10ce Kostas Papadimitriou
                $(".large-spinner").remove();
720 9ffd10ce Kostas Papadimitriou
721 9ffd10ce Kostas Papadimitriou
                storage.vms.reset_pending_actions();
722 9ffd10ce Kostas Papadimitriou
                storage.vms.stop_stats_update();
723 9ffd10ce Kostas Papadimitriou
724 9ffd10ce Kostas Papadimitriou
                // show current view
725 9ffd10ce Kostas Papadimitriou
                this.show_view_pane();
726 9ffd10ce Kostas Papadimitriou
                view.show();
727 9ffd10ce Kostas Papadimitriou
                
728 9ffd10ce Kostas Papadimitriou
                // update menus
729 9ffd10ce Kostas Papadimitriou
                if (this.select_view) {
730 9ffd10ce Kostas Papadimitriou
                    this.select_view.update_layout();
731 9ffd10ce Kostas Papadimitriou
                }
732 9ffd10ce Kostas Papadimitriou
                this.current_view.__update_layout();
733 9ffd10ce Kostas Papadimitriou
734 9ffd10ce Kostas Papadimitriou
                // update cookies
735 9ffd10ce Kostas Papadimitriou
                this.update_session();
736 9ffd10ce Kostas Papadimitriou
                
737 9ffd10ce Kostas Papadimitriou
                // machines view subnav
738 9ffd10ce Kostas Papadimitriou
                if (this.current_view.vms_view) {
739 9ffd10ce Kostas Papadimitriou
                    $("#machines-pane").show();
740 9ffd10ce Kostas Papadimitriou
                } else {
741 9ffd10ce Kostas Papadimitriou
                    $("#machines-pane").hide();
742 9ffd10ce Kostas Papadimitriou
                }
743 9ffd10ce Kostas Papadimitriou
                
744 9ffd10ce Kostas Papadimitriou
                // fix footer position
745 9ffd10ce Kostas Papadimitriou
                // TODO: move footer handlers in
746 9ffd10ce Kostas Papadimitriou
                // main view (from home.html)
747 9ffd10ce Kostas Papadimitriou
                if (window.positionFooter) {
748 9ffd10ce Kostas Papadimitriou
                    window.positionFooter();
749 9ffd10ce Kostas Papadimitriou
                }
750 8d08f18a Kostas Papadimitriou
751 9ffd10ce Kostas Papadimitriou
                // trigger view change event
752 9ffd10ce Kostas Papadimitriou
                this.trigger("view:change", this.current_view.view_id);
753 2c5adb8a Kostas Papadimitriou
                this.select_view.title.text(this.get_title());
754 9ffd10ce Kostas Papadimitriou
                $(window).trigger("view:change");
755 9ffd10ce Kostas Papadimitriou
                return view;
756 9ffd10ce Kostas Papadimitriou
            } catch (err) {
757 9ffd10ce Kostas Papadimitriou
                snf.ui.trigger_error(-2, "Cannot show view: " + view_id, err);
758 8d08f18a Kostas Papadimitriou
            }
759 8d08f18a Kostas Papadimitriou
        },
760 8d08f18a Kostas Papadimitriou
761 8d08f18a Kostas Papadimitriou
        reset_vm_actions: function() {
762 8d08f18a Kostas Papadimitriou
        
763 8d08f18a Kostas Papadimitriou
        },
764 8d08f18a Kostas Papadimitriou
        
765 8d08f18a Kostas Papadimitriou
        // identify current view
766 8d08f18a Kostas Papadimitriou
        // based on views element visibility
767 8d08f18a Kostas Papadimitriou
        current_view_id: function() {
768 8d08f18a Kostas Papadimitriou
            var found = false;
769 8d08f18a Kostas Papadimitriou
            _.each(this.views, function(key, value) {
770 8d08f18a Kostas Papadimitriou
                if (value.visible()) {
771 8d08f18a Kostas Papadimitriou
                    found = value;
772 8d08f18a Kostas Papadimitriou
                }
773 8d08f18a Kostas Papadimitriou
            })
774 8d08f18a Kostas Papadimitriou
            return found;
775 8d08f18a Kostas Papadimitriou
        }
776 8d08f18a Kostas Papadimitriou
777 8d08f18a Kostas Papadimitriou
    });
778 8d08f18a Kostas Papadimitriou
779 8d08f18a Kostas Papadimitriou
    snf.ui.main = new views.MainView();
780 b61dc01f Kostas Papadimitriou
    
781 b61dc01f Kostas Papadimitriou
    snf.ui.logout = function() {
782 b61dc01f Kostas Papadimitriou
        $.cookie("X-Auth-Token", null);
783 66be390b Kostas Papadimitriou
        if (snf.config.logout_url !== undefined)
784 b61dc01f Kostas Papadimitriou
        {
785 66be390b Kostas Papadimitriou
            window.location = snf.config.logout_url;
786 b61dc01f Kostas Papadimitriou
        } else {
787 b61dc01f Kostas Papadimitriou
            window.location.reload();
788 b61dc01f Kostas Papadimitriou
        }
789 b61dc01f Kostas Papadimitriou
    }
790 8d08f18a Kostas Papadimitriou
791 92a063bf Kostas Papadimitriou
    snf.ui.init = function() {
792 66be390b Kostas Papadimitriou
        if (snf.config.handle_window_exceptions) {
793 66be390b Kostas Papadimitriou
            window.onerror = function(msg, file, line) {
794 66be390b Kostas Papadimitriou
                snf.ui.trigger_error("CRITICAL", msg, {}, { file:file + ":" + line, allow_close: false });
795 66be390b Kostas Papadimitriou
            };
796 66be390b Kostas Papadimitriou
        }
797 92a063bf Kostas Papadimitriou
        snf.ui.main.load();
798 92a063bf Kostas Papadimitriou
    }
799 92a063bf Kostas Papadimitriou
800 8d08f18a Kostas Papadimitriou
})(this);