-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
410 lines (395 loc) · 12 KB
/
util.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
var ajaxFailedHandle=function(jqXHR, textStatus, errorThrown){
return false;
}
var fx={
'displayCenter':function(html,func,useOverlay){
var htmlId='fxOverlay'+Math.random().toString().replace('.','');
var htmlCode='<div style="position: fixed;left: -1000px;top:-1000px;z-index:9999;max-width:100%;" id="'+htmlId+'" class="_fxOverlayCenter">'+html+'</div>';
if(typeof useOverlay!='undefined' && useOverlay)
{
htmlCode='<div id="'+htmlId+'-bg" class="fxoverlay"></div>'+htmlCode;
}
if(jQuery('._fxOverlayCenter').length>0)
{
jQuery('._fxOverlayCenter').fadeOut('fast',function(){
jQuery('body').append(htmlCode);
});
}
else
{
jQuery('body').append(htmlCode);
}
var boxWith=jQuery('#'+htmlId).width();
var boxHeight=jQuery('#'+htmlId).height();
var winWidth=jQuery(window).width();
var winHeight=jQuery(window).height();
var left=Math.floor((winWidth-boxWith)/2);
var top=Math.floor((winHeight-boxHeight)/2);
jQuery('#'+htmlId).css({'display':'none','left':left+'px','top':top+'px'});
jQuery('#'+htmlId+' .fxClose').click(function(){
jQuery('#'+htmlId).fadeOut('fast',function(){
var bgId=jQuery(this).attr('id');
jQuery('#'+bgId+'-bg').fadeOut('fast',function(){
jQuery(this).remove();
});
jQuery(this).remove();
});
});
jQuery('#'+htmlId).fadeIn('fast',function(){
if(typeof func=='function')
{
func();
console.log('Đã gọi hàm callback');
}
console.log('Đã mở overlayPopup _fxOverlayCenter#'+htmlId);
});
return htmlId;
},
'showLoad':function(){
jQuery('#fxloading,#fxLoading,#fx-loading,._fxLoading,.fx-loading').fadeIn('fast');
},
'hideLoad':function(handler){
jQuery('#fxloading,#fxLoading,#fx-loading,._fxLoading,.fx-loading').fadeOut('fast');
},
'removeMessage':function(messageId){
jQuery('#'+messageId).slideUp('slow',function(){
jQuery(this).remove();
});
},
'displayMessage':function(msg){
if(typeof msg != 'string')
{
console.error('msg không phải là string');
console.error(msg);
return false;
}
if(typeof this.messageIndex == 'undefined')
this.messageIndex=10000;
this.messageIndex++;
if(typeof this.lastMessageId == 'undefined')
this.lastMessageId='';
if(this.lastMessageId)
{
jQuery('#'+this.lastMessageId).fadeOut('fast',function(){
jQuery(this).remove();
});
}
var messageId='_fxMsg_'+Math.random().toString().replace('.','');
this.lastMessageId=messageId;
jQuery('body').prepend('<div class="noty_bar noty_theme_default noty_layout_top noty_information" id="'+messageId+'" style="cursor: pointer; display: none; overflow: hidden; height: auto;z-index:'+this.messageIndex+';">\
<div class="noty_message">\
<span class="noty_text">\
'+msg+'\
</span>\
</div>\
</div>');
jQuery('#'+messageId).click(function(){
jQuery(this).slideUp('slow',function(){
jQuery(this).remove();
});
});
setTimeout("fx.removeMessage('"+messageId+"')",2500);
jQuery('#'+messageId).slideDown('fast');
},
'displayPopup':function(title,content,func,useOverlay){
if(typeof title!='string')
var title='';
if(typeof content!='string')
var content='';
if(typeof func!='function')
var func=function(){}
if(typeof useOverlay=='undefined')
var useOverlay=0;
var html='<div class="modal modal-light" id="popup-container" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block; position:static; width: auto; outline: none;max-width:100%;">\
<div class="modal-dialog" style="width: auto; min-width: 400px;">\
<div class="modal-content">\
<div class="modal-header">\
<button type="button" class="close btn-close-popup fxClose" data-dismiss="modal" aria-hidden="true">×</button>\
<h4 class="modal-title" id="popup-header">'+title+'</h4>\
</div>\
<div class="modal-body" id="popup-body" style="padding: 20px;">\
'+content+' \
</div>\
<!-- div class="modal-footer" id="popup-footer">\
<button id="btn-close-popup" class="fxClose btn-close-popup btn btn-default">Đóng</button>\
</div -->\
</div><!-- /.modal-content -->\
</div><!-- /.modal-dialog -->\
</div>';
return this.displayCenter(html,func,useOverlay);
},
'errorStatusHandler':function(data){
console.log('errorStatusHandler:');
console.log(data);
if(typeof data != 'object')
{
console.error('data phải là 1 đối tượng:');
console.error(data);
return false;
}
if(typeof data._fxErrorHandled != 'undefined' && data._fxErrorHandled)
{
return false;
}
if(typeof data._fxError=='string' && jQuery.trim(data._fxError)!='')
{
fx.messageBox('Có lỗi xảy ra',data._fxError);
}
else if(typeof data._fxErrors=='object' && data._fxErrors.length>0)
{
var errorStr=data._fxErrors.join('<br />');
fx.messageBox('Có lỗi xảy ra',errorStr);
console.log(data._fxErrors);
}
else if(typeof data._fxHtml=='string' && jQuery.trim(data._fxHtml)!='')
{
fx.displayCenter(data._fxHtml);
}
else
{
alert('Xảy ra lỗi không mong muốn, bật cửa sổ consolse để xem chi tiết.');
}
},
'scrollTo': function(selector,scrollTime)
{
if(typeof scrollTime!='number' || scrollTime<1000)
var scrollTime=1000;
if(jQuery(selector).length==0)
{
console.error('Không xác định được selector: '+selector+' để tìm vị trí cuộn.');
return false;
}
var boxOffset=jQuery(selector).offset();
var currentScrollTop=jQuery(document).scrollTop();
if(typeof boxOffset=='object' && typeof currentScrollTop=='number' && boxOffset.top!=currentScrollTop)
{
jQuery('body,html').animate({'scrollTop':boxOffset.top},scrollTime);
}
else
{
console.error('boxOffset:');
console.log(boxOffset);
console.error('currentScrollTop');
console.log(currentScrollTop);
}
}
};
//--Hàm tiện ích khác
fx.getCookie=function(name)
{
var regStr="/"+name+"\=([^;]*)/i"
eval('var cookieReg='+regStr);
var results=cookieReg.exec(document.cookie);
if(results!=null && results.length>1)
return results[1];
return '';
}
fx.messageBox=function(title,message,buttons,callback,useOverlay)
{
var _title='Thông báo',_message='',_buttons=[{'text':'OK','href':'javascript:void(0);','title':'Đóng cửa sổ này lại','callback':function(){}}];
var _callback=function(){};
var _useOverlay=false;
if(typeof message=='string')
_message=message;
if(typeof title=='string')
_title=title;
if(typeof buttons=='object')
{
if(buttons instanceof Array)
{
var tmpButtons=[];
for(var i=0;i<buttons.length;i++)
{
var button=buttons[i];
if(typeof button.text=='string')
{
if(typeof button.title!='string')
button.title='';
if(typeof button.href!='string')
button.href='javascript:void(0);';
tmpButtons.push(button);
}
}
if(tmpButtons.length>0)
_buttons=tmpButtons;
}
else
{
_buttons[0]=buttons;
}
}
if(typeof callback=='function')
{
_callback=callback;
}
if(typeof useOverlay!="undefined")
{
_useOverlay=useOverlay;
}
var messageBoxId=Math.random().toString().replace('.','');
var buttonHtml='';
for(var i=0;i<_buttons.length;i++)
{
buttonHtml+='<a id="button-'+i+'" class="btn btn-default" title="'+_buttons[i].title+'" href="'+_buttons[i].href+'">'+_buttons[i].text+'</a>';
}
var html='<div class="modal modal-light messagebox-container" id="'+messageBoxId+'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" style="display: block; position:static; width: auto; outline: none;max-width:100%;box-sizing:border-box;-webkit-box-sizing:border-box;">\
<div class="modal-dialog" style="width: auto; min-width: 320px;">\
<div class="modal-content">\
<div class="modal-header">\
<button type="button" class="close btn-close-popup fxClose" data-dismiss="modal" aria-hidden="true">×</button>\
<h4 class="modal-title" id="popup-header">'+_title+'</h4>\
</div>\
<div class="modal-body" id="popup-body" style="padding: 20px;">\
'+_message+' \
</div>\
<div class="modal-footer" id="popup-footer">\
'+buttonHtml+'\
</div>\
</div><!-- /.modal-content -->\
</div><!-- /.modal-dialog -->\
</div>';
var htmlId=this.displayCenter(html,null,_useOverlay);
for(var i=0;i<_buttons.length;i++)
{
jQuery('#button-'+i).attr('data-order',i);
jQuery('#button-'+i).click(function(){
var currentPath=window.location.pathname;
var order=jQuery(this).attr('data-order');
if(typeof _buttons[order].callback=='function')
_buttons[order].callback();
jQuery('#'+htmlId).fadeOut('fast',function(){
jQuery(this).remove();
});
jQuery('#'+htmlId+'-bg').fadeOut('fast',function(){
jQuery(this).remove();
});
if(currentPath!=window.location.pathname)
{
console.log(currentPath);
console.log(window.location.pathname);
return false;
}
return true;
});
}
if(typeof _callback=='function')
_callback();
}
fx.localStorage={
'check':function(){
if(typeof window.localStorage=="undefined")
{
console.error("Trình duyệt của bạn không hỗ trợ localStorage của Html5");
return false;
}
return true;
},
'set':function(name,value){
if(!fx.localStorage.check())
return false;
if(typeof name!="string" || typeof value!="string")
{
console.error("name và value muốn lưu trong localStorage phải là string");
return false;
}
window.localStorage.setItem(name,value);
return true;
},
'get':function(name){
if(!fx.localStorage.check())
return false;
if(typeof name!="string")
{
console.error("name của record muốn lấy từ localStorage phải là string");
return false;
}
var value=window.localStorage.getItem(name);
if(value==null)
return '';
return value;
}
};
fx.callReady=function(){
jQuery(document).ready();
console.log("Fx: Ready event called !");
}
fx.fireReadyDelay=function(){
setTimeout("fx.callReady()",2000);
console.log("Fx: ReadyDelay has been setup !");
}
jQuery.ajaxSetup({
'beforeSend':function(jqXHR, settings){
//--Auto Add Token
if(typeof fx.token=="string")
{
if(settings.url.indexOf('?')==-1)
settings.url+="?_fxToken="+escape(fx.token);
else
settings.url+="&_fxToken="+escape(fx.token);
console.log('Token Added Automatic !');
}
console.log('Ajax chuẩn bị gửi ....');
if(typeof settings.silentLoad=="undefined" || !settings.silentLoad)
{
fx.showLoad();
}
//console.log(jqXHR);
//console.log(settings);
},
'complete':function(jqXHR, textStatus){
fx.hideLoad();
if(textStatus=='success')
{
console.log('Ajax đã nhận phản hồi thành công !');
console.log('TextStatus: '+textStatus);
}
},
'success':function(data)
{
if(typeof data._fxStatus !='undefined' && !data._fxStatus)
{
console.error('Xảy ra lỗi khi xử lý trên server !');
fx.errorStatusHandler(data);
}
},
'error':function(jqXHR, textStatus, errorThrown){
console.error('ajax lỗi khi gửi/nhận !');
console.warn('errorThrown:'+errorThrown);
console.warn('Loại lỗi:'+textStatus);
console.warn('Nội dung phản hồi:');
console.warn(jqXHR.responseText);
switch(textStatus)
{
case 'timeout':
alert('Không nhận được trả lời từ máy chủ. Vui lòng thử lại.');
break;
case 'parsererror':
alert('Dữ liệu máy chủ trả về bị lỗi. Vui lòng thử lại.');
break;
case 'error':
if(errorThrown==='')
alert('Không kết nối được với máy chủ. Vui lòng thử lại sau.');
else if(errorThrown!==0 && errorThrown!=='0')
alert('Lỗi máy chủ: '+errorThrown+'. Vui lòng thử lại.');
break;
default:
alert('Xảy ra lỗi khi gửi yêu cầu, vui lòng thử lại.');
}
}
});
jQuery(document).ready(function(){
if(jQuery('#fxloading').length==0)
{
jQuery('body').append('<div id="fxloading" style="display:none"></div>');
}
});
/*
jQuery(window).error(function(e){
if(document.cookie.indexOf('debug')==-1)
{
console.error("Có lỗi JS phát sinh, bật Debug để xem.");
e.preventDefault();
return true;
}
});
*/