PageSourceSearch

https://www.archivecenter.net/scripts/common/commonUtil.js

js archivecenter.net collected 2026-10-02 19:05:19 UTC 12,961 bytes, 503 lines download raw bytes

1function getOthersByClass(el, className) {
2	const nodes = document.getElementsByClassName(className);
3	let siblings = [];
4	for (let i = 0; i < nodes.length; i++) {
5		if (el !== nodes[i]) {
6			siblings.push(nodes[i]);
7		}
8	}
9	return siblings;
10}
11
12const submitForm = document.createElement('form');
13
14function createSubmitForm(name, callUrl, method) {
15	submitForm.setAttribute('name', name);
16	submitForm.setAttribute('action', callUrl);
17	submitForm.setAttribute('method', method);
18
19	document.body.appendChild(submitForm);
20}
21
22const createHiddenInputsAndInsertToForm = function (...arrArgs) {
23	arrArgs.forEach((arg) => {
24		const input = document.createElement('input');
25		input.setAttribute('type', 'hidden');
26		input.setAttribute('name', arg[0]);
27		input.setAttribute('value', arg[1]);
28
29		submitForm.appendChild(input);
30	});
31};
32
33const createHiddenInputs = function (...arrArgs) {
34	const result = [];
35	arrArgs.forEach((arg) => {
36		const input = document.createElement('input');
37		input.setAttribute('type', 'hidden');
38		input.setAttribute('name', arg[0]);
39		input.setAttribute('value', arg[1]);
40		input.setAttribute('class', arg[2]);
41		result.push(input);
42	});
43	return result;
44};
45
46const createElement = function (obj) {
47	const result = document.createElement(obj.tag);
48	if (obj.id) {
49		result.setAttribute('id', obj.id);
50	}
51	if (obj.class) {
52		if (typeof obj.class === 'string') {
53			result.classList.add(obj.class);
54		} else if (obj.class.length > 0) {
55			obj.class.forEach(cls => result.classList.add(cls));
56		}
57	}
58	return result;
59};
60function changeSerialize(formData, inputName, inputValue) {
61	for (i = 0; i < formData.length; i++) {
62		if (formData[i].name == inputName) {
63			formData[i].value = inputValue;
64		}
65	}
66}
67
68function findValueInSerialize(formData, name) {
69	for (i = 0; i < formData.length; i++) {
70		if (formData[i].name === name) {
71			return formData[i].value;
72		}
73	}
74	return '';
75}
76
77const appendHtml = function (parent, html) {
78	if (!parent) return;
79	const docFrag = document.createDocumentFragment();
80	const div = docFrag.appendChild(document.createElement('div'));
81	div.innerHTML = html;
82	reAssembleScriptInDOM(div);
83	while (div.childElementCount) {
84		parent.appendChild(div.firstChild);
85	}
86}
87
88const prependHtml = function (parent, html) {
89	if (!parent) return;
90	const docFrag = document.createDocumentFragment();
91	const div = docFrag.appendChild(document.createElement('div'));
92	div.innerHTML = html;
93	reAssembleScriptInDOM(div);
94	while (div.childElementCount) {
95		parent.prepend(div.lastChild);
96	}
97}
98
99function reAssembleScriptInDOM(target) {
100	target.querySelectorAll('script').forEach(el => {
101		script = document.createElement('script');
102		if (el.src) script.src = el.src;
103		script.textContent = el.textContent;
104		target.appendChild(script);
105	});
106}
107
108function recordPageOfHash() {
109	if (typeof page !== 'undefined') {
110		window.location.hash = '#page' + page;
111	}
112}
113
114function getPageFromHash() {
115	if (window.location.hash) {
116		let lastPage = parseInt(location.hash.split('#')[1].replace('page', ''));
117		return lastPage / 20;
118	}
119	return 0;
120}
121
122function removeClassFromClass(objClassName, removedClassName) {
123	const el = document.getElementsByClassName(objClassName);
124	for (var i = 0; i < el.length; i++) {
125		el[i].classList.remove(removedClassName);
126	}
127}
128
129function getDocumentHeight() {
130	const body = document.body;
131	const html = document.documentElement;
132
133	return Math.max(
134		body.scrollHeight,
135		body.offsetHeight,
136		html.clientHeight,
137		html.scrollHeight,
138		html.offsetHeight
139	);
140}
141
142function getScrollTop() {
143	return window.pageYOffset !== undefined
144		? window.pageYOffset
145		: (document.documentElement || document.body.parentNode || document.body)
146				.scrollTop;
147}
148
149function removeSpecialCharacters(text) {
150	const specialCharacter = /[~!@\#$%^&*\()\-=+_'\;<>\/.\`:\"\\,\[\]?|{}]/gi;
151	if (text.length > 0 && text.match(specialCharacter)) {
152		text = text.replace(specialCharacter, '');
153	}
154	return text;
155}
156
157function validateElement(el, count) {
158	el.value = el.value.substring(0, count);
159	el.value = removeSpecialCharacters(el.value);
160}
161
162function validateString(str, count) {
163	str = str.substring(0, count);
164	str = removeSpecialCharacters(str);
165	return str;
166}
167
168function validateEachWord(array, count) {
169	Array.prototype.forEach.call(array, function (el) {
170		el.setAttribute('maxlength', count);
171		el.addEventListener('keyup', function (e) {
172			validateElement(el, count);
173		});
174	});
175}
176
177HTMLElement.prototype.closestByClass = function (className) {
178	let target = this;
179	while (!target.parentElement.classList.contains(className)) {
180		if (target.parentElement == null) {
181			return null;
182		}
183		target = target.parentElement;
184	}
185	return target.parentElement;
186};
187
188HTMLElement.prototype.siblingByClass = function (className) {
189	let target = this;
190	let children = target.parentElement.children;
191	for (let i = 0; i < children.length; i++) {
192		if (children[i].classList.contains(className)) {
193			return children[i];
194		}
195	}
196	return null;
197};
198
199HTMLElement.prototype.toggle = function () {
200	let target = this;
201	if (target.style.display === 'none') {
202		target.style.display = '';
203	} else {
204		target.style.display = 'none';
205	}
206};
207
208function siblings(el) {
209	let tempArr = new Array();
210	let children = el.parentElement.children;
211	for (var i = 0; i < children.length; i++) {
212		tempArr.push(children[i]);
213	}
214	return tempArr.filter(function (e) {
215		return e !== el;
216	});
217}
218
219function removeClassForArray(nodes, className) {
220	Array.prototype.forEach.call(nodes, function (el) {
221		el.classList.remove(className);
222	});
223}
224
225function toggleArrayNode(array) {
226	Array.prototype.forEach.call(array, function (el) {
227		if (el.style.display === 'none') {
228			el.style.display = 'block';
229		} else {
230			el.style.display = 'none';
231		}
232	});
233}
234
235function hideArrayNode(array) {
236	Array.prototype.forEach.call(array, function (el) {
237		el.style.display = 'none';
238	});
239}
240
241function showArrayNode(array) {
242	Array.prototype.forEach.call(array, function (el) {
243		el.style.display = '';
244	});
245}
246
247function toggleElement(el) {
248	if (el.style.display === 'none') {
249		el.style.display = '';
250	} else {
251		el.style.display = 'none';
252	}
253}
254
255function replaceAll(str, searchStr, replaceStr) {
256	return str.split(searchStr).join(replaceStr);
257}
258
259function toggleOneHideOthersByClass(className, el) {
260	const nodes = document.getElementsByClassName(className);
261	for (let i = 0; i < nodes.length; i++) {
262		if (nodes[i] !== el) {
263			nodes[i].style.display = 'none';
264		}
265	}
266	if (el.style.display !== 'none') {
267		el.style.display = 'none';
268	} else {
269		el.style.display = '';
270	}
271}
272
273function toggleClassForOneRemoveOthersByClass(className, toggleClass, el) {
274	const nodes = document.getElementsByClassName(className);
275	for (let i = 0; i < nodes.length; i++) {
276		if (nodes[i] !== el) {
277			nodes[i].classList.remove(toggleClass);
278		}
279	}
280	el.classList.toggle(toggleClass);
281}
282
283function trimAndRemoveSpace(str) {
284	return str.trim().replace(/(\s*)/g, '');
285}
286
287function setOtherDataset(nodes, target, name, beforeValue, afterValue) {
288	for (let i = 0; i < nodes.length; i++) {
289		if (nodes[i] !== target && nodes[i].dataset[name] === beforeValue) {
290			nodes[i].dataset[name] = afterValue;
291		}
292	}
293}
294
295function findParentByTag(el, tagName) {
296	let target = el.parentElement;
297	while (true) {
298		if (target.tagName === tagName) {
299			return target;
300		}
301		target = target.parentElement;
302	}
303}
304
305function checkAllInTbodyBootstrap(el) {
306	const checkboxs = findParentByTag(el, 'TABLE')
307		.getElementsByTagName('tbody')[0]
308		.querySelectorAll('input[type=checkbox]');
309	if (el.checked) {
310		for (let i = 0; i < checkboxs.length; i++) {
311			checkboxs[i].checked = true;
312			findParentByTag(checkboxs[i], 'SPAN').classList.add('checked');
313		}
314	} else {
315		for (let i = 0; i < checkboxs.length; i++) {
316			checkboxs[i].checked = false;
317			findParentByTag(checkboxs[i], 'SPAN').classList.remove('checked');
318		}
319	}
320}
321
322function convertNumberToDashedDate(num) {
323	if (num == null && num.toString().length !== 8) return num;
324	const strNum = num.toString();
325	return `${strNum.substring(0, 4)}-${strNum.substring(
326		4,
327		6
328	)}-${strNum.substring(6, 8)}`;
329}
330
331$(document).ready(function () {
332	$('input[type=date]').attr('max', '9999-12-31');
333});
334
335function findElementFromParent(childEl, targetEl) {
336	const parent = childEl.parentElement;
337	if (childEl === document.body || parent === document.body) return null;
338
339	if (parent === targetEl) {
340		return targetEl;
341	} else {
342		return findElementFromParent(parent, targetEl);
343	}
344}
345
346function doAjax(options) {
347	const [type, url, data, dataType, success, error] = setAjaxOptions(options);
348	const xhr = new XMLHttpRequest();
349	xhr.onreadystatechange = () => {
350		if (xhr.readyState === XMLHttpRequest.DONE) {
351			if (xhr.status === 200) {
352				success(xhr.response);
353			} else {
354				error(new Error(xhr.status));
355			}
356		}
357	};
358	let formedData;
359	switch (type) {
360		case 'GET':
361			formedData =
362				data != null
363					? Object.keys(data)
364							.map((key) => `${key}=${data[key]}`)
365							.join('&')
366					: null;
367			xhr.open(type, `${url}?${formedData}`);
368			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
369			xhr.send(null);
370			xhr.responseType = dataType;
371			break;
372		case 'POST':
373			xhr.open(type, url);
374			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
375			formedData =
376				data != null
377					? Object.keys(data)
378							.map((key) => `${key}=${data[key]}`)
379							.join('&')
380					: null;
381			xhr.send(formedData);
382			xhr.responseType = dataType;
383			break;
384		default:
385			xhr.open(type, url);
386			xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
387			xhr.send(null);
388			xhr.responseType = dataType || xhr.responseType;
389			break;
390	}
391}
392
393
394function setAjaxOptions(options) {
395	let url = (data = success = error = null);
396	let type = 'POST';
397	let dataType = 'text';
398	for (const key in options) {
399		switch (key) {
400			case 'type':
401				type = options[key].toUpperCase();
402				break;
403			case 'url':
404				url = options[key];
405				break;
406			case 'data':
407				data = options[key];
408				break;
409			case 'dataType':
410				dataType = options[key];
411				break;
412			case 'success':
413				success = options[key];
414				break;
415			case 'error':
416				error = options[key];
417				break;
418		}
419	}
420	return [type, url, data, dataType, success, error];
421}
422
423/*수정요함 class="class1 class2"이고 .class1{display:none}이면 class2.style.display 값 none으로 안 나오던데*/
424const toggleDisplay = function (selector) {
425	const target = document.querySelector(selector);
426	if (target.style.display === 'none') {
427		target.style.display = '';
428		return;
429	}
430	target.style.display = 'none';
431}
432
433const fadeToggleBySelector = function (selector) {
434	switch (selector[0]) {
435		case '#':
436			fadeToggle(document.getElementById(selector.substring(1, selector.length)));
437			break;
438		default:
439			document.querySelectorAll(selector).forEach(el => fadeToggle(el));
440			break;
441	}
442}
443
444const fadeToggle = function (el) {
445	if (el.classList.contains('arc-appear')) {
446		fadeHide(el);
447		return;
448	}
449	fadeShow(el);
450}
451
452const fadeShow = function (el) {
453	el.classList.remove('arc-disappear');
454	el.classList.add('arc-appear');
455}
456
457const fadeHide = function (el) {
458	el.classList.add('arc-disappear');
459	setTimeout(() => el.classList.remove('arc-appear'), 100);
460}
461
462const fadeHideHandler = function (selector) {
463	document.body.addEventListener('click', function (e) {
464		if (!e.target.closest(selector)) {
465			//document.body.querySelectorAll(selector).forEach((el) => el.querySelectorAll('.arc-display-toggle-target').forEach((el) => fadeHide(el)));
466		}
467	});
468};
469
470const hideItem = function (item) {
471	if (item) item.style.display = 'none';
472}
473
474const movePage = function (url) {
475	location.href = url;
476}
477
478const wrapElement = function (target, wrapperTag, ...classes) {
479	const wrapper = document.createElement(wrapperTag);
480	classes.forEach(clss => wrapper.classList.add(clss));
481	wrapper.innerHTML = target.outerHTML;
482	target.parentNode.insertBefore(wrapper, target);
483	target.remove();
484	return wrapper;
485};
486
487const wrapAllElement = function (targets, wrapperTag) {
488	Array.from(targets).forEach(el => wrapElement(el, wrapperTag));
489}
490
491const isEmpty = function(value) {
492	if (value === "" || value === null || value === undefined || (value !== null && typeof value === 'object' && !Object.keys(value).length)) {
493		return true;
494	}
495	return false;
496}
497
498const isNotEmpty = function(value) {
499	if (value === "" || value === null || value === undefined || (value !== null && typeof value === 'object' && !Object.keys(value).length)) {
500		return false;
501	}
502	return true;
503}

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.