<commit_before>var utils = require('./utils');
var $ = require('jquery');
var _ = require('underscore');

// TODO: use self everywhere

function D3SVGRuleSet(renderer) {
	var self = this;
	self.rule_map = {};
	self.renderer = renderer;

	self.addRule = function(condition, d3_shape, attrs, z_index) {
		var rule_id = Object.keys(self.rule_map).length;
		if (z_index === undefined) {
			z_index = rule_id;
		}
		self.rule_map[rule_id] = new D3SVGRule(rule_id, self.renderer, condition, d3_shape, attrs, z_index);
		return rule_id;
	};

	self.removeRule = function(rule_id) {
		self.rule_map[rule_id] = null;
	}

	self.getRules = function() {
		// returns a list of lists of rules to render in the given order
		var z_map = {};
		_.each(self.rule_map, function(rule, rule_id) {
			z_map[rule.z_index] = z_map[rule.z_index] || [];
			z_map[rule.z_index].push(rule);
		});
		return _.map(Object.keys(z_map).sort(), 
			function(z) { return z_map[z];});
	};

	self.fromJSON = function(json_rules) {
		self.rule_map = {};
		_.each(json_rules, function(rule) {
			self.addRule(rule.condition, rule.d3_shape, rule.attrs, rule.z_index);
		});
	};
}

function D3SVGRule(rule_id, renderer, condition, d3_shape, attrs, z_index) {
	var self = this;
	self.rule_id = rule_id; 
	self.renderer = renderer;
	self.d3_shape = d3_shape; 
	self.z_index = z_index; 
	self.condition = condition; 
	self.attrs = attrs;

	var widthLike = ['width', 'x'];
	var heightLike = ['height', 'y'];

	self.filterData = function(d3_data) {
		return d3_data.filter(self.condition);
	};

	self.apply = function(d3_g_selection) {
		var elts = d3_g_selection.select(function() {
			return this.appendChild(d3_shape.node().cloneNode(true));
		});
		_.each(self.attrs, function(val, key) {
			elts.attr(key, function(d,i) {
				// handle percentages manually so its percent of the specified
				//	cell dimensions. This cannot be done automatically b/c
				//	this is naturally handled as percentage of the entire svg
				//	and <g>'s don't have dimension
				var currVal = val;
				if (typeof currVal === 'function') {
					currVal = currVal(d,i);
				}
				if (typeof currVal === 'string' && currVal.indexOf('%') > -1) {
					currVal = parseFloat(currVal)/100;
					if (widthLike.indexOf(key) > -1) {
						currVal = currVal*self.renderer.config.cell_width;	
					} else if (heightLike.indexOf(key) > -1) {
						currVal = currVal*self.renderer.config.cell_height;	
					} 
				}
				return currVal+'';
			});
		});
	};
}

function D3SVGCellRenderer(track) {
	var self = this;
	this.rule_set = new D3SVGRuleSet(self);
	this.track = track;
	this.config = this.track.config;
	this.data = this.track.data;
	this.cell_area;
	this.svg;
	this.g;

	self.data_key = function(d) {
		return d[self.config.id_member];
	};

	self.parseRuleSet = function(json_rules) {
		self.rule_set.fromJSON(json_rules);
	};

	self.addRule = function(condition, d3_shape, attrs, z_index) {
		var ret = self.rule_set.addRule(condition, d3_shape, attrs, z_index);
		self.updateCells();
		return ret;
	};

	self.removeRule = function(rule_id) {
		self.rule_set.removeRule(rule_id);
		self.updateCells();
	};

	self.renderCells = function(cell_area) {
		self.cell_area = cell_area;<commit_msg>last commit of day - changing more this to self and modifying removeRule and deleting a comment<commit_after>var utils = require('./utils');
var $ = require('jquery');
var _ = require('underscore');

function D3SVGRuleSet(renderer) {
	var self = this;
	self.rule_map = {};
	self.renderer = renderer;

	self.addRule = function(condition, d3_shape, attrs, z_index) {
		var rule_id = Object.keys(self.rule_map).length;
		if (z_index === undefined) {
			z_index = rule_id;
		}
		self.rule_map[rule_id] = new D3SVGRule(rule_id, self.renderer, condition, d3_shape, attrs, z_index);
		return rule_id;
	};

	self.removeRule = function(rule_id) {
		delete self.rule_map[rule_id];
	}

	self.getRules = function() {
		// returns a list of lists of rules to render in the given order
		var z_map = {};
		_.each(self.rule_map, function(rule, rule_id) {
			z_map[rule.z_index] = z_map[rule.z_index] || [];
			z_map[rule.z_index].push(rule);
		});
		return _.map(Object.keys(z_map).sort(), 
			function(z) { return z_map[z];});
	};

	self.fromJSON = function(json_rules) {
		self.rule_map = {};
		_.each(json_rules, function(rule) {
			self.addRule(rule.condition, rule.d3_shape, rule.attrs, rule.z_index);
		});
	};
}

function D3SVGRule(rule_id, renderer, condition, d3_shape, attrs, z_index) {
	var self = this;
	self.rule_id = rule_id; 
	self.renderer = renderer;
	self.d3_shape = d3_shape; 
	self.z_index = z_index; 
	self.condition = condition; 
	self.attrs = attrs;

	var widthLike = ['width', 'x'];
	var heightLike = ['height', 'y'];

	self.filterData = function(d3_data) {
		return d3_data.filter(self.condition);
	};

	self.apply = function(d3_g_selection) {
		var elts = d3_g_selection.select(function() {
			return this.appendChild(d3_shape.node().cloneNode(true));
		});
		_.each(self.attrs, function(val, key) {
			elts.attr(key, function(d,i) {
				// handle percentages manually so its percent of the specified
				//	cell dimensions. This cannot be done automatically b/c
				//	this is naturally handled as percentage of the entire svg
				//	and <g>'s don't have dimension
				var currVal = val;
				if (typeof currVal === 'function') {
					currVal = currVal(d,i);
				}
				if (typeof currVal === 'string' && currVal.indexOf('%') > -1) {
					currVal = parseFloat(currVal)/100;
					if (widthLike.indexOf(key) > -1) {
						currVal = currVal*self.renderer.config.cell_width;	
					} else if (heightLike.indexOf(key) > -1) {
						currVal = currVal*self.renderer.config.cell_height;	
					} 
				}
				return currVal+'';
			});
		});
	};
}

function D3SVGCellRenderer(track) {
	var self = this;
	self.rule_set = new D3SVGRuleSet(self);
	self.track = track;
	self.config = self.track.config;
	self.data = self.track.data;
	self.cell_area;
	self.svg;
	self.g;

	self.data_key = function(d) {
		return d[self.config.id_member];
	};

	self.parseRuleSet = function(json_rules) {
		self.rule_set.fromJSON(json_rules);
	};

	self.addRule = function(condition, d3_shape, attrs, z_index) {
		var ret = self.rule_set.addRule(condition, d3_shape, attrs, z_index);
		self.updateCells();
		return ret;
	};

	self.removeRule = function(rule_id) {
		self.rule_set.removeRule(rule_id);
		self.updateCells();
	};

	self.renderCells = function(cell_area) {
		self.cell_area = cell_area;
{'fraction_non_alphanumeric': 0.14736680012319064, 'fraction_numerical': 0.0073914382506929475, 'mean_word_length': 3.8146775389177168, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6340088', 'n_tokens_mistral': 2630, 'n_tokens_neox': 2416, 'n_words': 486}