]> glassweightruler.freedombox.rocks Git - Ventoy.git/blob - LinuxGUI/WebUI/static/js/vtoy.js
1.0.65 release
[Ventoy.git] / LinuxGUI / WebUI / static / js / vtoy.js
1
2 // 包装ajax请求
3 function callVtoy(p1, p2, p3) {
4 var url = '/vtoy/json';
5 var data = {};
6 var func = function(data) {};
7
8 if (typeof(p1) === 'string') {
9 url = p1;
10 } else if (typeof(p1) === 'object') {
11 data = p1;
12 }
13 if (typeof(p2) === 'object') {
14 data = p2;
15 } else if (typeof(p2) === 'function') {
16 func = p2;
17 }
18 if (typeof(p3) === 'function') {
19 func = p3;
20 }
21
22 //vtoy.debug('callVtoy:\t\t\t\t' + JSON.stringify(data));
23 $.ajax({
24 url: url,
25 type: 'post',
26 cache: false,
27 dataType: 'json',
28 data: JSON.stringify(data),
29 success: func,
30 error: function(xmlHttpRequest, textStatus, errorThrown) {
31
32 if(undefined === errorThrown)
33 {
34 Message.error(vtoy_cur_language.STR_WEB_REMOTE_ABNORMAL);
35 }
36 else if(undefined === errorThrown.length)
37 {
38
39 }
40 else if('' == errorThrown.trim())
41 {
42 }
43 else
44 {
45 switch(errorThrown)
46 {
47 case 'timeout':
48 {
49 Message.error(vtoy_cur_language.STR_WEB_REQUEST_TIMEOUT);
50 break;
51 }
52 case 'Service Unavailable':
53 {
54 Message.error(vtoy_cur_language.STR_WEB_SERVICE_UNAVAILABLE);
55 break;
56 }
57 case 'abort':
58 {
59 break;
60 }
61 default:
62 {
63 Message.error(vtoy_cur_language.STR_WEB_COMMUNICATION_ERR + errorThrown);
64 break;
65 }
66 }
67 }
68 },
69 complete: function(data) {
70 //vtoy.debug('callVtoy\'s resp:\t\t' + data.responseText);
71 }
72 });
73 }
74
75 function callVtoyASyncTimeout(time, data, func) {
76 $.ajax({
77 url: '/vtoy/json',
78 type: 'post',
79 cache: false,
80 dataType: 'json',
81 async: true,
82 timeout: time,
83 data: JSON.stringify(data),
84 success: func,
85 error: function(xmlHttpRequest, textStatus, errorThrown) {
86 if(undefined === errorThrown)
87 {
88 }
89 else if(undefined === errorThrown.length)
90 {
91
92 }
93 else if('' == errorThrown.trim())
94 {
95 }
96 else
97 {
98 switch(errorThrown)
99 {
100 case 'timeout':
101 {
102 callVtoyASyncTimeout(time, data, func);
103 break;
104 }
105 case 'Service Unavailable':
106 {
107 break;
108 }
109 case 'abort':
110 {
111 break;
112 }
113 default:
114 {
115 break;
116 }
117 }
118 }
119 },
120 complete: function(data) {
121 //vtoy.debug('callVtoyASyncTimeout\'s resp:\t' + JSON.stringify(data));
122 }
123 });
124 }
125
126 function callVtoySync(data, func) {
127 //vtoy.debug('callVtoySync:\t\t\t' + JSON.stringify(data));
128 $.ajax({
129 url: '/vtoy/json',
130 type: 'post',
131 cache: false,
132 dataType: 'json',
133 async: false,
134 data: JSON.stringify(data),
135 success: function VtoyCallFuncWrapper(data) {
136 if (data.result === 'tokenerror') {
137 var titlestr = '<span class="fa fa-minus-circle" style="color:#dd4b39; font-weight:bold;"> ' + vtoy_cur_language.STR_ERROR + '</span>';
138 var msgstr = '<span style="font-size:14px; font-weight:bold;"> ' + vtoy_cur_language.STR_WEB_TOKEN_MISMATCH + '</span>';
139
140 Modal.alert({title:titlestr, msg:msgstr, btnok:vtoy_cur_language.STR_BTN_OK }).on(function(e) {
141 window.location.reload(true);
142 });
143 }
144 else if (data.result === 'busy') {
145 var titlestr = '<span class="fa fa-check-circle" style="color:green; font-weight:bold;"> ' + vtoy_cur_language.STR_INFO + '</span>';
146 var msgstr = '<span style="font-size:14px; font-weight:bold;"> ' + vtoy_cur_language.STR_WEB_SERVICE_BUSY + '</span>';
147 Modal.alert({title:titlestr, msg:msgstr, btnok:vtoy_cur_language.STR_BTN_OK });
148 }else {
149 func(data);
150 }
151 },
152 error: function(xmlHttpRequest, textStatus, errorThrown) {
153 if(undefined === errorThrown)
154 {
155 Message.error(vtoy_cur_language.STR_WEB_REMOTE_ABNORMAL);
156 }
157 else if(undefined === errorThrown.length)
158 {
159
160 }
161 else if('' == errorThrown.trim())
162 {
163 }
164 else
165 {
166 switch(errorThrown)
167 {
168 case 'timeout':
169 {
170 Message.error(vtoy_cur_language.STR_WEB_REQUEST_TIMEOUT);
171 break;
172 }
173 case 'Service Unavailable':
174 {
175 Message.error(vtoy_cur_language.STR_WEB_SERVICE_UNAVAILABLE);
176 break;
177 }
178 case 'abort':
179 {
180 break;
181 }
182 default:
183 {
184 Message.error(vtoy_cur_language.STR_WEB_COMMUNICATION_ERR + errorThrown);
185 break;
186 }
187 }
188 }
189 },
190 complete: function(data) {
191 //vtoy.debug('callVtoySync\'s resp:\t' + JSON.stringify(data));
192 }
193 });
194 }
195
196 var vtoy = {
197 baseurl : '',
198 status: '',
199 scan: {
200 time: 3,
201 ret: []
202 }
203 }
204
205 //
206 String.prototype.endsWith = function(str) {
207 if (str == null || str == "" || this.length == 0 || str.length > this.length)
208 return false;
209 if (this.substring(this.length - str.length) == str)
210 return true;
211 else
212 return false;
213 }
214
215 window.Message = function() {
216 var _showMsg = function(type, msg, time) {
217 var o = {type : type, msg : msg };
218 if(time) {
219 o.time = time;
220 }
221 _show(o);
222 }
223
224 var _show = function(options) {
225 var ops = {
226 msg : "提示内容",
227 type: 'S',
228 time: 3000
229 };
230 $.extend(ops, options);
231
232 var msg_class = 'alert-success';
233 if('S' === ops.type || 's' === ops.type) {
234 msg_class = 'alert-success';
235 } else if ('E' === ops.type || 'e' === ops.type) {
236 msg_class = 'alert-danger';
237 } else if ('W' === ops.type || 'w' === ops.type) {
238 msg_class = 'alert-warning';
239 } else if ('I' === ops.type || 'i' === ops.type) {
240 msg_class = 'alert-info';
241 } else {
242 alert("未知的类型,请使用: w-警告;s-成功;e-失败;i-提示");
243 return;
244 }
245 var $messageContainer = $("#fcss_message");
246 if($messageContainer.length === 0) {
247 $messageContainer = $('<div id="fcss_message" style="position:fixed; left: 20%; right: 20%; top:0px; z-index:99999999"></div>');
248 $messageContainer.appendTo($('body'));
249 }
250 var $div = $('<div class="alert ' + msg_class + ' alert-dismissible fade in" role="alert" style="margin-bottom: 0; padding-top:10px; padding-bottom: 10px;"></div>');
251 var $btn = $('<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>');
252 $div.append($btn).append(ops.msg).appendTo($messageContainer);
253 setTimeout(function() {
254 $div.remove();
255 }, ops.time);
256 }
257
258 var _success = function(msg, time) {
259 _showMsg('s', msg, time);
260 }
261 var _error = function(msg, time) {
262 _showMsg('e', msg, time || 5000);
263 }
264 var _warn = function(msg, time) {
265 _showMsg('w', msg, time);
266 }
267 var _info = function(msg, time) {
268 _showMsg('i', msg, time);
269 }
270
271 return {
272 success : _success,
273 error : _error,
274 warn : _warn,
275 info : _info,
276 show : _show
277 }
278 }();
279