1
|
|
|
;(function ($, window, document, undefined) { |
|
|
|
|
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) { |
|
|
|
|
39
|
|
|
self.updateChild(); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
// Refresh on load |
43
|
|
|
self.updateChild(); |
44
|
|
|
}, |
45
|
|
|
getFieldValue: function (element) { |
46
|
|
|
var value = null; |
|
|
|
|
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); |
|
|
|
|
61
|
|
|
|
62
|
|
|
return option.val() != ''; |
|
|
|
|
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
|
|
|
|
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.