forked from openstack/cloudkitty-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpricing.js
More file actions
149 lines (132 loc) · 5.08 KB
/
pricing.js
File metadata and controls
149 lines (132 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
Copyright 2015 Objectif Libre
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
License for the specific language governing permissions and limitations
under the License.
*/
pricing = {
is_price: false, // Is this a price display ?
init: function() {
this._attachInputHandlers(); // handler
},
init_work: function() {
this.is_price = true;
//$('#price').text('0');
this.init();
this.work();
},
/*
Attaches event handlers for the form elements associated with the price
(see horizon.Quota._attachInputHandlers).
*/
_attachInputHandlers: function() {
var scope = this;
if (this.is_price) {
var eventCallback = function(evt) {
scope.work();
};
$('#id_flavor').on('change', eventCallback);
$('#id_count').on('change', eventCallback);
$('#id_image_id').on('change', eventCallback);
}
},
work: function() {
// Quota available → what about the selected flavor
if (horizon.Quota.selected_flavor) {
// get data of the flavor (form)
_image = horizon.Quota.getSelectedImageOrSnapshot();
var flavor = horizon.Quota.selected_flavor.name;
var vcpus = horizon.Quota.selected_flavor.vcpus;
var disk = horizon.Quota.selected_flavor.disk;
var ephemeral = horizon.Quota.selected_flavor["OS-FLV-EXT-DATA:ephemeral"];
var disk_total = horizon.Quota.selected_flavor.disk + horizon.Quota.selected_flavor["OS-FLV-EXT-DATA:ephemeral"];
var disk_total_display = disk_total;
var ram = horizon.Quota.selected_flavor.ram;
var source_type = $('#id_source_type option:selected').val();
var source_val = $('#id_' + source_type + ' option:selected').val();
var instance_count = parseInt($("#id_count").val());
// make the json data form
desc_form = {
'metadata': {
'flavor': flavor,
'source_type': source_type,
'source_val': source_val, // images : horizon.Quota.findImageById(source_val);
},
'vcpus': vcpus,
'disk': disk,
'ephemeral': ephemeral,
'disk_total': disk_total,
'disk_total_display': disk_total_display,
'ram': ram
}
if (_image != undefined) {
desc_form['image_id'] = _image.id
}
var form_data = [{"desc": desc_form, "volume": instance_count}];
// send the JSON by a POST request
var url_data = [
'/dashboard/project/rating/quote',
'/project/rating/quote']
this.sendPost(form_data, url_data);
}
},
sendPost: function(form_data, url_data) {
var url = url_data.shift();
$.ajax({
type: "post", // send POST data
url: url,
dataType: 'json',
data: JSON.stringify(form_data), // data sent
contentType: 'application/json; charset=utf-8',
success: function (data) {
$("#price").text(data);
},
error: function () {
if (url_data)
pricing.sendPost(form_data, url_data);
},
beforeSend: function(xhr, settings){
$.ajaxSettings.beforeSend(xhr, settings);
}
});
}
};
// https://docs.djangoproject.com/en/dev/ref/contrib/csrf/#ajax
// http://stackoverflow.com/a/6199592
// http://coreymaynard.com/blog/performing-ajax-post-requests-in-django/
function getCookie(name) {
var cookieValue = null;
if (document.cookie && document.cookie != '') {
var cookies = document.cookie.split(';');
for (var i = 0; i < cookies.length; i++) {
var cookie = jQuery.trim(cookies[i]);
// Does this cookie string begin with the name we want?
if (cookie.substring(0, name.length + 1) == (name + '=')) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!(/^http:.*/.test(settings.url) || /^https:.*/.test(settings.url))) {
// Only send the token to relative URLs i.e. locally.
xhr.setRequestHeader("X-CSRFToken", getCookie('csrftoken'));
}
}
});
if(typeof horizon.Quota !== 'undefined') {
pricing.init_work();
} else {
addHorizonLoadEvent(function() {
pricing.init_work();
});
}