File size: 2,609 Bytes
5b4fd78
8f5120e
2b6ff0c
 
 
 
 
 
 
 
5b4fd78
 
 
 
 
001aa75
 
 
 
 
 
c6642be
 
001aa75
 
5b4fd78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4685f5
 
 
 
 
 
5b4fd78
 
 
 
 
 
 
8e6724d
2b6ff0c
5b4fd78
 
 
 
 
 
 
 
 
 
 
 
 
4bd86de
 
2b6ff0c
5b4fd78
 
 
001aa75
 
 
 
 
b3d6894
c6642be
 
 
5b4fd78
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

const ENDPOINT = "/static-proxy?url=https%3A%2F%2Fcoref.huggingface.co%2Fcoref%26quot%3B%3C%2Fspan%3E%3B
const DEFAULT_NLP_TEXT = () => {
	const items = [
		`I love my father and my mother. They work hard. She is always nice but he is sometimes rude.`,
		`My sister is swimming with her classmates. They are not bad, but she is better. I love watching her swim.`,
		`My mother's name is Sasha, she likes dogs.`
	];
	return items[Math.floor(Math.random()*items.length)];
}

const loading = () => {
	document.body.classList.toggle('loading');
};

const toggleDebug = () => {
	document.body.classList.toggle('debug');
	const icons = document.querySelectorAll('.svg-checkbox');
	(<any>icons).forEach((icon) => {
		icon.classList.toggle('hide');
	});
	/// local storage
	window.localStorage.setItem('debug', document.body.classList.contains('debug').toString());
};

const coref = new Coref(ENDPOINT, {
	onStart: loading,
	onSuccess: loading,
});

const getQueryVar = (key: string) => {
	const query = window.location.search.substring(1);
	const params = query.split('&').map(param => param.split('='));
	for (const param of params) {
		if (param[0] === key) { return decodeURIComponent(param[1]); }
	}
	return undefined;
}

const updateURL = (text) => {
	history.pushState({ text: text }, "", `?text=${encodeURIComponent(text)}`);
}

document.addEventListener('DOMContentLoaded', () => {
	const $input        = document.querySelector('input.input-message') as HTMLInputElement;
	const $form         = document.querySelector('form.js-form') as HTMLFormElement;
	const $checkbox     = document.querySelector('.js-checkbox') as HTMLElement;
	const $svgContainer = document.querySelector('.svg-container') as SVGSVGElement;
	coref.container     = document.querySelector('.container') as HTMLElement;
	coref.svgContainer  = $svgContainer;
	
	{
		// Initial text
		const queryText = getQueryVar('text');
		if (queryText) {
			$input.value = queryText;
			coref.parse(queryText);
		} else {
			coref.parse(DEFAULT_NLP_TEXT());  // Trigger run with default text.
		}
	}
	
	$input.addEventListener('keydown', (evt) => {
		if (evt.charCode === 13) {
			// 13 is the Enter key
			evt.preventDefault();
			$form.submit();
		}
	});
	
	$form.addEventListener('submit', (evt) => {
		evt.preventDefault();
		const text = ($input.value.length > 0)
			? $input.value
			: DEFAULT_NLP_TEXT();
		updateURL(text);
		coref.parse(text);
	});
	
	$checkbox.addEventListener('click', () => {
		toggleDebug();
	});
	
	// Turn on debug mode by default, unless the string `false` is stored in local storage:
	if (window.localStorage.getItem('debug') !== 'false') {
		toggleDebug();
	}
});