1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
13 * @see chrome/browser/ui/webui/options/autofill_options_handler.cc
15 var AutofillEntityMetadata;
17 cr.define('options.autofillOptions', function() {
18 /** @const */ var DeletableItem = options.DeletableItem;
19 /** @const */ var DeletableItemList = options.DeletableItemList;
20 /** @const */ var InlineEditableItem = options.InlineEditableItem;
21 /** @const */ var InlineEditableItemList = options.InlineEditableItemList;
24 * @return {!HTMLButtonElement}
26 function AutofillEditProfileButton(edit) {
27 var editButtonEl = /** @type {HTMLButtonElement} */(
28 document.createElement('button'));
29 editButtonEl.className =
30 'list-inline-button hide-until-hover custom-appearance';
31 editButtonEl.textContent =
32 loadTimeData.getString('autofillEditProfileButton');
33 editButtonEl.onclick = edit;
35 editButtonEl.onmousedown = function(e) {
36 // Don't select the row when clicking the button.
38 // Don't focus on the button when clicking it.
45 /** @return {!Element} */
46 function CreateGoogleAccountLabel() {
47 var label = document.createElement('div');
48 label.className = 'deemphasized hides-on-hover';
49 label.textContent = loadTimeData.getString('autofillFromGoogleAccount');
54 * Creates a new address list item.
56 * @param {AutofillEntityMetadata} metadata Details about an address profile.
57 * @extends {options.DeletableItem}
58 * @see chrome/browser/ui/webui/options/autofill_options_handler.cc
60 function AddressListItem(metadata) {
61 var el = cr.doc.createElement('div');
62 el.__proto__ = AddressListItem.prototype;
64 el.metadata_ = metadata;
70 AddressListItem.prototype = {
71 __proto__: DeletableItem.prototype,
74 decorate: function() {
75 DeletableItem.prototype.decorate.call(this);
77 var label = this.ownerDocument.createElement('div');
78 label.className = 'autofill-list-item';
79 label.textContent = this.metadata_.label;
80 this.contentElement.appendChild(label);
82 var sublabel = this.ownerDocument.createElement('div');
83 sublabel.className = 'deemphasized';
84 sublabel.textContent = this.metadata_.sublabel;
85 this.contentElement.appendChild(sublabel);
87 if (!this.metadata_.isLocal) {
88 this.deletable = false;
89 this.contentElement.appendChild(CreateGoogleAccountLabel());
93 var metadata = this.metadata_;
94 var editButtonEl = AutofillEditProfileButton(
95 AddressListItem.prototype.loadAddressEditor.bind(this));
96 this.contentElement.appendChild(editButtonEl);
100 * For local Autofill data, this function causes the AutofillOptionsHandler
101 * to call showEditAddressOverlay(). For Wallet data, the user is
102 * redirected to the Wallet web interface.
104 loadAddressEditor: function() {
105 if (this.metadata_.isLocal)
106 chrome.send('loadAddressEditor', [this.metadata_.guid]);
108 window.open(loadTimeData.getString('manageWalletAddressesUrl'));
113 * Creates a new credit card list item.
114 * @param {AutofillEntityMetadata} metadata Details about a credit card.
116 * @extends {options.DeletableItem}
118 function CreditCardListItem(metadata) {
119 var el = cr.doc.createElement('div');
120 el.__proto__ = CreditCardListItem.prototype;
122 el.metadata_ = metadata;
128 CreditCardListItem.prototype = {
129 __proto__: DeletableItem.prototype,
132 decorate: function() {
133 DeletableItem.prototype.decorate.call(this);
135 var label = this.ownerDocument.createElement('div');
136 label.className = 'autofill-list-item';
137 label.textContent = this.metadata_.label;
138 this.contentElement.appendChild(label);
140 var sublabel = this.ownerDocument.createElement('div');
141 sublabel.className = 'deemphasized';
142 sublabel.textContent = this.metadata_.sublabel;
143 this.contentElement.appendChild(sublabel);
145 if (!this.metadata_.isLocal) {
146 this.deletable = false;
147 this.contentElement.appendChild(CreateGoogleAccountLabel());
150 var guid = this.metadata_.guid;
151 if (this.metadata_.isCached) {
152 var localCopyText = this.ownerDocument.createElement('span');
153 localCopyText.className = 'hide-until-hover deemphasized';
154 localCopyText.textContent =
155 loadTimeData.getString('autofillDescribeLocalCopy');
156 this.contentElement.appendChild(localCopyText);
158 var clearLocalCopyButton = AutofillEditProfileButton(
159 function() { chrome.send('clearLocalCardCopy', [guid]); });
160 clearLocalCopyButton.textContent =
161 loadTimeData.getString('autofillClearLocalCopyButton');
162 this.contentElement.appendChild(clearLocalCopyButton);
165 // The 'Edit' button.
166 var metadata = this.metadata_;
167 var editButtonEl = AutofillEditProfileButton(
168 CreditCardListItem.prototype.loadCreditCardEditor.bind(this));
169 this.contentElement.appendChild(editButtonEl);
173 * For local Autofill data, this function causes the AutofillOptionsHandler
174 * to call showEditCreditCardOverlay(). For Wallet data, the user is
175 * redirected to the Wallet web interface.
177 loadCreditCardEditor: function() {
178 if (this.metadata_.isLocal)
179 chrome.send('loadCreditCardEditor', [this.metadata_.guid]);
181 window.open(loadTimeData.getString('manageWalletPaymentMethodsUrl'));
186 * Base class for shared implementation between address and credit card lists.
188 * @extends {options.DeletableItemList}
190 var AutofillProfileList = cr.ui.define('list');
192 AutofillProfileList.prototype = {
193 __proto__: DeletableItemList.prototype,
195 decorate: function() {
196 DeletableItemList.prototype.decorate.call(this);
198 this.addEventListener('blur', this.onBlur_);
202 * When the list loses focus, unselect all items in the list.
205 onBlur_: function() {
206 this.selectionModel.unselectAll();
211 * Create a new address list.
213 * @extends {options.autofillOptions.AutofillProfileList}
215 var AutofillAddressList = cr.ui.define('list');
217 AutofillAddressList.prototype = {
218 __proto__: AutofillProfileList.prototype,
220 decorate: function() {
221 AutofillProfileList.prototype.decorate.call(this);
225 activateItemAtIndex: function(index) {
226 this.getListItemByIndex(index).loadAddressEditor();
231 * @param {AutofillEntityMetadata} metadata
233 createItem: function(metadata) {
234 return new AddressListItem(metadata);
238 deleteItemAtIndex: function(index) {
239 AutofillOptions.removeData(this.dataModel.item(index).guid,
240 'Options_AutofillAddressDeleted');
245 * Create a new credit card list.
247 * @extends {options.DeletableItemList}
249 var AutofillCreditCardList = cr.ui.define('list');
251 AutofillCreditCardList.prototype = {
252 __proto__: AutofillProfileList.prototype,
254 decorate: function() {
255 AutofillProfileList.prototype.decorate.call(this);
259 activateItemAtIndex: function(index) {
260 this.getListItemByIndex(index).loadCreditCardEditor();
265 * @param {AutofillEntityMetadata} metadata
267 createItem: function(metadata) {
268 return new CreditCardListItem(metadata);
272 deleteItemAtIndex: function(index) {
273 AutofillOptions.removeData(this.dataModel.item(index).guid,
274 'Options_AutofillCreditCardDeleted');
279 AutofillProfileList: AutofillProfileList,
280 AddressListItem: AddressListItem,
281 CreditCardListItem: CreditCardListItem,
282 AutofillAddressList: AutofillAddressList,
283 AutofillCreditCardList: AutofillCreditCardList,