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