| 1 |
/* |
| 2 |
* React JQuery plugin |
| 3 |
* |
| 4 |
* Copyright 2011, Nathan Davis Olds |
| 5 |
* Distributed under the MIT license. |
| 6 |
*/ |
| 7 |
|
| 8 |
(function($){ |
| 9 |
|
| 10 |
var React = { |
| 11 |
reactTo: function(selector) { |
| 12 |
var $elements = $(selector), |
| 13 |
$reactor_element = $(this); |
| 14 |
|
| 15 |
var _proxy_event = function() { |
| 16 |
$reactor_element.trigger('react.reactor'); |
| 17 |
}; |
| 18 |
|
| 19 |
$elements.filter(':not(:text), :not(:password)').on('change.reactor', _proxy_event); |
| 20 |
$elements.filter(':text.date-picker').on('change.reactor', _proxy_event); |
| 21 |
$elements.filter(':text').on('keyup.reactor', _proxy_event); |
| 22 |
$elements.filter(':password').on('keyup.reactor', _proxy_event); |
| 23 |
|
| 24 |
return this; |
| 25 |
}, |
| 26 |
|
| 27 |
reactIf: function(sel, exp_func) { |
| 28 |
var $sel = $(sel), |
| 29 |
args = Array.prototype.slice.call( arguments, 2 ); |
| 30 |
|
| 31 |
var _func = function() { |
| 32 |
if ($.isFunction(exp_func)) { |
| 33 |
return exp_func.apply($sel); |
| 34 |
} else { |
| 35 |
var _returned = $.fn.reactor.helpers[exp_func].apply($sel, args); |
| 36 |
|
| 37 |
if ($.isFunction(_returned)) { |
| 38 |
return _returned.apply($sel) |
| 39 |
} else { |
| 40 |
return _returned; |
| 41 |
} |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
this.each(function() { |
| 46 |
var $reactor = $(this); |
| 47 |
|
| 48 |
if (!$reactor.hasClass('reactor')) { |
| 49 |
$reactor.reactor(); |
| 50 |
}; |
| 51 |
|
| 52 |
var conditions_arry = $reactor.data('conditions.reactor'); |
| 53 |
if (!$.isArray(conditions_arry)) { conditions_arry = [] }; |
| 54 |
|
| 55 |
conditions_arry.push(_func); |
| 56 |
|
| 57 |
$(this).data('conditions.reactor', conditions_arry); |
| 58 |
}); |
| 59 |
|
| 60 |
$(this).reactTo(sel); |
| 61 |
|
| 62 |
return this; |
| 63 |
}, |
| 64 |
|
| 65 |
react: function() { |
| 66 |
this.each(function() { |
| 67 |
$(this).trigger('react.reactor') |
| 68 |
}); |
| 69 |
|
| 70 |
return this; |
| 71 |
}, |
| 72 |
|
| 73 |
reactor: function(options) { |
| 74 |
var settings = $.extend({}, $.fn.reactor.defaults, options); |
| 75 |
|
| 76 |
this.each(function() { |
| 77 |
var $element = $(this); |
| 78 |
|
| 79 |
if (!$.isArray($element.data('conditions.reactor'))) { |
| 80 |
$element |
| 81 |
.data('conditions.reactor', []) |
| 82 |
.addClass('reactor'); |
| 83 |
} |
| 84 |
|
| 85 |
var isReactionary = function() { |
| 86 |
var conditionalArray = $(this).data('conditions.reactor'); |
| 87 |
var r = true; |
| 88 |
|
| 89 |
$.each(conditionalArray, function() { |
| 90 |
r = this.call(); |
| 91 |
return r; // short circuits the loop when any value is false |
| 92 |
}); |
| 93 |
|
| 94 |
return r; |
| 95 |
} |
| 96 |
|
| 97 |
var reaction = function(evt) { |
| 98 |
if (isReactionary.apply(this)) { |
| 99 |
settings.compliant.apply($element); |
| 100 |
} else { |
| 101 |
settings.uncompliant.apply($element); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
$element.on('react.reactor', reaction); |
| 106 |
}); |
| 107 |
|
| 108 |
return this; |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
$.fn.reactTo = React.reactTo; |
| 113 |
$.fn.reactIf = React.reactIf; |
| 114 |
$.fn.react = React.react; |
| 115 |
$.fn.reactor = React.reactor; |
| 116 |
|
| 117 |
$.fn.reactor.defaults = { |
| 118 |
compliant: function() { |
| 119 |
$(this).show(); |
| 120 |
}, |
| 121 |
uncompliant: function() { |
| 122 |
$(this).hide(); |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
$.fn.reactor.helpers = { |
| 127 |
NotBlank: function() { |
| 128 |
return( this.val().toString() != "" ); |
| 129 |
}, |
| 130 |
|
| 131 |
Blank: function() { |
| 132 |
return( this.val().toString() == "" ); |
| 133 |
}, |
| 134 |
|
| 135 |
HasElements: function() { |
| 136 |
return this.size() > 0; |
| 137 |
}, |
| 138 |
|
| 139 |
Disabled: function() { |
| 140 |
return( this.filter(':disabled').length > 0 ); |
| 141 |
}, |
| 142 |
|
| 143 |
Enabled: function() { |
| 144 |
return( this.filter(':enabled').length > 0 ); |
| 145 |
}, |
| 146 |
|
| 147 |
IsChecked: function() { |
| 148 |
return this.is(':checked'); |
| 149 |
}, |
| 150 |
|
| 151 |
IsNotChecked: function() { |
| 152 |
return !this.is(':checked'); |
| 153 |
}, |
| 154 |
|
| 155 |
EqualTo: function(matchStr) { |
| 156 |
var _func = function() { |
| 157 |
var v = this.val(); |
| 158 |
if (v) { |
| 159 |
return( v.toString() == matchStr.toString() ); |
| 160 |
} else { |
| 161 |
return false; |
| 162 |
} |
| 163 |
} |
| 164 |
return _func; |
| 165 |
}, |
| 166 |
|
| 167 |
NotEqualTo: function(matchStr) { |
| 168 |
var _func = function() { |
| 169 |
var v = this.val(); |
| 170 |
|
| 171 |
if (v) { |
| 172 |
return( v.toString() != matchStr.toString() ); |
| 173 |
} else { |
| 174 |
return true; |
| 175 |
} |
| 176 |
} |
| 177 |
return _func; |
| 178 |
}, |
| 179 |
|
| 180 |
NumberOfDigitsIs: function(number) { |
| 181 |
var comparisonString = this.val().toString().replace(/[^\d]+/g,''), |
| 182 |
passing = false, |
| 183 |
length = comparisonString.length; |
| 184 |
|
| 185 |
for(index in arguments) { |
| 186 |
if (length == arguments[index]) { |
| 187 |
passing = true; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
return passing; |
| 192 |
}, |
| 193 |
|
| 194 |
LessThan: function(number) { |
| 195 |
var _func = function() { |
| 196 |
var v = this.filter('span').length > 0 ? this.text() : this.val(); |
| 197 |
return( parseInt(v) < number ); |
| 198 |
} |
| 199 |
return _func; |
| 200 |
}, |
| 201 |
|
| 202 |
MoreThan: function(number) { |
| 203 |
var _func = function() { |
| 204 |
var v = this.filter('span').length > 0 ? this.text() : this.val(), |
| 205 |
result = (parseInt(v) > number) |
| 206 |
|
| 207 |
return(result); |
| 208 |
} |
| 209 |
return _func; |
| 210 |
}, |
| 211 |
|
| 212 |
Between: function(min, max) { |
| 213 |
var _func = function() { |
| 214 |
var v = this.val(); |
| 215 |
return(!(v && (parseInt(v) > max || parseInt(v) < min))); |
| 216 |
} |
| 217 |
return _func; |
| 218 |
}, |
| 219 |
|
| 220 |
BetweenSameLength: function(min, max) { |
| 221 |
var len = min.toString().length; |
| 222 |
var _func = function() { |
| 223 |
var v = this.val(); |
| 224 |
return(!(v && v.length == len && (parseInt(v) > max || parseInt(v) < min))); |
| 225 |
} |
| 226 |
return _func; |
| 227 |
}, |
| 228 |
|
| 229 |
HasValueWhenVisible: function() { |
| 230 |
if (this.is(':visible')) { |
| 231 |
return( this.val().toString() != "" && parseFloat(this.val()) != 0.0); |
| 232 |
} else { |
| 233 |
return true; |
| 234 |
} |
| 235 |
} |
| 236 |
}; |
| 237 |
})(jQuery); |