pandora/static/js/statisticsDialog.js

524 lines
26 KiB
JavaScript
Raw Permalink Normal View History

2012-03-22 20:08:51 +00:00
'use strict';
pandora.ui.statisticsDialog = function() {
var colors = {
system: {
'Android': [0, 255, 0],
2012-10-27 16:57:28 +00:00
'BlackBerry': [64, 64, 64],
2012-03-27 09:41:03 +00:00
'BSD': [255, 0, 0],
'iOS': [0, 128, 255],
2012-10-27 16:57:28 +00:00
'Java': [128, 128, 128],
'Linux': [255, 128, 0],
'Mac OS X': [0, 255, 255],
2012-10-27 16:57:28 +00:00
'Nokia': [255, 0, 255],
'PlayStation': [192, 192, 192],
2013-07-29 16:23:22 +00:00
'RIM Tablet OS': [64, 64, 64],
2012-03-27 10:03:30 +00:00
'Unix': [255, 255, 0],
2013-07-29 16:23:22 +00:00
'Wii': [192, 192, 192],
2013-07-30 17:03:27 +00:00
'Windows Phone': [0, 0, 128], // has to be before 'Windows'
'Windows': [0, 0, 255]
},
browser: {
2012-10-27 16:57:28 +00:00
'Camino': [192, 192, 192],
2012-10-27 19:53:28 +00:00
'Chrome Frame': [255, 255, 0], // has to be before 'Chrome'
2012-10-27 19:52:37 +00:00
'Chrome': [0, 255, 0],
2012-10-27 19:35:20 +00:00
'Chromium': [128, 255, 0],
2012-10-27 16:57:28 +00:00
'Epiphany': [128, 128, 128],
2012-03-27 10:24:16 +00:00
'Firefox': [255, 128, 0],
'Internet Explorer': [0, 0, 255],
2012-10-27 16:57:28 +00:00
'Konqueror': [64, 64, 64],
'Nokia Browser': [255, 0, 255],
'Opera': [255, 0, 0],
2012-03-27 10:24:16 +00:00
'Safari': [0, 255, 255],
'WebKit': [0, 255, 128]
}
2012-03-22 20:08:51 +00:00
},
dialogHeight = Math.round((window.innerHeight - 48) * 0.9),
dialogWidth = Math.round(window.innerWidth * 0.9),
tabs = [
2013-05-09 10:13:58 +00:00
{id: 'seen', title: Ox._('First Seen & Last Seen'), selected: true},
{id: 'locations', title: Ox._('Locations')},
{id: 'platforms', title: Ox._('Platforms & Browsers')}
2012-03-22 20:08:51 +00:00
],
$dialog = Ox.Dialog({
buttons: [
Ox.Button({
id: 'manageUsers',
2013-05-09 10:13:58 +00:00
title: Ox._('Manage Users...')
}).bindEvent({
click: function() {
$dialog.close();
pandora.$ui.usersDialog = pandora.ui.usersDialog().open();
}
}),
{},
2012-03-22 20:08:51 +00:00
Ox.Button({
id: 'close',
2013-05-09 10:13:58 +00:00
title: Ox._('Close')
2012-03-22 20:08:51 +00:00
}).bindEvent({
click: function() {
$dialog.close();
}
})
],
closeButton: true,
2013-11-20 08:12:59 +00:00
content: Ox.LoadingScreen().start(),
2012-03-22 20:08:51 +00:00
height: dialogHeight,
2012-03-22 20:30:31 +00:00
maximizeButton: true,
2012-03-22 20:08:51 +00:00
minHeight: 256,
minWidth: 512,
removeOnClose: true,
2013-05-09 10:13:58 +00:00
title: Ox._('Statistics'),
2012-03-22 20:08:51 +00:00
width: dialogWidth
})
.bindEvent({
resizeend: function(data) {
dialogWidth = data.width;
2014-09-30 11:52:53 +00:00
$tabPanel.reloadPanel();
}
}),
$tabPanel;
2012-03-22 20:08:51 +00:00
pandora.api.getStatistics(function(result) {
2014-01-19 06:44:19 +00:00
2013-10-22 15:58:47 +00:00
var data = result.data,
2012-03-22 20:08:51 +00:00
flagCountry = {},
$guestsCheckbox;
2012-03-22 20:08:51 +00:00
['all', 'registered'].forEach(function(mode) {
var keys, firstKey, lastKey;
keys = Object.keys(data[mode].month).map(function(key) {
2012-05-24 09:56:52 +00:00
return key.slice(0, 7);
2012-03-22 20:08:51 +00:00
}).sort();
firstKey = keys[0].split('-').map(function(str) {
return parseInt(str, 10);
});
lastKey = Ox.formatDate(new Date(), '%F').split('-').map(function(str) {
2012-03-22 20:08:51 +00:00
return parseInt(str, 10);
});
Ox.loop(firstKey[0], lastKey[0] + 1, function(year) {
['firstseen', 'lastseen'].forEach(function(key) {
var key = [year, key].join('-');
data[mode].year[key] = data[mode].year[key] || {};
2012-03-22 20:08:51 +00:00
});
Ox.loop(
year == firstKey[0] ? firstKey[1] : 1,
year == lastKey[0] ? lastKey[1] + 1 : 13,
function(month) {
['firstseen', 'lastseen'].forEach(function(key) {
var key = [year, Ox.pad(month, 2), key].join('-');
data[mode].month[key] = data[mode].month[key] || 0;
});
}
);
});
keys = Object.keys(data[mode].day).sort();
firstKey = keys[0].split('-').map(function(str) {
return parseInt(str, 10);
});
Ox.loop(firstKey[0], lastKey[0] + 1, function(year) {
Ox.loop(
year == firstKey[0] ? firstKey[1] : 1,
year == lastKey[0] ? lastKey[1] + 1 : 13,
function(month) {
Ox.loop(
year == firstKey[0] && month == firstKey[1] ? firstKey[2] : 1,
year == lastKey[0] && month == lastKey[1] ? lastKey[2] + 1
: Ox.getDaysInMonth(year, month) + 1,
function(day) {
var key = [year, Ox.pad(month, 2), Ox.pad(day, 2)].join('-');
data[mode].day[key] = data[mode].day[key] || {};
}
);
}
);
});
2012-03-22 20:08:51 +00:00
flagCountry[mode] = {};
['continent', 'region'].forEach(function(key) {
flagCountry[mode][key] = {};
Ox.forEach(data[mode][key], function(regionValue, regionKey) {
2012-03-25 21:12:13 +00:00
regionKey = regionKey.split(', ').pop();
2012-03-22 20:08:51 +00:00
var max = 0;
Ox.forEach(data[mode].country, function(countryValue, countryKey) {
2012-03-25 21:12:13 +00:00
countryKey = countryKey.split(', ').pop();
2012-03-22 20:08:51 +00:00
if (
2012-03-26 10:55:49 +00:00
(Ox.getCountryByName(countryKey) || {})[key] == regionKey
2012-03-22 20:08:51 +00:00
&& countryValue > max
) {
flagCountry[mode][key][regionKey] = countryKey;
max = countryValue;
}
});
});
});
});
$guestsCheckbox = Ox.Checkbox({
2013-05-09 10:13:58 +00:00
title: Ox._('Include Guests'),
2012-03-22 20:08:51 +00:00
value: false
})
.css({float: 'left', margin: '4px'})
.bindEvent({
change: function() {
2014-09-30 11:52:53 +00:00
$tabPanel.reloadPanel();
2012-03-22 20:08:51 +00:00
}
});
$tabPanel = Ox.TabPanel({
content: function(id) {
2014-09-26 12:12:25 +00:00
var chartWidth = dialogWidth - 32 - Ox.UI.SCROLLBAR_SIZE,
mode = $guestsCheckbox.options('value') ? 'all' : 'registered',
2012-03-22 20:08:51 +00:00
top = 16,
$content = Ox.Element()
.css({
padding: '16px',
overflowY: 'auto',
background: pandora.user.ui.theme == 'oxlight' ? 'rgb(240, 240, 240)'
: pandora.user.ui.theme == 'oxmedium' ? 'rgb(160, 160, 160)'
2012-03-22 20:08:51 +00:00
: 'rgb(16, 16, 16)'
});
if (id == 'seen') {
['year', 'month', 'lastdays', 'topdays', 'weekday', 'hour'].forEach(function(key) {
var isDate = ['year', 'month'].indexOf(key) > -1,
isDay = ['lastdays', 'topdays'].indexOf(key) > -1;
2012-03-22 20:08:51 +00:00
Ox.Chart({
color: function(value) {
var split = value.split('-'),
color = isDate ? Ox.rgb(
Ox.mod(8 - parseInt(split[1], 10), 12) * 30, 1, 0.5
2012-06-24 15:41:44 +00:00
) : Ox.rgb(
(Math.abs(11.5 - parseInt(split[0], 10)) - 0.5) * -11, 1, 0.5
2012-06-24 15:41:44 +00:00
);
if (pandora.user.ui.theme == 'oxlight') {
color = getColor(color);
}
return color;
},
data: data[mode][isDay ? 'day' : key],
2012-03-22 20:08:51 +00:00
formatKey: function(value) {
2012-03-25 21:12:13 +00:00
var ret, split;
if (isDate) {
2012-03-25 21:12:13 +00:00
split = value.split('-');
ret = (split.pop() == 'firstseen' ? 'First' : 'Last') + ': '
+ (key == 'year' ? '' : Ox.MONTHS[parseInt(split[1], 10) - 1])
+ ' ' + split[0];
} else if (isDay) {
split = value.split('-');
ret = Ox.SHORT_WEEKDAYS[parseInt(Ox.formatDate(value, '%u')) - 1] + ', '
+ Ox.SHORT_MONTHS[parseInt(split[1], 10) - 1] + ' '
+ parseInt(split[2], 10) + ', ' + split[0];
2012-03-25 21:12:13 +00:00
} else {
ret = key == 'weekday'
2012-06-13 13:17:08 +00:00
? Ox.WEEKDAYS[parseInt(value, 10) - 1]
2012-03-25 21:12:13 +00:00
: value + ':00';
}
return ret;
2012-03-22 20:08:51 +00:00
},
keyAlign: 'right',
keyWidth: 128,
limit: isDay ? 30 : 0,
rows: isDate ? 2 : 1,
sort: {
key: key == 'topdays' ? 'value' : 'key',
operator: isDate || isDay ? '-' : '+'
},
title: key == 'lastdays' ? Ox._('Last 30 Days')
: key == 'topdays' ? Ox._('Top 30 Days')
: Ox._(Ox.toTitleCase(key) + 's'),
2012-03-22 20:08:51 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
2013-02-20 08:57:52 +00:00
top += (isDay ? Math.min(Ox.len(data[mode].day), 30) : Ox.len(data[mode][key])) * 16 + 32;
2012-03-22 20:08:51 +00:00
});
} else if (id == 'locations') {
['continent', 'region', 'country', 'city'].forEach(function(key) {
Ox.Chart({
color: function(value) {
var color = Ox.getGeoColor(
key == 'continent' ? value : value.split(', ')[1]
);
if (pandora.user.ui.theme == 'oxlight') {
color = getColor(color);
}
return color;
},
2012-03-22 20:08:51 +00:00
data: data[mode][key],
formatKey: function(value) {
2012-03-25 21:12:13 +00:00
var city, country, split = value.split(', ');
if (key == 'continent' || key == 'region') {
country = flagCountry[mode][key][Ox.last(split)];
2012-03-22 20:08:51 +00:00
} else {
2012-03-25 21:12:13 +00:00
country = split[2];
city = key == 'city' ? split[3] : ''
2012-03-22 20:08:51 +00:00
}
return $('<div>')
.append(
$('<div>')
.css({
float: 'left',
width: '104px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(
Ox.last(split)
2012-03-22 20:08:51 +00:00
)
)
.append(
2012-03-25 21:12:13 +00:00
Ox.Element({
element: '<img>',
tooltip: mode == 'all' && (key == 'continent' || key == 'region')
? Ox.wordwrap(
2012-05-22 15:07:34 +00:00
Ox.COUNTRIES.filter(function(country) {
2012-03-25 21:12:13 +00:00
return country[key] == split[key == 'continent' ? 0 : 1]
&& country.code.length == 2
2012-05-22 15:07:34 +00:00
&& !country.exception
2012-03-25 21:12:13 +00:00
&& !country.disputed
2012-05-22 15:07:34 +00:00
&& !country.dissolved;
}).map(function(country) {
return country.name;
2012-03-25 21:12:13 +00:00
}).sort().join(', '),
64, '<br>', true
).split(', ').map(function(country) {
return Ox.values(Ox.map(data.all.country, function(value, key) {
return key.split(', ').pop()
})).indexOf(country.replace(/<br>/g, '')) > -1
? '<span class="OxBright">' + country + '</span>'
: country
}).join(', ')
: ''
})
2012-03-22 20:08:51 +00:00
.attr({
src: Ox.getFlagByGeoname(country, 16)
})
.css({
float: 'left',
width: '14px',
height: '14px',
borderRadius: '4px',
margin: '0 1px 0 1px'
})
);
},
keyAlign: 'right',
keyWidth: 128,
limit: 1000,
2012-03-22 20:08:51 +00:00
sort: {key: 'value', operator: '-'},
title: (
Ox.endsWith(key, 'y')
2012-05-22 13:15:16 +00:00
? Ox.toTitleCase(key).slice(0, -1) + 'ies'
: Ox.toTitleCase(key) + 's'
) + ' (' + (
Ox.len(data[mode][key]) > 1000
? '1,000 of '
: ''
) + Ox.formatNumber(Ox.len(data[mode][key])) + ')',
2012-03-22 20:08:51 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
2013-05-08 11:53:33 +00:00
top += Math.min(Ox.len(data[mode][key]), 1000) * 16 + 32;
2012-03-22 20:08:51 +00:00
});
} else if (id == 'platforms') {
2012-03-22 20:08:51 +00:00
['', 'version'].forEach(function(version, i) {
['system', 'browser'].forEach(function(key) {
Ox.Chart({
color: function(value) {
2012-10-27 17:28:47 +00:00
var name = version ? getName(key, value) : value,
2012-03-27 09:41:03 +00:00
color = colors[key][name];
if (pandora.user.ui.theme == 'oxlight') {
color = getColor(color);
}
return color;
},
2012-03-22 20:08:51 +00:00
data: data[mode][key + version],
formatKey: function(value) {
2012-10-27 17:28:47 +00:00
var name = version ? getName(key, value) : value,
2012-03-22 20:08:51 +00:00
$element = $('<div>');
$element.append(
$('<div>')
.css({
float: 'left',
width: '168px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
2012-03-27 09:41:03 +00:00
.html(
value
.replace(/BSD \((.+)\)/, '$1')
.replace(/Linux \((.+)\)/, '$1')
2012-03-27 10:03:30 +00:00
.replace(/Unix \((.+)\)/, '$1')
2012-03-27 09:41:03 +00:00
.replace(/Windows (NT \d+\.\d+) \((.+)\)/, 'Windows $2 ($1)')
)
2012-03-22 20:08:51 +00:00
).append(
$('<img>')
.attr({
2014-09-26 12:41:53 +00:00
src: Ox.UI.PATH + 'png/' + key
2012-03-22 20:08:51 +00:00
+ name.replace(/ /g, '') + '128.png'
})
.css({
float: 'left',
width: '14px',
height: '14px',
margin: '0 1px 0 1px'
})
);
return $element;
},
keyWidth: 192,
sort: version == ''
? {key: 'value', operator: '-'}
: {key: 'key', operator: '+'},
title: Ox._(key == 'system'
? (version == '' ? 'Platforms' : 'Platform Versions')
: (version == '' ? 'Browsers' : 'Browser Versions')),
2012-03-22 20:08:51 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode][key + version]) * 16 + 32;
});
2012-03-27 09:41:03 +00:00
Ox.Chart({
color: function(value) {
var color = Ox.zip(value.split(' / ').map(function(v, i) {
2012-10-27 17:34:22 +00:00
var key = ['system', 'browser'][i];
2012-10-27 17:28:47 +00:00
v = version ? getName(key, v) : v;
2012-10-27 17:34:22 +00:00
return colors[key][v];
})).map(function(c) {
return Math.round(Ox.sum(c) / 2);
});
if (pandora.user.ui.theme == 'oxlight') {
color = getColor(color);
2012-03-27 09:41:03 +00:00
}
return color;
},
data: data[mode]['systemandbrowser' + version],
formatKey: function(value) {
var $element = $('<div>')
.append(
$('<div>')
.css({
float: 'left',
width: '152px',
height: '14px',
marginLeft: '-4px',
marginRight: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis'
})
.html(
version
? value
.replace(/BSD \((.+)\)/, '$1')
.replace(/Linux \((.+)\)/, '$1')
.replace(/(Mac OS X \d+\.\d+) \(.+\)/, '$1')
2012-03-27 10:03:30 +00:00
.replace(/Unix \((.+)\)/, '$1')
2012-03-27 09:41:03 +00:00
.replace(/Windows NT \d+\.\d+ \((.+)\)/, 'Windows $1')
.replace(/Chrome Frame/, 'CF')
.replace(/Internet Explorer/, 'IE')
: value
)
);
value.split(' / ').forEach(function(value, i) {
2012-10-27 17:34:22 +00:00
var key = ['system', 'browser'][i];
2012-10-27 17:28:47 +00:00
value = version ? getName(key, value) : value;
2012-03-27 09:41:03 +00:00
$element.append(
$('<img>')
.attr({
2014-09-26 12:41:53 +00:00
src: Ox.UI.PATH + 'png/' + key
2012-03-27 09:41:03 +00:00
+ value.replace(/ /g, '') + '128.png'
})
.css({
width: '14px',
height: '14px',
margin: '0 1px 0 1px'
})
);
});
return $element;
},
keyWidth: 192,
sort: version == ''
? {key: 'value', operator: '-'}
: {key: 'key', operator: '+'},
title: version == ''
? Ox._('Platforms & Browsers')
: Ox._('Platform & Browser Versions'),
2012-03-27 09:41:03 +00:00
width: chartWidth
})
.css({
position: 'absolute',
left: '16px',
top: top + 'px'
})
.appendTo($content);
top += Ox.len(data[mode]['systemandbrowser' + version]) * 16 + 32;
2012-03-22 20:08:51 +00:00
});
}
$('<div>')
.css({
position: 'absolute',
top: top - 16 + 'px',
width: '1px',
height: '16px'
})
.appendTo($content);
return $content;
},
tabs: tabs
});
2012-05-22 13:15:16 +00:00
$tabPanel.find('.OxButtonGroup').css({width: '512px'});
2012-03-22 20:08:51 +00:00
$guestsCheckbox.appendTo($tabPanel.children('.OxBar'));
$dialog.options({content: $tabPanel});
});
function getColor(color) {
var hsl = Ox.hsl(color);
hsl[2] = Math.max(hsl[2] - 0.1, 0);
return Ox.rgb(hsl);
}
2012-10-27 17:28:47 +00:00
function getName(key, version) {
2012-03-27 09:41:03 +00:00
var name = '';
2012-10-27 17:28:47 +00:00
Ox.forEach(Object.keys(colors[key]), function(v) {
2012-03-27 09:41:03 +00:00
if (new RegExp('^' + v).test(version)) {
name = v;
2012-07-04 11:40:37 +00:00
return false;
2012-03-27 09:41:03 +00:00
}
});
return name;
}
2012-03-22 20:08:51 +00:00
return $dialog;
2012-05-22 13:15:16 +00:00
};