-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmumble.js
235 lines (220 loc) · 6.65 KB
/
mumble.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
/*
** Copyright 2011 Jamie Fraser <[email protected]>
** Copyright 2010 Pimmetje <[email protected]>
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Create object to
* Load the mumble viewer inside a div and make use it gets renderd
*
* @param jsonurl a full url to a JSONP from your mumble server
* @param div the div to load the viewer in
*/
function mumbleReader(jsonurl, div) {
var ldiv = div;
var ljsonurl = jsonurl;
//General options
var ltooltip = false;
var limgpath = "http://fwaggle.github.com/MumbleReader.js/";
var llength = 20;
/**************************************************************
* Mumble Reader functions
*/
this.settooltip = function (val) {
ltooltip = val;
};
this.setimgpath = function (val) {
limgpath = val;
};
this.setlength = function (val) {
llength = val;
};
/**
* Give a human readable format voor de time
*
* $param arg time in seconds
* @return string representation of time
*/
var parseTime = function (arg) {
var myTime = [];
myTime[0] = ["seconds", 1];
myTime[1] = ["minutes", 60];
myTime[2] = ["hours", 3600];
myTime[3] = ["days", 86400];
myTime[4] = ["weeks", 604800];
myTime[5] = ["months", 2628000];
myTime[6] = ["years", 31536000];
var i = 1;
while (i < 6 && (myTime[(i + 1)][1]) < arg) {
i++;
}
var temp = Math.floor(arg / myTime[i][1]);
var j = i - 1;
return temp + " " + myTime[i][0] + " " + Math.round((arg - (temp * myTime[i][1])) / myTime[j][1]) + " " + myTime[j][0];
};
/**
* Render a image for use in the viewer
*
* @param file the file name of the image
* @param alt the alt of the image
* @return valid html of the image for viewer
*/
var img = function (file, alt) {
return "<img src=\"" + limgpath + file + "\" alt=\"" + alt + "\" />";
};
/**
* Render channels and subchannels
*
* @param data the json data for the channels to render
* @return valid representation of the channel and subchannels
*/
var channels = function (data) {
var d = "";
$.each(data, function (i, da) {
d += channel(da);
});
return d;
};
/**
* Render a channel
*
* @param data the json data for the channel to render
* @return valid representation of the channel
*/
var channel = function (data) {
var tip = "Name: " + data.name;
var d = "<div class=\"mumbleChannel\"><a tooltip=\"" + tip + "\">" + img('channel.png', '') + data.name.substring(0, llength) + "</a>";
if (data.channels != null) {
d += channels(data.channels);
}
if (data.users != null) {
d += users(data.users);
}
d += "</div>";
return d;
};
/**
* Render users
*
* @param data user data
* @reten all users renderd
*/
var users = function (data) {
var d = "";
$.each(data, function (i, da) {
d += user(da);
});
return d;
};
/**
* Render a user
*
* @param data user data
* @reten the user renderd
*/
var user = function (data) {
var tip = "Name: " + data.name + "<br />Idle: " + parseTime(data.idlesecs) + "<br />Online: " + parseTime(data.onlinesecs) + "<br />OS: " + data.os;
var tipplain = '';
if (!ltooltip) {
tipplain = "Name: " + data.name + "\nIdle: " + parseTime(data.idlesecs) + "\nOnline: " + parseTime(data.onlinesecs) + "\nOS: " + data.os;
}
var imgf = (data.idlesecs == 0) ? img('talking_on.png', '') : img('talking_off.png', '');
var d = "<div class=\"mumbleUser\"><a tooltip=\"" + tip + "\" title=\"" + tipplain + "\">" + imgf + data.name.substring(0, llength);
d += "<div class=\"mumbleFlags\">";
d += userflags(data);
d += "</div></a>";
if (data.channels != null) {
d += users(data.users);
}
d += "</div>";
return d;
};
/**
* Create the img html code for all flags of a user
*
* @param data user data
* @reten HTML code for all the images for user flags
*/
var userflags = function (data) {
var imgf = "";
imgf += (data.mute) ? img('muted_server.png', 'Muted') : "";
imgf += (data.deaf) ? img('deafened_server.png', 'Deafened') : "";
imgf += (data.suppressed) ? img('muted_local.png', 'Suppressed') : "";
imgf += (data.selfMute) ? img('muted_self.png', 'Self-muted') : "";
imgf += (data.selfDeaf) ? img('deafened_self.png', 'Self-deafened') : "";
imgf += (data.id != -1) ? img('authenticated.png', 'Authenticated') : "";
return imgf;
};
/**
* Activate the tooltip for a given div
*
* @param div the div to activate the tooltips on
*/
var activatehover = function () {
// Select all elements with the \"tooltip\" attribute
$('#' + ldiv + ' [tooltip]').each(function () {
$(this).qtip({content: $(this).attr('tooltip')}); // Retrieve the tooltip attribute value from the current element
});
};
/**
* Rebder the mumble settings for root
*
* @param data the server data
* @return the renderd data
*/
var root = function (data) {
var tip = "Uptime: " + parseTime(data.x_uptime);
var d = "<div class=\"mumstatus\">";
var src = (data.x_connecturl != null) ? data.x_connecturl : '';
d += "<a href=\"" + src + "\" tooltip=\"" + tip + "\" title=\"" + tip + "\">" + img('mumble.png', '') + " " + data.name.substring(0, llength) + ": </a><br />";
if (data.root.channels != null) {
d += channels(data.root.channels);
}
if (data.root.users != null) {
d += users(data.root.users);
}
d += "</div>";
return d;
};
/**
* Will render the view and put it in a div
*
* @param data the json data object
*/
var render = function (data) {
if (data != null) {
var d = root(data);
if (ltooltip) {
$('#' + ldiv + ' [tooltip]').qtip("hide");
$('#' + ldiv + ' [tooltip]').qtip("destroy");
}
$('#' + div).empty();
$('#' + div).append(d);
if (ltooltip) {
activatehover();
}
}
};
/**
* This methode will render the viewer
*/
this.start = function () {
$.getJSON(jsonurl,
function (data) {
render(data);
}
);
};
}