UNPKG

api-console-assets

Version:

This repo only exists to publish api console components to npm

219 lines (201 loc) 6.95 kB
<!-- @license Copyright 2016 The Advanced REST client authors <arc@mulesoft.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <link rel="import" href="../polymer/polymer.html"> <link rel="import" href="../iron-meta/iron-meta.html"> <script> /** * Singleton IronMeta instance. */ Polymer.IronValidatableBehaviorMeta = null; /** * ** Anypoint version of the iron-validatable-behavior ** * * This element is to be private and used as a replacemenet to any PolymerElements * * * `Use Polymer.IronValidatableBehavior` to implement an element that validates * user input. * Use the related `Polymer.IronValidatorBehavior` to add custom validation logic * to an iron-input. * * By default, an `<iron-form>` element validates its fields when the user presses the submit * button. * To validate a form imperatively, call the form's `validate()` method, which in turn will * call `validate()` on all its children. By using `Polymer.IronValidatableBehavior`, your * custom element will get a public `validate()`, which * will return the validity of the element, and a corresponding `invalid` attribute, * which can be used for styling. * * To implement the custom validation logic of your element, you must override * the protected `_getValidity()` method of this behaviour, rather than `validate()`. * See [this](https://github.com/PolymerElements/iron-form/blob/master/demo/simple-element.html) * for an example. * * ### Accessibility * * Changing the `invalid` property, either manually or by calling `validate()` will update the * `aria-invalid` attribute. * * @demo demo/index.html * @polymerBehavior */ Polymer.IronValidatableBehavior = { properties: { /** * Name of the validator or validators to use. * If the element should be validated by more than one validator then separate names with * space. See docs for `Polymer.PolymerValidatorBehavior` for description of how to define a * validator. */ validator: { type: String }, /** * After calling `validate()` this is be populated by latest result of the * test for each validator. Result item contains following properties: * * - validator {String} Name of the validator * - valid {Boolean} Result of the test * - message {String} Error message * * This property is `undefined` if `validator` is not set. */ validationStates: { type: Array, readOnly: true, notify: true }, /** * True if the last call to `validate` is invalid. */ invalid: { notify: true, reflectToAttribute: true, type: Boolean, value: false }, /** * This property is deprecated and should not be used. Use the global * validator meta singleton, `Polymer.IronValidatableBehaviorMeta` instead. */ _validatorMeta: { type: Object }, /** * Namespace for this validator. This property is deprecated and should * not be used. For all intents and purposes, please consider it a * read-only, config-time property. */ validatorType: { type: String, value: 'validator' }, /** * Overrides `Polymer.IronValidatableBehavior#_validator` * List of validators to use. */ _validator: { type: Array, computed: '__computeValidator(validator)' } }, observers: [ '_invalidChanged(invalid)' ], registered: function() { Polymer.IronValidatableBehaviorMeta = new Polymer.IronMeta({type: 'validator'}); }, _invalidChanged: function() { if (this.invalid) { this.setAttribute('aria-invalid', 'true'); } else { this.removeAttribute('aria-invalid'); } }, /** * Overrides `Polymer.IronValidatableBehavior#hasValidator` * @return {boolean} True if the validator `validator` exists. */ hasValidator: function() { return !!(this._validator && this._validator.length); }, /** * Returns true if the `value` is valid, and updates `invalid`. If you want * your element to have custom validation logic, do not override this method; * override `_getValidity(value)` instead. * @param {Object} value The value to be validated. By default, it is passed * to the validator's `validate()` function, if a validator is set. * @return {boolean} True if `value` is valid. */ validate: function(value) { this.invalid = !this._getValidity(value); return !this.invalid; }, /** * Overrides `Polymer.IronValidatableBehavior#hasValidator` * * Returns true if `value` is valid. By default, it is passed * to the validator's `validate()` function, if a validator is set. You * should override this method if you want to implement custom validity * logic for your element. * * @param {Object} value The value to be validated. * @return {boolean} True if `value` is valid. */ _getValidity: function(value) { if (this.hasValidator()) { var result = true; var states = []; this._validator.forEach(function(validator) { var validatorResult = { validator: validator.validatorName, message: validator.message }; if (!validator.validate(value)) { result = false; validatorResult.valid = false; } else { validatorResult.valid = true; } states.push(validatorResult); }); this._setValidationStates(states); return result; } return true; }, // Overrides `Polymer.IronValidatableBehavior#__computeValidator` __computeValidator: function() { if (!Polymer.IronValidatableBehaviorMeta) { return; } var validator = this.validator; if (!validator) { return; } var validatorsNames = validator.split(' '); if (validatorsNames.length === 0) { return; } var result = []; validatorsNames.forEach(function(name) { var validator = Polymer.IronValidatableBehaviorMeta.byKey(name); if (validator) { result.push(validator); } }); return result; }, }; </script>