-
Notifications
You must be signed in to change notification settings - Fork 1
/
internet.js
130 lines (123 loc) · 4.52 KB
/
internet.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
var internetClass = function() {
var type= '';
var TimeoutForCheck= 5000;
var MinAniTime= 700; //2500;
var textWaiting= texty.connecting;
var textNoInternet= texty.nointernet;
var textNoData1= texty.nodata1;
var textNoData2= texty.nodata2;
var textAgain= texty.internetagain;
var _that= this;
// var urlApi = 'http://www.cllevents.com';
this.init= function(){
document.addEventListener("offline", offline, false);
document.addEventListener("online", online, false);
};
/* *
* Wrapper okolo $.post()
* - Jako URL bere urlApi
* - action == parametry pro $.post()
* - ifonlinefce == fce se vykona pokud je zarizeni online + funkcni pripojeni
* - ifofflinefce* == fce, ktera se vykona pokud je zarizeni offline ci je nefunkcni pripojeni
* - animation* == logicka hodnota: if true => prida waiting animation (viz div.modal v html)
* Ad * == neni vyzadovano
* */
this.post= function(action, ifonlinefce, ifofflinefce, animation){
animation= animation || false;
ifofflinefce= ifofflinefce || function (){
console.log(textNoInternet)};
if(animation) _that.animationon();
this.if(function(){
$.post(urlApi, action, function(data){
ifonlinefce(data);
}).done(
_that.animationoff(animation, true)
)}, function(){ifofflinefce(); _that.animationoff(animation, false);}
);
};
/* *
* Switch podle (ne)funkcniho pripojeni
* ... vykona prislusnou fci (nejsou vyzadovany)
* */
this.if= function(ifonlinefce, ifofflinefce){
ifonlinefce= ifonlinefce || function (){};
ifofflinefce= ifofflinefce || function (){};
type= checkConnection();
if (type != "none") {
online().then(function(){
if (type != "none") ifonlinefce(); else ifofflinefce();
});
} else {
ifofflinefce();
}
};
/* *
* Ping
* klasicky ping - dokonceni se zachycuje metodou .then(function(){...})
* */
this.ping= function(url) {
return new Promise(function(resolve, reject){
const request= new XMLHttpRequest();
request.onload= function(){
if (this.status === 200) resolve(true);
else resolve(false);
};
request.onerror= function(){
resolve(false);
//reject(new Error('XMLHttpRequest Error: '+this.statusText));
};
request.open('GET', url);
request.send();
});
};
/* *
* DataError
* - text* == pokud vyplneno => error data nenactena,
* jinak ozn. jako chyba pripojeni!
* Ad * == neni vyzadovano
* */
this.dataerror= function(text) {
var textError= textNoInternet;
if(text) textError= text;
console.log(textError);
$(" #modal > p strong").html(textError+"<br><br>"+texty["internetagain"]+"<br>");
$("body").addClass("loadingError").removeClass("loading");
};
this.animationon= function(){
$("body").addClass("loading");
};
this.animationoff= function(animation, noerror){
setTimeout(function(){
if(animation && noerror) {
$("body").removeClass("loading");
} else if(animation && !noerror){
$("body").removeClass("loading");
_that.dataerror();
} else {}
}, MinAniTime);
};
function online(){
return _that.ping(urlApi).then(function(success){
if(success) type= checkConnection();
else offline();
});
}
function offline(){
console.log("Offline!");
type= 'none';
}
function checkConnection(){
var networkState = navigator.connection.type;
var states = {};
states[Connection.UNKNOWN] = 'Unknown connection';
states[Connection.ETHERNET] = 'Ethernet connection';
states[Connection.WIFI] = 'WiFi connection';
states[Connection.CELL_2G] = 'Cell 2G connection';
states[Connection.CELL_3G] = 'Cell 3G connection';
states[Connection.CELL_4G] = 'Cell 4G connection';
states[Connection.CELL] = 'Cell generic connection';
states[Connection.NONE] = 'none';
return states[networkState];
}
this.init();
};