| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 @license | 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also | 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> | 9 --> |
| 10 | 10 |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 /** | 79 /** |
| 80 * Regular expression to match valid input characters. | 80 * Regular expression to match valid input characters. |
| 81 */ | 81 */ |
| 82 allowedPattern: { | 82 allowedPattern: { |
| 83 type: String | 83 type: String |
| 84 }, | 84 }, |
| 85 | 85 |
| 86 _previousValidInput: { | 86 _previousValidInput: { |
| 87 type: String, | 87 type: String, |
| 88 value: '' | 88 value: '' |
| 89 }, |
| 90 |
| 91 _patternAlreadyChecked: { |
| 92 type: Boolean, |
| 93 value: false |
| 89 } | 94 } |
| 90 | 95 |
| 91 }, | 96 }, |
| 92 | 97 |
| 93 listeners: { | 98 listeners: { |
| 94 'input': '_onInput', | 99 'input': '_onInput', |
| 95 'keydown': '_onKeydown' | 100 'keypress': '_onKeypress' |
| 96 }, | 101 }, |
| 97 | 102 |
| 98 get _patternRegExp() { | 103 get _patternRegExp() { |
| 99 var pattern; | 104 var pattern; |
| 100 if (this.allowedPattern) { | 105 if (this.allowedPattern) { |
| 101 pattern = new RegExp(this.allowedPattern); | 106 pattern = new RegExp(this.allowedPattern); |
| 102 } else if (this.pattern) { | 107 } else if (this.pattern) { |
| 103 pattern = new RegExp(this.pattern); | 108 pattern = new RegExp(this.pattern); |
| 104 } else { | 109 } else { |
| 105 switch (this.type) { | 110 switch (this.type) { |
| 106 case 'number': | 111 case 'number': |
| 107 pattern = /[0-9.,e-]/; | 112 pattern = /[0-9.,e-]/; |
| 108 break; | 113 break; |
| 109 } | 114 } |
| 110 } | 115 } |
| 111 return pattern; | 116 return pattern; |
| 112 }, | 117 }, |
| 113 | 118 |
| 114 ready: function() { | 119 ready: function() { |
| 115 this.bindValue = this.value; | 120 this.bindValue = this.value; |
| 116 }, | 121 }, |
| 117 | 122 |
| 118 _bindValueChanged: function() { | 123 _bindValueChanged: function() { |
| 119 if (this.value !== this.bindValue) { | 124 if (this.value !== this.bindValue) { |
| 120 this.value = this.bindValue; | 125 this.value = !this.bindValue ? '' : this.bindValue; |
| 121 } | 126 } |
| 122 // manually notify because we don't want to notify until after setting val
ue | 127 // manually notify because we don't want to notify until after setting val
ue |
| 123 this.fire('bind-value-changed', {value: this.bindValue}); | 128 this.fire('bind-value-changed', {value: this.bindValue}); |
| 124 }, | 129 }, |
| 125 | 130 |
| 126 _onInput: function() { | 131 _onInput: function() { |
| 132 // Need to validate each of the characters pasted if they haven't |
| 133 // been validated inside `_onKeypress` already. |
| 134 if (this.preventInvalidInput && !this._patternAlreadyChecked) { |
| 135 var valid = this._checkPatternValidity(); |
| 136 if (!valid) { |
| 137 this.value = this._previousValidInput; |
| 138 } |
| 139 } |
| 140 |
| 127 this.bindValue = this.value; | 141 this.bindValue = this.value; |
| 142 this._previousValidInput = this.value; |
| 143 this._patternAlreadyChecked = false; |
| 128 }, | 144 }, |
| 129 | 145 |
| 130 _isPrintable: function(keyCode) { | 146 _isPrintable: function(event) { |
| 131 var printable = | 147 // What a control/printable character is varies wildly based on the browse
r. |
| 132 (keyCode > 47 && keyCode < 58) || // number keys | 148 // - most control characters (arrows, backspace) do not send a `keypress`
event |
| 133 keyCode == 32 || keyCode == 13 || // spacebar & return key | 149 // in Chrome, but the *do* on Firefox |
| 134 (keyCode > 64 && keyCode < 91) || // letter keys | 150 // - in Firefox, when they do send a `keypress` event, control chars have |
| 135 (keyCode > 95 && keyCode < 112) || // numpad keys | 151 // a charCode = 0, keyCode = xx (for ex. 40 for down arrow) |
| 136 (keyCode > 185 && keyCode < 193) || // ;=,-./` (in order) | 152 // - printable characters always send a keypress event. |
| 137 (keyCode > 218 && keyCode < 223); // [\]' (in order) | 153 // - in Firefox, printable chars always have a keyCode = 0. In Chrome, the
keyCode |
| 138 return printable; | 154 // always matches the charCode. |
| 155 // None of this makes any sense. |
| 156 |
| 157 var nonPrintable = |
| 158 (event.keyCode == 8) || // backspace |
| 159 (event.keyCode == 19) || // pause |
| 160 (event.keyCode == 20) || // caps lock |
| 161 (event.keyCode == 27) || // escape |
| 162 (event.keyCode == 45) || // insert |
| 163 (event.keyCode == 46) || // delete |
| 164 (event.keyCode == 144) || // num lock |
| 165 (event.keyCode == 145) || // scroll lock |
| 166 (event.keyCode > 32 && event.keyCode < 41) || // page up/down, end, ho
me, arrows |
| 167 (event.keyCode > 111 && event.keyCode < 124); // fn keys |
| 168 |
| 169 return !(event.charCode == 0 && nonPrintable); |
| 139 }, | 170 }, |
| 140 | 171 |
| 141 // convert printable numpad keys to number keys | 172 _onKeypress: function(event) { |
| 142 _correctNumpadKeys: function(keyCode) { | |
| 143 return (keyCode > 95 && keyCode < 112) ? keyCode - 48 : keyCode; | |
| 144 }, | |
| 145 | |
| 146 _onKeydown: function(event) { | |
| 147 if (!this.preventInvalidInput && this.type !== 'number') { | 173 if (!this.preventInvalidInput && this.type !== 'number') { |
| 148 return; | 174 return; |
| 149 } | 175 } |
| 150 var regexp = this._patternRegExp; | 176 var regexp = this._patternRegExp; |
| 151 if (!regexp) { | 177 if (!regexp) { |
| 152 return; | 178 return; |
| 153 } | 179 } |
| 154 var thisChar = String.fromCharCode(this._correctNumpadKeys(event.keyCode))
; | 180 |
| 155 if (this._isPrintable(event.keyCode) && !regexp.test(thisChar)) { | 181 // Handle special keys and backspace |
| 182 if (event.metaKey || event.ctrlKey || event.altKey) |
| 183 return; |
| 184 |
| 185 // Check the pattern either here or in `_onInput`, but not in both. |
| 186 this._patternAlreadyChecked = true; |
| 187 |
| 188 var thisChar = String.fromCharCode(event.charCode); |
| 189 if (this._isPrintable(event) && !regexp.test(thisChar)) { |
| 156 event.preventDefault(); | 190 event.preventDefault(); |
| 157 } | 191 } |
| 158 }, | 192 }, |
| 159 | 193 |
| 194 _checkPatternValidity: function() { |
| 195 var regexp = this._patternRegExp; |
| 196 if (!regexp) { |
| 197 return true; |
| 198 } |
| 199 for (var i = 0; i < this.value.length; i++) { |
| 200 if (!regexp.test(this.value[i])) { |
| 201 return false; |
| 202 } |
| 203 } |
| 204 return true; |
| 205 }, |
| 206 |
| 160 /** | 207 /** |
| 161 * Returns true if `value` is valid. The validator provided in `validator` w
ill be used first, | 208 * Returns true if `value` is valid. The validator provided in `validator` w
ill be used first, |
| 162 * then any constraints. | 209 * then any constraints. |
| 163 * @return {Boolean} True if the value is valid. | 210 * @return {boolean} True if the value is valid. |
| 164 */ | 211 */ |
| 165 validate: function() { | 212 validate: function() { |
| 166 // Empty, non-required input is valid. | 213 // Empty, non-required input is valid. |
| 167 if (!this.required && this.value == '') | 214 if (!this.required && this.value == '') { |
| 215 this.invalid = false; |
| 168 return true; | 216 return true; |
| 169 | 217 } |
| 218 |
| 170 var valid; | 219 var valid; |
| 171 if (this.hasValidator()) { | 220 if (this.hasValidator()) { |
| 172 valid = Polymer.IronValidatableBehavior.validate.call(this, this.value); | 221 valid = Polymer.IronValidatableBehavior.validate.call(this, this.value); |
| 173 } else { | 222 } else { |
| 174 this.invalid = !this.validity.valid; | 223 this.invalid = !this.validity.valid; |
| 175 valid = this.validity.valid; | 224 valid = this.validity.valid; |
| 176 } | 225 } |
| 177 this.fire('iron-input-validate'); | 226 this.fire('iron-input-validate'); |
| 178 return valid; | 227 return valid; |
| 179 } | 228 } |
| 180 | 229 |
| 181 }); | 230 }); |
| 182 | 231 |
| 183 /* | 232 /* |
| 184 The `iron-input-validate` event is fired whenever `validate()` is called. | 233 The `iron-input-validate` event is fired whenever `validate()` is called. |
| 185 @event iron-input-validate | 234 @event iron-input-validate |
| 186 */ | 235 */ |
| 187 | 236 |
| 188 </script> | 237 </script> |
| OLD | NEW |