aboutsummaryrefslogtreecommitdiff
path: root/p/scripts/feed.js
blob: 7516f9e51b26deb1a994b727bcdcf4dbe350c9d4 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
'use strict';
/* globals init_archiving, init_configuration_alert, init_password_observers, init_slider, init_url_observers */

// <popup>
let popup = null;
let popup_iframe_container = null;
let popup_iframe = null;
let popup_working = false;

function openPopupWithSource(source) {
	if (popup_working === true) {
		return false;
	}
	popup_working = true;
	popup_iframe.src = source;
	popup_iframe_container.style.display = 'table-row';
	popup.style.display = 'block';
}

function closePopup() {
	popup.style.display = 'none';
	popup_iframe_container.style.display = 'none';
	popup_iframe.src = 'about:blank';
	popup_working = false;
}

function init_popup() {
	// Fetch elements.
	popup = document.getElementById('popup');
	if (popup) {
		popup_iframe_container = document.getElementById('popup-iframe-container');
		popup_iframe = document.getElementById('popup-iframe');

		// Configure close button.
		document.getElementById('popup-close').addEventListener('click', function (ev) {
			closePopup();
		});

		// Configure close-on-click.
		window.addEventListener('click', function (ev) {
			if (ev.target == popup) {
				closePopup();
			}
		});
	}
}
// </popup>

function init_popup_preview_selector() {
	const link = document.getElementById('popup-preview-selector');

	if (!link) {
		return;
	}

	link.addEventListener('click', function (ev) {
		const selector_entries = document.getElementById('path_entries').value;
		const selector_entries_filter = document.getElementById('path_entries_filter').value;
		const href = link.href.replace('selector-token', encodeURIComponent(selector_entries))
			.replace('selector-filter-token', encodeURIComponent(selector_entries_filter));

		openPopupWithSource(href);

		ev.preventDefault();
	});
}

function init_disable_elements_on_update(parent) {
	const inputs = parent.querySelectorAll('input[data-disable-update]');
	for (const input of inputs) {
		input.addEventListener('input', (e) => {
			const elem = document.getElementById(e.target.dataset.disableUpdate);
			if (elem) {
				elem.disabled = true;
				elem.remove();
			}
		});
	}
}

/**
 * Allow a <select class="select-show"> to hide/show elements defined by <option data-show="elem-id"></option>
 */
function init_select_show(parent) {
	const listener = (select) => {
		const options = select.querySelectorAll('option[data-show]');
		const shows = {};	// To allow multiple options to show the same element
		for (const option of options) {
			const targets = option.dataset.show.split(' ');	// Allow multiple targets
			for (const target of targets) {
				if (!shows[target]) {
					shows[target] = option.selected;
				}
			}
		}

		for (const show in shows) {
			const elem = document.getElementById(show);
			if (elem) {
				elem.style.display = shows[show] ? 'block' : 'none';
			}
		}
	};

	const selects = parent.querySelectorAll('select.select-show');
	for (const select of selects) {
		select.addEventListener('change', (e) => listener(e.target));
		listener(select);
	}
}

/** Automatically validate XPath textarea fields */
function init_valid_xpath(parent) {
	const listener = (textarea) => {
		const evaluator = new XPathEvaluator();
		try {
			if (textarea.value === '' || evaluator.createExpression(textarea.value) != null) {
				textarea.setCustomValidity('');
			}
		} catch (ex) {
			textarea.setCustomValidity(ex);
		}
	};

	const textareas = parent.querySelectorAll('textarea.valid-xpath');
	for (const textarea of textareas) {
		textarea.addEventListener('change', (e) => listener(e.target));
		listener(textarea);
	}
}

function init_feed_afterDOM() {
	if (!window.init_slider) {
		if (window.console) {
			console.log('FreshRSS feed waiting for JS…');
		}
		setTimeout(init_feed_afterDOM, 50);
		return;
	}

	const slider = document.getElementById('slider');
	if (slider) {
		slider.addEventListener('freshrss:slider-load', function (e) {
			init_popup();
			init_popup_preview_selector();
			init_select_show(slider);
			init_disable_elements_on_update(slider);
			init_password_observers(slider);
			init_url_observers(slider);
			init_valid_xpath(slider);
		});
		init_slider(slider);
		init_archiving(slider);
	} else {
		init_archiving(document.body);
		init_popup();
		init_popup_preview_selector();
		init_select_show(document.body);
		init_disable_elements_on_update(document.body);
		init_password_observers(document.body);
		init_valid_xpath(document.body);
	}

	init_configuration_alert();

	if (window.console) {
		console.log('FreshRSS feed init done.');
	}
}

if (document.readyState && document.readyState !== 'loading') {
	init_feed_afterDOM();
} else {
	document.addEventListener('DOMContentLoaded', function () {
		if (window.console) {
			console.log('FreshRSS feed waiting for DOMContentLoaded…');
		}
		init_feed_afterDOM();
	}, false);
}

// @license-end