-
Notifications
You must be signed in to change notification settings - Fork 469
/
notifier.js
152 lines (131 loc) 路 4.92 KB
/
notifier.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* -------------------------------------------------------------------------- */
/* Copyright 2002-2019, OpenNebula Project, OpenNebula Systems */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
define(function(require) {
require("jgrowl");
var Locale = require("utils/locale");
//Notification of submission of action
var _notifySubmit = function(action, args, extra_param) {
var action_text = action.replace(/OpenNebula\./, "").replace(/\./, " ");
var msg = "";
if (!args || (typeof args === "object" && args.constructor != Array)) {
msg += action_text;
} else {
msg += action_text + ": " + args;
};
if (extra_param && extra_param.constructor != Object) {
msg += " >> " + extra_param;
};
$.jGrowl(msg, {theme: "jGrowl-notify-submit", position: "bottom-right"});
};
//Notification on error
var _notifyError = function(msg, target=undefined, context=undefined) {
if(target && context){
var tab = $(target).find($(".is-invalid-input",context));
tab.parents().each(
function(id,element){
var parent = $(element);
if(parent.hasClass("tabs-panel")){
var tabs = parent.parent().siblings($(".tabs",context));
if(tabs && tabs.length){
var find = tabs.find($("a[href$=\"#"+parent.attr("id")+"\"]",context))
if(find && find.length){
find.click();
return false;
}
}
}
}
);
}
$.jGrowl(msg, {theme: "jGrowl-notify-error", position: "bottom-right", sticky: true});
};
//Standard notification
var _notifyMessage = function(msg) {
$.jGrowl(msg, {theme: "jGrowl-notify-submit", position: "bottom-right"});
};
var _notifyCustom = function(title, msg, sticky) {
msg = (title ? title + "</br>" : "") + msg;
$.jGrowl(msg, {theme: "jGrowl-notify-submit", position: "bottom-right", sticky: sticky});
};
//standard handling for the server errors on ajax requests.
//Pops up a message with the information.
var _onError = function(request, error_json, container) {
var method;
var action;
var object;
var id;
var reason;
var m;
var message = error_json.error.message;
//redirect to login if unauthenticated
if (error_json.error.http_status == "401") {
window.location.href = "login";
return false;
};
if (!message) {
_notifyError(Locale.tr("Cannot contact server: is it running and reachable?"));
return false;
};
if (error_json.error.http_status == "404") {
_notifyError(message);
return false;
}
if (container) {
container.show();
return false;
}
if (message.match(/^Network is unreachable .+$/)) {
_notifyError(Locale.tr("Network is unreachable: is OpenNebula running?"));
return false;
};
//Parse known errors:
var get_error = /^\[(\w+)\] Error getting ([\w ]+) \[(\d+)\]\.$/;
var auth_error = /^\[(\w+)\] User \[(\d+)\] not authorized to perform action on ([\w ]+).$/;
if (m = message.match(get_error)) {
method = m[1];
action = "Show";
object = m[2];
id = m[3];
} else if (m = message.match(auth_error)) {
method = m[1];
object = m[3];
reason = Locale.tr("Unauthorized");
};
if (m) {
var rows;
var i;
var value;
rows = ["method", "action", "object", "id", "reason"];
message = "";
for (i = 0; i < rows.length; i++) {
key = rows[i];
value = eval(key);
if (value)
message += "<tr><td class=\"key_error\">" + key + "</td><td>" + value + "</td></tr>";
}
message = "<table>" + message + "</table>";
};
_notifyError(message);
return true;
};
return {
"notifySubmit": _notifySubmit,
"notifyError": _notifyError,
"notifyMessage": _notifyMessage,
"notifyCustom": _notifyCustom,
"onError": _onError
};
});