Statistics
| Branch: | Tag: | Revision:

root / snf-app / synnefo / ui / static / snf / js / ui / web / ui_custom_images.js @ 1faf0b9c

History | View | Annotate | Download (5.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 api = snf.api = snf.api || {};
9
    var models = snf.models = snf.models || {}
10
    var storage = snf.storage = snf.storage || {};
11
    var ui = snf.ui = snf.ui || {};
12
    var util = snf.util = snf.util || {};
13

    
14
    var views = snf.views = snf.views || {}
15

    
16
    // shortcuts
17
    var bb = root.Backbone;
18
    var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
19

    
20
    views.CustomImagesView = views.CollectionView.extend({
21

    
22
        confirm_delete_msg: 'Are you sure you want to remove this image ?',
23
        create_success_msg: 'Custom image created successfully.',
24
        update_success_msg: 'Custom image updated successfully.',
25
        create_failed_msg: 'Failed to register custom image.',
26

    
27

    
28
        initialize: function(options) {
29
            this.collection = storage.images;
30
            views.CustomImagesView.__super__.initialize.apply(this, arguments);
31
            _.bindAll(this);
32
            this.keys_limit = snf.config.userdata_keys_limit || 10000;
33
            this.bind("item:add", this.animate_on_add);
34
        },
35

    
36
        _get_models: function() {
37
            return this.collection.get_personal_images();
38
        },
39

    
40
        animate_on_add: function(list, el, model) {
41
            el.hide();
42
            el.fadeIn(400);
43
        },
44

    
45
        append_actions: function(el, model) {
46
            var actions = $('<div class="item-actions">' +
47
                            '<div class="item-action remove">remove</div>' + 
48
                            '<div class="item-action confirm-remove">' + 
49
                            '<span class="text do-confirm">confirm</span>' + 
50
                            '<span class="cancel-remove cancel">X</span></div>' + 
51
                            '<div class="item-action edit">edit</div>' + 
52
                            '</div>');
53
            el.append(actions);
54
        },
55

    
56
        update_list_item: function(el, model) {
57
            el.find(".name").text(model.get("name"));
58
            return el;
59
        },
60

    
61
        update_list: function() {
62
            views.CustomImagesView.__super__.update_list.apply(this, arguments);
63
            this.check_limit();
64
        },
65

    
66
        check_limit: function() {
67
            if (snf.storage.keys.length >= this.keys_limit) {
68
                this.$(".collection-action").hide();
69
                this.$(".limit-msg").show();
70
            } else {
71
                this.$(".collection-action").show();
72
                this.$(".limit-msg").hide();
73
            }
74
        },
75

    
76
        update_form_from_model: function(model) {
77
            this.form.find("input.input-name").val(model.get("name"));
78
        },
79

    
80
        get_save_params: function(data, options) {
81
            options.data = {'image': {'serverRef':3, 'name': "test image"}};
82
            return options
83
        },
84

    
85
        get_form_data: function() {
86
            return {
87
                'name': this.form.find("input.input-name").val(),
88
                'url': this.form.find("input.input-url").val(),
89
                'format': this.form.find("input.input-forma").val(),
90
                'is_public': this.form.find("input.input-public").val()
91
            }
92
        },
93
        
94
        get_fields_map: function() {
95
            return {
96
                    'name': "input.input-name", 
97
                    'url': "input.input-url",
98
                    'public': "input.input-public",
99
                    'format': "input.input-format"
100
                };
101
        },
102
        
103
        validate_data: function(data) {
104
            var user_data = _.clone(data)
105
            var errors = new snf.util.errorList();
106
            
107
            if (!data.name || _.clean(data.name) == "") {
108
                errors.add("name", "Provide a valid image name");
109
            }
110

    
111
            if (!data.url || _.clean(data.url) == "" || !urlregex.test(data.url)) {
112
                errors.add("url", "Provide a valid url");
113
            }
114

    
115
            return errors;
116
        },
117

    
118
        reset: function() {
119
            this.$(".list-messages").empty();
120
            this.$(".form-messages").empty();
121
            this.$(".model-item").removeClass("expanded");
122
            this.close_form();
123
        }
124

    
125
    })
126

    
127
    views.CustomImagesOverlay = views.Overlay.extend({
128
        
129
        view_id: "custom_images_view",
130
        content_selector: "#user_custom_images",
131
        css_class: 'overlay-custom-images overlay-info',
132
        overlay_id: "user_custom_images_overlay",
133

    
134
        title: "Manage your OS images",
135
        subtitle: "OS Images",
136

    
137
        initialize: function(options) {
138
            views.CustomImagesOverlay.__super__.initialize.apply(this, arguments);
139
            this.subview = new views.CustomImagesView({el:this.$(".custom-images-view")});
140
            
141
            var self = this;
142
            this.$(".previous-view-link").live('click', function(){
143
                self.hide();
144
            })
145
        },
146

    
147
        show: function(view) {
148
            this.from_view = view || undefined;
149
            
150
            if (this.from_view) {
151
                this.$(".previous-view-link").show();
152
            } else {
153
                this.$(".previous-view-link").hide();
154
            }
155

    
156
            this.subview.reset();
157
            views.CustomImagesOverlay.__super__.show.apply(this, arguments);
158
        },
159
        
160
        onClose: function() {
161
            if (this.from_view) {
162
                this.hiding = true;
163
                this.from_view.skip_reset_on_next_open = true;
164
                this.from_view.show();
165
                this.from_view = undefined;
166
            }
167
        },
168

    
169
        init_handlers: function() {
170
        }
171
        
172
    });
173
})(this);
174