| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" | 5 #include "chrome/browser/ui/views/payments/credit_card_editor_view_controller.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 // credit card editor. | 85 // credit card editor. |
| 86 // +----------------------------------------------+ | 86 // +----------------------------------------------+ |
| 87 // | Cards Accepted | | 87 // | Cards Accepted | |
| 88 // | | | 88 // | | |
| 89 // | | VISA | | MC | | AMEX | | | 89 // | | VISA | | MC | | AMEX | | |
| 90 // +----------------------------------------------+ | 90 // +----------------------------------------------+ |
| 91 std::unique_ptr<views::View> | 91 std::unique_ptr<views::View> |
| 92 CreditCardEditorViewController::CreateHeaderView() { | 92 CreditCardEditorViewController::CreateHeaderView() { |
| 93 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); | 93 std::unique_ptr<views::View> view = base::MakeUnique<views::View>(); |
| 94 | 94 |
| 95 // 9dp is required between the first and second row. | 95 // 9dp is required between the first row (label) and second row (icons). |
| 96 constexpr int kRowVerticalInset = 9; | 96 constexpr int kRowVerticalSpacing = 9; |
| 97 views::BoxLayout* layout = new views::BoxLayout( | 97 views::BoxLayout* layout = new views::BoxLayout( |
| 98 views::BoxLayout::kVertical, payments::kPaymentRequestRowHorizontalInsets, | 98 views::BoxLayout::kVertical, payments::kPaymentRequestRowHorizontalInsets, |
| 99 payments::kPaymentRequestRowVerticalInsets, kRowVerticalInset); | 99 0, kRowVerticalSpacing); |
| 100 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); | 100 layout->set_main_axis_alignment(views::BoxLayout::MAIN_AXIS_ALIGNMENT_START); |
| 101 layout->set_cross_axis_alignment( | 101 layout->set_cross_axis_alignment( |
| 102 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); | 102 views::BoxLayout::CROSS_AXIS_ALIGNMENT_START); |
| 103 view->SetLayoutManager(layout); | 103 view->SetLayoutManager(layout); |
| 104 | 104 |
| 105 // "Cards accepted" label is "disabled" grey. | 105 // "Cards accepted" label is "disabled" grey. |
| 106 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( | 106 std::unique_ptr<views::Label> label = base::MakeUnique<views::Label>( |
| 107 l10n_util::GetStringUTF16(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL)); | 107 l10n_util::GetStringUTF16(IDS_PAYMENTS_ACCEPTED_CARDS_LABEL)); |
| 108 label->SetDisabledColor(label->GetNativeTheme()->GetSystemColor( | 108 label->SetDisabledColor(label->GetNativeTheme()->GetSystemColor( |
| 109 ui::NativeTheme::kColorId_LabelDisabledColor)); | 109 ui::NativeTheme::kColorId_LabelDisabledColor)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 129 | 129 |
| 130 icons_row->AddChildView(card_icon_view.release()); | 130 icons_row->AddChildView(card_icon_view.release()); |
| 131 } | 131 } |
| 132 view->AddChildView(icons_row.release()); | 132 view->AddChildView(icons_row.release()); |
| 133 | 133 |
| 134 return view; | 134 return view; |
| 135 } | 135 } |
| 136 | 136 |
| 137 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { | 137 std::vector<EditorField> CreditCardEditorViewController::GetFieldDefinitions() { |
| 138 return std::vector<EditorField>{ | 138 return std::vector<EditorField>{ |
| 139 {autofill::CREDIT_CARD_NUMBER, |
| 140 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), |
| 141 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 139 {autofill::CREDIT_CARD_NAME_FULL, | 142 {autofill::CREDIT_CARD_NAME_FULL, |
| 140 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), | 143 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_NAME_ON_CARD), |
| 141 EditorField::LengthHint::HINT_LONG, /* required= */ true}, | 144 EditorField::LengthHint::HINT_LONG, /* required= */ true}, |
| 142 {autofill::CREDIT_CARD_NUMBER, | |
| 143 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_CREDIT_CARD_NUMBER), | |
| 144 EditorField::LengthHint::HINT_LONG, /* required= */ true}, | |
| 145 {autofill::CREDIT_CARD_EXP_MONTH, | 145 {autofill::CREDIT_CARD_EXP_MONTH, |
| 146 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), | 146 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_MONTH), |
| 147 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 147 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 148 EditorField::ControlType::COMBOBOX}, | 148 EditorField::ControlType::COMBOBOX}, |
| 149 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, | 149 {autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR, |
| 150 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), | 150 l10n_util::GetStringUTF16(IDS_AUTOFILL_FIELD_LABEL_EXPIRATION_YEAR), |
| 151 EditorField::LengthHint::HINT_SHORT, /* required= */ true, | 151 EditorField::LengthHint::HINT_SHORT, /* required= */ true, |
| 152 EditorField::ControlType::COMBOBOX}}; | 152 EditorField::ControlType::COMBOBOX}}; |
| 153 } | 153 } |
| 154 | 154 |
| (...skipping 28 matching lines...) Expand all Loading... |
| 183 // Add the card (will not add a duplicate). | 183 // Add the card (will not add a duplicate). |
| 184 request()->personal_data_manager()->AddCreditCard(credit_card); | 184 request()->personal_data_manager()->AddCreditCard(credit_card); |
| 185 | 185 |
| 186 return true; | 186 return true; |
| 187 } | 187 } |
| 188 | 188 |
| 189 std::unique_ptr<ValidationDelegate> | 189 std::unique_ptr<ValidationDelegate> |
| 190 CreditCardEditorViewController::CreateValidationDelegate( | 190 CreditCardEditorViewController::CreateValidationDelegate( |
| 191 const EditorField& field) { | 191 const EditorField& field) { |
| 192 return base::MakeUnique< | 192 return base::MakeUnique< |
| 193 CreditCardEditorViewController::CreditCardValidationDelegate>(field); | 193 CreditCardEditorViewController::CreditCardValidationDelegate>(field, |
| 194 this); |
| 194 } | 195 } |
| 195 | 196 |
| 196 std::unique_ptr<ui::ComboboxModel> | 197 std::unique_ptr<ui::ComboboxModel> |
| 197 CreditCardEditorViewController::GetComboboxModelForType( | 198 CreditCardEditorViewController::GetComboboxModelForType( |
| 198 const autofill::ServerFieldType& type) { | 199 const autofill::ServerFieldType& type) { |
| 199 switch (type) { | 200 switch (type) { |
| 200 case autofill::CREDIT_CARD_EXP_MONTH: { | 201 case autofill::CREDIT_CARD_EXP_MONTH: { |
| 201 int default_index = 0; | 202 int default_index = 0; |
| 202 std::vector<base::string16> months = | 203 std::vector<base::string16> months = |
| 203 GetExpirationMonthItems(&default_index); | 204 GetExpirationMonthItems(&default_index); |
| 204 return std::unique_ptr<ui::ComboboxModel>( | 205 return std::unique_ptr<ui::ComboboxModel>( |
| 205 new PreselectedComboboxModel(months, default_index)); | 206 new PreselectedComboboxModel(months, default_index)); |
| 206 } | 207 } |
| 207 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: | 208 case autofill::CREDIT_CARD_EXP_4_DIGIT_YEAR: |
| 208 return std::unique_ptr<ui::ComboboxModel>( | 209 return std::unique_ptr<ui::ComboboxModel>( |
| 209 new ui::SimpleComboboxModel(GetExpirationYearItems())); | 210 new ui::SimpleComboboxModel(GetExpirationYearItems())); |
| 210 default: | 211 default: |
| 211 NOTREACHED(); | 212 NOTREACHED(); |
| 212 break; | 213 break; |
| 213 } | 214 } |
| 214 return std::unique_ptr<ui::ComboboxModel>(); | 215 return std::unique_ptr<ui::ComboboxModel>(); |
| 215 } | 216 } |
| 216 | 217 |
| 217 CreditCardEditorViewController::CreditCardValidationDelegate:: | 218 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 218 CreditCardValidationDelegate(const EditorField& field) | 219 CreditCardValidationDelegate(const EditorField& field, |
| 219 : field_(field) {} | 220 EditorViewController* controller) |
| 221 : field_(field), controller_(controller) {} |
| 220 CreditCardEditorViewController::CreditCardValidationDelegate:: | 222 CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 221 ~CreditCardValidationDelegate() {} | 223 ~CreditCardValidationDelegate() {} |
| 222 | 224 |
| 223 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 225 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 224 ValidateTextfield(views::Textfield* textfield) { | 226 ValidateTextfield(views::Textfield* textfield) { |
| 225 return ValidateValue(textfield->text()); | 227 return ValidateValue(textfield->text()); |
| 226 } | 228 } |
| 227 | 229 |
| 228 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 230 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 229 ValidateCombobox(views::Combobox* combobox) { | 231 ValidateCombobox(views::Combobox* combobox) { |
| 230 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); | 232 return ValidateValue(combobox->GetTextForRow(combobox->selected_index())); |
| 231 } | 233 } |
| 232 | 234 |
| 233 bool CreditCardEditorViewController::CreditCardValidationDelegate:: | 235 bool CreditCardEditorViewController::CreditCardValidationDelegate:: |
| 234 ValidateValue(const base::string16& value) { | 236 ValidateValue(const base::string16& value) { |
| 235 if (!value.empty()) { | 237 if (!value.empty()) { |
| 236 base::string16 error_message; | 238 base::string16 error_message; |
| 237 // TODO(mathp): Display |error_message| around |textfield|. | 239 bool is_valid = |
| 238 return autofill::IsValidForType(value, field_.type, &error_message); | 240 autofill::IsValidForType(value, field_.type, &error_message); |
| 241 controller_->DisplayErrorMessageForField(field_, error_message); |
| 242 return is_valid; |
| 239 } | 243 } |
| 240 | 244 |
| 241 // TODO(mathp): Display "required" error if applicable. | 245 bool is_required_valid = !field_.required; |
| 242 return !field_.required; | 246 const base::string16 displayed_message = |
| 247 is_required_valid ? base::ASCIIToUTF16("") |
| 248 : l10n_util::GetStringUTF16( |
| 249 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 250 controller_->DisplayErrorMessageForField(field_, displayed_message); |
| 251 return is_required_valid; |
| 243 } | 252 } |
| 244 | 253 |
| 245 } // namespace payments | 254 } // namespace payments |
| OLD | NEW |