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

skins/cat17/src/scripts/donationCommentForm.js   A

Complexity

Total Complexity 17
Complexity/F 1.31

Size

Lines of Code 103
Function Count 13

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 0
Metric Value
cc 0
wmc 17
c 7
b 0
f 0
nc 16
mnd 1
bc 18
fnc 13
dl 0
loc 103
rs 10
bpm 1.3846
cpm 1.3076
noi 1

8 Functions

Rating   Name   Duplication   Size   Complexity  
A donationCommentForm.js ➔ onFormSubmit 0 4 1
A donationCommentForm.js ➔ setupForm 0 4 1
A donationCommentForm.js ➔ setupFormValidation 0 14 1
A donationCommentForm.js ➔ elementIsInvalid 0 3 1
A donationCommentForm.js ➔ updateElementValidationState 0 14 3
A donationCommentForm.js ➔ onSubmitSuccess 0 10 1
A donationCommentForm.js ➔ onSubmitFailure 0 11 1
B donationCommentForm.js ➔ handleFormSubmission 0 25 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