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