<commit_before>/*global module, require*/
var _ = require('underscore');
module.exports = function MultiRootLayout() {
	'use strict';
	var self = this,
		mergeNodes = function (rootLayout, dx, rootIdea) {
			_.each(rootLayout, function (node) {
				node.x = node.x + dx;
				node.rootId = rootIdea.id;
				node.y = node.y + ((rootIdea.attr && rootIdea.attr.position && rootIdea.attr.position[1]) || 0);
			});
		},
		toStoredLayout = function (rootLayout, rootIdea) {
			var dimensions = {
				minX: 0,
				maxX: 0,
				minY: 0,
				maxY: 0,
				rootIdea: rootIdea,
				rootLayout: rootLayout
			};
			_.each(rootLayout, function (node) {
				dimensions.minX = Math.min(dimensions.minX, node.x);
				dimensions.maxX = Math.max(dimensions.maxX, node.x + node.width);
				dimensions.minY = Math.min(dimensions.minY, node.y);
				dimensions.maxY = Math.max(dimensions.maxY, node.y + node.width);
			});
			dimensions.height = dimensions.maxY - dimensions.minY;
			dimensions.width = dimensions.maxX - dimensions.minX;
			return dimensions;
		},
		calcTotalWidth = function (margin) {
			var totalWidth = 0;
			combinedLayout.forEach(function (storedRootLayout) {
				totalWidth = totalWidth + storedRootLayout.width;
			});
			totalWidth = totalWidth + (margin * (combinedLayout.length - 1));
			return totalWidth;
		},
		combinedLayout = [];

	self.appendRootNodeLayout = function (rootLayout, rootIdea) {
		combinedLayout.push(toStoredLayout(rootLayout, rootIdea));
	};
	self.getCombinedLayout = function (margin) {
		var totalWidth, xOffset,
			result = {};
		if (!margin) {
			throw 'invalid-args';
		}
		totalWidth = calcTotalWidth(margin);
		xOffset = totalWidth * -0.5;
		combinedLayout.forEach(function (storedRootLayout) {
			var dx = xOffset - storedRootLayout.minX;
			mergeNodes(storedRootLayout.rootLayout, dx, storedRootLayout.rootIdea);
			result = _.extend(result, storedRootLayout.rootLayout);
			xOffset = xOffset + storedRootLayout.width + margin;
		});
		return result;
	};
};<commit_msg>starting to work on positioning<commit_after>/*global module, require*/
var _ = require('underscore');
module.exports = function MultiRootLayout() {
	'use strict';
	var self = this,
		mergeNodes = function (storedLayout, dx, dy) {
			_.each(storedLayout.rootLayout, function (node) {
				node.x = node.x + dx;
				node.y = node.y + dy;
				node.rootId = storedLayout.rootIdea.id;
			});
		},
		globalIdeaTopLeftPosition = function (idea) {
			var positionArray = (idea && idea.attr && idea.attr.position) || [0, 0, 0];
			return {
				x: positionArray[0],
				y: positionArray[1],
				priority: positionArray[2]
			};
		},
		toStoredLayout = function (rootLayout, rootIdea) {
			var dimensions = {
				minX: 0,
				maxX: 0,
				minY: 0,
				maxY: 0,
				rootIdea: rootIdea,
				rootNode: rootLayout[rootIdea.id],
				rootLayout: rootLayout
			};
			_.each(rootLayout, function (node) {
				dimensions.minX = Math.min(dimensions.minX, node.x);
				dimensions.maxX = Math.max(dimensions.maxX, node.x + node.width);
				dimensions.minY = Math.min(dimensions.minY, node.y);
				dimensions.maxY = Math.max(dimensions.maxY, node.y + node.width);
			});
			dimensions.height = dimensions.maxY - dimensions.minY;
			dimensions.width = dimensions.maxX - dimensions.minX;
			return dimensions;
		},
		isPositioned = function (rootIdea) {
			return globalIdeaTopLeftPosition(rootIdea).priority;
		},
		calcDesiredRootNodeCenter = function (storedLayout) {
			var rootPosition = globalIdeaTopLeftPosition(storedLayout.rootIdea);
			return {
				x: (rootPosition.x + storedLayout.rootNode.width / 2),
				y: (rootPosition.y + storedLayout.rootNode.height / 2),
			};
		},
		positionedLayouts = [],
		unpositionedLayouts = [];

	self.appendRootNodeLayout = function (rootLayout, rootIdea) {
		var storedLayout = toStoredLayout(rootLayout, rootIdea);
		if (isPositioned(rootIdea)) {
			positionedLayouts.push(storedLayout);
		} else {
			unpositionedLayouts.push(storedLayout);
		}
	};
	self.getCombinedLayout = function (margin) {
		var result = {},
			mostRecentlyPositioned = positionedLayouts.length && _.max(positionedLayouts, function (layout) {
				return globalIdeaTopLeftPosition(layout.rootIdea).priority;
			}),
			desiredRootCenter = mostRecentlyPositioned && calcDesiredRootNodeCenter(mostRecentlyPositioned),
			rootDistance = function (storedLayout) {
				var rootCenter = calcDesiredRootNodeCenter(storedLayout);
				return Math.pow(rootCenter.x - desiredRootCenter.x, 2) + Math.pow(rootCenter.y - desiredRootCenter.y, 2);
			},
			sortedPositionedLayouts = _.sortBy(positionedLayouts, rootDistance),
			positionLayout = function (storedLayout, desiredPosition) {
				if (!storedLayout) {
					return;
				}
				desiredPostion = desiredPosition ||  globalIdeaTopLeftPosition(storedLayout.rootIdea);
				mergeNodes(storedLayout,
					desiredPosition.x - storedLayout.rootNode.x,
					desiredPosition.y - storedLayout.rootNode.y
				);
			};

		positionLayout(mostRecentlyPositioned);
		sortedPositionedLayouts.forEach(function (storedLayout) {
			positionLayout(storedLayout);
		});


		if (!margin) {
			throw 'invalid-args';
		}
		unpositionedLayouts.forEach(function (storedRootLayout) {
		});
		return result;
	};
};
{'fraction_non_alphanumeric': 0.1276555023923445, 'fraction_numerical': 0.004593301435406699, 'mean_word_length': 3.92090395480226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1427728', 'n_tokens_mistral': 2047, 'n_tokens_neox': 1716, 'n_words': 323}