Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / ui / static / snf / js / glance_models.js @ 30b6f316

History | View | Annotate | Download (3.6 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
            return this.get('size') > 0 ? util.readablizeBytes(this.get('size')) : "unknown";
26
        },
27

    
28
        display_size: function() {
29
            return this.get_readable_size();
30
        }
31
    })
32

    
33
    models.GlanceImages = snf.models.Images.extend({
34
        model: models.GlanceImage,
35
        api_type: 'glance',
36
        type_selections: {'personal':'My images', 
37
                          'shared': 'Shared with me', 
38
                          'public': 'Public'},
39
        type_selections_order: ['system', 'personal', 'shared', 'public'],
40
        display_metadata: ['created_at', 'updated_at', 'filename', 'format', 
41
                            'size', 'status'],
42
        read_method: 'head',
43

    
44
        // custom glance api parser
45
        parse: function (resp, xhr) {
46
            if (_.isArray(resp)) {
47
                resp = {'images': {'values': resp }};
48
            }
49
            return models.GlanceImages.__super__.parse.call(this, resp, xhr);
50
        },
51

    
52
        _read_image_from_request: function(image, msg, xhr) {
53
            var img = {};
54
            img['metadata'] = {values:{}};
55

    
56
            var headers = snf.util.parseHeaders(xhr.getAllResponseHeaders().toLowerCase());
57

    
58
            _.each(headers, function(value, key) {
59
                if (key.indexOf("x-image-meta") == -1) {
60
                    return
61
                }
62

    
63
                if (key.indexOf("x-image-meta-property") == -1) {
64
                    img[key.replace("x-image-meta-","").replace(/-/g,"_")] = _.trim(value);
65
                } else {
66
                    img.metadata.values[key.replace('x-image-meta-property-',"").replace(/-/g,"_")] = _.trim(value);
67
                }
68
            
69
            })
70

    
71
            return img;
72
        },
73

    
74
        parse_meta: function(img) {
75
            if (img.properties) {
76
                img.metadata = {};
77
                img.metadata.values = img.properties;
78
            }
79
            
80
            img = models.GlanceImages.__super__.parse_meta.call(this, img);
81
            return img;
82
        },
83

    
84
        get_system_images: function() {
85
            return _.filter(this.active(), function(i){ return i.get_owner() == snf.config.system_images_owner })
86
        },
87

    
88
        get_personal_images: function() {
89
            return _.filter(this.active(), function(i) { return i.get_owner() == snf.user.username });
90
        },
91

    
92
        get_public_images: function() {
93
            return _.filter(this.active(), function(i){ return i.is_public() })
94
        },
95

    
96
        get_shared_images: function() {
97
            return _.filter(this.active(), function(i){ return i.get_owner() != snf.config.system_images_owner && 
98
                               i.get_owner() != snf.user.username &&
99
                               !i.is_public() })
100
        }
101

    
102
    })
103
        
104
    // replace images storage collection
105
    snf.glance.register = function() {
106
        // storage initialization
107
        snf.storage.glance = {};
108
        snf.storage.glance.images = new models.GlanceImages();
109

    
110
        // use glance images
111
        snf.storage.images = snf.storage.glance.images;
112
    }
113

    
114
})(this);
115