-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.input-depends-on.js
100 lines (79 loc) · 2.17 KB
/
jquery.input-depends-on.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* jQuery inputDependsOn plugin
*
* Version: 0.1.0
* Date: Wed Aug 4 12:07:29 2010 -0600
*
* Copyright 2010, Paul Macek
* http://github.com/macek/jquery-input-depends-on
*
* Licensed under MIT
* http://www.opensource.org/licenses/mit-license.php
*
*/
(function($){
$.fn.inputDependsOn = function(id, values, options){
var self = this;
// methods
this.disable = function(e){
return e.addClass(options.cssClass).attr({disabled: true}).trigger("disable");
};
this.enable = function(e){
return e.removeClass(options.cssClass).attr({disabled: null}).trigger("enable");
};
var find_matching_value = function(input){
var ret = null;
// if parent value is an array...
if(input instanceof Array){
$.each(input, function(k, v){
if(v in values){
ret = v;
}
});
}
// parent value is a non-array
else {
if(input in values){
ret = input;
}
}
return ret;
};
// options
options = $.extend({}, $.fn.inputDependsOn.defaults, options || {});
// find parent
var parent = $(id);
// iterate through selected children
this.each(function(){
var child = $(this);
// parent binds
parent
.bind("change", function(){
var v = find_matching_value(parent.val());
// parent must be enabled to change children
if(parent.is(":not(:disabled)") && v != null){
// call private method
// e.g., this["enable"](child)
self[values[v]](child);
}
else {
self[options.init](child);
}
// trigger change event down the dependency tree
child.change();
})
.bind("disable", function(){
// trigger disable event down the dependency tree
self.disable(child);
})
;
});
// init
parent.change();
return this;
};
$.fn.inputDependsOn.defaults = {
init: "disable",
cssClass: "disabled"
};
})(jQuery);