Skip to content

Commit 0c1559c

Browse files
committed
convert frontend javascript into revealing module pattern
1 parent 344e3b4 commit 0c1559c

File tree

4 files changed

+298
-242
lines changed

4 files changed

+298
-242
lines changed

public/themes/pterodactyl/js/frontend/2fa-modal.js

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -17,60 +17,75 @@
1717
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
20-
$(document).ready(function () {
21-
$('#close_reload').click(function () {
22-
location.reload();
23-
});
24-
$('#do_2fa').submit(function (event) {
25-
event.preventDefault();
2620

27-
$.ajax({
28-
type: 'PUT',
29-
url: Router.route('account.security.totp'),
30-
headers: {
31-
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
32-
}
33-
}).done(function (data) {
34-
var image = new Image();
35-
image.src = data.qrImage;
36-
$(image).load(function () {
37-
$('#hide_img_load').slideUp(function () {
38-
$('#qr_image_insert').attr('src', image.src).slideDown();
21+
var TwoFactorModal = (function () {
22+
23+
function bindListeners() {
24+
$(document).ready(function () {
25+
$('#close_reload').click(function () {
26+
location.reload();
27+
});
28+
$('#do_2fa').submit(function (event) {
29+
event.preventDefault();
30+
31+
$.ajax({
32+
type: 'PUT',
33+
url: Router.route('account.security.totp'),
34+
headers: {
35+
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
36+
}
37+
}).done(function (data) {
38+
var image = new Image();
39+
image.src = data.qrImage;
40+
$(image).load(function () {
41+
$('#hide_img_load').slideUp(function () {
42+
$('#qr_image_insert').attr('src', image.src).slideDown();
43+
});
44+
});
45+
$('#2fa_secret_insert').html(data.secret);
46+
$('#open2fa').modal('show');
47+
}).fail(function (jqXHR) {
48+
alert('An error occured while attempting to load the 2FA setup modal. Please try again.');
49+
console.error(jqXHR);
50+
});
51+
52+
});
53+
$('#2fa_token_verify').submit(function (event) {
54+
event.preventDefault();
55+
$('#submit_action').html('<i class="fa fa-spinner fa-spin"></i> Submit').addClass('disabled');
56+
57+
$.ajax({
58+
type: 'POST',
59+
url: Router.route('account.security.totp'),
60+
headers: {
61+
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
62+
},
63+
data: {
64+
token: $('#2fa_token').val()
65+
}
66+
}).done(function (data) {
67+
$('#notice_box_2fa').hide();
68+
if (data === 'true') {
69+
$('#notice_box_2fa').html('<div class="alert alert-success">2-Factor Authentication has been enabled on your account. Press \'Close\' below to reload the page.</div>').slideDown();
70+
} else {
71+
$('#notice_box_2fa').html('<div class="alert alert-danger">The token provided was invalid.</div>').slideDown();
72+
}
73+
}).fail(function (jqXHR) {
74+
$('#notice_box_2fa').html('<div class="alert alert-danger">There was an error while attempting to enable 2-Factor Authentication on this account.</div>').slideDown();
75+
console.error(jqXHR);
76+
}).always(function () {
77+
$('#submit_action').html('Submit').removeClass('disabled');
3978
});
4079
});
41-
$('#2fa_secret_insert').html(data.secret);
42-
$('#open2fa').modal('show');
43-
}).fail(function (jqXHR) {
44-
alert('An error occured while attempting to load the 2FA setup modal. Please try again.');
45-
console.error(jqXHR);
4680
});
81+
}
4782

48-
});
49-
$('#2fa_token_verify').submit(function (event) {
50-
event.preventDefault();
51-
$('#submit_action').html('<i class="fa fa-spinner fa-spin"></i> Submit').addClass('disabled');
83+
return {
84+
init: function () {
85+
bindListeners();
86+
}
87+
}
5288

53-
$.ajax({
54-
type: 'POST',
55-
url: Router.route('account.security.totp'),
56-
headers: {
57-
'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'),
58-
},
59-
data: {
60-
token: $('#2fa_token').val()
61-
}
62-
}).done(function (data) {
63-
$('#notice_box_2fa').hide();
64-
if (data === 'true') {
65-
$('#notice_box_2fa').html('<div class="alert alert-success">2-Factor Authentication has been enabled on your account. Press \'Close\' below to reload the page.</div>').slideDown();
66-
} else {
67-
$('#notice_box_2fa').html('<div class="alert alert-danger">The token provided was invalid.</div>').slideDown();
68-
}
69-
}).fail(function (jqXHR) {
70-
$('#notice_box_2fa').html('<div class="alert alert-danger">There was an error while attempting to enable 2-Factor Authentication on this account.</div>').slideDown();
71-
console.error(jqXHR);
72-
}).always(function () {
73-
$('#submit_action').html('Submit').removeClass('disabled');
74-
});
75-
});
7689
});
90+
91+
TwoFactorModal.init();

public/themes/pterodactyl/js/frontend/server.socket.js

Lines changed: 79 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -17,79 +17,94 @@
1717
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
20-
(function initSocket() {
21-
if (typeof $.notifyDefaults !== 'function') {
22-
console.error('Notify does not appear to be loaded.');
23-
return;
24-
}
2520

26-
if (typeof io !== 'function') {
27-
console.error('Socket.io is reqired to use this panel.');
28-
return;
29-
}
21+
var Server = (function () {
3022

31-
$.notifyDefaults({
32-
placement: {
33-
from: 'bottom',
34-
align: 'right'
35-
},
36-
newest_on_top: true,
37-
delay: 2000,
38-
animate: {
39-
enter: 'animated zoomInDown',
40-
exit: 'animated zoomOutDown'
23+
function initSocket() {
24+
if (typeof $.notifyDefaults !== 'function') {
25+
console.error('Notify does not appear to be loaded.');
26+
return;
27+
}
28+
29+
if (typeof io !== 'function') {
30+
console.error('Socket.io is reqired to use this panel.');
31+
return;
4132
}
42-
});
4333

44-
var notifySocketError = false;
34+
$.notifyDefaults({
35+
placement: {
36+
from: 'bottom',
37+
align: 'right'
38+
},
39+
newest_on_top: true,
40+
delay: 2000,
41+
animate: {
42+
enter: 'animated zoomInDown',
43+
exit: 'animated zoomOutDown'
44+
}
45+
});
4546

46-
window.Socket = io(Pterodactyl.node.scheme + '://' + Pterodactyl.node.fqdn + ':' + Pterodactyl.node.daemonListen + '/ws/' + Pterodactyl.server.uuid, {
47-
'query': 'token=' + Pterodactyl.server.daemonSecret,
48-
});
47+
var notifySocketError = false;
4948

50-
Socket.io.on('connect_error', function (err) {
51-
if(typeof notifySocketError !== 'object') {
52-
notifySocketError = $.notify({
53-
message: 'There was an error attempting to establish a WebSocket connection to the Daemon. This panel will not work as expected.<br /><br />' + err,
54-
}, {
55-
type: 'danger',
56-
delay: 0
57-
});
58-
}
59-
});
49+
window.Socket = io(Pterodactyl.node.scheme + '://' + Pterodactyl.node.fqdn + ':' + Pterodactyl.node.daemonListen + '/ws/' + Pterodactyl.server.uuid, {
50+
'query': 'token=' + Pterodactyl.server.daemonSecret,
51+
});
52+
53+
Socket.io.on('connect_error', function (err) {
54+
if(typeof notifySocketError !== 'object') {
55+
notifySocketError = $.notify({
56+
message: 'There was an error attempting to establish a WebSocket connection to the Daemon. This panel will not work as expected.<br /><br />' + err,
57+
}, {
58+
type: 'danger',
59+
delay: 0
60+
});
61+
}
62+
});
63+
64+
// Connected to Socket Successfully
65+
Socket.on('connect', function () {
66+
if (notifySocketError !== false) {
67+
notifySocketError.close();
68+
notifySocketError = false;
69+
}
70+
});
6071

61-
// Connected to Socket Successfully
62-
Socket.on('connect', function () {
63-
if (notifySocketError !== false) {
64-
notifySocketError.close();
65-
notifySocketError = false;
72+
Socket.on('initial status', function (data) {
73+
setStatusIcon(data.status);
74+
});
75+
76+
Socket.on('status', function (data) {
77+
setStatusIcon(data.status);
78+
});
79+
}
80+
81+
function setStatusIcon(status) {
82+
switch (status) {
83+
case 0:
84+
$('#server_status_icon').html('<i class="fa fa-circle text-danger"></i> Offline');
85+
break;
86+
case 1:
87+
$('#server_status_icon').html('<i class="fa fa-circle text-success"></i> Online');
88+
break;
89+
case 2:
90+
$('#server_status_icon').html('<i class="fa fa-circle text-warning"></i> Starting');
91+
break;
92+
case 3:
93+
$('#server_status_icon').html('<i class="fa fa-circle text-warning"></i> Stopping');
94+
break;
95+
default:
96+
break;
6697
}
67-
});
98+
}
6899

69-
Socket.on('initial status', function (data) {
70-
setStatusIcon(data.status);
71-
});
100+
return {
101+
init: function () {
102+
initSocket();
103+
},
104+
105+
setStatusIcon: setStatusIcon,
106+
}
72107

73-
Socket.on('status', function (data) {
74-
setStatusIcon(data.status);
75-
});
76108
})();
77109

78-
function setStatusIcon(status) {
79-
switch (status) {
80-
case 0:
81-
$('#server_status_icon').html('<i class="fa fa-circle text-danger"></i> Offline');
82-
break;
83-
case 1:
84-
$('#server_status_icon').html('<i class="fa fa-circle text-success"></i> Online');
85-
break;
86-
case 2:
87-
$('#server_status_icon').html('<i class="fa fa-circle text-warning"></i> Starting');
88-
break;
89-
case 3:
90-
$('#server_status_icon').html('<i class="fa fa-circle text-warning"></i> Stopping');
91-
break;
92-
default:
93-
break;
94-
}
95-
}
110+
Server.init();

0 commit comments

Comments
 (0)