<commit_before>import View from 'core/view/view';
import Command from 'core/infrastructure/command';
import * as columnService from 'core/column/column.service';
import * as sortService from 'core/sort/sort.service';
import behaviorFactory from './behaviors/highlight.behavior.factory';
import {noop} from 'core/services/utility';
import {GRID_PREFIX} from 'core/definition';

export default class HighlightView extends View {
	constructor(model, markup, apply) {
		super(model);
		this.model = model;
		this.markup = markup;
		this.apply = apply;

		let blur = noop;
		this.column = new Command({
			execute: (column, state) => {
				if (state) {
					blur = this.highlight(column.key, 'highlighted');
				}
				else {
					blur();
					blur = noop;
				}
			}
		});

		this.onInit(this.model);
	}

	onInit(model) {
		let blurs = [];
		model.sortChanged.watch(e => {
			if (e.hasChanges('by')) {
				blurs = this.invalidate(blurs);
			}
			this.behavior = behaviorFactory(this.model, this.markup);
		});

		model.viewChanged.watch(() => {
			blurs = this.invalidate(blurs);
			this.behavior = behaviorFactory(this.model, this.markup);
		});

		model.selectionChanged.watch(e => {
			if (e.hasChanges('unit') || e.hasChanges('mode')) {
				if (this.behavior) {
					this.behavior.destroy();
				}

				this.behavior = behaviorFactory(this.model, this.markup);
			}
			if (this.behavior) {
				const items = model.selection().items;
				this.apply(() => this.behavior.apply(items), 0);
			}
		});
	}

	invalidate(dispose) {
		dispose.forEach(f => f());

		dispose = [];
		const sortBy = this.model.sort().by;
		for (let entry of sortBy) {
			const key = sortService.key(entry);
			dispose.push(this.highlight(key, 'sorted'));
		}

		return dispose;
	}


	columnIndex(key) {
		const columnRows = this.model.view().columns;
		const columns = columnService.lineView(columnRows).map(v => v.model);
		const index = columnService.findIndex(columns, key);
		if (index >= 0) {
			// TODO: add pivot col support
			const column = columns[index];
			if (column.type === 'pivot' || column.type === 'pad') {
				return -1;
			}
		}

		return index;
	}

	highlight(key, cls) {
		const index = this.columnIndex(key);
		if (index < 0) {
			return noop;
		}

		const head = this.markup.head;
		if (head && head.rows.length) {
			for (let row of head.rows) {
				row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
				if (index > 0) {
					row.cells[index - 1].classList.add(`${GRID_PREFIX}-${cls}-prev`);
				}

				if (index < head.rows.length - 1) {
					row.cells[index + 1].classList.add(`${GRID_PREFIX}-${cls}-next`);
				}
			}
		}

		const body = this.markup.body;
		if (body) {
			for (let row of body.rows) {
				row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
			}
		}

		const foot = this.markup.foot;
		if (foot) {
			for (let row of foot.rows) {
				row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
			}
		}

		return () => this.blur(key, cls);
	}

	blur(key, cls) {
		const index = this.columnIndex(key);
		if (index < 0) {
			return noop;
		}

		const head = this.markup.head;
		if (head && head.rows.length) {
			for (let row of head.rows) {
				row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
				if (index > 0) {
					row.cells[index - 1].classList.remove(`${GRID_PREFIX}-${cls}-prev`);
				}

				if (index < head.rows.length - 1) {
					row.cells[index + 1].classList.remove(`${GRID_PREFIX}-${cls}-next`);
				}
			}
		}

		const body = this.markup.body;
		if (body) {
			for (let row of body.rows) {
				row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
			}
		}

		const foot = this.markup.foot;
		if (foot) {
			for (let row of foot.rows) {
				row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
			}
		}
	}
}<commit_msg>Dont need to check on markup cause view.core use onlink now<commit_after>import View from 'core/view/view';
import Command from 'core/infrastructure/command';
import * as columnService from 'core/column/column.service';
import * as sortService from 'core/sort/sort.service';
import behaviorFactory from './behaviors/highlight.behavior.factory';
import {noop} from 'core/services/utility';
import {GRID_PREFIX} from 'core/definition';

export default class HighlightView extends View {
	constructor(model, markup, apply) {
		super(model);
		this.model = model;
		this.markup = markup;
		this.apply = apply;

		let blur = noop;
		this.column = new Command({
			execute: (column, state) => {
				if (state) {
					blur = this.highlight(column.key, 'highlighted');
				}
				else {
					blur();
					blur = noop;
				}
			}
		});

		this.onInit(this.model);
	}

	onInit(model) {
		let blurs = [];
		model.sortChanged.watch(e => {
			if (e.hasChanges('by')) {
				blurs = this.invalidate(blurs);
			}
			this.behavior = behaviorFactory(this.model, this.markup);
		});

		model.viewChanged.watch(() => {
			blurs = this.invalidate(blurs);
			this.behavior = behaviorFactory(this.model, this.markup);
		});

		model.selectionChanged.watch(e => {
			if (e.hasChanges('unit') || e.hasChanges('mode')) {
				if (this.behavior) {
					this.behavior.destroy();
				}

				this.behavior = behaviorFactory(this.model, this.markup);
			}
			if (this.behavior) {
				const items = model.selection().items;
				this.apply(() => this.behavior.apply(items), 0);
			}
		});
	}

	invalidate(dispose) {
		dispose.forEach(f => f());

		dispose = [];
		const sortBy = this.model.sort().by;
		for (let entry of sortBy) {
			const key = sortService.key(entry);
			dispose.push(this.highlight(key, 'sorted'));
		}

		return dispose;
	}


	columnIndex(key) {
		const columnRows = this.model.view().columns;
		const columns = columnService.lineView(columnRows).map(v => v.model);
		const index = columnService.findIndex(columns, key);
		if (index >= 0) {
			// TODO: add pivot col support
			const column = columns[index];
			if (column.type === 'pivot' || column.type === 'pad') {
				return -1;
			}
		}

		return index;
	}

	highlight(key, cls) {
		const index = this.columnIndex(key);
		if (index < 0) {
			return noop;
		}

		const head = this.markup.head;
		if (head.rows.length) {
			for (let row of head.rows) {
				row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
				if (index > 0) {
					row.cells[index - 1].classList.add(`${GRID_PREFIX}-${cls}-prev`);
				}

				if (index < head.rows.length - 1) {
					row.cells[index + 1].classList.add(`${GRID_PREFIX}-${cls}-next`);
				}
			}
		}

		const body = this.markup.body;
		for (let row of body.rows) {
			row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
		}

		const foot = this.markup.foot;
		for (let row of foot.rows) {
			row.cells[index].classList.add(`${GRID_PREFIX}-${cls}`);
		}

		return () => this.blur(key, cls);
	}

	blur(key, cls) {
		const index = this.columnIndex(key);
		if (index < 0) {
			return noop;
		}

		const head = this.markup.head;
		if (head.rows.length) {
			for (let row of head.rows) {
				row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
				if (index > 0) {
					row.cells[index - 1].classList.remove(`${GRID_PREFIX}-${cls}-prev`);
				}

				if (index < head.rows.length - 1) {
					row.cells[index + 1].classList.remove(`${GRID_PREFIX}-${cls}-next`);
				}
			}
		}

		const body = this.markup.body;
		for (let row of body.rows) {
			row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
		}

		const foot = this.markup.foot;
		for (let row of foot.rows) {
			row.cells[index].classList.remove(`${GRID_PREFIX}-${cls}`);
		}
	}
}
{'fraction_non_alphanumeric': 0.17754395311667553, 'fraction_numerical': 0.0034629728289824187, 'mean_word_length': 3.1349118942731278, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 25, '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': '1349778', 'n_tokens_mistral': 3198, 'n_tokens_neox': 2683, 'n_words': 528}