2014-04-30 10:45:12 +04:00
/ * T h i s S o u r c e C o d e F o r m i s s u b j e c t t o t h e t e r m s o f t h e M o z i l l a P u b l i c
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http : //mozilla.org/MPL/2.0/. */
2014-04-23 05:46:17 +04:00
( function ( global ) {
2014-03-29 03:40:25 +04:00
2013-07-16 01:31:38 +04:00
'use strict' ;
2013-02-07 02:45:47 +04:00
var visualizations = { } ;
2013-07-17 21:18:14 +04:00
var currentFilter ;
2014-03-29 01:52:15 +04:00
var userSettings = { } ;
2014-03-29 03:40:25 +04:00
var allConnections = [ ] ;
2014-04-08 04:26:29 +04:00
var g = global ;
2013-06-04 04:04:21 +04:00
// Constants for indexes of properties in array format
const SOURCE = 0 ;
const TARGET = 1 ;
const TIMESTAMP = 2 ;
const CONTENT _TYPE = 3 ;
const COOKIE = 4 ;
const SOURCE _VISITED = 5 ;
const SECURE = 6 ;
const SOURCE _PATH _DEPTH = 7 ;
const SOURCE _QUERY _DEPTH = 8 ;
const SOURCE _SUB = 9 ;
const TARGET _SUB = 10 ;
const METHOD = 11 ;
const STATUS = 12 ;
const CACHEABLE = 13 ;
2013-06-14 02:40:22 +04:00
const FROM _PRIVATE _MODE = 14 ;
2013-05-29 21:30:16 +04:00
2013-07-11 00:42:06 +04:00
var vizcanvas = document . querySelector ( '.vizcanvas' ) ;
2013-07-10 09:22:53 +04:00
var mapDocument , mapcanvas ;
2014-04-23 05:46:17 +04:00
document . querySelector ( '.world-map' ) . addEventListener ( 'load' , function ( event ) {
2013-07-10 09:22:53 +04:00
mapDocument = event . target . contentDocument ;
mapcanvas = mapDocument . querySelector ( '.mapcanvas' ) ;
2014-04-08 04:26:29 +04:00
initMap ( mapcanvas , mapDocument ) ;
2013-07-10 09:22:53 +04:00
} , false ) ;
2014-03-29 03:40:25 +04:00
// Export everything
global . visualizations = visualizations ;
global . currentFilter = currentFilter ;
global . userSettings = userSettings ;
global . vizcanvas = vizcanvas ;
global . allConnections = allConnections ;
2013-07-10 09:22:53 +04:00
2013-07-03 03:27:10 +04:00
// DOM Utility
2014-04-23 05:46:17 +04:00
global . elem = function elem ( name , attributes , children ) {
// name is the tagName of an element
// [optional] attributes can be null or undefined, or an object of key/values to setAttribute on, attribute values can be functions to call to get the actual value
// [optional] children can be an element, text or an array (or null or undefined). If an array, can contain strings or elements
var e = document . createElement ( name ) ;
var val ;
if ( attributes && ( Array . isArray ( attributes ) || attributes . nodeName || typeof attributes === 'string' ) ) {
children = attributes ;
attributes = null ;
}
try {
if ( attributes ) {
Object . keys ( attributes ) . forEach ( function ( key ) {
if ( attributes [ key ] === null || attributes [ key ] === undefined ) return ;
if ( typeof attributes [ key ] === 'function' ) {
val = attributes [ key ] ( key , attributes ) ;
if ( val ) {
e . setAttribute ( key , val ) ;
}
} else {
e . setAttribute ( key , attributes [ key ] ) ;
}
} ) ;
}
2014-04-25 03:48:33 +04:00
} catch ( err ) {
2014-04-23 05:46:17 +04:00
console . log ( 'attributes: not what we think they are: %o' , attributes ) ;
}
if ( children ) {
if ( ! Array . isArray ( children ) ) {
children = [ children ] ; // convenience, allow a single argument vs. an array of one
2013-07-03 03:27:10 +04:00
}
2014-04-23 05:46:17 +04:00
children . forEach ( function ( child ) {
if ( child . nodeName ) {
e . appendChild ( child ) ;
} else {
// assumes child is a string
e . appendChild ( document . createTextNode ( child ) ) ;
}
} ) ;
}
return e ;
2013-07-03 03:27:10 +04:00
} ;
2014-04-23 05:46:17 +04:00
window . addEventListener ( 'load' , function ( evt ) {
2014-04-24 08:20:44 +04:00
console . debug ( 'window onload' ) ;
2014-04-23 05:46:17 +04:00
self . port . emit ( 'uiready' ) ;
// Wire up events
document . querySelector ( '[data-value=Graph]' ) . setAttribute ( "data-selected" , true ) ;
var visualizationName = "graph" ;
2014-04-24 08:20:44 +04:00
console . debug ( "current vis" , visualizationName ) ;
2014-04-23 05:46:17 +04:00
g . currentVisualization = visualizations [ visualizationName ] ;
switchVisualization ( visualizationName ) ;
2013-02-07 02:45:47 +04:00
} ) ;
2013-03-14 00:30:09 +04:00
2014-04-23 05:46:17 +04:00
function initCap ( str ) {
return str [ 0 ] . toUpperCase ( ) + str . slice ( 1 ) ;
2013-04-06 02:48:41 +04:00
}
2014-04-23 05:46:17 +04:00
global . switchVisualization = function switchVisualization ( name ) {
// var startTime = Date.now();
2014-04-24 08:20:44 +04:00
console . debug ( 'switchVisualizations(' + name + ')' ) ;
2014-04-23 05:46:17 +04:00
if ( g . currentVisualization != visualizations [ name ] ) {
g . currentVisualization . emit ( 'remove' ) ;
}
g . currentVisualization = visualizations [ name ] ;
resetAdditionalUI ( ) ;
g . currentVisualization . emit ( 'init' ) ;
self . port . emit ( "prefChanged" , {
defaultVisualization : name
} ) ;
// console.log('it took %s ms to switch visualizations', Date.now() - startTime);
2014-04-25 03:48:33 +04:00
} ;
2013-06-17 22:46:59 +04:00
2014-04-23 05:46:17 +04:00
function resetAdditionalUI ( ) {
// toggle off info panel
document . querySelector ( "#content" ) . classList . remove ( "showinfo" ) ;
var activeTab = document . querySelector ( ".info-panel-controls ul li.active" ) ;
if ( activeTab ) { // make the active tab inactive, if any
activeTab . classList . remove ( "active" ) ;
activeTab . querySelector ( "img" ) . classList . remove ( "hidden" ) ;
activeTab . querySelector ( "i" ) . classList . add ( "hidden" ) ;
}
// hide all help sections
document . querySelector ( ".help-content .graph-view-help" ) . classList . add ( "hidden" ) ;
document . querySelector ( ".help-content .list-view-help" ) . classList . add ( "hidden" ) ;
// show vizcanvas again in case it is hidden
document . querySelector ( ".vizcanvas" ) . classList . remove ( "hide" ) ;
// toggle footer section accordingly
document . querySelector ( ".graph-footer" ) . classList . add ( "hidden" ) ;
document . querySelector ( ".list-footer" ) . classList . add ( "hidden" ) ;
var vizName = g . currentVisualization . name ;
document . querySelector ( "." + vizName + "-footer" ) . classList . remove ( "hidden" ) ;
2013-03-14 00:30:09 +04:00
}
2013-05-31 01:10:53 +04:00
2013-07-13 00:34:32 +04:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2014-04-23 05:46:17 +04:00
* Format date string
* /
global . formattedDate = function formattedDate ( date , format ) {
var d = ( typeof date == "number" ) ? new Date ( date ) : date ;
var month = [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "June" , "July" , "Aug" , "Sept" , "Oct" , "Nov" , "Dec" ] [ d . getMonth ( ) ] ;
var formatted = month + " " + d . getDate ( ) + ", " + d . getFullYear ( ) ;
if ( format == "long" ) {
var dayInWeek = [ "Sun" , "Mon" , "Tue" , "Wed" , "Thu" , "Fri" , "Sat" ] [ d . getDay ( ) ] ;
formatted = dayInWeek + ", " + formatted + " " + ( ( d . getHours ( ) == 12 ) ? 12 : ( d . getHours ( ) % 12 ) ) + ':' + d . toLocaleFormat ( '%M' ) + [ 'AM' , 'PM' ] [ Math . floor ( d . getHours ( ) / 12 ) ] ;
}
return formatted ;
2014-04-25 03:48:33 +04:00
} ;
2013-07-31 00:56:22 +04:00
2013-07-13 00:34:32 +04:00
2014-04-23 05:46:17 +04:00
global . singularOrPluralNoun = function singularOrPluralNoun ( num , str ) {
if ( typeof num != "number" ) {
num = parseFloat ( num ) ;
}
return ( num !== 1 ) ? str + "s" : str ;
2014-04-25 03:48:33 +04:00
} ;
2014-03-29 03:40:25 +04:00
2013-07-31 00:56:22 +04:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
2014-04-23 05:46:17 +04:00
* update Stats Bar
* /
global . updateStatsBar = function updateStatsBar ( ) {
var dateSince = "just now" ;
if ( global . allConnections . length > 0 ) {
dateSince = formattedDate ( global . allConnections [ 0 ] [ 2 ] ) ;
}
document . querySelector ( ".top-bar .date-gathered" ) . textContent = dateSince ;
document . querySelector ( ".top-bar .third-party-sites" ) . textContent = aggregate . trackerCount + " " + singularOrPluralNoun ( aggregate . trackerCount , "THIRD PARTY SITE" ) ;
document . querySelector ( ".top-bar .first-party-sites" ) . textContent = aggregate . siteCount + " " + singularOrPluralNoun ( aggregate . siteCount , "SITE" ) ;
2014-04-25 03:48:33 +04:00
} ;
2013-10-17 07:14:13 +04:00
2014-03-29 03:40:25 +04:00
} ) ( this ) ;