openmedialibrary/static/js/statusIcon.js

89 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-05-04 17:26:43 +00:00
'use strict';
2014-05-18 23:24:04 +00:00
oml.ui.statusIcon = function(user, index) {
2014-05-04 17:26:43 +00:00
// FIXME: not only '-webkit'
2014-05-18 23:24:04 +00:00
var status = getStatus(user),
2014-05-04 17:26:43 +00:00
that = Ox.Element({
tooltip: Ox._({
connected: 'Connected',
disconnected: 'Disconnected',
transferring: 'Transferring'
}[status])
})
.css({
width: '10px',
height: '10px',
margin: '3px',
borderRadius: '5px'
})
.append(
$('<div>')
.css({
width: '8px',
height: '8px',
margin: '1px',
borderRadius: '4px'
})
);
2014-05-18 23:24:04 +00:00
render();
if (user) {
var superRemove = that.remove;
that.remove = function() {
oml.unbindEvent({
status: update
})
superRemove();
};
oml.bindEvent({
status: update
});
}
2014-05-04 17:26:43 +00:00
2014-05-18 23:24:04 +00:00
function getStatus(data) {
return !oml.user.online && index ? 'unknown'
: data.online ? 'connected'
: 'disconnected';
}
function render() {
var color = {
connected: [[64, 255, 64], [0, 192, 0]],
disconnected: [[255, 64, 64], [192, 0, 0]],
transferring: [[64, 255, 255], [0, 192, 192]],
unknown: [[255, 255, 64], [192, 192, 0]]
}[status].map(function(rgb) {
return 'rgb(' + rgb.join(', ') + ')';
}).join(', ');
that.options({
tooltip: Ox._({
connected: 'Connected',
disconnected: 'Disconnected',
transferring: 'Transferring'
}[status])
}).css({
background: '-webkit-linear-gradient(bottom, ' + color + ')',
});
that.find('div').css({
background: '-webkit-linear-gradient(top, ' + color + ')',
});
}
function update(data) {
if (data.id == user.id) {
var newStatus = getStatus(data);
if (status != newStatus) {
status = newStatus;
render();
}
}
}
return that;
};