Skip to content

Instantly share code, notes, and snippets.

@boldsupport
Last active October 11, 2017 14:53
Show Gist options
  • Save boldsupport/aace0d0cd77bc2ab58e9b87315c9198e to your computer and use it in GitHub Desktop.
Save boldsupport/aace0d0cd77bc2ab58e9b87315c9198e to your computer and use it in GitHub Desktop.
function BTME(data_product_id){this.changes=[false,false,false,false,false];this.pricing_unit=null;this.pricing_multiplier=null;this.pricing_id=null;this.default_field_value=null;this.global_min=null;this.global_max=null;this.local_limits=[];this.commitTimeout=2000;this.product_id=data_product_id===undefined?null:data_product_id;}
BTME.prototype={constructor:BTME,setCommitTimeout:function(timeout){this.commitTimeout=timeout;},changePricingUnit:function(pricing_unit,multiplier,priceId){this.pricing_unit=pricing_unit;this.pricing_multiplier=multiplier;this.pricing_id=priceId;this.changes[0]=true;return this;},setGlobalMinimum:function(min){this.global_min=min;this.changes[1]=true;return this;},setGlobalMaximum:function(max){this.global_max=max;this.changes[2]=true;return this;},setFieldMinimum:function(fieldNumber,min,errorMessage){var fieldIndex=this.indexOfFieldLimits(fieldNumber-1);if(fieldIndex>-1){this.local_limits[fieldIndex][1]=min;this.local_limits[fieldIndex][3]=errorMessage;}
else{this.local_limits.push([fieldNumber-1,min,null,errorMessage,null]);}
this.changes[3]=true;return this;},setFieldMaximum:function(fieldNumber,max,errorMessage){var fieldIndex=this.indexOfFieldLimits(fieldNumber-1);if(fieldIndex>-1){this.local_limits[fieldIndex][2]=max;this.local_limits[fieldIndex][4]=errorMessage;}
else{this.local_limits.push([fieldNumber-1,null,max,null,errorMessage]);}
this.changes[4]=true;return this;},indexOfFieldLimits:function(fieldIndex){var result=-1;for(var i=0;i<this.local_limits.length;i++){if(this.local_limits[i][0]===fieldIndex){result=i;break;}}
return result;},commit:function(){var that=this;setTimeout(function(){if(that.product_id!=null&&jQuery('.bold-btm').attr('data-product-id')==that.product_id||that.product_id==null){if(that.changes[0]){var currency=jQuery('#'+that.pricing_id).html();var symbol=currency.charAt(0);var price=Number(currency.replace(/[^0-9\.]+/g,""));price=price*that.pricing_multiplier;jQuery('.btm_unit').html(that.pricing_unit);jQuery('#'+that.pricing_id).html(symbol+(price.toFixed(2)));}
if(that.changes[1]){jQuery('.btm_field').each(function(index){if(jQuery.inArray(index,jQuery.map(that.local_limits,function(v){return v[0];}))==-1){jQuery('#btm_field_'+index).on('change',function(){var quantity=jQuery('#btm_field_'+index).val();if(quantity<that.global_min){jQuery('#btm_field_'+index).val('');if(jQuery('.btm-error-msg'+index).length){jQuery('.btm-error-msg'+index).remove();}
jQuery('.measurement_div #'+index).find('.field_name').css('display','inline-block');jQuery('.measurement_div #'+index).find('.field_name').css('width','auto');jQuery('.measurement_div #'+index).find('.field_name').after('<span class="btm-error-msg'+index+'" style="color:red;margin-left:10px;display:inline-block;">*cannot be less than '+that.global_min+' '+jQuery('.measurement_div #'+index).find('label').eq(1).html().toLowerCase()+'.</span><div class="btm-error-msg'+index+'" style="display:block;"></div>');}
else if(that.global_max!=null){if(jQuery('.btm-error-msg'+index).length&&quantity<that.global_max){jQuery('.measurement_div #'+index).find('.field_name').css('display','block');jQuery('.btm-error-msg'+index).remove();}}
else{if(jQuery('.btm-error-msg'+index).length){jQuery('.measurement_div #'+index).find('.field_name').css('display','block');jQuery('.btm-error-msg'+index).remove();}}});}});}
if(that.changes[2]){jQuery('.btm_field').each(function(index){if(jQuery.inArray(index,jQuery.map(that.local_limits,function(v){return v[0];}))==-1){jQuery('#btm_field_'+index).on('change',function(){var quantity=jQuery('#btm_field_'+index).val();if(quantity>that.global_max){jQuery('#btm_field_'+index).val('');if(jQuery('.btm-error-msg'+index).length){jQuery('.btm-error-msg'+index).remove();}
jQuery('.measurement_div #'+index).find('.field_name').css('display','inline-block');jQuery('.measurement_div #'+index).find('.field_name').css('width','auto');jQuery('.measurement_div #'+index).find('.field_name').after('<span class="btm-error-msg'+index+'" style="color:red;margin-left:10px;display:inline-block;">*cannot be greater than '+that.global_max+' '+jQuery('.measurement_div #'+index).find('label').eq(1).html().toLowerCase()+'.</span><div class="btm-error-msg'+index+'" style="display:block;"></div>');}
else if(that.global_min!=null){if(jQuery('.btm-error-msg'+index).length&&quantity>that.global_min){jQuery('.measurement_div #'+index).find('.field_name').css('display','block');jQuery('.btm-error-msg'+index).remove();}}
else{if(jQuery('.btm-error-msg'+index).length){jQuery('.measurement_div #'+index).find('.field_name').css('display','block');jQuery('.btm-error-msg'+index).remove();}}});}});}
if(that.changes[3]||that.changes[4]){jQuery.each(that.local_limits,function(index){jQuery('#btm_field_'+that.local_limits[index][0]).on('change',function(){var quantity=jQuery('#btm_field_'+that.local_limits[index][0]).val();var hasError=false;if(that.local_limits[index][1]!=null){if(quantity<that.local_limits[index][1]){jQuery('#btm_field_'+that.local_limits[index][0]).val('');if(jQuery('.btm-error-msg'+that.local_limits[index][0]).length){jQuery('.btm-error-msg'+that.local_limits[index][0]).remove();}
jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').css('display','inline-block');jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').css('width','auto');jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').after('<span class="btm-error-msg'+that.local_limits[index][0]+'" style="color:red;margin-left:10px;display:inline-block;">*'+that.local_limits[index][3]+'</span><div class="btm-error-msg'+that.local_limits[index][0]+'" style="display:block;"></div>');hasError=true;}}
if(that.local_limits[index][2]!=null&&hasError==false){if(quantity>that.local_limits[index][2]){jQuery('#btm_field_'+that.local_limits[index][0]).val('');if(jQuery('.btm-error-msg'+that.local_limits[index][0]).length){jQuery('.btm-error-msg'+that.local_limits[index][0]).remove();}
jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').css('display','inline-block');jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').css('width','auto');jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').after('<span class="btm-error-msg'+that.local_limits[index][0]+'" style="color:red;margin-left:10px;display:inline-block;">*'+that.local_limits[index][4]+'</span><div class="btm-error-msg'+that.local_limits[index][0]+'" style="display:block;"></div>');hasError=true;}}
if(that.global_min!=null&&that.local_limits[index][1]==null){if(jQuery('.btm-error-msg'+that.local_limits[index][0]).length&&quantity<that.global_min){hasError=true;}}
if(that.global_max!=null&&that.local_limits[index][2]==null){if(jQuery('.btm-error-msg'+that.local_limits[index][0]).length&&quantity>that.global_max){hasError=true;}}
if(!hasError){jQuery('.measurement_div #'+that.local_limits[index][0]).find('.field_name').css('display','block');jQuery('.btm-error-msg'+that.local_limits[index][0]).remove();}});});}}},this.commitTimeout);return this;}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment