Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function () {
- var textareaAutosizeInit = function () {
- var containsText = function (value) {
- return (value.replace(/\s/g, "").length > 0);
- };
- $.fn.autoSize = function (options) {
- $(this).each(function () {
- if (!$(this).is("textarea")) return;
- var self = this;
- var height = this.offsetHeight;
- var diff = parseInt($(this).css("paddingBottom")) + parseInt($(this).css("paddingTop"));
- // Firefox: scrollHeight isn't full height on border-box.
- if (this.scrollHeight + diff <= height) {
- diff = 0;
- }
- if (containsText(self.value)) {
- $(this).height(this.scrollHeight);
- }
- var maxHeight = parseInt($(this).css("max-height"));
- var scrollEnabled = false;
- this.style.overflowY = "hidden";
- // Keyup is required for IE to properly reset height when deleting text.
- $(this).on("input keyup", function () {
- var currentHeight = self.offsetHeight;
- self.style.height = "auto";
- var newHeight = self.scrollHeight - diff;
- if (newHeight >= maxHeight - diff) {
- newHeight = maxHeight - diff;
- if (!scrollEnabled) {
- self.style.overflowY = "scroll";
- scrollEnabled = true;
- }
- } else {
- if (scrollEnabled) {
- self.style.overflowY = "hidden";
- scrollEnabled = false;
- }
- }
- if (currentHeight != newHeight) {
- self.style.height = newHeight + diff + "px";
- self.scrollTop = self.scrollHeight;
- if(options.callback)
- if (typeof options.callback == "function") {
- options.callback();
- }
- }
- });
- });
- };
- };
- var selectInputsInit = function () {
- var openedSelector = null;
- $(window).blur(function () {
- if (openedSelector) {
- $(openedSelector).removeClass("opened");
- $("html").unbind("mousedown");
- openedSelector = null;
- }
- });
- $.fn.selectInputs = function () {
- $(this).each(function () {
- if (!$(this).is("form")) return;
- var inputs = {};
- var form = this;
- $(this).find(".select-button, .select-field").each(function () {
- var inputName = $(this).data("input");
- var inputObj = $(form).find("input[name='" + inputName + "']")[0];
- if (inputObj) {
- if (!inputs[inputName]) {
- inputs[inputName] = {
- inputElement: inputObj,
- selectButton: null,
- selectField: null
- };
- }
- if ($(this).hasClass("select-field")) {
- inputs[inputName].selectField = this;
- } else {
- inputs[inputName].selectButton = this;
- }
- } else {
- throw{
- name: "SelectInputs error",
- message: "Couldn't find \"" + inputName + "\" input.",
- toString: function () {
- return this.name + ": " + this.message;
- }
- };
- }
- });
- $(this).find(".select-options").each(function () {
- var selectOptions = this;
- var inputName = $(this).data("input");
- var input = inputs[inputName];
- if (input) {
- var defaultOption = null;
- $(input.selectButton).mousedown(function (event) {
- event.stopPropagation();
- if ($(selectOptions).hasClass("opened")) {
- $("html").unbind("mousedown");
- openedSelector = null;
- } else {
- var html = $("html");
- if (openedSelector) {
- $(openedSelector).removeClass("opened");
- $(html).unbind("mousedown");
- }
- $(html).mousedown(function () {
- $(selectOptions).removeClass("opened");
- $(this).unbind("mousedown");
- });
- openedSelector = selectOptions;
- }
- $(selectOptions).toggleClass("opened");
- });
- $(input.selectField).mousedown(function (event) {
- event.stopPropagation();
- $(input.selectButton).mousedown();
- });
- $(this).mousedown(function (event) {
- event.stopPropagation();
- });
- $(this).find("span").each(function () {
- var optionLabel = $(this).html();
- var optionValue = ($(this).data("value") ? $(this).data("value") : optionLabel);
- $(this).mousedown(function (event) {
- event.stopPropagation();
- });
- $(this).mouseup(function (event) {
- input.inputElement.value = optionValue;
- if (input.selectField) {
- input.selectField.innerHTML = optionLabel;
- }
- $(selectOptions).removeClass("opened");
- openedSelector = null;
- $("html").unbind("mousedown");
- });
- $(this).click(function () {
- event.preventDefault();
- });
- if (!defaultOption && typeof $(this).data("selected") != "undefined") {
- defaultOption = {
- value: optionValue,
- label: optionLabel
- };
- }
- });
- if (defaultOption) {
- input.inputElement.value = defaultOption.value;
- input.selectField.innerHTML = defaultOption.label;
- }
- }
- });
- });
- };
- };
- var numberInputsInit = function () {
- $.fn.numberOnly = function (min, max) {
- $(this).each(function () {
- if (!$(this).is("input")) return;
- var self = this;
- $(this).change(function () {
- var newValue = parseInt(self.value.replace(/[^0-9.]/g, ""));
- if (typeof min != "undefined" && newValue < min) {
- newValue = min;
- }
- if (typeof max != "undefined" && newValue > max) {
- newValue = max;
- }
- self.value = (!isNaN(newValue) ? newValue : "");
- });
- });
- }
- };
- textareaAutosizeInit();
- numberInputsInit();
- selectInputsInit();
- $(document).ready(function () {
- var form = $("#form");
- form.selectInputs();
- form.find("input[name='item-amount']").numberOnly(0, 1000000);
- form.find("input[name='item-price']").numberOnly(0, 10000000);
- form.find("textarea[name='item-comment']").autoSize({
- callback: updateFooter
- });
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement