|
1
|
|
|
define(function(require) { |
|
2
|
|
|
'use strict'; |
|
3
|
|
|
|
|
4
|
|
|
var AddressView; |
|
5
|
|
|
var _ = require('underscore'); |
|
6
|
|
|
var $ = require('jquery'); |
|
7
|
|
|
var mediator = require('oroui/js/mediator'); |
|
8
|
|
|
var BaseComponent = require('oroui/js/app/views/base/view'); |
|
9
|
|
|
|
|
10
|
|
|
AddressView = BaseComponent.extend({ |
|
11
|
|
|
options: { |
|
12
|
|
|
addedAddressOptionClass: 'option_added_address', |
|
13
|
|
|
selectors: { |
|
14
|
|
|
address: null, |
|
15
|
|
|
fieldsContainer: null, |
|
16
|
|
|
region: null, |
|
17
|
|
|
shipToBillingCheckbox: null, |
|
18
|
|
|
externalShipToBillingCheckbox: null |
|
19
|
|
|
} |
|
20
|
|
|
}, |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @inheritDoc |
|
24
|
|
|
*/ |
|
25
|
|
|
constructor: function AddressView() { |
|
26
|
|
|
AddressView.__super__.constructor.apply(this, arguments); |
|
27
|
|
|
}, |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @inheritDoc |
|
31
|
|
|
*/ |
|
32
|
|
|
initialize: function(options) { |
|
33
|
|
|
this.options = $.extend(true, {}, this.options, options); |
|
34
|
|
|
|
|
35
|
|
|
this.$addressSelector = this.$el.find(this.options.selectors.address); |
|
36
|
|
|
this.$fieldsContainer = this.$el.find(this.options.selectors.fieldsContainer); |
|
37
|
|
|
this.$regionSelector = this.$el.find(this.options.selectors.region); |
|
38
|
|
|
|
|
39
|
|
|
this.needCheckAddressTypes = this.options.selectors.shipToBillingCheckbox; |
|
40
|
|
|
if (this.needCheckAddressTypes) { |
|
41
|
|
|
this.typesMapping = this.$addressSelector.data('addresses-types'); |
|
42
|
|
|
this.$shipToBillingCheckbox = this.$el.find(this.options.selectors.shipToBillingCheckbox); |
|
43
|
|
|
this.$shipToBillingCheckbox.on('change', _.bind(this._handleShipToBillingAddressCheckbox, this)); |
|
44
|
|
|
this.shipToBillingContainer = this.$shipToBillingCheckbox.closest('fieldset'); |
|
45
|
|
|
if (this.options.selectors.externalShipToBillingCheckbox) { |
|
46
|
|
|
this.$externalShipToBillingCheckbox = $(this.options.selectors.externalShipToBillingCheckbox); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
this.$addressSelector.on('change', _.bind(this._onAddressChanged, this)); |
|
51
|
|
|
this.$regionSelector.on('change', _.bind(this._onRegionListChanged, this)); |
|
52
|
|
|
|
|
53
|
|
|
if (this.$fieldsContainer.find('.notification_error').length) { |
|
54
|
|
|
this.$fieldsContainer.removeClass('hidden'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
this._onAddressChanged(); |
|
58
|
|
|
this._handleShipToBillingAddressCheckbox(); |
|
59
|
|
|
mediator.on('checkout:address:updated', this._onAddressUpdated, this); |
|
60
|
|
|
mediator.on('checkout:ship_to_checkbox:changed', this._onShipToCheckboxChanged, this); |
|
61
|
|
|
}, |
|
62
|
|
|
|
|
63
|
|
|
_handleShipToBillingAddressCheckbox: function(e) { |
|
|
|
|
|
|
64
|
|
|
var disabled = false; |
|
65
|
|
|
if (this.needCheckAddressTypes) { |
|
66
|
|
|
disabled = this.$shipToBillingCheckbox.prop('checked') && this.$externalShipToBillingCheckbox; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
if (!disabled) { |
|
70
|
|
|
this.$addressSelector.find('option.' + this.options.addedAddressOptionClass).remove(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
var isFormVisible = this._isFormVisible(); |
|
74
|
|
|
var showNewAddressForm = !disabled && isFormVisible; |
|
75
|
|
|
this._handleNewAddressForm(showNewAddressForm); |
|
76
|
|
|
|
|
77
|
|
|
if (!showNewAddressForm) { |
|
78
|
|
|
this.$addressSelector.focus(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
var isSelectorNotAvailable = isFormVisible && this._isOnlyOneOption(); |
|
82
|
|
|
|
|
83
|
|
|
this.$addressSelector.prop('disabled', disabled || isSelectorNotAvailable).inputWidget('refresh'); |
|
84
|
|
|
|
|
85
|
|
|
mediator.trigger('checkout:ship_to_checkbox:changed', this.$shipToBillingCheckbox); |
|
86
|
|
|
if (isSelectorNotAvailable) { |
|
87
|
|
|
this.$addressSelector.inputWidget('dispose'); |
|
88
|
|
|
this.$addressSelector.hide().attr('data-skip-input-widgets', true); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// if external checkbox exists - synchronize it |
|
92
|
|
|
if (this.$externalShipToBillingCheckbox) { |
|
93
|
|
|
this.$externalShipToBillingCheckbox.off('change'); |
|
94
|
|
|
this.$externalShipToBillingCheckbox.prop('checked', disabled); |
|
95
|
|
|
this.$externalShipToBillingCheckbox.on( |
|
96
|
|
|
'change', |
|
97
|
|
|
_.bind(this._handleExternalShipToBillingAddressCheckbox, this) |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
}, |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param {Boolean} show |
|
104
|
|
|
* @private |
|
105
|
|
|
*/ |
|
106
|
|
|
_handleNewAddressForm: function(show) { |
|
107
|
|
|
if (!this.$externalShipToBillingCheckbox) { |
|
108
|
|
|
return; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
if (show) { |
|
112
|
|
|
this._showForm(); |
|
113
|
|
|
} else { |
|
114
|
|
|
this._hideForm(true); |
|
115
|
|
|
} |
|
116
|
|
|
}, |
|
117
|
|
|
|
|
118
|
|
|
_handleExternalShipToBillingAddressCheckbox: function() { |
|
119
|
|
|
this.$shipToBillingCheckbox.prop( |
|
120
|
|
|
'checked', |
|
121
|
|
|
this.$externalShipToBillingCheckbox.prop('checked') |
|
122
|
|
|
).trigger('change'); |
|
123
|
|
|
}, |
|
124
|
|
|
|
|
125
|
|
|
_onAddressChanged: function() { |
|
126
|
|
|
if (this._isFormVisible()) { |
|
127
|
|
|
this._showForm(); |
|
128
|
|
|
} else { |
|
129
|
|
|
this._hideForm(); |
|
130
|
|
|
} |
|
131
|
|
|
mediator.trigger('checkout:address:updated', this.$addressSelector); |
|
132
|
|
|
}, |
|
133
|
|
|
|
|
134
|
|
|
_onAddressUpdated: function($addressSelector) { |
|
135
|
|
|
if ($addressSelector === this.$addressSelector) { |
|
136
|
|
|
return; |
|
137
|
|
|
} |
|
138
|
|
|
if (this.$addressSelector.prop('disabled') && this.$shipToBillingCheckbox.prop('checked')) { |
|
139
|
|
|
var addressValue = $addressSelector.val(); |
|
140
|
|
|
var addressTitle = $addressSelector.find('option:selected').text(); |
|
141
|
|
|
this.$addressSelector.val(addressValue); |
|
142
|
|
|
// if no value - add needed value |
|
143
|
|
|
if (this.$addressSelector.val() !== addressValue) { |
|
144
|
|
|
var $addedAddress = this.$addressSelector.find('.' + this.options.addedAddressOptionClass); |
|
145
|
|
|
if (!$addedAddress.length) { |
|
146
|
|
|
$addedAddress = $('<option/>').addClass(this.options.addedAddressOptionClass); |
|
147
|
|
|
this.$addressSelector.append($addedAddress); |
|
148
|
|
|
} |
|
149
|
|
|
$addedAddress.attr('value', addressValue).text(addressTitle); |
|
150
|
|
|
this.$addressSelector.val(addressValue); |
|
151
|
|
|
} |
|
152
|
|
|
this.$addressSelector.inputWidget('refresh'); |
|
153
|
|
|
} |
|
154
|
|
|
}, |
|
155
|
|
|
|
|
156
|
|
|
_onShipToCheckboxChanged: function($shipToCheckbox) { |
|
157
|
|
|
if (!$shipToCheckbox || ($shipToCheckbox === this.$shipToBillingCheckbox)) { |
|
158
|
|
|
return; |
|
159
|
|
|
} |
|
160
|
|
|
if ($shipToCheckbox.prop('checked')) { |
|
161
|
|
|
mediator.trigger('checkout:address:updated', this.$addressSelector); |
|
162
|
|
|
} |
|
163
|
|
|
}, |
|
164
|
|
|
|
|
165
|
|
|
_isFormVisible: function() { |
|
166
|
|
|
return this.$addressSelector.val() === '0'; |
|
167
|
|
|
}, |
|
168
|
|
|
|
|
169
|
|
|
_isOnlyOneOption: function() { |
|
170
|
|
|
return this.$addressSelector[0].length === 1; |
|
171
|
|
|
}, |
|
172
|
|
|
|
|
173
|
|
|
_showForm: function() { |
|
174
|
|
|
if (this.needCheckAddressTypes) { |
|
175
|
|
|
this.shipToBillingContainer.removeClass('hidden'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
this.$fieldsContainer.removeClass('hidden'); |
|
179
|
|
|
}, |
|
180
|
|
|
|
|
181
|
|
|
_hideForm: function(showCheckbox) { |
|
182
|
|
|
if (this.needCheckAddressTypes) { |
|
183
|
|
|
if (showCheckbox || _.indexOf(this.typesMapping[this.$addressSelector.val()], 'shipping') > -1) { |
|
184
|
|
|
this.shipToBillingContainer.removeClass('hidden'); |
|
185
|
|
|
} else { |
|
186
|
|
|
this.$shipToBillingCheckbox.prop('checked', false); |
|
187
|
|
|
this.$shipToBillingCheckbox.trigger('change'); |
|
188
|
|
|
this.shipToBillingContainer.addClass('hidden'); |
|
189
|
|
|
} |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
this.$fieldsContainer.addClass('hidden'); |
|
193
|
|
|
}, |
|
194
|
|
|
|
|
195
|
|
|
_setAddressSelectorState: function(state) { |
|
196
|
|
|
this.$addressSelector.prop('disabled', state).inputWidget('refresh'); |
|
197
|
|
|
}, |
|
198
|
|
|
|
|
199
|
|
|
_onRegionListChanged: function(e) { |
|
|
|
|
|
|
200
|
|
|
this.$regionSelector.inputWidget('refresh'); |
|
201
|
|
|
} |
|
202
|
|
|
}); |
|
203
|
|
|
|
|
204
|
|
|
return AddressView; |
|
205
|
|
|
}); |
|
206
|
|
|
|
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.