Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_metadata_view.js @ 383d7369

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

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

    
15
    // shortcuts
16
    var bb = root.Backbone;
17

    
18

    
19
    views.MetadataView = views.Overlay.extend({
20
        
21
        view_id: "metadata_view",
22
        content_selector: "#metadata-overlay-content",
23
        css_class: 'overlay-metadata overlay-info',
24
        overlay_id: "metadata-overlay",
25

    
26
        subtitle: "",
27
        title: "Manage metadata",
28

    
29
        initialize: function(options) {
30
            views.MetadataView.__super__.initialize.apply(this);
31
            _.bindAll(this);
32

    
33
            this.current_vm = undefined;
34
            this.list = this.$(".options-list");
35
            this.tpl = this.$("li.meta-object-tpl");
36
            this.editor = this.$(".editor");
37

    
38
            this.pre_init_handlers();
39
        },
40

    
41
        pre_init_handlers: function() {
42
            this.$(".editor .cancel").click(_.bind(function(){
43
                this.close_editor();
44
            }, this))
45
            this.$(".editor .create").click(_.bind(function(){
46
                this.submit_editor();
47
            }, this));
48
        },
49

    
50
        show: function(vm) {
51
            this.current_vm = vm;
52
            views.MetadataView.__super__.show.apply(this);
53
        },
54

    
55
        get_meta: function() {
56
            return this.current_vm.get_meta();
57
        },
58
        
59
        get_meta_el: function(key, value) {
60
            return this.tpl.clone();
61
        },
62

    
63
        beforeOpen: function() {
64
            this.update_layout();
65
        },
66

    
67
        update_layout: function() {
68
            if (!this.editing) {
69
                this.editor.hide();
70
            }
71

    
72
            this.update_vm_details();
73
            // update metadata
74
            this.list.empty();
75
            _.each(this.get_meta(), _.bind(function(value, key) {
76
                var el = this.get_meta_el();
77
                el.find(".title").text(util.truncate(key, 15)).attr("title", key);
78
                el.find(".value").text(util.truncate(value, 15)).attr("title", value);
79
                el.data('meta', {'key':key, 'value':value});
80
                this.list.append(el);
81
                $(el).data({key:key, value:value});
82
            }, this));
83

    
84
            this.list.append('<li class="options-object create">' + 
85
                             '<div class="options-object-cont">' +
86
                             '<span class="title">Add new</span>' + 
87
                             '<span class="value">metadata key</span>' + 
88
                             '</div>' + 
89
                             '</li>')
90

    
91
            this.list.show();
92
            this.init_handlers();
93
        },
94
        
95
        meta_from_el: function(el) {
96
            return el.closest("li").data("meta");
97
        },
98
    
99
        show_editor: function(meta, el) {
100
            this.editing = true;
101

    
102
            this.editor.find("label").removeClass("error");
103
            if (meta) {
104
                this.editor.find(".predefined").hide();
105
                this.editor.find("input.meta-key").val(meta.key).attr("disabled", true);
106
                this.editor.find("input.meta-value").val(meta.value);
107
            } else {
108
                this.editor.find(".predefined").show();
109
                this.editor.find("input.meta-key").val("").attr("disabled", false);
110
                this.editor.find("input.meta-value").val("");
111
            }
112
            this.$(".editor").fadeIn(200);
113

    
114
            if (meta) {
115
                this.editor.find("input.meta-value").focus().select();
116
            } else {
117
                this.editor.find("input.meta-key").focus();
118
            }
119
            
120
            // remove predefined for existing keys
121
            var existing_keys = this.current_vm.get_meta();
122
            if (!meta) {
123
                this.editor.find(".predefined-meta-key").each(function(i, el){
124
                    if (existing_keys[$(el).text()]) {
125
                        $(el).hide();
126
                    } else {
127
                        $(el).show();
128
                    }
129
                })
130
            }
131
        },
132

    
133
        update_vm_details: function() {
134
            // show proper title
135
            this.set_subtitle(this.current_vm.get("name") + snf.ui.helpers.vm_icon_tag(this.current_vm, "small"));
136
        },
137

    
138
        validate: function(meta) {
139
            if (!meta) { return false };
140
            if ((meta.key && meta.key != "") && (meta.value && meta.value != "")) {
141
                return true;
142
            }
143
            return false;
144
        },
145

    
146
        get_editor_values: function() {
147
            var meta = {};
148
            meta.key = this.editor.find("input.meta-key").val();
149
            meta.value = this.editor.find("input.meta-value").val();
150

    
151
            meta.key = _(meta.key).trim();
152
            meta.value = _(meta.value).trim();
153
            return meta;
154
        },
155
    
156
        submit_editor: function() {
157
            if (!this.editing) { return };
158
            this.editing = false;
159
            var meta = this.get_editor_values();
160
            if (this.validate(meta)) {
161
                this.current_vm.save_meta(meta, _.bind(function() {
162
                    this.close_editor();
163
                }, this));
164
            } else {
165
                this.editor.find(".form-field label").addClass("error");
166
            }
167

    
168
        },
169

    
170
        remove_meta: function(key) {
171
            this.current_vm.remove_meta(key);
172
        },
173

    
174
        close_editor: function() {
175
            this.$(".editor").fadeOut(100);
176
            this.editing = false;
177
        },
178

    
179
        init_handlers: function() {
180
            var self = this;
181
            this.list.find(".remove").click(function(e){
182
                e.preventDefault();
183
                var meta = self.meta_from_el($(this));
184
                self.remove_meta(meta.key);
185
                $(this).parent().remove();
186
            });
187

    
188
            this.list.find(".edit").click(function(e) {
189
                e.preventDefault();
190
                var meta = self.meta_from_el($(this));
191
                self.show_editor(meta, $(this));
192
            })
193

    
194
            //this.list.find(".options-object").dblclick(function(e) {
195
                //e.preventDefault();
196
                //var meta = self.meta_from_el($(this));
197
                //self.show_editor(meta, $(this));
198
            //})
199

    
200
            this.list.find("li.create").click(function(){
201
                self.show_create();
202
            })
203
            
204
            this.editor.find("input").keyup(_.bind(function(e){
205
                if (e.keyCode == 13) { this.submit_editor() };    
206
            }, this));
207

    
208
            this.editor.find(".predefined-meta-key").click(function() {
209
                self.editor.find("input.meta-key").val($(this).text());
210
                self.editor.find("input.meta-value").focus();
211
            })
212

    
213
            this.current_vm.bind("change", this.handle_vm_change)
214
        },
215

    
216
        show_create: function() {
217
            this.show_editor();
218
        },
219

    
220
        unbind_vm_handlers: function() {
221
            this.current_vm.unbind("change", this.handle_vm_change);
222
        },
223

    
224
        handle_vm_change: function(vm) {
225
            this.update_vm_details();
226
            this.update_layout();
227
        },
228

    
229
        onClose: function() {
230
            this.editing = false;
231
            this.unbind_vm_handlers();
232
        }
233
    });
234
    
235
})(this);