Website Summary Files Commits Branches @commit @diff @stats
path: root / wp-content / themes / carmag / assets / src / scripts / scripts.js
blob: 93e8823f6b93f7a015bac211f550a76cc2c3a8b5 (plain) (blame)
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
jQuery(document).ready(function ($) {
	"use strict";
	/*
	|--------------------------------------------------------------------------
	| Developer mode
	|--------------------------------------------------------------------------
	|
	| Set to true - it will allow printing in the console. Alsways check for this
	| variables when running tests so you dont forget about certain console.logs.
	| Id needed for development testing this variable should be used.
	|
	*/
	const devMode = function () {
		return true;
	};

	// Disable console.log for production site.
	if (!devMode()) {
		console.log = function () {};
	}
});

const toggleDrawer = () => {
	const drawerClassList = event.target.classList;
	const navClassList = document.querySelectorAll(".menu-primary-container")[0]
		.classList;
	drawerClassList.toggle("opened");
	navClassList.toggle("opened");
};

const separator = document.createElement("span");
separator.className = "separator";
const loginLink = document.querySelectorAll(".login")[0];
if (loginLink) {
	loginLink.before(separator);
}

const faqFields = document.querySelectorAll(".faq-field");
faqFields.forEach((field) => {
	field.addEventListener("click", () => {
		field.querySelector(".field-heading").classList.toggle("opened");
		field.querySelector(".field-content").classList.toggle("opened");
	});
});

let selectedBrand = document.getElementById("brands");
let modelsNames = Object.keys(carModels);
modelsNames.forEach((model) => {
	let option = document.createElement("option");
	option.value = model;
	option.innerHTML = model;
	selectedBrand.appendChild(option);
});
const handleModels = () => {
	const brand = event.target.value;
	const modelsSelect = document.getElementById("models");
	const defaultOption = modelsSelect.firstElementChild;
	modelsSelect.innerHTML = "";
	modelsSelect.append(defaultOption);
	let models;
	if (selectedType.value === "car") {
		models = carModels;
	} else if (selectedType.value === "truck") {
		models = truckModels;
	} else if (selectedType.value === "motorcycle") {
		models = motorcycleModels;
	} else if (selectedType.value === "tractor") {
		models = agriculturalBrands;
	} else if (selectedType.value === "bus") {
		models = busModels;
	}
	if (brand) {
		models[brand].forEach((model) => {
			let option = document.createElement("option");
			option.value = model;
			option.innerHTML = model;
			modelsSelect.appendChild(option);
		});
	} else {
		let option = document.createElement("option");
		option.value = "";
		option.innerHTML = "Model";
		modelsSelect.appendChild(option);
	}
};
let selectedType = document.getElementById("category");
let types = document.querySelectorAll(".type");
types.forEach((type) => {
	type.addEventListener("click", () => {
		event.stopPropagation();
		types.forEach((type) => {
			type.classList.remove("selected");
		});
		event.target.classList.add("selected");
		selectedType.value = event.target.id;
		let models;
		if (selectedType.value === "car") {
			models = carModels;
		} else if (selectedType.value === "truck") {
			models = truckModels;
		} else if (selectedType.value === "motorcycle") {
			models = motorcycleModels;
		} else if (selectedType.value === "tractor") {
			models = agriculturalBrands;
		} else if (selectedType.value === "bus") {
			models = busModels;
		}
		modelsNames = Object.keys(models);
		const defaultOption = selectedBrand.firstElementChild;
		selectedBrand.innerHTML = "";
		selectedBrand.append(defaultOption);
		modelsNames.forEach((model) => {
			let option = document.createElement("option");
			option.value = model;
			option.innerHTML = model;
			selectedBrand.appendChild(option);
		});
	});
});

const switcher = document.querySelectorAll(".switcher")[0];
if (switcher) {
	switcher.addEventListener("click", () => {
		event.stopPropagation();
		const firstPrice = document.querySelectorAll(".price-first")[0];
		const secondPrice = document.querySelectorAll(".price-second")[0];
		[firstPrice.value, secondPrice.value] = [
			secondPrice.value,
			firstPrice.value,
		];
	});
}