diff options
Diffstat (limited to 'plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js')
-rw-r--r-- | plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js | 175 |
1 files changed, 126 insertions, 49 deletions
diff --git a/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js b/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js index 34dc4dd9..9217e67a 100644 --- a/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js +++ b/plugins/jetpack/modules/widget-visibility/widget-conditions/widget-conditions.js @@ -1,6 +1,8 @@ /* jshint onevar: false, smarttabs: true */ -/* global ajaxurl */ /* global isRtl */ +/* global widget_conditions_parent_pages */ +/* global widget_conditions_data */ +/* global jQuery */ jQuery( function( $ ) { var widgets_shell = $( 'div#widgets-right' ); @@ -10,10 +12,12 @@ jQuery( function( $ ) { } function setWidgetMargin( $widget ) { + var currentWidth, + extra; if ( $( 'body' ).hasClass( 'wp-customizer' ) ) { // set the inside widget 2 top this way we can see the widget settings - $widget.find('.widget-inside').css( 'top', 0 ); + $widget.find( '.widget-inside' ).css( 'top', 0 ); return; } @@ -27,23 +31,20 @@ jQuery( function( $ ) { $widget.data( 'original-style', $widget.attr( 'style' ) ); } - var currentWidth = $widget.width(); + currentWidth = $widget.width(); if ( currentWidth < 400 ) { - var extra = 400 - currentWidth; - if( isRtl ) { + extra = 400 - currentWidth; + if ( isRtl ) { $widget.css( 'position', 'relative' ).css( 'right', '-' + extra + 'px' ).css( 'width', '400px' ); } else { $widget.css( 'position', 'relative' ).css( 'left', '-' + extra + 'px' ).css( 'width', '400px' ); } - } - } - else if ( $widget.data( 'original-style' ) ) { + } else if ( $widget.data( 'original-style' ) ) { // Restore any original inline styles when visibility is toggled off. $widget.attr( 'style', $widget.data( 'original-style' ) ).data( 'original-style', null ); - } - else { + } else { $widget.removeAttr( 'style' ); } } @@ -73,22 +74,24 @@ jQuery( function( $ ) { } ); widgets_shell.on( 'click.widgetconditions', 'a.add-condition', function( e ) { - e.preventDefault(); - var $condition = $( this ).closest( 'div.condition' ), - $conditionClone = $condition.clone().insertAfter( $condition ); + $conditionClone = $condition.clone().data( 'rule-major', '' ).data( 'rule-minor', '' ).data( 'has-children','' ).insertAfter( $condition ); + + e.preventDefault(); $conditionClone.find( 'select.conditions-rule-major' ).val( '' ); $conditionClone.find( 'select.conditions-rule-minor' ).html( '' ).attr( 'disabled' ); - $conditionClone.find( 'span.conditions-rule-has-children' ).hide().html( '' ); - } ); + $conditionClone.find( 'span.conditions-rule-has-children' ).hide().find( 'input[type="checkbox"]' ).removeAttr( 'checked' ); - widgets_shell.on( 'click.widgetconditions', 'a.display-options', function ( e ) { - e.preventDefault(); + resetRuleIndexes( $conditionClone.closest( '.conditions' ) ); + } ); + widgets_shell.on( 'click.widgetconditions', 'a.display-options', function( e ) { var $displayOptionsButton = $( this ), $widget = $displayOptionsButton.closest( 'div.widget' ); + e.preventDefault(); + $widget.find( 'div.widget-conditional' ).toggleClass( 'widget-conditional-hide' ); $( this ).toggleClass( 'active' ); $widget.toggleClass( 'expanded' ); @@ -96,23 +99,27 @@ jQuery( function( $ ) { if ( $( this ).hasClass( 'active' ) ) { $widget.find( 'input[name=widget-conditions-visible]' ).val( '1' ); + $widget.find( '.condition' ).each( function() { + buildMinorConditions( $( this ) ); + } ); } else { $widget.find( 'input[name=widget-conditions-visible]' ).val( '0' ); } - } ); widgets_shell.on( 'click.widgetconditions', 'a.delete-condition', function( e ) { - e.preventDefault(); - var $condition = $( this ).closest( 'div.condition' ); + e.preventDefault(); + if ( $condition.is( ':first-child' ) && $condition.is( ':last-child' ) ) { $( this ).closest( 'div.widget' ).find( 'a.display-options' ).click(); $condition.find( 'select.conditions-rule-major' ).val( '' ).change(); } else { $condition.detach(); } + + resetRuleIndexes( $condition.closest( '.conditions' ) ); } ); widgets_shell.on( 'click.widgetconditions', 'div.widget-top', function() { @@ -130,49 +137,119 @@ jQuery( function( $ ) { } } ); + widgets_shell.on( 'change.widgetconditions', 'input.conditions-match-all', function() { + $( this ).parents( '.widget-conditional' ) + .toggleClass( 'conjunction' ) + .toggleClass( 'intersection' ); + } ); + $( document ).on( 'change.widgetconditions', 'select.conditions-rule-major', function() { - var $conditionsRuleMajor = $ ( this ), + var $conditionsRuleMajor = $( this ), $conditionsRuleMinor = $conditionsRuleMajor.siblings( 'select.conditions-rule-minor:first' ), - $conditionsRuleHasChildren = $conditionsRuleMajor.siblings( 'span.conditions-rule-has-children' ); - - if ( $conditionsRuleMajor.val() ) { - if ( $conditionsRuleMajor.val() !== 'page' ){ - $conditionsRuleHasChildren.hide().html( '' ); - } + $conditionsRuleHasChildren = $conditionsRuleMajor.siblings( 'span.conditions-rule-has-children' ), + $condition = $conditionsRuleMinor.closest( '.condition' ); - $conditionsRuleMinor.html( '' ).append( $( '<option/>' ).text( $conditionsRuleMinor.data( 'loading-text' ) ) ); + $condition.data( 'rule-minor', '' ).data( 'rule-major', $conditionsRuleMajor.val() ); - var data = { - action: 'widget_conditions_options', - major: $conditionsRuleMajor.val() - }; - - jQuery.post( ajaxurl, data, function( html ) { - $conditionsRuleMinor.html( html ).removeAttr( 'disabled' ); - } ); + if ( $conditionsRuleMajor.val() ) { + buildMinorConditions( $condition ); } else { $conditionsRuleMajor.siblings( 'select.conditions-rule-minor' ).attr( 'disabled', 'disabled' ).html( '' ); - $conditionsRuleHasChildren.hide().html( '' ); + $conditionsRuleHasChildren.hide().find( 'input[type="checkbox"]' ).removeAttr( 'checked' ); } } ); $( document ).on( 'change.widgetconditions', 'select.conditions-rule-minor', function() { - var $conditionsRuleMinor = $ ( this ), + var $conditionsRuleMinor = $( this ), $conditionsRuleMajor = $conditionsRuleMinor.siblings( 'select.conditions-rule-major' ), - $conditionsRuleHasChildren = $conditionsRuleMinor.siblings( 'span.conditions-rule-has-children' ); + $conditionsRuleHasChildren = $conditionsRuleMinor.siblings( 'span.conditions-rule-has-children' ), + $condition = $conditionsRuleMinor.closest( '.condition' ); + + $condition.data( 'rule-minor', $conditionsRuleMinor.val() ); if ( $conditionsRuleMajor.val() === 'page' ) { - var data = { - action: 'widget_conditions_has_children', - major: $conditionsRuleMajor.val(), - minor: $conditionsRuleMinor.val() - }; - - jQuery.post( ajaxurl, data, function( html ) { - $conditionsRuleHasChildren.html( html ).show(); - } ); + if ( $conditionsRuleMinor.val() in widget_conditions_parent_pages ) { + $conditionsRuleHasChildren.show(); + } else { + $conditionsRuleHasChildren.hide().find( 'input[type="checkbox"]' ).removeAttr( 'checked' ); + } } else { - $conditionsRuleHasChildren.hide().html( '' ); + $conditionsRuleHasChildren.hide().find( 'input[type="checkbox"]' ).removeAttr( 'checked' ); } } ); + + $( document ).on( 'widget-updated widget-synced', function( e, widget ) { + widget.find( '.condition' ).each( function() { + buildMinorConditions( $( this ) ); + } ); + } ); + + function buildMinorConditions( condition ) { + var minor, + hasChildren, + majorData, + i, + j, + key, + val, + _len, + _jlen, + subkey, + subval, + optgroup, + select = condition.find( '.conditions-rule-minor' ).html( '' ), + major = condition.data( 'rule-major' ); + + if ( ! major ) { + select.attr( 'disabled', 'disabled' ); + return; + } + + minor = condition.data( 'rule-minor' ); + hasChildren = condition.data( 'rule-has-children' ); + majorData = widget_conditions_data[ major ]; + + for ( i = 0, _len = majorData.length; i < _len; i++ ) { + key = majorData[i][0]; + val = majorData[i][1]; + + if ( typeof val === 'object' ) { + optgroup = $( '<optgroup/>' ).attr( 'label', key ); + + for ( j = 0, _jlen = val.length; j < _jlen; j++ ) { + subkey = majorData[i][1][j][0]; + subval = majorData[i][1][j][1]; + + optgroup.append( $( '<option/>' ).val( subkey ).text( subval.replace( / /g, '\xA0' ) ) ); + } + + select.append( optgroup ); + } else { + select.append( $( '<option/>' ).val( key ).text( val.replace( / /g, '\xA0' ) ) ); + } + } + + select.removeAttr( 'disabled' ); + select.val( minor ); + + if ( 'page' === major && minor in widget_conditions_parent_pages ) { + select.siblings( 'span.conditions-rule-has-children' ).show(); + + if ( hasChildren ) { + select.siblings( 'span.conditions-rule-has-children' ).find( 'input[type="checkbox"]' ).attr( 'checked', 'checked' ); + } + } else { + select.siblings( 'span.conditions-rule-has-children' ).hide().find( 'input[type="checkbox"]' ).removeAttr( 'checked' ); + } + } + + function resetRuleIndexes( widget ) { + var index = 0; + widget.find( 'span.conditions-rule-has-children' ) + .find( 'input[type="checkbox"]' ) + .each( function() { + $( this ).attr( 'name', 'conditions[page_children][' + index + ']' ); + index++; + } ); + } } ); |