| OLD | NEW |
| (Empty) |
| 1 | |
| 2 | |
| 3 (function() { | |
| 4 | |
| 5 var paperInput = CoreStyle.g.paperInput = CoreStyle.g.paperInput || {}; | |
| 6 | |
| 7 paperInput.labelColor = '#757575'; | |
| 8 paperInput.focusedColor = '#4059a9'; | |
| 9 paperInput.invalidColor = '#d34336'; | |
| 10 | |
| 11 Polymer('paper-input-decorator',{ | |
| 12 | |
| 13 publish: { | |
| 14 | |
| 15 /** | |
| 16 * The label for this input. It normally appears as grey text inside | |
| 17 * the text input and disappears once the user enters text. | |
| 18 * | |
| 19 * @attribute label | |
| 20 * @type string | |
| 21 * @default '' | |
| 22 */ | |
| 23 label: '', | |
| 24 | |
| 25 /** | |
| 26 * If true, the label will "float" above the text input once the | |
| 27 * user enters text instead of disappearing. | |
| 28 * | |
| 29 * @attribute floatingLabel | |
| 30 * @type boolean | |
| 31 * @default false | |
| 32 */ | |
| 33 floatingLabel: false, | |
| 34 | |
| 35 /** | |
| 36 * Set to true to style the element as disabled. | |
| 37 * | |
| 38 * @attribute disabled | |
| 39 * @type boolean | |
| 40 * @default false | |
| 41 */ | |
| 42 disabled: {value: false, reflect: true}, | |
| 43 | |
| 44 /** | |
| 45 * Use this property to override the automatic label visibility. | |
| 46 * If this property is set to `true` or `false`, the label visibility | |
| 47 * will respect this value instead of be based on whether there is | |
| 48 * a non-null value in the input. | |
| 49 * | |
| 50 * @attribute labelVisible | |
| 51 * @type boolean | |
| 52 * @default false | |
| 53 */ | |
| 54 labelVisible: null, | |
| 55 | |
| 56 /** | |
| 57 * Set this property to true to show the error message. | |
| 58 * | |
| 59 * @attribute isInvalid | |
| 60 * @type boolean | |
| 61 * @default false | |
| 62 */ | |
| 63 isInvalid: false, | |
| 64 | |
| 65 /** | |
| 66 * The message to display if the input value fails validation. If this | |
| 67 * is unset or the empty string, a default message is displayed dependin
g | |
| 68 * on the type of validation error. | |
| 69 * | |
| 70 * @attribute error | |
| 71 * @type string | |
| 72 */ | |
| 73 error: '', | |
| 74 | |
| 75 focused: {value: false, reflect: true} | |
| 76 | |
| 77 }, | |
| 78 | |
| 79 computed: { | |
| 80 floatingLabelVisible: 'floatingLabel && !_labelVisible', | |
| 81 _labelVisible: '(labelVisible === true || labelVisible === false) ? labe
lVisible : _autoLabelVisible' | |
| 82 }, | |
| 83 | |
| 84 ready: function() { | |
| 85 // Delegate focus/blur events | |
| 86 Polymer.addEventListener(this, 'focus', this.focusAction.bind(this), tru
e); | |
| 87 Polymer.addEventListener(this, 'blur', this.blurAction.bind(this), true)
; | |
| 88 }, | |
| 89 | |
| 90 attached: function() { | |
| 91 this.input = this.querySelector('input,textarea'); | |
| 92 | |
| 93 this.mo = new MutationObserver(function() { | |
| 94 this.input = this.querySelector('input,textarea'); | |
| 95 }.bind(this)); | |
| 96 this.mo.observe(this, {childList: true}); | |
| 97 }, | |
| 98 | |
| 99 detached: function() { | |
| 100 this.mo.disconnect(); | |
| 101 this.mo = null; | |
| 102 }, | |
| 103 | |
| 104 prepareLabelTransform: function() { | |
| 105 var toRect = this.$.floatedLabelText.getBoundingClientRect(); | |
| 106 var fromRect = this.$.labelText.getBoundingClientRect(); | |
| 107 if (toRect.width !== 0) { | |
| 108 var sy = toRect.height / fromRect.height; | |
| 109 this.$.labelText.cachedTransform = | |
| 110 'scale3d(' + (toRect.width / fromRect.width) + ',' + sy + ',1) ' + | |
| 111 'translate3d(0,' + (toRect.top - fromRect.top) / sy + 'px,0)'; | |
| 112 } | |
| 113 }, | |
| 114 | |
| 115 animateFloatingLabel: function() { | |
| 116 if (!this.floatingLabel || this.labelAnimated) { | |
| 117 return false; | |
| 118 } | |
| 119 | |
| 120 if (!this.$.labelText.cachedTransform) { | |
| 121 this.prepareLabelTransform(); | |
| 122 } | |
| 123 | |
| 124 // If there's still no cached transform, the input is invisible so don't | |
| 125 // do the animation. | |
| 126 if (!this.$.labelText.cachedTransform) { | |
| 127 return false; | |
| 128 } | |
| 129 | |
| 130 this.labelAnimated = true; | |
| 131 // Handle interrupted animation | |
| 132 this.async(function() { | |
| 133 this.transitionEndAction(); | |
| 134 }, null, 250); | |
| 135 | |
| 136 if (this._labelVisible) { | |
| 137 // Handle if the label started out floating | |
| 138 if (!this.$.labelText.style.webkitTransform && !this.$.labelText.style
.transform) { | |
| 139 this.$.labelText.style.webkitTransform = this.$.labelText.cachedTran
sform; | |
| 140 this.$.labelText.style.transform = this.$.labelText.cachedTransform; | |
| 141 this.$.labelText.offsetTop; | |
| 142 } | |
| 143 this.$.labelText.style.webkitTransform = ''; | |
| 144 this.$.labelText.style.transform = ''; | |
| 145 } else { | |
| 146 this.$.labelText.style.webkitTransform = this.$.labelText.cachedTransf
orm; | |
| 147 this.$.labelText.style.transform = this.$.labelText.cachedTransform; | |
| 148 this.input.placeholder = ''; | |
| 149 } | |
| 150 | |
| 151 return true; | |
| 152 }, | |
| 153 | |
| 154 _labelVisibleChanged: function(old) { | |
| 155 // do not do the animation on first render | |
| 156 if (old !== undefined) { | |
| 157 if (!this.animateFloatingLabel()) { | |
| 158 this.updateInputLabel(this.input, this.label); | |
| 159 } | |
| 160 } | |
| 161 }, | |
| 162 | |
| 163 labelVisibleChanged: function() { | |
| 164 if (this.labelVisible === 'true') { | |
| 165 this.labelVisible = true; | |
| 166 } else if (this.labelVisible === 'false') { | |
| 167 this.labelVisible = false; | |
| 168 } | |
| 169 }, | |
| 170 | |
| 171 labelChanged: function() { | |
| 172 if (this.input) { | |
| 173 this.updateInputLabel(this.input, this.label); | |
| 174 } | |
| 175 }, | |
| 176 | |
| 177 isInvalidChanged: function() { | |
| 178 this.classList.toggle('invalid', this.isInvalid); | |
| 179 }, | |
| 180 | |
| 181 focusedChanged: function() { | |
| 182 this.updateLabelVisibility(this.input && this.input.value); | |
| 183 }, | |
| 184 | |
| 185 inputChanged: function(old) { | |
| 186 if (this.input) { | |
| 187 this.updateLabelVisibility(this.input.value); | |
| 188 this.updateInputLabel(this.input, this.label); | |
| 189 } | |
| 190 if (old) { | |
| 191 this.updateInputLabel(old, ''); | |
| 192 } | |
| 193 }, | |
| 194 | |
| 195 focusAction: function() { | |
| 196 this.focused = true; | |
| 197 }, | |
| 198 | |
| 199 blurAction: function(e) { | |
| 200 this.focused = false; | |
| 201 }, | |
| 202 | |
| 203 /** | |
| 204 * Updates the label visibility based on a value. This is handled automati
cally | |
| 205 * if the user is typing, but if you imperatively set the input value you
need | |
| 206 * to call this function. | |
| 207 * | |
| 208 * @method updateLabelVisibility | |
| 209 * @param {string} value | |
| 210 */ | |
| 211 updateLabelVisibility: function(value) { | |
| 212 var v = (value !== null && value !== undefined) ? String(value) : value; | |
| 213 this._autoLabelVisible = (!this.focused && !v) || (!this.floatingLabel &
& !v); | |
| 214 }, | |
| 215 | |
| 216 updateInputLabel: function(input, label) { | |
| 217 if (this._labelVisible) { | |
| 218 this.input.placeholder = this.label; | |
| 219 } else { | |
| 220 this.input.placeholder = ''; | |
| 221 } | |
| 222 if (label) { | |
| 223 input.setAttribute('aria-label', label); | |
| 224 } else { | |
| 225 input.removeAttribute('aria-label'); | |
| 226 } | |
| 227 }, | |
| 228 | |
| 229 inputAction: function(e) { | |
| 230 this.updateLabelVisibility(e.target.value); | |
| 231 }, | |
| 232 | |
| 233 downAction: function(e) { | |
| 234 if (this.disabled) { | |
| 235 return; | |
| 236 } | |
| 237 | |
| 238 if (this.focused) { | |
| 239 return; | |
| 240 } | |
| 241 | |
| 242 if (this.input) { | |
| 243 this.input.focus(); | |
| 244 e.preventDefault(); | |
| 245 } | |
| 246 | |
| 247 // The underline spills from the tap location | |
| 248 var rect = this.$.underline.getBoundingClientRect(); | |
| 249 var right = e.x - rect.left; | |
| 250 this.$.focusedUnderline.style.mozTransformOrigin = right + 'px'; | |
| 251 this.$.focusedUnderline.style.webkitTransformOrigin = right + 'px '; | |
| 252 this.$.focusedUnderline.style.transformOriginX = right + 'px'; | |
| 253 | |
| 254 // Animations only run when the user interacts with the input | |
| 255 this.underlineAnimated = true; | |
| 256 | |
| 257 // Handle interrupted animation | |
| 258 this.async(function() { | |
| 259 this.transitionEndAction(); | |
| 260 }, null, 250); | |
| 261 }, | |
| 262 | |
| 263 transitionEndAction: function() { | |
| 264 this.underlineAnimated = false; | |
| 265 this.labelAnimated = false; | |
| 266 if (this._labelVisible) { | |
| 267 this.input.placeholder = this.label; | |
| 268 } | |
| 269 } | |
| 270 | |
| 271 }); | |
| 272 | |
| 273 }()); | |
| 274 | |
| 275 | |
| OLD | NEW |