API Docs for: 1.0.0
Show:

File: Resources/public/js/views/fields/ez-textline-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-textline-editview', function (Y) {
    "use strict";
    /**
     * Provides the field edit view for the Text Line (ezstring) fields
     *
     * @module ez-textline-editview
     */
    Y.namespace('eZ');

    var L = Y.Lang,
        FIELDTYPE_IDENTIFIER = 'ezstring';

    /**
     * Text Line edit view
     *
     * @namespace eZ
     * @class TextLineEditView
     * @constructor
     * @extends eZ.FieldEditView
     */
    Y.eZ.TextLineEditView = Y.Base.create('textLineEditView', Y.eZ.FieldEditView, [], {
        events: {
            '.ez-textline-input-ui input': {
                'blur': 'validate',
                'valuechange': 'validate'
            }
        },

        /**
         * Validates the current input of text line
         *
         * @method validate
         */
        validate: function () {
            var validity = this._getInputValidity(),
                config = this._variables();

            if ( validity.patternMismatch ) {
                // min length issue
                this.set(
                    'errorStatus',
                    L.sub('The value should have at least {minLength} characters', config)
                );
            } else if ( validity.valueMissing ) {
                this.set('errorStatus', Y.eZ.trans('this.field.is.required', {}, 'fieldedit'));
            } else {
                this.set('errorStatus', false);
            }
        },

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

            if ( def.validatorConfiguration.StringLengthValidator.minStringLength ) {
                // building the min length constraint for the pattern HTML5
                // attribute as minlength attribute does not exist...
                minLengthPattern = '.{' + def.validatorConfiguration.StringLengthValidator.minStringLength + ',}';
                minLength = def.validatorConfiguration.StringLengthValidator.minStringLength;
            }
            if ( def.validatorConfiguration.StringLengthValidator.maxStringLength ) {
                maxLength = def.validatorConfiguration.StringLengthValidator.maxStringLength;
            }

            return {
                "isRequired": def.isRequired || !!minLength,
                "maxLength": maxLength,
                "minLength": minLength,
                "minLengthPattern": minLengthPattern
            };
        },

        /**
         * Returns the input validity state object for the input generated by
         * the text line 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-textline-input-ui input').get('validity');
        },

        /**
         * Returns the currently filled text line value
         *
         * @protected
         * @method _getFieldValue
         * @return String
         */
        _getFieldValue: function () {
            return this.get('container').one('.ez-textline-input-ui input').get('value');
        }
    });

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