Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / ui / web / ui_create_view.js @ a45b30a7

History | View | Annotate | Download (66.5 kB)

1 00469232 Kostas Papadimitriou
// Copyright 2011 GRNET S.A. All rights reserved.
2 00469232 Kostas Papadimitriou
// 
3 00469232 Kostas Papadimitriou
// Redistribution and use in source and binary forms, with or
4 00469232 Kostas Papadimitriou
// without modification, are permitted provided that the following
5 00469232 Kostas Papadimitriou
// conditions are met:
6 00469232 Kostas Papadimitriou
// 
7 00469232 Kostas Papadimitriou
//   1. Redistributions of source code must retain the above
8 00469232 Kostas Papadimitriou
//      copyright notice, this list of conditions and the following
9 00469232 Kostas Papadimitriou
//      disclaimer.
10 00469232 Kostas Papadimitriou
// 
11 00469232 Kostas Papadimitriou
//   2. Redistributions in binary form must reproduce the above
12 00469232 Kostas Papadimitriou
//      copyright notice, this list of conditions and the following
13 00469232 Kostas Papadimitriou
//      disclaimer in the documentation and/or other materials
14 00469232 Kostas Papadimitriou
//      provided with the distribution.
15 00469232 Kostas Papadimitriou
// 
16 00469232 Kostas Papadimitriou
// THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 00469232 Kostas Papadimitriou
// OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 00469232 Kostas Papadimitriou
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 00469232 Kostas Papadimitriou
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 00469232 Kostas Papadimitriou
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 00469232 Kostas Papadimitriou
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 00469232 Kostas Papadimitriou
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 00469232 Kostas Papadimitriou
// USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 00469232 Kostas Papadimitriou
// AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 00469232 Kostas Papadimitriou
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 00469232 Kostas Papadimitriou
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 00469232 Kostas Papadimitriou
// POSSIBILITY OF SUCH DAMAGE.
28 00469232 Kostas Papadimitriou
// 
29 00469232 Kostas Papadimitriou
// The views and conclusions contained in the software and
30 00469232 Kostas Papadimitriou
// documentation are those of the authors and should not be
31 00469232 Kostas Papadimitriou
// interpreted as representing official policies, either expressed
32 00469232 Kostas Papadimitriou
// or implied, of GRNET S.A.
33 00469232 Kostas Papadimitriou
// 
34 00469232 Kostas Papadimitriou
35 8d08f18a Kostas Papadimitriou
;(function(root){
36 8d08f18a Kostas Papadimitriou
37 8d08f18a Kostas Papadimitriou
    // root
38 8d08f18a Kostas Papadimitriou
    var root = root;
39 8d08f18a Kostas Papadimitriou
    
40 8d08f18a Kostas Papadimitriou
    // setup namepsaces
41 8d08f18a Kostas Papadimitriou
    var snf = root.synnefo = root.synnefo || {};
42 8d08f18a Kostas Papadimitriou
    var models = snf.models = snf.models || {}
43 8d08f18a Kostas Papadimitriou
    var storage = snf.storage = snf.storage || {};
44 8d08f18a Kostas Papadimitriou
    var ui = snf.ui = snf.ui || {};
45 8d08f18a Kostas Papadimitriou
    var util = snf.util = snf.util || {};
46 8d08f18a Kostas Papadimitriou
47 8d08f18a Kostas Papadimitriou
    var views = snf.views = snf.views || {}
48 8d08f18a Kostas Papadimitriou
49 8d08f18a Kostas Papadimitriou
    // shortcuts
50 8d08f18a Kostas Papadimitriou
    var bb = root.Backbone;
51 8d08f18a Kostas Papadimitriou
52 8d08f18a Kostas Papadimitriou
53 8d08f18a Kostas Papadimitriou
    views.VMCreationPasswordView = views.Overlay.extend({
54 8d08f18a Kostas Papadimitriou
        view_id: "creation_password_view",
55 8d08f18a Kostas Papadimitriou
        content_selector: "#creation-password-overlay",
56 8d08f18a Kostas Papadimitriou
        css_class: 'overlay-password overlay-info',
57 8d08f18a Kostas Papadimitriou
        overlay_id: "creation-password-overlay",
58 8d08f18a Kostas Papadimitriou
59 8d08f18a Kostas Papadimitriou
        subtitle: "",
60 8d08f18a Kostas Papadimitriou
        title: "Machine password",
61 8d08f18a Kostas Papadimitriou
62 8d08f18a Kostas Papadimitriou
        initialize: function(options) {
63 8d08f18a Kostas Papadimitriou
            views.FeedbackView.__super__.initialize.apply(this, arguments);
64 8d08f18a Kostas Papadimitriou
            _.bindAll(this, 'show_password');
65 8d08f18a Kostas Papadimitriou
66 8d08f18a Kostas Papadimitriou
            this.password = this.$("#new-machine-password");
67 35584d80 Kostas Papadimitriou
            this.copy = this.$(".clipboard");
68 60599850 Kostas Papadimitriou
69 60599850 Kostas Papadimitriou
            this.$(".show-machine").click(_.bind(function(){
70 c6e9caec Kostas Papadimitriou
                if (this.$(".show-machine").hasClass("in-progress")) {
71 c6e9caec Kostas Papadimitriou
                    return;
72 c6e9caec Kostas Papadimitriou
                }
73 60599850 Kostas Papadimitriou
                this.hide();
74 60599850 Kostas Papadimitriou
            }, this));
75 c6e9caec Kostas Papadimitriou
76 c6e9caec Kostas Papadimitriou
            _.bindAll(this, "handle_vm_added");
77 c6e9caec Kostas Papadimitriou
            storage.vms.bind("add", this.handle_vm_added);
78 35584d80 Kostas Papadimitriou
            this.password.text("");
79 c6e9caec Kostas Papadimitriou
        },
80 c6e9caec Kostas Papadimitriou
81 c6e9caec Kostas Papadimitriou
        handle_vm_added: function() {
82 c6e9caec Kostas Papadimitriou
            this.$(".show-machine").removeClass("in-progress");
83 8d08f18a Kostas Papadimitriou
        },
84 8d08f18a Kostas Papadimitriou
        
85 8d08f18a Kostas Papadimitriou
        show_password: function() {
86 c6e9caec Kostas Papadimitriou
            this.$(".show-machine").addClass("in-progress");
87 8d08f18a Kostas Papadimitriou
            this.password.text(this.pass);
88 a08a37d2 Kostas Papadimitriou
            if (storage.vms.get(this.vm_id)) {
89 2b2afade Kostas Papadimitriou
                this.$(".show-machine").removeClass("in-progress");
90 2b2afade Kostas Papadimitriou
            }
91 35584d80 Kostas Papadimitriou
            
92 35584d80 Kostas Papadimitriou
            this.clip = new snf.util.ClipHelper(this.copy, this.pass);
93 8d08f18a Kostas Papadimitriou
        },
94 8d08f18a Kostas Papadimitriou
95 8d08f18a Kostas Papadimitriou
        onClose: function() {
96 8d08f18a Kostas Papadimitriou
            this.password.text("");
97 c6e9caec Kostas Papadimitriou
            this.vm_id = undefined;
98 35584d80 Kostas Papadimitriou
            try { delete this.clip; } catch (err) {};
99 8d08f18a Kostas Papadimitriou
        },
100 bd8fa10c Kostas Papadimitriou
        
101 bd8fa10c Kostas Papadimitriou
        beforeOpen: function() {
102 35584d80 Kostas Papadimitriou
            this.copy.empty();
103 bd8fa10c Kostas Papadimitriou
        },
104 bd8fa10c Kostas Papadimitriou
        
105 bd8fa10c Kostas Papadimitriou
        onOpen: function() {
106 35584d80 Kostas Papadimitriou
            this.show_password();
107 bd8fa10c Kostas Papadimitriou
        },
108 bd8fa10c Kostas Papadimitriou
109 8d08f18a Kostas Papadimitriou
        show: function(pass, vm_id) {
110 8d08f18a Kostas Papadimitriou
            this.pass = pass;
111 8d08f18a Kostas Papadimitriou
            this.vm_id = vm_id;
112 c6e9caec Kostas Papadimitriou
            
113 8d08f18a Kostas Papadimitriou
            views.VMCreationPasswordView.__super__.show.apply(this, arguments);
114 8d08f18a Kostas Papadimitriou
        }
115 8d08f18a Kostas Papadimitriou
    })
116 8d08f18a Kostas Papadimitriou
117 8d08f18a Kostas Papadimitriou
118 8d08f18a Kostas Papadimitriou
    
119 8d08f18a Kostas Papadimitriou
    views.CreateVMStepView = views.View.extend({
120 8d08f18a Kostas Papadimitriou
        step: "1",
121 8d08f18a Kostas Papadimitriou
        title: "Image",
122 8d08f18a Kostas Papadimitriou
        submit: false,
123 8d08f18a Kostas Papadimitriou
124 8d08f18a Kostas Papadimitriou
        initialize: function(view) {
125 8d08f18a Kostas Papadimitriou
            this.parent = view;
126 8d08f18a Kostas Papadimitriou
            this.el = view.$("div.create-step-cont.step-" + this.step);
127 820ef2f0 Kostas Papadimitriou
            this.header = this.$(".step-header .step-" + this.step);
128 8d08f18a Kostas Papadimitriou
            this.view_id = "create_step_" + this.step;
129 8d08f18a Kostas Papadimitriou
130 8d08f18a Kostas Papadimitriou
            views.CreateVMStepView.__super__.initialize.apply(this);
131 8d08f18a Kostas Papadimitriou
        },
132 8d08f18a Kostas Papadimitriou
133 8d08f18a Kostas Papadimitriou
        show: function() {
134 8d08f18a Kostas Papadimitriou
            // show current
135 8d08f18a Kostas Papadimitriou
            this.el.show();
136 8d08f18a Kostas Papadimitriou
            this.header.addClass("current");
137 820ef2f0 Kostas Papadimitriou
            this.header.show();
138 8d08f18a Kostas Papadimitriou
            this.update_layout();
139 8d08f18a Kostas Papadimitriou
        },
140 8d08f18a Kostas Papadimitriou
141 8d08f18a Kostas Papadimitriou
        reset: function() {
142 8d08f18a Kostas Papadimitriou
        }
143 8d08f18a Kostas Papadimitriou
    })
144 8d08f18a Kostas Papadimitriou
145 8d08f18a Kostas Papadimitriou
    views.CreateImageSelectView = views.CreateVMStepView.extend({
146 8d08f18a Kostas Papadimitriou
147 8d08f18a Kostas Papadimitriou
        initialize: function() {
148 8d08f18a Kostas Papadimitriou
            views.CreateImageSelectView.__super__.initialize.apply(this, arguments);
149 8d08f18a Kostas Papadimitriou
150 820ef2f0 Kostas Papadimitriou
            // elements
151 73e25ce2 Kostas Papadimitriou
            this.images_list_cont = this.$(".images-list-cont");
152 820ef2f0 Kostas Papadimitriou
            this.images_list = this.$(".images-list-cont ul");
153 820ef2f0 Kostas Papadimitriou
            this.image_details = this.$(".images-info-cont");
154 820ef2f0 Kostas Papadimitriou
            this.image_details_desc = this.$(".images-info-cont .description p");
155 820ef2f0 Kostas Papadimitriou
            this.image_details_title = this.$(".images-info-cont h4");
156 820ef2f0 Kostas Papadimitriou
            this.image_details_size = this.$(".images-info-cont .size p");
157 820ef2f0 Kostas Papadimitriou
            this.image_details_os = this.$(".images-info-cont .os p");
158 820ef2f0 Kostas Papadimitriou
            this.image_details_kernel = this.$(".images-info-cont .kernel p");
159 820ef2f0 Kostas Papadimitriou
            this.image_details_gui = this.$(".images-info-cont .gui p");
160 2cee7cb0 Kostas Papadimitriou
            this.image_details_vm = this.$(".images-info-cont .vm-name p");
161 820ef2f0 Kostas Papadimitriou
162 820ef2f0 Kostas Papadimitriou
            this.categories_list = this.$(".category-filters");
163 2cee7cb0 Kostas Papadimitriou
            
164 820ef2f0 Kostas Papadimitriou
            // params initialization
165 4cff0386 Kostas Papadimitriou
            this.type_selections = {"system": "System"};
166 4cff0386 Kostas Papadimitriou
            this.type_selections_order = ['system'];
167 4cff0386 Kostas Papadimitriou
            
168 4cff0386 Kostas Papadimitriou
            this.images_storage = snf.storage.images;
169 4cff0386 Kostas Papadimitriou
170 4cff0386 Kostas Papadimitriou
            // apply image service specific image types
171 4cff0386 Kostas Papadimitriou
            if (this.images_storage.type_selections) {
172 4cff0386 Kostas Papadimitriou
                this.type_selections = _.extend(
173 4cff0386 Kostas Papadimitriou
                    this.images_storage.type_selections,
174 4cff0386 Kostas Papadimitriou
                    this.type_selections)
175 2cee7cb0 Kostas Papadimitriou
176 4cff0386 Kostas Papadimitriou
                this.type_selections_order = this.images_storage.type_selections_order;
177 4cff0386 Kostas Papadimitriou
            }
178 4cff0386 Kostas Papadimitriou
179 4cff0386 Kostas Papadimitriou
            this.selected_type = undefined;
180 820ef2f0 Kostas Papadimitriou
            this.selected_categories = [];
181 4cff0386 Kostas Papadimitriou
182 820ef2f0 Kostas Papadimitriou
            this.images = [];
183 2cee7cb0 Kostas Papadimitriou
            this.images_ids = [];
184 54235b2e Kostas Papadimitriou
            this.custom_images = [];
185 820ef2f0 Kostas Papadimitriou
186 820ef2f0 Kostas Papadimitriou
            // handlers initialization
187 4cff0386 Kostas Papadimitriou
            this.create_types_selection_options();
188 820ef2f0 Kostas Papadimitriou
            this.init_handlers();
189 820ef2f0 Kostas Papadimitriou
            this.init_position();
190 820ef2f0 Kostas Papadimitriou
        },
191 4cff0386 Kostas Papadimitriou
        
192 820ef2f0 Kostas Papadimitriou
        init_position: function() {
193 820ef2f0 Kostas Papadimitriou
            //this.el.css({position: "absolute"});
194 820ef2f0 Kostas Papadimitriou
            //this.el.css({top:"10px"})
195 820ef2f0 Kostas Papadimitriou
        },
196 820ef2f0 Kostas Papadimitriou
        
197 820ef2f0 Kostas Papadimitriou
        init_handlers: function() {
198 820ef2f0 Kostas Papadimitriou
            var self = this;
199 820ef2f0 Kostas Papadimitriou
            this.types.live("click", function() {
200 820ef2f0 Kostas Papadimitriou
                self.select_type($(this).attr("id").replace("type-select-",""));
201 61d7787c Kostas Papadimitriou
            });
202 4cff0386 Kostas Papadimitriou
            
203 4cff0386 Kostas Papadimitriou
            this.image_details.find(".hide").click(_.bind(function(){
204 4cff0386 Kostas Papadimitriou
                this.hide_image_details();
205 4cff0386 Kostas Papadimitriou
            }, this));
206 61d7787c Kostas Papadimitriou
207 61d7787c Kostas Papadimitriou
            this.$(".register-custom-image").live("click", function(){
208 61d7787c Kostas Papadimitriou
                var confirm_close = true;
209 61d7787c Kostas Papadimitriou
                if (confirm_close) {
210 61d7787c Kostas Papadimitriou
                    snf.ui.main.custom_images_view.show(self.parent);
211 61d7787c Kostas Papadimitriou
                } else {
212 61d7787c Kostas Papadimitriou
                }
213 820ef2f0 Kostas Papadimitriou
            })
214 a020e99e Kostas Papadimitriou
215 a020e99e Kostas Papadimitriou
            $(".image-warning .confirm").bind('click', function(){
216 a020e99e Kostas Papadimitriou
                $(".image-warning").hide();
217 a020e99e Kostas Papadimitriou
                $(".create-controls").show();
218 a020e99e Kostas Papadimitriou
            })
219 820ef2f0 Kostas Papadimitriou
        },
220 820ef2f0 Kostas Papadimitriou
221 4cff0386 Kostas Papadimitriou
        update_images: function(images) {
222 4cff0386 Kostas Papadimitriou
            this.images = images;
223 2cee7cb0 Kostas Papadimitriou
            this.images_ids = _.map(this.images, function(img){return img.id});
224 4cff0386 Kostas Papadimitriou
            return this.images;
225 2cee7cb0 Kostas Papadimitriou
        },
226 2cee7cb0 Kostas Papadimitriou
227 4cff0386 Kostas Papadimitriou
        create_types_selection_options: function() {
228 4cff0386 Kostas Papadimitriou
            var list = this.$("ul.type-filter");
229 4cff0386 Kostas Papadimitriou
            _.each(this.type_selections_order, _.bind(function(key) {
230 4cff0386 Kostas Papadimitriou
                list.append('<li id="type-select-{0}">{1}</li>'.format(key, this.type_selections[key]));
231 4cff0386 Kostas Papadimitriou
            }, this));
232 4cff0386 Kostas Papadimitriou
            this.types = this.$(".type-filter li");
233 54235b2e Kostas Papadimitriou
        },
234 54235b2e Kostas Papadimitriou
235 8d08f18a Kostas Papadimitriou
        update_layout: function() {
236 4cff0386 Kostas Papadimitriou
            if (!this.selected_type) {
237 4cff0386 Kostas Papadimitriou
                this.selected_type = _.keys(this.type_selections)[0];
238 2cee7cb0 Kostas Papadimitriou
            }
239 4cff0386 Kostas Papadimitriou
            this.select_type(this.selected_type);
240 820ef2f0 Kostas Papadimitriou
        },
241 820ef2f0 Kostas Papadimitriou
        
242 820ef2f0 Kostas Papadimitriou
        get_categories: function(images) {
243 820ef2f0 Kostas Papadimitriou
            return [];
244 820ef2f0 Kostas Papadimitriou
            return ["Desktop", "Server", "Linux", "Windows"];
245 820ef2f0 Kostas Papadimitriou
        },
246 820ef2f0 Kostas Papadimitriou
247 820ef2f0 Kostas Papadimitriou
        reset_categories: function() {
248 820ef2f0 Kostas Papadimitriou
            var categories = this.get_categories(this.images);
249 820ef2f0 Kostas Papadimitriou
            this.categories_list.find("li").remove();
250 820ef2f0 Kostas Papadimitriou
251 820ef2f0 Kostas Papadimitriou
            _.each(categories, _.bind(function(cat) {
252 820ef2f0 Kostas Papadimitriou
                var el = $("<li />");
253 820ef2f0 Kostas Papadimitriou
                el.text(cat);
254 820ef2f0 Kostas Papadimitriou
                this.categories_list.append(el);
255 820ef2f0 Kostas Papadimitriou
            }, this));
256 820ef2f0 Kostas Papadimitriou
257 820ef2f0 Kostas Papadimitriou
            if (!categories.length) { 
258 820ef2f0 Kostas Papadimitriou
                this.categories_list.parent().find(".clear").hide();
259 820ef2f0 Kostas Papadimitriou
                this.categories_list.parent().find(".empty").show();
260 820ef2f0 Kostas Papadimitriou
            } else {
261 820ef2f0 Kostas Papadimitriou
                this.categories_list.parent().find(".clear").show();
262 820ef2f0 Kostas Papadimitriou
                this.categories_list.parent().find(".empty").hide();
263 820ef2f0 Kostas Papadimitriou
            }
264 820ef2f0 Kostas Papadimitriou
        },
265 820ef2f0 Kostas Papadimitriou
        
266 4cff0386 Kostas Papadimitriou
        show_loading_view: function() {
267 4cff0386 Kostas Papadimitriou
            this.$(".images-list-cont .empty").hide();
268 4cff0386 Kostas Papadimitriou
            this.images_list.hide();
269 4cff0386 Kostas Papadimitriou
            this.$(".images-list-cont .loading").show();
270 4cff0386 Kostas Papadimitriou
            this.$(".images-list-cont .images-list").hide();
271 4cff0386 Kostas Papadimitriou
            this.reset_categories();
272 4cff0386 Kostas Papadimitriou
            this.update_images([]);
273 4cff0386 Kostas Papadimitriou
            this.reset_images();
274 4cff0386 Kostas Papadimitriou
            this.hide_list_loading();
275 4cff0386 Kostas Papadimitriou
        },
276 4cff0386 Kostas Papadimitriou
277 4cff0386 Kostas Papadimitriou
        hide_loading_view: function(images) {
278 4cff0386 Kostas Papadimitriou
            this.$(".images-list-cont .loading").hide();
279 4cff0386 Kostas Papadimitriou
            this.$(".images-list-cont .images-list").show();
280 4cff0386 Kostas Papadimitriou
            this.reset_categories();
281 4cff0386 Kostas Papadimitriou
            this.update_images(images);
282 4cff0386 Kostas Papadimitriou
            this.reset_images();
283 4cff0386 Kostas Papadimitriou
            this.select_image(this.selected_image);
284 4cff0386 Kostas Papadimitriou
            this.hide_list_loading();
285 2ee94f4c Kostas Papadimitriou
            $(".custom-image-help").hide();
286 2ee94f4c Kostas Papadimitriou
            if (this.selected_type == 'personal' && !images.length) {
287 2ee94f4c Kostas Papadimitriou
                $(".custom-image-help").show();
288 2ee94f4c Kostas Papadimitriou
            }
289 2ee94f4c Kostas Papadimitriou
290 4cff0386 Kostas Papadimitriou
        },
291 4cff0386 Kostas Papadimitriou
292 820ef2f0 Kostas Papadimitriou
        select_type: function(type) {
293 820ef2f0 Kostas Papadimitriou
            this.selected_type = type;
294 820ef2f0 Kostas Papadimitriou
            this.types.removeClass("selected");
295 820ef2f0 Kostas Papadimitriou
            this.types.filter("#type-select-" + this.selected_type).addClass("selected");
296 e472ed67 Kostas Papadimitriou
            this.images_storage.update_images_for_type(
297 4cff0386 Kostas Papadimitriou
                this.selected_type, 
298 4cff0386 Kostas Papadimitriou
                _.bind(this.show_loading_view, this), 
299 4cff0386 Kostas Papadimitriou
                _.bind(this.hide_loading_view, this)
300 4cff0386 Kostas Papadimitriou
            );
301 28958b8e Kostas Papadimitriou
302 61d7787c Kostas Papadimitriou
            this.update_layout_for_type(type);
303 61d7787c Kostas Papadimitriou
        },
304 61d7787c Kostas Papadimitriou
305 61d7787c Kostas Papadimitriou
        update_layout_for_type: function(type) {
306 4cff0386 Kostas Papadimitriou
            if (type != "system") {
307 4cff0386 Kostas Papadimitriou
                this.$(".custom-action").hide();
308 61d7787c Kostas Papadimitriou
            } else {
309 61d7787c Kostas Papadimitriou
                this.$(".custom-action").hide();
310 61d7787c Kostas Papadimitriou
            }
311 4cff0386 Kostas Papadimitriou
312 54235b2e Kostas Papadimitriou
        },
313 54235b2e Kostas Papadimitriou
314 54235b2e Kostas Papadimitriou
        show_list_loading: function() {
315 54235b2e Kostas Papadimitriou
            this.$(".images-list-cont").addClass("loading");
316 54235b2e Kostas Papadimitriou
        },
317 54235b2e Kostas Papadimitriou
318 54235b2e Kostas Papadimitriou
        hide_list_loading: function() {
319 54235b2e Kostas Papadimitriou
            this.$(".images-list-cont").removeClass("loading");
320 8d08f18a Kostas Papadimitriou
        },
321 fc6f43c3 Kostas Papadimitriou
        
322 fc6f43c3 Kostas Papadimitriou
        display_warning_for_image: function(image) {
323 580c3b9b Kostas Papadimitriou
          if (image && !image.is_system_image() && !image.owned_by(synnefo.user)) {
324 fc6f43c3 Kostas Papadimitriou
            $(".create-vm .image-warning").show();
325 a020e99e Kostas Papadimitriou
            $(".create-controls").hide();
326 fc6f43c3 Kostas Papadimitriou
          } else {
327 fc6f43c3 Kostas Papadimitriou
            $(".create-vm .image-warning").hide();
328 a020e99e Kostas Papadimitriou
            $(".create-controls").show();
329 fc6f43c3 Kostas Papadimitriou
          }
330 fc6f43c3 Kostas Papadimitriou
        },
331 8d08f18a Kostas Papadimitriou
332 8d08f18a Kostas Papadimitriou
        select_image: function(image) {
333 e472ed67 Kostas Papadimitriou
            if (image && image.get('id') && !_.include(this.images_ids, image.get('id'))) {
334 e472ed67 Kostas Papadimitriou
                image = undefined;
335 e472ed67 Kostas Papadimitriou
            }
336 820ef2f0 Kostas Papadimitriou
            if (!image && this.images_ids.length) {
337 820ef2f0 Kostas Papadimitriou
                if (this.selected_image && this.images_ids.indexOf(this.selected_image.id) > -1) {
338 820ef2f0 Kostas Papadimitriou
                    image = this.selected_image;
339 820ef2f0 Kostas Papadimitriou
                } else {
340 4cff0386 Kostas Papadimitriou
                    image = this.images_storage.get(this.images_ids[0]);
341 820ef2f0 Kostas Papadimitriou
                }
342 8d08f18a Kostas Papadimitriou
            }
343 e472ed67 Kostas Papadimitriou
             
344 e472ed67 Kostas Papadimitriou
            // no images select null image so that next button gets hidden
345 e472ed67 Kostas Papadimitriou
            if (!this.images_ids.length) { image = undefined };
346 820ef2f0 Kostas Papadimitriou
            
347 2cee7cb0 Kostas Papadimitriou
            if ((!this.selected_image && image) || (this.selected_image != image))
348 2cee7cb0 Kostas Papadimitriou
                this.trigger("change", image);
349 fc6f43c3 Kostas Papadimitriou
                this.display_warning_for_image(image);
350 2cee7cb0 Kostas Papadimitriou
351 8d08f18a Kostas Papadimitriou
            this.selected_image = image;
352 2cee7cb0 Kostas Papadimitriou
                
353 820ef2f0 Kostas Papadimitriou
            if (image) {
354 820ef2f0 Kostas Papadimitriou
                this.images_list.find(".image-details").removeClass("selected");
355 820ef2f0 Kostas Papadimitriou
                this.images_list.find(".image-details#create-vm-image-" + this.selected_image.id).addClass("selected");
356 4cff0386 Kostas Papadimitriou
                this.update_image_details(image);
357 820ef2f0 Kostas Papadimitriou
358 820ef2f0 Kostas Papadimitriou
            } else {
359 820ef2f0 Kostas Papadimitriou
            }
360 cd4d5c5f Kostas Papadimitriou
361 4cff0386 Kostas Papadimitriou
            this.image_details.hide();
362 cd4d5c5f Kostas Papadimitriou
            this.validate();
363 8d08f18a Kostas Papadimitriou
        },
364 8d08f18a Kostas Papadimitriou
365 4cff0386 Kostas Papadimitriou
        update_image_details: function(image) {
366 4cff0386 Kostas Papadimitriou
            this.image_details_desc.hide().parent().hide();
367 1d356257 Kostas Papadimitriou
            if (image.get_description()) {
368 fcd31170 Kostas Papadimitriou
                this.image_details_desc.html(image.get_description(false)).show().parent().show();
369 df251d55 Kostas Papadimitriou
            }
370 df251d55 Kostas Papadimitriou
            var img = snf.ui.helpers.os_icon_tag(image.escape("OS"))
371 df251d55 Kostas Papadimitriou
            if (image.get("name")) {
372 df251d55 Kostas Papadimitriou
                this.image_details_title.html(img + image.escape("name")).show().parent().show();
373 df251d55 Kostas Papadimitriou
            }
374 4cff0386 Kostas Papadimitriou
            
375 4cff0386 Kostas Papadimitriou
            var extra_details = this.image_details.find(".extra-details");
376 4cff0386 Kostas Papadimitriou
            // clean prevously added extra details
377 4cff0386 Kostas Papadimitriou
            extra_details.find(".image-detail").remove();
378 1d356257 Kostas Papadimitriou
            
379 1d356257 Kostas Papadimitriou
            var skip_keys = ['description', 'sortorder']
380 4cff0386 Kostas Papadimitriou
            var meta_keys = ['owner', 'OS', 'kernel', 'GUI'];
381 4cff0386 Kostas Papadimitriou
            var detail_tpl = ('<div class="clearfix image-detail {2}">' +
382 1d356257 Kostas Papadimitriou
                             '<span class="title clearfix">{0}' +
383 1d356257 Kostas Papadimitriou
                             '<span class="custom">custom</span></span>' +
384 4cff0386 Kostas Papadimitriou
                             '<p class="value">{1}</p>' + 
385 4cff0386 Kostas Papadimitriou
                             '</div>');
386 4cff0386 Kostas Papadimitriou
            meta_keys = _.union(meta_keys, this.images_storage.display_metadata || []);
387 1d356257 Kostas Papadimitriou
            
388 1d356257 Kostas Papadimitriou
            var append_metadata_row = function(key, is_extra) {
389 4cff0386 Kostas Papadimitriou
                var value;
390 4cff0386 Kostas Papadimitriou
                var method = 'get_' + key.toLowerCase();
391 30b6f316 Kostas Papadimitriou
                var display_method = 'display_' + key.toLowerCase();
392 1faf0b9c Kostas Papadimitriou
                 
393 30b6f316 Kostas Papadimitriou
                if (image[display_method]) {
394 30b6f316 Kostas Papadimitriou
                    value = image[display_method]();
395 30b6f316 Kostas Papadimitriou
                } else if (image[method]) {
396 4cff0386 Kostas Papadimitriou
                    value = image[method]();
397 4cff0386 Kostas Papadimitriou
                } else {
398 4cff0386 Kostas Papadimitriou
                    value = image.get(key);
399 4cff0386 Kostas Papadimitriou
400 4cff0386 Kostas Papadimitriou
                    if (!value) {
401 4cff0386 Kostas Papadimitriou
                        value = image.get_meta(key);
402 4cff0386 Kostas Papadimitriou
                    }
403 4cff0386 Kostas Papadimitriou
                }
404 1d356257 Kostas Papadimitriou
                    
405 4cff0386 Kostas Papadimitriou
                if (!value) { return; }
406 1d356257 Kostas Papadimitriou
                 
407 1d356257 Kostas Papadimitriou
                var label = this.images_storage.meta_labels[key];
408 1d356257 Kostas Papadimitriou
                if (!label) {
409 1d356257 Kostas Papadimitriou
                    var label = _(key.replace(/_/g," ")).capitalize();
410 1d356257 Kostas Papadimitriou
                }
411 1d356257 Kostas Papadimitriou
                var row_cls = key.toLowerCase();
412 1d356257 Kostas Papadimitriou
                if (is_extra) { row_cls += " extra-meta" };
413 fcd31170 Kostas Papadimitriou
                extra_details.append(detail_tpl.format(_.escape(label), value, row_cls));
414 1d356257 Kostas Papadimitriou
            }
415 1d356257 Kostas Papadimitriou
416 1d356257 Kostas Papadimitriou
            _.each(meta_keys, function(key) {
417 1d356257 Kostas Papadimitriou
                append_metadata_row.apply(this, [key]);
418 1d356257 Kostas Papadimitriou
            }, this);
419 1d356257 Kostas Papadimitriou
            
420 1d356257 Kostas Papadimitriou
            if (synnefo.storage.images.display_extra_metadata) {
421 0dee4086 Kostas Papadimitriou
                _.each(image.get('metadata'), function(value, key) {
422 1d356257 Kostas Papadimitriou
                    if (!_.contains(meta_keys, key) && 
423 1d356257 Kostas Papadimitriou
                        !_.contains(meta_keys, key.toLowerCase()) &&
424 1d356257 Kostas Papadimitriou
                        !_.contains(meta_keys, key.toUpperCase()) &&
425 1d356257 Kostas Papadimitriou
                        !_.contains(skip_keys, key)) {
426 1d356257 Kostas Papadimitriou
                            append_metadata_row.apply(this, [key, true]);
427 1d356257 Kostas Papadimitriou
                    }
428 1d356257 Kostas Papadimitriou
                }, this);
429 1d356257 Kostas Papadimitriou
            }
430 4cff0386 Kostas Papadimitriou
        },
431 4cff0386 Kostas Papadimitriou
432 8d08f18a Kostas Papadimitriou
        reset_images: function() {
433 820ef2f0 Kostas Papadimitriou
            this.images_list.find("li").remove();
434 820ef2f0 Kostas Papadimitriou
            _.each(this.images, _.bind(function(img){
435 8d08f18a Kostas Papadimitriou
                this.add_image(img);
436 8d08f18a Kostas Papadimitriou
            }, this))
437 820ef2f0 Kostas Papadimitriou
            
438 820ef2f0 Kostas Papadimitriou
            if (this.images.length) {
439 820ef2f0 Kostas Papadimitriou
                this.images_list.parent().find(".empty").hide();
440 4cff0386 Kostas Papadimitriou
                this.images_list.show();
441 820ef2f0 Kostas Papadimitriou
            } else {
442 820ef2f0 Kostas Papadimitriou
                this.images_list.parent().find(".empty").show();
443 4cff0386 Kostas Papadimitriou
                this.images_list.hide();
444 820ef2f0 Kostas Papadimitriou
            }
445 8d08f18a Kostas Papadimitriou
446 8d08f18a Kostas Papadimitriou
            var self = this;
447 820ef2f0 Kostas Papadimitriou
            this.images_list.find(".image-details").click(function(){
448 8d08f18a Kostas Papadimitriou
                self.select_image($(this).data("image"));
449 820ef2f0 Kostas Papadimitriou
            });
450 820ef2f0 Kostas Papadimitriou
            
451 820ef2f0 Kostas Papadimitriou
        },
452 820ef2f0 Kostas Papadimitriou
453 820ef2f0 Kostas Papadimitriou
        show: function() {
454 4cff0386 Kostas Papadimitriou
            this.image_details.hide();
455 259d30d8 Kostas Papadimitriou
            this.parent.$(".create-controls").show();
456 259d30d8 Kostas Papadimitriou
457 259d30d8 Kostas Papadimitriou
            views.CreateImageSelectView.__super__.show.apply(this, arguments);
458 8d08f18a Kostas Papadimitriou
        },
459 8d08f18a Kostas Papadimitriou
460 8d08f18a Kostas Papadimitriou
        add_image: function(img) {
461 820ef2f0 Kostas Papadimitriou
            var image = $(('<li id="create-vm-image-{1}"' +
462 4cff0386 Kostas Papadimitriou
                           'class="image-details clearfix">{2}{0}'+
463 4cff0386 Kostas Papadimitriou
                           '<span class="show-details">details</span>'+
464 4cff0386 Kostas Papadimitriou
                           '<span class="size"><span class="prepend">by </span>{5}</span>' + 
465 4cff0386 Kostas Papadimitriou
                           '<span class="owner">' +
466 4cff0386 Kostas Papadimitriou
                           '<span class="prepend"></span>' +
467 4cff0386 Kostas Papadimitriou
                           '{3}</span>' + 
468 4cff0386 Kostas Papadimitriou
                           '<p>{4}</p>' +
469 df251d55 Kostas Papadimitriou
                           '</li>').format(img.escape("name"), 
470 820ef2f0 Kostas Papadimitriou
                                                  img.id, 
471 df251d55 Kostas Papadimitriou
                                                  snf.ui.helpers.os_icon_tag(img.escape("OS")),
472 df251d55 Kostas Papadimitriou
                                                  _.escape(img.get_readable_size()),
473 fcd31170 Kostas Papadimitriou
                                                  util.truncate(img.get_description(false), 35),
474 1faf0b9c Kostas Papadimitriou
                                                  _.escape(img.display_owner())));
475 8d08f18a Kostas Papadimitriou
            image.data("image", img);
476 8d08f18a Kostas Papadimitriou
            image.data("image_id", img.id);
477 820ef2f0 Kostas Papadimitriou
            this.images_list.append(image);
478 4cff0386 Kostas Papadimitriou
            image.find(".show-details").click(_.bind(function(e){
479 4cff0386 Kostas Papadimitriou
                e.preventDefault();
480 4cff0386 Kostas Papadimitriou
                e.stopPropagation();
481 4cff0386 Kostas Papadimitriou
                this.show_image_details(img);
482 4cff0386 Kostas Papadimitriou
            }, this))
483 4cff0386 Kostas Papadimitriou
        },
484 4cff0386 Kostas Papadimitriou
            
485 4cff0386 Kostas Papadimitriou
        hide_image_details: function() {
486 4cff0386 Kostas Papadimitriou
            this.image_details.fadeOut(200);
487 4cff0386 Kostas Papadimitriou
            this.parent.$(".create-controls").show();
488 4cff0386 Kostas Papadimitriou
        },
489 4cff0386 Kostas Papadimitriou
490 4cff0386 Kostas Papadimitriou
        show_image_details: function(img) {
491 4cff0386 Kostas Papadimitriou
            this.parent.$(".create-controls").hide();
492 4cff0386 Kostas Papadimitriou
            this.update_image_details(img);
493 38521f1e Kostas Papadimitriou
            this.image_details.fadeIn(100);
494 8d08f18a Kostas Papadimitriou
        },
495 8d08f18a Kostas Papadimitriou
496 8d08f18a Kostas Papadimitriou
        reset: function() {
497 c9074a14 Kostas Papadimitriou
            this.selected_image = false;
498 2cee7cb0 Kostas Papadimitriou
            this.select_type("system");
499 8d08f18a Kostas Papadimitriou
        },
500 8d08f18a Kostas Papadimitriou
501 8d08f18a Kostas Papadimitriou
        get: function() {
502 8d08f18a Kostas Papadimitriou
            return {'image': this.selected_image};
503 cd4d5c5f Kostas Papadimitriou
        },
504 cd4d5c5f Kostas Papadimitriou
505 cd4d5c5f Kostas Papadimitriou
        validate: function() {
506 cd4d5c5f Kostas Papadimitriou
            if (!this.selected_image) {
507 cd4d5c5f Kostas Papadimitriou
                this.parent.$(".form-action.next").hide();
508 cd4d5c5f Kostas Papadimitriou
            } else {
509 cd4d5c5f Kostas Papadimitriou
                this.parent.$(".form-action.next").show();
510 cd4d5c5f Kostas Papadimitriou
            }
511 8d08f18a Kostas Papadimitriou
        }
512 8d08f18a Kostas Papadimitriou
    });
513 8d08f18a Kostas Papadimitriou
514 8d08f18a Kostas Papadimitriou
    views.CreateFlavorSelectView = views.CreateVMStepView.extend({
515 8d08f18a Kostas Papadimitriou
        step: 2,
516 8d08f18a Kostas Papadimitriou
        initialize: function() {
517 8d08f18a Kostas Papadimitriou
            views.CreateFlavorSelectView.__super__.initialize.apply(this, arguments);
518 8d08f18a Kostas Papadimitriou
            this.parent.bind("image:change", _.bind(this.handle_image_change, this));
519 8d08f18a Kostas Papadimitriou
520 820ef2f0 Kostas Papadimitriou
            this.cpus = this.$(".flavors-cpu-list");
521 820ef2f0 Kostas Papadimitriou
            this.disks = this.$(".flavors-disk-list");
522 d965e6dc Kostas Papadimitriou
            this.disk_templates = this.$(".flavors-disk-template-list");
523 820ef2f0 Kostas Papadimitriou
            this.mems = this.$(".flavors-mem-list");
524 8d08f18a Kostas Papadimitriou
525 820ef2f0 Kostas Papadimitriou
            this.predefined_flavors = SUGGESTED_FLAVORS;
526 2ecab6a2 Kostas Papadimitriou
            this.predefined_flavors_keys = _.keys(SUGGESTED_FLAVORS);
527 2ecab6a2 Kostas Papadimitriou
            this.predefined_flavors_keys = _.sortBy(this.predefined_flavors_keys, _.bind(function(k){
528 2ecab6a2 Kostas Papadimitriou
                var flv = this.predefined_flavors[k];
529 59f43586 Kostas Papadimitriou
                return (flv.ram * flv.cpu * flv.disk);
530 2ecab6a2 Kostas Papadimitriou
            }, this));
531 2ecab6a2 Kostas Papadimitriou
532 820ef2f0 Kostas Papadimitriou
            this.predefined = this.$(".predefined-list");
533 8d08f18a Kostas Papadimitriou
        },
534 8d08f18a Kostas Papadimitriou
535 8d08f18a Kostas Papadimitriou
        handle_image_change: function(data) {
536 8d08f18a Kostas Papadimitriou
            this.current_image = data;
537 820ef2f0 Kostas Papadimitriou
            this.update_valid_predefined();
538 dbddee9c Kostas Papadimitriou
            this.current_flavor = undefined;
539 8d08f18a Kostas Papadimitriou
            this.update_flavors_data();
540 3b463c5a Kostas Papadimitriou
            this.update_predefined_flavors();
541 8d08f18a Kostas Papadimitriou
            this.reset_flavors();
542 8d08f18a Kostas Papadimitriou
            this.update_layout();
543 8d08f18a Kostas Papadimitriou
        },
544 8d08f18a Kostas Papadimitriou
545 283bb7de Kostas Papadimitriou
        validate_selected_flavor: function() {
546 283bb7de Kostas Papadimitriou
            if (!this.flavor_is_valid(this.current_flavor)) {
547 283bb7de Kostas Papadimitriou
                this.select_valid_flavor();
548 283bb7de Kostas Papadimitriou
            }
549 283bb7de Kostas Papadimitriou
        },
550 283bb7de Kostas Papadimitriou
551 8d08f18a Kostas Papadimitriou
        reset_flavors: function() {
552 8d08f18a Kostas Papadimitriou
            this.$(".flavor-opts-list .option").remove();
553 8d08f18a Kostas Papadimitriou
            this.create_flavors();
554 8d08f18a Kostas Papadimitriou
        },
555 8d08f18a Kostas Papadimitriou
556 820ef2f0 Kostas Papadimitriou
        update_predefined_flavors: function() {
557 820ef2f0 Kostas Papadimitriou
            this.predefined.find("li").remove();
558 2ecab6a2 Kostas Papadimitriou
            _.each(this.predefined_flavors_keys, _.bind(function(key) {
559 2ecab6a2 Kostas Papadimitriou
                var val = this.predefined_flavors[key];
560 820ef2f0 Kostas Papadimitriou
                var el = $(('<li class="predefined-selection" id="predefined-flavor-{0}">' +
561 df251d55 Kostas Papadimitriou
                           '{1}</li>').format(key, _.escape(_(key).capitalize())));
562 820ef2f0 Kostas Papadimitriou
563 820ef2f0 Kostas Papadimitriou
                this.predefined.append(el);
564 d965e6dc Kostas Papadimitriou
                el.data({flavor: storage.flavors.get_flavor(val.cpu, val.ram, val.disk, val.disk_template, this.flavors)});
565 820ef2f0 Kostas Papadimitriou
                el.click(_.bind(function() {
566 820ef2f0 Kostas Papadimitriou
                    this.handle_predefined_click(el);
567 820ef2f0 Kostas Papadimitriou
                }, this))
568 820ef2f0 Kostas Papadimitriou
            }, this));
569 820ef2f0 Kostas Papadimitriou
            this.update_valid_predefined();
570 820ef2f0 Kostas Papadimitriou
        },
571 820ef2f0 Kostas Papadimitriou
572 820ef2f0 Kostas Papadimitriou
        handle_predefined_click: function(el) {
573 820ef2f0 Kostas Papadimitriou
            if (el.hasClass("disabled")) { return };
574 16c860f4 Kostas Papadimitriou
            this.set_current(el.data("flavor"));
575 820ef2f0 Kostas Papadimitriou
        },
576 820ef2f0 Kostas Papadimitriou
577 283bb7de Kostas Papadimitriou
        select_valid_flavor: function() {
578 283bb7de Kostas Papadimitriou
            var found = false;
579 283bb7de Kostas Papadimitriou
            var self = this;
580 3b463c5a Kostas Papadimitriou
581 3b463c5a Kostas Papadimitriou
            _.each(["cpu", "mem", "disk"], function(t) {
582 3b463c5a Kostas Papadimitriou
              var el = $(".flavor-options."+t);
583 3b463c5a Kostas Papadimitriou
              var all = el.find(".flavor-opts-list li").length;
584 3b463c5a Kostas Papadimitriou
              var disabled = el.find(".flavor-opts-list li.disabled").length;
585 3b463c5a Kostas Papadimitriou
              if (disabled >= all) {
586 3b463c5a Kostas Papadimitriou
                el.find("h4").addClass("error");
587 3b463c5a Kostas Papadimitriou
              } else {
588 3b463c5a Kostas Papadimitriou
                el.find("h4").removeClass("error");
589 3b463c5a Kostas Papadimitriou
              }
590 3b463c5a Kostas Papadimitriou
            })
591 3b463c5a Kostas Papadimitriou
592 283bb7de Kostas Papadimitriou
            _.each(this.flavors, function(flv) {
593 283bb7de Kostas Papadimitriou
                if (self.flavor_is_valid(flv)) {
594 283bb7de Kostas Papadimitriou
                    found = flv;
595 283bb7de Kostas Papadimitriou
                    return false;
596 283bb7de Kostas Papadimitriou
                }
597 283bb7de Kostas Papadimitriou
            });
598 283bb7de Kostas Papadimitriou
            
599 283bb7de Kostas Papadimitriou
            if (found) {
600 283bb7de Kostas Papadimitriou
                this.set_current(found);
601 283bb7de Kostas Papadimitriou
            } else {
602 283bb7de Kostas Papadimitriou
                this.current_flavor = undefined;
603 283bb7de Kostas Papadimitriou
                this.validate();
604 283bb7de Kostas Papadimitriou
                this.$("li.predefined-selection").addClass("disabled");
605 283bb7de Kostas Papadimitriou
                this.$(".flavor-opts-list li").removeClass("selected");
606 283bb7de Kostas Papadimitriou
            }
607 283bb7de Kostas Papadimitriou
        },
608 283bb7de Kostas Papadimitriou
609 820ef2f0 Kostas Papadimitriou
        update_valid_predefined: function() {
610 820ef2f0 Kostas Papadimitriou
            this.update_unavailable_values();
611 820ef2f0 Kostas Papadimitriou
            var self = this;
612 10f8b7ca Kostas Papadimitriou
            this.valid_predefined = _.select(
613 10f8b7ca Kostas Papadimitriou
              _.map(this.predefined_flavors, function(flv, key){
614 10f8b7ca Kostas Papadimitriou
                var existing = storage.flavors.get_flavor(flv.cpu, 
615 10f8b7ca Kostas Papadimitriou
                                                          flv.ram, 
616 10f8b7ca Kostas Papadimitriou
                                                          flv.disk, 
617 10f8b7ca Kostas Papadimitriou
                                                          flv.disk_template, 
618 10f8b7ca Kostas Papadimitriou
                                                          self.flavors);
619 820ef2f0 Kostas Papadimitriou
                // non existing
620 820ef2f0 Kostas Papadimitriou
                if (!existing) {
621 820ef2f0 Kostas Papadimitriou
                    return false;
622 820ef2f0 Kostas Papadimitriou
                }
623 820ef2f0 Kostas Papadimitriou
                
624 820ef2f0 Kostas Papadimitriou
                // not available for image
625 ac5a4134 Kostas Papadimitriou
                if (self.unavailable_values && self.unavailable_values.disk.indexOf(
626 ac5a4134 Kostas Papadimitriou
                    existing.get("disk")) > -1) {
627 ac5a4134 Kostas Papadimitriou
                      return false
628 820ef2f0 Kostas Papadimitriou
                }
629 10f8b7ca Kostas Papadimitriou
                
630 10f8b7ca Kostas Papadimitriou
                // quota check
631 10f8b7ca Kostas Papadimitriou
                var quotas = synnefo.storage.quotas.get_available_for_vm();
632 10f8b7ca Kostas Papadimitriou
                var unavailable_check = 
633 10f8b7ca Kostas Papadimitriou
                  synnefo.storage.flavors.unavailable_values_for_quotas;
634 10f8b7ca Kostas Papadimitriou
                var unavailable = unavailable_check(quotas, [existing]);
635 10f8b7ca Kostas Papadimitriou
                if ((_.filter(unavailable, function(values, flvkey) {
636 10f8b7ca Kostas Papadimitriou
                  return values.length > 0
637 10f8b7ca Kostas Papadimitriou
                })).length > 0) {
638 10f8b7ca Kostas Papadimitriou
                  return false;
639 10f8b7ca Kostas Papadimitriou
                }
640 10f8b7ca Kostas Papadimitriou
                
641 820ef2f0 Kostas Papadimitriou
                return key;
642 820ef2f0 Kostas Papadimitriou
            }), function(ret) { return ret });
643 820ef2f0 Kostas Papadimitriou
            
644 820ef2f0 Kostas Papadimitriou
            $("li.predefined-selection").addClass("disabled");
645 820ef2f0 Kostas Papadimitriou
            _.each(this.valid_predefined, function(key) {
646 820ef2f0 Kostas Papadimitriou
                $("#predefined-flavor-" + key).removeClass("disabled");
647 820ef2f0 Kostas Papadimitriou
            })
648 820ef2f0 Kostas Papadimitriou
        },
649 820ef2f0 Kostas Papadimitriou
650 820ef2f0 Kostas Papadimitriou
        update_selected_predefined: function() {
651 820ef2f0 Kostas Papadimitriou
            var self = this;
652 820ef2f0 Kostas Papadimitriou
            this.predefined.find("li").removeClass("selected");
653 820ef2f0 Kostas Papadimitriou
654 820ef2f0 Kostas Papadimitriou
            _.each(this.valid_predefined, function(key){
655 820ef2f0 Kostas Papadimitriou
                var flv = self.predefined_flavors[key];
656 d965e6dc Kostas Papadimitriou
                var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, flv.disk_template, self.flavors);
657 820ef2f0 Kostas Papadimitriou
658 820ef2f0 Kostas Papadimitriou
                if (exists && (exists.id == self.current_flavor.id)) {
659 820ef2f0 Kostas Papadimitriou
                    $("#predefined-flavor-" + key).addClass("selected");
660 820ef2f0 Kostas Papadimitriou
                }
661 820ef2f0 Kostas Papadimitriou
            })
662 820ef2f0 Kostas Papadimitriou
        },
663 820ef2f0 Kostas Papadimitriou
        
664 8d08f18a Kostas Papadimitriou
        update_flavors_data: function() {
665 820ef2f0 Kostas Papadimitriou
            this.flavors = storage.flavors.active();
666 8d08f18a Kostas Papadimitriou
            this.flavors_data = storage.flavors.get_data(this.flavors);
667 820ef2f0 Kostas Papadimitriou
            
668 820ef2f0 Kostas Papadimitriou
            var self = this;
669 820ef2f0 Kostas Papadimitriou
            var set = false;
670 820ef2f0 Kostas Papadimitriou
            
671 820ef2f0 Kostas Papadimitriou
            // FIXME: validate current flavor
672 820ef2f0 Kostas Papadimitriou
            
673 820ef2f0 Kostas Papadimitriou
            if (!this.current_flavor) {
674 820ef2f0 Kostas Papadimitriou
                _.each(this.valid_predefined, function(key) {
675 820ef2f0 Kostas Papadimitriou
                    var flv = self.predefined_flavors[key];
676 d965e6dc Kostas Papadimitriou
                    var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, flv.disk_template, self.flavors);
677 820ef2f0 Kostas Papadimitriou
                    if (exists && !set) {
678 820ef2f0 Kostas Papadimitriou
                        self.set_current(exists);
679 820ef2f0 Kostas Papadimitriou
                        set = true;
680 820ef2f0 Kostas Papadimitriou
                    }
681 820ef2f0 Kostas Papadimitriou
                })
682 8d08f18a Kostas Papadimitriou
            }
683 820ef2f0 Kostas Papadimitriou
684 820ef2f0 Kostas Papadimitriou
            this.update_unavailable_values();
685 8d08f18a Kostas Papadimitriou
        },
686 8d08f18a Kostas Papadimitriou
687 820ef2f0 Kostas Papadimitriou
        update_unavailable_values: function() {
688 7ac49c2e Kostas Papadimitriou
            
689 7ac49c2e Kostas Papadimitriou
            var unavailable = {disk:[], ram:[], cpu:[]}
690 7ac49c2e Kostas Papadimitriou
            var user_excluded = {disk:[], ram:[], cpu:[]}
691 7ac49c2e Kostas Papadimitriou
            var image_excluded = {disk:[], ram:[], cpu:[]}
692 7ac49c2e Kostas Papadimitriou
693 7ac49c2e Kostas Papadimitriou
            if (this.current_image) {
694 7ac49c2e Kostas Papadimitriou
              image_excluded = storage.flavors.unavailable_values_for_image(this.current_image);
695 7ac49c2e Kostas Papadimitriou
            }
696 7ac49c2e Kostas Papadimitriou
697 ab3df8df Kostas Papadimitriou
            var quotas = synnefo.storage.quotas.get_available_for_vm({active: true});
698 ab3df8df Kostas Papadimitriou
            var user_excluded = storage.flavors.unavailable_values_for_quotas(quotas);
699 7ac49c2e Kostas Papadimitriou
700 7ac49c2e Kostas Papadimitriou
            unavailable.disk = user_excluded.disk.concat(image_excluded.disk);
701 7ac49c2e Kostas Papadimitriou
            unavailable.ram = user_excluded.ram.concat(image_excluded.ram);
702 7ac49c2e Kostas Papadimitriou
            unavailable.cpu = user_excluded.cpu.concat(image_excluded.cpu);
703 7ac49c2e Kostas Papadimitriou
            
704 7ac49c2e Kostas Papadimitriou
            this.unavailable_values = unavailable;
705 820ef2f0 Kostas Papadimitriou
        },
706 820ef2f0 Kostas Papadimitriou
        
707 820ef2f0 Kostas Papadimitriou
        flavor_is_valid: function(flv) {
708 820ef2f0 Kostas Papadimitriou
            if (!flv) { return false };
709 283bb7de Kostas Papadimitriou
710 d965e6dc Kostas Papadimitriou
            var existing = storage.flavors.get_flavor(flv.get("cpu"), flv.get("ram"), flv.get("disk"), flv.get("disk_template"), this.flavors);
711 820ef2f0 Kostas Papadimitriou
            if (!existing) { return false };
712 283bb7de Kostas Papadimitriou
            
713 ac5a4134 Kostas Papadimitriou
            if (this.unavailable_values && (this.unavailable_values.disk.indexOf(parseInt(flv.get("disk"))) > -1)) {
714 283bb7de Kostas Papadimitriou
                return false;
715 820ef2f0 Kostas Papadimitriou
            }
716 7ac49c2e Kostas Papadimitriou
            if (this.unavailable_values && (this.unavailable_values.ram.indexOf(parseInt(flv.get("ram"))) > -1)) {
717 7ac49c2e Kostas Papadimitriou
                return false;
718 7ac49c2e Kostas Papadimitriou
            }
719 7ac49c2e Kostas Papadimitriou
            if (this.unavailable_values && (this.unavailable_values.cpu.indexOf(parseInt(flv.get("cpu"))) > -1)) {
720 7ac49c2e Kostas Papadimitriou
                return false;
721 7ac49c2e Kostas Papadimitriou
            }
722 820ef2f0 Kostas Papadimitriou
            return true;
723 820ef2f0 Kostas Papadimitriou
        },
724 820ef2f0 Kostas Papadimitriou
            
725 885a592b Kostas Papadimitriou
        set_valid_current_for: function(t, val) {
726 885a592b Kostas Papadimitriou
            var found = this.flavors[0];
727 885a592b Kostas Papadimitriou
            _.each(this.flavors, function(flv) {
728 885a592b Kostas Papadimitriou
                if (flv.get(t) == val) {
729 885a592b Kostas Papadimitriou
                    found = flv;
730 885a592b Kostas Papadimitriou
                }
731 885a592b Kostas Papadimitriou
            });
732 885a592b Kostas Papadimitriou
733 885a592b Kostas Papadimitriou
            this.set_current(found);
734 283bb7de Kostas Papadimitriou
            this.validate_selected_flavor();
735 885a592b Kostas Papadimitriou
        },
736 885a592b Kostas Papadimitriou
737 8d08f18a Kostas Papadimitriou
        set_current: function(flv) {
738 885a592b Kostas Papadimitriou
739 885a592b Kostas Papadimitriou
            if (!flv) {
740 885a592b Kostas Papadimitriou
                // user clicked on invalid combination
741 885a592b Kostas Papadimitriou
                // force the first available choice for the
742 885a592b Kostas Papadimitriou
                // type of option he last clicked
743 885a592b Kostas Papadimitriou
                this.set_valid_current_for.apply(this, this.last_choice);
744 885a592b Kostas Papadimitriou
                return;
745 885a592b Kostas Papadimitriou
            }
746 885a592b Kostas Papadimitriou
747 8d08f18a Kostas Papadimitriou
            this.current_flavor = flv;
748 8d08f18a Kostas Papadimitriou
            this.trigger("change");
749 cd4d5c5f Kostas Papadimitriou
            if (this.current_flavor) {
750 cd4d5c5f Kostas Papadimitriou
                this.update_selected_flavor();
751 cd4d5c5f Kostas Papadimitriou
                this.update_selected_predefined();
752 cd4d5c5f Kostas Papadimitriou
            }
753 cd4d5c5f Kostas Papadimitriou
            
754 cd4d5c5f Kostas Papadimitriou
            this.validate();
755 820ef2f0 Kostas Papadimitriou
        },
756 820ef2f0 Kostas Papadimitriou
        
757 820ef2f0 Kostas Papadimitriou
        select_default_flavor: function() {
758 820ef2f0 Kostas Papadimitriou
               
759 820ef2f0 Kostas Papadimitriou
        },
760 820ef2f0 Kostas Papadimitriou
761 820ef2f0 Kostas Papadimitriou
        update_selected_from_ui: function() {
762 820ef2f0 Kostas Papadimitriou
            this.set_current(this.ui_selected());
763 820ef2f0 Kostas Papadimitriou
        },
764 820ef2f0 Kostas Papadimitriou
        
765 820ef2f0 Kostas Papadimitriou
        update_disabled_flavors: function() {
766 820ef2f0 Kostas Papadimitriou
            this.$(".flavor-options.disk li").removeClass("disabled");
767 820ef2f0 Kostas Papadimitriou
            if (!this.unavailable_values) { return }
768 885a592b Kostas Papadimitriou
            
769 885a592b Kostas Papadimitriou
            this.$("#create-vm-flavor-options .flavor-options.disk li").each(_.bind(function(i, el){
770 ac5a4134 Kostas Papadimitriou
                var el_value = $(el).data("value");
771 820ef2f0 Kostas Papadimitriou
                if (this.unavailable_values.disk.indexOf(el_value) > -1) {
772 820ef2f0 Kostas Papadimitriou
                    $(el).addClass("disabled");
773 7ac49c2e Kostas Papadimitriou
                    $(el).removeClass("selected");
774 7ac49c2e Kostas Papadimitriou
                };
775 7ac49c2e Kostas Papadimitriou
            }, this));
776 7ac49c2e Kostas Papadimitriou
777 426e1fb9 Kostas Papadimitriou
            this.$("#create-vm-flavor-options .flavor-options.mem li").each(_.bind(function(i, el){
778 7ac49c2e Kostas Papadimitriou
                var el_value = $(el).data("value");
779 7ac49c2e Kostas Papadimitriou
                if (this.unavailable_values.ram.indexOf(el_value) > -1) {
780 7ac49c2e Kostas Papadimitriou
                    $(el).addClass("disabled");
781 7ac49c2e Kostas Papadimitriou
                    $(el).removeClass("selected");
782 7ac49c2e Kostas Papadimitriou
                };
783 7ac49c2e Kostas Papadimitriou
            }, this));
784 7ac49c2e Kostas Papadimitriou
785 7ac49c2e Kostas Papadimitriou
            this.$("#create-vm-flavor-options .flavor-options.cpu li").each(_.bind(function(i, el){
786 7ac49c2e Kostas Papadimitriou
                var el_value = $(el).data("value");
787 7ac49c2e Kostas Papadimitriou
                if (this.unavailable_values.cpu.indexOf(el_value) > -1) {
788 7ac49c2e Kostas Papadimitriou
                    $(el).addClass("disabled");
789 7ac49c2e Kostas Papadimitriou
                    $(el).removeClass("selected");
790 820ef2f0 Kostas Papadimitriou
                };
791 820ef2f0 Kostas Papadimitriou
            }, this));
792 8d08f18a Kostas Papadimitriou
        },
793 8d08f18a Kostas Papadimitriou
794 8d08f18a Kostas Papadimitriou
        create_flavors: function() {
795 820ef2f0 Kostas Papadimitriou
            var flavors = this.get_active_flavors();
796 820ef2f0 Kostas Papadimitriou
            var valid_flavors = this.get_valid_flavors();
797 d965e6dc Kostas Papadimitriou
            this.__added_flavors = {'cpu':[], 'ram':[], 'disk':[], 'disk_template':[] };
798 820ef2f0 Kostas Papadimitriou
799 8d08f18a Kostas Papadimitriou
            _.each(flavors, _.bind(function(flv){
800 8d08f18a Kostas Papadimitriou
                this.add_flavor(flv);
801 8d08f18a Kostas Papadimitriou
            }, this));
802 8d08f18a Kostas Papadimitriou
            
803 5f26e13f Kostas Papadimitriou
            this.sort_flavors(this.disks);
804 5f26e13f Kostas Papadimitriou
            this.sort_flavors(this.cpus);
805 5f26e13f Kostas Papadimitriou
            this.sort_flavors(this.mems);
806 d965e6dc Kostas Papadimitriou
            this.sort_flavors(this.disk_templates);
807 5f26e13f Kostas Papadimitriou
808 8d08f18a Kostas Papadimitriou
            var self = this;
809 8d08f18a Kostas Papadimitriou
            this.$(".flavor-options li.option").click(function(){
810 8d08f18a Kostas Papadimitriou
                var el = $(this);
811 820ef2f0 Kostas Papadimitriou
812 820ef2f0 Kostas Papadimitriou
                if (el.hasClass("disabled")) { return }
813 820ef2f0 Kostas Papadimitriou
814 8d08f18a Kostas Papadimitriou
                el.parent().find(".option").removeClass("selected");
815 8d08f18a Kostas Papadimitriou
                el.addClass("selected");
816 7ac49c2e Kostas Papadimitriou
817 885a592b Kostas Papadimitriou
                if (el.hasClass("mem")) { self.last_choice = ["ram", $(this).data("value")] }
818 885a592b Kostas Papadimitriou
                if (el.hasClass("cpu")) { self.last_choice = ["cpu", $(this).data("value")] }
819 885a592b Kostas Papadimitriou
                if (el.hasClass("disk")) { self.last_choice = ["disk", $(this).data("value")] }
820 d965e6dc Kostas Papadimitriou
                if (el.hasClass("disk_template")) { self.last_choice = ["disk_template", $(this).data("value")] }
821 820ef2f0 Kostas Papadimitriou
822 8d08f18a Kostas Papadimitriou
                self.update_selected_from_ui();
823 fb7e2d64 Kostas Papadimitriou
            });
824 d965e6dc Kostas Papadimitriou
825 fb7e2d64 Kostas Papadimitriou
            $(".flavor-opts-list").each(function(){
826 fb7e2d64 Kostas Papadimitriou
              var el = $(this);
827 fb7e2d64 Kostas Papadimitriou
              if (el.find(".option").length > 6) {
828 fb7e2d64 Kostas Papadimitriou
                el.addClass("compact");
829 fb7e2d64 Kostas Papadimitriou
              }
830 fb7e2d64 Kostas Papadimitriou
            });
831 8d08f18a Kostas Papadimitriou
        },
832 5f26e13f Kostas Papadimitriou
833 5f26e13f Kostas Papadimitriou
        sort_flavors: function(els) {
834 5f26e13f Kostas Papadimitriou
            var prev = undefined;
835 5f26e13f Kostas Papadimitriou
            els.find("li").each(function(i,el){
836 5f26e13f Kostas Papadimitriou
                el = $(el);
837 5f26e13f Kostas Papadimitriou
                if (!prev) { prev = el; return true };
838 5f26e13f Kostas Papadimitriou
                if (el.data("value") < prev.data("value")) {
839 5f26e13f Kostas Papadimitriou
                    prev.before(el);
840 5f26e13f Kostas Papadimitriou
                }
841 5f26e13f Kostas Papadimitriou
                prev = el;
842 5f26e13f Kostas Papadimitriou
            })
843 5f26e13f Kostas Papadimitriou
        },
844 8d08f18a Kostas Papadimitriou
        
845 8d08f18a Kostas Papadimitriou
        ui_selected: function() {
846 820ef2f0 Kostas Papadimitriou
            var args = [this.$(".option.cpu.selected").data("value"), 
847 8d08f18a Kostas Papadimitriou
                this.$(".option.mem.selected").data("value"), 
848 8d08f18a Kostas Papadimitriou
                this.$(".option.disk.selected").data("value"),
849 d965e6dc Kostas Papadimitriou
                this.$(".option.disk_template.selected").data("value"),
850 8d08f18a Kostas Papadimitriou
            this.flavors];
851 7ac49c2e Kostas Papadimitriou
            
852 820ef2f0 Kostas Papadimitriou
            var flv = storage.flavors.get_flavor.apply(storage.flavors, args);
853 820ef2f0 Kostas Papadimitriou
            return flv;
854 8d08f18a Kostas Papadimitriou
        },
855 8d08f18a Kostas Papadimitriou
856 8d08f18a Kostas Papadimitriou
        update_selected_flavor: function() {
857 820ef2f0 Kostas Papadimitriou
            var flv = this.current_flavor;
858 cd4d5c5f Kostas Papadimitriou
            if (!flv) { return }
859 820ef2f0 Kostas Papadimitriou
            this.$(".option").removeClass("selected");
860 8d08f18a Kostas Papadimitriou
861 820ef2f0 Kostas Papadimitriou
            this.$(".option.cpu.value-" + flv.get("cpu")).addClass("selected");
862 820ef2f0 Kostas Papadimitriou
            this.$(".option.mem.value-" + flv.get("ram")).addClass("selected");
863 820ef2f0 Kostas Papadimitriou
            this.$(".option.disk.value-" + flv.get("disk")).addClass("selected");
864 d965e6dc Kostas Papadimitriou
            this.$(".option.disk_template.value-" + flv.get("disk_template")).addClass("selected");
865 5afdfc72 Kostas Papadimitriou
            
866 5afdfc72 Kostas Papadimitriou
            var disk_el = this.$(".option.disk_template.value-" + flv.get("disk_template"));
867 5afdfc72 Kostas Papadimitriou
            var basebgpos = 470;
868 5afdfc72 Kostas Papadimitriou
                
869 5afdfc72 Kostas Papadimitriou
            var append_to_bg_pos = 40 + (disk_el.index() * 91);
870 5afdfc72 Kostas Papadimitriou
            var bg_pos = basebgpos - append_to_bg_pos;
871 5afdfc72 Kostas Papadimitriou
872 5afdfc72 Kostas Papadimitriou
            this.$(".disk-template-description").css({backgroundPosition:'-' + bg_pos + 'px top'})
873 5afdfc72 Kostas Papadimitriou
            this.$(".disk-template-description p").html(flv.get_disk_template_info().description || "");
874 8d08f18a Kostas Papadimitriou
        },
875 885a592b Kostas Papadimitriou
        
876 d965e6dc Kostas Papadimitriou
        __added_flavors: {'cpu':[], 'ram':[], 'disk':[], 'disk_template':[]},
877 8d08f18a Kostas Papadimitriou
        add_flavor: function(flv) {
878 d965e6dc Kostas Papadimitriou
            var values = {'cpu': flv.get('cpu'), 
879 d965e6dc Kostas Papadimitriou
                          'mem': flv.get('ram'), 
880 d965e6dc Kostas Papadimitriou
                          'disk': flv.get('disk'), 
881 d965e6dc Kostas Papadimitriou
                          'disk_template': flv.get('disk_template')};
882 820ef2f0 Kostas Papadimitriou
883 820ef2f0 Kostas Papadimitriou
            disabled = "";
884 885a592b Kostas Papadimitriou
            
885 885a592b Kostas Papadimitriou
            if (this.__added_flavors.cpu.indexOf(values.cpu) == -1) {
886 820ef2f0 Kostas Papadimitriou
                var cpu = $(('<li class="option cpu value-{0} {1}">' + 
887 820ef2f0 Kostas Papadimitriou
                             '<span class="value">{0}</span>' + 
888 df251d55 Kostas Papadimitriou
                             '<span class="metric">x</span></li>').format(
889 df251d55 Kostas Papadimitriou
                            _.escape(values.cpu), disabled)).data('value', values.cpu);
890 8d08f18a Kostas Papadimitriou
                this.cpus.append(cpu);
891 885a592b Kostas Papadimitriou
                this.__added_flavors.cpu.push(values.cpu);
892 8d08f18a Kostas Papadimitriou
            }
893 885a592b Kostas Papadimitriou
894 885a592b Kostas Papadimitriou
            if (this.__added_flavors.ram.indexOf(values.mem) == -1) {
895 6653db48 Kostas Papadimitriou
                var mem_value = parseInt(_.escape(values.mem))*1024*1024;
896 6653db48 Kostas Papadimitriou
                var displayvalue = synnefo.util.readablizeBytes(mem_value, 
897 6653db48 Kostas Papadimitriou
                                                               0).split(" ");
898 6653db48 Kostas Papadimitriou
                var mem = $(('<li class="option mem value-{2}">' + 
899 820ef2f0 Kostas Papadimitriou
                             '<span class="value">{0}</span>' + 
900 6653db48 Kostas Papadimitriou
                             '<span class="metric">{1}</span></li>').format(
901 6653db48 Kostas Papadimitriou
                          displayvalue[0], displayvalue[1], values.mem)).data(
902 6653db48 Kostas Papadimitriou
                          'value', values.mem);
903 8d08f18a Kostas Papadimitriou
                this.mems.append(mem);
904 885a592b Kostas Papadimitriou
                this.__added_flavors.ram.push(values.mem);
905 8d08f18a Kostas Papadimitriou
            }
906 885a592b Kostas Papadimitriou
907 885a592b Kostas Papadimitriou
            if (this.__added_flavors.disk.indexOf(values.disk) == -1) {
908 820ef2f0 Kostas Papadimitriou
                var disk = $(('<li class="option disk value-{0}">' + 
909 820ef2f0 Kostas Papadimitriou
                              '<span class="value">{0}</span>' + 
910 df251d55 Kostas Papadimitriou
                              '<span class="metric">GB</span></li>').format(
911 df251d55 Kostas Papadimitriou
                            _.escape(values.disk))).data('value', values.disk);
912 8d08f18a Kostas Papadimitriou
                this.disks.append(disk);
913 885a592b Kostas Papadimitriou
                this.__added_flavors.disk.push(values.disk)
914 8d08f18a Kostas Papadimitriou
            }
915 d965e6dc Kostas Papadimitriou
            
916 d965e6dc Kostas Papadimitriou
            if (this.__added_flavors.disk_template.indexOf(values.disk_template) == -1) {
917 d965e6dc Kostas Papadimitriou
                var template_info = flv.get_disk_template_info();
918 5afdfc72 Kostas Papadimitriou
                var disk_template = $(('<li title="{2}" class="option disk_template value-{0}">' + 
919 5afdfc72 Kostas Papadimitriou
                                       '<span class="value name">{1}</span>' +
920 d965e6dc Kostas Papadimitriou
                                       '</li>').format(values.disk_template, 
921 df251d55 Kostas Papadimitriou
                                            _.escape(template_info.name), 
922 d965e6dc Kostas Papadimitriou
                                            template_info.description)).data('value', 
923 d965e6dc Kostas Papadimitriou
                                                                values.disk_template);
924 d965e6dc Kostas Papadimitriou
925 d965e6dc Kostas Papadimitriou
                this.disk_templates.append(disk_template);
926 5afdfc72 Kostas Papadimitriou
                //disk_template.tooltip({position:'top center', offset:[-5,0], delay:100, tipClass:'tooltip disktip'});
927 d965e6dc Kostas Papadimitriou
                this.__added_flavors.disk_template.push(values.disk_template)
928 d965e6dc Kostas Papadimitriou
            }
929 8d08f18a Kostas Papadimitriou
            
930 8d08f18a Kostas Papadimitriou
        },
931 820ef2f0 Kostas Papadimitriou
        
932 820ef2f0 Kostas Papadimitriou
        get_active_flavors: function() {
933 820ef2f0 Kostas Papadimitriou
            return storage.flavors.active();
934 820ef2f0 Kostas Papadimitriou
        },
935 8d08f18a Kostas Papadimitriou
936 820ef2f0 Kostas Papadimitriou
        get_valid_flavors: function() {
937 8d08f18a Kostas Papadimitriou
            return this.flavors;
938 8d08f18a Kostas Papadimitriou
        },
939 8d08f18a Kostas Papadimitriou
940 8d08f18a Kostas Papadimitriou
        update_layout: function() {
941 820ef2f0 Kostas Papadimitriou
            this.update_selected_flavor();
942 820ef2f0 Kostas Papadimitriou
            this.update_disabled_flavors();
943 cd4d5c5f Kostas Papadimitriou
            this.validate();
944 283bb7de Kostas Papadimitriou
            this.validate_selected_flavor();
945 3b463c5a Kostas Papadimitriou
            this.update_quota_display();
946 3b463c5a Kostas Papadimitriou
        },
947 3b463c5a Kostas Papadimitriou
        
948 3b463c5a Kostas Papadimitriou
        update_quota_display: function() {
949 3b463c5a Kostas Papadimitriou
950 426e1fb9 Kostas Papadimitriou
          var quotas = synnefo.storage.quotas;
951 3b463c5a Kostas Papadimitriou
          _.each(["disk", "ram", "cpu"], function(type) {
952 ab3df8df Kostas Papadimitriou
            var active = true;
953 ab3df8df Kostas Papadimitriou
            var key = 'available';
954 ab3df8df Kostas Papadimitriou
            var available_dsp = quotas.get('cyclades.'+type).get_readable(key, active);
955 b76769ef Kostas Papadimitriou
            var available = quotas.get('cyclades.'+type).get_available(key);
956 3b463c5a Kostas Papadimitriou
            var content = "({0} left)".format(available_dsp);
957 3b463c5a Kostas Papadimitriou
            if (available <= 0) { content = "(None left)" }
958 3b463c5a Kostas Papadimitriou
            
959 3b463c5a Kostas Papadimitriou
            if (type == "ram") { type = "mem" }
960 3b463c5a Kostas Papadimitriou
            $(".flavor-options."+type+" h4 .available").text(content);
961 3b463c5a Kostas Papadimitriou
            if (available <= 0) {
962 3b463c5a Kostas Papadimitriou
              $(".flavor-options."+type+" h4 .available").addClass("error");
963 3b463c5a Kostas Papadimitriou
            } else {
964 3b463c5a Kostas Papadimitriou
              $(".flavor-options."+type+" h4 .available").removeClass("error");
965 3b463c5a Kostas Papadimitriou
            }
966 3b463c5a Kostas Papadimitriou
          })
967 8d08f18a Kostas Papadimitriou
        },
968 8d08f18a Kostas Papadimitriou
969 8d08f18a Kostas Papadimitriou
        reset: function() {
970 8d08f18a Kostas Papadimitriou
            this.current_image = storage.images.at(0);
971 8d08f18a Kostas Papadimitriou
            this.flavors = [];
972 8d08f18a Kostas Papadimitriou
            this.flavors_data = {'cpu':[], 'mem':[], 'disk':[]};
973 8d08f18a Kostas Papadimitriou
            this.update_flavors_data();
974 8d08f18a Kostas Papadimitriou
        },
975 8d08f18a Kostas Papadimitriou
976 cd4d5c5f Kostas Papadimitriou
        validate: function() {
977 cd4d5c5f Kostas Papadimitriou
            if (!this.current_flavor) {
978 cd4d5c5f Kostas Papadimitriou
                this.parent.$(".form-action.next").hide();
979 cd4d5c5f Kostas Papadimitriou
            } else {
980 cd4d5c5f Kostas Papadimitriou
                this.parent.$(".form-action.next").show();
981 cd4d5c5f Kostas Papadimitriou
            }
982 cd4d5c5f Kostas Papadimitriou
        },
983 cd4d5c5f Kostas Papadimitriou
984 8d08f18a Kostas Papadimitriou
        get: function() {
985 d965e6dc Kostas Papadimitriou
            return {'flavor': this.current_flavor}
986 8d08f18a Kostas Papadimitriou
        }
987 c566f369 Kostas Papadimitriou
    });
988 c566f369 Kostas Papadimitriou
    
989 c566f369 Kostas Papadimitriou
990 c566f369 Kostas Papadimitriou
    views.CreateColumnSelectOptionView = bb.View.extend({
991 c566f369 Kostas Papadimitriou
        tagName: 'li',
992 c566f369 Kostas Papadimitriou
        el: undefined,
993 c566f369 Kostas Papadimitriou
        model: undefined,
994 c566f369 Kostas Papadimitriou
        id_prefix: 'model-',
995 c566f369 Kostas Papadimitriou
        tpl: '<input type="checkbox" class="check"/><span class="title"></span>',
996 c566f369 Kostas Papadimitriou
        className: 'list-item-option clearfix',
997 c566f369 Kostas Papadimitriou
        events: {
998 c566f369 Kostas Papadimitriou
          'click': 'handle_click'
999 c566f369 Kostas Papadimitriou
        },
1000 8d08f18a Kostas Papadimitriou
1001 c566f369 Kostas Papadimitriou
        initialize: function(options) {
1002 c566f369 Kostas Papadimitriou
          _.bindAll(this);
1003 c566f369 Kostas Papadimitriou
          this.model.bind("change", this.render);
1004 c566f369 Kostas Papadimitriou
          this.model.bind("remove", this.remove);
1005 c566f369 Kostas Papadimitriou
          this.selected = false;
1006 c566f369 Kostas Papadimitriou
          if (options.get_model_title) {
1007 c566f369 Kostas Papadimitriou
            this.get_model_title = _.bind(options.get_model_title, this);
1008 c566f369 Kostas Papadimitriou
          }
1009 c566f369 Kostas Papadimitriou
          this.model_title_attr = options.model_title_attr;
1010 c566f369 Kostas Papadimitriou
          $(this.el).append($(this.tpl));
1011 c566f369 Kostas Papadimitriou
        },
1012 c566f369 Kostas Papadimitriou
        
1013 c566f369 Kostas Papadimitriou
        id: function() {
1014 c566f369 Kostas Papadimitriou
          return this.id_prefix + this.model && this.model.id || '';
1015 c566f369 Kostas Papadimitriou
        },
1016 c566f369 Kostas Papadimitriou
        
1017 c566f369 Kostas Papadimitriou
        handle_click: function() {
1018 c566f369 Kostas Papadimitriou
          this.selected = !this.selected;
1019 c566f369 Kostas Papadimitriou
          this.render();
1020 c566f369 Kostas Papadimitriou
        },
1021 c566f369 Kostas Papadimitriou
1022 c566f369 Kostas Papadimitriou
        remove: function() {
1023 c566f369 Kostas Papadimitriou
          this.model.unbind("change", this.render);
1024 c566f369 Kostas Papadimitriou
          this.model.unbind("remove", this.remove);
1025 c566f369 Kostas Papadimitriou
        },
1026 c566f369 Kostas Papadimitriou
        
1027 c566f369 Kostas Papadimitriou
        get_model_title: function() {
1028 c566f369 Kostas Papadimitriou
          return this.model.get(this.model_title_attr || 'id');
1029 c566f369 Kostas Papadimitriou
        },
1030 c566f369 Kostas Papadimitriou
1031 c566f369 Kostas Papadimitriou
        render: function() {
1032 c566f369 Kostas Papadimitriou
          $(this.el).find(".title").text(this.get_model_title());
1033 c566f369 Kostas Papadimitriou
          $(this.el).toggleClass('selected', this.selected);
1034 c566f369 Kostas Papadimitriou
          if (this.selected) {
1035 c566f369 Kostas Papadimitriou
            $(this.el).find("input").attr("checked", true);
1036 c566f369 Kostas Papadimitriou
          } else {
1037 c566f369 Kostas Papadimitriou
            $(this.el).find("input").attr("checked", false);
1038 c566f369 Kostas Papadimitriou
          }
1039 c566f369 Kostas Papadimitriou
        }
1040 c566f369 Kostas Papadimitriou
    });
1041 c566f369 Kostas Papadimitriou
    
1042 c566f369 Kostas Papadimitriou
    views.CreateColumnIPOptionView = views.CreateColumnSelectOptionView.extend({
1043 c566f369 Kostas Papadimitriou
      get_model_title: function() {
1044 c566f369 Kostas Papadimitriou
        return this.model.get('ip');
1045 c566f369 Kostas Papadimitriou
      }
1046 8d08f18a Kostas Papadimitriou
    });
1047 8d08f18a Kostas Papadimitriou
1048 c566f369 Kostas Papadimitriou
    views.CreateColumnPrivateNetworkOptionView = views.CreateColumnSelectOptionView.extend({
1049 c566f369 Kostas Papadimitriou
      get_model_title: function() {
1050 c566f369 Kostas Papadimitriou
        return this.model.get('name');
1051 c566f369 Kostas Papadimitriou
      }
1052 c566f369 Kostas Papadimitriou
    });
1053 c566f369 Kostas Papadimitriou
1054 c566f369 Kostas Papadimitriou
    views.CreateColumnSelectListView = bb.View.extend({
1055 c566f369 Kostas Papadimitriou
        collection: undefined,
1056 c566f369 Kostas Papadimitriou
        header: undefined,
1057 c566f369 Kostas Papadimitriou
        tagName: 'div',
1058 c566f369 Kostas Papadimitriou
        extra_class: '',
1059 c566f369 Kostas Papadimitriou
        el: undefined,
1060 c566f369 Kostas Papadimitriou
        title_tpl: undefined,
1061 c566f369 Kostas Papadimitriou
        title: 'List view',
1062 c566f369 Kostas Papadimitriou
        description: 'List view description.',
1063 c566f369 Kostas Papadimitriou
        empty_msg: 'No entries.',
1064 c566f369 Kostas Papadimitriou
        item_cls: views.CreateColumnSelectOptionView,
1065 c566f369 Kostas Papadimitriou
        className: 'list-cont create-column-select personalize-cont',
1066 c566f369 Kostas Papadimitriou
1067 c566f369 Kostas Papadimitriou
        initialize: function(options) {
1068 c566f369 Kostas Papadimitriou
          _.bindAll(this);
1069 c566f369 Kostas Papadimitriou
          if (options.extra_class) {
1070 c566f369 Kostas Papadimitriou
            $(this.el).addClass(options.extra_class);
1071 c566f369 Kostas Papadimitriou
          }
1072 c566f369 Kostas Papadimitriou
          this.update_collection = options.update_collection;
1073 c566f369 Kostas Papadimitriou
          this.title = options.title || this.title;
1074 c566f369 Kostas Papadimitriou
          this.titple_tpl = options.title_tpl || this.title_tpl;
1075 c566f369 Kostas Papadimitriou
          this.description = options.description || this.description;
1076 c566f369 Kostas Papadimitriou
          this.empty_msg = options.empty_msg || this.empty_msg;
1077 c566f369 Kostas Papadimitriou
          this.item_cls = options.item_cls || this.item_cls;
1078 c566f369 Kostas Papadimitriou
          this.select_first_as_default = options.select_first_as_default;
1079 c566f369 Kostas Papadimitriou
          this.filter_items = options.filter_items;
1080 c566f369 Kostas Papadimitriou
          this.post_render_entries = options.post_render_entries || function() {};
1081 c566f369 Kostas Papadimitriou
          this.init_events = options.init_events || function() {};
1082 c566f369 Kostas Papadimitriou
1083 c566f369 Kostas Papadimitriou
          this.init_events = _.bind(this.init_events, this);
1084 c566f369 Kostas Papadimitriou
          this.post_render_entries = _.bind(this.post_render_entries, this);
1085 c566f369 Kostas Papadimitriou
1086 c566f369 Kostas Papadimitriou
          this._ul = $('<ul class="confirm-params">');
1087 c566f369 Kostas Papadimitriou
          this._title = $("<h4>");
1088 c566f369 Kostas Papadimitriou
          this._description = $("<p class='desc'>");
1089 c566f369 Kostas Papadimitriou
          this._empty = $("<p class='empty hidden desc'>");
1090 c566f369 Kostas Papadimitriou
          this._empty.html(this.empty_msg);
1091 c566f369 Kostas Papadimitriou
        
1092 c566f369 Kostas Papadimitriou
          this.item_views = [];
1093 c566f369 Kostas Papadimitriou
1094 c566f369 Kostas Papadimitriou
          $(this.el).append(this._title);
1095 c566f369 Kostas Papadimitriou
          $(this.el).append(this._description);
1096 c566f369 Kostas Papadimitriou
          $(this.el).append(this._empty);
1097 c566f369 Kostas Papadimitriou
          $(this.el).append(this._ul);
1098 c566f369 Kostas Papadimitriou
1099 c566f369 Kostas Papadimitriou
          this['$el'] = $(this.el);
1100 c566f369 Kostas Papadimitriou
1101 c566f369 Kostas Papadimitriou
          if (!this.title_tpl) { this.title_tpl = this.title };
1102 c566f369 Kostas Papadimitriou
1103 c566f369 Kostas Papadimitriou
          this.collection.bind("change", this.render_entries);
1104 c566f369 Kostas Papadimitriou
          this.collection.bind("reset", this.render_entries);
1105 c566f369 Kostas Papadimitriou
          this.collection.bind("add", this.render_entries);
1106 c566f369 Kostas Papadimitriou
          this.collection.bind("remove", this.remove_entry);
1107 c566f369 Kostas Papadimitriou
          
1108 c566f369 Kostas Papadimitriou
          this.fetcher = undefined;
1109 c566f369 Kostas Papadimitriou
          if (this.update_collection) {
1110 c68ad72e Kostas Papadimitriou
              this.fetcher_params = [snf.config.update_interval, 
1111 c68ad72e Kostas Papadimitriou
                    snf.config.update_interval_increase || 500,
1112 c68ad72e Kostas Papadimitriou
                    snf.config.fast_interval || snf.config.update_interval/2, 
1113 c68ad72e Kostas Papadimitriou
                    snf.config.update_interval_increase_after_calls || 4,
1114 c68ad72e Kostas Papadimitriou
                    snf.config.update_interval_max || 20000,
1115 c68ad72e Kostas Papadimitriou
                    true, 
1116 c68ad72e Kostas Papadimitriou
                    {is_recurrent: true, update: true}];
1117 c566f369 Kostas Papadimitriou
              this.fetcher = this.collection.get_fetcher.apply(this.collection, 
1118 c566f369 Kostas Papadimitriou
                                                _.clone(this.fetcher_params));
1119 c566f369 Kostas Papadimitriou
              this.fetcher.start();
1120 c566f369 Kostas Papadimitriou
          }
1121 c566f369 Kostas Papadimitriou
          this.render();
1122 c566f369 Kostas Papadimitriou
          this.init_events();
1123 c566f369 Kostas Papadimitriou
        },
1124 c566f369 Kostas Papadimitriou
        
1125 c566f369 Kostas Papadimitriou
        render: function() {
1126 c566f369 Kostas Papadimitriou
          this._title.html(this.title_tpl);
1127 c566f369 Kostas Papadimitriou
          this._description.html(this.description);
1128 c566f369 Kostas Papadimitriou
          this.render_entries();
1129 c566f369 Kostas Papadimitriou
        },
1130 c566f369 Kostas Papadimitriou
        
1131 c566f369 Kostas Papadimitriou
        remove_entry: function(model) {
1132 c566f369 Kostas Papadimitriou
          if (!this.item_views[model.id]) { return }
1133 c566f369 Kostas Papadimitriou
          this.item_views[model.id].remove();
1134 c566f369 Kostas Papadimitriou
          delete this.item_views[model.pk]
1135 c566f369 Kostas Papadimitriou
        },
1136 c566f369 Kostas Papadimitriou
        
1137 c566f369 Kostas Papadimitriou
        get_selected: function() {
1138 c566f369 Kostas Papadimitriou
          return _.map(_.filter(this.item_views, function(v) { 
1139 c566f369 Kostas Papadimitriou
            return v.selected
1140 c566f369 Kostas Papadimitriou
          }), function(v) {
1141 c566f369 Kostas Papadimitriou
            return v.model
1142 c566f369 Kostas Papadimitriou
          });
1143 c566f369 Kostas Papadimitriou
        },
1144 c566f369 Kostas Papadimitriou
        
1145 c566f369 Kostas Papadimitriou
        check_empty: function() {
1146 c566f369 Kostas Papadimitriou
          if (this.item_views.length == 0) {
1147 c566f369 Kostas Papadimitriou
            this._empty.show();
1148 c566f369 Kostas Papadimitriou
          } else {
1149 c566f369 Kostas Papadimitriou
            this._empty.hide();
1150 c566f369 Kostas Papadimitriou
          }
1151 c566f369 Kostas Papadimitriou
        },
1152 c566f369 Kostas Papadimitriou
1153 c566f369 Kostas Papadimitriou
        render_entries: function() {
1154 c566f369 Kostas Papadimitriou
          var entries;
1155 c566f369 Kostas Papadimitriou
          if (this.filter_items) {
1156 c566f369 Kostas Papadimitriou
            entries = this.collection.filter(this.filter_items);
1157 c566f369 Kostas Papadimitriou
          } else {
1158 c566f369 Kostas Papadimitriou
            entries = this.collection.models;
1159 c566f369 Kostas Papadimitriou
          }
1160 c566f369 Kostas Papadimitriou
          
1161 c566f369 Kostas Papadimitriou
          var selected = this.get_selected();
1162 c566f369 Kostas Papadimitriou
          
1163 c566f369 Kostas Papadimitriou
          _.each(entries, _.bind(function(model) {
1164 c566f369 Kostas Papadimitriou
            if (this.item_views[model.id]) {
1165 c566f369 Kostas Papadimitriou
              this.item_views[model.id].render();
1166 c566f369 Kostas Papadimitriou
            } else {
1167 c566f369 Kostas Papadimitriou
              var view = new this.item_cls({model:model});
1168 c566f369 Kostas Papadimitriou
              if (!selected.length && this.select_first_as_default) { 
1169 c566f369 Kostas Papadimitriou
                view.selected = true; selected = [1] 
1170 c566f369 Kostas Papadimitriou
              }
1171 c566f369 Kostas Papadimitriou
              view.render();
1172 c566f369 Kostas Papadimitriou
              this.item_views[model.id] = view;
1173 c566f369 Kostas Papadimitriou
              this._ul.append($(view.el));
1174 c566f369 Kostas Papadimitriou
            }
1175 c566f369 Kostas Papadimitriou
          }, this));
1176 c566f369 Kostas Papadimitriou
          this.check_empty();
1177 c566f369 Kostas Papadimitriou
          this.post_render_entries();
1178 c566f369 Kostas Papadimitriou
        },
1179 c566f369 Kostas Papadimitriou
1180 c566f369 Kostas Papadimitriou
        remove: function() {
1181 c566f369 Kostas Papadimitriou
          _.each(this.item_views, function(v){
1182 c566f369 Kostas Papadimitriou
            v.remove();
1183 c566f369 Kostas Papadimitriou
          });
1184 c566f369 Kostas Papadimitriou
          if (this.update_collection) { this.fetcher.stop() }
1185 c566f369 Kostas Papadimitriou
          this.unbind();
1186 c566f369 Kostas Papadimitriou
          this.collection.unbind("change", this.render_entries);
1187 c566f369 Kostas Papadimitriou
          this.collection.unbind("reset", this.render_entries);
1188 c566f369 Kostas Papadimitriou
          this.collection.unbind("add", this.render_entries);
1189 c566f369 Kostas Papadimitriou
          this.collection.unbind("remove", this.remove_entry);
1190 c566f369 Kostas Papadimitriou
          views.CreateColumnSelectListView.__super__.remove.apply(this, arguments);
1191 c566f369 Kostas Papadimitriou
        }
1192 c566f369 Kostas Papadimitriou
    });
1193 c566f369 Kostas Papadimitriou
1194 c566f369 Kostas Papadimitriou
    views.CreateNetworkingView = views.CreateVMStepView.extend({
1195 8d08f18a Kostas Papadimitriou
        step: 3,
1196 8d08f18a Kostas Papadimitriou
        initialize: function() {
1197 c566f369 Kostas Papadimitriou
            views.CreateNetworkingView.__super__.initialize.apply(this, arguments);
1198 c566f369 Kostas Papadimitriou
            this.init_handlers();
1199 c566f369 Kostas Papadimitriou
            this.selected_keys = [];
1200 c68ad72e Kostas Papadimitriou
            this.cont = this.$(".step-cont");
1201 c566f369 Kostas Papadimitriou
        },
1202 c566f369 Kostas Papadimitriou
        
1203 c566f369 Kostas Papadimitriou
        init_subviews: function() {
1204 c566f369 Kostas Papadimitriou
            var create_view = this.parent;
1205 c566f369 Kostas Papadimitriou
            if (!this.networks_view) {
1206 c68ad72e Kostas Papadimitriou
              this.networks_view = new views.NetworkSelectView({
1207 c68ad72e Kostas Papadimitriou
                container: this.cont
1208 c566f369 Kostas Papadimitriou
              });
1209 c68ad72e Kostas Papadimitriou
              this.networks_view.hide(true);
1210 c566f369 Kostas Papadimitriou
            }
1211 c566f369 Kostas Papadimitriou
        },
1212 c566f369 Kostas Papadimitriou
1213 c566f369 Kostas Papadimitriou
        init_handlers: function() {
1214 c566f369 Kostas Papadimitriou
        },
1215 c566f369 Kostas Papadimitriou
1216 c566f369 Kostas Papadimitriou
        show: function() {
1217 c566f369 Kostas Papadimitriou
            views.CreateNetworkingView.__super__.show.apply(this, arguments);
1218 c566f369 Kostas Papadimitriou
            this.init_subviews();
1219 c566f369 Kostas Papadimitriou
            this.update_layout();
1220 c68ad72e Kostas Papadimitriou
            this.networks_view.show(true);
1221 c566f369 Kostas Papadimitriou
        },
1222 c566f369 Kostas Papadimitriou
        
1223 c68ad72e Kostas Papadimitriou
        hide_step: function() {
1224 c68ad72e Kostas Papadimitriou
            this.networks_view && this.networks_view.hide(true);
1225 c68ad72e Kostas Papadimitriou
        },
1226 c68ad72e Kostas Papadimitriou
1227 c566f369 Kostas Papadimitriou
        update_layout: function() {
1228 c566f369 Kostas Papadimitriou
        },
1229 c566f369 Kostas Papadimitriou
1230 c566f369 Kostas Papadimitriou
        reset: function() {
1231 c566f369 Kostas Papadimitriou
            this.selected_keys = [];
1232 c566f369 Kostas Papadimitriou
            this.update_layout();
1233 c566f369 Kostas Papadimitriou
        },
1234 c566f369 Kostas Papadimitriou
        
1235 c566f369 Kostas Papadimitriou
        get_selected_networks: function() {
1236 c68ad72e Kostas Papadimitriou
            if (!this.networks_view) { return [] }
1237 c68ad72e Kostas Papadimitriou
            return this.networks_view.get_selected_networks();
1238 c566f369 Kostas Papadimitriou
        },
1239 c566f369 Kostas Papadimitriou
        
1240 c566f369 Kostas Papadimitriou
        get_selected_addresses: function() {
1241 c68ad72e Kostas Papadimitriou
            if (!this.networks_view) { return [] }
1242 c68ad72e Kostas Papadimitriou
            return this.networks_view.get_selected_floating_ips();
1243 c566f369 Kostas Papadimitriou
        },
1244 c566f369 Kostas Papadimitriou
1245 c566f369 Kostas Papadimitriou
        get: function() {
1246 c566f369 Kostas Papadimitriou
            return {
1247 c566f369 Kostas Papadimitriou
              'addresses': this.get_selected_addresses(),
1248 c566f369 Kostas Papadimitriou
              'networks': this.get_selected_networks()
1249 c566f369 Kostas Papadimitriou
            }
1250 c566f369 Kostas Papadimitriou
        },
1251 c566f369 Kostas Papadimitriou
1252 c566f369 Kostas Papadimitriou
        remove: function() {
1253 c566f369 Kostas Papadimitriou
          if (this.networks_view) {
1254 c566f369 Kostas Papadimitriou
            this.networks_view.remove();
1255 c566f369 Kostas Papadimitriou
            delete this.networks_view;
1256 c566f369 Kostas Papadimitriou
          }
1257 c566f369 Kostas Papadimitriou
        }
1258 c566f369 Kostas Papadimitriou
    });
1259 c566f369 Kostas Papadimitriou
1260 c566f369 Kostas Papadimitriou
    views.CreatePersonalizeView = views.CreateVMStepView.extend({
1261 c566f369 Kostas Papadimitriou
        step: 4,
1262 c566f369 Kostas Papadimitriou
        initialize: function() {
1263 8d08f18a Kostas Papadimitriou
            views.CreateSubmitView.__super__.initialize.apply(this, arguments);
1264 73e25ce2 Kostas Papadimitriou
            this.roles = this.$("li.predefined-meta.role .values");
1265 73e25ce2 Kostas Papadimitriou
            this.name = this.$("input.rename-field");
1266 8d08f18a Kostas Papadimitriou
            this.name_changed = false;
1267 73e25ce2 Kostas Papadimitriou
            this.init_suggested_roles();
1268 8d08f18a Kostas Papadimitriou
            this.init_handlers();
1269 47276ec2 Kostas Papadimitriou
            this.ssh_list = this.$(".ssh ul");
1270 47276ec2 Kostas Papadimitriou
            this.selected_keys = [];
1271 47276ec2 Kostas Papadimitriou
1272 47276ec2 Kostas Papadimitriou
            var self = this;
1273 8d08f18a Kostas Papadimitriou
        },
1274 8d08f18a Kostas Papadimitriou
1275 73e25ce2 Kostas Papadimitriou
        init_suggested_roles: function() {
1276 73e25ce2 Kostas Papadimitriou
            var cont = this.roles;
1277 73e25ce2 Kostas Papadimitriou
            cont.empty();
1278 73e25ce2 Kostas Papadimitriou
            
1279 73e25ce2 Kostas Papadimitriou
            // TODO: get suggested from snf.api.conf
1280 73e25ce2 Kostas Papadimitriou
            _.each(window.SUGGESTED_ROLES, function(r){
1281 df251d55 Kostas Papadimitriou
                var el = $('<span class="val">{0}</span>'.format(_.escape(r)));
1282 73e25ce2 Kostas Papadimitriou
                el.data("value", r);
1283 73e25ce2 Kostas Papadimitriou
                cont.append(el);
1284 73e25ce2 Kostas Papadimitriou
                el.click(function() {
1285 73e25ce2 Kostas Papadimitriou
                    $(this).parent().find(".val").removeClass("selected");
1286 73e25ce2 Kostas Papadimitriou
                    $(this).toggleClass("selected");
1287 73e25ce2 Kostas Papadimitriou
                })
1288 47276ec2 Kostas Papadimitriou
            });
1289 47276ec2 Kostas Papadimitriou
            
1290 47276ec2 Kostas Papadimitriou
            var self = this;
1291 47276ec2 Kostas Papadimitriou
            $(".ssh li.ssh-key-option").live("click", function(e) {
1292 47276ec2 Kostas Papadimitriou
                var key = $(this).data("model");
1293 47276ec2 Kostas Papadimitriou
                self.select_key(key);
1294 47276ec2 Kostas Papadimitriou
            });
1295 47276ec2 Kostas Papadimitriou
        },
1296 47276ec2 Kostas Papadimitriou
1297 47276ec2 Kostas Papadimitriou
        select_key: function(key) {
1298 47276ec2 Kostas Papadimitriou
            var exists = this.selected_keys.indexOf(key.id);
1299 47276ec2 Kostas Papadimitriou
            if (exists > -1) {
1300 47276ec2 Kostas Papadimitriou
                this.selected_keys.splice(exists, 1);
1301 47276ec2 Kostas Papadimitriou
            } else {
1302 47276ec2 Kostas Papadimitriou
                this.selected_keys.push(key.id);
1303 47276ec2 Kostas Papadimitriou
            }
1304 47276ec2 Kostas Papadimitriou
            this.update_ui_keys_selections(this.selected_keys);
1305 47276ec2 Kostas Papadimitriou
        },
1306 47276ec2 Kostas Papadimitriou
1307 47276ec2 Kostas Papadimitriou
        update_ui_keys_selections: function(keys) {
1308 47276ec2 Kostas Papadimitriou
            var self = this;
1309 47276ec2 Kostas Papadimitriou
            self.$(".ssh-key-option").removeClass("selected");
1310 47276ec2 Kostas Papadimitriou
            self.$(".ssh-key-option .check").attr("checked", false);
1311 47276ec2 Kostas Papadimitriou
            _.each(keys, function(kid) {
1312 47276ec2 Kostas Papadimitriou
                $("#ssh-key-option-" + kid).addClass("selected");
1313 47276ec2 Kostas Papadimitriou
                $("#ssh-key-option-" + kid).find(".check").attr("checked", true);
1314 47276ec2 Kostas Papadimitriou
            });
1315 47276ec2 Kostas Papadimitriou
        },
1316 47276ec2 Kostas Papadimitriou
1317 47276ec2 Kostas Papadimitriou
        update_ssh_keys: function() {
1318 47276ec2 Kostas Papadimitriou
            this.ssh_list.empty();
1319 47276ec2 Kostas Papadimitriou
            var keys = snf.storage.keys.models;
1320 47276ec2 Kostas Papadimitriou
            if (keys.length == 0) { 
1321 47276ec2 Kostas Papadimitriou
                this.$(".ssh .empty").show();
1322 47276ec2 Kostas Papadimitriou
            } else {
1323 47276ec2 Kostas Papadimitriou
                this.$(".ssh .empty").hide();
1324 47276ec2 Kostas Papadimitriou
            }
1325 47276ec2 Kostas Papadimitriou
            _.each(keys, _.bind(function(key){
1326 807a0305 Kostas Papadimitriou
                var name = _.escape(util.truncate(key.get("name"), 45));
1327 807a0305 Kostas Papadimitriou
                var el = $('<li id="ssh-key-option-{1}" class="ssh-key-option">{0}</li>'.format(name, key.id));
1328 47276ec2 Kostas Papadimitriou
                var check = $('<input class="check" type="checkbox"></input>')
1329 47276ec2 Kostas Papadimitriou
                el.append(check);
1330 47276ec2 Kostas Papadimitriou
                el.data("model", key);
1331 47276ec2 Kostas Papadimitriou
                this.ssh_list.append(el);
1332 47276ec2 Kostas Papadimitriou
            }, this));
1333 73e25ce2 Kostas Papadimitriou
        },
1334 73e25ce2 Kostas Papadimitriou
1335 8d08f18a Kostas Papadimitriou
        init_handlers: function() {
1336 820ef2f0 Kostas Papadimitriou
            this.name.bind("keypress", _.bind(function(e) {
1337 8d08f18a Kostas Papadimitriou
                this.name_changed = true;
1338 c566f369 Kostas Papadimitriou
                if (e.keyCode == 13) { this.parent.set_step(5); this.parent.update_layout() };    
1339 8d08f18a Kostas Papadimitriou
            }, this));
1340 8d08f18a Kostas Papadimitriou
1341 8d08f18a Kostas Papadimitriou
            this.name.bind("click", _.bind(function() {
1342 8d08f18a Kostas Papadimitriou
                if (!this.name_changed) {
1343 8d08f18a Kostas Papadimitriou
                    this.name.val("");
1344 8d08f18a Kostas Papadimitriou
                }
1345 8d08f18a Kostas Papadimitriou
            }, this))
1346 8d08f18a Kostas Papadimitriou
        },
1347 8d08f18a Kostas Papadimitriou
1348 8d08f18a Kostas Papadimitriou
        show: function() {
1349 47276ec2 Kostas Papadimitriou
            views.CreatePersonalizeView.__super__.show.apply(this, arguments);
1350 8d08f18a Kostas Papadimitriou
            this.update_layout();
1351 8d08f18a Kostas Papadimitriou
        },
1352 820ef2f0 Kostas Papadimitriou
        
1353 8d08f18a Kostas Papadimitriou
        update_layout: function() {
1354 8d08f18a Kostas Papadimitriou
            var params = this.parent.get_params();
1355 47276ec2 Kostas Papadimitriou
1356 cd4d5c5f Kostas Papadimitriou
            if (!params.image || !params.flavor) { return }
1357 820ef2f0 Kostas Papadimitriou
1358 820ef2f0 Kostas Papadimitriou
            if (!params.image) { return }
1359 c130b56e Kostas Papadimitriou
            var vm_name_tpl = snf.config.vm_name_template || "My {0} server";
1360 93c25f8d Kostas Papadimitriou
            //if (params.image.is_snapshot()) { vm_name_tpl = "{0}" };
1361 8626b696 Kostas Papadimitriou
            var vm_name = vm_name_tpl.format(params.image.get("name"));
1362 8d08f18a Kostas Papadimitriou
            var orig_name = vm_name;
1363 8d08f18a Kostas Papadimitriou
            
1364 8d08f18a Kostas Papadimitriou
            var existing = true;
1365 8d08f18a Kostas Papadimitriou
            var j = 0;
1366 b128dc09 Kostas Papadimitriou
1367 820ef2f0 Kostas Papadimitriou
            while (existing && !this.name_changed) {
1368 c566f369 Kostas Papadimitriou
                var existing = storage.vms.select(function(vm){
1369 c566f369 Kostas Papadimitriou
                  return vm.get("name") == vm_name
1370 c566f369 Kostas Papadimitriou
                }).length;
1371 8d08f18a Kostas Papadimitriou
                if (existing) {
1372 8d08f18a Kostas Papadimitriou
                    j++;
1373 8d08f18a Kostas Papadimitriou
                    vm_name = orig_name + " " + j;
1374 8d08f18a Kostas Papadimitriou
                }
1375 8d08f18a Kostas Papadimitriou
            }
1376 47276ec2 Kostas Papadimitriou
1377 8d08f18a Kostas Papadimitriou
            if (!_(this.name.val()).trim() || !this.name_changed) {
1378 8d08f18a Kostas Papadimitriou
                this.name.val(vm_name);
1379 8d08f18a Kostas Papadimitriou
            }
1380 8d08f18a Kostas Papadimitriou
1381 4f1968db Kostas Papadimitriou
            if (!this.name_changed && this.parent.visible()) {
1382 1325fc64 Kostas Papadimitriou
                if (!$.browser.msie && !$.browser.opera) {
1383 4f1968db Kostas Papadimitriou
                    this.$("#create-vm-name").select();
1384 4f1968db Kostas Papadimitriou
                } else {
1385 4f1968db Kostas Papadimitriou
                    window.setTimeout(_.bind(function(){
1386 4f1968db Kostas Papadimitriou
                        this.$("#create-vm-name").select();
1387 1325fc64 Kostas Papadimitriou
                    }, this), 400)
1388 4f1968db Kostas Papadimitriou
                }
1389 820ef2f0 Kostas Papadimitriou
            }
1390 820ef2f0 Kostas Papadimitriou
            
1391 820ef2f0 Kostas Papadimitriou
            var img = snf.ui.helpers.os_icon_path(params.image.get("OS"))
1392 820ef2f0 Kostas Papadimitriou
            this.name.css({backgroundImage:"url({0})".format(img)})
1393 47276ec2 Kostas Papadimitriou
            
1394 47276ec2 Kostas Papadimitriou
            if (!params.image.supports('ssh')) {
1395 47276ec2 Kostas Papadimitriou
                this.disable_ssh_keys();
1396 47276ec2 Kostas Papadimitriou
            } else {
1397 47276ec2 Kostas Papadimitriou
                this.enable_ssh_keys();
1398 47276ec2 Kostas Papadimitriou
                this.update_ssh_keys();
1399 47276ec2 Kostas Papadimitriou
            }
1400 820ef2f0 Kostas Papadimitriou
1401 47276ec2 Kostas Papadimitriou
            this.update_ui_keys_selections(this.selected_keys);
1402 47276ec2 Kostas Papadimitriou
        },
1403 47276ec2 Kostas Papadimitriou
1404 47276ec2 Kostas Papadimitriou
        disable_ssh_keys: function() {
1405 47276ec2 Kostas Papadimitriou
            this.$(".disabled.desc").show();
1406 47276ec2 Kostas Papadimitriou
            this.$(".empty.desc").hide();
1407 47276ec2 Kostas Papadimitriou
            this.$(".ssh .confirm-params").hide();
1408 47276ec2 Kostas Papadimitriou
            this.selected_keys = [];
1409 47276ec2 Kostas Papadimitriou
        },
1410 47276ec2 Kostas Papadimitriou
1411 47276ec2 Kostas Papadimitriou
        enable_ssh_keys: function() {
1412 47276ec2 Kostas Papadimitriou
            this.$(".ssh .confirm-params").show();
1413 47276ec2 Kostas Papadimitriou
            this.$(".disabled.desc").hide();
1414 8d08f18a Kostas Papadimitriou
        },
1415 8d08f18a Kostas Papadimitriou
1416 8d08f18a Kostas Papadimitriou
        reset: function() {
1417 73e25ce2 Kostas Papadimitriou
            this.roles.find(".val").removeClass("selected");
1418 820ef2f0 Kostas Papadimitriou
            this.name_changed = false;
1419 47276ec2 Kostas Papadimitriou
            this.selected_keys = [];
1420 8d08f18a Kostas Papadimitriou
            this.update_layout();
1421 8d08f18a Kostas Papadimitriou
        },
1422 8d08f18a Kostas Papadimitriou
1423 73e25ce2 Kostas Papadimitriou
        get_meta: function() {
1424 73e25ce2 Kostas Papadimitriou
            if (this.roles.find(".selected").length == 0) {
1425 73e25ce2 Kostas Papadimitriou
                return false;
1426 73e25ce2 Kostas Papadimitriou
            }
1427 73e25ce2 Kostas Papadimitriou
1428 73e25ce2 Kostas Papadimitriou
            var role = $(this.roles.find(".selected").get(0)).data("value");
1429 73e25ce2 Kostas Papadimitriou
            return {'Role': role }
1430 73e25ce2 Kostas Papadimitriou
        },
1431 73e25ce2 Kostas Papadimitriou
1432 8d08f18a Kostas Papadimitriou
        get: function() {
1433 73e25ce2 Kostas Papadimitriou
            var val = {'name': this.name.val() };
1434 73e25ce2 Kostas Papadimitriou
            if (this.get_meta()) {
1435 73e25ce2 Kostas Papadimitriou
                val.metadata = this.get_meta();
1436 73e25ce2 Kostas Papadimitriou
            }
1437 47276ec2 Kostas Papadimitriou
1438 47276ec2 Kostas Papadimitriou
            val.keys = _.map(this.selected_keys, function(k){ return snf.storage.keys.get(k)});
1439 73e25ce2 Kostas Papadimitriou
            
1440 73e25ce2 Kostas Papadimitriou
            return val;
1441 8d08f18a Kostas Papadimitriou
        }
1442 8d08f18a Kostas Papadimitriou
    });
1443 8d08f18a Kostas Papadimitriou
1444 47276ec2 Kostas Papadimitriou
    views.CreateSubmitView = views.CreateVMStepView.extend({
1445 c566f369 Kostas Papadimitriou
        step: 5,
1446 47276ec2 Kostas Papadimitriou
        initialize: function() {
1447 47276ec2 Kostas Papadimitriou
            views.CreateSubmitView.__super__.initialize.apply(this, arguments);
1448 47276ec2 Kostas Papadimitriou
            this.roles = this.$("li.predefined-meta.role .values");
1449 47276ec2 Kostas Papadimitriou
            this.confirm = this.$(".confirm-params ul");
1450 47276ec2 Kostas Papadimitriou
            this.name = this.$("h3.vm-name");
1451 47276ec2 Kostas Papadimitriou
            this.keys = this.$(".confirm-params.ssh");
1452 47276ec2 Kostas Papadimitriou
            this.meta = this.$(".confirm-params.meta");
1453 c566f369 Kostas Papadimitriou
            this.ip_addresses = this.$(".confirm-params.ip-addresses");
1454 c566f369 Kostas Papadimitriou
            this.private_networks = this.$(".confirm-params.private-networks");
1455 47276ec2 Kostas Papadimitriou
            this.init_handlers();
1456 47276ec2 Kostas Papadimitriou
        },
1457 47276ec2 Kostas Papadimitriou
1458 47276ec2 Kostas Papadimitriou
        init_handlers: function() {
1459 47276ec2 Kostas Papadimitriou
        },
1460 47276ec2 Kostas Papadimitriou
1461 47276ec2 Kostas Papadimitriou
        show: function() {
1462 47276ec2 Kostas Papadimitriou
            views.CreateSubmitView.__super__.show.apply(this, arguments);
1463 47276ec2 Kostas Papadimitriou
            this.update_layout();
1464 47276ec2 Kostas Papadimitriou
        },
1465 47276ec2 Kostas Papadimitriou
        
1466 c566f369 Kostas Papadimitriou
        update_network_details: function() {
1467 c566f369 Kostas Papadimitriou
            var data = this.parent.get_params();
1468 c566f369 Kostas Papadimitriou
            var ips = data.addresses;
1469 c566f369 Kostas Papadimitriou
            var networks = data.networks;
1470 c566f369 Kostas Papadimitriou
1471 c566f369 Kostas Papadimitriou
            this.ip_addresses.empty();
1472 c566f369 Kostas Papadimitriou
            if (!ips|| ips.length == 0) {
1473 c566f369 Kostas Papadimitriou
                this.ip_addresses.append(this.make("li", {'class':'empty'}, 
1474 c566f369 Kostas Papadimitriou
                                           'No ip addresses selected'))
1475 c566f369 Kostas Papadimitriou
            }
1476 c566f369 Kostas Papadimitriou
            _.each(ips, _.bind(function(ip) {
1477 c566f369 Kostas Papadimitriou
                var el = this.make("li", {'class':'selected-ip-address'}, 
1478 c68ad72e Kostas Papadimitriou
                                  ip.get('floating_ip_address'));
1479 c566f369 Kostas Papadimitriou
                this.ip_addresses.append(el);
1480 c566f369 Kostas Papadimitriou
            }, this))
1481 c566f369 Kostas Papadimitriou
1482 c566f369 Kostas Papadimitriou
            this.private_networks.empty();
1483 c566f369 Kostas Papadimitriou
            if (!networks || networks.length == 0) {
1484 c566f369 Kostas Papadimitriou
                this.private_networks.append(this.make("li", {'class':'empty'}, 
1485 c566f369 Kostas Papadimitriou
                                             'No private networks selected'))
1486 c566f369 Kostas Papadimitriou
            }
1487 c566f369 Kostas Papadimitriou
            _.each(networks, _.bind(function(network) {
1488 c566f369 Kostas Papadimitriou
                var el = this.make("li", {'class':'selected-private-network'}, 
1489 c566f369 Kostas Papadimitriou
                                  network.get('name'));
1490 c566f369 Kostas Papadimitriou
                this.private_networks.append(el);
1491 c566f369 Kostas Papadimitriou
            }, this))
1492 c566f369 Kostas Papadimitriou
1493 c566f369 Kostas Papadimitriou
        },
1494 c566f369 Kostas Papadimitriou
1495 47276ec2 Kostas Papadimitriou
        update_flavor_details: function() {
1496 47276ec2 Kostas Papadimitriou
            var flavor = this.parent.get_params().flavor;
1497 47276ec2 Kostas Papadimitriou
1498 47276ec2 Kostas Papadimitriou
            function set_detail(sel, key) {
1499 47276ec2 Kostas Papadimitriou
                var val = key;
1500 47276ec2 Kostas Papadimitriou
                if (key == undefined) { val = flavor.get(sel) };
1501 47276ec2 Kostas Papadimitriou
                this.$(".confirm-cont.flavor .flavor-" + sel + " .value").text(val)
1502 47276ec2 Kostas Papadimitriou
            }
1503 47276ec2 Kostas Papadimitriou
            
1504 47276ec2 Kostas Papadimitriou
            set_detail("cpu", flavor.get("cpu") + "x");
1505 47276ec2 Kostas Papadimitriou
            set_detail("ram", flavor.get("ram") + " MB");
1506 47276ec2 Kostas Papadimitriou
            set_detail("disk", util.readablizeBytes(flavor.get("disk") * 1024 * 1024 * 1024));
1507 d965e6dc Kostas Papadimitriou
            set_detail("disktype", flavor.get_disk_template_info().name);
1508 47276ec2 Kostas Papadimitriou
        },
1509 47276ec2 Kostas Papadimitriou
1510 47276ec2 Kostas Papadimitriou
        update_image_details: function() {
1511 47276ec2 Kostas Papadimitriou
            var image = this.parent.get_params().image;
1512 47276ec2 Kostas Papadimitriou
1513 47276ec2 Kostas Papadimitriou
            function set_detail(sel, key) {
1514 47276ec2 Kostas Papadimitriou
                var val = key;
1515 47276ec2 Kostas Papadimitriou
                if (key == undefined) { val = image.get(sel) };
1516 47276ec2 Kostas Papadimitriou
                this.$(".confirm-cont.image .image-" + sel + " .value").text(val)
1517 47276ec2 Kostas Papadimitriou
            }
1518 47276ec2 Kostas Papadimitriou
            
1519 1d356257 Kostas Papadimitriou
            set_detail("description", image.get_description());
1520 f21051c8 Kostas Papadimitriou
            set_detail("name", util.truncate(image.get("name"), 30));
1521 43ec1206 Kostas Papadimitriou
            set_detail("os", _(image.get_os()).capitalize());
1522 43ec1206 Kostas Papadimitriou
            set_detail("gui", image.get_gui());
1523 df251d55 Kostas Papadimitriou
            set_detail("size", _.escape(image.get_readable_size()));
1524 47276ec2 Kostas Papadimitriou
            set_detail("kernel");
1525 47276ec2 Kostas Papadimitriou
        },
1526 47276ec2 Kostas Papadimitriou
1527 47276ec2 Kostas Papadimitriou
        update_selected_keys: function(keys) {
1528 47276ec2 Kostas Papadimitriou
            this.keys.empty();
1529 47276ec2 Kostas Papadimitriou
            if (!keys || keys.length == 0) {
1530 47276ec2 Kostas Papadimitriou
                this.keys.append(this.make("li", {'class':'empty'}, 'No keys selected'))
1531 47276ec2 Kostas Papadimitriou
            }
1532 47276ec2 Kostas Papadimitriou
            _.each(keys, _.bind(function(key) {
1533 807a0305 Kostas Papadimitriou
                var name = _.escape(util.truncate(key.get("name"), 20))
1534 807a0305 Kostas Papadimitriou
                var el = this.make("li", {'class':'selected-ssh-key'}, name);
1535 47276ec2 Kostas Papadimitriou
                this.keys.append(el);
1536 47276ec2 Kostas Papadimitriou
            }, this))
1537 47276ec2 Kostas Papadimitriou
        },
1538 47276ec2 Kostas Papadimitriou
1539 47276ec2 Kostas Papadimitriou
        update_selected_meta: function(meta) {
1540 47276ec2 Kostas Papadimitriou
            this.meta.empty();
1541 47276ec2 Kostas Papadimitriou
            if (!meta || meta.length == 0) {
1542 47276ec2 Kostas Papadimitriou
                this.meta.append(this.make("li", {'class':'empty'}, 'No tags selected'))
1543 47276ec2 Kostas Papadimitriou
            }
1544 47276ec2 Kostas Papadimitriou
            _.each(meta, _.bind(function(value, key) {
1545 c566f369 Kostas Papadimitriou
                var el = this.make("li", {'class':'confirm-value'});
1546 c566f369 Kostas Papadimitriou
                var name = this.make("span", {'class':'ckey'}, key);
1547 c566f369 Kostas Papadimitriou
                var value = this.make("span", {'class':'cval'}, value);
1548 47276ec2 Kostas Papadimitriou
1549 47276ec2 Kostas Papadimitriou
                $(el).append(name)
1550 47276ec2 Kostas Papadimitriou
                $(el).append(value);
1551 47276ec2 Kostas Papadimitriou
                this.meta.append(el);
1552 47276ec2 Kostas Papadimitriou
            }, this));
1553 47276ec2 Kostas Papadimitriou
        },
1554 47276ec2 Kostas Papadimitriou
1555 47276ec2 Kostas Papadimitriou
        update_layout: function() {
1556 47276ec2 Kostas Papadimitriou
            var params = this.parent.get_params();
1557 47276ec2 Kostas Papadimitriou
            if (!params.image || !params.flavor) { return }
1558 47276ec2 Kostas Papadimitriou
1559 47276ec2 Kostas Papadimitriou
            if (!params.image) { return }
1560 47276ec2 Kostas Papadimitriou
1561 f21051c8 Kostas Papadimitriou
            this.name.text(util.truncate(params.name, 50));
1562 47276ec2 Kostas Papadimitriou
1563 47276ec2 Kostas Papadimitriou
            this.confirm.find("li.image .value").text(params.flavor.get("image"));
1564 47276ec2 Kostas Papadimitriou
            this.confirm.find("li.cpu .value").text(params.flavor.get("cpu") + "x");
1565 47276ec2 Kostas Papadimitriou
            this.confirm.find("li.mem .value").text(params.flavor.get("ram"));
1566 47276ec2 Kostas Papadimitriou
            this.confirm.find("li.disk .value").text(params.flavor.get("disk"));
1567 47276ec2 Kostas Papadimitriou
1568 47276ec2 Kostas Papadimitriou
            var img = snf.ui.helpers.os_icon_path(params.image.get("OS"))
1569 47276ec2 Kostas Papadimitriou
            this.name.css({backgroundImage:"url({0})".format(img)})
1570 47276ec2 Kostas Papadimitriou
1571 47276ec2 Kostas Papadimitriou
            this.update_image_details();
1572 47276ec2 Kostas Papadimitriou
            this.update_flavor_details();
1573 c566f369 Kostas Papadimitriou
            this.update_network_details();
1574 47276ec2 Kostas Papadimitriou
1575 47276ec2 Kostas Papadimitriou
            if (!params.image.supports('ssh')) {
1576 47276ec2 Kostas Papadimitriou
                this.keys.hide();
1577 47276ec2 Kostas Papadimitriou
                this.keys.prev().hide();
1578 47276ec2 Kostas Papadimitriou
            } else {
1579 47276ec2 Kostas Papadimitriou
                this.keys.show();
1580 47276ec2 Kostas Papadimitriou
                this.keys.prev().show();
1581 47276ec2 Kostas Papadimitriou
                this.update_selected_keys(params.keys);
1582 47276ec2 Kostas Papadimitriou
            }
1583 47276ec2 Kostas Papadimitriou
            
1584 47276ec2 Kostas Papadimitriou
            this.update_selected_meta(params.metadata);
1585 47276ec2 Kostas Papadimitriou
        },
1586 47276ec2 Kostas Papadimitriou
1587 47276ec2 Kostas Papadimitriou
        reset: function() {
1588 47276ec2 Kostas Papadimitriou
            this.update_layout();
1589 47276ec2 Kostas Papadimitriou
        },
1590 47276ec2 Kostas Papadimitriou
1591 47276ec2 Kostas Papadimitriou
        get_meta: function() {
1592 47276ec2 Kostas Papadimitriou
        },
1593 47276ec2 Kostas Papadimitriou
1594 47276ec2 Kostas Papadimitriou
        get: function() {
1595 47276ec2 Kostas Papadimitriou
            return {};
1596 47276ec2 Kostas Papadimitriou
        }
1597 47276ec2 Kostas Papadimitriou
    });
1598 47276ec2 Kostas Papadimitriou
1599 8d08f18a Kostas Papadimitriou
    views.CreateVMView = views.Overlay.extend({
1600 8d08f18a Kostas Papadimitriou
        
1601 8d08f18a Kostas Papadimitriou
        view_id: "create_vm_view",
1602 8d08f18a Kostas Papadimitriou
        content_selector: "#createvm-overlay-content",
1603 8d08f18a Kostas Papadimitriou
        css_class: 'overlay-createvm overlay-info',
1604 8d08f18a Kostas Papadimitriou
        overlay_id: "metadata-overlay",
1605 8d08f18a Kostas Papadimitriou
1606 8d08f18a Kostas Papadimitriou
        subtitle: false,
1607 8d08f18a Kostas Papadimitriou
        title: "Create new machine",
1608 8d08f18a Kostas Papadimitriou
1609 8d08f18a Kostas Papadimitriou
        initialize: function(options) {
1610 8d08f18a Kostas Papadimitriou
            views.CreateVMView.__super__.initialize.apply(this);
1611 8d08f18a Kostas Papadimitriou
            this.current_step = 1;
1612 8d08f18a Kostas Papadimitriou
1613 8d08f18a Kostas Papadimitriou
            this.password_view = new views.VMCreationPasswordView();
1614 8d08f18a Kostas Papadimitriou
1615 8d08f18a Kostas Papadimitriou
            this.steps = [];
1616 8d08f18a Kostas Papadimitriou
            this.steps[1] = new views.CreateImageSelectView(this);
1617 8d08f18a Kostas Papadimitriou
            this.steps[1].bind("change", _.bind(function(data) {this.trigger("image:change", data)}, this));
1618 8d08f18a Kostas Papadimitriou
1619 8d08f18a Kostas Papadimitriou
            this.steps[2] = new views.CreateFlavorSelectView(this);
1620 c566f369 Kostas Papadimitriou
            this.steps[3] = new views.CreateNetworkingView(this);
1621 c566f369 Kostas Papadimitriou
            this.steps[4] = new views.CreatePersonalizeView(this);
1622 c566f369 Kostas Papadimitriou
            this.steps[5] = new views.CreateSubmitView(this);
1623 8d08f18a Kostas Papadimitriou
1624 1325fc64 Kostas Papadimitriou
            this.cancel_btn = this.$(".create-controls .cancel");
1625 1325fc64 Kostas Papadimitriou
            this.next_btn = this.$(".create-controls .next");
1626 1325fc64 Kostas Papadimitriou
            this.prev_btn = this.$(".create-controls .prev");
1627 1325fc64 Kostas Papadimitriou
            this.submit_btn = this.$(".create-controls .submit");
1628 1325fc64 Kostas Papadimitriou
1629 1325fc64 Kostas Papadimitriou
            this.history = this.$(".steps-history");
1630 1325fc64 Kostas Papadimitriou
            this.history_steps = this.$(".steps-history .steps-history-step");
1631 8d08f18a Kostas Papadimitriou
            
1632 8d08f18a Kostas Papadimitriou
            this.init_handlers();
1633 8d08f18a Kostas Papadimitriou
        },
1634 8d08f18a Kostas Papadimitriou
1635 8d08f18a Kostas Papadimitriou
        init_handlers: function() {
1636 1325fc64 Kostas Papadimitriou
            var self = this;
1637 8d08f18a Kostas Papadimitriou
            this.next_btn.click(_.bind(function(){
1638 b128dc09 Kostas Papadimitriou
                this.set_step(this.current_step + 1);
1639 8d08f18a Kostas Papadimitriou
                this.update_layout();
1640 8d08f18a Kostas Papadimitriou
            }, this))
1641 8d08f18a Kostas Papadimitriou
            this.prev_btn.click(_.bind(function(){
1642 b128dc09 Kostas Papadimitriou
                this.set_step(this.current_step - 1);
1643 8d08f18a Kostas Papadimitriou
                this.update_layout();
1644 8d08f18a Kostas Papadimitriou
            }, this))
1645 8d08f18a Kostas Papadimitriou
            this.cancel_btn.click(_.bind(function(){
1646 8d08f18a Kostas Papadimitriou
                this.close_all();
1647 8d08f18a Kostas Papadimitriou
            }, this))
1648 8d08f18a Kostas Papadimitriou
            this.submit_btn.click(_.bind(function(){
1649 8d08f18a Kostas Papadimitriou
                this.submit();
1650 8d08f18a Kostas Papadimitriou
            }, this))
1651 1325fc64 Kostas Papadimitriou
            
1652 1325fc64 Kostas Papadimitriou
            this.history.find(".completed").live("click", function() {
1653 1325fc64 Kostas Papadimitriou
                var step = parseInt($(this).attr("id").replace("vm-create-step-history-", ""));
1654 1325fc64 Kostas Papadimitriou
                self.set_step(step);
1655 1325fc64 Kostas Papadimitriou
                self.update_layout();
1656 1325fc64 Kostas Papadimitriou
            })
1657 8d08f18a Kostas Papadimitriou
        },
1658 8d08f18a Kostas Papadimitriou
1659 8d08f18a Kostas Papadimitriou
        set_step: function(st) {
1660 8d08f18a Kostas Papadimitriou
        },
1661 8d08f18a Kostas Papadimitriou
        
1662 8d08f18a Kostas Papadimitriou
        validate: function(data) {
1663 8d08f18a Kostas Papadimitriou
            if (_(data.name).trim() == "") {
1664 8d08f18a Kostas Papadimitriou
                this.$(".form-field").addClass("error");
1665 8d08f18a Kostas Papadimitriou
                return false;
1666 8d08f18a Kostas Papadimitriou
            } else {
1667 8d08f18a Kostas Papadimitriou
                return true;
1668 8d08f18a Kostas Papadimitriou
            }
1669 8d08f18a Kostas Papadimitriou
        },
1670 8d08f18a Kostas Papadimitriou
1671 8d08f18a Kostas Papadimitriou
        submit: function() {
1672 820ef2f0 Kostas Papadimitriou
            if (this.submiting) { return };
1673 8d08f18a Kostas Papadimitriou
            var data = this.get_params();
1674 73e25ce2 Kostas Papadimitriou
            var meta = {};
1675 47276ec2 Kostas Papadimitriou
            var extra = {};
1676 47276ec2 Kostas Papadimitriou
            var personality = [];
1677 47276ec2 Kostas Papadimitriou
1678 8d08f18a Kostas Papadimitriou
            if (this.validate(data)) {
1679 8d08f18a Kostas Papadimitriou
                this.submit_btn.addClass("in-progress");
1680 820ef2f0 Kostas Papadimitriou
                this.submiting = true;
1681 73e25ce2 Kostas Papadimitriou
                if (data.metadata) { meta = data.metadata; }
1682 47276ec2 Kostas Papadimitriou
                if (data.keys && data.keys.length > 0) {
1683 c566f369 Kostas Papadimitriou
                    personality.push(
1684 c566f369 Kostas Papadimitriou
                      data.image.personality_data_for_keys(data.keys))
1685 47276ec2 Kostas Papadimitriou
                }
1686 47276ec2 Kostas Papadimitriou
1687 2221a5a0 Kostas Papadimitriou
                if (personality.length) {
1688 bdfb089f Kostas Papadimitriou
                    extra['personality'] = _.flatten(personality);
1689 2221a5a0 Kostas Papadimitriou
                }
1690 c566f369 Kostas Papadimitriou
                
1691 c68ad72e Kostas Papadimitriou
                extra['networks'] = [];
1692 c68ad72e Kostas Papadimitriou
                _.each(data.networks, function(n) {
1693 c68ad72e Kostas Papadimitriou
                  extra.networks.push({'uuid': n.get('id')})
1694 c68ad72e Kostas Papadimitriou
                });
1695 9c8f31ad Kostas Papadimitriou
                _.each(data.addresses, function(ip) {
1696 c68ad72e Kostas Papadimitriou
                  extra.networks.push({
1697 c68ad72e Kostas Papadimitriou
                    'uuid': ip.get('network').get('id'),
1698 c68ad72e Kostas Papadimitriou
                    'fixed_ip': ip.get('floating_ip_address')
1699 c68ad72e Kostas Papadimitriou
                  });
1700 c68ad72e Kostas Papadimitriou
                });
1701 c68ad72e Kostas Papadimitriou
1702 c68ad72e Kostas Papadimitriou
                _.map(data.networks, function(n) { return n.get('id') });
1703 c566f369 Kostas Papadimitriou
                storage.vms.create(data.name, data.image, data.flavor, 
1704 c566f369 Kostas Papadimitriou
                                   meta, extra, _.bind(function(data){
1705 1610b2da Kostas Papadimitriou
                    _.each(data.addresses, function(ip) {
1706 1610b2da Kostas Papadimitriou
                      ip.set({'status': 'connecting'});
1707 1610b2da Kostas Papadimitriou
                    });
1708 8d08f18a Kostas Papadimitriou
                    this.close_all();
1709 c566f369 Kostas Papadimitriou
                    this.password_view.show(data.server.adminPass, 
1710 c566f369 Kostas Papadimitriou
                                            data.server.id);
1711 c451f1c4 Kostas Papadimitriou
                    var self = this;
1712 c451f1c4 Kostas Papadimitriou
                    window.setTimeout(function() {
1713 c451f1c4 Kostas Papadimitriou
                      self.submiting = false;
1714 c451f1c4 Kostas Papadimitriou
                    }, 1000);
1715 8d08f18a Kostas Papadimitriou
                }, this));
1716 8d08f18a Kostas Papadimitriou
            }
1717 8d08f18a Kostas Papadimitriou
        },
1718 8d08f18a Kostas Papadimitriou
1719 8d08f18a Kostas Papadimitriou
        close_all: function() {
1720 c566f369 Kostas Papadimitriou
          this.hide();
1721 c566f369 Kostas Papadimitriou
        },
1722 c566f369 Kostas Papadimitriou
1723 c566f369 Kostas Papadimitriou
        onClose: function() {
1724 c566f369 Kostas Papadimitriou
          this.steps[3].remove();
1725 8d08f18a Kostas Papadimitriou
        },
1726 8d08f18a Kostas Papadimitriou
1727 8d08f18a Kostas Papadimitriou
        reset: function() {
1728 c566f369 Kostas Papadimitriou
          this.current_step = 1;
1729 b128dc09 Kostas Papadimitriou
1730 c566f369 Kostas Papadimitriou
          this.steps[1].reset();
1731 c566f369 Kostas Papadimitriou
          this.steps[2].reset();
1732 c566f369 Kostas Papadimitriou
          this.steps[3].reset();
1733 c566f369 Kostas Papadimitriou
          this.steps[4].reset();
1734 8d08f18a Kostas Papadimitriou
1735 c566f369 Kostas Papadimitriou
          //this.steps[1].show();
1736 c566f369 Kostas Papadimitriou
          //this.steps[2].show();
1737 c566f369 Kostas Papadimitriou
          //this.steps[3].show();
1738 c566f369 Kostas Papadimitriou
          //this.steps[4].show();
1739 b128dc09 Kostas Papadimitriou
1740 c566f369 Kostas Papadimitriou
          this.submit_btn.removeClass("in-progress");
1741 8d08f18a Kostas Papadimitriou
        },
1742 8d08f18a Kostas Papadimitriou
1743 8d08f18a Kostas Papadimitriou
        onShow: function() {
1744 8d08f18a Kostas Papadimitriou
        },
1745 8d08f18a Kostas Papadimitriou
1746 8d08f18a Kostas Papadimitriou
        update_layout: function() {
1747 8d08f18a Kostas Papadimitriou
            this.show_step(this.current_step);
1748 8d08f18a Kostas Papadimitriou
            this.current_view.update_layout();
1749 8d08f18a Kostas Papadimitriou
        },
1750 8d08f18a Kostas Papadimitriou
1751 8d08f18a Kostas Papadimitriou
        beforeOpen: function() {
1752 47276ec2 Kostas Papadimitriou
            if (!this.skip_reset_on_next_open) {
1753 47276ec2 Kostas Papadimitriou
                this.submiting = false;
1754 47276ec2 Kostas Papadimitriou
                this.reset();
1755 47276ec2 Kostas Papadimitriou
                this.current_step = 1;
1756 47276ec2 Kostas Papadimitriou
                this.$(".steps-container").css({"margin-left":0 + "px"});
1757 47276ec2 Kostas Papadimitriou
                this.show_step(1);
1758 47276ec2 Kostas Papadimitriou
            }
1759 47276ec2 Kostas Papadimitriou
            
1760 47276ec2 Kostas Papadimitriou
            this.skip_reset_on_next_open = false;
1761 47276ec2 Kostas Papadimitriou
            this.update_layout();
1762 8d08f18a Kostas Papadimitriou
        },
1763 b128dc09 Kostas Papadimitriou
        
1764 b128dc09 Kostas Papadimitriou
        set_step: function(step) {
1765 8d08f18a Kostas Papadimitriou
            if (step <= 1) {
1766 8d08f18a Kostas Papadimitriou
                step = 1
1767 8d08f18a Kostas Papadimitriou
            }
1768 8d08f18a Kostas Papadimitriou
            if (step > this.steps.length - 1) {
1769 8d08f18a Kostas Papadimitriou
                step = this.steps.length - 1;
1770 8d08f18a Kostas Papadimitriou
            }
1771 bd8fa10c Kostas Papadimitriou
            this.current_step = step;
1772 b128dc09 Kostas Papadimitriou
        },
1773 b128dc09 Kostas Papadimitriou
1774 b128dc09 Kostas Papadimitriou
        show_step: function(step) {
1775 4cff0386 Kostas Papadimitriou
            // FIXME: this shouldn't be here
1776 4cff0386 Kostas Papadimitriou
            // but since we are not calling step.hide this should work
1777 4cff0386 Kostas Papadimitriou
            this.steps[1].image_details.hide();
1778 c68ad72e Kostas Papadimitriou
            
1779 c68ad72e Kostas Papadimitriou
            this.current_view && this.current_view.hide_step && this.current_view.hide_step();
1780 bd8fa10c Kostas Papadimitriou
            this.current_view = this.steps[step];
1781 bd8fa10c Kostas Papadimitriou
            this.update_controls();
1782 bd8fa10c Kostas Papadimitriou
1783 b128dc09 Kostas Papadimitriou
            this.steps[step].show();
1784 820ef2f0 Kostas Papadimitriou
            var width = this.el.find('.container').width();
1785 820ef2f0 Kostas Papadimitriou
            var left = (step -1) * width * -1;
1786 1325fc64 Kostas Papadimitriou
            this.$(".steps-container").animate({"margin-left": left + "px"}, 300);
1787 1325fc64 Kostas Papadimitriou
1788 1325fc64 Kostas Papadimitriou
            this.update_steps_history();
1789 1325fc64 Kostas Papadimitriou
        },
1790 1325fc64 Kostas Papadimitriou
1791 1325fc64 Kostas Papadimitriou
        update_steps_history: function() {
1792 1325fc64 Kostas Papadimitriou
            var self = this;
1793 1325fc64 Kostas Papadimitriou
            function get_step(s) {
1794 1325fc64 Kostas Papadimitriou
                return self.history.find(".step" + s + "h");
1795 1325fc64 Kostas Papadimitriou
            }
1796 1325fc64 Kostas Papadimitriou
            
1797 1325fc64 Kostas Papadimitriou
            var current_step = parseInt(this.current_view.step);
1798 1325fc64 Kostas Papadimitriou
            _.each(this.steps, function(stepv) {
1799 1325fc64 Kostas Papadimitriou
                var step = parseInt(stepv.step);
1800 1325fc64 Kostas Papadimitriou
                get_step(step).removeClass("completed").removeClass("current");
1801 1325fc64 Kostas Papadimitriou
                if (step == current_step) {
1802 1325fc64 Kostas Papadimitriou
                    get_step(step).removeClass("completed").addClass("current");
1803 1325fc64 Kostas Papadimitriou
                }
1804 1325fc64 Kostas Papadimitriou
                if (step < current_step) {
1805 1325fc64 Kostas Papadimitriou
                    get_step(step).removeClass("current").addClass("completed");
1806 1325fc64 Kostas Papadimitriou
                }
1807 1325fc64 Kostas Papadimitriou
            });
1808 8d08f18a Kostas Papadimitriou
        },
1809 8d08f18a Kostas Papadimitriou
1810 8d08f18a Kostas Papadimitriou
        update_controls: function() {
1811 8d08f18a Kostas Papadimitriou
            var step = this.current_step;
1812 8d08f18a Kostas Papadimitriou
            if (step == 1) {
1813 8d08f18a Kostas Papadimitriou
                this.prev_btn.hide();
1814 8d08f18a Kostas Papadimitriou
                this.cancel_btn.show();
1815 8d08f18a Kostas Papadimitriou
            } else {
1816 8d08f18a Kostas Papadimitriou
                this.prev_btn.show();
1817 8d08f18a Kostas Papadimitriou
                this.cancel_btn.hide();
1818 8d08f18a Kostas Papadimitriou
            }
1819 8d08f18a Kostas Papadimitriou
            
1820 8d08f18a Kostas Papadimitriou
            if (step == this.steps.length - 1) {
1821 8d08f18a Kostas Papadimitriou
                this.next_btn.hide();
1822 8d08f18a Kostas Papadimitriou
                this.submit_btn.show();
1823 8d08f18a Kostas Papadimitriou
            } else {
1824 8d08f18a Kostas Papadimitriou
                this.next_btn.show();
1825 8d08f18a Kostas Papadimitriou
                this.submit_btn.hide();
1826 8d08f18a Kostas Papadimitriou
            }
1827 8d08f18a Kostas Papadimitriou
        },
1828 8d08f18a Kostas Papadimitriou
1829 8d08f18a Kostas Papadimitriou
        get_params: function() {
1830 c566f369 Kostas Papadimitriou
            return _.extend({}, this.steps[1].get(), this.steps[2].get(), this.steps[3].get(), this.steps[4].get());
1831 8d08f18a Kostas Papadimitriou
        }
1832 8d08f18a Kostas Papadimitriou
    });
1833 8d08f18a Kostas Papadimitriou
    
1834 8d08f18a Kostas Papadimitriou
})(this);