OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 @license |
| 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 |
| 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 |
| 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 |
| 9 --> |
| 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 <link rel="import" href="../iron-flex-layout/classes/iron-flex-layout.html"> |
| 12 <link rel="import" href="../paper-input/paper-input-behavior.html"> |
| 13 <link rel="import" href="../paper-input/paper-input-container.html"> |
| 14 <link rel="import" href="../paper-input/paper-input-error.html"> |
| 15 <link rel="import" href="../iron-input/iron-input.html"> |
| 16 <link rel="import" href="../iron-form-element-behavior/iron-form-element-behavio
r.html"> |
| 17 <link rel="import" href="../iron-icon/iron-icon.html"> |
| 18 |
| 19 <script src="cc-validator.js"></script> |
| 20 |
| 21 <!-- |
| 22 `gold-cc-input` is a single-line text field with Material Design styling |
| 23 for entering a credit card number. As the user types, the number will be |
| 24 formatted by adding a space every 4 digits. |
| 25 |
| 26 <gold-cc-input></gold-cc-input> |
| 27 |
| 28 It may include an optional label, which by default is "Card number". |
| 29 |
| 30 <gold-cc-input label="CC"></gold-cc-input> |
| 31 |
| 32 ### Validation |
| 33 |
| 34 The input can detect whether a credit card number is valid, and the type |
| 35 of credit card it is, using the Luhn checksum. See `http://jquerycreditcardvalid
ator.com/` |
| 36 for more information. |
| 37 |
| 38 The input can be automatically validated as the user is typing by using |
| 39 the `auto-validate` and `required` attributes. For manual validation, the |
| 40 element also has a `validate()` method, which returns the validity of the |
| 41 input as well sets any appropriate error messages and styles. |
| 42 |
| 43 See `Polymer.PaperInputBehavior` for more API docs. |
| 44 |
| 45 ### Styling |
| 46 |
| 47 See `Polymer.PaperInputContainer` for a list of custom properties used to |
| 48 style this element. |
| 49 |
| 50 @group Gold Elements |
| 51 @hero hero.svg |
| 52 @demo demo/index.html |
| 53 @class gold-cc-input |
| 54 --> |
| 55 |
| 56 <dom-module id="gold-cc-input"> |
| 57 <style> |
| 58 :host { |
| 59 display: block; |
| 60 } |
| 61 |
| 62 /* Use a container so that when hiding the icon, the layout doesn't jump aroun
d. */ |
| 63 .icon-container { |
| 64 margin-left: 10px; |
| 65 height: 27px; |
| 66 } |
| 67 |
| 68 iron-icon { |
| 69 --iron-icon-width: 40px; |
| 70 --iron-icon-height: 24px; |
| 71 } |
| 72 </style> |
| 73 |
| 74 <template> |
| 75 |
| 76 <paper-input-container id="container" |
| 77 disabled$="[[disabled]]" |
| 78 no-label-float="[[noLabelFloat]]" |
| 79 always-float-label="[[_computeAlwaysFloatLabel(alwaysFloatLabel,placehol
der)]]" |
| 80 invalid="[[invalid]]"> |
| 81 |
| 82 <label hidden$="[[!label]]">[[label]]</label> |
| 83 |
| 84 <div class="horizontal layout"> |
| 85 <input is="iron-input" id="input" |
| 86 aria-labelledby$="[[_ariaLabelledBy]]" |
| 87 aria-describedby$="[[_ariaDescribedBy]]" |
| 88 bind-value="{{value}}" |
| 89 type="tel" |
| 90 maxlength="30" |
| 91 required$="[[required]]" |
| 92 allowed-pattern="[0-9 ]" |
| 93 prevent-invalid-input |
| 94 autocomplete="cc-number" |
| 95 name$="[[name]]" |
| 96 disabled$="[[disabled]]" |
| 97 invalid="{{invalid}}" |
| 98 autofocus$="[[autofocus]]" |
| 99 inputmode$="[[inputmode]]" |
| 100 placeholder$="[[placeholder]]" |
| 101 readonly$="[[readonly]]" |
| 102 size$="[[size]]"> |
| 103 <div class="icon-container"><iron-icon id="icon"></iron-icon></div> |
| 104 </div> |
| 105 |
| 106 <template is="dom-if" if="[[errorMessage]]"> |
| 107 <paper-input-error>[[errorMessage]]</paper-input-error> |
| 108 </template> |
| 109 |
| 110 </paper-input-container> |
| 111 </template> |
| 112 |
| 113 </dom-module> |
| 114 |
| 115 <script> |
| 116 (function() { |
| 117 Polymer({ |
| 118 |
| 119 is: 'gold-cc-input', |
| 120 |
| 121 behaviors: [ |
| 122 Polymer.PaperInputBehavior, |
| 123 Polymer.IronValidatableBehavior, |
| 124 Polymer.IronFormElementBehavior |
| 125 ], |
| 126 |
| 127 properties: { |
| 128 /** |
| 129 * The label for this input. |
| 130 */ |
| 131 label: { |
| 132 type: String, |
| 133 value: "Card number" |
| 134 }, |
| 135 |
| 136 /** |
| 137 * The type of the credit card, if it is valid. Empty otherwise. |
| 138 */ |
| 139 cardType: { |
| 140 type: String, |
| 141 notify: true |
| 142 }, |
| 143 }, |
| 144 |
| 145 observers: [ |
| 146 '_computeValue(value)' |
| 147 ], |
| 148 |
| 149 _computeValue: function(value) { |
| 150 var start = this.$.input.selectionStart; |
| 151 var previousCharASpace = this.value.charAt(start - 1) == ' '; |
| 152 |
| 153 value = value.replace(/\s+/g, ''); |
| 154 var formattedValue = ''; |
| 155 for (var i = 0; i < value.length; i++) { |
| 156 // Add a space after every 4 characters. |
| 157 if ((i != 0) && (i % 4 == 0)) { |
| 158 formattedValue += ' '; |
| 159 } |
| 160 formattedValue += value[i]; |
| 161 } |
| 162 this.updateValueAndPreserveCaret(formattedValue.trim()); |
| 163 |
| 164 if (this.autoValidate) |
| 165 this.validate(); |
| 166 |
| 167 // If the character right before the selection is a newly inserted |
| 168 // space, we need to advance the selection to maintain the caret position. |
| 169 if (!previousCharASpace && this.value.charAt(start - 1) == ' ') { |
| 170 this.$.input.selectionStart = start+1; |
| 171 this.$.input.selectionEnd = start+1; |
| 172 } |
| 173 }, |
| 174 |
| 175 validate: function() { |
| 176 // Empty, non-required input is valid. |
| 177 if (!this.required && this.value == '') { |
| 178 return true; |
| 179 } |
| 180 |
| 181 var result = CreditCardValidator.validate(this.value); |
| 182 var valid = result.valid && result.length_valid; |
| 183 this.cardType = valid ? result.card_type.name : ''; |
| 184 |
| 185 // Update the container and its addons (i.e. the custom error-message). |
| 186 this.$.container.invalid = !valid; |
| 187 this.$.container.updateAddons({ |
| 188 inputElement: this.$.input, |
| 189 value: this.value, |
| 190 invalid: !valid |
| 191 }); |
| 192 |
| 193 // We don't have icons for all the card types. |
| 194 if (valid && result.card_type.icon) { |
| 195 this.$.icon.src = this.resolveUrl(result.card_type.icon); |
| 196 this.$.icon.alt = this.cardType; |
| 197 this.$.icon.hidden = false; |
| 198 } else { |
| 199 this.$.icon.src = null; |
| 200 this.$.icon.alt = ''; |
| 201 this.$.icon.hidden = true; |
| 202 } |
| 203 |
| 204 return valid; |
| 205 } |
| 206 }) |
| 207 |
| 208 })(); |
| 209 |
| 210 </script> |
OLD | NEW |