| 1 | jQuery(function ($) { |
|---|
| 2 | |
|---|
| 3 | window.get_selector = function (field_name) { |
|---|
| 4 | var selector = '#field-' + field_name; |
|---|
| 5 | if (field_name == 'owner' && $(selector).length != 1) { |
|---|
| 6 | selector = '[id$="_reassign_owner"]' |
|---|
| 7 | } else if (field_name == 'resolution' && $(selector).length != 1) { |
|---|
| 8 | selector = '#action_resolve_resolve_resolution'; |
|---|
| 9 | } |
|---|
| 10 | return selector; |
|---|
| 11 | }; |
|---|
| 12 | |
|---|
| 13 | window.apply_rules = function () { |
|---|
| 14 | var input = $(this); |
|---|
| 15 | |
|---|
| 16 | // execute the rule lifecycle... |
|---|
| 17 | |
|---|
| 18 | // setup each rule |
|---|
| 19 | $.each(triggers, function (trigger, specs) { |
|---|
| 20 | $.each(specs, function (i, spec) { |
|---|
| 21 | spec.rule.setup(input, spec); |
|---|
| 22 | }); |
|---|
| 23 | }); |
|---|
| 24 | |
|---|
| 25 | // apply each rule |
|---|
| 26 | $.each(triggers, function (trigger, specs) { |
|---|
| 27 | $.each(specs, function (i, spec) { |
|---|
| 28 | spec.rule.apply(input, spec); |
|---|
| 29 | }); |
|---|
| 30 | }); |
|---|
| 31 | |
|---|
| 32 | // complete each rule |
|---|
| 33 | $.each(triggers, function (trigger, specs) { |
|---|
| 34 | $.each(specs, function (i, spec) { |
|---|
| 35 | spec.rule.complete(input, spec); |
|---|
| 36 | }); |
|---|
| 37 | }); |
|---|
| 38 | |
|---|
| 39 | // update layout (see layout.js) |
|---|
| 40 | inputs_layout.update(); |
|---|
| 41 | header_layout.update(); |
|---|
| 42 | |
|---|
| 43 | }; |
|---|
| 44 | |
|---|
| 45 | // add selector and rule class to each trigger object |
|---|
| 46 | var rules = window.dynfields_rules; |
|---|
| 47 | for (var prop in triggers) { |
|---|
| 48 | triggers[prop].selector = get_selector(prop); |
|---|
| 49 | triggers[prop].forEach(function (spec) { |
|---|
| 50 | spec.rule = rules[spec.rule_name]; |
|---|
| 51 | }); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | if (window.location.pathname.match(/\/query$/)) { |
|---|
| 55 | // hide all "hide_always" fields |
|---|
| 56 | $.each(triggers, function (trigger, specs) { |
|---|
| 57 | $.each(specs, function (i, spec) { |
|---|
| 58 | spec.rule.query(spec); |
|---|
| 59 | }); |
|---|
| 60 | }); |
|---|
| 61 | } else { |
|---|
| 62 | var inputs = []; |
|---|
| 63 | |
|---|
| 64 | // collect all input fields that trigger rules |
|---|
| 65 | $.each(triggers, function (trigger, specs) { |
|---|
| 66 | var input = $(specs.selector).get(); |
|---|
| 67 | inputs.push(input); |
|---|
| 68 | }); |
|---|
| 69 | inputs = $.unique(inputs); |
|---|
| 70 | |
|---|
| 71 | // attach change event to each input and trigger first change |
|---|
| 72 | $.each(inputs, function (obj) { |
|---|
| 73 | $(this).change(apply_rules).change(); |
|---|
| 74 | }); |
|---|
| 75 | |
|---|
| 76 | // apply rules on auto preview |
|---|
| 77 | $(document).ajaxComplete(function(event, xhr, settings) { |
|---|
| 78 | if (settings.url === location.pathname) { |
|---|
| 79 | $.each(inputs, apply_rules); |
|---|
| 80 | } |
|---|
| 81 | }); |
|---|
| 82 | } |
|---|
| 83 | }); |
|---|