bindings
6 months ago
commands
3 weeks ago
pro
3 weeks ago
_acf-compatibility.js
1 year ago
_acf-condition-types.js
8 months ago
_acf-condition.js
1 year ago
_acf-conditions.js
1 year ago
_acf-field-accordion.js
6 months ago
_acf-field-button-group.js
8 months ago
_acf-field-checkbox.js
1 month ago
_acf-field-color-picker.js
8 months ago
_acf-field-date-picker.js
1 month ago
_acf-field-date-time-picker.js
1 month ago
_acf-field-file.js
1 month ago
_acf-field-google-map.js
6 months ago
_acf-field-icon-picker.js
1 month ago
_acf-field-image.js
1 month ago
_acf-field-link.js
1 year ago
_acf-field-oembed.js
1 month ago
_acf-field-page-link.js
1 year ago
_acf-field-post-object.js
1 year ago
_acf-field-radio.js
1 month ago
_acf-field-range.js
1 year ago
_acf-field-relationship.js
1 month ago
_acf-field-select.js
1 month ago
_acf-field-tab.js
1 month ago
_acf-field-taxonomy.js
8 months ago
_acf-field-time-picker.js
1 month ago
_acf-field-true-false.js
1 month ago
_acf-field-url.js
1 year ago
_acf-field-user.js
1 year ago
_acf-field-wysiwyg.js
1 month ago
_acf-field.js
1 year ago
_acf-fields.js
1 year ago
_acf-helpers.js
1 year ago
_acf-hooks.js
1 year ago
_acf-internal-post-type.js
8 months ago
_acf-media.js
3 weeks ago
_acf-modal.js
1 year ago
_acf-model.js
1 year ago
_acf-notice.js
8 months ago
_acf-panel.js
1 year ago
_acf-popup.js
8 months ago
_acf-postbox.js
1 year ago
_acf-screen.js
10 months ago
_acf-select2.js
1 year ago
_acf-tinymce.js
6 months ago
_acf-tooltip.js
10 months ago
_acf-unload.js
1 year ago
_acf-validation.js
1 month ago
_acf.js
7 months ago
_browse-fields-modal.js
8 months ago
_field-group-compatibility.js
1 year ago
_field-group-conditions.js
1 year ago
_field-group-field.js
4 months ago
_field-group-fields.js
10 months ago
_field-group-locations.js
1 year ago
_field-group-settings.js
1 year ago
_field-group.js
10 months ago
acf-escaped-html-notice.js
1 year ago
acf-field-group.js
1 year ago
acf-input.js
1 year ago
acf-internal-post-type.js
1 year ago
acf.js
1 year ago
_acf-conditions.js
302 lines
| 1 | ( function ( $, undefined ) { |
| 2 | // vars |
| 3 | var CONTEXT = 'conditional_logic'; |
| 4 | |
| 5 | /** |
| 6 | * conditionsManager |
| 7 | * |
| 8 | * description |
| 9 | * |
| 10 | * @date 1/2/18 |
| 11 | * @since ACF 5.6.5 |
| 12 | * |
| 13 | * @param type $var Description. Default. |
| 14 | * @return type Description. |
| 15 | */ |
| 16 | |
| 17 | var conditionsManager = new acf.Model( { |
| 18 | id: 'conditionsManager', |
| 19 | |
| 20 | priority: 20, // run actions later |
| 21 | |
| 22 | actions: { |
| 23 | new_field: 'onNewField', |
| 24 | }, |
| 25 | |
| 26 | onNewField: function ( field ) { |
| 27 | if ( field.has( 'conditions' ) ) { |
| 28 | field.getConditions().render(); |
| 29 | } |
| 30 | }, |
| 31 | } ); |
| 32 | |
| 33 | /** |
| 34 | * acf.Field.prototype.getField |
| 35 | * |
| 36 | * Finds a field that is related to another field |
| 37 | * |
| 38 | * @date 1/2/18 |
| 39 | * @since ACF 5.6.5 |
| 40 | * |
| 41 | * @param type $var Description. Default. |
| 42 | * @return type Description. |
| 43 | */ |
| 44 | |
| 45 | var getSiblingField = function ( field, key ) { |
| 46 | // find sibling (very fast) |
| 47 | var fields = acf.getFields( { |
| 48 | key: key, |
| 49 | sibling: field.$el, |
| 50 | suppressFilters: true, |
| 51 | } ); |
| 52 | |
| 53 | // find sibling-children (fast) |
| 54 | // needed for group fields, accordions, etc |
| 55 | if ( ! fields.length ) { |
| 56 | fields = acf.getFields( { |
| 57 | key: key, |
| 58 | parent: field.$el.parent(), |
| 59 | suppressFilters: true, |
| 60 | } ); |
| 61 | } |
| 62 | |
| 63 | // Check for fields on other settings tabs (probably less fast). |
| 64 | if ( ! fields.length && $( '.acf-field-settings' ).length ) { |
| 65 | fields = acf.getFields( { |
| 66 | key: key, |
| 67 | parent: field.$el.parents( '.acf-field-settings:first' ), |
| 68 | suppressFilters: true, |
| 69 | } ); |
| 70 | } |
| 71 | |
| 72 | if ( ! fields.length && $( '#acf-basic-settings' ).length ) { |
| 73 | fields = acf.getFields( { |
| 74 | key: key, |
| 75 | parent: $( '#acf-basic-settings'), |
| 76 | suppressFilters: true, |
| 77 | } ); |
| 78 | } |
| 79 | |
| 80 | // return |
| 81 | if ( fields.length ) { |
| 82 | return fields[ 0 ]; |
| 83 | } |
| 84 | return false; |
| 85 | }; |
| 86 | |
| 87 | acf.Field.prototype.getField = function ( key ) { |
| 88 | // get sibling field |
| 89 | var field = getSiblingField( this, key ); |
| 90 | |
| 91 | // return early |
| 92 | if ( field ) { |
| 93 | return field; |
| 94 | } |
| 95 | |
| 96 | // move up through each parent and try again |
| 97 | var parents = this.parents(); |
| 98 | for ( var i = 0; i < parents.length; i++ ) { |
| 99 | // get sibling field |
| 100 | field = getSiblingField( parents[ i ], key ); |
| 101 | |
| 102 | // return early |
| 103 | if ( field ) { |
| 104 | return field; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // return |
| 109 | return false; |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * acf.Field.prototype.getConditions |
| 114 | * |
| 115 | * Returns the field's conditions instance |
| 116 | * |
| 117 | * @date 1/2/18 |
| 118 | * @since ACF 5.6.5 |
| 119 | * |
| 120 | * @param type $var Description. Default. |
| 121 | * @return type Description. |
| 122 | */ |
| 123 | |
| 124 | acf.Field.prototype.getConditions = function () { |
| 125 | // instantiate |
| 126 | if ( ! this.conditions ) { |
| 127 | this.conditions = new Conditions( this ); |
| 128 | } |
| 129 | |
| 130 | // return |
| 131 | return this.conditions; |
| 132 | }; |
| 133 | |
| 134 | /** |
| 135 | * Conditions |
| 136 | * |
| 137 | * description |
| 138 | * |
| 139 | * @date 1/2/18 |
| 140 | * @since ACF 5.6.5 |
| 141 | * |
| 142 | * @param type $var Description. Default. |
| 143 | * @return type Description. |
| 144 | */ |
| 145 | var timeout = false; |
| 146 | var Conditions = acf.Model.extend( { |
| 147 | id: 'Conditions', |
| 148 | |
| 149 | data: { |
| 150 | field: false, // The field with "data-conditions" (target). |
| 151 | timeStamp: false, // Reference used during "change" event. |
| 152 | groups: [], // The groups of condition instances. |
| 153 | }, |
| 154 | |
| 155 | setup: function ( field ) { |
| 156 | // data |
| 157 | this.data.field = field; |
| 158 | |
| 159 | // vars |
| 160 | var conditions = field.get( 'conditions' ); |
| 161 | |
| 162 | // detect groups |
| 163 | if ( conditions instanceof Array ) { |
| 164 | // detect groups |
| 165 | if ( conditions[ 0 ] instanceof Array ) { |
| 166 | // loop |
| 167 | conditions.map( function ( rules, i ) { |
| 168 | this.addRules( rules, i ); |
| 169 | }, this ); |
| 170 | |
| 171 | // detect rules |
| 172 | } else { |
| 173 | this.addRules( conditions ); |
| 174 | } |
| 175 | |
| 176 | // detect rule |
| 177 | } else { |
| 178 | this.addRule( conditions ); |
| 179 | } |
| 180 | }, |
| 181 | |
| 182 | change: function ( e ) { |
| 183 | // this function may be triggered multiple times per event due to multiple condition classes |
| 184 | // compare timestamp to allow only 1 trigger per event |
| 185 | if ( this.get( 'timeStamp' ) === e.timeStamp ) { |
| 186 | return false; |
| 187 | } else { |
| 188 | this.set( 'timeStamp', e.timeStamp, true ); |
| 189 | } |
| 190 | |
| 191 | // render condition and store result |
| 192 | var changed = this.render(); |
| 193 | }, |
| 194 | |
| 195 | render: function () { |
| 196 | return this.calculate() ? this.show() : this.hide(); |
| 197 | }, |
| 198 | |
| 199 | show: function () { |
| 200 | return this.get( 'field' ).showEnable( this.cid, CONTEXT ); |
| 201 | }, |
| 202 | |
| 203 | hide: function () { |
| 204 | return this.get( 'field' ).hideDisable( this.cid, CONTEXT ); |
| 205 | }, |
| 206 | |
| 207 | calculate: function () { |
| 208 | // vars |
| 209 | var pass = false; |
| 210 | |
| 211 | // loop |
| 212 | this.getGroups().map( function ( group ) { |
| 213 | // ignore this group if another group passed |
| 214 | if ( pass ) return; |
| 215 | |
| 216 | // find passed |
| 217 | var passed = group.filter( function ( condition ) { |
| 218 | return condition.calculate(); |
| 219 | } ); |
| 220 | |
| 221 | // if all conditions passed, update the global var |
| 222 | if ( passed.length == group.length ) { |
| 223 | pass = true; |
| 224 | } |
| 225 | } ); |
| 226 | |
| 227 | return pass; |
| 228 | }, |
| 229 | |
| 230 | hasGroups: function () { |
| 231 | return this.data.groups != null; |
| 232 | }, |
| 233 | |
| 234 | getGroups: function () { |
| 235 | return this.data.groups; |
| 236 | }, |
| 237 | |
| 238 | addGroup: function () { |
| 239 | var group = []; |
| 240 | this.data.groups.push( group ); |
| 241 | return group; |
| 242 | }, |
| 243 | |
| 244 | hasGroup: function ( i ) { |
| 245 | return this.data.groups[ i ] != null; |
| 246 | }, |
| 247 | |
| 248 | getGroup: function ( i ) { |
| 249 | return this.data.groups[ i ]; |
| 250 | }, |
| 251 | |
| 252 | removeGroup: function ( i ) { |
| 253 | this.data.groups[ i ].delete; |
| 254 | return this; |
| 255 | }, |
| 256 | |
| 257 | addRules: function ( rules, group ) { |
| 258 | rules.map( function ( rule ) { |
| 259 | this.addRule( rule, group ); |
| 260 | }, this ); |
| 261 | }, |
| 262 | |
| 263 | addRule: function ( rule, group ) { |
| 264 | // defaults |
| 265 | group = group || 0; |
| 266 | |
| 267 | // vars |
| 268 | var groupArray; |
| 269 | |
| 270 | // get group |
| 271 | if ( this.hasGroup( group ) ) { |
| 272 | groupArray = this.getGroup( group ); |
| 273 | } else { |
| 274 | groupArray = this.addGroup(); |
| 275 | } |
| 276 | |
| 277 | // instantiate |
| 278 | var condition = acf.newCondition( rule, this ); |
| 279 | |
| 280 | // bail early if condition failed (field did not exist) |
| 281 | if ( ! condition ) { |
| 282 | return false; |
| 283 | } |
| 284 | |
| 285 | // add rule |
| 286 | groupArray.push( condition ); |
| 287 | }, |
| 288 | |
| 289 | hasRule: function () {}, |
| 290 | |
| 291 | getRule: function ( rule, group ) { |
| 292 | // defaults |
| 293 | rule = rule || 0; |
| 294 | group = group || 0; |
| 295 | |
| 296 | return this.data.groups[ group ][ rule ]; |
| 297 | }, |
| 298 | |
| 299 | removeRule: function () {}, |
| 300 | } ); |
| 301 | } )( jQuery ); |
| 302 |