All Downloads are FREE. Search and download functionalities are using the official Maven repository.

META-INF.resources.butterfaces-ts.butterfaces-maxlength.jquery.ts Maven / Gradle / Ivy

The newest version!
///
///
///

(function ($: any) {
    $.fn.butterMaxLength = function (maxLength: number, maxLengthText: string) {
        let ERROR_STYLE_CLASS = "has-error";

        return this.each(function () {
            // console.log("initializing max length");
            let $root = $(this);
            let $valueElement = $root.find("textarea");
            let $maxLength = $root.find(".butter-component-maxlength-counter");

            if ($maxLength.length > 0) {
                // console.log("found max length element");
                let hasInitialValidationError = $root.hasClass(ERROR_STYLE_CLASS);

                let _checkValue = function () {
                    let value = $valueElement.val();
                    // console.log("checking value");
                    // console.log(value);
                    if (!ButterFaces.Object.isNullOrUndefined(value)) {
                        let freeLetterCount = maxLength - value.length;
                        let formatted = ButterFaces.String.format(maxLengthText, [freeLetterCount, maxLength]);
                        $maxLength.text(formatted);

                        if (!hasInitialValidationError) {
                            if (freeLetterCount < 0) {
                                $root.addClass(ERROR_STYLE_CLASS);
                            } else {
                                $root.removeClass(ERROR_STYLE_CLASS);
                            }
                        }
                    }
                };

                $valueElement.on("focus blur keyup cut paste", _checkValue);

                // initial check
                _checkValue();
            }
        });
    };
}(jQuery));




© 2015 - 2025 Weber Informatics LLC | Privacy Policy