OLD | NEW |
---|---|
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 cr.define('options', function() { | 5 cr.define('options', function() { |
6 const OptionsPage = options.OptionsPage; | 6 const OptionsPage = options.OptionsPage; |
7 | 7 |
8 // The GUID of the loaded address. | 8 // The GUID of the loaded address. |
9 var guid; | 9 var guid; |
10 | 10 |
(...skipping 22 matching lines...) Expand all Loading... | |
33 var self = this; | 33 var self = this; |
34 $('autoFillEditAddressCancelButton').onclick = function(event) { | 34 $('autoFillEditAddressCancelButton').onclick = function(event) { |
35 self.dismissOverlay_(); | 35 self.dismissOverlay_(); |
36 } | 36 } |
37 $('autoFillEditAddressApplyButton').onclick = function(event) { | 37 $('autoFillEditAddressApplyButton').onclick = function(event) { |
38 self.saveAddress_(); | 38 self.saveAddress_(); |
39 self.dismissOverlay_(); | 39 self.dismissOverlay_(); |
40 } | 40 } |
41 | 41 |
42 self.guid = ''; | 42 self.guid = ''; |
43 self.populateCountryList_(); | |
43 self.clearInputFields_(); | 44 self.clearInputFields_(); |
44 self.connectInputEvents_(); | 45 self.connectInputEvents_(); |
45 }, | 46 }, |
46 | 47 |
47 /** | 48 /** |
48 * Clears any uncommitted input, resets the stored GUID and dismisses the | 49 * Clears any uncommitted input, resets the stored GUID and dismisses the |
49 * overlay. | 50 * overlay. |
50 * @private | 51 * @private |
51 */ | 52 */ |
52 dismissOverlay_: function() { | 53 dismissOverlay_: function() { |
53 this.clearInputFields_(); | 54 this.clearInputFields_(); |
54 this.guid = ''; | 55 this.guid = ''; |
55 OptionsPage.closeOverlay(); | 56 OptionsPage.closeOverlay(); |
56 }, | 57 }, |
57 | 58 |
58 /** | 59 /** |
59 * Aggregates the values in the input fields into an array and sends the | 60 * Aggregates the values in the input fields into an array and sends the |
60 * array to the AutoFill handler. | 61 * array to the AutoFill handler. |
61 * @private | 62 * @private |
62 */ | 63 */ |
63 saveAddress_: function() { | 64 saveAddress_: function() { |
64 var address = new Array(); | 65 var address = new Array(); |
65 address[0] = this.guid; | 66 address[0] = this.guid; |
66 address[1] = $('fullName').value; | 67 address[1] = $('fullName').value; |
67 address[2] = $('companyName').value; | 68 address[2] = $('country').value; |
dhollowa
2011/02/17 00:55:28
Country down below.
Ilya Sherman
2011/02/17 23:09:11
Done.
| |
68 address[3] = $('addrLine1').value; | 69 address[3] = $('companyName').value; |
69 address[4] = $('addrLine2').value; | 70 address[4] = $('addrLine1').value; |
70 address[5] = $('city').value; | 71 address[5] = $('addrLine2').value; |
71 address[6] = $('state').value; | 72 address[6] = $('city').value; |
72 address[7] = $('zipCode').value; | 73 address[7] = $('state').value; |
73 address[8] = $('country').value; | 74 address[8] = $('postal-code').value; |
74 address[9] = $('phone').value; | 75 address[9] = $('phone').value; |
75 address[10] = $('fax').value; | 76 address[10] = $('fax').value; |
76 address[11] = $('email').value; | 77 address[11] = $('email').value; |
77 | 78 |
78 chrome.send('setAddress', address); | 79 chrome.send('setAddress', address); |
79 }, | 80 }, |
80 | 81 |
81 /** | 82 /** |
82 * Connects each input field to the inputFieldChanged_() method that enables | 83 * Connects each input field to the inputFieldChanged_() method that enables |
83 * or disables the 'Ok' button based on whether all the fields are empty or | 84 * or disables the 'Ok' button based on whether all the fields are empty or |
84 * not. | 85 * not. |
85 * @private | 86 * @private |
86 */ | 87 */ |
87 connectInputEvents_: function() { | 88 connectInputEvents_: function() { |
88 var self = this; | 89 var self = this; |
89 $('fullName').oninput = $('companyName').oninput = | 90 $('fullName').oninput = $('country').oninput = $('companyName').oninput = |
90 $('addrLine1').oninput = $('addrLine2').oninput = $('city').oninput = | 91 $('addrLine1').oninput = $('addrLine2').oninput = $('city').oninput = |
91 $('state').oninput = $('country').oninput = $('zipCode').oninput = | 92 $('state').oninput = $('postal-code').oninput = $('phone').oninput = |
92 $('phone').oninput = $('fax').oninput = | 93 $('fax').oninput = $('email').oninput = function(event) { |
93 $('email').oninput = function(event) { | |
94 self.inputFieldChanged_(); | 94 self.inputFieldChanged_(); |
95 } | 95 } |
96 | |
97 $('country').onchange = function(event) { | |
98 self.countryChanged_(); | |
99 } | |
96 }, | 100 }, |
97 | 101 |
98 /** | 102 /** |
99 * Checks the values of each of the input fields and disables the 'Ok' | 103 * Checks the values of each of the input fields and disables the 'Ok' |
100 * button if all of the fields are empty. | 104 * button if all of the fields are empty. |
101 * @private | 105 * @private |
102 */ | 106 */ |
103 inputFieldChanged_: function() { | 107 inputFieldChanged_: function() { |
104 var disabled = | 108 var disabled = |
105 !$('fullName').value && !$('companyName').value && | 109 !$('fullName').value && !$('country').value && |
106 !$('addrLine1').value && !$('addrLine2').value && !$('city').value && | 110 !$('companyName').value && !$('addrLine1').value && |
107 !$('state').value && !$('zipCode').value && !('country').value && | 111 !$('addrLine2').value && !$('city').value && !$('state').value && |
108 !$('phone').value && !$('fax').value && !$('email').value; | 112 !$('postal-code').value && !$('phone').value && !$('fax').value && |
113 !$('email').value; | |
109 $('autoFillEditAddressApplyButton').disabled = disabled; | 114 $('autoFillEditAddressApplyButton').disabled = disabled; |
110 }, | 115 }, |
111 | 116 |
112 /** | 117 /** |
118 * Updates the postal code and state field labels appropriately for the | |
119 * selected country. | |
120 * @private | |
121 */ | |
122 countryChanged_: function() { | |
123 var countryCode = $('country').value; | |
124 if (!countryCode) | |
125 countryCode = templateData.defaultCountryCode; | |
126 | |
127 var details = templateData.autofillCountryData[countryCode]; | |
128 var postal = $('postal-code-label'); | |
129 postal.textContent = details['postalCodeLabel']; | |
130 $('state-label').textContent = details['stateLabel']; | |
131 | |
132 // Also update the 'Ok' button as needed. | |
133 this.inputFieldChanged_(); | |
134 }, | |
135 | |
136 /** | |
137 * Populates the country <select> list. | |
138 * @private | |
139 */ | |
140 populateCountryList_: function() { | |
141 var countryData = templateData.autofillCountryData; | |
142 var defaultCountryCode = templateData.defaultCountryCode; | |
143 | |
144 // Build an array of the country names and their corresponding country | |
145 // codes, so that we can sort and insert them in order. | |
146 var countries = []; | |
147 for (var countryCode in countryData) { | |
148 // We always want the default country to be at the top of the list, so | |
149 // we handle it separately. | |
150 if (countryCode == defaultCountryCode) | |
151 continue; | |
152 | |
153 var country = { | |
154 countryCode: countryCode, | |
155 name: countryData[countryCode]['name'] | |
156 }; | |
157 countries.push(country); | |
158 } | |
159 | |
160 // Sort the countries in alphabetical order by name. | |
161 countries = countries.sort(function(a, b) { | |
162 return a.name < b.name ? -1 : 1; | |
163 }); | |
164 | |
165 // Insert the empty and default countries at the beginning of the array. | |
166 var emptyCountry = { | |
167 countryCode: '', | |
168 name: '' | |
169 }; | |
170 var defaultCountry = { | |
171 countryCode: defaultCountryCode, | |
172 name: countryData[defaultCountryCode]['name'] | |
173 }; | |
174 countries.unshift(emptyCountry, defaultCountry); | |
175 | |
176 // Add the countries to the country <select> list. | |
177 var countryList = $('country'); | |
178 for (var i = 0; i < countries.length; i++) { | |
179 var country = new Option(countries[i].name, countries[i].countryCode); | |
180 countryList.appendChild(country) | |
181 } | |
182 }, | |
183 | |
184 /** | |
113 * Clears the value of each input field. | 185 * Clears the value of each input field. |
114 * @private | 186 * @private |
115 */ | 187 */ |
116 clearInputFields_: function() { | 188 clearInputFields_: function() { |
117 $('fullName').value = ''; | 189 $('fullName').value = ''; |
190 $('country').value = ''; | |
118 $('companyName').value = ''; | 191 $('companyName').value = ''; |
119 $('addrLine1').value = ''; | 192 $('addrLine1').value = ''; |
120 $('addrLine2').value = ''; | 193 $('addrLine2').value = ''; |
121 $('city').value = ''; | 194 $('city').value = ''; |
122 $('state').value = ''; | 195 $('state').value = ''; |
123 $('zipCode').value = ''; | 196 $('postal-code').value = ''; |
124 $('country').value = ''; | |
125 $('phone').value = ''; | 197 $('phone').value = ''; |
126 $('fax').value = ''; | 198 $('fax').value = ''; |
127 $('email').value = ''; | 199 $('email').value = ''; |
200 | |
201 this.countryChanged_(); | |
128 }, | 202 }, |
129 | 203 |
130 /** | 204 /** |
131 * Loads the address data from |address|, sets the input fields based on | 205 * Loads the address data from |address|, sets the input fields based on |
132 * this data and stores the GUID of the address. | 206 * this data and stores the GUID of the address. |
133 * @private | 207 * @private |
134 */ | 208 */ |
135 loadAddress_: function(address) { | 209 loadAddress_: function(address) { |
136 this.setInputFields_(address); | 210 this.setInputFields_(address); |
137 this.inputFieldChanged_(); | 211 this.inputFieldChanged_(); |
138 this.guid = address['guid']; | 212 this.guid = address['guid']; |
139 }, | 213 }, |
140 | 214 |
141 /** | 215 /** |
142 * Sets the value of each input field according to |address| | 216 * Sets the value of each input field according to |address| |
143 * @private | 217 * @private |
144 */ | 218 */ |
145 setInputFields_: function(address) { | 219 setInputFields_: function(address) { |
146 $('fullName').value = address['fullName']; | 220 $('fullName').value = address['fullName']; |
221 $('country').value = address['country']; | |
147 $('companyName').value = address['companyName']; | 222 $('companyName').value = address['companyName']; |
148 $('addrLine1').value = address['addrLine1']; | 223 $('addrLine1').value = address['addrLine1']; |
149 $('addrLine2').value = address['addrLine2']; | 224 $('addrLine2').value = address['addrLine2']; |
150 $('city').value = address['city']; | 225 $('city').value = address['city']; |
151 $('state').value = address['state']; | 226 $('state').value = address['state']; |
152 $('zipCode').value = address['zipCode']; | 227 $('postal-code').value = address['postal-code']; |
153 $('country').value = address['country']; | |
154 $('phone').value = address['phone']; | 228 $('phone').value = address['phone']; |
155 $('fax').value = address['fax']; | 229 $('fax').value = address['fax']; |
156 $('email').value = address['email']; | 230 $('email').value = address['email']; |
231 | |
232 this.countryChanged_(); | |
157 }, | 233 }, |
158 }; | 234 }; |
159 | 235 |
160 AutoFillEditAddressOverlay.clearInputFields = function() { | 236 AutoFillEditAddressOverlay.clearInputFields = function() { |
161 AutoFillEditAddressOverlay.getInstance().clearInputFields_(); | 237 AutoFillEditAddressOverlay.getInstance().clearInputFields_(); |
162 }; | 238 }; |
163 | 239 |
164 AutoFillEditAddressOverlay.loadAddress = function(address) { | 240 AutoFillEditAddressOverlay.loadAddress = function(address) { |
165 AutoFillEditAddressOverlay.getInstance().loadAddress_(address); | 241 AutoFillEditAddressOverlay.getInstance().loadAddress_(address); |
166 }; | 242 }; |
167 | 243 |
168 AutoFillEditAddressOverlay.setTitle = function(title) { | 244 AutoFillEditAddressOverlay.setTitle = function(title) { |
169 $('autoFillAddressTitle').textContent = title; | 245 $('autoFillAddressTitle').textContent = title; |
170 }; | 246 }; |
171 | 247 |
172 // Export | 248 // Export |
173 return { | 249 return { |
174 AutoFillEditAddressOverlay: AutoFillEditAddressOverlay | 250 AutoFillEditAddressOverlay: AutoFillEditAddressOverlay |
175 }; | 251 }; |
176 }); | 252 }); |
OLD | NEW |