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