This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
conditionize.jquery.js
67 lines (62 loc) · 2.23 KB
/
conditionize.jquery.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
(function($) {
$.fn.conditionize = function(options) {
var settings = $.extend({
hideJS: true
}, options );
$.fn.eval = function(valueIs, valueShould, operator) {
switch(operator) {
case '==':
return valueIs == valueShould;
break;
case '!=':
return valueIs != valueShould;
case '<=':
return valueIs <= valueShould;
case '<':
return valueIs < valueShould;
case '>=':
return valueIs >= valueShould;
case '>':
return valueIs > valueShould;
case 'in':
return valueIs !== '' && valueShould.includes(valueIs);
}
}
$.fn.showOrHide = function(listenTo, listenFor, operator, $section) {
if ($(listenTo).is('select, input[type=text]') && $.fn.eval($(listenTo).val(), listenFor, operator)) {
$section.slideDown();
}
else if ($(listenTo + ":checked").filter(function(idx, elem){return $.fn.eval(elem.value, listenFor, operator);}).length > 0) {
$section.slideDown();
}
else {
$section.slideUp();
$section.find('select, input').each(function(){
if ( ($(this).attr('type')=='radio') || ($(this).attr('type')=='checkbox') ) {
$(this).prop('checked', false).trigger('change');
}
else{
$(this).val('').trigger('change');
}
});
}
}
return this.each( function() {
var cleanSelector = $(this).data('cond-option').toString().replace(/(:|\.|\[|\]|,)/g, "\\$1");
var listenTo = (cleanSelector.substring(0,1)=='#'?cleanSelector:"[name=" + cleanSelector + "]");
var listenFor = $(this).data('cond-value');
var operator = $(this).data('cond-operator') ? $(this).data('cond-operator') : '==';
var $section = $(this);
//Set up event listener
$(listenTo).on('change', function() {
$.fn.showOrHide(listenTo, listenFor, operator, $section);
});
//If setting was chosen, hide everything first...
if (settings.hideJS) {
$(this).hide();
}
//Show based on current value on page load
$.fn.showOrHide(listenTo, listenFor, operator, $section);
});
}
}(jQuery));