Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / ui / static / snf / js / glance_models.js @ 43ec1206

History | View | Annotate | Download (4.4 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
            }
91

    
92
            // fixes plankton regression (returns lowercase meta keys)
93
            if (img.metadata.values.os && !img.metadata.values.OS) {
94
                img.metadata.values.OS = img.metadata.values.os;
95
            }
96

    
97
            img = models.GlanceImages.__super__.parse_meta.call(this, img);
98
            return img;
99
        },
100

    
101
        get_system_images: function() {
102
            return _.filter(this.active(), function(i) { 
103
                return _.include(_.keys(snf.config.system_images_owners), 
104
                                 i.get_owner());
105
            })
106
        },
107

    
108
        get_personal_images: function() {
109
            return _.filter(this.active(), function(i) { 
110
                return i.get_owner() == snf.user.username 
111
            });
112
        },
113

    
114
        get_public_images: function() {
115
            return _.filter(this.active(), function(i){ return i.is_public() })
116
        },
117

    
118
        get_shared_images: function() {
119
            return _.filter(this.active(), function(i){ 
120
                return !_.include(_.keys(snf.config.system_images_owners), 
121
                                  i.get_owner()) && 
122
                               i.get_owner() != snf.user.username &&
123
                               !i.is_public();
124
            });
125
        }
126

    
127
    })
128
        
129
    // replace images storage collection
130
    snf.glance.register = function() {
131
        // storage initialization
132
        snf.storage.glance = {};
133
        snf.storage.glance.images = new models.GlanceImages();
134

    
135
        // use glance images
136
        snf.storage.images = snf.storage.glance.images;
137
    }
138

    
139
})(this);
140