2017-10-03 12:17:14 +02:00
|
|
|
/* 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/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const {createCollapser, createActionButtons} = require("./gui");
|
2019-04-08 00:02:29 +02:00
|
|
|
const extension = require("../lib/extension");
|
2017-10-03 12:17:14 +02:00
|
|
|
|
|
|
|
const actions = [];
|
|
|
|
const addAction = function addAction(action){
|
|
|
|
actions.push(action);
|
|
|
|
};
|
|
|
|
|
|
|
|
const addToContainer = function(){
|
|
|
|
const container = document.getElementById("prints");
|
2019-04-08 00:02:29 +02:00
|
|
|
container.querySelector("li").textContent = extension.getTranslation("pleaseWait");
|
2017-10-03 12:17:14 +02:00
|
|
|
var first = true;
|
|
|
|
|
|
|
|
return function addToContainer(domainNotification){
|
|
|
|
if (first){
|
|
|
|
container.innerHTML = "";
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
container.appendChild(domainNotification.node());
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
|
2019-04-30 23:42:21 +02:00
|
|
|
const DomainNotification = function DomainNotification(domain, messageId, count = 0, api = ""){
|
2018-09-11 23:54:59 +02:00
|
|
|
if (domain instanceof URL){
|
|
|
|
this.urls().add(domain.href);
|
|
|
|
domain = domain.hostname;
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
this.domain = domain;
|
|
|
|
this.messageId = messageId;
|
2018-09-11 23:54:59 +02:00
|
|
|
this.count = count;
|
2019-04-30 23:42:21 +02:00
|
|
|
this.api = api;
|
2018-08-27 22:09:51 +02:00
|
|
|
this.extraNotifications = 0;
|
2017-10-03 12:17:14 +02:00
|
|
|
addToContainer(this);
|
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
2018-09-11 23:54:59 +02:00
|
|
|
DomainNotification.prototype.urls = function urls(){
|
|
|
|
const urls = new Set();
|
|
|
|
this.urls = function(){
|
|
|
|
return urls;
|
|
|
|
};
|
|
|
|
return urls;
|
|
|
|
};
|
|
|
|
|
2017-10-03 12:17:14 +02:00
|
|
|
DomainNotification.prototype.notifications = function notifications(){
|
|
|
|
const notifications = [];
|
|
|
|
this.notifications = function(){
|
|
|
|
return notifications;
|
|
|
|
};
|
|
|
|
return notifications;
|
|
|
|
};
|
|
|
|
|
|
|
|
DomainNotification.prototype.addNotification = function addNotification(notification){
|
2018-08-27 22:09:51 +02:00
|
|
|
if (this.notifications().length > 250){
|
|
|
|
this.addMore();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.notifications().push(notification);
|
2018-09-11 23:54:59 +02:00
|
|
|
this.urls().add(notification.url.href);
|
2018-08-27 22:09:51 +02:00
|
|
|
this.notificationsNode().appendChild(notification.node());
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
this.update();
|
|
|
|
};
|
2018-08-27 22:09:51 +02:00
|
|
|
|
|
|
|
DomainNotification.prototype.addMore = function addMore(){
|
|
|
|
this.notificationsNode().appendChild(document.createTextNode("..."));
|
|
|
|
this.extraNotifications += 1;
|
|
|
|
this.addMore = function addMore(){
|
|
|
|
this.extraNotifications += 1;
|
|
|
|
};
|
|
|
|
};
|
2017-10-03 12:17:14 +02:00
|
|
|
|
|
|
|
// DOM node creation functions
|
|
|
|
|
|
|
|
DomainNotification.prototype.node = function node(){
|
|
|
|
const node = document.createElement("li");
|
2018-08-22 22:16:49 +02:00
|
|
|
node.className = "domainPrints collapsible collapsed";
|
2017-10-03 12:17:14 +02:00
|
|
|
node.appendChild(this.textNode());
|
2017-10-14 12:24:53 +02:00
|
|
|
node.appendChild(document.createElement("br"));
|
2017-10-03 12:17:14 +02:00
|
|
|
createCollapser(node);
|
|
|
|
node.appendChild(this.notificationsNode());
|
|
|
|
|
|
|
|
this.node = function(){
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
DomainNotification.prototype.update = function update(){
|
|
|
|
this.updateTextNode();
|
2018-09-11 23:54:59 +02:00
|
|
|
this.node().classList[this.notifications().length? "remove": "add"]("empty");
|
2017-10-03 12:17:14 +02:00
|
|
|
this.notifications().forEach(function(notification){
|
|
|
|
notification.update();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DomainNotification.prototype.textNode = function textNode(){
|
|
|
|
const node = document.createElement("span");
|
2017-10-14 12:24:53 +02:00
|
|
|
node.className = "text";
|
2017-10-03 12:17:14 +02:00
|
|
|
this.textNode = function(){
|
|
|
|
return node;
|
|
|
|
};
|
2019-04-08 00:02:29 +02:00
|
|
|
var messageParts = extension.getTranslation(this.messageId).split(/\{url\}/g);
|
2017-10-03 12:17:14 +02:00
|
|
|
node.appendChild(document.createTextNode(messageParts.shift()));
|
|
|
|
while (messageParts.length){
|
|
|
|
var urlSpan = document.createElement("span");
|
2019-06-25 23:22:55 +02:00
|
|
|
urlSpan.textContent = this.domain || extension.getTranslation("localFile");
|
2017-10-14 12:24:53 +02:00
|
|
|
urlSpan.className = "url hasHiddenActions";
|
|
|
|
urlSpan.appendChild(this.actionsNode());
|
2017-10-03 12:17:14 +02:00
|
|
|
node.appendChild(urlSpan);
|
|
|
|
node.appendChild(document.createTextNode(messageParts.shift()));
|
|
|
|
}
|
|
|
|
node.appendChild(document.createTextNode(" ("));
|
|
|
|
var countSpan = document.createElement("span");
|
|
|
|
countSpan.className = "count";
|
2017-10-10 13:38:55 +02:00
|
|
|
countSpan.textContent = "0";
|
2017-10-03 12:17:14 +02:00
|
|
|
node.appendChild(countSpan);
|
|
|
|
node.appendChild(document.createTextNode(") "));
|
|
|
|
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
DomainNotification.prototype.updateTextNode = function updateTextNode(){
|
|
|
|
const node = this.textNode();
|
|
|
|
const notifications = this.notifications();
|
2018-09-11 23:54:59 +02:00
|
|
|
const urls = Array.from(this.urls()).join("\n");
|
2018-08-27 22:09:51 +02:00
|
|
|
node.querySelectorAll(".url").forEach((urlSpan) => {
|
2018-09-11 23:54:59 +02:00
|
|
|
urlSpan.title = urls;
|
2017-10-03 12:17:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
node.title = notifications.map(function(notification){
|
|
|
|
return notification.timestamp + ": " + notification.functionName;
|
2018-09-11 23:54:59 +02:00
|
|
|
}).join("\n") + (this.extraNotifications? "\n...": "");
|
2017-10-03 12:17:14 +02:00
|
|
|
|
2018-08-27 22:09:51 +02:00
|
|
|
node.querySelectorAll(".count").forEach((countSpan) => {
|
2018-09-11 23:54:59 +02:00
|
|
|
if (this.count){
|
|
|
|
countSpan.textContent = this.count;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
countSpan.textContent = notifications.length + this.extraNotifications;
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
DomainNotification.prototype.actionsNode = function actionsNode(){
|
|
|
|
const node = document.createElement("div");
|
2017-10-14 12:24:53 +02:00
|
|
|
node.className = "actions";
|
2019-04-30 23:42:21 +02:00
|
|
|
createActionButtons(node, actions, {domain: this.domain, urls: this.urls(), api: this.api});
|
2017-10-03 12:17:14 +02:00
|
|
|
this.actionsNode = function(){
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
|
|
|
DomainNotification.prototype.notificationsNode = function notificationsNode(){
|
|
|
|
const node = document.createElement("ul");
|
|
|
|
node.className = "notifications collapsing";
|
|
|
|
this.notificationsNode = function(){
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
return node;
|
|
|
|
};
|
|
|
|
|
|
|
|
const domains = new Map();
|
2019-04-30 23:42:21 +02:00
|
|
|
const domainNotification = function(url, messageId, count = 0, api = ""){
|
2018-09-11 23:54:59 +02:00
|
|
|
const domain = url.hostname;
|
2017-10-03 12:17:14 +02:00
|
|
|
var domainNotification = domains.get(domain + messageId);
|
|
|
|
if (!domainNotification){
|
2019-04-30 23:42:21 +02:00
|
|
|
domainNotification = new DomainNotification(url, messageId, count, api);
|
2017-10-03 12:17:14 +02:00
|
|
|
domains.set(domain + messageId, domainNotification);
|
|
|
|
}
|
2018-09-11 23:54:59 +02:00
|
|
|
else {
|
|
|
|
domainNotification.count += count;
|
|
|
|
domainNotification.urls().add(url.href);
|
|
|
|
domainNotification.update();
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
return domainNotification;
|
|
|
|
};
|
|
|
|
domainNotification.addAction = addAction;
|
2019-04-08 00:02:29 +02:00
|
|
|
if ((typeof module) !== "undefined"){
|
|
|
|
module.exports = domainNotification;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
require.register("./domainNotification", domainNotification);
|
|
|
|
}
|
2017-10-03 12:17:14 +02:00
|
|
|
}());
|