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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import AddressForm from "./address-form.js";
import AddressOption from "../components/address-option.js";
import RichPicker from "./rich-picker.js";
import paymentRequest from "../paymentRequest.js";
import HandleEventMixin from "../mixins/HandleEventMixin.js";
/**
* <address-picker></address-picker>
* Container around add/edit links and <rich-select> with
* <address-option> listening to savedAddresses & tempAddresses.
*/
export default class AddressPicker extends HandleEventMixin(RichPicker) {
static get pickerAttributes() {
return ["address-fields", "break-after-nth-field", "data-field-separator"];
}
static get observedAttributes() {
return RichPicker.observedAttributes.concat(AddressPicker.pickerAttributes);
}
constructor() {
super();
this.dropdown.setAttribute("option-type", "address-option");
}
attributeChangedCallback(name, oldValue, newValue) {
super.attributeChangedCallback(name, oldValue, newValue);
// connectedCallback may add and adjust elements & values
// so avoid calling render before the element is connected
if (
this.isConnected &&
AddressPicker.pickerAttributes.includes(name) &&
oldValue !== newValue
) {
this.render(this.requestStore.getState());
}
}
get fieldNames() {
if (this.hasAttribute("address-fields")) {
let names = this.getAttribute("address-fields")
.trim()
.split(/\s+/);
if (names.length) {
return names;
}
}
return [
// "address-level1", // TODO: bug 1481481 - not required for some countries e.g. DE
"address-level2",
"country",
"name",
"postal-code",
"street-address",
];
}
/**
* De-dupe and filter addresses for the given set of fields that will be visible
*
* @param {object} addresses
* @param {array?} fieldNames - optional list of field names that be used when
* de-duping and excluding entries
* @returns {object} filtered copy of given addresses
*/
filterAddresses(addresses, fieldNames = this.fieldNames) {
let uniques = new Set();
let result = {};
for (let [guid, address] of Object.entries(addresses)) {
let addressCopy = {};
let isMatch = false;
// exclude addresses that are missing all of the requested fields
for (let name of fieldNames) {
if (address[name]) {
isMatch = true;
addressCopy[name] = address[name];
}
}
if (isMatch) {
let key = JSON.stringify(addressCopy);
// exclude duplicated addresses
if (!uniques.has(key)) {
uniques.add(key);
result[guid] = address;
}
}
}
return result;
}
get options() {
return this.dropdown.popupBox.options;
}
/**
* @param {object} state - See `PaymentsStore.setState`
* The value of the picker is retrieved from state store rather than the DOM
* @returns {string} guid
*/
getCurrentValue(state) {
let [selectedKey, selectedLeaf] = this.selectedStateKey.split("|");
let guid = state[selectedKey];
if (selectedLeaf) {
guid = guid[selectedLeaf];
}
return guid;
}
render(state) {
let selectedAddressGUID = this.getCurrentValue(state) || "";
let addresses = paymentRequest.getAddresses(state);
let desiredOptions = [];
let filteredAddresses = this.filterAddresses(addresses, this.fieldNames);
for (let [guid, address] of Object.entries(filteredAddresses)) {
let optionEl = this.dropdown.getOptionByValue(guid);
if (!optionEl) {
optionEl = document.createElement("option");
optionEl.value = guid;
}
for (let key of AddressOption.recordAttributes) {
let val = address[key];
if (val) {
optionEl.setAttribute(key, val);
} else {
optionEl.removeAttribute(key);
}
}
optionEl.dataset.fieldSeparator = this.dataset.fieldSeparator;
if (this.hasAttribute("address-fields")) {
optionEl.setAttribute(
"address-fields",
this.getAttribute("address-fields")
);
} else {
optionEl.removeAttribute("address-fields");
}
if (this.hasAttribute("break-after-nth-field")) {
optionEl.setAttribute(
"break-after-nth-field",
this.getAttribute("break-after-nth-field")
);
} else {
optionEl.removeAttribute("break-after-nth-field");
}
// fieldNames getter is not used here because it returns a default array with
// attributes even when "address-fields" observed attribute is null.
let addressFields = this.getAttribute("address-fields");
optionEl.textContent = AddressOption.formatSingleLineLabel(
address,
addressFields
);
desiredOptions.push(optionEl);
}
this.dropdown.popupBox.textContent = "";
if (this._allowEmptyOption) {
let optionEl = document.createElement("option");
optionEl.value = "";
desiredOptions.unshift(optionEl);
}
for (let option of desiredOptions) {
this.dropdown.popupBox.appendChild(option);
}
// Update selectedness after the options are updated
this.dropdown.value = selectedAddressGUID;
if (selectedAddressGUID && selectedAddressGUID !== this.dropdown.value) {
throw new Error(
`${this.selectedStateKey} option ${selectedAddressGUID} ` +
`does not exist in the address picker`
);
}
super.render(state);
}
get selectedStateKey() {
return this.getAttribute("selected-state-key");
}
errorForSelectedOption(state) {
let superError = super.errorForSelectedOption(state);
if (superError) {
return superError;
}
if (!this.selectedOption) {
return "";
}
let merchantFieldErrors = AddressForm.merchantFieldErrorsForForm(
state,
this.selectedStateKey.split("|")
);
// TODO: errors in priority order.
return (
Object.values(merchantFieldErrors).find(msg => {
return typeof msg == "string" && msg.length;
}) || ""
);
}
onChange(event) {
let [selectedKey, selectedLeaf] = this.selectedStateKey.split("|");
if (!selectedKey) {
return;
}
// selectedStateKey can be a '|' delimited string indicating a path into the state object
// to update with the new value
let newState = {};
if (selectedLeaf) {
let currentState = this.requestStore.getState();
newState[selectedKey] = Object.assign({}, currentState[selectedKey], {
[selectedLeaf]: this.dropdown.value,
});
} else {
newState[selectedKey] = this.dropdown.value;
}
this.requestStore.setState(newState);
}
onClick({ target }) {
let pageId;
let currentState = this.requestStore.getState();
let nextState = {
page: {},
};
switch (this.selectedStateKey) {
case "selectedShippingAddress":
pageId = "shipping-address-page";
break;
case "selectedPayerAddress":
pageId = "payer-address-page";
break;
case "basic-card-page|billingAddressGUID":
pageId = "billing-address-page";
break;
default: {
throw new Error(
"onClick, un-matched selectedStateKey: " + this.selectedStateKey
);
}
}
nextState.page.id = pageId;
let addressFields = this.getAttribute("address-fields");
nextState[pageId] = { addressFields };
switch (target) {
case this.addLink: {
nextState[pageId].guid = null;
break;
}
case this.editLink: {
nextState[pageId].guid = this.getCurrentValue(currentState);
break;
}
default: {
throw new Error("Unexpected onClick");
}
}
this.requestStore.setState(nextState);
}
}
customElements.define("address-picker", AddressPicker);
|