daily pastebin goal
82%
SHARE
TWEET

Untitled

a guest May 16th, 2018 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function ($) {
  2.   Drupal.behaviors.schoeller_pricing = {
  3.     attach: function(context) {
  4.       var $placeholder = $('.price-range-replaceholder', context);
  5.       if (!$placeholder.length) {
  6.         return;
  7.       }
  8.      $placeholder.closest('.node').find('input[name="quantity"]').bind('keyup', this.quantity_update_handler);
  9.     },
  10.     quantity_update_handler: function() {
  11.       var $this = $(this),
  12.           quantity = parseInt($this.val(), 10);
  13.           $placeholder = $this.closest('.node').find('.price-range-replaceholder'),
  14.           placeholderText = $placeholder.text(),
  15.           ranges = $placeholder.data('ranges'),
  16.           pps = $placeholder.data('pps'),
  17.           currency = $placeholder.data('currency'),
  18.           before = placeholderText.search(/[0-9,.]+/) !== 0,
  19.           wasComma = false,
  20.           amount = 0;
  21.       if (typeof pps == "undefined") {
  22.         currency = placeholderText.match(/[^0-9,.]+/)[0];
  23.         pps = placeholderText.match(/[0-9,.]+/)[0];
  24.         $placeholder.data({'currency': currency, 'pps': pps});
  25.       }
  26.       // if it's a comma number, replace all periods with commas and vice versa
  27.       if (pps.charAt(pps.length - 3) == ',') {
  28.         wasComma = true;
  29.         pps = pps.replace(/[.]/g, '§').replace(/[,]/g, '.').replace(/[§]/g, ',');
  30.       }
  31.       ranges.amount = parseFloat(pps, 10) - (ranges.unit / 100);
  32.      
  33.       if (quantity < ranges.ranges[0]) {
  34.         amount = ranges.amount;
  35.       }
  36.       else {
  37.         // loop through ranges in reverse order, break as soon as we got a match
  38.         for (var i = ranges.ranges.length; i > 0; i--) {
  39.           if (ranges.ranges[i-1] <= quantity) {
  40.             // amount - percentage
  41.             amount = (ranges.amount - (ranges.amount * (ranges.percentages[i-1] / 100)));
  42.             break;
  43.           }
  44.         }
  45.       }
  46.       // Done doing maths, re-add unit price and make into a string
  47.       amount = ((ranges.unit / 100) + amount).toString();
  48.       // Add trailing 0s if necessary
  49.       var pos = amount.search(/[.]/);
  50.       // No decimals
  51.       if (pos === -1) {
  52.         amount = amount + '.00';
  53.       }
  54.       // 1 decimal
  55.       if (pos === (amount.length - 2)) {
  56.         amount = amount + '0';
  57.       }
  58.       // if it was a comma number, turn it back into one
  59.       if (wasComma) {
  60.         amount.replace(/[.]/g, '§').replace(/[,]/g, '.').replace(/[§]/g, ',');
  61.       }
  62.       // Re-add the currency symbol
  63.       if (before) {
  64.         amount = currency + amount;
  65.       }
  66.       else {
  67.         amount = amount + currency;
  68.       }
  69.       // @TODO: thousands delimiter?
  70.       $placeholder.text(amount);
  71.     }
  72.   }
  73. })(jQuery);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top