Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.9 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) { this.error_stack = {} };
85
            
86
            snf.api.error_state = true;
87
            snf.api.trigger("change:error_state", true);
88

    
89
            var error_entry = [ns, code, message, type, details, error_options];
90
            this.error_stack[new Date()] = error_entry;
91
            this.display_error.apply(this, error_entry);
92
            this.show();
93
        },
94

    
95
        display_error: function(ns, code, message, type, details, error_options) {
96
            this.error_options = {'allow_report': true, 'allow_reload': true, 'extra_details': {}, 'non_critical': false};
97

    
98
            if (error_options) {
99
                this.error_options = _.extend(this.error_options, error_options);
100
            }
101

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

    
108
            this.update_details();
109
            
110
            if (error_options.non_critical) {
111
                this.el.addClass("non-critical");
112
            } else {
113
                this.el.removeClass("non-critical");
114
            }
115

    
116
            this.$(".actions .show-details").click();
117
            this.$(".key.details").click();
118
            this.$(".error-more-details").hide();
119
        },
120

    
121
        update_details: function() {
122
            var title = "Application error";
123
            if (this.ns && this.type) {
124
                title = this.type + ": " + this.message;
125
            }
126
            this.$(".header .title").text(title);
127
            this.$(".error-code").text(this.code || "");
128
            this.$(".error-type").text(this.type || "");
129
            this.$(".error-module").text(this.ns || "");
130
            this.$(".message p").text(this.message || "");
131
            this.$(".error-more-details p").html(this.details || "no info");
132

    
133
            this.$(".extra-details").remove();
134
            _.each(this.error_options.extra_details, function(value, key){
135
                var opt = $(('<span class="extra-details key">{0}</span>' +
136
                            '<span class="extra-details value">{1}</span>').format(key, value))
137
                this.$(".value.error-type").after(opt);
138
            })
139

    
140
        },
141

    
142
        beforeOpen: function() {
143
            this.$(".error-details").hide();
144
            this.$(".show-details").show();
145
            this.$(".hide-details").hide();
146

    
147
            if (this.error_options.allow_report) {
148
                this.$(".report-error").show();
149
            } else {
150
                this.$(".report-error").hide();
151
            }
152

    
153
            if (this.error_options.allow_reload) {
154
                this.$(".reload-app").show();
155
            } else {
156
                this.$(".reload-app").hide();
157
            }
158
        },
159

    
160
        hide: function(reset_state) {
161
            if (reset_state === undefined) { reset_state = true };
162
            if (reset_state) {
163
                snf.api.error_state = false;
164
                snf.api.trigger("change:error_state", snf.api.error_state);
165
            }
166
            views.ErrorView.__super__.hide.apply(this);
167
        },
168

    
169
        onClose: function(reset_state) {
170
            this.trigger("close", this);
171
        }
172
    });
173

    
174
})(this);