1/* global sumosubs_variation_switcher_params */ 2 3jQuery( function( $ ) { 4 // sumosubs_variation_switcher_params is required to continue, ensure the object exists 5 if ( typeof sumosubs_variation_switcher_params === 'undefined' ) { 6 return false; 7 } 8 9 var $variation_switcher_div = $( '.sumo_subscription_variation_switcher, .sumo_subscription_details' ).closest( 'div' ); 10 11 var is_blocked = function( $node ) { 12 return $node.is( '.processing' ) || $node.parents( '.processing' ).length; 13 }; 14 15 /** 16 * Block a node visually for processing. 17 * 18 * @param {JQuery Object} $node 19 */ 20 var block = function( $node ) { 21 if ( ! is_blocked( $node ) ) { 22 $node.addClass( 'processing' ).block( { 23 message : null, 24 overlayCSS : { 25 background : '#fff', 26 opacity : 0.6 27 } 28 } ); 29 } 30 }; 31 32 /** 33 * Unblock a node after processing is complete. 34 * 35 * @param {JQuery Object} $node 36 */ 37 var unblock = function( $node ) { 38 $node.removeClass( 'processing' ).unblock(); 39 }; 40 41 var variation_switcher = { 42 /** 43 * Manage Variation Switcher UI events. 44 */ 45 init : function() { 46 $( document ).on( 'click', '.variation_switch_button', this.loadVariationSwitcher ); 47 $( document ).on( 'click', '.variation_switch_submit', this.onSubmitVariationSwitch ); 48 $( document ).on( 'click', '.reset_variation_switch', this.onResetVariationSwitch ); 49 $( document ).on( 'change', '.variation_attribute_switch_selector', this.onLoadVariationAttributesUponSwitch ); 50 }, 51 loadVariationSwitcher : function( evt ) { 52 var $this = $( evt.currentTarget ); 53 var $post_id = $this.data( 'post_id' ); 54 55 $this.hide( ); 56 57 $( '.variation_attribute_switch_selector_' + $post_id ).show( ); 58 $( '#variation_switch_submit_' + $post_id ).show( ); 59 }, 60 onResetVariationSwitch : function( evt ) { 61 var $this = $( evt.currentTarget ); 62 var $post_id = $this.data( 'post_id' ); 63 64 variation_switcher.resetVariationSwitch( '#reset_variation_switch_' + $post_id, $post_id ); 65 $this.hide( ); 66 }, 67 onSubmitVariationSwitch : function( evt ) { 68 var $this = $( evt.currentTarget ); 69 var $post_id = $this.attr( 'data-post_id' ); 70 var $plan_matched_attributes_key = $this.data( 'plan_matched_attributes_key' ); 71 var attribute_value_to_switch = [ ]; 72 var attribute_selected = [ ]; 73 74 $.each( $plan_matched_attributes_key, function( key, attribute_value ) { 75 var attr_selected = $( '#variation_attribute_selected_' + attribute_value + '_' + $post_id ); 76 var attr_field = $( '#variation_attribute_switch_selector_' + attribute_value + '_' + $post_id ); 77 78 attribute_selected.push( attr_selected.val() ); 79 attribute_value_to_switch.push( attr_field.val() ); 80 } ); 81 82 if ( $( attribute_value_to_switch ).not( attribute_selected ).length === 0 && $( attribute_selected ).not( attribute_value_to_switch ).length === 0 ) { 83 alert( sumosubs_variation_switcher_params.same_variation_message ); 84 return false; 85 } 86 87 $.blockUI.defaults.overlayCSS.cursor = 'wait'; 88 block( $variation_switcher_div ); 89 90 $.ajax( { 91 type : 'POST', 92 url : sumosubs_variation_switcher_params.wp_ajax_url, 93 data : { 94 action : 'sumosubscription_save_swapped_subscription_variation', 95 security : sumosubs_variation_switcher_params.variation_switch_submit_nonce, 96 post_id : $post_id, 97 switched_by : sumosubs_variation_switcher_params.switched_by, 98 plan_matched_attributes_key : $plan_matched_attributes_key, 99 attribute_value_to_switch : attribute_value_to_switch 100 }, 101 success : function( data ) { 102 if ( data === '1' ) { 103 alert( sumosubs_variation_switcher_params.success_message ); 104 } else if ( data === '2' ) { 105 alert( sumosubs_variation_switcher_params.notice_message ); 106 } else { 107 alert( sumosubs_variation_switcher_params.failure_message ); 108 } 109 110 variation_switcher.resetVariationSwitch( '#reset_variation_switch_' + $post_id, $post_id ); 111 }, 112 complete : function() { 113 unblock( $variation_switcher_div ); 114 window.location.reload(); 115 } 116 } ); 117 }, 118 onLoadVariationAttributesUponSwitch : function( evt ) { 119 var $this = $( evt.currentTarget ); 120 var $this_selected_attribute_value = $this.val( ); 121 var $this_selected_attribute_key = $this.data( 'selected_attribute_key' ); 122 var $plan_matched_attributes_key = $this.data( 'plan_matched_attributes_key' ); 123 var $post_id = $this.data( 'post_id' ); 124 var this_selected_attributes = [ ], this_selected_attributes_val = [ ]; 125 var object = { }; 126 127 $( '#load_variation_attributes_' + $post_id ).show(); 128 $( '#reset_variation_switch_' + $post_id ).show(); 129 130 if ( $this_selected_attribute_value.indexOf( 'attribute_' ) !== - 1 ) { 131 $( '#load_variation_attributes_' + $post_id ).hide(); 132 return false;
133 } 134 135 $.each( $plan_matched_attributes_key, function( index, matched_attribute_key ) { 136 var $matched_attribute_value = $( '#variation_attribute_switch_selector_' + matched_attribute_key + '_' + $post_id ).val(); 137 138 if ( 'attribute_' + $matched_attribute_value !== matched_attribute_key ) { 139 object[matched_attribute_key] = $matched_attribute_value; 140 this_selected_attributes.push( object ); 141 this_selected_attributes_val.push( $matched_attribute_value ); 142 } 143 } ); 144 145 $.ajax( { 146 type : 'POST', 147 url : sumosubs_variation_switcher_params.wp_ajax_url, 148 data : { 149 action : 'sumosubscription_get_subscription_variation_attributes_upon_switch', 150 security : sumosubs_variation_switcher_params.variation_swapping_nonce, 151 post_id : $post_id, 152 selected_attributes : this_selected_attributes, 153 selected_attribute_key : $this_selected_attribute_key, 154 selected_attribute_value : $this_selected_attribute_value, 155 }, 156 success : function( plan_matched_variation ) { 157 $( '#load_variation_attributes_' + $post_id ).hide(); 158 159 var filtered_attributes_value = [ ]; 160 161 $.each( $plan_matched_attributes_key, function( index, matched_attribute_key ) { 162 if ( plan_matched_variation !== '' ) { 163 var default_select_caption = sumosubs_variation_switcher_params.i18n_default_attribute_select + matched_attribute_key.replace( 'attribute_', '' ) + ' ..'; 164 165 filtered_attributes_value.push( default_select_caption ); 166 167 $.each( plan_matched_variation, function( index, variations ) { 168 $.each( variations, function( attribute_key, attribute_value ) { 169 if ( typeof ( attribute_value ) === 'object' ) { 170 $.each( attribute_value, function( index, each_attr_value ) { 171 if ( attribute_key === matched_attribute_key && $.inArray( each_attr_value, filtered_attributes_value ) === - 1 ) { 172 filtered_attributes_value.push( each_attr_value ); 173 } 174 } ); 175 } else { 176 if ( attribute_key === matched_attribute_key && $.inArray( attribute_value, filtered_attributes_value ) === - 1 ) { 177 filtered_attributes_value.push( attribute_value ); 178 } 179 } 180 } ); 181 } ); 182 var $elements = $( '#variation_attribute_switch_selector_' + matched_attribute_key + '_' + $post_id ); 183 $elements.empty( ); 184 185 if ( filtered_attributes_value === '' ) { 186 $( '#variation_attribute_switch_selector_' + matched_attribute_key + '_' + $post_id ).hide( ); 187 } else { 188 $.each( filtered_attributes_value, function( key, value ) { 189 if ( value !== '' ) { 190 if ( default_select_caption === value ) { 191 $elements.append( $( '<option></option>' ).attr( 'value', matched_attribute_key ).text( sumosubs_variation_switcher_params.i18n_default_attribute_select + matched_attribute_key.replace( 'attribute_', '' ) ) ); 192 } else if ( key > 0 ) { 193 if ( $.inArray( value, this_selected_attributes_val ) !== - 1 ) { 194 $elements.append( $( '<option></option>' ).attr( 'value', value ).text( value ).prop( 'selected', true ) ); 195 } else { 196 $elements.append( $( '<option></option>' ).attr( 'value', value ).text( value ) ); 197 } 198 } 199 } 200 } ); 201 } 202 filtered_attributes_value = [ ]; 203 } else { 204 variation_switcher.resetVariationSwitch( '#reset_variation_switch_' + $post_id, $post_id ); 205 $( '.reset_variation_switch' ).hide( ); 206 } 207 } ); 208 }, 209 error : function( ) { 210 $( '#load_variation_attributes_' + $post_id ).hide(); 211 212 alert( sumosubs_variation_switcher_params.failure_message ); 213 }, 214 dataType : 'json', 215 async : false 216 } ); 217 }, 218 resetVariationSwitch : function( evt, $post_id ) { 219 var $plan_matched_attributes = $( evt ).data( 'plan_matched_attributes' ); 220 var $plan_matched_attributes_key = $( evt ).data( 'plan_matched_attributes_key' ); 221 var filtered_attributes_value = [ ]; 222 223 $.each( $plan_matched_attributes_key, function( index, matched_attribute_key ) { 224 $.each( $plan_matched_attributes, function( key, options ) { 225 $.each( options, function( attribute_key, attribute_value ) { 226 227 if ( 'attribute_' + key === matched_attribute_key ) { 228 if ( $.inArray( key, filtered_attributes_value ) === - 1 ) { 229 filtered_attributes_value.push( key ); 230 } 231 232 filtered_attributes_value.push( attribute_value ); 233 } 234 } ); 235 } ); 236 var $elements = $( '#variation_attribute_switch_selector_' + matched_attribute_key + '_' + $post_id ); 237 $elements.empty( ); 238 239 $.each( filtered_attributes_value, function( key, value ) { 240 if ( key === 0 ) { 241 $elements.append( $( '<option></option>' ).attr( 'value', value ).text( sumosubs_variation_switcher_params.i18n_default_attribute_select + value + ' ..' ) ); 242 } else { 243 $elements.append( $( '<option></option>' ).attr( 'value', value ).text( value ) ); 244 } 245 } ); 246 filtered_attributes_value = [ ]; 247 } ); 248 $( '.variation_attribute_switch_selector_' + $post_id ).show( ); 249 } 250 }; 251 252 variation_switcher.init(); 253} );
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.