Revision 101e6604 ui/static/snf/js/ui/web/ui_main_view.js

b/ui/static/snf/js/ui/web/ui_main_view.js
55 55
        
56 56
        initialize: function() {
57 57
            this.actions = {};
58
            this.ns_config = {};
59

  
58 60
            views.MultipleActionsView.__super__.initialize.call(this);
59
            
60
            // view elements
61
            this.confirm_actions = this.$(".confirm_multiple_actions");
62
            this.confirm_actions_yes = this.$(".confirm_multiple_actions button.yes");
63
            this.confirm_actions_no = this.$(".confirm_multiple_actions button.no");
64
            this.confirm_reboot = this.$(".confirm_reboot_required");
65
            this.confirm_reboot_yes = this.$(".confirm_reboot_required button.yes");
66
            this.confirm_reboot_no = this.$(".confirm_reboot_required button.no");
67
            this.confirm_reboot_list = this.confirm_reboot.find(".reboot-machines-list");
61

  
62
            this.ns_tpl = this.$(".confirm_multiple_actions-template").clone()
68 63

  
69 64
            this.init_handlers();
70 65
            this.update_layout();
......
74 69
            this.fix_position = _.throttle(this.fix_position, 100);
75 70
            this.update_layout = _.throttle(this.update_layout, 100);
76 71
            this.show_limit = 1;
72

  
73
            this.init_ns("vms", {
74
                msg_tpl:"Your actions will affect 1 machine",
75
                msg_tpl_plural:"Your actions will affect {0} machines",
76
                actions_msg: {confirm: "Confirm all", cancel: "Cancel all"},
77
                limit: 1,
78
                cancel_all: function() { snf.storage.vms.reset_pending_actions(); },
79
                do_all: function() { snf.storage.vms.do_all_pending_actions(); }
80
            });
81
            
82
            this.init_ns("nets", {
83
                msg_tpl:"Your actions will affect 1 private network",
84
                msg_tpl_plural:"Your actions will affect {0} private networks",
85
                actions_msg: {confirm: "Confirm all", cancel: "Cancel all"},
86
                limit: 1,
87
                cancel_all: function() { snf.storage.networks.reset_pending_actions(); },
88
                do_all: function() { snf.storage.networks.do_all_pending_actions(); }
89
            });
90

  
91
            this.init_ns("reboots", {
92
                msg_tpl:"1 machine needs to be rebooted for changes to apply.",
93
                msg_tpl_plural:"{0} machines needs to be rebooted for changes to apply.",
94
                actions_msg: {confirm: "Reboot all", cancel: "Cancel all"},
95
                limit: 0,
96
                cancel_all: function() { snf.storage.vms.reset_reboot_required(); },
97
                do_all: function() { snf.storage.vms.do_all_reboots(); }
98
            });
99
        },
100
        
101
        init_ns: function(ns, params) {
102
            this.actions[ns] = {};
103
            var nsconf = this.ns_config[ns] = params || {};
104
            nsconf.cont = $(this.$("#conirm_multiple_cont_template").clone());
105
            nsconf.cont.attr("id", "confirm_multiple_cont_" + ns);
106
            $(this.el).find(".ns-confirms-cont").append(nsconf.cont).addClass(ns);
107
            $(this.el).find(".ns-confirms-cont").append(nsconf.cont).addClass("confirm-ns");
108
            nsconf.cont.find(".msg button.yes").text(
109
                nsconf.actions_msg.confirm).click(_.bind(this.do_all, this, ns));
110
            nsconf.cont.find(".msg button.no").text(
111
                nsconf.actions_msg.cancel).click(_.bind(this.cancel_all, this, ns));
112
        },
113

  
114
        do_all: function(ns) {
115
            this.ns_config[ns].do_all();
116
        },
117

  
118
        cancel_all: function(ns) {
119
            this.ns_config[ns].cancel_all();
77 120
        },
78 121

  
79 122
        init_handlers: function() {
......
86 129
            $(window).scroll(_.bind(function(){
87 130
                this.fix_position();
88 131
            }, this));
89
            
90
            // confirm/cancel button handlers
91
            var self = this;
92
            this.confirm_actions_yes.click(function(){ self.do_all(); })
93
            this.confirm_actions_no.click(function(){
94
                self.reset_actions();
95
            });
96

  
97
            this.confirm_reboot_yes.click(function(){ self.do_reboot_all(); })
98
            this.confirm_reboot_no.click(function(){
99
                self.reset_reboots();
100
            });
101

  
102
            storage.vms.bind("change:pending_action", _.bind(this.handle_vm_change, this));
103
            storage.vms.bind("change:reboot_required", _.bind(this.handle_vm_change, this));
104 132

  
133
            storage.vms.bind("change:pending_action", _.bind(this.handle_action_add, this, "vms"));
134
            storage.vms.bind("change:reboot_required", _.bind(this.handle_action_add, this, "reboots"));
135
            storage.networks.bind("change:actions", _.bind(this.handle_action_add, this, "nets"));
105 136
        },
106 137

  
107
        handle_vm_change: function(vm) {
108
            if (vm.has_pending_action()) {
109
                var action = vm.get("pending_action");
110
                this.add_action(vm, action);
111
            } else {
112
                this.remove_action(vm);
138
        handle_action_add: function(type, model, action) {
139
            var actions = this.actions[type];
140
            
141
            // TODO: remove type specific addition code in its own namespace
142
            if (type == "nets") {
143
                if (!action || action.is_empty()) {
144
                    delete actions[model.id];
145
                } else {
146
                    actions[model.id] = {model: model, actions: action.actions};
147
                }
113 148
            }
114
            this.update_layout();
115
        },
116

  
117
        add_action: function(vm, action) {
118
            this._actions[vm.id] = {'vm': vm, 'action': action};
119
        },
120 149

  
121
        remove_action: function(vm) {
122
            delete this._actions[vm.id];
123
        },
150
            if (type == "vms") {
151
                _.each(actions, function(action) {
152
                    if (action.model.id == model.id) {
153
                        delete actions[action]
154
                    }
155
                });
156

  
157
                var actobject = {};
158
                actobject[action] = [[]];
159
                actions[model.id] = {model: model, actions: actobject};
160
                if (typeof action == "undefined") {
161
                    delete actions[model.id]
162
                }
163
            }
124 164

  
125
        reset: function() {
126
            this._actions = {};
165
            if (type == "reboots") {
166
                _.each(actions, function(action) {
167
                    if (action.model.id == model.id) {
168
                        delete actions[action]
169
                    }
170
                });
171
                var actobject = {};
172
                actobject['reboot'] = [[]];
173
                actions[model.id] = {model: model, actions: actobject};
174
                if (!action) {
175
                    delete actions[model.id]
176
                }
177
            }
178
            
127 179
            this.update_layout();
128 180
        },
129
        
130
        reboot_vm: function(vm) {
131
            vm.call("reboot");
132
        },
133 181

  
134
        do_reboot_all: function() {
135
            _.each(storage.vms.get_reboot_required(), function(vm){
136
                this.reboot_vm(vm)
137
            }, this)  
138
        },
182
        update_actions_content: function(ns) {
183
            var conf = this.ns_config[ns];
184
            conf.cont.find(".details").empty();
185
            conf.cont.find(".msg p").text("");
186
            
187
            var count = 0;
188
            var actionscount = 0;
189
            _.each(this.actions[ns], function(actions, model_id) {
190
                count++;
191
                _.each(actions.actions, function(params, act_name){
192
                    if (params && params.length) {
193
                        actionscount += params.length;
194
                    } else {
195
                        actionscount++;
196
                    }
197
                })
198
                this.total_confirm_actions++;
199
            });
200
            
201
            var limit = conf.limit;
202
            if (ui.main.current_view.view_id == "vm_list") {
203
                limit = 0;
204
            }
139 205

  
140
        do_all: function() {
141
            _.each(this._actions, function(action){
142
                action.vm.call(action.action);
143
            }, this)  
144
            this.reset_actions();
145
        },
206
            if (actionscount > limit) {
207
                conf.cont.show();
208
                this.confirm_ns_open++;
209
            } else {
210
                conf.cont.hide();
211
            }
212
            
213
            var msg = count > 1 ? conf.msg_tpl_plural : conf.msg_tpl;
214
            conf.cont.find(".msg p").text(msg.format(count));
146 215

  
147
        reset_reboots: function () {
148
            _.each(storage.vms.get_reboot_required(), function(vm) {vm.set({'reboot_required': false})}, this);
149
            this.update_layout();
216
            return conf.cont;
150 217
        },
151 218

  
152
        reset_actions: function() {
153
            _.each(this._actions, _.bind(function(action){
154
                try {
155
                    action.vm.clear_pending_action();
156
                    this.remove_action(action.vm);
157
                } catch(err) {
158
                    console.error("vm " + action.vm.id + " failed to reset", err);
159
                }
160
            }, this))  
161
        },
162
        
163 219
        fix_position: function() {
164 220
            $('.confirm_multiple').removeClass('fixed');
165 221
            if (($(this.el).offset().top +$(this.el).height())> ($(window).scrollTop() + $(window).height())) {
......
167 223
            }
168 224
        },
169 225
        
170
        check_notify_limit: function() {
171
            this.show_limit = 1;
172
            if (ui.main.current_view && ['networks', 'vm_list'].indexOf(ui.main.current_view.view_id) > -1) {
173
                this.show_limit = 0;
174
            }
175
        },
176
        
177
        update_reboot_required_list: function(vms) {
178
            this.confirm_reboot_list.empty();
179
        },
180

  
181
        update_reboot_required: function() {
182
            var vms = storage.vms.get_reboot_required();
183
            if (vms.length) {
184
                this.confirm_reboot.find(".actionLen").text(vms.length);
185
                this.update_reboot_required_list();
186
                this.confirm_reboot.show();
187
                $(this.el).show();
188
            } else {
189
                if (!this.actions_visible) {
190
                   $(this.el).hide();
191
                }
192
                this.confirm_reboot.hide();
193
            }
194
        },
195

  
196 226
        update_layout: function() {
197
            this.check_notify_limit();
198
            this.actions_visible = false;
227
            this.confirm_ns_open = 0;
228
            this.total_confirm_actions = 0;
199 229

  
200
            if (_.size(this._actions) > this.show_limit) {
201
                this.actions_visible = true;
230
            $(this.el).show();
231
            $(this.el).find("#conirm_multiple_cont_template").hide();
232
            $(this.el).find(".confirm-ns").show();
233
            
234
            _.each(this.ns_config, _.bind(function(params, key) {
235
                this.update_actions_content(key);
236
            }, this));
237

  
238
            if (this.confirm_ns_open > 0) {
202 239
                $(this.el).show();
203
                this.confirm_actions.show();
240
                this.$(".confirm-all-cont").hide();
241
                this.$(".ns-confirms-cont").show();
204 242
            } else {
205 243
                $(this.el).hide();
206
                this.confirm_actions.hide();
244
                this.$(".confirm-all-cont").hide();
245
                this.$(".ns-confirms-cont").hide();
207 246
            }
208 247

  
209
            this.update_reboot_required();
210
            this.confirm_actions.find(".actionLen").text(_.size(this._actions));
211 248
            $(window).trigger("resize");
212 249
        }
213 250
    })
......
759 796
        },
760 797

  
761 798
        _show_view: function(view_id) {
762
            try {
763 799
                // same view, visible
764 800
                // get out of here asap
765 801
                if (this.current_view && 
......
785 821
                $(".large-spinner").remove();
786 822

  
787 823
                storage.vms.reset_pending_actions();
824
                storage.networks.reset_pending_actions();
788 825
                storage.vms.stop_stats_update();
789 826

  
790 827
                // show current view
......
819 856
                this.select_view.title.text(this.get_title());
820 857
                $(window).trigger("view:change");
821 858
                return view;
822
            } catch (err) {
823
                snf.ui.trigger_error(-2, "Cannot show view: " + view_id, err);
824
            }
825 859
        },
826 860

  
827 861
        reset_vm_actions: function() {

Also available in: Unified diff