voterunner

quick and dirty votes and discussions
git clone https://git.ce9e.org/voterunner.git

commit
582f5f7da88709949b9fe83ef2304096d0ddffcf
parent
1e4fe6b0e45c5dc80f0c824e78bbf5b251292619
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2020-10-16 15:46
refactor: use arrow functions

Diffstat

M static/src/voterunner.js 39 ++++++++++++---------------------------
M static/test/test.js 2 +-

2 files changed, 13 insertions, 28 deletions


diff --git a/static/src/voterunner.js b/static/src/voterunner.js

@@ -41,13 +41,10 @@ var throttle = function(fn, timeout) {
   41    41 
   42    42 var getVotes = function(nodes, node) {
   43    43 	if (!node.votes) {
   44    -1 		node.votes = 1 + nodes.filter(function(follower) {
   45    -1 			return follower.delegate === node.id;
   46    -1 		}).map(function(follower) {
   47    -1 			return getVotes(nodes, follower);
   48    -1 		}).reduce(function(a, b) {
   49    -1 			return a + b;
   50    -1 		}, 0);
   -1    44 		node.votes = 1 + nodes
   -1    45 			.filter(n => n.delegate === node.id)
   -1    46 			.map(n => getVotes(nodes, n))
   -1    47 			.reduce((sum, n) => sum + n, 0);
   51    48 	}
   52    49 
   53    50 	return node.votes;
@@ -56,9 +53,7 @@ var getVotes = function(nodes, node) {
   56    53 var getDelegationChain = function(nodes, node) {
   57    54 	if (!node.delegationChain) {
   58    55 		if (node.delegate) {
   59    -1 			var delegate = nodes.find(function(n) {
   60    -1 				return n.id === node.delegate;
   61    -1 			});
   -1    56 			var delegate = nodes.find(n => n.id === node.delegate);
   62    57 			var delegationChain = getDelegationChain(nodes, delegate);
   63    58 			node.delegationChain = [node.delegate].concat(delegationChain);
   64    59 		} else {
@@ -74,14 +69,10 @@ var getName = function(node) {
   74    69 };
   75    70 
   76    71 var tplFollowers = function(nodes, id, ID) {
   77    -1 	var _tplNode = function(node) {
   78    -1 		return tplNode(nodes, node, ID);
   79    -1 	};
   80    -1 	return nodes.filter(function(node) {
   81    -1 		return node.delegate === id;
   82    -1 	}).sort(function(a, b) {
   83    -1 		return getVotes(nodes, b) - getVotes(nodes, a);
   84    -1 	}).map(_tplNode);
   -1    72 	return nodes
   -1    73 		.filter(n => n.delegate === id)
   -1    74 		.sort((a, b) => getVotes(nodes, b) - getVotes(nodes, a))
   -1    75 		.map(n => tplNode(nodes, n, ID));
   85    76 };
   86    77 
   87    78 var tplNode = function(nodes, node, ID) {
@@ -185,9 +176,7 @@ document.addEventListener('DOMContentLoaded', function() {
  185   176 	var nodes = JSON.parse(document.querySelector('#json-nodes').dataset.value);
  186   177 
  187   178 	var getNode = function(id) {
  188    -1 		var node = nodes.find(function(node) {
  189    -1 			return node.id === id;
  190    -1 		});
   -1   179 		var node = nodes.find(n => n.id === id);
  191   180 		if (!node) {
  192   181 			node = {
  193   182 				id: id,
@@ -213,9 +202,7 @@ document.addEventListener('DOMContentLoaded', function() {
  213   202 		}
  214   203 	};
  215   204 
  216    -1 	var user = nodes.find(function(node) {
  217    -1 		return node.id === ID;
  218    -1 	});
   -1   205 	var user = nodes.find(n => n.id === ID);
  219   206 	if (user) {
  220   207 		document.querySelector('.user__name input').value = user.name;
  221   208 		document.querySelector('.user__comment textarea').value = user.comment;
@@ -277,9 +264,7 @@ document.addEventListener('DOMContentLoaded', function() {
  277   264 
  278   265 	var pushComment = throttle(function() {
  279   266 		var comment = document.querySelector('.user__comment textarea').value;
  280    -1 		var node = nodes.find(function(n) {
  281    -1 			return n.id === ID;
  282    -1 		});
   -1   267 		var node = nodes.find(n => n.id === ID);
  283   268 		// Do not create a new node if the comment is empty.
  284   269 		// This can happen e.g. on a keydown event from the ctrl or shift keys.
  285   270 		if (node || comment) {

diff --git a/static/test/test.js b/static/test/test.js

@@ -179,7 +179,7 @@ describe('remove', function() {
  179   179 		setUp('/test' + test + '/' + ID, function(b) {
  180   180 			browser = b;
  181   181 			d = browser.contentDocument;
  182    -1 			browser.contentWindow.confirm = function() {return true};
   -1   182 			browser.contentWindow.confirm = () => true;
  183   183 
  184   184 			// create something to delete
  185   185 			userName = d.querySelector('.user__name input');