Advertisement
Guest User

шоп

a guest
Mar 5th, 2015
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function () {
  2.     var textareaAutosizeInit = function () {
  3.         var containsText = function (value) {
  4.             return (value.replace(/\s/g, "").length > 0);
  5.         };
  6.  
  7.         $.fn.autoSize = function (options) {
  8.             $(this).each(function () {
  9.                 if (!$(this).is("textarea")) return;
  10.  
  11.                 var self = this;
  12.                 var height = this.offsetHeight;
  13.                 var diff = parseInt($(this).css("paddingBottom")) + parseInt($(this).css("paddingTop"));
  14.  
  15.                 // Firefox: scrollHeight isn't full height on border-box.
  16.                 if (this.scrollHeight + diff <= height) {
  17.                     diff = 0;
  18.                 }
  19.  
  20.                 if (containsText(self.value)) {
  21.                     $(this).height(this.scrollHeight);
  22.                 }
  23.  
  24.                 var maxHeight = parseInt($(this).css("max-height"));
  25.                 var scrollEnabled = false;
  26.                 this.style.overflowY = "hidden";
  27.  
  28.                 // Keyup is required for IE to properly reset height when deleting text.
  29.                 $(this).on("input keyup", function () {
  30.                     var currentHeight = self.offsetHeight;
  31.                     self.style.height = "auto";
  32.                     var newHeight = self.scrollHeight - diff;
  33.  
  34.                     if (newHeight >= maxHeight - diff) {
  35.                         newHeight = maxHeight - diff;
  36.                         if (!scrollEnabled) {
  37.                             self.style.overflowY = "scroll";
  38.                             scrollEnabled = true;
  39.                         }
  40.                     } else {
  41.                         if (scrollEnabled) {
  42.                             self.style.overflowY = "hidden";
  43.                             scrollEnabled = false;
  44.                         }
  45.                     }
  46.  
  47.                     if (currentHeight != newHeight) {
  48.                         self.style.height = newHeight + diff + "px";
  49.                         self.scrollTop = self.scrollHeight;
  50.                         if(options.callback)
  51.                         if (typeof options.callback == "function") {
  52.                             options.callback();
  53.                         }
  54.                     }
  55.                 });
  56.             });
  57.         };
  58.     };
  59.  
  60.     var selectInputsInit = function () {
  61.         var openedSelector = null;
  62.  
  63.         $(window).blur(function () {
  64.             if (openedSelector) {
  65.                 $(openedSelector).removeClass("opened");
  66.                 $("html").unbind("mousedown");
  67.                 openedSelector = null;
  68.             }
  69.         });
  70.  
  71.         $.fn.selectInputs = function () {
  72.             $(this).each(function () {
  73.                 if (!$(this).is("form")) return;
  74.  
  75.                 var inputs = {};
  76.                 var form = this;
  77.  
  78.                 $(this).find(".select-button, .select-field").each(function () {
  79.                     var inputName = $(this).data("input");
  80.                     var inputObj = $(form).find("input[name='" + inputName + "']")[0];
  81.  
  82.                     if (inputObj) {
  83.                         if (!inputs[inputName]) {
  84.                             inputs[inputName] = {
  85.                                 inputElement: inputObj,
  86.                                 selectButton: null,
  87.                                 selectField: null
  88.                             };
  89.                         }
  90.                         if ($(this).hasClass("select-field")) {
  91.                             inputs[inputName].selectField = this;
  92.                         } else {
  93.                             inputs[inputName].selectButton = this;
  94.                         }
  95.                     } else {
  96.                         throw{
  97.                             name: "SelectInputs error",
  98.                             message: "Couldn't find \"" + inputName + "\" input.",
  99.                             toString: function () {
  100.                                 return this.name + ": " + this.message;
  101.                             }
  102.                         };
  103.                     }
  104.                 });
  105.  
  106.                 $(this).find(".select-options").each(function () {
  107.                     var selectOptions = this;
  108.                     var inputName = $(this).data("input");
  109.                     var input = inputs[inputName];
  110.  
  111.                     if (input) {
  112.                         var defaultOption = null;
  113.  
  114.                         $(input.selectButton).mousedown(function (event) {
  115.                             event.stopPropagation();
  116.                             if ($(selectOptions).hasClass("opened")) {
  117.                                 $("html").unbind("mousedown");
  118.                                 openedSelector = null;
  119.                             } else {
  120.                                 var html = $("html");
  121.                                 if (openedSelector) {
  122.                                     $(openedSelector).removeClass("opened");
  123.                                     $(html).unbind("mousedown");
  124.                                 }
  125.                                 $(html).mousedown(function () {
  126.                                     $(selectOptions).removeClass("opened");
  127.                                     $(this).unbind("mousedown");
  128.                                 });
  129.                                 openedSelector = selectOptions;
  130.                             }
  131.                             $(selectOptions).toggleClass("opened");
  132.                         });
  133.                         $(input.selectField).mousedown(function (event) {
  134.                             event.stopPropagation();
  135.                             $(input.selectButton).mousedown();
  136.                         });
  137.  
  138.                         $(this).mousedown(function (event) {
  139.                             event.stopPropagation();
  140.                         });
  141.                         $(this).find("span").each(function () {
  142.                             var optionLabel = $(this).html();
  143.                             var optionValue = ($(this).data("value") ? $(this).data("value") : optionLabel);
  144.  
  145.                             $(this).mousedown(function (event) {
  146.                                 event.stopPropagation();
  147.                             });
  148.                             $(this).mouseup(function (event) {
  149.                                 input.inputElement.value = optionValue;
  150.                                 if (input.selectField) {
  151.                                     input.selectField.innerHTML = optionLabel;
  152.                                 }
  153.                                 $(selectOptions).removeClass("opened");
  154.                                 openedSelector = null;
  155.                                 $("html").unbind("mousedown");
  156.                             });
  157.                             $(this).click(function () {
  158.                                 event.preventDefault();
  159.                             });
  160.  
  161.                             if (!defaultOption && typeof $(this).data("selected") != "undefined") {
  162.                                 defaultOption = {
  163.                                     value: optionValue,
  164.                                     label: optionLabel
  165.                                 };
  166.                             }
  167.                         });
  168.  
  169.                         if (defaultOption) {
  170.                             input.inputElement.value = defaultOption.value;
  171.                             input.selectField.innerHTML = defaultOption.label;
  172.                         }
  173.                     }
  174.                 });
  175.             });
  176.         };
  177.     };
  178.  
  179.     var numberInputsInit = function () {
  180.         $.fn.numberOnly = function (min, max) {
  181.             $(this).each(function () {
  182.                 if (!$(this).is("input")) return;
  183.  
  184.                 var self = this;
  185.                 $(this).change(function () {
  186.                     var newValue = parseInt(self.value.replace(/[^0-9.]/g, ""));
  187.                     if (typeof min != "undefined" && newValue < min) {
  188.                         newValue = min;
  189.                     }
  190.                     if (typeof max != "undefined" && newValue > max) {
  191.                         newValue = max;
  192.                     }
  193.                     self.value = (!isNaN(newValue) ? newValue : "");
  194.                 });
  195.             });
  196.         }
  197.     };
  198.  
  199.     textareaAutosizeInit();
  200.     numberInputsInit();
  201.     selectInputsInit();
  202.  
  203.     $(document).ready(function () {
  204.         var form = $("#form");
  205.         form.selectInputs();
  206.         form.find("input[name='item-amount']").numberOnly(0, 1000000);
  207.         form.find("input[name='item-price']").numberOnly(0, 10000000);
  208.         form.find("textarea[name='item-comment']").autoSize({
  209.             callback: updateFooter
  210.         });
  211.     });
  212. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement