-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarcode-scanner-input.html
7 lines (5 loc) · 1.21 KB
/
barcode-scanner-input.html
1
2
3
4
5
6
7
<html><head><link rel="import" href="../polymer/polymer.html">
</head><body><dom-module id="barcode-scanner-input">
<script>!function(){Polymer({is:"barcode-scanner-input",properties:{code:{type:String,notify:!0,readOnly:!0},validateRegex:{type:String,value:"/^[0-9-]+$/",observer:"_onRegexChange"},disabled:{type:Boolean,value:!1,reflectToAttribute:!0,observer:"_onDisabled"},timeout:{type:Number,value:100}},ready:function(){this.TIMER=setTimeout(function(){},0),this._curCode="",window.addEventListener("keyup",this._handleKeyEvent.bind(this))},clear:function(){this.setCode("")},enable:function(){this.set("disabled",!1)},disable:function(){this.set("disabled",!0)},_onRegexChange:function(e){var t=e.split("/"),i=t[1],s=t[2];this._regex=new RegExp(i,s)},_onDisabled:function(e){this._curCode=""},_handleKeyEvent:function(e){this.disabled||(clearTimeout(this.TIMER),this.TIMER=setTimeout(this._validate.bind(this),this.timeout),e.keyCode<31||(this._curCode+=e.key))},_validate:function(){this.disabled||(this._curCode.match(this._regex)?(this._setCode(this._curCode),this.fire("barcode",this._curCode)):(this.fire("invalid-barcode",this._curCode),this._setCode("")),this._curCode="")}})}();</script>
</dom-module>
</body></html>