<commit_before>/*global m:false */
var mc = mc || {};

mc.Datatable = {
	controller: function (cols, config) {
		this.cols = cols;
		this.config = config = config || {};


		if (config.url) {
			this.data = m.request({
				url: config.url,
				method: 'GET'
			});
		}
		if (config.data) {
			this.data = (typeof config.data == 'function' ? config.data : m.prop(config.data));
		}

		this.sort = function (target) {
			var key = target.parentNode.getAttribute('data-colkey'),
				col = this.activeCols[key];
			if (this.lastSorted && this.lastSorted != key) {
				this.activeCols[this.lastSorted]._sorted = 'none';
			}
			var reverse = (col._sorted == 'asc' ? -1 : 1);
			this.data(this.data().sort(function (a, b) {
				a = a[key];
				b = b[key];
				return (a == b ? 0 : (a < b ? -1 : 1) * reverse);
			}));
			col._sorted = (reverse > 0 ? 'asc' : 'desc');
			this.lastSorted = key;
			while (target.nodeName != 'TABLE') target = target.parentNode;
			//target.innerHTML = '';
			m.render(target, mc.Datatable.contentsView(this));
		};

		this.onCellClick = function (target) {
			while (target.nodeName != 'TD' && target.nodeName != 'TABLE') target = target.parentNode;
			if (target.nodeName == 'TABLE') return;
			var colIndex = target.cellIndex,
				col = this.dataRow[colIndex],
				rowIndex = target.parentNode.sectionRowIndex,
				row = this.data()[rowIndex];
			return this.config.onclick.call(this, row[col.key], row, col);
		};

		this.onclick = function (e) {
			var target = e.target;
			if (target.nodeName == 'I') return this.sort(target);
			if (typeof this.config.onclick == 'function') {
				m.startComputation();
				var ret = this.onCellClick(target);
				m.endComputation();
				return ret;
			}
		}.bind(this);

		this.setWidth = function (attrs, width) {
			if (!width) return;
			if (/^\d+$/.test(width)) width += 'px';
			if (!attrs.style) attrs.style = '';
			if (width) attrs.style += 'width:' + width + ';';
		};
	},
	view: function (ctrl, options) {
		var cols = ctrl.cols;
		ctrl.viewOptions = options;

		if (!ctrl.data()) {
			return m('div', 'Sorry, no data to display');
		}
		options = options || {};
		options.classNames = options.classNames || {};

		var attrs = {
			class: options.classNames.table || 'datatable',
			// onclick: ctrl.onclick
			config: function (el, isOld) {
				if (isOld) return;
				el.addEventListener('click', ctrl.onclick);
			}

		};

		ctrl.setWidth(attrs, options.width);

		return m(
			'table',
			attrs,
			this.contentsView(ctrl)
		);
	
		 
	},
	contentsView: function (ctrl) {
		var cols = ctrl.cols,
			options = ctrl.viewOptions;
		return [
				this.headView(ctrl, cols, options),<commit_msg>Avoid rendering outside the table when not needed<commit_after>/*global m:false */
var mc = mc || {};

mc.Datatable = {
	controller: function (cols, config) {
		this.cols = cols;
		this.config = config = config || {};


		if (config.url) {
			this.data = m.request({
				url: config.url,
				method: 'GET'
			});
		}
		if (config.data) {
			this.data = (typeof config.data == 'function' ? config.data : m.prop(config.data));
		}

		this.sort = function (target) {
			var key = target.parentNode.getAttribute('data-colkey'),
				col = this.activeCols[key];
			if (this.lastSorted && this.lastSorted != key) {
				this.activeCols[this.lastSorted]._sorted = 'none';
			}
			var reverse = (col._sorted == 'asc' ? -1 : 1);
			this.data(this.data().sort(function (a, b) {
				a = a[key];
				b = b[key];
				return (a == b ? 0 : (a < b ? -1 : 1) * reverse);
			}));
			col._sorted = (reverse > 0 ? 'asc' : 'desc');
			this.lastSorted = key;
			m.render(this._tableEl, mc.Datatable.contentsView(this));
		};

		this.onCellClick = function (target) {
			while (target.nodeName != 'TD' && target.nodeName != 'TABLE') target = target.parentNode;
			if (target.nodeName == 'TABLE') return;
			var colIndex = target.cellIndex,
				col = this.dataRow[colIndex],
				rowIndex = target.parentNode.sectionRowIndex,
				row = this.data()[rowIndex];
			return this.config.onclick.call(this, row[col.key], row, col);
		};

		this.onclick = function (e) {
			var target = e.target;
			if (target.nodeName == 'I') return this.sort(target);
			if (typeof this.config.onclick == 'function') {
				m.startComputation();
				var ret = this.onCellClick(target);
				m.endComputation();
				return ret;
			}
		}.bind(this);

		this.setWidth = function (attrs, width) {
			if (!width) return;
			if (/^\d+$/.test(width)) width += 'px';
			if (!attrs.style) attrs.style = '';
			if (width) attrs.style += 'width:' + width + ';';
		};
	},
	view: function (ctrl, options) {
		var cols = ctrl.cols;
		ctrl.viewOptions = options;

		if (!ctrl.data()) {
			return m('div', 'Sorry, no data to display');
		}
		options = options || {};
		options.classNames = options.classNames || {};

		var attrs = {
			class: options.classNames.table || 'datatable',
			// onclick: ctrl.onclick
			config: function (el, isOld) {
				if (isOld) return;
				el.addEventListener('click', ctrl.onclick);
				ctrl._tableEl = el;
				m.render(el,  mc.Datatable.contentsView(ctrl));
			}

		};

		ctrl.setWidth(attrs, options.width);

		return m(
			'table',
			attrs
		);
	
		 
	},
	contentsView: function (ctrl) {
		var cols = ctrl.cols,
			options = ctrl.viewOptions;
		return [
				this.headView(ctrl, cols, options),
{'fraction_non_alphanumeric': 0.1778277996623523, 'fraction_numerical': 0.0022509848058525606, 'mean_word_length': 3.036336109008327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4648196', 'n_tokens_mistral': 2195, 'n_tokens_neox': 1872, 'n_words': 421}