root / ui / static / snf / js / ui / web / ui_create_view.js @ cd4d5c5f
History | View | Annotate | Download (32 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var models = snf.models = snf.models || {}
|
9 |
var storage = snf.storage = snf.storage || {};
|
10 |
var ui = snf.ui = snf.ui || {};
|
11 |
var util = snf.util = snf.util || {};
|
12 |
|
13 |
var views = snf.views = snf.views || {}
|
14 |
|
15 |
// shortcuts
|
16 |
var bb = root.Backbone;
|
17 |
|
18 |
|
19 |
views.VMCreationPasswordView = views.Overlay.extend({ |
20 |
view_id: "creation_password_view", |
21 |
content_selector: "#creation-password-overlay", |
22 |
css_class: 'overlay-password overlay-info', |
23 |
overlay_id: "creation-password-overlay", |
24 |
|
25 |
subtitle: "", |
26 |
title: "Machine password", |
27 |
|
28 |
initialize: function(options) { |
29 |
views.FeedbackView.__super__.initialize.apply(this, arguments); |
30 |
_.bindAll(this, 'show_password'); |
31 |
|
32 |
this.password = this.$("#new-machine-password"); |
33 |
this.copy = this.$(".clip-copy"); |
34 |
|
35 |
this.$(".show-machine").click(_.bind(function(){ |
36 |
if (this.$(".show-machine").hasClass("in-progress")) { |
37 |
return;
|
38 |
} |
39 |
this.hide();
|
40 |
snf.ui.main.show_vm_details(storage.vms.get(this.vm_id));
|
41 |
}, this));
|
42 |
|
43 |
_.bindAll(this, "handle_vm_added"); |
44 |
storage.vms.bind("add", this.handle_vm_added); |
45 |
}, |
46 |
|
47 |
handle_vm_added: function() { |
48 |
this.$(".show-machine").removeClass("in-progress"); |
49 |
}, |
50 |
|
51 |
show_password: function() { |
52 |
this.$(".show-machine").addClass("in-progress"); |
53 |
this.password.text(this.pass); |
54 |
}, |
55 |
|
56 |
onClose: function() { |
57 |
this.password.text(""); |
58 |
this.vm_id = undefined; |
59 |
}, |
60 |
|
61 |
beforeOpen: function() { |
62 |
try { delete this.clipboard } catch (err) { console.log(err) }; |
63 |
this.clipboard = new util.ClipHelper(this.copy); |
64 |
}, |
65 |
|
66 |
onOpen: function() { |
67 |
this.copy.show();
|
68 |
try {
|
69 |
this.clipboard.setText(this.pass); |
70 |
this.copy.show();
|
71 |
} catch (err) {
|
72 |
this.copy.hide();
|
73 |
} |
74 |
}, |
75 |
|
76 |
show: function(pass, vm_id) { |
77 |
this.pass = pass;
|
78 |
this.vm_id = vm_id;
|
79 |
|
80 |
views.VMCreationPasswordView.__super__.show.apply(this, arguments); |
81 |
this.show_password();
|
82 |
} |
83 |
}) |
84 |
|
85 |
|
86 |
|
87 |
views.CreateVMStepView = views.View.extend({ |
88 |
step: "1", |
89 |
title: "Image", |
90 |
submit: false, |
91 |
|
92 |
initialize: function(view) { |
93 |
this.parent = view;
|
94 |
this.el = view.$("div.create-step-cont.step-" + this.step); |
95 |
this.header = this.$(".step-header .step-" + this.step); |
96 |
this.view_id = "create_step_" + this.step; |
97 |
|
98 |
views.CreateVMStepView.__super__.initialize.apply(this);
|
99 |
}, |
100 |
|
101 |
show: function() { |
102 |
// show current
|
103 |
this.el.show();
|
104 |
this.header.addClass("current"); |
105 |
this.header.show();
|
106 |
this.update_layout();
|
107 |
}, |
108 |
|
109 |
reset: function() { |
110 |
} |
111 |
}) |
112 |
|
113 |
views.CreateImageSelectView = views.CreateVMStepView.extend({ |
114 |
|
115 |
initialize: function() { |
116 |
views.CreateImageSelectView.__super__.initialize.apply(this, arguments); |
117 |
|
118 |
// elements
|
119 |
this.images_list_cont = this.$(".images-list-cont"); |
120 |
this.images_list = this.$(".images-list-cont ul"); |
121 |
this.image_details = this.$(".images-info-cont"); |
122 |
this.image_details_desc = this.$(".images-info-cont .description p"); |
123 |
this.image_details_title = this.$(".images-info-cont h4"); |
124 |
this.image_details_size = this.$(".images-info-cont .size p"); |
125 |
this.image_details_os = this.$(".images-info-cont .os p"); |
126 |
this.image_details_kernel = this.$(".images-info-cont .kernel p"); |
127 |
this.image_details_gui = this.$(".images-info-cont .gui p"); |
128 |
|
129 |
this.types = this.$(".type-filter li"); |
130 |
this.categories_list = this.$(".category-filters"); |
131 |
|
132 |
// params initialization
|
133 |
this.type_selections = ["system", "custom"] |
134 |
this.selected_type = "system"; |
135 |
this.selected_categories = [];
|
136 |
this.images = [];
|
137 |
|
138 |
// update
|
139 |
this.update_images();
|
140 |
|
141 |
// handlers initialization
|
142 |
this.init_handlers();
|
143 |
this.init_position();
|
144 |
}, |
145 |
|
146 |
init_position: function() { |
147 |
//this.el.css({position: "absolute"});
|
148 |
//this.el.css({top:"10px"})
|
149 |
}, |
150 |
|
151 |
init_handlers: function() { |
152 |
var self = this; |
153 |
this.types.live("click", function() { |
154 |
self.select_type($(this).attr("id").replace("type-select-","")); |
155 |
}) |
156 |
}, |
157 |
|
158 |
update_images: function() { |
159 |
this.images = storage.images.active();
|
160 |
this.images_ids = _.map(this.images, function(img){return img.id}); |
161 |
if (this.selected_type == "custom") { this.images = []; this.images_ids = []; } |
162 |
|
163 |
return this.images; |
164 |
}, |
165 |
|
166 |
update_layout: function() { |
167 |
this.select_type(this.selected_type); |
168 |
}, |
169 |
|
170 |
get_categories: function(images) { |
171 |
return [];
|
172 |
return ["Desktop", "Server", "Linux", "Windows"]; |
173 |
}, |
174 |
|
175 |
reset_categories: function() { |
176 |
var categories = this.get_categories(this.images); |
177 |
this.categories_list.find("li").remove(); |
178 |
|
179 |
_.each(categories, _.bind(function(cat) {
|
180 |
var el = $("<li />"); |
181 |
el.text(cat); |
182 |
this.categories_list.append(el);
|
183 |
}, this));
|
184 |
|
185 |
if (!categories.length) {
|
186 |
this.categories_list.parent().find(".clear").hide(); |
187 |
this.categories_list.parent().find(".empty").show(); |
188 |
} else {
|
189 |
this.categories_list.parent().find(".clear").show(); |
190 |
this.categories_list.parent().find(".empty").hide(); |
191 |
} |
192 |
}, |
193 |
|
194 |
select_type: function(type) { |
195 |
this.selected_type = type;
|
196 |
this.types.removeClass("selected"); |
197 |
this.types.filter("#type-select-" + this.selected_type).addClass("selected"); |
198 |
|
199 |
this.reset_categories();
|
200 |
this.update_images();
|
201 |
this.reset_images();
|
202 |
this.select_image();
|
203 |
}, |
204 |
|
205 |
select_image: function(image) { |
206 |
if (!image && this.images_ids.length) { |
207 |
if (this.selected_image && this.images_ids.indexOf(this.selected_image.id) > -1) { |
208 |
image = this.selected_image;
|
209 |
} else {
|
210 |
image = storage.images.get(this.images_ids[0]); |
211 |
} |
212 |
} |
213 |
|
214 |
if (!this.images_ids.length) { image = this.selected_image || undefined }; |
215 |
|
216 |
this.selected_image = image;
|
217 |
this.trigger("change", image); |
218 |
|
219 |
if (image) {
|
220 |
this.image_details.show();
|
221 |
this.images_list.find(".image-details").removeClass("selected"); |
222 |
this.images_list.find(".image-details#create-vm-image-" + this.selected_image.id).addClass("selected"); |
223 |
|
224 |
this.image_details_desc.text(image.get("description")); |
225 |
|
226 |
var img = snf.ui.helpers.os_icon_tag(image.get("OS")) |
227 |
this.image_details_title.html(img + image.get("name")); |
228 |
this.image_details_os.text(_(image.get("OS")).capitalize()); |
229 |
this.image_details_kernel.text(image.get("kernel")); |
230 |
|
231 |
var size = image.get_readable_size();
|
232 |
|
233 |
this.image_details_size.text(size);
|
234 |
this.image_details_gui.text(image.get("GUI")); |
235 |
|
236 |
} else {
|
237 |
this.image_details.hide();
|
238 |
} |
239 |
|
240 |
this.validate();
|
241 |
}, |
242 |
|
243 |
reset_images: function() { |
244 |
this.images_list.find("li").remove(); |
245 |
_.each(this.images, _.bind(function(img){ |
246 |
this.add_image(img);
|
247 |
}, this))
|
248 |
|
249 |
if (this.images.length) { |
250 |
this.images_list.parent().find(".empty").hide(); |
251 |
} else {
|
252 |
this.images_list.parent().find(".empty").show(); |
253 |
} |
254 |
|
255 |
this.select_image();
|
256 |
|
257 |
var self = this; |
258 |
this.images_list.find(".image-details").click(function(){ |
259 |
self.select_image($(this).data("image")); |
260 |
}); |
261 |
|
262 |
}, |
263 |
|
264 |
show: function() { |
265 |
views.CreateImageSelectView.__super__.show.apply(this, arguments); |
266 |
}, |
267 |
|
268 |
add_image: function(img) { |
269 |
var image = $(('<li id="create-vm-image-{1}"' + |
270 |
'class="image-details clearfix">{2}{0}' +
|
271 |
'<p>{4}</p><span class="size">{3}' +
|
272 |
'</span></li>').format(img.get("name"), |
273 |
img.id, |
274 |
snf.ui.helpers.os_icon_tag(img.get("OS")),
|
275 |
img.get_readable_size(), |
276 |
util.truncate(img.get("description"),35))); |
277 |
image.data("image", img);
|
278 |
image.data("image_id", img.id);
|
279 |
this.images_list.append(image);
|
280 |
}, |
281 |
|
282 |
reset: function() { |
283 |
this.selected_image = undefined; |
284 |
this.reset_images();
|
285 |
}, |
286 |
|
287 |
get: function() { |
288 |
return {'image': this.selected_image}; |
289 |
}, |
290 |
|
291 |
validate: function() { |
292 |
if (!this.selected_image) { |
293 |
this.parent.$(".form-action.next").hide(); |
294 |
} else {
|
295 |
this.parent.$(".form-action.next").show(); |
296 |
} |
297 |
} |
298 |
}); |
299 |
|
300 |
views.CreateFlavorSelectView = views.CreateVMStepView.extend({ |
301 |
step: 2, |
302 |
initialize: function() { |
303 |
views.CreateFlavorSelectView.__super__.initialize.apply(this, arguments); |
304 |
this.parent.bind("image:change", _.bind(this.handle_image_change, this)); |
305 |
|
306 |
this.cpus = this.$(".flavors-cpu-list"); |
307 |
this.disks = this.$(".flavors-disk-list"); |
308 |
this.mems = this.$(".flavors-mem-list"); |
309 |
|
310 |
this.predefined_flavors = SUGGESTED_FLAVORS;
|
311 |
this.predefined_flavors_keys = _.keys(SUGGESTED_FLAVORS);
|
312 |
this.predefined_flavors_keys = _.sortBy(this.predefined_flavors_keys, _.bind(function(k){ |
313 |
var flv = this.predefined_flavors[k]; |
314 |
return flv.ram * flv.cpu * flv.disk;
|
315 |
}, this));
|
316 |
|
317 |
this.predefined = this.$(".predefined-list"); |
318 |
this.update_predefined_flavors();
|
319 |
}, |
320 |
|
321 |
handle_image_change: function(data) { |
322 |
this.current_image = data;
|
323 |
this.update_valid_predefined();
|
324 |
this.update_flavors_data();
|
325 |
this.reset_flavors();
|
326 |
this.update_layout();
|
327 |
}, |
328 |
|
329 |
reset_flavors: function() { |
330 |
this.$(".flavor-opts-list .option").remove(); |
331 |
this.create_flavors();
|
332 |
}, |
333 |
|
334 |
update_predefined_flavors: function() { |
335 |
this.predefined.find("li").remove(); |
336 |
_.each(this.predefined_flavors_keys, _.bind(function(key) { |
337 |
var val = this.predefined_flavors[key]; |
338 |
var el = $(('<li class="predefined-selection" id="predefined-flavor-{0}">' + |
339 |
'{1}</li>').format(key, _(key).capitalize()));
|
340 |
|
341 |
this.predefined.append(el);
|
342 |
el.data({flavor: storage.flavors.get_flavor(val.cpu, val.ram, val.disk, this.flavors)}) |
343 |
el.click(_.bind(function() {
|
344 |
this.handle_predefined_click(el);
|
345 |
}, this))
|
346 |
}, this));
|
347 |
this.update_valid_predefined();
|
348 |
}, |
349 |
|
350 |
handle_predefined_click: function(el) { |
351 |
if (el.hasClass("disabled")) { return }; |
352 |
this.set_current(el.data("flavor")) |
353 |
}, |
354 |
|
355 |
update_valid_predefined: function() { |
356 |
this.update_unavailable_values();
|
357 |
var self = this; |
358 |
this.valid_predefined = _.select(_.map(this.predefined_flavors, function(flv, key){ |
359 |
var existing = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
|
360 |
// non existing
|
361 |
if (!existing) {
|
362 |
return false; |
363 |
} |
364 |
|
365 |
// not available for image
|
366 |
if (self.unavailable_values && self.unavailable_values.disk.indexOf(existing.get_disk_size()) > -1) { |
367 |
return false |
368 |
} |
369 |
|
370 |
return key;
|
371 |
}), function(ret) { return ret }); |
372 |
|
373 |
$("li.predefined-selection").addClass("disabled"); |
374 |
_.each(this.valid_predefined, function(key) { |
375 |
$("#predefined-flavor-" + key).removeClass("disabled"); |
376 |
}) |
377 |
}, |
378 |
|
379 |
update_selected_predefined: function() { |
380 |
var self = this; |
381 |
this.predefined.find("li").removeClass("selected"); |
382 |
|
383 |
_.each(this.valid_predefined, function(key){ |
384 |
var flv = self.predefined_flavors[key];
|
385 |
var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
|
386 |
|
387 |
if (exists && (exists.id == self.current_flavor.id)) {
|
388 |
$("#predefined-flavor-" + key).addClass("selected"); |
389 |
} |
390 |
}) |
391 |
}, |
392 |
|
393 |
update_flavors_data: function() { |
394 |
this.flavors = storage.flavors.active();
|
395 |
this.flavors_data = storage.flavors.get_data(this.flavors); |
396 |
|
397 |
var self = this; |
398 |
var set = false; |
399 |
|
400 |
// FIXME: validate current flavor
|
401 |
|
402 |
if (!this.current_flavor) { |
403 |
_.each(this.valid_predefined, function(key) { |
404 |
var flv = self.predefined_flavors[key];
|
405 |
var exists = storage.flavors.get_flavor(flv.cpu, flv.ram, flv.disk, self.flavors);
|
406 |
if (exists && !set) {
|
407 |
self.set_current(exists); |
408 |
set = true;
|
409 |
} |
410 |
}) |
411 |
} |
412 |
|
413 |
this.update_unavailable_values();
|
414 |
}, |
415 |
|
416 |
update_unavailable_values: function() { |
417 |
if (!this.current_image) { this.unavailable_values = {disk:[], ram:[], cpu:[]}; return }; |
418 |
this.unavailable_values = storage.flavors.unavailable_values_for_image(this.current_image); |
419 |
}, |
420 |
|
421 |
flavor_is_valid: function(flv) { |
422 |
if (!flv) { return false }; |
423 |
var existing = storage.flavors.get_flavor(flv.get("cpu"), flv.get("ram"), flv.get("disk"), this.flavors); |
424 |
if (!existing) { return false }; |
425 |
if (this.unavailable_values && this.unavailable_values.disk.indexOf(flv.get("disk") > -1)) { |
426 |
return false |
427 |
} |
428 |
return true; |
429 |
}, |
430 |
|
431 |
set_valid_current_for: function(t, val) { |
432 |
var found = this.flavors[0]; |
433 |
_.each(this.flavors, function(flv) { |
434 |
if (flv.get(t) == val) {
|
435 |
found = flv; |
436 |
} |
437 |
}); |
438 |
|
439 |
this.set_current(found);
|
440 |
}, |
441 |
|
442 |
set_current: function(flv) { |
443 |
|
444 |
if (!flv) {
|
445 |
// user clicked on invalid combination
|
446 |
// force the first available choice for the
|
447 |
// type of option he last clicked
|
448 |
this.set_valid_current_for.apply(this, this.last_choice); |
449 |
return;
|
450 |
} |
451 |
|
452 |
this.current_flavor = flv;
|
453 |
this.trigger("change"); |
454 |
if (this.current_flavor) { |
455 |
this.update_selected_flavor();
|
456 |
this.update_selected_predefined();
|
457 |
} |
458 |
|
459 |
this.validate();
|
460 |
}, |
461 |
|
462 |
select_default_flavor: function() { |
463 |
|
464 |
}, |
465 |
|
466 |
update_selected_from_ui: function() { |
467 |
this.set_current(this.ui_selected()); |
468 |
}, |
469 |
|
470 |
update_disabled_flavors: function() { |
471 |
this.$(".flavor-options.disk li").removeClass("disabled"); |
472 |
if (!this.unavailable_values) { return } |
473 |
|
474 |
this.$("#create-vm-flavor-options .flavor-options.disk li").each(_.bind(function(i, el){ |
475 |
var el_value = $(el).data("value") * 1000; |
476 |
if (this.unavailable_values.disk.indexOf(el_value) > -1) { |
477 |
$(el).addClass("disabled"); |
478 |
}; |
479 |
}, this));
|
480 |
}, |
481 |
|
482 |
create_flavors: function() { |
483 |
var flavors = this.get_active_flavors(); |
484 |
var valid_flavors = this.get_valid_flavors(); |
485 |
this.__added_flavors = {'cpu':[], 'ram':[], 'disk':[]}; |
486 |
|
487 |
_.each(flavors, _.bind(function(flv){
|
488 |
this.add_flavor(flv);
|
489 |
}, this));
|
490 |
|
491 |
var self = this; |
492 |
this.$(".flavor-options li.option").click(function(){ |
493 |
var el = $(this); |
494 |
|
495 |
if (el.hasClass("disabled")) { return } |
496 |
|
497 |
el.parent().find(".option").removeClass("selected"); |
498 |
el.addClass("selected");
|
499 |
|
500 |
if (el.hasClass("mem")) { self.last_choice = ["ram", $(this).data("value")] } |
501 |
if (el.hasClass("cpu")) { self.last_choice = ["cpu", $(this).data("value")] } |
502 |
if (el.hasClass("disk")) { self.last_choice = ["disk", $(this).data("value")] } |
503 |
|
504 |
self.update_selected_from_ui(); |
505 |
}) |
506 |
}, |
507 |
|
508 |
ui_selected: function() { |
509 |
var args = [this.$(".option.cpu.selected").data("value"), |
510 |
this.$(".option.mem.selected").data("value"), |
511 |
this.$(".option.disk.selected").data("value"), |
512 |
this.flavors];
|
513 |
|
514 |
var flv = storage.flavors.get_flavor.apply(storage.flavors, args);
|
515 |
return flv;
|
516 |
}, |
517 |
|
518 |
update_selected_flavor: function() { |
519 |
var flv = this.current_flavor; |
520 |
if (!flv) { return } |
521 |
this.$(".option").removeClass("selected"); |
522 |
|
523 |
this.$(".option.cpu.value-" + flv.get("cpu")).addClass("selected"); |
524 |
this.$(".option.mem.value-" + flv.get("ram")).addClass("selected"); |
525 |
this.$(".option.disk.value-" + flv.get("disk")).addClass("selected"); |
526 |
}, |
527 |
|
528 |
__added_flavors: {'cpu':[], 'ram':[], 'disk':[]}, |
529 |
add_flavor: function(flv) { |
530 |
var values = {'cpu': flv.get('cpu'), 'mem': flv.get('ram'), 'disk': flv.get('disk')}; |
531 |
|
532 |
disabled = "";
|
533 |
|
534 |
if (this.__added_flavors.cpu.indexOf(values.cpu) == -1) { |
535 |
var cpu = $(('<li class="option cpu value-{0} {1}">' + |
536 |
'<span class="value">{0}</span>' +
|
537 |
'<span class="metric">x</span></li>').format(values.cpu, disabled)).data('value', values.cpu); |
538 |
this.cpus.append(cpu);
|
539 |
this.__added_flavors.cpu.push(values.cpu);
|
540 |
} |
541 |
|
542 |
if (this.__added_flavors.ram.indexOf(values.mem) == -1) { |
543 |
var mem = $(('<li class="option mem value-{0}">' + |
544 |
'<span class="value">{0}</span>' +
|
545 |
'<span class="metric">MB</span></li>').format(values.mem)).data('value', values.mem); |
546 |
this.mems.append(mem);
|
547 |
this.__added_flavors.ram.push(values.mem);
|
548 |
} |
549 |
|
550 |
if (this.__added_flavors.disk.indexOf(values.disk) == -1) { |
551 |
var disk = $(('<li class="option disk value-{0}">' + |
552 |
'<span class="value">{0}</span>' +
|
553 |
'<span class="metric">GB</span></li>').format(values.disk)).data('value', values.disk); |
554 |
this.disks.append(disk);
|
555 |
this.__added_flavors.disk.push(values.disk)
|
556 |
} |
557 |
|
558 |
|
559 |
}, |
560 |
|
561 |
get_active_flavors: function() { |
562 |
return storage.flavors.active();
|
563 |
}, |
564 |
|
565 |
get_valid_flavors: function() { |
566 |
return this.flavors; |
567 |
}, |
568 |
|
569 |
update_layout: function() { |
570 |
this.update_selected_flavor();
|
571 |
this.update_disabled_flavors();
|
572 |
this.validate();
|
573 |
}, |
574 |
|
575 |
reset: function() { |
576 |
this.current_image = storage.images.at(0); |
577 |
this.flavors = [];
|
578 |
this.flavors_data = {'cpu':[], 'mem':[], 'disk':[]}; |
579 |
this.update_flavors_data();
|
580 |
}, |
581 |
|
582 |
validate: function() { |
583 |
if (!this.current_flavor) { |
584 |
this.parent.$(".form-action.next").hide(); |
585 |
} else {
|
586 |
this.parent.$(".form-action.next").show(); |
587 |
} |
588 |
}, |
589 |
|
590 |
get: function() { |
591 |
return {'flavor': this.current_flavor} |
592 |
} |
593 |
|
594 |
}); |
595 |
|
596 |
views.CreateSubmitView = views.CreateVMStepView.extend({ |
597 |
step: 3, |
598 |
initialize: function() { |
599 |
views.CreateSubmitView.__super__.initialize.apply(this, arguments); |
600 |
this.roles = this.$("li.predefined-meta.role .values"); |
601 |
this.confirm = this.$(".confirm-params ul"); |
602 |
this.name = this.$("input.rename-field"); |
603 |
this.name_changed = false; |
604 |
this.init_suggested_roles();
|
605 |
this.init_handlers();
|
606 |
}, |
607 |
|
608 |
init_suggested_roles: function() { |
609 |
var cont = this.roles; |
610 |
cont.empty(); |
611 |
|
612 |
// TODO: get suggested from snf.api.conf
|
613 |
_.each(window.SUGGESTED_ROLES, function(r){
|
614 |
var el = $('<span class="val">{0}</span>'.format(r)); |
615 |
el.data("value", r);
|
616 |
cont.append(el); |
617 |
el.click(function() {
|
618 |
$(this).parent().find(".val").removeClass("selected"); |
619 |
$(this).toggleClass("selected"); |
620 |
}) |
621 |
}) |
622 |
}, |
623 |
|
624 |
init_handlers: function() { |
625 |
this.name.bind("keypress", _.bind(function(e) { |
626 |
this.name_changed = true; |
627 |
if (e.keyCode == 13) { this.parent.submit() }; |
628 |
}, this));
|
629 |
|
630 |
this.name.bind("click", _.bind(function() { |
631 |
if (!this.name_changed) { |
632 |
this.name.val(""); |
633 |
} |
634 |
}, this))
|
635 |
}, |
636 |
|
637 |
show: function() { |
638 |
views.CreateSubmitView.__super__.show.apply(this, arguments); |
639 |
this.update_layout();
|
640 |
}, |
641 |
|
642 |
update_flavor_details: function() { |
643 |
var flavor = this.parent.get_params().flavor; |
644 |
|
645 |
function set_detail(sel, key) { |
646 |
var val = key;
|
647 |
if (key == undefined) { val = flavor.get(sel) }; |
648 |
this.$(".confirm-cont.flavor .flavor-" + sel + " .value").text(val) |
649 |
} |
650 |
|
651 |
set_detail("cpu", flavor.get("cpu") + "x"); |
652 |
set_detail("ram", flavor.get("ram") + " MB"); |
653 |
set_detail("disk", util.readablizeBytes(flavor.get("disk") * 1024 * 1024 * 1024)); |
654 |
}, |
655 |
|
656 |
update_image_details: function() { |
657 |
var image = this.parent.get_params().image; |
658 |
|
659 |
function set_detail(sel, key) { |
660 |
var val = key;
|
661 |
if (key == undefined) { val = image.get(sel) }; |
662 |
this.$(".confirm-cont.image .image-" + sel + " .value").text(val) |
663 |
} |
664 |
|
665 |
set_detail("description");
|
666 |
set_detail("name");
|
667 |
set_detail("os", _(image.get("OS")).capitalize()); |
668 |
set_detail("gui", image.get("GUI")); |
669 |
set_detail("size", image.get_readable_size());
|
670 |
set_detail("kernel");
|
671 |
}, |
672 |
|
673 |
update_layout: function() { |
674 |
var params = this.parent.get_params(); |
675 |
if (!params.image || !params.flavor) { return } |
676 |
|
677 |
if (!params.image) { return } |
678 |
var vm_name = "My {0} server".format(params.image.get("name")); |
679 |
var orig_name = vm_name;
|
680 |
|
681 |
var existing = true; |
682 |
var j = 0; |
683 |
|
684 |
while (existing && !this.name_changed) { |
685 |
var existing = storage.vms.select(function(vm){return vm.get("name") == vm_name}).length |
686 |
if (existing) {
|
687 |
j++; |
688 |
vm_name = orig_name + " " + j;
|
689 |
} |
690 |
} |
691 |
if (!_(this.name.val()).trim() || !this.name_changed) { |
692 |
this.name.val(vm_name);
|
693 |
} |
694 |
|
695 |
this.confirm.find("li.image .value").text(params.flavor.get("image")); |
696 |
this.confirm.find("li.cpu .value").text(params.flavor.get("cpu") + "x"); |
697 |
this.confirm.find("li.mem .value").text(params.flavor.get("ram")); |
698 |
this.confirm.find("li.disk .value").text(params.flavor.get("disk")); |
699 |
|
700 |
if (!this.name_changed && this.parent.visible()) { |
701 |
if (!$.browser.msie && !$.browser.opera) { |
702 |
this.$("#create-vm-name").select(); |
703 |
} else {
|
704 |
window.setTimeout(_.bind(function(){
|
705 |
this.$("#create-vm-name").select(); |
706 |
}, this), 400) |
707 |
} |
708 |
} |
709 |
|
710 |
var img = snf.ui.helpers.os_icon_path(params.image.get("OS")) |
711 |
this.name.css({backgroundImage:"url({0})".format(img)}) |
712 |
|
713 |
this.update_image_details();
|
714 |
this.update_flavor_details();
|
715 |
}, |
716 |
|
717 |
reset: function() { |
718 |
this.roles.find(".val").removeClass("selected"); |
719 |
this.name_changed = false; |
720 |
this.update_layout();
|
721 |
}, |
722 |
|
723 |
get_meta: function() { |
724 |
if (this.roles.find(".selected").length == 0) { |
725 |
return false; |
726 |
} |
727 |
|
728 |
var role = $(this.roles.find(".selected").get(0)).data("value"); |
729 |
return {'Role': role } |
730 |
}, |
731 |
|
732 |
get: function() { |
733 |
var val = {'name': this.name.val() }; |
734 |
if (this.get_meta()) { |
735 |
val.metadata = this.get_meta();
|
736 |
} |
737 |
|
738 |
return val;
|
739 |
} |
740 |
}); |
741 |
|
742 |
views.CreateVMView = views.Overlay.extend({ |
743 |
|
744 |
view_id: "create_vm_view", |
745 |
content_selector: "#createvm-overlay-content", |
746 |
css_class: 'overlay-createvm overlay-info', |
747 |
overlay_id: "metadata-overlay", |
748 |
|
749 |
subtitle: false, |
750 |
title: "Create new machine", |
751 |
|
752 |
initialize: function(options) { |
753 |
views.CreateVMView.__super__.initialize.apply(this);
|
754 |
this.current_step = 1; |
755 |
|
756 |
this.password_view = new views.VMCreationPasswordView(); |
757 |
|
758 |
this.steps = [];
|
759 |
this.steps[1] = new views.CreateImageSelectView(this); |
760 |
this.steps[1].bind("change", _.bind(function(data) {this.trigger("image:change", data)}, this)); |
761 |
|
762 |
this.steps[2] = new views.CreateFlavorSelectView(this); |
763 |
this.steps[3] = new views.CreateSubmitView(this); |
764 |
|
765 |
this.cancel_btn = this.$(".create-controls .cancel"); |
766 |
this.next_btn = this.$(".create-controls .next"); |
767 |
this.prev_btn = this.$(".create-controls .prev"); |
768 |
this.submit_btn = this.$(".create-controls .submit"); |
769 |
|
770 |
this.history = this.$(".steps-history"); |
771 |
this.history_steps = this.$(".steps-history .steps-history-step"); |
772 |
|
773 |
this.init_handlers();
|
774 |
}, |
775 |
|
776 |
init_handlers: function() { |
777 |
var self = this; |
778 |
this.next_btn.click(_.bind(function(){ |
779 |
this.set_step(this.current_step + 1); |
780 |
this.update_layout();
|
781 |
}, this))
|
782 |
this.prev_btn.click(_.bind(function(){ |
783 |
this.set_step(this.current_step - 1); |
784 |
this.update_layout();
|
785 |
}, this))
|
786 |
this.cancel_btn.click(_.bind(function(){ |
787 |
this.close_all();
|
788 |
}, this))
|
789 |
this.submit_btn.click(_.bind(function(){ |
790 |
this.submit();
|
791 |
}, this))
|
792 |
|
793 |
this.history.find(".completed").live("click", function() { |
794 |
var step = parseInt($(this).attr("id").replace("vm-create-step-history-", "")); |
795 |
self.set_step(step); |
796 |
self.update_layout(); |
797 |
}) |
798 |
}, |
799 |
|
800 |
set_step: function(st) { |
801 |
}, |
802 |
|
803 |
validate: function(data) { |
804 |
if (_(data.name).trim() == "") { |
805 |
this.$(".form-field").addClass("error"); |
806 |
return false; |
807 |
} else {
|
808 |
return true; |
809 |
} |
810 |
}, |
811 |
|
812 |
submit: function() { |
813 |
if (this.submiting) { return }; |
814 |
var data = this.get_params(); |
815 |
var meta = {};
|
816 |
if (this.validate(data)) { |
817 |
this.submit_btn.addClass("in-progress"); |
818 |
this.submiting = true; |
819 |
if (data.metadata) { meta = data.metadata; }
|
820 |
storage.vms.create(data.name, data.image, data.flavor, meta, {}, _.bind(function(data){
|
821 |
this.close_all();
|
822 |
this.password_view.show(data.server.adminPass, data.server.id);
|
823 |
this.submiting = false; |
824 |
}, this));
|
825 |
} |
826 |
}, |
827 |
|
828 |
close_all: function() { |
829 |
this.hide();
|
830 |
}, |
831 |
|
832 |
reset: function() { |
833 |
this.current_step = 1; |
834 |
|
835 |
this.steps[1].reset(); |
836 |
this.steps[2].reset(); |
837 |
this.steps[3].reset(); |
838 |
|
839 |
this.steps[1].show(); |
840 |
this.steps[2].show(); |
841 |
this.steps[3].show(); |
842 |
|
843 |
this.submit_btn.removeClass("in-progress"); |
844 |
}, |
845 |
|
846 |
onShow: function() { |
847 |
this.reset()
|
848 |
this.update_layout();
|
849 |
}, |
850 |
|
851 |
update_layout: function() { |
852 |
this.show_step(this.current_step); |
853 |
this.current_view.update_layout();
|
854 |
}, |
855 |
|
856 |
beforeOpen: function() { |
857 |
this.submiting = false; |
858 |
this.reset();
|
859 |
this.current_step = 1; |
860 |
this.$(".steps-container").css({"margin-left":0 + "px"}); |
861 |
this.show_step(1); |
862 |
}, |
863 |
|
864 |
set_step: function(step) { |
865 |
if (step <= 1) { |
866 |
step = 1
|
867 |
} |
868 |
if (step > this.steps.length - 1) { |
869 |
step = this.steps.length - 1; |
870 |
} |
871 |
this.current_step = step;
|
872 |
}, |
873 |
|
874 |
show_step: function(step) { |
875 |
this.current_view = this.steps[step]; |
876 |
this.update_controls();
|
877 |
|
878 |
this.steps[step].show();
|
879 |
var width = this.el.find('.container').width(); |
880 |
var left = (step -1) * width * -1; |
881 |
this.$(".steps-container").animate({"margin-left": left + "px"}, 300); |
882 |
|
883 |
this.update_steps_history();
|
884 |
}, |
885 |
|
886 |
update_steps_history: function() { |
887 |
var self = this; |
888 |
function get_step(s) { |
889 |
return self.history.find(".step" + s + "h"); |
890 |
} |
891 |
|
892 |
var current_step = parseInt(this.current_view.step); |
893 |
_.each(this.steps, function(stepv) { |
894 |
var step = parseInt(stepv.step);
|
895 |
get_step(step).removeClass("completed").removeClass("current"); |
896 |
if (step == current_step) {
|
897 |
get_step(step).removeClass("completed").addClass("current"); |
898 |
} |
899 |
if (step < current_step) {
|
900 |
get_step(step).removeClass("current").addClass("completed"); |
901 |
} |
902 |
}); |
903 |
}, |
904 |
|
905 |
update_controls: function() { |
906 |
var step = this.current_step; |
907 |
if (step == 1) { |
908 |
this.prev_btn.hide();
|
909 |
this.cancel_btn.show();
|
910 |
} else {
|
911 |
this.prev_btn.show();
|
912 |
this.cancel_btn.hide();
|
913 |
} |
914 |
|
915 |
if (step == this.steps.length - 1) { |
916 |
this.next_btn.hide();
|
917 |
this.submit_btn.show();
|
918 |
} else {
|
919 |
this.next_btn.show();
|
920 |
this.submit_btn.hide();
|
921 |
} |
922 |
}, |
923 |
|
924 |
get_params: function() { |
925 |
return _.extend({}, this.steps[1].get(), this.steps[2].get(), this.steps[3].get()); |
926 |
} |
927 |
}); |
928 |
|
929 |
})(this);
|
930 |
|