Revision 8a6915df snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_vms_base_view.js
b/snf-cyclades-app/synnefo/ui/static/snf/js/ui/web/ui_vms_base_view.js | ||
---|---|---|
54 | 54 |
|
55 | 55 |
var hasKey = Object.prototype.hasOwnProperty; |
56 | 56 |
|
57 |
views.CreateSnapshotView = views.Overlay.extend({ |
|
58 |
view_id: "snapshot_create_view", |
|
59 |
content_selector: "#snapshot-create-content", |
|
60 |
css_class: 'overlay-snapshot-create overlay-info', |
|
61 |
overlay_id: "snapshot-create-overlay", |
|
62 |
|
|
63 |
title: "Create new snapshot", |
|
64 |
subtitle: "Machines", |
|
65 |
|
|
66 |
initialize: function(options) { |
|
67 |
views.CreateSnapshotView.__super__.initialize.apply(this); |
|
68 |
|
|
69 |
this.create_button = this.$("form .form-action.create"); |
|
70 |
this.text = this.$(".snapshot-create-name"); |
|
71 |
this.description = this.$(".snapshot-create-desc"); |
|
72 |
this.form = this.$("form"); |
|
73 |
this.init_handlers(); |
|
74 |
}, |
|
75 |
|
|
76 |
show: function(vm) { |
|
77 |
this.vm = vm; |
|
78 |
views.CreateSnapshotView.__super__.show.apply(this); |
|
79 |
}, |
|
80 |
|
|
81 |
init_handlers: function() { |
|
82 |
|
|
83 |
this.create_button.click(_.bind(function(e){ |
|
84 |
this.submit(); |
|
85 |
}, this)); |
|
86 |
|
|
87 |
this.form.submit(_.bind(function(e){ |
|
88 |
e.preventDefault(); |
|
89 |
this.submit(); |
|
90 |
return false; |
|
91 |
}, this)) |
|
92 |
|
|
93 |
this.text.keypress(_.bind(function(e){ |
|
94 |
if (e.which == 13) {this.submit()}; |
|
95 |
},this)) |
|
96 |
}, |
|
97 |
|
|
98 |
submit: function() { |
|
99 |
if (this.validate()) { |
|
100 |
this.create(); |
|
101 |
}; |
|
102 |
}, |
|
103 |
|
|
104 |
validate: function() { |
|
105 |
// sanitazie |
|
106 |
var t = this.text.val(); |
|
107 |
t = t.replace(/^\s+|\s+$/g,""); |
|
108 |
this.text.val(t); |
|
109 |
|
|
110 |
if (this.text.val() == "") { |
|
111 |
this.text.closest(".form-field").addClass("error"); |
|
112 |
this.text.focus(); |
|
113 |
return false; |
|
114 |
} else { |
|
115 |
this.text.closest(".form-field").removeClass("error"); |
|
116 |
} |
|
117 |
return true; |
|
118 |
}, |
|
119 |
|
|
120 |
create: function() { |
|
121 |
this.create_button.addClass("in-progress"); |
|
122 |
|
|
123 |
var name = this.text.val(); |
|
124 |
var desc = this.description.val(); |
|
125 |
|
|
126 |
this.vm.create_snapshot(name, desc, _.bind(function() { |
|
127 |
this.hide(); |
|
128 |
}, this)); |
|
129 |
}, |
|
130 |
|
|
131 |
_default_values: function() { |
|
132 |
var date = (new Date()).toString(); |
|
133 |
var vmname = this.vm.get('name'); |
|
134 |
var id = this.vm.id; |
|
135 |
|
|
136 |
var name = "snf-{0} '{1}' snapshot ({2})".format(id, vmname, date); |
|
137 |
var description = "Snapshot created at: {0}".format(date); |
|
138 |
description += "\n" + "Machine name: '{0}'".format(vmname); |
|
139 |
description += "\n" + "Machine id: '{0}'".format(id); |
|
140 |
|
|
141 |
return { |
|
142 |
'name': name, |
|
143 |
'description': description |
|
144 |
} |
|
145 |
}, |
|
146 |
|
|
147 |
beforeOpen: function() { |
|
148 |
this.create_button.removeClass("in-progress") |
|
149 |
this.text.closest(".form-field").removeClass("error"); |
|
150 |
var defaults = this._default_values(); |
|
151 |
|
|
152 |
this.text.val(defaults.name); |
|
153 |
this.description.val(defaults.description); |
|
154 |
this.text.show(); |
|
155 |
this.text.focus(); |
|
156 |
this.description.show(); |
|
157 |
}, |
|
158 |
|
|
159 |
onOpen: function() { |
|
160 |
this.text.focus(); |
|
161 |
} |
|
162 |
}); |
|
163 |
|
|
57 | 164 |
// base class for views that contain/handle VMS |
58 | 165 |
views.VMListView = views.View.extend({ |
59 | 166 |
|
... | ... | |
636 | 743 |
// initial hide |
637 | 744 |
if (this.hide) { $(this.el).hide() }; |
638 | 745 |
|
746 |
if (this.$('.snapshot').length) { |
|
747 |
this.$('.snapshot').click(_.bind(function() { |
|
748 |
synnefo.ui.main.create_snapshot_view.show(this.vm); |
|
749 |
}, this)); |
|
750 |
} |
|
639 | 751 |
// action links events |
640 | 752 |
_.each(models.VM.ACTIONS, function(action) { |
641 | 753 |
var action = action; |
Also available in: Unified diff