UNPKG

@haxtheweb/haxcms-nodejs

Version:

HAXcms single and multisite nodejs server, api, and administration

80 lines (64 loc) 6.6 kB
/** @license Copyright (c) 2017 Vaadin Ltd. This program is available under Apache License Version 2.0, available at https://vaadin.com/license/ */ import"../../../@polymer/polymer/polymer-element.js";import"../../../@polymer/polymer/lib/elements/custom-style.js";import{TextFieldElement as e}from"./vaadin-text-field.js";import{DomModule as t}from"../../../@polymer/polymer/lib/elements/dom-module.js";import{html as i}from"../../../@polymer/polymer/lib/utils/html-tag.js";const n=i`<dom-module id="vaadin-number-field-template"> <template> <style> :host([readonly]) [part\$="button"] { pointer-events: none; } [part="decrease-button"]::before { content: "−"; } [part="increase-button"]::before { content: "+"; } [part="decrease-button"], [part="increase-button"] { -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } /* Hide the native arrow icons */ [part="value"]::-webkit-outer-spin-button, [part="value"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; } [part="value"] { /* Older Firefox versions (v47.0) requires !important */ -moz-appearance: textfield !important; } :host([dir="rtl"]) [part="input-field"] { direction: ltr; } :host([dir="rtl"]) [part="value"]::placeholder { direction: rtl; } :host([dir="rtl"]) [part="input-field"] ::slotted(input)::placeholder { direction: rtl; } :host([dir="rtl"]) [part="value"]:-ms-input-placeholder, :host([dir="rtl"]) [part="input-field"] ::slotted(input):-ms-input-placeholder { direction: rtl; } :host([dir="rtl"]:not([has-controls])) [part="value"]::placeholder { text-align: left; } :host([dir="rtl"]:not([has-controls])) [part="input-field"] ::slotted(input)::placeholder { text-align: left; } :host([dir="rtl"]:not([has-controls])) [part="value"]:-ms-input-placeholder, :host([dir="rtl"]:not([has-controls])) [part="input-field"] ::slotted(input):-ms-input-placeholder { text-align: left; } </style> <div disabled\$="[[!_allowed(-1, value, min, max, step)]]" part="decrease-button" on-click="_decreaseValue" on-touchend="_decreaseButtonTouchend" hidden\$="[[!hasControls]]"> </div> <div disabled\$="[[!_allowed(1, value, min, max, step)]]" part="increase-button" on-click="_increaseValue" on-touchend="_increaseButtonTouchend" hidden\$="[[!hasControls]]"> </div> </template> </dom-module>`;let s;document.head.appendChild(n.content);class NumberFieldElement extends e{static get is(){return"vaadin-number-field"}static get version(){return"2.10.0"}static get properties(){return{hasControls:{type:Boolean,value:!1,reflectToAttribute:!0},min:{type:Number,reflectToAttribute:!0,observer:"_minChanged"},max:{type:Number,reflectToAttribute:!0,observer:"_maxChanged"},step:{type:Number,value:1,observer:"_stepChanged"}}}ready(){super.ready(),this.__previousValidInput=this.value||"",this.inputElement.type="number",this.inputElement.addEventListener("change",this.__onInputChange.bind(this))}_decreaseButtonTouchend(e){e.preventDefault(),this._decreaseValue()}_increaseButtonTouchend(e){e.preventDefault(),this._increaseValue()}static get template(){if(!s){s=super.template.cloneNode(!0);const e=t.import(this.is+"-template","template"),i=e.content.querySelector('[part="decrease-button"]'),n=e.content.querySelector('[part="increase-button"]'),r=e.content.querySelector("style"),a=s.content.querySelector('[part="input-field"]'),l=s.content.querySelector('[name="prefix"]');a.insertBefore(i,l),a.appendChild(n),s.content.appendChild(r)}return s}_createConstraintsObserver(){this._createMethodObserver("_constraintsChanged(required, minlength, maxlength, pattern, min, max, step)")}_constraintsChanged(e,t,i,n,s,r,a){if(!this.invalid)return;const isNumUnset=e=>!e&&0!==e;isNumUnset(s)&&isNumUnset(r)?super._constraintsChanged(e,t,i,n):this.validate()}_decreaseValue(){this._incrementValue(-1)}_increaseValue(){this._incrementValue(1)}_incrementValue(e){if(this.disabled||this.readonly)return;let t=parseFloat(this.value);this.value?t<this.min?(e=0,t=this.min):t>this.max&&(e=0,t=this.max):0==this.min&&e<0||0==this.max&&e>0||0==this.max&&0==this.min?(e=0,t=0):(null==this.max||this.max>=0)&&(null==this.min||this.min<=0)?t=0:this.min>0?(t=this.min,this.max<0&&e<0&&(t=this.max),e=0):this.max<0&&(t=this.max,e<0?e=0:this._getIncrement(1,t-this.step)>this.max?t-=2*this.step:t-=this.step);const i=this._getIncrement(e,t);this.value&&0!=e&&!this._incrementIsInsideTheLimits(e,t)||this._setValue(i)}_setValue(e){this.value=this._inputElementValue=String(parseFloat(e)),this.dispatchEvent(new CustomEvent("change",{bubbles:!0}))}_getIncrement(e,t){let i=this.step||1,n=this.min||0;const s=Math.max(this._getMultiplier(t),this._getMultiplier(i),this._getMultiplier(n));i*=s,n*=s;const r=((t=Math.round(t*s))-n)%i;return e>0?(t-r+i)/s:e<0?(t-(r||i))/s:t/s}_getDecimalCount(e){const t=String(e),i=t.indexOf(".");return-1===i?1:t.length-i-1}_getMultiplier(e){if(!isNaN(e))return Math.pow(10,this._getDecimalCount(e))}_incrementIsInsideTheLimits(e,t){return e<0?null==this.min||this._getIncrement(e,t)>=this.min:e>0?null==this.max||this._getIncrement(e,t)<=this.max:this._getIncrement(e,t)<=this.max&&this._getIncrement(e,t)>=this.min}_allowed(e){const t=e*(this.step||1),i=parseFloat(this.value);return!this.value||!this.disabled&&this._incrementIsInsideTheLimits(t,i)}_stepChanged(e,t){this.__validateByStep=this.__stepChangedCalled||null!==this.getAttribute("step"),this.inputElement.step=this.__validateByStep?e:"any",this.__stepChangedCalled=!0,this.setAttribute("step",e)}_minChanged(e){this.inputElement.min=e}_maxChanged(e){this.inputElement.max=e}_valueChanged(e,t){e&&isNaN(parseFloat(e))?this.value="":"string"!=typeof this.value&&(this.value=String(this.value)),super._valueChanged(this.value,t)}_onKeyDown(e){38==e.keyCode?(e.preventDefault(),this._increaseValue()):40==e.keyCode&&(e.preventDefault(),this._decreaseValue()),super._onKeyDown(e)}__onInputChange(){this.validate()}checkValidity(){return void 0!==this.min||void 0!==this.max||this.__validateByStep?this.inputElement.checkValidity():super.checkValidity()}_setHasInputValue(e){this._hasInputValue=e.target.value.length>0||e.target.validity.badInput}}window.customElements.define(NumberFieldElement.is,NumberFieldElement);export{NumberFieldElement};