-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
243 lines (174 loc) · 7.65 KB
/
main.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
/* GLOBAL CONFIGURATION */
var mm = mm || {};
// IMPORTANT: Update also main.js#version check, latestVersion.js, main.php
mm.version = '3.6.6b';
mm.localStorage = mm.localStorage || new mMLocalStorage();
mm.settings = mm.settings || {};
// Default page to view
mm.settings.mainHash = mm.settings.mainHash || '#page-status';
mm.settings.checkLatestVersion = _.isUndefined(mm.settings.checkLatestVersion) ? true : mm.settings.checkLatestVersion;
mm.settings.notifyDelay = _.isUndefined(mm.settings.notifyDelay) ? 6000 : mm.settings.notifyDelay;
mm.settings.turtleMode = mm.settings.turtleMode || {};
mm.settings.turtleMode.maxDownLimit = _.isUndefined(mm.settings.turtleMode.maxDownLimit) ? 10 : mm.settings.turtleMode.maxDownLimit;
mm.settings.turtleMode.maxUpLimit = _.isUndefined(mm.settings.turtleMode.maxUpLimit) ? 4 : mm.settings.turtleMode.maxUpLimit;
mm.settings.page = mm.settings.page || {};
mm.settings.page.status = mm.settings.page.status || {};
mm.settings.page.status.refresh = _.isUndefined(mm.settings.page.status.refresh) ? 3000 : mm.settings.page.status.refresh;
mm.settings.page.status.tickChart = _.isUndefined(mm.settings.page.status.tickChart) ? 10 : mm.settings.page.status.tickChart;
mm.settings.page.downloads = mm.settings.page.downloads || {};
mm.settings.page.downloads.refreshList = _.isUndefined(mm.settings.page.downloads.refreshList) ? 3000 : mm.settings.page.downloads.refreshList;
mm.settings.page.downloads.filterStatus = mm.localStorage.get('page-downloads-filterStatus', 'all');
mm.settings.page.downloads.filterCategory = mm.localStorage.get('page-downloads-filterCategory', 'all');
mm.settings.page.downloads.sortOn = mm.localStorage.get('page-downloads-sortOn', 'name');
mm.settings.page.downloads.sortOrder = mm.localStorage.get('page-downloads-sortOrder', '0');
mm.settings.page.graph = mm.settings.page.graph || {};
mm.settings.page.graph.refresh = _.isUndefined(mm.settings.page.graph.refresh) ? 3000 : mm.settings.page.graph.refresh;
/* INIT SERVICE WORKER */
if ('serviceWorker' in navigator) {
window.addEventListener('load', function() {
navigator.serviceWorker.register('./service-worker.js', { scope: './' }).then(function(reg) {
if (reg.installing) {
console.log('[service worker] Installing...');
} else if (reg.waiting) {
console.log('[service worker] Waiting...');
} else if (reg.active) {
console.log('[service worker] Active');
}
}).catch(function(error) {
console.log('[service worker] FAILED registration', error);
});
window.addEventListener('beforeinstallprompt', event => {
event.preventDefault();
//event.prompt();
});
});
}
/* JQUERY MOBILE EVENTS */
$(document).one('mobileinit', function() {
// Disable send form through AJAX (default behavior)
$.extend($.mobile, {
ajaxEnabled: false
});
});
$(document).one('pagebeforecreate', function() {
$panel = $('#menu-panel');
idbDownloads.addCurrentDownloads();
});
var globalTimer = 0;
$(document).on('pagebeforecreate', function() {
// Stop timer (es. status page)
if (globalTimer) {
clearInterval(globalTimer);
globalTimer = 0;
}
});
$(document).one('pagecreate', function() {
// Force JQuery mobile to set hash in the url (ancor)
$(document).on('vclick', 'a.hash-link', function(event) {
event.preventDefault();
location.hash = $(this).attr('href');
if (!isDesktop()) {
$panel.panel('close');
}
});
// Update page (not menu) with link.href through ajax
$(document).on('vclick', 'a.ajax-link', function(event) {
event.preventDefault();
var loaderText = $(this).data('text');
var options = {};
if (loaderText) {
options = { loaderText: loaderText, loaderTextVisible: true };
}
$(this).jQMobileAjaxLink(options);
});
$('#pnMenuLogout').on('vclick', function(event) {
event.preventDefault();
eraseCookie('auth');
window.location = $(event.currentTarget).attr('href');
});
$('#btScrollUp').on('vclick', function(event) {
event.preventDefault();
$('html, body').animate({ scrollTop: '0px' });
});
// Support for open panel menu with swipe
$(document).on('swipeleft swiperight', 'div[data-role="page"]', function(event) {
// We check if there is no open panel on the page because otherwise
// a swipe to close the left panel would also open the right panel (and v.v.).
// We do this by checking the data that the framework stores on the page element (panel: open).
if ($('.ui-page-active').jqmData('panel') !== 'open') {
if (event.type === 'swiperight') {
$panel.panel('open');
}
}
});
// Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
/*if (('standalone' in window.navigator) && window.navigator.standalone) {
$('a').bind('vclick', function (event) {
var newLocation = $(event.currentTarget).attr("href");
if (newLocation != undefined && newLocation.substr(0, 1) != '#') {
event.preventDefault();
window.location = newLocation;
}
}
);
}*/
// Call the first hashchange
$(window).hashchange();
$panel.panel().enhanceWithin();
if (isDesktop()) {
$panel.panel('open');
}
// VERSION CHECK
var checkLatestVersion = () => setTimeout(function() {
$.ajax({
url: "https://rawgit.com/elbowz/mobileMule/master/latestVersion.js",
dataType: 'script',
success: function() {
if (latestVersion != mm.version) {
notify.message('<a href="https://github.com/elbowz/mobileMule"> ' + latestVersion + ' release is available!</a>');
}
},
error: function() {
notify.error('Something go wrong during the new version check');
}
});
}, 1000);
if (mm.settings.checkLatestVersion) checkLatestVersion();
if (location.protocol != 'https:') addToHomescreen({ maxDisplayCount: 4 });
});
var oldHash;
$(window).on('hashchange', function() {
location.hash = location.hash || mm.settings.mainHash
var hash = location.hash;
if (oldHash && hash.search(oldHash) == 0) return;
// Check if a ajax call for change the page
if (hash.search('#page-') == 0) {
var file = hash.substr(6) + '.php'
$.mobile.loading('show');
if (!mm.donate) {
if (_.contains(['#page-search', '#page-config', '#page-mobilemule', '#page-footer'], hash)) {
hash = '#page-donate';
file = hash.substr(6) + '.php'
}
}
if (hash == '#page-donate') {
$('#header > h1').html('Donation Pack. <i class="fa fa-thumbs-o-up fa-fw"></i> ');
} else {
// Manipulate menu link text
var head = $panel.find('a[href="' + hash + '"]').clone();
if (head.length) {
head.find('i').removeClass('pull-right');
// Set page header
$('#header > h1').html(head[0].innerHTML);
}
}
// Set page content
$('#container').load(file, function() {
$('#main').trigger('pagebeforecreate').enhanceWithin().trigger('pagecreate');
$.mobile.loading('hide');
});
}
oldHash = hash;
// Google Analytics
ga('send', 'pageview', { 'page': location.pathname + location.search + location.hash });
});