-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathjquery.dependent-questions.js
193 lines (169 loc) · 6.25 KB
/
jquery.dependent-questions.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*!
* Dependent questions jQuery plugin.
*
* Author: [email protected]
*
* Written using boilerplate code from
* http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
*
* © 2012 James Sinclair
* MIT License.
*/
/*jslint nomen:true, browser:true*/
/*globals jQuery*/
;
(function ($, undefined) {
"use strict";
// See http://bit.ly/rc0Nzl for reasoning on why window, document and undefined
// are passed in here.
// Return true if haystack contains needle
function contains(haystack, needle) {
var len = haystack.length,
i = 0;
for (; i < len; i++) {
if (haystack[i] === needle) {
return true;
}
}
return false;
}
// Configuration and defaults.
var pluginName = "dependentQuestions",
defaults = {
"effect": "slide",
// Must be one of 'slide' or 'fade'
"duration": "fast"
},
effectsMap = {
"slide": {
"show": "slideDown",
"hide": "slideUp"
},
"fade": {
"show": "fadeIn",
"hide": "fadeOut"
}
};
// The actual plugin constructor
function QuestionToggler(element, options) {
this.element = element;
// Handle options.
this.options = $.extend({}, defaults, options);
this._defaults = defaults;
this._name = pluginName;
this.init();
}
// Initialisation. Parses all the 'data-depends-on' attributes and sets event
// listeners.
QuestionToggler.prototype.init = function () {
// Find all elements that have a 'data-depends-on' attribute.
var questions, dataRe, dependentsMap, effect, duration;
effect = this.options.effect;
duration = this.options.duration;
dependentsMap = {};
// Regular expression for parsing data attribute.
dataRe = new RegExp("([^=]*)=(.*)");
// Fetch all the elements with 'data-depends-on' attributes.
if (!this.element.find) { this.element = $(this.element); }
questions = this.element.find("[data-depends-on]");
// Get the value of a form element by name, even if it is a radio button
// or checkbox
function getValueByName(inputName) {
var multiInputs, el, elType, multi, checkVals;
multiInputs = ["radio", "checkbox"];
el = $("[name='" + inputName + "']");
if (el.length === 0) { return undefined; }
elType = el.attr("type");
multi = contains(multiInputs, elType);
if (!multi) { return el.val(); }
if (elType === "radio") { return el.filter(":checked").val(); }
if (elType === "checkbox") {
checkVals = $.map(el.filter(":checked"), function (checkbox) {
return $(checkbox).val();
});
return checkVals;
}
}
// Event handler for when a toggling element changes value.
function onTogglerChange(evt) {
var toggler, toggleSpec;
toggler = $(evt.target);
toggleSpec = toggler.data("toggle-spec") || [];
$.each(toggleSpec, function(i, spec) {
var dependents, showVal, val, show, effectFunc, action;
dependents = spec.dependents;
showVal = spec.showVal;
val = getValueByName(toggler.attr("name"));
show = (typeof val === "object") ?
contains(val, showVal) :
(val === showVal);
action = (show) ? "show" : "hide";
effectFunc = effectsMap[effect][action];
dependents[effectFunc](duration);
});
}
// Create a map of things that need to be toggled.
function addMapEntry(i, element) {
var data, el;
el = $(element);
data = el.data("depends-on");
if (!dependentsMap[data]) {
dependentsMap[data] = el;
} else {
dependentsMap[data] = el.add(dependentsMap[data]);
}
}
questions.each(addMapEntry);
// Create event listeners
function setListener(key, dependents) {
var matches, name, showVal, toggler, toggleSpec;
// Parse the key value
matches = dataRe.exec(key);
if (!matches) { return; }
name = matches[1];
showVal = matches[2];
// Grab the relevant elements
toggler = $("[name='" + name + "']");
toggleSpec = toggler.data("toggle-spec") || [];
toggleSpec.push({
dependents : dependents,
showVal : showVal
});
toggler.data("toggle-spec", toggleSpec);
toggler.change(onTogglerChange);
}
$.each(dependentsMap, setListener);
// Set the initial state of each dependent question to match form state.
function showHideByValue(key, dependents) {
var matches, name, showVal, val, func, show;
// Parse the key value
matches = dataRe.exec(key);
if (!matches) { return; }
name = matches[1];
showVal = matches[2];
// Work out whether or not to show the value
val = getValueByName(name);
if (typeof val === "object") {
show = contains(val, showVal);
} else {
show = (val === showVal);
}
func = (show) ? "show" : "hide";
dependents[func]();
}
$.each(dependentsMap, showHideByValue);
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[pluginName] = function (options) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(
this,
"plugin_" + pluginName,
new QuestionToggler(this, options)
);
}
});
};
}(jQuery, undefined));