Statistics
| Branch: | Tag: | Revision:

root / ui / static / snf / js / ui / web / ui_error_view.js @ a59437e9

History | View | Annotate | Download (6.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 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
    views.ErrorView = views.Overlay.extend({
19
        
20
        view_id: "error_view",
21
        content_selector: "#error-overlay-content",
22
        css_class: 'overlay-error',
23
        overlay_id: "error-overlay",
24
        error_stack: {},
25

    
26
        initialize: function() {
27
            views.ErrorView.__super__.initialize.apply(this, arguments);
28
            var self = this;
29

    
30
            this.error_state = false;
31

    
32
            this.$(".actions .show-details, .actions .hide-details").click(function() {
33
                self.$(".error-details").toggle();
34
                self.$(".show-details").toggle();
35
                self.$(".hide-details").toggle();
36
            });
37

    
38
            this.$(".key.details").click(function() {
39
                $(this).next().toggle();
40
                if (!$(this).next().is(":visible")) {
41
                    $(this).addClass("expand");
42
                } else {
43
                    $(this).removeClass("expand");
44
                }
45
            })
46

    
47
            this.$(".actions .report-error").click(_.bind(function() {
48
                this.report_error();
49
            }, this));
50

    
51
            this.$(".actions .hide-details").hide();
52

    
53
            this.$(".reload-app").click(function(){
54
                window.location.reload(true);
55
            })
56
        },
57

    
58
        error_object: function() {
59
            return {ns:this.ns, code:this.code, message:this.message, details:this.details};
60
        },
61

    
62
        report_error: function() {
63
            this.feedback_view = this.feedback_view || ui.main.feedback_view;
64
            this.hide(false);
65
            window.setTimeout(_.bind(function() {
66
                this.feedback_view.show(this.get_report_message(), true, {error: this.error_object()});
67
            }, this), 400);
68
        },
69

    
70
        get_report_message: function() {
71
            var fdb_msg =   "Error report\n" +
72
                "-------------------" + "\n" +
73
                "Code: " + this.code + "\n" + 
74
                "Type: " + this.type + "\n" +
75
                "Message: " + this.message + "\n" +
76
                "Module: " + this.ns + "\n" +
77
                "Details: " + this.details + "\n\n" +
78
                "Please describe the actions that triggered the error:\n"
79
            
80
            return fdb_msg;
81
        },
82
        
83
        show_error: function(ns, code, message, type, details, error_options) {
84
            if (snf.api.error_state == snf.api.STATES.NORMAL) { this.error_stack = {} };
85
                
86
            var error_entry = [ns, code, message, type, details, error_options];
87
            this.error_stack[new Date()] = error_entry;
88
            this.display_error.apply(this, error_entry);
89
            this.show();
90
        },
91

    
92
        display_error: function(ns, code, message, type, details, error_options) {
93
            this.error_options = {'allow_report': true, 'allow_reload': true, 
94
                'extra_details': {}, 'non_critical': false, 
95
                'allow_details': false };
96
            
97
            if (error_options) {
98
                this.error_options = _.extend(this.error_options, error_options);
99
            }
100

    
101
            this.code = code;
102
            this.ns = ns;
103
            this.type = type;
104
            this.details = details ? (details.toString ? details.toString() : details) : undefined;
105
            this.message = message;
106
            this.title = error_options.title || undefined;
107

    
108
            this.update_details();
109
            
110
            if (error_options.non_critical) {
111
                this.el.addClass("non-critical");
112
                this.error_options.allow_details = false;
113
            } else {
114
                this.el.removeClass("non-critical");
115
                this.error_options.allow_details = true;
116
            }
117
            
118
            if (APP_DEBUG) {
119
                this.error_options.allow_details = true;
120
            }
121
            
122
            this.$(".actions .show-details").click();
123
            this.$(".key.details").click();
124
            this.$(".error-more-details").hide();
125
        },
126

    
127
        update_details: function() {
128
            var title = "Application error";
129
            if (this.ns && this.type) {
130
                title = this.title || this.type + " Error";
131
            }
132

    
133
            this.$(".header .title").text(title);
134
            this.$(".error-code").text(this.code || "");
135
            this.$(".error-type").text(this.type || "");
136
            this.$(".error-module").text(this.ns || "");
137
            this.$(".message p").text(this.message || "");
138
            this.$(".error-more-details p").html(this.details || "no info");
139

    
140
            this.$(".extra-details").remove();
141
            _.each(this.error_options.extra_details, function(value, key){
142
                var opt = $(('<span class="extra-details key">{0}</span>' +
143
                            '<span class="extra-details value">{1}</span>').format(key, value))
144
                this.$(".value.error-type").after(opt);
145
            })
146

    
147
        },
148

    
149
        beforeOpen: function() {
150
            this.$(".error-details").hide();
151
            this.$(".key.details").addClass("expand");
152
            this.$(".show-details").show();
153
            this.$(".hide-details").hide();
154
            
155
            if (this.error_options.allow_details) {
156
                this.$(".show-details").show();
157
            } else {
158
                this.$(".show-details").hide();
159
            }
160

    
161
            if (this.error_options.allow_report) {
162
                this.$(".report-error").show();
163
            } else {
164
                this.$(".report-error").hide();
165
            }
166

    
167
            if (this.error_options.allow_reload) {
168
                this.$(".reload-app").show();
169
            } else {
170
                this.$(".reload-app").hide();
171
            }
172

    
173
        },
174

    
175
        onOpen: function() {
176
            var self = this;
177

    
178
            this.$(".closeme").unbind("click");
179
            this.$(".closeme").bind("click", function(){
180
                self.hide("reset")
181
            })
182
        },
183

    
184
        hide: function(reset_state) {
185
            if (reset_state === "reset") { snf.api.trigger("reset") };
186
            views.ErrorView.__super__.hide.apply(this);
187
        },
188

    
189
        onClose: function(reset_state) {
190
            this.trigger("close", this);
191
        }
192
    });
193

    
194
})(this);