mirror of
https://github.com/10h30/ultimatemember.git
synced 2026-07-11 18:56:10 +09:00
refactor conditional logic fields
This commit is contained in:
+11
-27
@@ -1,37 +1,21 @@
|
||||
jQuery(document).ready(function() {
|
||||
|
||||
|
||||
jQuery(document).on('input change', '.um-field input[type=text]', function(){
|
||||
|
||||
live_field = jQuery(this).parents('.um-field').data('key');
|
||||
live_value = jQuery(this).val();
|
||||
um_conditional();
|
||||
|
||||
if( um_field_do_init ){
|
||||
um_field_init();
|
||||
}
|
||||
});
|
||||
jQuery('.um-field input[type=text]').trigger('input');
|
||||
|
||||
jQuery(document).on('change', '.um-field select, .um-field input[type=radio], .um-field input[type=checkbox]', function(){
|
||||
|
||||
live_field = jQuery(this).parents('.um-field').data('key');
|
||||
live_value = jQuery(this).val();
|
||||
|
||||
if ( jQuery(this).is(':checkbox') ) {
|
||||
if ( jQuery(this).parents('.um-field').find('input:checked').length > 1 ) {
|
||||
live_value = '';
|
||||
jQuery(this).parents('.um-field').find('input:checked').each(function(){
|
||||
live_value = live_value + jQuery(this).val() + ' ';
|
||||
});
|
||||
} else {
|
||||
live_value = jQuery(this).parents('.um-field').find('input:checked').val();
|
||||
}
|
||||
if( um_field_do_init ){
|
||||
um_field_init();
|
||||
}
|
||||
|
||||
if ( jQuery(this).is(':radio') ) {
|
||||
live_value = jQuery(this).parents('.um-field').find('input[type=radio]:checked').val();
|
||||
}
|
||||
|
||||
um_conditional();
|
||||
|
||||
});
|
||||
jQuery('.um-field select, .um-field input[type=radio], .um-field input[type=checkbox]').trigger('change');
|
||||
|
||||
|
||||
um_field_init();
|
||||
um_field_do_init = true;
|
||||
|
||||
|
||||
});
|
||||
|
||||
+245
-148
@@ -1,171 +1,271 @@
|
||||
var live_field;
|
||||
var live_value;
|
||||
var um_live_field;
|
||||
var um_live_value;
|
||||
var um_field_conditions_array = {};
|
||||
var um_field_relations = {};
|
||||
var um_field_added = [];
|
||||
var um_field_loaded = false;
|
||||
var um_field_do_init = false;
|
||||
|
||||
function um_conditional(){
|
||||
function um_field_init(){
|
||||
|
||||
var um_fields_conditions = 1;
|
||||
jQuery('.um-field[data-key]').each(function(){
|
||||
um_field_set_conditions( jQuery(this), true );
|
||||
});
|
||||
|
||||
um_field_relationship();
|
||||
|
||||
um_field_apply_conditions();
|
||||
um_field_loaded = true;
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
function um_field_set_conditions( um_field_dom, add_fade ){
|
||||
var um_field_key = um_field_dom.data('key');
|
||||
var arr_field = [];
|
||||
for (var i = 0; i < 5; i++) {
|
||||
|
||||
var action0 = um_field_dom.data('cond-'+i+'-action');
|
||||
var field0 = um_field_dom.data('cond-'+i+'-field');
|
||||
var operator0 = um_field_dom.data('cond-'+i+'-operator');
|
||||
var value0 = um_field_dom.data('cond-'+i+'-value');
|
||||
|
||||
if( typeof value0 !== 'undefined' && um_field_loaded == false ){
|
||||
arr_field.push({
|
||||
child: um_field_key,
|
||||
action: action0,
|
||||
field: field0,
|
||||
operator: operator0,
|
||||
value: value0
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
jQuery('.um-field.um-is-conditional').each(function(){
|
||||
var found = 0;
|
||||
var um_field_key = jQuery(this).data('key');
|
||||
//console.log('-----'+ ( um_fields_conditions++ ) +'----');
|
||||
|
||||
for (var i = 0; i < 5; i++) {
|
||||
} // end for
|
||||
|
||||
var action0 = jQuery(this).data('cond-'+i+'-action');
|
||||
var field0 = jQuery(this).data('cond-'+i+'-field');
|
||||
var operator0 = jQuery(this).data('cond-'+i+'-operator');
|
||||
var value0 = jQuery(this).data('cond-'+i+'-value');
|
||||
if( jQuery.inArray( um_field_key, um_field_added ) <= -1 && um_field_loaded == false ){
|
||||
um_field_added.push( um_field_key );
|
||||
um_field_conditions_array[ um_field_key ] = arr_field;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
function um_field_apply_conditions(){
|
||||
|
||||
jQuery.each( um_field_added, function( i, field_key ){
|
||||
|
||||
if( ( action0 == 'show' && field0 == live_field && typeof value0 !== 'undefined' ) || jQuery('div.um-field[data-key='+field0+']').is(':hidden') ){
|
||||
jQuery(this).hide();
|
||||
um_reset_field('div.um-field[data-cond-'+i+'-field='+um_field_key+']');
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').hide();
|
||||
if( um_field_relations[ field_key ].length <= 0 ){
|
||||
um_field_relations[ field_key ] = [{
|
||||
child: field_key,
|
||||
action:'child',
|
||||
field: '',
|
||||
operator: '',
|
||||
}];
|
||||
}
|
||||
|
||||
jQuery.each( um_field_relations[ field_key ], function( ii, conditions ){
|
||||
|
||||
var add_fade = true;
|
||||
var action0 = conditions.action;
|
||||
var value0 = conditions.value;
|
||||
var operator0 = conditions.operator;
|
||||
var field0 = conditions.field;
|
||||
var um_field_parent_dom = '';
|
||||
|
||||
if ( action0 == 'show' && field0 == live_field && typeof value0 !== 'undefined' ) {
|
||||
if( action0 == 'show' || action0 == 'hide' ){
|
||||
//console.log( um_field_do_init );
|
||||
}
|
||||
um_field_parent_dom = jQuery('.um-field[data-key="'+field_key+'"]:visible').find('input[type=text],textarea,input[type=checkbox],input[type=radio],select');
|
||||
|
||||
var um_field_data = um_get_field_data( um_field_parent_dom );
|
||||
var um_live_field = um_field_data.key;
|
||||
var um_live_value = um_field_data.value;
|
||||
|
||||
var um_field_child_dom = jQuery('.um-field[data-key="'+conditions.child+'"]').find('input[type=text],textarea,input[type=checkbox],input[type=radio],select');
|
||||
|
||||
if( action0 == 'show' ){
|
||||
jQuery('.um-field[data-key="'+conditions.child+'"]').hide();
|
||||
}
|
||||
|
||||
if ( operator0 == 'empty' ) {
|
||||
if ( !live_value || live_value == '' || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
if ( action0 == 'show' && typeof value0 !== 'undefined' ) {
|
||||
|
||||
if ( operator0 == 'empty' ) {
|
||||
if ( !um_live_value || um_live_value == '' ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not empty' ) {
|
||||
if ( live_value && live_value != '' || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'equals to' ) {
|
||||
if ( value0 == live_value || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not equals' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) != parseInt( value0 ) && live_value || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
} else if ( !jQuery.isNumeric( value0 ) && value0 != live_value || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
if ( operator0 == 'not empty' ) {
|
||||
if ( um_live_value && um_live_value != '' ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'greater than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) > parseInt( value0 ) || found > 0) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
if ( operator0 == 'equals to' ) {
|
||||
if ( value0 == um_live_value ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not equals' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) != parseInt( value0 ) && um_live_value ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
} else if ( !jQuery.isNumeric( value0 ) && value0 != um_live_value ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'greater than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) > parseInt( value0 ) ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'less than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) < parseInt( value0 ) && um_live_value ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'contains' ) {
|
||||
if ( um_live_value && um_live_value.indexOf( value0 ) >= 0 ) {
|
||||
um_field_show( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( operator0 == 'less than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) < parseInt( value0 ) && live_value || found > 0) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
}
|
||||
if( action0 == 'hide' ){
|
||||
jQuery('.um-field[data-key="'+conditions.child+'"]').show();
|
||||
}
|
||||
|
||||
if ( operator0 == 'contains' ) {
|
||||
if ( live_value && live_value.indexOf( value0 ) >= 0 || found > 0 ) {
|
||||
jQuery(this).fadeIn();
|
||||
found++;
|
||||
}
|
||||
}
|
||||
if ( action0 == 'hide' && typeof value0 !== 'undefined' ) {
|
||||
|
||||
if ( operator0 == 'empty' ) {
|
||||
if ( !um_live_value || um_live_value == '' ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not empty' ) {
|
||||
if ( um_live_value && um_live_value != '' ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'equals to' ) {
|
||||
if ( value0 == um_live_value ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not equals' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) != parseInt( value0 ) && um_live_value ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
} else if ( !jQuery.isNumeric( value0 ) && value0 != um_live_value ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'greater than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) > parseInt( value0 ) ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'less than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( um_live_value ) < parseInt( value0 ) && um_live_value ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'contains' ) {
|
||||
if ( um_live_value && um_live_value.indexOf( value0 ) >= 0 ) {
|
||||
um_field_hide( um_field_child_dom, add_fade, operator0, um_live_field );
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
function um_field_show( field, add_fade, optr, k ){
|
||||
var parent_wrapper = field.parents('.um-field');
|
||||
|
||||
if( add_fade ){
|
||||
parent_wrapper.fadeIn();
|
||||
}else{
|
||||
parent_wrapper.show();
|
||||
}
|
||||
//console.log('show um field "'+k+'" operator:'+optr );
|
||||
}
|
||||
|
||||
function um_field_hide( field, add_fade, optr, k ){
|
||||
var parent_wrapper = field.parents('.um-field');
|
||||
|
||||
if( add_fade ){
|
||||
parent_wrapper.fadeOut();
|
||||
}else{
|
||||
parent_wrapper.hide();
|
||||
}
|
||||
//console.log('hide um field "'+k+'" operator:'+optr );
|
||||
}
|
||||
|
||||
function um_get_field_data( um_field_dom ){
|
||||
um_live_field = um_field_dom.parents('.um-field').data('key');
|
||||
um_live_value = um_field_dom.val();
|
||||
|
||||
if ( um_field_dom.is(':checkbox') ) {
|
||||
if ( um_field_dom.parents('.um-field').find('input:checked').length > 1 ) {
|
||||
um_live_value = '';
|
||||
um_field_dom.parents('.um-field').find('input:checked').each(function(){
|
||||
um_live_value = um_live_value + um_field_dom.val() + ' ';
|
||||
});
|
||||
} else {
|
||||
um_live_value = um_field_dom.parents('.um-field').find('input:checked').val();
|
||||
}
|
||||
}
|
||||
|
||||
if ( action0 == 'hide' && field0 == live_field && typeof value0 !== 'undefined' ) {
|
||||
if ( um_field_dom.is(':radio') ) {
|
||||
um_live_value = um_field_dom.parents('.um-field').find('input[type=radio]:checked').val();
|
||||
}
|
||||
|
||||
if ( operator0 == 'empty' ) {
|
||||
if ( !live_value || live_value == '' ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
return {
|
||||
key: um_live_field,
|
||||
value: um_live_value
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'not empty' ) {
|
||||
if ( live_value && live_value != '' ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
function um_field_relationship(){
|
||||
var arr_fields = um_field_conditions_array;
|
||||
if( um_field_loaded == false ){
|
||||
jQuery.each( arr_fields, function(k, f) {
|
||||
|
||||
}
|
||||
}
|
||||
var new_arr_field = [];
|
||||
var arr_um_field_exists = [];
|
||||
|
||||
if ( operator0 == 'equals to' ) {
|
||||
if ( value0 == live_value ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
jQuery.each( arr_fields, function(ii,field) {
|
||||
for (var i = 0; i <= field.length; i++ ){
|
||||
|
||||
}
|
||||
}
|
||||
if( typeof field[ i ] !== 'undefined' ){
|
||||
if( k == field[ i ].field ){
|
||||
new_arr_field.push( field[ i ] );
|
||||
arr_um_field_exists.push( field[ i ].child );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if ( operator0 == 'not equals' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) != parseInt( value0 ) && live_value ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else if ( !jQuery.isNumeric( value0 ) && value0 != live_value ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'greater than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) > parseInt( value0 ) ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'less than' ) {
|
||||
if ( jQuery.isNumeric( value0 ) && parseInt( live_value ) < parseInt( value0 ) && live_value ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if ( operator0 == 'contains' ) {
|
||||
if ( live_value && live_value.indexOf( value0 ) >= 0 ) {
|
||||
jQuery(this).hide();
|
||||
found++;
|
||||
} else {
|
||||
jQuery(this).fadeIn();
|
||||
jQuery('div.um-field[data-cond-'+i+'-field='+um_field_key+']').show();
|
||||
|
||||
}
|
||||
}
|
||||
//console.log( 'hide',i,value0, live_value );
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
um_field_relations[ k ] = new_arr_field;
|
||||
});
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
@@ -185,12 +285,6 @@ function UM_check_password_matched() {
|
||||
});
|
||||
}
|
||||
|
||||
jQuery(document).ready(function(){
|
||||
if(jQuery('input[data-key=user_password],input[data-key=confirm_user_password]').length == 2) {
|
||||
UM_check_password_matched();
|
||||
}
|
||||
});
|
||||
|
||||
function UM_hide_menus() {
|
||||
|
||||
menu = jQuery('.um-dropdown');
|
||||
@@ -824,11 +918,15 @@ function um_remove_modal(){
|
||||
}
|
||||
|
||||
function um_modal_size( aclass ) {
|
||||
|
||||
jQuery('.um-modal:visible').addClass(aclass);
|
||||
|
||||
}
|
||||
|
||||
function um_modal_add_attr( id, value ) {
|
||||
|
||||
jQuery('.um-modal:visible').data( id, value );
|
||||
|
||||
}
|
||||
|
||||
function prepare_Modal() {
|
||||
@@ -902,10 +1000,9 @@ jQuery(function(){
|
||||
}
|
||||
});
|
||||
|
||||
// Fixed touchscreen sensitivity
|
||||
jQuery(document).on('touchend', function(){
|
||||
//jQuery(".select2-search, .select2-focusser").remove();
|
||||
})
|
||||
if( jQuery('input[data-key=user_password],input[data-key=confirm_user_password]').length == 2 ) {
|
||||
UM_check_password_matched();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user