This repository has been archived by the owner on Jun 1, 2021. It is now read-only.
/
followup.joelpurra.js
101 lines (79 loc) · 2.9 KB
/
followup.joelpurra.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
/*!
* @license FollowUp
* Copyright © 2012, 2013 Joel Purra <http://joelpurra.se/>
* Released under MIT, BSD and GPL license. Comply with at least one.
*
* https://github.com/joelpurra/followup
*
* A jQuery plugin to show and hide follow up questions in a form. The
* follow ups are specified through HTML5 [data-*] attributes.
*/
/*jslint white: true, todo: true */
/*global jQuery: true */
// Set up namespace, if needed
var JoelPurra = JoelPurra || {};
(function($, namespace) {
"use strict";
var tag = "FollowUp",
eventNamespace = "." + tag,
fuq = namespace.FollowUp = namespace.FollowUp || {};
fuq.getMode = function($element) {
return $element.attr("data-has-follow-up");
};
fuq.getTarget = function($element) {
var $target, mode = fuq.getMode($element);
switch (mode) {
case "sibling":
$target = $element.parent().find("[data-is-follow-up]");
break;
// case "selector" is the default/fallback
default:
$target = $($element.attr("data-target"));
break;
}
return $target;
};
fuq.getRadioGroup = function($element) {
// TODO: escape name
return $element.closest("form").find(":radio[name=" + $element.attr("name") + "]");
};
fuq.getGroup = function($element) {
var $elements;
if ($element.is(":radio")) {
$elements = fuq.getRadioGroup($element);
} else {
$elements = $element.closest("[data-has-follow-ups]").find("[data-has-follow-up]");
}
return $elements.add($element).filter("[data-has-follow-up]");
};
fuq.showHide = function($element) {
var $target = fuq.getTarget($element);
// TODO: animations? Inline elements do not animate well.
// http://stackoverflow.com/questions/231937/animating-inline-elements-with-jquery
if ($element.is(":checked")) {
$target.show();
// Add or remove follow up required flags
$target.find("[data-is-follow-up-required=required]").attr("required", "required");
} else {
$target.hide();
// Add or remove follow up required flags
$target.find("[data-is-follow-up-required=required]").removeAttr("required");
}
};
fuq.checkGroup = function($group) {
$group.each(function() {
fuq.showHide($(this));
});
};
fuq.check = function() {
var $this = $(this),
$group = fuq.getGroup($this);
fuq.checkGroup($group);
};
$(function() {
// TODO: use document level dynamic filtering to catch dynamically inserted questions/form elements?
var $watch = $("[data-has-follow-ups], [data-has-follow-up]");
$watch.on("change" + eventNamespace, fuq.check);
fuq.checkGroup($watch);
});
}(jQuery, JoelPurra));