PageSourceSearch

https://saarlane.ee/assets/js/app/browse.locations.js

js saarlane.ee collected 2026-10-02 07:19:23 UTC 10,968 bytes, 381 lines download raw bytes

1/*
2 * LaraClassifier - Classified Ads Web Application
3 * Copyright (c) BeDigit. All Rights Reserved
4 *
5 * Website: https://laraclassifier.com
6 * Author: BeDigit | https://bedigit.com
7 *
8 * LICENSE
9 * -------
10 * This software is furnished under a license and may be used and copied
11 * only in accordance with the terms of such license and with the inclusion
12 * of the above copyright notice. If you Purchased from CodeCanyon,
13 * Please read the full License from here - https://codecanyon.net/licenses/standard
14 */
15
16if (typeof defaultAdminType === 'undefined') {
17	var defaultAdminType = 0;
18}
19if (typeof defaultAdminCode === 'undefined') {
20	var defaultAdminCode = 0;
21}
22if (typeof loadingWd === 'undefined') {
23	var loadingWd = 'Loading...';
24}
25
26$(document).ready(function () {
27	
28	$(document).on('change', '#countryCode', function (e) {
29		countryCode = changeCountry(countryCode, this);
30	});
31	
32	$('#browseLocations').on('show.bs.modal', function (e) {
33		let triggerEl = $(e.relatedTarget); /* Button that triggered the modal */
34		let params = getLocationsBrowsingParameters(countryCode, triggerEl, defaultAdminType, defaultAdminCode);
35		browseLocations(params);
36	});
37	$(document).on('click submit', '#modalQuerySearchBtn', function (e) {
38		e.preventDefault(); /* prevents submit or reload */
39		
40		let params = getLocationsBrowsingParameters(countryCode, this);
41		browseLocations(params);
42		
43		return false;
44	});
45	$(document).on('click', '#browseLocations .is-admin, #browseLocations .page-link', function (e) {
46		e.preventDefault();
47		
48		let params = getLocationsBrowsingParameters(countryCode, this);
49		browseLocations(params);
50		
51		return false;
52	});
53	$(document).on('click', '#modalQueryClearBtn', function (e) {
54		e.preventDefault();
55		
56		let queryEl = $('#modalQuery');
57		let query = queryEl.val();
58		if (!isEmpty(query)) {
59			queryEl.val('');
60			let params = getLocationsBrowsingParameters(countryCode, this);
61			browseLocations(params);
62		}
63		queryEl.focus();
64		
65		return false;
66	});
67	
68});
69
70/**
71 * Get & update the locations browsing parameters
72 *
73 * @param countryCode
74 * @param jsThis
75 * @param adminType
76 * @param adminCode
77 * @returns {{}|{countryChanged: (*|number), currSearch: (*|jQuery), adminType: *, query: string, adminCode: *, _token: (*|jQuery), cityId: number, languageCode: string, url: string}}
78 */
79function getLocationsBrowsingParameters(countryCode, jsThis = null, adminType = null, adminCode = null) {
80	let params = {};
81	
82	/* Check required variables */
83	if (typeof languageCode === 'undefined' || typeof countryCode === 'undefined') {
84		return params;
85	}
86	
87	let thisEl = null;
88	let thisElTagName = '';
89	let thisElClass = '';
90	if (isDefined(jsThis) && jsThis !== null) {
91		thisEl = $(jsThis);
92		thisElTagName = thisEl.get(0).tagName.toLowerCase();
93		thisElClass = thisEl.attr('class');
94		thisElClass = (isDefined(thisElClass) && thisElClass != null) ? thisElClass : '';
95	}
96	
97	/* Location's modal fields */
98	let countryChangedEl = $('#modalCountryChanged');
99	let urlEl = $('#modalUrl');
100	let adminTypeEl = $('#modalAdminType');
101	let adminCodeEl = $('#modalAdminCode');
102	let queryEl = $('#modalQuery');
103	let query = '';
104	
105	/* Main form fields */
106	let cityIdEl = $('#cityId');
107	let cityId = 0;
108	
109	let isGoingToBaseUrlTrigger = (
110		isDefined(thisElTagName)
111		&& isDefined(thisElClass)
112		&& thisElTagName != null
113		&& thisElClass != null
114		&& thisElTagName === 'a'
115		&& thisElClass.indexOf('is-admin') !== -1
116		&& thisElClass.indexOf('go-base-url') !== -1
117	);
118	
119	/* Retrieve the adminType & the adminCode */
120	adminType = !isEmpty(adminType) ? adminType : adminTypeEl.val();
121	adminType = !isEmpty(adminType) ? adminType : 0;
122	adminCode = !isEmpty(adminCode) ? adminCode : 0;
123	
124	/* Base URL */
125	let baseUrl = '';
126	let url = '';
127	
128	if (!isEmpty(adminType)) {
129		baseUrl = siteUrl + '/ajax/locations/' + strToLower(countryCode) + '/admins/' + adminType;
130	} else {
131		baseUrl = siteUrl + '/ajax/locations/' + strToLower(countryCode) + '/cities';
132	}
133	
134	if (isGoingToBaseUrlTrigger) {
135		let goBaseUrl = null;
136		if (thisEl !== null) {
137			goBaseUrl = thisEl.data('url');
138		}
139		url = !isEmpty(goBaseUrl) ? goBaseUrl : baseUrl;
140		urlEl.val(url);
141		queryEl.val('');
142	} else {
143		if (thisElTagName === 'select') {
144			adminCode = !isEmpty(adminCode) ? adminCode : adminCodeEl.val();
145			adminCode = !isEmpty(adminCode) ? adminCode : 0;
146		}
147		
148		if (thisEl !== null) {
149			let dataAdminCode = thisEl.data('adminCode');
150			if (isDefined(dataAdminCode)) {
151				adminCode = dataAdminCode;
152			}
153			
154			if (thisElTagName === 'select') {
155				$('#modalTriggerName').val(thisElTagName);
156			}
157			
158			if (thisElTagName === 'a') {
159				$('.tooltip.show.modal-tooltip').hide();
160				if (thisElClass.indexOf('is-admin') !== -1) {
161					queryEl.val('');
162				}
163			}
164			
165			if (!isDefined(adminCode) || isEmpty(adminCode)) {
166				if (thisElTagName === 'a') {
167					if ((thisElClass.indexOf('is-admin') !== -1) || (thisElClass.indexOf('page-link') !== -1)) {
168						url = thisEl.data('url');
169						if (!isEmpty(url)) {
170							let urlToSave = removeURLParameter(url, 'page');
171							urlEl.val(urlToSave);
172							
173							let urlArr = url.split('/');
174							if (url.indexOf(baseUrl) !== -1) {
175								let tmpPart = isDefined(urlArr[7]) ? urlArr[7] : '';
176								let tmpArr = tmpPart.split('?');
177								adminType = (isDefined(tmpArr[0]) && !isEmpty(tmpArr[0])) ? tmpArr[0] : 0;
178								adminCode = 0;
179							} else {
180								adminType = isDefined(urlArr[7]) ? urlArr[7] : 0;
181								adminCode = isDefined(urlArr[8]) ? urlArr[8] : 0;
182							}
183						}
184					}
185				} else {
186					url = urlEl.val();
187					adminCode = adminCodeEl.val();
188				}
189			}
190			if (thisElTagName === 'button' || thisElTagName === 'a') {
191				query = queryEl.val();
192				if (thisElTagName === 'button') {
193					url = removeURLParameter(url, 'page');
194				}
195			}
196			if (thisElTagName === 'input' && thisEl.attr('type') === 'text') {
197				query = thisEl.val();
198			}
199			
200			/* Get the (eventual) current city's ID from bootstrap modal link */
201			if (thisElTagName === 'a' && thisEl.attr('href') === '#browseLocations') {
202				cityId = thisEl.data('cityId');
203			}
204		}
205		
206		/* Get the (eventual) current city's ID from a main form's city's field */
207		if (!isDefined(cityId) || isEmpty(cityId)) {
208			cityId = (isDefined(cityIdEl) && cityIdEl != null) ? cityIdEl.val() : 0;
209			cityId = isDefined(cityId) ? cityId : 0;
210		}
211		
212		if (isEmpty(url)) {
213			url = baseUrl;
214			if (!isEmpty(adminType) && !isEmpty(adminCode)) {
215				url = siteUrl + '/ajax/locations/' + strToLower(countryCode) + '/admins/' + adminType + '/' + adminCode + '/cities';
216			}
217		}
218		
219		/* Save the adminType & the adminCode */
220		adminTypeEl.val(adminType);
221		adminCodeEl.val(adminCode);
222	}
223	
224	let countryChanged = (isDefined(countryChangedEl) && countryChangedEl != null) ? countryChangedEl.val() : 0;
225	
226	params = {
227		languageCode: languageCode,
228		countryChanged: countryChanged,
229		url: url,
230		adminType: adminType,
231		adminCode: adminCode,
232		query: query,
233		cityId: cityId,
234		currSearch: $('#currSearch').val(),
235		_token: $('input[name=_token]').val(),
236	};
237	
238	return params;
239}
240
241/**
242 * Get & append cities of a given administrative division's code
243 *
244 * @param params
245 */
246function browseLocations(params) {
247	/* console.log(params); */
248	/* Check required variables */
249	if (
250		typeof params.languageCode === 'undefined'
251		|| typeof params.countryChanged === 'undefined'
252		|| typeof params.url === 'undefined'
253		|| typeof params.adminType === 'undefined'
254		|| typeof params.adminCode === 'undefined'
255		|| typeof params.query === 'undefined'
256		|| typeof params.cityId === 'undefined'
257		|| typeof params.currSearch === 'undefined'
258		|| typeof params._token === 'undefined'
259	) {
260		return false;
261	}
262	
263	/* Elements to update */
264	let locationsListEl = $('#locationsList');
265	let locationsTitleEl = $('#locationsTitle');
266	
267	let ajax = $.ajax({
268		method: 'POST',
269		url: params.url,
270		data: {
271			'languageCode': params.languageCode,
272			'countryChanged': params.countryChanged,
273			'adminType': params.adminType,
274			'adminCode': params.adminCode,
275			'query': params.query,
276			'cityId': params.cityId,
277			'currSearch': params.currSearch,
278			'_token': params._token
279		},
280		beforeSend: function () {
281			/*
282			let spinner = '<div class="d-flex align-items-center">\n' +
283				'  <strong>' + loadingWd + '</strong>\n' +
284				'  <div class="spinner-border ms-auto" role="status" aria-hidden="true"></div>\n' +
285				'</div>';
286			locationsListEl.html(spinner);
287			*/
288			
289			locationsListEl.empty().addClass('py-4').busyLoad('hide');
290			locationsListEl.busyLoad('show', {
291				text: loadingWd,
292				custom: createCustomSpinnerEl(),
293				background: '#fff',
294				containerItemClass: 'm-5',
295			});
296		}
297	});
298	ajax.done(function (xhr) {
299		locationsListEl.removeClass('py-4').busyLoad('hide');
300		
301		try {
302			if (typeof xhr.locationsTitle == 'undefined' || typeof xhr.locationsContent == 'undefined') {
303				locationsListEl.empty();
304				
305				return false;
306			}
307			
308			/* Update modal content */
309			locationsTitleEl.html(xhr.locationsTitle);
310			locationsListEl.html(xhr.locationsContent);
311			
312			/* Enable the tooltip */
313			/* To prevent the tooltip in bootstrap doesn't work after ajax, use selector on exist element like body */
314			let bodyEl = $('body');
315			bodyEl.tooltip({selector: '[data-bs-toggle="tooltip"]'});
316		} catch (e) {
317			jsAlert(e, 'error', false, true);
318		}
319	});
320	ajax.fail(function (xhr) {
321		locationsListEl.empty();
322		
323		let message = getJqueryAjaxError(xhr);
324		if (message !== null) {
325			jsAlert(message, 'error', false, true);
326			
327			/* Close the Modal */
328			let modalEl = document.querySelector('#browseLocations');
329			if (typeof modalEl !== 'undefined' && modalEl !== null) {
330				let modalObj = bootstrap.Modal.getInstance(modalEl);
331				if (modalObj !== null) {
332					modalObj.hide();
333				}
334			}
335		}
336	});
337}
338
339/**
340 * Change Country
341 *
342 * @param countryCode
343 * @param jsThis
344 * @returns {*}
345 */
346function changeCountry(countryCode, jsThis) {
347	let thisEl = $(jsThis);
348	
349	let newCountryCode = thisEl.val();
350	let newAdminType = thisEl.find(':selected').data('adminType');
351	
352	if (isEmpty(newCountryCode)) {
353		return countryCode;
354	}
355	
356	/* Location's modal fields */
357	let countryChangedEl = $('#modalCountryChanged');
358	let urlEl = $('#modalUrl');
359	let adminTypeEl = $('#modalAdminType');
360	let adminCodeEl = $('#modalAdminCode');
361	let queryEl = $('#modalQuery');
362	
363	/* Main form fields */
364	let cityIdEl = $('#cityId');
365	
366	/* Reset all fields and match them with the new country */
367	countryChangedEl.val(1);
368	let baseUrl = '';
369	if (!isEmpty(newAdminType)) {
370		baseUrl = siteUrl + '/ajax/locations/' + strToLower(newCountryCode) + '/admins/' + newAdminType;
371	} else {
372		baseUrl = siteUrl + '/ajax/locations/' + strToLower(newCountryCode) + '/cities';
373	}
374	urlEl.val(baseUrl);
375	adminTypeEl.val(!isEmpty(newAdminType) ? newAdminType : 0);
376	adminCodeEl.val(0);
377	queryEl.val('');
378	cityIdEl.empty().append('<option value="0">' + lang.select.city + '</option>').val('0').trigger('change');
379	
380	return newCountryCode;
381}

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.