PageSourceSearch

https://high.org/wp-content/themes/icelander-child/assets/js/page-teacher-resources.js?t=1790849555?ver=1.0.0

js high.org collected 2026-10-01 10:19:57 UTC 5,637 bytes, 203 lines download raw bytes

1/**
2 * Functions for the Teacher Resources Page.
3 */
4
5const pageTeacherResources = {
6	perPage: 12,
7	teacherResourcesInputs: null,
8	inputCollections: null,
9	s: null,
10	rt: null,
11	filterByNames: {
12		trt: 'teacher-resource-tax',
13		all: 'all-id'
14	},
15	init: () => {
16		if (pageTeacherResources.shouldCallFunctions('/teacher-resources')) {
17			window.addEventListener('DOMContentLoaded', async function () {
18				await pageTeacherResources.loadVariables();
19				await pageTeacherResources.addEventListenerToFilterInputs();
20				await pageTeacherResources.loadTeacherResources(1);
21			});
22		}
23	},
24	shouldCallFunctions: (param) => {
25		return (window.location.href.indexOf(param) >= 0 ? true : false);
26	},
27	loadVariables: async () => {
28		pageTeacherResources.teacherResourcesInputs = document.querySelectorAll('.teacher-resources-search-filter');
29		pageTeacherResources.inputCollections = {
30			'teacher-resources': pageTeacherResources.teacherResourcesInputs
31		};
32	},
33	addEventListenerToFilterInputs: async () => {
34		const inputTypes = ['checkbox'];
35
36		Object.entries(pageTeacherResources.inputCollections).forEach(entry => {
37			const [type, inputCollection] = entry;
38
39			if (!pageTeacherResources.checkIfElementExistsOnDOM(inputCollection)) {
40				return;
41			}
42
43			inputCollection.forEach((inputItem) => {
44				const eventType = inputTypes.includes(inputItem.type) ? 'change' : 'blur';
45
46				inputItem.addEventListener(eventType, async (e) => {
47					await pageTeacherResources.setCheckedInputSiblings(inputItem);
48					await pageTeacherResources.uncheckInputAll(inputItem);
49				});
50			});
51		});
52	},
53	setCheckedInputSiblings: async (inputChecked) => {
54		if (inputChecked.name.indexOf('all') == -1) {
55			return false;
56		}
57
58		const parentDiv = findParent(inputChecked, '.at-filters-component-filter-list-container');
59
60		parentDiv.childNodes.forEach((childDiv) => {
61			if (childDiv.tagName === 'DIV') {
62				if (childDiv.firstChild.name !== inputChecked.name) {
63					childDiv.firstChild.checked = inputChecked.checked;
64				}
65			}
66		});
67	},
68	uncheckInputAll: async (inputChecked) => {
69		if (
70			inputChecked.name.indexOf('all') >= 0 ||
71			inputChecked.checked
72		) {
73			return false;
74		}
75
76		const parentDiv = findParent(inputChecked, '.at-filters-component-filter-list-container');
77
78		if (parentDiv !== null) {
79			parentDiv.firstChild.firstChild.checked = false;
80		}
81	},
82	checkIfElementExistsOnDOM: (element) => {
83		if (element === null) {
84			return false;
85		}
86
87		return element.length;
88	},
89	redirectURLWithFilterValues: (bodyData, type) => {
90		const taxFields = bodyData?.taxFields ? Object.entries(bodyData?.taxFields) : [];
91		let newParams = pageTeacherResources.mountBaseQueryString(type);
92
93		taxFields.forEach(([field, value]) => {
94			newParams += pageTeacherResources.mountParamsToURL(field, value);
95		});
96
97		window.location.href = `${window.location.origin}/${newParams}`;
98	},
99	submitForm: async () => {
100		const checkedFields = document.querySelectorAll('.teacher-resources-search-filter:checked');
101
102		bodyData = pageTeacherResources.mountBodyData(checkedFields, 'teacher-resource');
103		pageTeacherResources.redirectURLWithFilterValues(bodyData, 'teacher-resources');
104	},
105	mountBodyData: (elements, type) => {
106		const searchReturn = {s: ''};
107		pageTeacherResources.s = searchReturn.s;
108
109		searchReturn['queryType'] = type;
110
111		if (elements) {
112			searchReturn['taxFields'] = {};
113
114			elements.forEach((field) => {
115				if (!field.value) {
116					return;
117				}
118
119				let fieldValue = field.value;
120
121				if (field.name.includes('teacher-resource-tax-')) {
122					fieldValue = field.name.replace('teacher-resource-tax-', '');
123				}
124
125				if (field.name.includes('tax') || field.name.includes('all-')) {
126					searchReturn['taxFields'][field.name] = fieldValue;
127				}
128			});
129		}
130
131		return searchReturn;
132	},
133	mountBaseQueryString: (type) => {
134		return `?s=${pageTeacherResources.s}&rt=${type}`;
135	},
136	mountParamsToURL: (field, value) => {
137		let params = '';
138		const filterByNames = Object.entries(pageTeacherResources.filterByNames);
139
140		if (!isNaN(field)) {
141			params = `&${field}=${value}`;
142		}
143
144		filterByNames.forEach(([param, name]) => {
145
146			if (field.indexOf(name) == -1) {
147				return;
148			}
149
150			const arrayName = field.split('-');
151			if (arrayName.length >= 3) {
152				let lastIndex = arrayName.length - 1;
153				params = `&${param}-${arrayName[lastIndex]}=${value}`;
154				return;
155			}
156
157			if (isNaN(value)) {
158				params = `&${param}=${value}`;
159			}
160		})
161
162		return params;
163	},
164	loadTeacherResources: async (page) => {
165		const container = document.querySelector('#load-teacher-resources');
166
167		addLoading(document.querySelector('#page'), true);
168
169		if (container.getAttribute('data-loaded-page')) {
170			page = container.getAttribute('data-loaded-page');
171
172			container.removeAttribute('data-loaded-page');
173		}
174
175		const teacherResourcesContent = await fetchTeacherResourcesContent(page, pageTeacherResources.perPage);
176
177		removeLoadingBlock();
178		updateInnerHTML(container, teacherResourcesContent);
179	},
180	paginate: async (e, element) => {
181		e.preventDefault();
182
183		const contentElement = document.querySelector('#load-teacher-resources');
184		const page = element.getAttribute('data-page') ?? 1;
185		const titleElement = findParent(e.target, '#main')
186			.querySelector('.at-section-title');
187
188		if (!contentElement.getAttribute('data-page')) {
189			contentElement.setAttribute('data-page', 1);
190		}
191
192		await scrollToElement(titleElement);
193		await pageTeacherResources.loadTeacherResources(page);
194
195		if (page > 1) {
196			injectURL('teacher-resources', '/teacher-resources/page/' + page);
197		} else {
198			injectURL('teacher-resources', '/teacher-resources/');
199		}
200	}
201}
202
203pageTeacherResources.init();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.