2010-07-25 23:13:27 -04:00
NEWSBLUR . ReaderStatistics = function ( feed _id , options ) {
var defaults = { } ;
this . options = $ . extend ( { } , defaults , options ) ;
this . model = NEWSBLUR . AssetModel . reader ( ) ;
this . google _favicon _url = 'http://www.google.com/s2/favicons?domain_url=' ;
this . feed _id = feed _id ;
this . feed = this . model . get _feed ( feed _id ) ;
2010-07-30 17:12:20 -04:00
this . feeds = this . model . get _feeds ( ) ;
2010-07-30 23:50:49 -04:00
this . first _load = true ;
2010-07-25 23:13:27 -04:00
this . runner ( ) ;
} ;
NEWSBLUR . ReaderStatistics . prototype = {
runner : function ( ) {
this . make _modal ( ) ;
this . open _modal ( ) ;
this . get _stats ( ) ;
2010-07-30 17:12:20 -04:00
this . $modal . bind ( 'change' , $ . rescope ( this . handle _change , this ) ) ;
2010-07-25 23:13:27 -04:00
} ,
make _modal : function ( ) {
var self = this ;
this . $modal = $ . make ( 'div' , { className : 'NB-modal-statistics NB-modal' } , [
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-modal-loading' } ) ,
2010-07-25 23:13:27 -04:00
$ . make ( 'h2' , { className : 'NB-modal-title' } , 'Statistics & History' ) ,
$ . make ( 'h2' , { className : 'NB-modal-subtitle' } , [
$ . make ( 'img' , { className : 'NB-modal-statistics-feed-image feed_favicon' , src : this . google _favicon _url + this . feed . feed _link } ) ,
$ . make ( 'span' , { className : 'NB-modal-statistics-feed-title' } , this . feed . feed _title )
2010-07-26 22:21:58 -04:00
] ) ,
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-modal-statistics-info' } ) ,
$ . make ( 'div' , { className : 'NB-modal-feed-chooser-container' } , [
this . make _feed _chooser ( )
] )
2010-07-25 23:13:27 -04:00
] ) ;
} ,
2010-07-30 17:12:20 -04:00
initialize _feed : function ( feed _id ) {
this . feed _id = feed _id ;
this . feed = this . model . get _feed ( feed _id ) ;
$ ( '.NB-modal-subtitle .NB-modal-statistics-feed-image' , this . $manage ) . attr ( 'src' , this . google _favicon _url + this . feed [ 'feed_link' ] ) ;
$ ( '.NB-modal-subtitle .NB-modal-statistics-feed-title' , this . $manage ) . html ( this . feed [ 'feed_title' ] ) ;
} ,
2010-07-25 23:13:27 -04:00
open _modal : function ( ) {
var self = this ;
this . $modal . modal ( {
'minWidth' : 600 ,
2010-07-30 23:50:49 -04:00
'minHeight' : 400 ,
2010-07-25 23:13:27 -04:00
'overlayClose' : true ,
'onOpen' : function ( dialog ) {
dialog . overlay . fadeIn ( 200 , function ( ) {
dialog . container . fadeIn ( 200 ) ;
dialog . data . fadeIn ( 200 ) ;
} ) ;
} ,
'onShow' : function ( dialog ) {
2010-07-26 22:21:58 -04:00
$ ( '#simplemodal-container' ) . corner ( '6px' ) ;
2010-07-25 23:13:27 -04:00
} ,
'onClose' : function ( dialog ) {
dialog . data . hide ( ) . empty ( ) . remove ( ) ;
dialog . container . hide ( ) . empty ( ) . remove ( ) ;
dialog . overlay . fadeOut ( 200 , function ( ) {
dialog . overlay . empty ( ) . remove ( ) ;
$ . modal . close ( ) ;
} ) ;
$ ( '.NB-modal-holder' ) . empty ( ) . remove ( ) ;
}
} ) ;
} ,
2010-07-30 17:12:20 -04:00
make _feed _chooser : function ( ) {
var $chooser = $ . make ( 'select' , { name : 'feed' , className : 'NB-modal-feed-chooser' } ) ;
for ( var f in this . feeds ) {
var feed = this . feeds [ f ] ;
var $option = $ . make ( 'option' , { value : feed . id } , feed . feed _title ) ;
$option . appendTo ( $chooser ) ;
if ( feed . id == this . feed _id ) {
$option . attr ( 'selected' , true ) ;
}
}
$ ( 'option' , $chooser ) . tsort ( ) ;
return $chooser ;
} ,
2010-07-25 23:13:27 -04:00
get _stats : function ( ) {
2010-07-30 17:12:20 -04:00
var $loading = $ ( '.NB-modal-loading' , this . $modal ) ;
$loading . addClass ( 'NB-active' ) ;
2010-07-25 23:13:27 -04:00
this . model . get _feed _statistics ( this . feed _id , $ . rescope ( this . populate _stats , this ) ) ;
} ,
populate _stats : function ( s , data ) {
2010-07-30 23:50:49 -04:00
var self = this ;
2010-07-25 23:13:27 -04:00
NEWSBLUR . log ( [ 'Stats' , data ] ) ;
2010-07-30 23:50:49 -04:00
2010-07-30 17:12:20 -04:00
var $loading = $ ( '.NB-modal-loading' , this . $modal ) ;
$loading . removeClass ( 'NB-active' ) ;
2010-07-28 01:14:25 -04:00
var interval _start = data [ 'update_interval_minutes' ] ;
var interval _end = data [ 'update_interval_minutes' ] * 1.25 ;
var interval = '' ;
if ( interval _start < 60 ) {
interval = interval _start + ' to ' + interval _end + ' minutes' ;
} else {
var interval _start _hours = parseInt ( interval _start / 60 , 10 ) ;
var interval _end _hours = parseInt ( interval _end / 60 , 10 ) ;
var dec _start = interval _start % 60 ;
var dec _end = interval _end % 60 ;
interval = interval _start _hours + ( dec _start >= 30 ? '.5' : '' ) + ' to ' + interval _end _hours + ( dec _end >= 30 || interval _start _hours == interval _end _hours ? '.5' : '' ) + ' hours' ;
}
2010-07-30 23:50:49 -04:00
var $stats = this . make _stats ( data , interval ) ;
$ ( '.NB-modal-statistics-info' , this . $modal ) . replaceWith ( $stats ) ;
setTimeout ( function ( ) {
self . make _charts ( data ) ;
} , this . first _load ? 500 : 50 ) ;
setTimeout ( function ( ) {
$ ( '#simplemodal-container' ) . css ( 'height' , null ) ;
$ . modal . impl . setContainerDimensions ( true ) ;
} , 50 ) ;
} ,
make _stats : function ( data , interval ) {
2010-07-26 22:21:58 -04:00
var $stats = $ . make ( 'div' , { className : 'NB-modal-statistics-info' } , [
2010-07-28 01:14:25 -04:00
$ . make ( 'div' , { className : 'NB-statistics-stat NB-statistics-updates' } , [
$ . make ( 'div' , { className : 'NB-statistics-update' } , [
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'Last Update' ) ,
$ . make ( 'div' , { className : 'NB-statistics-count' } , data [ 'last_update' ] + ' ago' )
2010-07-28 01:14:25 -04:00
] ) ,
$ . make ( 'div' , { className : 'NB-statistics-update' } , [
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'Every' ) ,
$ . make ( 'div' , { className : 'NB-statistics-count' } , interval )
2010-07-28 01:14:25 -04:00
] ) ,
$ . make ( 'div' , { className : 'NB-statistics-update' } , [
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'Next Update' ) ,
$ . make ( 'div' , { className : 'NB-statistics-count' } , 'in ' + data [ 'next_update' ] )
2010-07-28 01:14:25 -04:00
] )
] ) ,
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-statistics-stat NB-statistics-history' } , [
$ . make ( 'div' , { className : 'NB-statistics-history-stat' } , [
$ . make ( 'div' , { className : 'NB-statistics-count' } , '' + data [ 'subscriber_count' ] ) ,
2010-07-30 23:50:49 -04:00
$ . make ( 'div' , { className : 'NB-statistics-label' } , 'subscribers' ) ,
2010-07-30 17:12:20 -04:00
$ . make ( 'div' , { className : 'NB-statistics-count' } , '' + data [ 'average_stories_per_month' ] ) ,
$ . make ( 'div' , { className : 'NB-statistics-label' } , ' stories per month' )
2010-07-30 23:50:49 -04:00
] ) ,
$ . make ( 'div' , { id : 'NB-statistics-history-chart' , className : 'NB-statistics-history-chart' } )
2010-07-26 22:21:58 -04:00
] )
] ) ;
2010-07-30 23:50:49 -04:00
return $stats ;
} ,
make _charts : function ( data ) {
var r = Raphael ( "NB-statistics-history-chart" , 325 , 220 ) ;
var lines = r . g . linechart ( 20 , 20 , 290 , 200 ,
[ [ 0 , 2 , 4 , 6 , 8 , 10 , 12 ] ,
[ 0 , 2 , 4 , 6 , 8 , 10 , 12 ] ] ,
[ [ 12 , 12 , 23 , 15 , 17 , 27 , 22 ] ,
[ 10 , 20 , 30 , 25 , 15 , 28 , 2 ] ] , {
nostroke : false ,
axis : false ,
symbol : "o" ,
smooth : true
} ) . hoverColumn ( function ( ) {
this . tags = r . set ( ) ;
for ( var i = 0 , ii = this . y . length ; i < ii ; i ++ ) {
this . tags . push ( r . g . tag ( this . x , this . y [ i ] , this . values [ i ] , 160 , 10 ) . insertBefore ( this ) . attr ( [ { fill : "#fff" } , { fill : this . symbols [ i ] . attr ( "fill" ) } ] ) ) ;
}
} , function ( ) {
this . tags && this . tags . remove ( ) ;
} ) ;
lines . symbols . attr ( { r : 3 } ) ;
// lines.lines[0].animate({"stroke-width": 6}, 1000);
// lines.symbols[0].attr({stroke: "#fff"});
// lines.symbols[0][1].animate({fill: "#f00"}, 1000);
2010-07-30 17:12:20 -04:00
} ,
handle _change : function ( elem , e ) {
var self = this ;
$ . targetIs ( e , { tagSelector : '.NB-modal-feed-chooser' } , function ( $t , $p ) {
var feed _id = $t . val ( ) ;
2010-07-30 23:50:49 -04:00
self . first _load = false ;
2010-07-30 17:12:20 -04:00
self . initialize _feed ( feed _id ) ;
self . get _stats ( ) ;
} ) ;
2010-07-25 23:13:27 -04:00
}
} ;