1$(document).ready((function($jq) { 2 $jq.fxbConditions = function(el, options) { 3 this.el = el; 4 this.$el = $jq(el); 5 this.options = $jq.extend({}, $jq.fxbConditions.defaultOptions, options); 6 }, 7 $jq.extend($jq.fxbConditions, 8 { 9 defaultOptions: { 10 fieldConditions: [], 11 animate: true 12 }, 13 14 helpers: { 15 normalize: function(value, preserveCase) { 16 if (value == null) { 17 return ""; 18 } 19 20 return preserveCase ? String(value) : String(value).toLowerCase(); 21 }, 22 23 toNumber: function(value) { 24 value = Number(value); 25 return isNaN(value) ? undefined : value; 26 }, 27 28 indexOf: function(str, value, startIndex, preserveCase) { 29 str = this.normalize(str, preserveCase); 30 value = this.normalize(value, preserveCase); 31 32 return str.indexOf(value, startIndex); 33 }, 34 35 endsWith: function(str, value, preserveCase) { 36 str = this.normalize(str, preserveCase); 37 value = this.normalize(value, preserveCase); 38 39 var lengthDiff = str.length - value.length; 40 return lengthDiff >= 0 && str.substring(lengthDiff) === value; 41 } 42 }, 43 44 actions: { 45 show: function($target) { 46 if (this.loaded && this.options.animate) { 47 $target.slideDown(); 48 } else { 49 $target.show(); 50 } 51 52 this.setRequired($target); 53 }, 54 55 hide: function($target) { 56 if (this.loaded && this.options.animate) { 57 if ($target.is(":visible")) { 58 $target.slideUp(); 59 } 60 } else { 61 $target.hide(); 62 } 63 64 this.setRequired($target); 65 }, 66 67 enable: function($target) { 68 if ($target.is("input,select,textarea,button")) { 69 $target.prop("disabled", false); 70 this.setRequired($target); 71 } 72 }, 73 74 disable: function($target) { 75 if ($target.is("input,select,textarea,button")) { 76 $target.prop("disabled", true); 77 this.setRequired($target); 78 } 79 }, 80 81 "go to page": function($target, action, conditionsResult) { 82 $target.each(function(idx, target) { 83 if (target.name && 84 target.name.length && 85 $jq(target).is("input[type='submit'], button[type='submit']")) { 86 var $nextPageEl = this.$el.find("[name=\"" + target.name + "\"][data-sc-next-page]"); 87 if (!$nextPageEl.length) { 88 if (conditionsResult && action.value) { 89 var $currentEl = this.$el.find("[name=\"" + target.name + "\"]"); 90 $jq('<input>').attr({ 91 type: 'hidden', 92 name: target.name, 93 value: action.value, 94 'data-sc-next-page': "" 95 }).insertAfter($currentEl.last()); 96 } 97 98 return; 99 } 100 101 var value = action.value; 102 if (!conditionsResult) { 103 value = $nextPageEl.data("sc-next-page"); 104 105 for (var i = (this.executedActions.length - 1); i >= 0; i--) { 106 var prevAction = this.executedActions[i]; 107 if (prevAction.fieldId === action.fieldId && 108 prevAction.conditionsResult && 109 prevAction.actionType.toLowerCase() === "go to page") { 110 value = prevAction.value; 111 break; 112 } 113 } 114 } 115 116 $nextPageEl.val(value); 117 $nextPageEl.prop("disabled", !value); 118 } 119 }.bind(this)); 120 }, 121 122 // action pairs used for finding the opposite action to execute when conditions are not satisfied 123 actionLinks: { 124 show: "hide", 125 enable: "disable", 126 "go to page": "go to page" 127 }, 128 129 addAction: function(actionName, actionFn, oppositeActionName, oppositeActionFn) { 130 if (actionName && actionName.length) { 131 actionName = actionName.toLowerCase(); 132 133 this[actionName] = actionFn; 134 135 if (arguments.length === 2) { 136 return; 137 } 138 139 if (oppositeActionName && oppositeActionName.length) { 140 oppositeActionName = oppositeActionName.toLowerCase(); 141 142 this.actionLinks[actionName] = oppositeActionName; 143 if (arguments.length > 3) { 144 this[oppositeActionName] = oppositeActionFn; 145 } 146 } else { 147 delete this.actionLinks[actionName]; 148 } 149 } 150 }, 151 152 getAction: function(actionName, conditionsResult) { 153 if (actionName && actionName.length) { 154 actionName = actionName.toLowerCase(); 155 156 if (conditionsResult) { 157 return this[actionName]; 158 } 159 160 if (this.actionLinks.hasOwnProperty(actionName)) { 161 var oppositeActionName = this.actionLinks[actionName]; 162 return this[oppositeActionName]; 163 } else { 164 for (var property in this.actionLinks) { 165 if (this.actionLinks.hasOwnProperty(property) && 166 this.actionLinks[property] === actionName) { 167 return this[property]; 168 } 169 } 170 } 171 } 172 173 return this[actionName]; 174 } 175 }, 176 177 operators: { 178 "contains": function(conditionValue, fieldValue) { 179 return $jq.fxbConditions.helpers.indexOf(fieldValue, conditionValue) >= 0; 180 }, 181 182 "does not contain": function(conditionValue, fieldValue) { 183 return $jq.fxbConditions.helpers.indexOf(fieldValue, conditionValue) === -1; 184 }, 185 186 "starts with": function(conditionValue, fieldValue) { 187 return $jq.fxbConditions.helpers.indexOf(fieldValue, conditionValue) === 0; 188 }, 189 190 "does not start with": function(conditionValue, fieldValue) { 191 return $jq.fxbConditions.helpers.indexOf(fieldValue, conditionValue) !== 0; 192 }, 193 194 "ends with": function(conditionValue, fieldValue) { 195 return $jq.fxbConditions.helpers.endsWith(fieldValue, conditionValue); 196 }, 197 198 "does not end with": function(conditionValue, fieldValue) { 199 return !$jq.fxbConditions.helpers.endsWith(fieldValue, conditionValue); 200 }, 201 202 "is equal to": function(conditionValue, fieldValue) { 203 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 204 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 205 if (fieldValue === conditionValue) { 206 return true; 207 } 208 209 if (conditionValue.length) {
210 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 211 if (typeof left === "number") { 212 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 213 return typeof right === "number" && left === right; 214 } 215 } 216 217 return false; 218 }, 219 220 "is not equal to": function(conditionValue, fieldValue) { 221 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 222 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 223 if (fieldValue === conditionValue) { 224 return false; 225 } 226 227 if (conditionValue.length) { 228 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 229 if (typeof left === "number") { 230 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 231 return typeof right !== "number" || left !== right; 232 } 233 } 234 235 return true; 236 }, 237 238 "is greater than": function(conditionValue, fieldValue) { 239 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 240 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 241 242 if (conditionValue.length) { 243 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 244 if (typeof left === "number") { 245 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 246 return typeof right === "number" && right > left; 247 } 248 } 249 250 return fieldValue > conditionValue; 251 }, 252 253 "is greater than or equal to": function(conditionValue, fieldValue) { 254 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 255 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 256 257 if (fieldValue === conditionValue) { 258 return true; 259 } 260 261 if (conditionValue.length) { 262 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 263 if (typeof left === "number") { 264 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 265 return typeof right === "number" && right >= left; 266 } 267 } 268 269 return fieldValue >= conditionValue; 270 }, 271 272 "is less than": function(conditionValue, fieldValue) { 273 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 274 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 275 276 if (conditionValue.length) { 277 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 278 if (typeof left === "number") { 279 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 280 return typeof right === "number" && right < left; 281 } 282 } 283 284 return fieldValue < conditionValue; 285 }, 286 287 "is less than or equal to": function(conditionValue, fieldValue) { 288 conditionValue = $jq.fxbConditions.helpers.normalize(conditionValue); 289 fieldValue = $jq.fxbConditions.helpers.normalize(fieldValue); 290 291 if (fieldValue === conditionValue) { 292 return true; 293 } 294 295 if (conditionValue.length) {
296 var left = $jq.fxbConditions.helpers.toNumber(conditionValue); 297 if (typeof left === "number") { 298 var right = $jq.fxbConditions.helpers.toNumber(fieldValue); 299 return typeof right === "number" && right <= left; 300 } 301 } 302 303 return fieldValue <= conditionValue; 304 }, 305 306 addOperator: function(operatorName, operatorFn) { 307 if (operatorName && operatorName.length) { 308 this[operatorName.toLowerCase()] = operatorFn; 309 } 310 }, 311 312 getOperator: function(operatorName) { 313 return operatorName && operatorName.length ? this[operatorName.toLowerCase()] : null; 314 } 315 }, 316 317 prototype: { 318 initConditions: function(options) { 319 this.options = $jq.extend(true, this.options || {}, options); 320 321 if (!this.options.fieldConditions) { 322 return; 323 } 324 325 var sourceFields = []; 326 this.options.fieldConditions.forEach(function(fieldCondition) { 327 if (!fieldCondition || !fieldCondition.conditions) { 328 return; 329 } 330 331 fieldCondition.conditions.forEach(function(condition) { 332 if (condition.fieldId && sourceFields.indexOf(condition.fieldId) === -1) { 333 sourceFields.push(condition.fieldId); 334 var $source = this.$el.find("[data-sc-field-key=\"" + condition.fieldId + "\"]") 335 .filter(function() { 336 return $jq.fxbConditions.helpers.endsWith(this.name, "value"); 337 }); 338 if ($source.length) { 339 $source.on("change", this.applyConditions.bind(this)); 340 } 341 } 342 }.bind(this)); 343 }.bind(this)); 344 345 this.applyConditions(); 346 this.loaded = true; 347 }, 348 349 applyConditions: function() { 350 if (!this.options.fieldConditions) { 351 return; 352 } 353 354 this.executedActions = []; 355 356 this.options.fieldConditions.forEach(function(fieldCondition) { 357 if (fieldCondition && fieldCondition.actions && fieldCondition.actions.length) { 358 var conditionsResult = this.evaluateConditions(fieldCondition); 359 fieldCondition.actions.forEach(function(action) { 360 this.executeAction(action, conditionsResult); 361 }.bind(this)); 362 } 363 }.bind(this)); 364 }, 365 366 setRequired: function($targets) { 367 $targets.each(function(idx, target) { 368 var $target = $jq(target);
369 if ($target.is("input:not([type='submit']), select, textarea")) { 370 var name = target.name; 371 if (!$jq.fxbConditions.helpers.endsWith(name, "value")) 372 return; 373 374 name = name.slice(0, -(name.length - name.lastIndexOf(".") - 1)) + "Required"; 375 var $requiredEl = this.$el.find("[name=\"" + name + "\"][data-sc-conditions-required]"), 376 isNotAccessible = $target.is(":hidden") || 377 $target.css("visibility") === "hidden" || 378 target.disabled; 379 380 if (!$requiredEl.length) { 381 if (isNotAccessible) { 382 var $currentEl = this.$el.find("[name=\"" + target.name + "\"]"); 383 $jq('<input>').attr({ 384 type: 'hidden', 385 name: name, 386 value: false, 387 "data-sc-conditions-required": "" 388 }).insertAfter($currentEl.last()); 389 } 390 391 return; 392 } 393 394 $requiredEl.val(false); 395 $requiredEl.prop("disabled", !isNotAccessible); 396 } else { 397 this.setRequired($target.find("input:not([type='submit']), select, textarea")); 398 } 399 }.bind(this)); 400 }, 401 402 executeAction: function(action, conditionsResult) { 403 if (action && action.fieldId && action.actionType) { 404 var $target = this.$el.find("[data-sc-field-key=\"" + action.fieldId + "\"]"); 405 if ($target.length) { 406 var actionFn = $jq.fxbConditions.actions.getAction(action.actionType, conditionsResult); 407 if (actionFn && typeof actionFn === "function") { 408 actionFn.call(this, $target, action, conditionsResult); 409 var executedAction = $jq.extend(true, 410 action, 411 { 412 conditionsResult: conditionsResult 413 }); 414 this.executedActions.push(executedAction); 415 } 416 } 417 } 418 }, 419 420 evaluateConditions: function(fieldCondition) { 421 if (!fieldCondition || !fieldCondition.conditions) return true; 422 423 var matchType = (fieldCondition.matchType || "").toLowerCase(); 424 switch (matchType) { 425 case "all": 426 return fieldCondition.conditions.every(this.isConditionSatisfied.bind(this)); 427 case "any": 428 default: 429 return fieldCondition.conditions.some(this.isConditionSatisfied.bind(this)); 430 } 431 }, 432 433 getValueList: function(fieldId) { 434 var $fieldEl = this.$el.find("[data-sc-field-key=\"" + fieldId + "\"]").filter(function() { 435 return $jq.fxbConditions.helpers.endsWith(this.name, "value"); 436 }); 437 438 var $listElements = $fieldEl.filter(function(idx, element) { 439 return element.type === "checkbox" || element.type === "radio"; 440 }); 441 442 var value; 443 if ($listElements.length) { 444 if ($listElements.length > 1) { 445 value = $listElements.filter(":checked").map(function() { 446 return $jq(this).val(); 447 }).get(); 448 if (!value.length) { 449 value.push(""); 450 } 451 } else { 452 value = [$listElements[0].checked ? "true" : "false"]; 453 } 454 } else { 455 value = [$fieldEl.val()]; 456 } 457 458 return value; 459 }, 460 461 isConditionSatisfied: function(condition) { 462 if (condition && condition.operator) { 463 var operatorFn = $jq.fxbConditions.operators.getOperator(condition.operator); 464 if (operatorFn && typeof operatorFn === "function") { 465 466 var valueList = this.getValueList(condition.fieldId); 467 var result = condition.operator === "is not equal to" 468 ? valueList.every(operatorFn.bind(this, condition.value)) 469 : valueList.some(operatorFn.bind(this, condition.value)); 470 return result; 471 }; 472 } 473 474 return false; 475 } 476 } 477 }); 478 479 $jq.fn.init_fxbConditions = function(options) { 480 return this.each(function() { 481 var conditions = $jq.data(this, "fxbForms.conditions"); 482 if (conditions) { 483 conditions.initConditions(options); 484 } else { 485 conditions = new $jq.fxbConditions(this, options); 486 $jq.data(this, "fxbForms.conditions", conditions); 487 conditions.initConditions(); 488 } 489 }); 490 }; 491})(jQuery));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.