API Docs for: 1.0.0
Show:

File: Resources/public/js/views/fields/ez-integer-editview.js

/*
 * Copyright (C) eZ Systems AS. All rights reserved.
 * For full copyright and license information view LICENSE file distributed with this source code.
 */
YUI.add('ez-integer-editview', function (Y) {
    "use strict";
    /**
     * Provides the field edit view for the Integer (ezinteger) fields
     *
     * @module ez-integer-editview
     */
    Y.namespace('eZ');

    var FIELDTYPE_IDENTIFIER = 'ezinteger',
        INTEGER_PATTERN = "-?\\d*"; // WARNING: each backslash is doubled, because it is escaped on output otherwise;

    /**
     * Integer edit view
     *
     * @namespace eZ
     * @class IntegerEditView
     * @constructor
     * @extends eZ.FieldEditView
     */
    Y.eZ.IntegerEditView = Y.Base.create('integerEditView', Y.eZ.FieldEditView, [], {
        events: {
            '.ez-integer-input-ui input': {
                'blur': 'validate',
                'valuechange': 'validate'
            }
        },

        /**
         * Validates the current input of Integer
         *
         * @method validate
         */
        validate: function () {
            var validity = this._getInputValidity(),
                config = this._variables(),
                inputValue = this.get('container').one('.ez-integer-input-ui input').get('value');

            if ( validity.valueMissing ) {
                this.set('errorStatus', Y.eZ.trans('this.field.is.required', {}, 'fieldedit'));
                // Integer pattern validation
            } else if ( validity.patternMismatch ) {
                this.set(
                    'errorStatus',
                    Y.eZ.trans('value.should.be.valid.integer', {}, 'fieldedit')
                );
                // Range validation
            } else if ( config.maxIntegerValue && inputValue > config.maxIntegerValue ) {
                this.set(
                    'errorStatus',
                    Y.eZ.trans('value.should.be.less.than', config, 'fieldedit')
                );
            } else if ( config.minIntegerValue && inputValue < config.minIntegerValue ) {
                this.set(
                    'errorStatus',
                    Y.eZ.trans('value.should.be.more.than', config, 'fieldedit')
                );

            } else {
                this.set('errorStatus', false);
            }
        },

        /**
         * Defines the variables to be imported in the field edit template for
         * integer
         *
         * @protected
         * @method _variables
         * @return {Object} containing isRequired, minIntegerValue and
         * maxIntegerValue entries
         */
        _variables: function () {
            var minIntegerValue = false,
                maxIntegerValue = false,
                def = this.get('fieldDefinition');

            if ( def.validatorConfiguration.IntegerValueValidator.minIntegerValue ) {
                minIntegerValue = def.validatorConfiguration.IntegerValueValidator.minIntegerValue;
            }
            if ( def.validatorConfiguration.IntegerValueValidator.maxIntegerValue ) {
                maxIntegerValue = def.validatorConfiguration.IntegerValueValidator.maxIntegerValue;
            }

            return {
                "isRequired": def.isRequired,
                "integerPattern": INTEGER_PATTERN,
                "minIntegerValue": minIntegerValue,
                "maxIntegerValue": maxIntegerValue
            };
        },

        /**
         * Returns the input validity state object for the input generated by
         * the Integer template
         *
         * See https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
         *
         * @protected
         * @method _getInputValidity
         * @return {ValidityState}
         */
        _getInputValidity: function () {
            return this.get('container').one('.ez-integer-input-ui input').get('validity');
        },

        /**
         * Returns the currently filled value
         *
         * @protected
         * @method _getFieldValue
         * @return Number
         */
        _getFieldValue: function () {
            return parseInt(this.get('container').one('.ez-integer-input-ui input').get('value'), 10);
        },
    });

    Y.eZ.FieldEditView.registerFieldEditView(
        FIELDTYPE_IDENTIFIER, Y.eZ.IntegerEditView
    );
});