summaryrefslogtreecommitdiffstats
path: root/chromium/chrome/browser/resources/settings/autofill_page/credit_card_edit_dialog.js
blob: a4ca3337d155c197980678d1b9733b6b44562df3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

/**
 * @fileoverview 'settings-credit-card-edit-dialog' is the dialog that allows
 * editing or creating a credit card entry.
 */

(function() {
'use strict';

Polymer({
  is: 'settings-credit-card-edit-dialog',

  properties: {
    /**
     * The credit card being edited.
     * @type {!chrome.autofillPrivate.CreditCardEntry}
     */
    creditCard: Object,

    /**
     * The actual title that's used for this dialog. Will be context sensitive
     * based on if |creditCard| is being created or edited.
     * @private
     */
    title_: String,

    /**
     * The list of months to show in the dropdown.
     * @private {!Array<string>}
     */
    monthList_: {
      type: Array,
      value: [
        '01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'
      ],
    },

    /**
     * The list of years to show in the dropdown.
     * @private {!Array<string>}
     */
    yearList_: Array,

    /** @private */
    expirationYear_: String,

    /** @private {string|undefined} */
    expirationMonth_: String,
  },

  behaviors: [
    I18nBehavior,
  ],

  /**
   * @return {boolean} True iff the provided expiration date is passed.
   * @private
   */
  checkIfCardExpired_: function(expirationMonth_, expirationYear_) {
    const now = new Date();
    return (
        expirationYear_ < now.getFullYear() ||
        (expirationYear_ == now.getFullYear() &&
         expirationMonth_ <= now.getMonth()));
  },

  /** @override */
  attached: function() {
    this.title_ = this.i18n(
        this.creditCard.guid ? 'editCreditCardTitle' : 'addCreditCardTitle');

    // Needed to initialize the disabled state of the Save button.
    this.onCreditCardNameOrNumberChanged_();

    // Add a leading '0' if a month is 1 char.
    if (this.creditCard.expirationMonth.length == 1) {
      this.creditCard.expirationMonth = '0' + this.creditCard.expirationMonth;
    }

    const date = new Date();
    let firstYear = date.getFullYear();
    let lastYear = firstYear + 19;  // Show next 19 years (20 total).
    let selectedYear = parseInt(this.creditCard.expirationYear, 10);

    // |selectedYear| must be valid and between first and last years.
    if (!selectedYear) {
      selectedYear = firstYear;
    } else if (selectedYear < firstYear) {
      firstYear = selectedYear;
    } else if (selectedYear > lastYear) {
      lastYear = selectedYear;
    }

    const yearList = [];
    for (let i = firstYear; i <= lastYear; ++i) {
      yearList.push(i.toString());
    }
    this.yearList_ = yearList;

    this.async(() => {
      this.expirationYear_ = selectedYear.toString();
      this.expirationMonth_ = this.creditCard.expirationMonth;
      this.$.dialog.showModal();
    });
  },

  /** Closes the dialog. */
  close: function() {
    this.$.dialog.close();
  },

  /**
   * Handler for tapping the 'cancel' button. Should just dismiss the dialog.
   * @private
   */
  onCancelButtonTap_: function() {
    this.$.dialog.cancel();
  },

  /**
   * Handler for tapping the save button.
   * @private
   */
  onSaveButtonTap_: function() {
    if (!this.saveEnabled_()) {
      return;
    }

    // If the card is expired, reflect the error to the user.
    // Otherwise, update the card, save and close the dialog.
    if (!this.checkIfCardExpired_(
            this.expirationMonth_, this.expirationYear_)) {
      this.creditCard.expirationYear = this.expirationYear_;
      this.creditCard.expirationMonth = this.expirationMonth_;
      this.fire('save-credit-card', this.creditCard);
      this.close();
    }
  },

  /** @private */
  onMonthChange_: function() {
    this.expirationMonth_ = this.monthList_[this.$.month.selectedIndex];
  },

  /** @private */
  onYearChange_: function() {
    this.expirationYear_ = this.yearList_[this.$.year.selectedIndex];
  },

  /** @private */
  onCreditCardNameOrNumberChanged_: function() {
    this.$.saveButton.disabled = !this.saveEnabled_();
  },

  /** @private */
  saveEnabled_: function() {
    return (this.creditCard.name && this.creditCard.name.trim()) ||
        (this.creditCard.cardNumber && this.creditCard.cardNumber.trim());
  },
});
})();