PageSourceSearch

https://www.admiraltaverns.co.uk/wp-content/themes/customsite/js/authority-letter.js?ver=1877285073734424

js admiraltaverns.co.uk collected 2026-09-25 06:43:12 UTC 7,508 bytes, 287 lines download raw bytes

1/* global admiral_authority_letter_action */
2
3function coderra_authority_letter_notice(type, message) {
4	var $wrap = jQuery('#coderra_authority_letter_notice');
5
6	if (!$wrap.length) {
7		return;
8	}
9
10	$wrap.removeClass('d-none alert-success alert-danger alert-info');
11
12	if ('success' === type) {
13		$wrap.addClass('alert alert-success');
14	} else if ('error' === type) {
15		$wrap.addClass('alert alert-danger');
16	} else {
17		$wrap.addClass('alert alert-info');
18	}
19
20	$wrap.text(message);
21}
22
23function coderra_authority_letter_set_submitting(isSubmitting) {
24	var $btn = jQuery('#coderra_authority_letter_submit');
25
26	if (!$btn.length) {
27		return;
28	}
29
30	if (isSubmitting) {
31		$btn.prop('disabled', true).addClass('disabled').attr('aria-busy', 'true');
32		return;
33	}
34
35	$btn.prop('disabled', false).removeClass('disabled').removeAttr('aria-busy');
36}
37
38function coderra_authority_letter_submit(e) {
39	e.preventDefault();
40
41	var $form = jQuery(this);
42
43	coderra_authority_letter_notice('info', 'Submitting...');
44	coderra_authority_letter_set_submitting(true);
45
46	var payload = $form.serializeArray();
47	payload.push({ name: 'action', value: 'coderra_authority_letter_submit' });
48	payload.push({ name: 'nonce', value: admiral_authority_letter_action.nonce });
49
50	console.log('ajax_url', admiral_authority_letter_action && admiral_authority_letter_action.ajax_url);
51	console.log('nonce', admiral_authority_letter_action && admiral_authority_letter_action.nonce);
52	console.log('payload', payload);
53
54	jQuery.ajax({
55		url: admiral_authority_letter_action.ajax_url,
56		method: 'POST',
57		dataType: 'json',
58		data: jQuery.param(payload)
59	})
60		.done(function (resp) {
61			if (resp && resp.success) {
62				coderra_authority_letter_notice(
63					'success',
64					(resp.data && resp.data.message) ? resp.data.message : 'Submitted successfully.'
65				);
66				// $form[0].reset();
67				// If using select2:
68				// jQuery('#coderra_house_id').val('').trigger('change');
69				return;
70			}
71
72			coderra_authority_letter_notice(
73				'error',
74				(resp && resp.data && resp.data.message) ? resp.data.message : 'Submission failed.'
75			);
76		})
77		.fail(function (xhr) {
78			var msg = 'Submission failed.';
79
80			// Try to parse the JSON response body (WP sends JSON even on 400).
81			if (xhr && xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
82				msg = xhr.responseJSON.data.message;
83			} else if (xhr && xhr.responseText) {
84				// Sometimes helpful when nonce fails (HTML) or server echoes something unexpected.
85				msg = xhr.responseText;
86			}
87
88			coderra_authority_letter_notice('error', msg);
89		})
90		.always(function () {
91			coderra_authority_letter_set_submitting(false);
92		});
93}
94
95jQuery(function () {
96	var $form = jQuery('#coderra_authority_letter_form');
97	if ($form.length) {
98		$form.on('submit', coderra_authority_letter_submit);
99	}
100});
101
102/* global jQuery */
103
104(function ($) {
105	'use strict';
106
107	var currentMode = 'unset'; // 'unset' | 'company' | 'sole'
108
109	function coderra_party_get_side($el) {
110		var $side = $el.closest('[data-party-side]');
111		return $side.length ? String($side.data('party-side')) : '';
112	}
113
114	function coderra_party_has_any_value($wrap) {
115		var hasValue = false;
116
117		$wrap.find('input, select, textarea').each(function () {
118			var $field = $(this);
119
120			if ($field.prop('disabled')) {
121				return;
122			}
123
124			var type = String($field.attr('type') || '').toLowerCase();
125			var tag = String($field.prop('tagName') || '').toLowerCase();
126
127			if (type === 'hidden' || type === 'button' || type === 'submit' || type === 'reset') {
128				return;
129			}
130
131			if (type === 'checkbox' || type === 'radio') {
132				if ($field.is(':checked')) {
133					hasValue = true;
134					return false;
135				}
136				return;
137			}
138
139			if (tag === 'select') {
140				var vSel = $field.val();
141				if (Array.isArray(vSel) ? vSel.length : String(vSel || '').trim() !== '') {
142					hasValue = true;
143					return false;
144				}
145				return;
146			}
147
148			if (String($field.val() || '').trim() !== '') {
149				hasValue = true;
150				return false;
151			}
152		});
153
154		return hasValue;
155	}
156
157	function coderra_party_clear_fields($wrap) {
158		$wrap.find('input, select, textarea').each(function () {
159			var $field = $(this);
160			var type = String($field.attr('type') || '').toLowerCase();
161			var tag = String($field.prop('tagName') || '').toLowerCase();
162
163			if (type === 'hidden' || type === 'button' || type === 'submit' || type === 'reset') {
164				return;
165			}
166
167			if (type === 'checkbox' || type === 'radio') {
168				$field.prop('checked', false).trigger('change');
169				return;
170			}
171
172			if (tag === 'select') {
173				$field.val('').trigger('change'); // works for select2 too
174				return;
175			}
176
177			$field.val('').trigger('input').trigger('change');
178		});
179	}
180
181	function coderra_party_set_enabled($wrap, isEnabled) {
182		$wrap.toggleClass('coderra-side-disabled', !isEnabled);
183
184		$wrap.find('input, select, textarea, button').each(function () {
185			var $field = $(this);
186			var type = String($field.attr('type') || '').toLowerCase();
187
188			// Don't disable the global toggle buttons if they live inside table (avoid foot-gun).
189			if ($field.is('#coderra_party_toggle_company, #coderra_party_toggle_sole')) {
190				return;
191			}
192
193			// Hidden fields can remain enabled.
194			if (type === 'hidden') {
195				return;
196			}
197
198			$field.prop('disabled', !isEnabled);
199		});
200	}
201
202	function coderra_party_sync_toggle(mode) {
203		$('#coderra_party_toggle_company')
204			.toggleClass('active', mode === 'company')
205			.attr('aria-pressed', mode === 'company' ? 'true' : 'false');
206
207		$('#coderra_party_toggle_sole')
208			.toggleClass('active', mode === 'sole')
209			.attr('aria-pressed', mode === 'sole' ? 'true' : 'false');
210	}
211
212	function coderra_party_apply_mode(mode) {
213		var $company = $('[data-party-side="company"]');
214		var $sole = $('[data-party-side="sole"]');
215
216		if (!$company.length || !$sole.length) {
217			return;
218		}
219
220		currentMode = mode;
221
222		// Persist for server-side.
223		$('#coderra_party_type').val(mode === 'unset' ? '' : mode);
224
225		// Update toggle UI.
226		coderra_party_sync_toggle(mode);
227
228		// Apply behaviour.
229		if (mode === 'unset') {
230			coderra_party_set_enabled($company, true);
231			coderra_party_set_enabled($sole, true);
232			return;
233		}
234
235		if (mode === 'company') {
236			coderra_party_clear_fields($sole);
237			coderra_party_set_enabled($sole, false);
238			coderra_party_set_enabled($company, true);
239			return;
240		}
241
242		if (mode === 'sole') {
243			coderra_party_clear_fields($company);
244			coderra_party_set_enabled($company, false);
245			coderra_party_set_enabled($sole, true);
246		}
247	}
248
249	function coderra_party_auto_detect_and_lock($changedEl) {
250		if (currentMode !== 'unset') {
251			return;
252		}
253
254		var side = coderra_party_get_side($changedEl);
255		if (!side) {
256			return;
257		}
258
259		var $sideWrap = $changedEl.closest('[data-party-side="' + side + '"]');
260		if ($sideWrap.length && coderra_party_has_any_value($sideWrap)) {
261			coderra_party_apply_mode(side);
262		}
263	}
264
265	$(function () {
266		// Initial: unset (both enabled).
267		coderra_party_apply_mode('unset');
268
269		// Auto-lock once user starts filling.
270		$(document).on('input change', '[data-party-side] input, [data-party-side] select, [data-party-side] textarea', function () {
271			coderra_party_auto_detect_and_lock($(this));
272		});
273
274		// Manual toggle.
275		$(document).on('click', '#coderra_party_toggle_company, #coderra_party_toggle_sole', function (e) {
276			e.preventDefault();
277
278			var nextMode = String($(this).data('party') || '');
279			if (!nextMode) {
280				return;
281			}
282
283			// Switching mode always clears+disables the other side.
284			coderra_party_apply_mode(nextMode);
285		});
286	});
287})(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.