-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDisconnectBox.js
160 lines (132 loc) · 4.82 KB
/
DisconnectBox.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
/**
* # DisconnectBox
* Copyright(c) 2019 Stefano Balietti
* MIT Licensed
*
* Shows a disconnect button
*
* www.nodegame.org
*/
(function(node) {
"use strict";
node.widgets.register('DisconnectBox', DisconnectBox);
// ## Meta-data
DisconnectBox.version = '0.4.0';
DisconnectBox.description = 'Monitors and handles disconnections';
DisconnectBox.title = false;
DisconnectBox.panel = false;
DisconnectBox.className = 'disconnectbox';
DisconnectBox.texts = {
leave: 'Leave Task',
left: 'You Left',
disconnected: 'Disconnected!',
connected: 'Connected'
};
// ## Dependencies
DisconnectBox.dependencies = {};
/**
* ## DisconnectBox constructor
*
*/
function DisconnectBox() {
// ### DisconnectBox.showStatus
// If TRUE, it shows current connection status. Default: TRUE
this.showStatus = null;
// ### DisconnectBox.showDiscBtn
// If TRUE, it shows the disconnect button. Default: FALSE
this.showDiscBtn = null;
// ### DisconnectBox.statusSpan
// The SPAN containing the status
this.statusSpan = null;
// ### DisconnectBox.disconnectBtn
// The button for disconnection
this.disconnectBtn = null;
// ### DisconnectBox.disconnectBtn
// TRUE, user pressed the disconnect button
this.userDiscFlag = null;
// ### DisconnectBox.ee
// The event emitter with whom the events are registered
this.ee = null;
// ### DisconnectBox.disconnectCb
// Callback executed when a disconnection is detected
this.disconnectCb = null;
// ### DisconnectBox.disconnectCb
// Callback executed when a connection is detected
this.connectCb = null;
}
// ## DisconnectBox methods
DisconnectBox.prototype.init = function(opts) {
if (opts.connectCb) {
if ('function' !== typeof opts.connectCb) {
throw new TypeError('DisconnectBox.init: connectCb must be ' +
'function or undefined. Found: ' +
opts.connectCb);
}
this.connectCb = opts.connectCb;
}
if (opts.disconnectCb) {
if ('function' !== typeof opts.disconnectCb) {
throw new TypeError('DisconnectBox.init: disconnectCb must ' +
'be function or undefined. Found: ' +
opts.disconnectCb);
}
this.disconnectCb = opts.disconnectCb;
}
this.showDiscBtn = !!opts.showDiscBtn;
this.showStatus = !!opts.showStatus;
};
DisconnectBox.prototype.append = function() {
var that, con;
that = this;
con = node.socket.isConnected();
if (this.showStatus) {
this.statusSpan = W.add('span', this.bodyDiv);
this.updateStatus(con ? 'connected' : 'disconnected');
}
if (this.showDiscBtn) {
this.disconnectBtn = W.add('button', this.bodyDiv, {
innerHTML: this.getText(con ? 'leave' : 'left'),
className: 'btn',
style: { 'margin-left': '10px' }
});
if (!con) this.disconnectBtn.disabled = true;
this.disconnectBtn.onclick = function() {
that.disconnectBtn.disabled = true;
that.userDiscFlag = true;
node.socket.disconnect();
};
}
};
DisconnectBox.prototype.updateStatus = function(status) {
if (!this.statusSpan) {
node.warn('DisconnectBox.updateStatus: display disabled.');
return;
}
this.statusSpan.innerHTML = this.getText(status);
this.statusSpan.className = status === 'disconnected' ?
'text-danger' : '';
};
DisconnectBox.prototype.listeners = function() {
var that;
that = this;
this.ee = node.getCurrentEventEmitter();
this.ee.on('SOCKET_DISCONNECT', function() {
if (that.statusSpan) that.updateStatus('disconnected');
if (that.disconnectBtn) {
that.disconnectBtn.disabled = true;
that.disconnectBtn.innerHTML = that.getText('left');
}
if (that.disconnectCb) that.disconnectCb(that.userDiscFlag);
});
this.ee.on('SOCKET_CONNECT', function() {
if (that.statusSpan) that.updateStatus('connected');
if (that.disconnectBtn) {
that.disconnectBtn.disabled = false;
that.disconnectBtn.innerHTML = that.getText('leave');
}
if (that.connectCb) that.disconnectCb();
// Reset pressedDisc.
that.userDiscFlag = false;
});
};
})(node);