Completed
Push — master ( 4794f7...9a9ebc )
by Jeroen De
317:08 queued 252:08
created

donationCommentForm.js ➔ handleFormSubmission   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
c 3
b 0
f 0
nc 1
dl 0
loc 25
rs 8.8571
nop 1

2 Functions

Rating   Name   Duplication   Size   Complexity  
A donationCommentForm.js ➔ ... ➔ $.ajax.success 0 8 2
A donationCommentForm.js ➔ ... ➔ $.ajax.error 0 3 1
1
(function ($) {
2
3
	var form = $('#comment-form');
4
5
	var inputElements = form.find('input, textarea');
6
	var submitButton = form.find('input[type="submit"]');
7
8
	setupForm();
9
10
	function setupForm() {
11
		setupFormValidation();
12
		form.bind( 'submit', handleFormSubmission );
13
	}
14
15
	function setupFormValidation() {
16
		form.submit(onFormSubmit);
17
		submitButton.click(onFormSubmit);
18
19
		inputElements.keypress(function () {
20
			$(this).data('data-entered', true);
21
		});
22
23
		inputElements.blur(function () {
24
			if ($(this).data('data-entered')) {
25
				updateElementValidationState.apply( this );
26
			}
27
		});
28
	}
29
30
	function onFormSubmit() {
31
		inputElements.each(updateElementValidationState);
32
		return inputElements.filter(elementIsInvalid).length === 0;
33
	}
34
35
	function updateElementValidationState() {
36
		if ($(this).val() === "" || !this.checkValidity()) {
37
			$(this).removeClass('valid');
38
			$(this).parent().removeClass('valid');
39
			$(this).addClass('invalid');
40
			$(this).parent().addClass('invalid');
41
		}
42
		else {
43
			$(this).removeClass('invalid');
44
			$(this).parent().removeClass('invalid');
45
			$(this).addClass('valid');
46
			$(this).parent().addClass('valid');
47
		}
48
	}
49
50
	function elementIsInvalid() {
51
		return $(this).val() === "" || !this.checkValidity();
52
	}
53
54
	function handleFormSubmission( event ) {
55
		event.preventDefault();
56
57
		submitButton.attr( 'disabled', 'disabled' );
58
		submitButton.addClass( 'btn-unactive' );
59
60
		form.find( '.message' ).remove();
61
62
		$.ajax( form.attr( 'action' ), {
63
			data: $( this ).serialize(),
64
			dataType: 'json',
65
			type: 'POST',
66
			success: function( response ) {
67
				if ( response.status === 'OK' ) {
68
					onSubmitSuccess( response.message );
69
				}
70
				else {
71
					onSubmitFailure( response.message );
72
				}
73
			},
74
			error: function ( e ) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
75
				onSubmitFailure();
76
			}
77
		});
78
	}
79
80
	function onSubmitSuccess( message ) {
81
		submitButton.after(
82
			$( '<div />' )
83
				.addClass( 'message' )
84
				.addClass( 'success-message' )
85
				.text( message || 'Vielen Dank! Die Nachricht wurde verschickt!' ) // TODO: is this default needed?
86
		);
87
88
		$( '#cancel-link' ).text( 'Zurück zur Spendenbestätigung' ); // TODO: i18n
89
	}
90
91
	function onSubmitFailure( message ) {
92
		submitButton.removeAttr( 'disabled' );
93
		submitButton.removeClass( 'btn-unactive' );
94
95
		submitButton.after(
96
			$( '<div />' )
97
				.addClass( 'message' )
98
				.addClass( 'error-message' )
99
				.text( message || 'Die Nachricht konnte auf Grund eines Fehlers nicht verschickt werden.' ) // TODO: is this default needed?
100
		);
101
	}
102
103
})(jQuery);
104