lightbeam/data/lightbeam.js

179 строки
6.2 KiB
JavaScript
Исходник Постоянная ссылка Обычный вид История

/* This Source Code Form is subject to the terms of the Mozilla Public
* 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) {
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 = {};
var allConnections = [];
2014-04-08 04:26:29 +04:00
var g = global;
// 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;
const FROM_PRIVATE_MODE = 14;
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);
// 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]);
}
});
}
} 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) {
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";
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
});
2014-04-23 05:46:17 +04:00
function initCap(str) {
return str[0].toUpperCase() + str.slice(1);
}
2014-04-23 05:46:17 +04:00
global.switchVisualization = function switchVisualization(name) {
// var startTime = Date.now();
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-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");
}
/****************************************
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;
};
2013-07-31 00:56:22 +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;
};
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");
};
2013-10-17 07:14:13 +04:00
})(this);