balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

required.js (13523B)


      1 /* global redux */
      2 
      3 (function( $ ) {
      4 	'use strict';
      5 
      6 	$.redux = $.redux || {};
      7 
      8 	$.redux.makeBoolStr = function( val ) {
      9 		if ( 'false' === val || false === val || '0' === val || 0 === val || null === val || '' === val ) {
     10 			return 'false';
     11 		} else if ( 'true' === val || true === val || '1' === val || 1 === val ) {
     12 			return 'true';
     13 		} else {
     14 			return val;
     15 		}
     16 	};
     17 
     18 	$.redux.checkRequired = function( el ) {
     19 		$.redux.required();
     20 
     21 		$( 'body' ).on(
     22 			'change',
     23 			'.redux-main select, .redux-main radio, .redux-main input[type=checkbox], .redux-main input[type=hidden]',
     24 			function() {
     25 				$.redux.check_dependencies( this );
     26 			}
     27 		);
     28 
     29 		$( 'body' ).on(
     30 			'check_dependencies',
     31 			function( e, variable ) {
     32 				e = null;
     33 				$.redux.check_dependencies( variable );
     34 			}
     35 		);
     36 
     37 		if ( redux.customizer ) {
     38 			el.find( '.customize-control.redux-field.hide' ).hide();
     39 		}
     40 
     41 		el.find( '.redux-container td > fieldset:empty,td > div:empty' ).parent().parent().hide();
     42 	};
     43 
     44 	$.redux.required = function() {
     45 
     46 		// Hide the fold elements on load.
     47 		// It's better to do this by PHP but there is no filter in tr tag , so is not possible
     48 		// we going to move each attributes we may need for folding to tr tag.
     49 		$.each(
     50 			redux.opt_names,
     51 			function( x ) {
     52 				$.each(
     53 					window['redux_' + redux.opt_names[x].replace( /\-/g, '_' )].folds,
     54 					function( i, v ) {
     55 						var div;
     56 						var rawTable;
     57 
     58 						var fieldset = $( '#' + redux.opt_names[x] + '-' + i );
     59 
     60 						fieldset.parents( 'tr:first, li:first' ).addClass( 'fold' );
     61 
     62 						if ( 'hide' === v ) {
     63 							fieldset.parents( 'tr:first, li:first' ).addClass( 'hide' );
     64 
     65 							if ( fieldset.hasClass( 'redux-container-section' ) ) {
     66 								div = $( '#section-' + i );
     67 
     68 								if ( div.hasClass( 'redux-section-indent-start' ) ) {
     69 									$( '#section-table-' + i ).hide().addClass( 'hide' );
     70 									div.hide().addClass( 'hide' );
     71 								}
     72 							}
     73 
     74 							if ( fieldset.hasClass( 'redux-container-info' ) ) {
     75 								$( '#info-' + i ).hide().addClass( 'hide' );
     76 							}
     77 
     78 							if ( fieldset.hasClass( 'redux-container-divide' ) ) {
     79 								$( '#divide-' + i ).hide().addClass( 'hide' );
     80 							}
     81 
     82 							if ( fieldset.hasClass( 'redux-container-raw' ) ) {
     83 								rawTable = fieldset.parents().find( 'table#' + redux.opt_names[x] + '-' + i );
     84 								rawTable.hide().addClass( 'hide' );
     85 							}
     86 						}
     87 					}
     88 				);
     89 			}
     90 		);
     91 	};
     92 
     93 	$.redux.getContainerValue = function( id ) {
     94 		var value = $( '#' + redux.optName.args.opt_name + '-' + id ).serializeForm();
     95 
     96 		if ( null !== value && 'object' === typeof value && value.hasOwnProperty( redux.optName.args.opt_name ) ) {
     97 			value = value[redux.optName.args.opt_name][id];
     98 		}
     99 
    100 		if ( $( '#' + redux.optName.args.opt_name + '-' + id ).hasClass( 'redux-container-media' ) ) {
    101 			value = value.url;
    102 		}
    103 
    104 		return value;
    105 	};
    106 
    107 	$.redux.check_dependencies = function( variable ) {
    108 		var current;
    109 		var id;
    110 		var container;
    111 		var isHidden;
    112 
    113 		if ( null === redux.optName.required ) {
    114 			return;
    115 		}
    116 
    117 		current = $( variable );
    118 		id      = current.parents( '.redux-field:first' ).data( 'id' );
    119 
    120 		if ( ! redux.optName.required.hasOwnProperty( id ) ) {
    121 			return;
    122 		}
    123 
    124 		container = current.parents( '.redux-field-container:first' );
    125 		isHidden  = container.parents( 'tr:first' ).hasClass( 'hide' );
    126 
    127 		if ( ! container.parents( 'tr:first' ).length ) {
    128 			isHidden = container.parents( '.customize-control:first' ).hasClass( 'hide' );
    129 		}
    130 
    131 		$.each(
    132 			redux.optName.required[id],
    133 			function( child ) {
    134 				var div;
    135 				var rawTable;
    136 				var tr;
    137 
    138 				var current       = $( this );
    139 				var show          = false;
    140 				var childFieldset = $( '#' + redux.optName.args.opt_name + '-' + child );
    141 
    142 				tr = childFieldset.parents( 'tr:first' );
    143 
    144 				if ( 0 === tr.length ) {
    145 					tr = childFieldset.parents( 'li:first' );
    146 				}
    147 
    148 				if ( ! isHidden ) {
    149 					show = $.redux.check_parents_dependencies( child );
    150 				}
    151 
    152 				if ( true === show ) {
    153 
    154 					// Shim for sections.
    155 					if ( childFieldset.hasClass( 'redux-container-section' ) ) {
    156 						div = $( '#section-' + child );
    157 
    158 						if ( div.hasClass( 'redux-section-indent-start' ) && div.hasClass( 'hide' ) ) {
    159 							$( '#section-table-' + child ).fadeIn( 300 ).removeClass( 'hide' );
    160 							div.fadeIn( 300 ).removeClass( 'hide' );
    161 						}
    162 					}
    163 
    164 					if ( childFieldset.hasClass( 'redux-container-info' ) ) {
    165 						$( '#info-' + child ).fadeIn( 300 ).removeClass( 'hide' );
    166 					}
    167 
    168 					if ( childFieldset.hasClass( 'redux-container-divide' ) ) {
    169 						$( '#divide-' + child ).fadeIn( 300 ).removeClass( 'hide' );
    170 					}
    171 
    172 					if ( childFieldset.hasClass( 'redux-container-raw' ) ) {
    173 						rawTable = childFieldset.parents().find( 'table#' + redux.optName.args.opt_name + '-' + child );
    174 						rawTable.fadeIn( 300 ).removeClass( 'hide' );
    175 					}
    176 
    177 					tr.fadeIn(
    178 						300,
    179 						function() {
    180 							$( this ).removeClass( 'hide' );
    181 							if ( redux.optName.required.hasOwnProperty( child ) ) {
    182 								$.redux.check_dependencies( $( '#' + redux.optName.args.opt_name + '-' + child ).children().first() );
    183 							}
    184 
    185 							$.redux.initFields();
    186 						}
    187 					);
    188 
    189 					if ( childFieldset.hasClass( 'redux-container-section' ) || childFieldset.hasClass( 'redux-container-info' ) ) {
    190 						tr.css( { display: 'none' } );
    191 					}
    192 				} else if ( false === show ) {
    193 					tr.fadeOut(
    194 						100,
    195 						function() {
    196 							$( this ).addClass( 'hide' );
    197 							if ( redux.optName.required.hasOwnProperty( child ) ) {
    198 								$.redux.required_recursive_hide( child );
    199 							}
    200 						}
    201 					);
    202 				}
    203 
    204 				current.find( 'select, radio, input[type=checkbox]' ).trigger( 'change' );
    205 			}
    206 		);
    207 	};
    208 
    209 	$.redux.required_recursive_hide = function( id ) {
    210 		var div;
    211 		var rawTable;
    212 		var toFade;
    213 
    214 		toFade = $( '#' + redux.optName.args.opt_name + '-' + id ).parents( 'tr:first' );
    215 		if ( 0 === toFade ) {
    216 			toFade = $( '#' + redux.optName.args.opt_name + '-' + id ).parents( 'li:first' );
    217 		}
    218 
    219 		toFade.fadeOut(
    220 			50,
    221 			function() {
    222 				$( this ).addClass( 'hide' );
    223 
    224 				if ( $( '#' + redux.optName.args.opt_name + '-' + id ).hasClass( 'redux-container-section' ) ) {
    225 					div = $( '#section-' + id );
    226 
    227 					if ( div.hasClass( 'redux-section-indent-start' ) ) {
    228 						$( '#section-table-' + id ).fadeOut( 50 ).addClass( 'hide' );
    229 						div.fadeOut( 50 ).addClass( 'hide' );
    230 					}
    231 				}
    232 
    233 				if ( $( '#' + redux.optName.args.opt_name + '-' + id ).hasClass( 'redux-container-info' ) ) {
    234 					$( '#info-' + id ).fadeOut( 50 ).addClass( 'hide' );
    235 				}
    236 
    237 				if ( $( '#' + redux.optName.args.opt_name + '-' + id ).hasClass( 'redux-container-divide' ) ) {
    238 					$( '#divide-' + id ).fadeOut( 50 ).addClass( 'hide' );
    239 				}
    240 
    241 				if ( $( '#' + redux.optName.args.opt_name + '-' + id ).hasClass( 'redux-container-raw' ) ) {
    242 					rawTable = $( '#' + redux.optName.args.opt_name + '-' + id ).parents().find( 'table#' + redux.optName.args.opt_name + '-' + id );
    243 					rawTable.fadeOut( 50 ).addClass( 'hide' );
    244 				}
    245 
    246 				if ( redux.optName.required.hasOwnProperty( id ) ) {
    247 					$.each(
    248 						redux.optName.required[id],
    249 						function( child ) {
    250 							$.redux.required_recursive_hide( child );
    251 						}
    252 					);
    253 				}
    254 			}
    255 		);
    256 	};
    257 
    258 	$.redux.check_parents_dependencies = function( id ) {
    259 		var show = '';
    260 
    261 		if ( redux.optName.required_child.hasOwnProperty( id ) ) {
    262 			$.each(
    263 				redux.optName.required_child[id],
    264 				function( i, parentData ) {
    265 					var parentValue;
    266 
    267 					i = null;
    268 
    269 					if ( $( '#' + redux.optName.args.opt_name + '-' + parentData.parent ).parents( 'tr:first' ).hasClass( 'hide' ) ) {
    270 						show = false;
    271 					} else if ( $( '#' + redux.optName.args.opt_name + '-' + parentData.parent ).parents( 'li:first' ).hasClass( 'hide' ) ) {
    272 						show = false;
    273 					} else {
    274 						if ( false !== show ) {
    275 							parentValue = $.redux.getContainerValue( parentData.parent );
    276 
    277 							show = $.redux.check_dependencies_visibility( parentValue, parentData );
    278 						}
    279 					}
    280 				}
    281 			);
    282 		} else {
    283 			show = true;
    284 		}
    285 
    286 		return show;
    287 	};
    288 
    289 	$.redux.check_dependencies_visibility = function( parentValue, data ) {
    290 		var show       = false;
    291 		var checkValue = data.checkValue;
    292 		var operation  = data.operation;
    293 		var arr;
    294 
    295 		if ( $.isPlainObject( parentValue ) ) {
    296 			parentValue = Object.keys( parentValue ).map(
    297 				function( key ) {
    298 					return [key, parentValue[key]];
    299 				}
    300 			);
    301 		}
    302 
    303 		switch ( operation ) {
    304 			case '=':
    305 			case 'equals':
    306 				if ( $.isArray( parentValue ) ) {
    307 					$( parentValue[0] ).each(
    308 						function( idx, val ) {
    309 							idx = null;
    310 
    311 							if ( $.isArray( checkValue ) ) {
    312 								$( checkValue ).each(
    313 									function( i, v ) {
    314 										i = null;
    315 										if ( $.redux.makeBoolStr( val ) === $.redux.makeBoolStr( v ) ) {
    316 											show = true;
    317 
    318 											return true;
    319 										}
    320 									}
    321 								);
    322 							} else {
    323 								if ( $.redux.makeBoolStr( val ) === $.redux.makeBoolStr( checkValue ) ) {
    324 									show = true;
    325 
    326 									return true;
    327 								}
    328 							}
    329 						}
    330 					);
    331 				} else {
    332 					if ( $.isArray( checkValue ) ) {
    333 						$( checkValue ).each(
    334 							function( i, v ) {
    335 								i = null;
    336 
    337 								if ( $.redux.makeBoolStr( parentValue ) === $.redux.makeBoolStr( v ) ) {
    338 									show = true;
    339 								}
    340 							}
    341 						);
    342 					} else {
    343 						if ( $.redux.makeBoolStr( parentValue ) === $.redux.makeBoolStr( checkValue ) ) {
    344 							show = true;
    345 						}
    346 					}
    347 				}
    348 				break;
    349 
    350 			case '!=':
    351 			case 'not':
    352 				if ( $.isArray( parentValue ) ) {
    353 					$( parentValue[0] ).each(
    354 						function( idx, val ) {
    355 							idx = null;
    356 
    357 							if ( $.isArray( checkValue ) ) {
    358 								$( checkValue ).each(
    359 									function( i, v ) {
    360 										i = null;
    361 
    362 										if ( $.redux.makeBoolStr( val ) !== $.redux.makeBoolStr( v ) ) {
    363 											show = true;
    364 
    365 											return true;
    366 										}
    367 									}
    368 								);
    369 							} else {
    370 								if ( $.redux.makeBoolStr( val ) !== $.redux.makeBoolStr( checkValue ) ) {
    371 									show = true;
    372 
    373 									return true;
    374 								}
    375 							}
    376 						}
    377 					);
    378 				} else {
    379 					if ( $.isArray( checkValue ) ) {
    380 						$( checkValue ).each(
    381 							function( i, v ) {
    382 								i = null;
    383 
    384 								if ( $.redux.makeBoolStr( parentValue ) !== $.redux.makeBoolStr( v ) ) {
    385 									show = true;
    386 								}
    387 							}
    388 						);
    389 					} else {
    390 						if ( $.redux.makeBoolStr( parentValue ) !== $.redux.makeBoolStr( checkValue ) ) {
    391 							show = true;
    392 						}
    393 					}
    394 				}
    395 				break;
    396 
    397 			case '>':
    398 			case 'greater':
    399 			case 'is_larger':
    400 				if ( parseFloat( parentValue ) > parseFloat( checkValue ) ) {
    401 					show = true;
    402 				}
    403 				break;
    404 
    405 			case '>=':
    406 			case 'greater_equal':
    407 			case 'is_larger_equal':
    408 				if ( parseFloat( parentValue ) >= parseFloat( checkValue ) ) {
    409 					show = true;
    410 				}
    411 				break;
    412 
    413 			case '<':
    414 			case 'less':
    415 			case 'is_smaller':
    416 				if ( parseFloat( parentValue ) < parseFloat( checkValue ) ) {
    417 					show = true;
    418 				}
    419 				break;
    420 
    421 			case '<=':
    422 			case 'less_equal':
    423 			case 'is_smaller_equal':
    424 				if ( parseFloat( parentValue ) <= parseFloat( checkValue ) ) {
    425 					show = true;
    426 				}
    427 				break;
    428 
    429 			case 'contains':
    430 				if ( $.isPlainObject( parentValue ) ) {
    431 					parentValue = Object.keys( parentValue ).map(
    432 						function( key ) {
    433 							return [key, parentValue[key]];
    434 						}
    435 					);
    436 				}
    437 
    438 				if ( $.isPlainObject( checkValue ) ) {
    439 					checkValue = Object.keys( checkValue ).map(
    440 						function( key ) {
    441 							return [key, checkValue[key]];
    442 						}
    443 					);
    444 				}
    445 
    446 				if ( $.isArray( checkValue ) ) {
    447 					$( checkValue ).each(
    448 						function( idx, val ) {
    449 							var breakMe = false;
    450 							var toFind  = val[0];
    451 							var findVal = val[1];
    452 
    453 							idx = null;
    454 
    455 							$( parentValue ).each(
    456 								function( i, v ) {
    457 									var toMatch  = v[0];
    458 									var matchVal = v[1];
    459 
    460 									i = null;
    461 
    462 									if ( toFind === toMatch ) {
    463 										if ( findVal === matchVal ) {
    464 											show    = true;
    465 											breakMe = true;
    466 
    467 											return false;
    468 										}
    469 									}
    470 								}
    471 							);
    472 
    473 							if ( true === breakMe ) {
    474 								return false;
    475 							}
    476 						}
    477 					);
    478 				} else {
    479 					if ( parentValue.toString().indexOf( checkValue ) !== - 1 ) {
    480 						show = true;
    481 					}
    482 				}
    483 				break;
    484 
    485 			case 'doesnt_contain':
    486 			case 'not_contain':
    487 				if ( $.isPlainObject( parentValue ) ) {
    488 					arr = Object.keys( parentValue ).map(
    489 						function( key ) {
    490 							return parentValue[key];
    491 						}
    492 					);
    493 
    494 					parentValue = arr;
    495 				}
    496 
    497 				if ( $.isPlainObject( checkValue ) ) {
    498 					arr = Object.keys( checkValue ).map(
    499 						function( key ) {
    500 							return checkValue[key];
    501 						}
    502 					);
    503 
    504 					checkValue = arr;
    505 				}
    506 
    507 				if ( $.isArray( checkValue ) ) {
    508 					$( checkValue ).each(
    509 						function( idx, val ) {
    510 							idx = null;
    511 
    512 							if ( parentValue.toString().indexOf( val ) === - 1 ) {
    513 								show = true;
    514 							}
    515 						}
    516 					);
    517 				} else {
    518 					if ( parentValue.toString().indexOf( checkValue ) === - 1 ) {
    519 						show = true;
    520 					}
    521 				}
    522 				break;
    523 
    524 			case 'is_empty_or':
    525 				if ( '' === parentValue || checkValue === parentValue ) {
    526 					show = true;
    527 				}
    528 				break;
    529 
    530 			case 'not_empty_and':
    531 				if ( '' !== parentValue && checkValue !== parentValue ) {
    532 					show = true;
    533 				}
    534 				break;
    535 
    536 			case 'is_empty':
    537 			case 'empty':
    538 			case '!isset':
    539 				if ( ! parentValue || '' === parentValue || null === parentValue ) {
    540 					show = true;
    541 				}
    542 				break;
    543 
    544 			case 'not_empty':
    545 			case '!empty':
    546 			case 'isset':
    547 				if ( parentValue && '' !== parentValue && null !== parentValue ) {
    548 					show = true;
    549 				}
    550 				break;
    551 		}
    552 
    553 		return show;
    554 	};
    555 })( jQuery );