forked from Endereco/endereco-wp5-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endereco.js
153 lines (136 loc) · 4.42 KB
/
endereco.js
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
import Promise from 'promise-polyfill';
import merge from 'lodash.merge';
import EnderecoIntegrator from './node_modules/@endereco/js-sdk/modules/integrator';
import css from './endereco.scss';
import 'polyfill-array-includes';
if ('NodeList' in window && !NodeList.prototype.forEach) {
NodeList.prototype.forEach = function (callback, thisArg) {
thisArg = thisArg || window;
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, this[i], i, this);
}
};
}
if (!window.Promise) {
window.Promise = Promise;
}
EnderecoIntegrator.postfix = {
ams: {
countryCode: '',
postalCode: '',
locality: '',
streetFull: '',
streetName: '',
buildingNumber: '',
addressStatus: '',
addressTimestamp: '',
addressPredictions: '',
additionalInfo: '',
},
personServices: {
salutation: '',
firstName: ''
},
emailServices: {
email: ''
}
};
EnderecoIntegrator.css = css[0][1];
EnderecoIntegrator.resolvers.countryCodeWrite = function (value) {
return new Promise(function (resolve, reject) {
resolve(value.toUpperCase());
});
}
EnderecoIntegrator.resolvers.countryCodeSetValue = function (subscriber, value) {
if (
!!jQuery
) {
jQuery(subscriber.object).val(value).trigger('change');
} else {
subscriber.object.value = value;
}
}
EnderecoIntegrator.resolvers.subdivisionCodeSetValue = function (subscriber, value) {
if (
!!jQuery
) {
jQuery(subscriber.object).val(value).trigger('change');
} else {
subscriber.object.value = value;
}
}
EnderecoIntegrator.resolvers.countryCodeRead = function (value) {
return new Promise(function (resolve, reject) {
resolve(value.toUpperCase());
});
}
EnderecoIntegrator.resolvers.salutationWrite = function (value) {
var mapping = {
'F': 'ms',
'M': 'mr'
};
return new Promise(function (resolve, reject) {
resolve(mapping[value]);
});
}
EnderecoIntegrator.resolvers.salutationRead = function (value) {
var mapping = {
'ms': 'F',
'mr': 'M'
};
return new Promise(function (resolve, reject) {
resolve(mapping[value]);
});
}
EnderecoIntegrator.onAjaxFormHandler.push( function(EAO) {
EAO.forms.forEach( function(form) {
var submitButtons = form.querySelectorAll('[type="submit"]');
submitButtons.forEach( function(buttonElement) {
buttonElement.addEventListener('click', function(e) {
if (EAO.util.shouldBeChecked()) {
e.preventDefault();
e.stopPropagation();
if (window.EnderecoIntegrator && !window.EnderecoIntegrator.submitResume) {
window.EnderecoIntegrator.submitResume = function() {
if(form.dispatchEvent(
new EAO.util.CustomEvent(
'submit',
{
'bubbles': true,
'cancelable': true
}
)
)) {
form.submit();
}
window.EnderecoIntegrator.submitResume = undefined;
}
}
EAO.util.checkAddress()
.catch(function() {
EAO.waitForAllPopupsToClose().then(function() {
if (window.EnderecoIntegrator && window.EnderecoIntegrator.submitResume) {
window.EnderecoIntegrator.submitResume();
}
}).catch()
});
return false;
}
})
})
})
});
EnderecoIntegrator.afterAMSActivation.push( function(EAO) {
});
if (window.EnderecoIntegrator) {
window.EnderecoIntegrator = merge(window.EnderecoIntegrator, EnderecoIntegrator);
} else {
window.EnderecoIntegrator = EnderecoIntegrator;
}
window.EnderecoIntegrator.asyncCallbacks.forEach(function (cb) {
cb();
});
window.EnderecoIntegrator.asyncCallbacks = [];
window.EnderecoIntegrator.waitUntilReady().then(function () {
//
});