root / snf-cyclades-app / synnefo / ui / static / snf / js / glance_models.js @ c16d0b2c
History | View | Annotate | Download (4.5 kB)
1 |
;(function(root){
|
---|---|
2 |
|
3 |
// root
|
4 |
var root = root;
|
5 |
|
6 |
// setup namepsaces
|
7 |
var snf = root.synnefo = root.synnefo || {};
|
8 |
var glance = snf.glance = snf.glance || {};
|
9 |
var models = glance.models = glance.models || {}
|
10 |
var storage = glance.storage = glance.storage || {};
|
11 |
var util = snf.util = snf.util || {};
|
12 |
|
13 |
// shortcuts
|
14 |
var bb = root.Backbone;
|
15 |
var slice = Array.prototype.slice
|
16 |
|
17 |
models.GlanceImage = snf.models.Image.extend({ |
18 |
api_type: 'glance', |
19 |
|
20 |
get_size: function() { |
21 |
return this.get('size') / 1024 / 1024; |
22 |
}, |
23 |
|
24 |
get_readable_size: function() { |
25 |
var unknown_title = snf.config.image_deleted_size_title || "(none)"; |
26 |
if (this.is_deleted()) { return unknown_title } |
27 |
return this.get('size') > 0 ? util.readablizeBytes(this.get('size')) : unknown_title; |
28 |
}, |
29 |
|
30 |
display_size: function() { |
31 |
return this.get_readable_size(); |
32 |
}, |
33 |
|
34 |
get_os: function() { |
35 |
return this.get_meta('os'); |
36 |
}, |
37 |
|
38 |
get_gui: function() { |
39 |
return this.get_meta('gui'); |
40 |
} |
41 |
|
42 |
}) |
43 |
|
44 |
models.GlanceImages = snf.models.Images.extend({ |
45 |
model: models.GlanceImage,
|
46 |
api_type: 'glance', |
47 |
|
48 |
type_selections: {'personal':'My images', |
49 |
'shared': 'Shared with me', |
50 |
'public': 'Public'}, |
51 |
type_selections_order: ['system', 'personal', 'shared', 'public'], |
52 |
display_metadata: ['created_at', 'updated_at', 'filename', 'format', |
53 |
'size', 'status'], |
54 |
read_method: 'head', |
55 |
|
56 |
// custom glance api parser
|
57 |
parse: function (resp, xhr) { |
58 |
if (_.isArray(resp)) {
|
59 |
resp = {'images': {'values': resp }}; |
60 |
} |
61 |
return models.GlanceImages.__super__.parse.call(this, resp, xhr); |
62 |
}, |
63 |
|
64 |
_read_image_from_request: function(image, msg, xhr) { |
65 |
var img = {};
|
66 |
img['metadata'] = {values:{}}; |
67 |
|
68 |
var headers = snf.util.parseHeaders(xhr.getAllResponseHeaders().toLowerCase());
|
69 |
|
70 |
_.each(headers, function(value, key) {
|
71 |
if (key.indexOf("x-image-meta") == -1) { |
72 |
return
|
73 |
} |
74 |
|
75 |
if (key.indexOf("x-image-meta-property") == -1) { |
76 |
img[key.replace("x-image-meta-","").replace(/-/g,"_")] = _.trim(value); |
77 |
} else {
|
78 |
img.metadata.values[key.replace('x-image-meta-property-',"").replace(/-/g,"_")] = _.trim(value); |
79 |
} |
80 |
|
81 |
}) |
82 |
|
83 |
return img;
|
84 |
}, |
85 |
|
86 |
parse_meta: function(img) { |
87 |
if (img.properties) {
|
88 |
img.metadata = {}; |
89 |
img.metadata.values = img.properties; |
90 |
} else {
|
91 |
if (!img.metadata) {
|
92 |
img.metadata = {values:{}};
|
93 |
} |
94 |
} |
95 |
|
96 |
// fixes plankton regression (returns lowercase meta keys)
|
97 |
if (img.metadata.values.os && !img.metadata.values.OS) {
|
98 |
img.metadata.values.OS = img.metadata.values.os; |
99 |
} |
100 |
|
101 |
img = models.GlanceImages.__super__.parse_meta.call(this, img);
|
102 |
return img;
|
103 |
}, |
104 |
|
105 |
get_system_images: function() { |
106 |
return _.filter(this.active(), function(i) { |
107 |
return _.include(_.keys(snf.config.system_images_owners),
|
108 |
i.get_owner()); |
109 |
}) |
110 |
}, |
111 |
|
112 |
get_personal_images: function() { |
113 |
return _.filter(this.active(), function(i) { |
114 |
return i.get_owner() == snf.user.username
|
115 |
}); |
116 |
}, |
117 |
|
118 |
get_public_images: function() { |
119 |
return _.filter(this.active(), function(i){ return i.is_public() }) |
120 |
}, |
121 |
|
122 |
get_shared_images: function() { |
123 |
return _.filter(this.active(), function(i){ |
124 |
return !_.include(_.keys(snf.config.system_images_owners),
|
125 |
i.get_owner()) && |
126 |
i.get_owner() != snf.user.username && |
127 |
!i.is_public(); |
128 |
}); |
129 |
} |
130 |
|
131 |
}) |
132 |
|
133 |
// replace images storage collection
|
134 |
snf.glance.register = function() { |
135 |
// storage initialization
|
136 |
snf.storage.glance = {}; |
137 |
snf.storage.glance.images = new models.GlanceImages();
|
138 |
|
139 |
// use glance images
|
140 |
snf.storage.images = snf.storage.glance.images; |
141 |
} |
142 |
|
143 |
})(this);
|
144 |
|