PageSourceSearch

https://carnejovenaragon.es/wp-content/plugins/qlk-carnejoven/publ…/qlk-carnejoven-renewal-form.js?ver=1.0.0

js carnejovenaragon.es collected 2026-10-08 10:47:10 UTC 7,698 bytes, 229 lines download raw bytes

1(function( $ ) {
2	'use strict';
3
4	const saveCardApplicationForm = (form) => {
5		//Disabled button
6		const submitBtn = $("#qlk-carnejoven-card-application-form-submit");
7		submitBtn.prop('disabled', true);
8
9		const formData = new FormData(form);
10		formData.append('action', 'qlk_carnejoven_save_renewal_card_application_form');
11		formData.append('nonce', qlk_carnejoven.nonce);
12
13		// const data = form.serialize() + `&action=qlk_carnejoven_save_card_application_form&nonce=${qlk_carnejoven.nonce}`;
14		$.ajax({
15			url: qlk_carnejoven.ajaxurl,
16			type: 'POST',
17			data: formData,
18			dataType:  'json',
19			processData: false,
20			contentType: false,
21			success: function(json) {
22				if(json?.success){
23					
24					if(json?.data?.data?.amount == 0 && json?.data?.data?.shipping_type == 'online'){
25						window.location.href = '/solicitud-completada/';
26					}else if(json?.data?.data?.redirect_url){
27						window.location.href = json?.data?.data?.redirect_url;
28					}else{
29						window.location.href = '/solicitud-registrada/';
30					}
31
32					const msg = `<p>La solicitud se ha registrado exitosamente.</p>`
33
34					$("#qlk-carnejoven-card-application-renewal-form-response")
35						.html(msg)
36						.removeClass("qlk-carnejoven-error-response")
37						.addClass("qlk-carnejoven-success-response");
38
39					form.reset();
40				}else{
41					let errorHtml = "<ul>";
42					json?.data?.errors.forEach(error => {
43						errorHtml += `<li>${error}</li>`;
44					});
45					errorHtml += "</ul>";
46
47					$("#qlk-carnejoven-card-application-renewal-form-response")
48						.html(errorHtml)
49						.removeClass("qlk-carnejoven-success-response")
50						.addClass("qlk-carnejoven-error-response");
51				}
52				
53			},
54			error: function(xhr, status, error) {
55				console.log(xhr.responseText);
56			},
57			complete: function(){
58				//Enabled button
59				submitBtn.prop('disabled', false);
60				grecaptcha.reset();
61			}
62		});
63	}
64	
65	const getApiResource = ( action, success = function(){}, data = {}) => {
66
67		const submitBtn = $("#qlk-carnejoven-card-application-renewal-form-submit");
68		submitBtn.prop('disabled', true);
69
70		$.ajax({
71			url: qlk_carnejoven.ajaxurl,
72			type: 'POST',
73			data: {
74				action: action,
75				nonce: qlk_carnejoven.nonce,
76				...data
77			},
78			dataType:  'json',
79			success: function(json) {
80				success(json);
81			},
82			error: function(xhr, status, error) {
83				console.log(xhr.responseText);
84			},
85			complete: function(){
86				//Enabled button
87				submitBtn.prop('disabled', false);
88			}
89		});
90	}
91
92	const makeOptions = (data, label = 'name', value = 'id', selected = '') => {
93		let options = "<option value=''>Seleccione</option>";
94		for (let i = 0; i < data.length; i++) {
95			options += `<option value="${data[i][value]}" ${data[i][value] == selected ? 'selected' : ''}>${data[i][label]}</option>`;
96		}
97		return options;
98	}
99
100	const loadSelectOptions = (action, object, data = {}, label = 'name', value = 'id') => {
101		const success = function(json){
102			const objData = object.data();
103			const options = makeOptions(json?.data?.data, label, value, objData?.selected);
104			object.html(options);
105			if(object.data('selected')){
106				object.trigger('change');
107			}
108		}
109		getApiResource(action, success, data);
110	}
111
112	const validateShowDocumentFile = function(){
113		const oldIdentification = $("#qlk-carnejoven-card-application-renewal-form input[name=old_identification]").val();
114		const oldGender = $("#qlk-carnejoven-card-application-renewal-form input[name=old_gender]").val();
115
116		const identification = $("#qlk-carnejoven-card-application-renewal-form input[name=identification]").val();
117		const gender = $("#qlk-carnejoven-card-application-renewal-form select[name=gender]").val();
118		const container = $("#qlk-carnejoven-card-application-form-identification_file_container");
119
120		if(identification != oldIdentification || gender != oldGender){
121			container.removeClass("qlk-carnejoven-hide");
122			container.find("input[name=identification_file]").attr('required', 'required').attr("disabled", false);
123		}else{
124			container.addClass("qlk-carnejoven-hide");
125			container.find("input[name=identification_file]").removeAttr('required').attr("disabled", true);
126		}
127	}
128
129	const validateShowAttachmentFile = function(){
130		const card_application_type_slug = $("#qlk-carnejoven-card-application-renewal-form input[name=card_application_type_slug]:checked").val();
131		const container = $("#qlk-carnejoven-card-application-form-attachment_file_container");
132		if(card_application_type_slug == 'stolen'){
133			container.removeClass("qlk-carnejoven-hide");
134			container.find("input[name=attachment_file]").attr('required', 'required').attr("disabled", false);
135		}else{
136			container.addClass("qlk-carnejoven-hide");
137			container.find("input[name=attachment_file]").removeAttr('required').attr("disabled", true);
138		}
139	}
140
141	$(document).ready(function(){
142
143		const form = $("#qlk-carnejoven-card-application-renewal-form");
144		
145		if(form.length > 0){
146
147			// alert(window.location.href)
148			if (window.location.href.includes("renovacion/")) {
149				setTimeout(function() {
150					$("#qlk-carnejoven-renewal-form-tab").trigger("click");					
151				},2000)
152			}
153
154			//Events
155			form.on("submit", function(e){
156				
157				e.preventDefault();
158
159				const form = this;
160				// const form = $('#qlk-carnejoven-card-application-form')[0];
161				// const form = document.getElementById('qlk-carnejoven-card-application-form');
162
163				if (this.checkValidity()) {
164					saveCardApplicationForm(form);
165				} else {
166					this.reportValidity();
167				}
168			});
169			
170			$("#qlk-carnejoven-card-application-renewal-form select[name=shipping_type]").on("change", function(){
171				const obj = $(this);
172				const container = $("#qlk-carnejoven-card-application-renewal-form-card_shipping_type_id-container");
173				const select = container.find("select");
174				if(obj.val() != 'online'){
175					container.removeClass("qlk-carnejoven-hide");
176					select.attr('required', 'required');
177				}else{
178					container.addClass("qlk-carnejoven-hide");
179					select.val(null).trigger('change');
180					select.removeAttr('required');
181				}
182			});
183			
184			$("#qlk-carnejoven-card-application-renewal-form select[name=province_id]").on("change", function(){
185				const obj = $(this);				
186				loadSelectOptions(
187					'qlk_carnejoven_get_regions',
188					$("#qlk-carnejoven-card-application-renewal-form select[name=region_id]"),
189					{province_id: obj.val()}
190				);
191			});
192			
193			$("#qlk-carnejoven-card-application-renewal-form select[name=region_id]").on("change", function(){
194				const obj = $(this);				
195				loadSelectOptions(
196					'qlk_carnejoven_get_populations',
197					$("#qlk-carnejoven-card-application-renewal-form select[name=population_id]"),
198					{region_id: obj.val()}
199				);
200			});
201
202			$("#qlk-carnejoven-card-application-renewal-form input[name=identification], #qlk-carnejoven-card-application-renewal-form select[name=gender]").on("change", function(){
203				validateShowDocumentFile();
204			});
205			
206			$("#qlk-carnejoven-card-application-renewal-form input[name=card_application_type_slug]").on("click", function(){
207				validateShowAttachmentFile();
208			});
209
210			loadSelectOptions(
211				'qlk_carnejoven_get_identification_types',
212				$("#qlk-carnejoven-card-application-renewal-form select[name=identification_type_id]"),
213			);
214			
215			loadSelectOptions(
216				'qlk_carnejoven_get_provinces',
217				$("#qlk-carnejoven-card-application-renewal-form select[name=province_id]"),
218			);
219
220			//On load
221			validateShowDocumentFile();
222			validateShowAttachmentFile();
223		}
224
225	});
226
227	
228
229})( jQuery );

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.