Passed
Branch develop (893f38)
by Hans Erik
06:54
created

Plugin.updateChild   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 1
nc 1
nop 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 18 2
1
;(function ($, window, document, undefined) {
0 ignored issues
show
Unused Code introduced by
The parameter undefined 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...
Unused Code introduced by
The parameter window 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...
Unused Code introduced by
The parameter document 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...
2
3
	// Create the defaults once
4
	var pluginName = "childlist";
5
6
	var defaults = {
7
		parentSelector : '.js-childlist-parent',
8
		parentVarName  : null,
9
		parentOnChange : true,
10
		childSelector  : '.js-childlist-child',
11
		ajaxUrl        : null
12
	};
13
14
	// The actual plugin constructor
15
	function Plugin(element, options) {
16
		this.element = element;
17
		this.options = $.extend({}, defaults, options);
18
		this._defaults = defaults;
19
20
		// Fields
21
		this.childField  = $(this.element);
22
		this.parentField = $(this.options.parentSelector);
23
24
		// Initial values
25
		this.parentValue = this.getFieldValue(this.parentField);
26
		this.childValue  = this.getFieldValue(this.element);
27
28
		// Selector values
29
		this._name = pluginName;
30
31
		this.init();
32
	}
33
34
	Plugin.prototype = {
35
		init: function () {
36
			var self = this;
37
38
			self.parentField.change(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...
39
				self.updateChild();
40
			});
41
42
			// Refresh on load
43
			self.updateChild();
44
		},
45
		getFieldValue: function (element) {
46
			var value = null;
0 ignored issues
show
Unused Code introduced by
The assignment to value seems to be never used. If you intend to free memory here, this is not necessary since the variable leaves the scope anyway.
Loading history...
47
48
			if ($(element).is('select')) {
49
				var option = $(element).find('option:selected');
50
				value = option.val();
51
			} else {
52
				value = $(element).val();
53
			}
54
55
			return value;
56
		},
57
		isFieldActive: function (element) {
58
			var self = this;
59
60
			var value = self.getFieldValue(element);
0 ignored issues
show
Unused Code introduced by
The variable value seems to be never used. Consider removing it.
Loading history...
61
62
			return option.val() != '';
0 ignored issues
show
Bug introduced by
The variable option seems to be never declared. If this is a global, consider adding a /** global: option */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
63
		},
64
		updateChild: function() {
65
			var self = this;
66
67
			self.parentValue  = self.getFieldValue(self.parentField);
68
69
			// Execute AJAX query
70
			$.ajax({
71
				url: self.options.ajaxUrl,
72
				type: "POST",
73
				dataType: "json",
74
				cache: false,
75
				data: self.options.parentVarName + "=" + self.parentValue,
76
				success: function(data){
77
					var options = "";
78
					if (data !== null)
79
					{
80
						$.each(data, function(index, item) {
81
							options += '<option value="' + item['value'] + '"';
82
							if (item['value'] === self.childValue)
83
							{
84
								options += ' selected="selected"';
85
							}
86
							options += ' >' + item['text'] + '</option>';
87
						});
88
					}
89
90
					// Fill the child select
91
					self.childField.empty().append(options);
92
					self.childField.trigger('liszt:updated');
93
				}
94
			});
95
		},
96
		resetField: function (element) {
97
			$(element).val('');
98
			$(element).trigger('liszt:updated');
99
		},
100
		submitForm: function() {
101
			this.theForm.submit();
102
		}
103
	};
104
105
	// A really lightweight plugin wrapper around the constructor,
106
	// preventing against multiple instantiations
107
	$.fn[pluginName] = function (options) {
108
		return this.each(function () {
109
			if (!$.data(this, "plugin_" + pluginName)) {
110
				$.data(this, "plugin_" + pluginName, new Plugin(this, options));
111
			}
112
		});
113
	};
114
115
})(jQuery, window, document);
116