@osimatic/helpers-js 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/array_field.html +186 -0
- package/form_helper.js +3 -4
- package/location.js +10 -0
- package/package.json +1 -1
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
<form action="#">
|
|
2
|
+
<div class="form-group field_emails">
|
|
3
|
+
<label>Liste des adresses email :</label>
|
|
4
|
+
<div class="hide list_empty"><em>aucune</em><br></div>
|
|
5
|
+
<table class="table table-sm">
|
|
6
|
+
<tbody>
|
|
7
|
+
<tr class="hide base">
|
|
8
|
+
<td><input type="hidden" name="emails[]"> <span class="value"></span></td>
|
|
9
|
+
<td class="text-end"><a href="#" title="Supprimer" class="remove btn btn-sm btn-danger"><i class="fas fa-times"></i></a></td>
|
|
10
|
+
</tr>
|
|
11
|
+
</tbody>
|
|
12
|
+
</table>
|
|
13
|
+
<div class="links text-center">
|
|
14
|
+
<a href="#" class="add_one btn btn-sm btn-success">Ajouter une adresse email</a>
|
|
15
|
+
</div>
|
|
16
|
+
<div class="item_add_one">
|
|
17
|
+
<div class="alert alert-danger pt-1 pb-1 errors hide"></div>
|
|
18
|
+
<div class="form-inline">
|
|
19
|
+
<input type="text" class="form-control" value="" placeholder="Adresse email" size="40" />
|
|
20
|
+
<a href="#" title="Ajouter" class="add btn btn-success"><i class="fas fa-plus"></i></a>
|
|
21
|
+
<a href="#" class="cancel">Annuler</a>
|
|
22
|
+
</div>
|
|
23
|
+
</div>
|
|
24
|
+
<div class="item_add_mutli">
|
|
25
|
+
<div class="alert alert-danger pt-1 pb-1 errors hide"></div>
|
|
26
|
+
<div class="form-group">
|
|
27
|
+
<label>Liste des adresses email :</label>
|
|
28
|
+
<textarea name="phone_numbers" class="form-control" rows="10"></textarea>
|
|
29
|
+
<span class="form-text">Une adresse email par ligne.</span>
|
|
30
|
+
</div>
|
|
31
|
+
<div class="form-inline">
|
|
32
|
+
<a href="#" title="Ajouter" class="add btn btn-success"><i class="fas fa-plus"></i></a>
|
|
33
|
+
<a href="#" class="cancel">Annuler</a>
|
|
34
|
+
</div>
|
|
35
|
+
</div>
|
|
36
|
+
</div>
|
|
37
|
+
|
|
38
|
+
<div class="form-group field_phone_numbers">
|
|
39
|
+
<label>Liste des numéros de téléphone :</label>
|
|
40
|
+
<div class="hide list_empty"><em>aucun</em><br></div>
|
|
41
|
+
<table class="table table-sm">
|
|
42
|
+
<tbody>
|
|
43
|
+
<tr class="hide base">
|
|
44
|
+
<td><input type="hidden" name="phone_numbers[]"> <span class="value"></span></td>
|
|
45
|
+
<td class="text-end"><a href="#" title="Supprimer" class="remove btn btn-sm btn-danger"><i class="fas fa-times"></i></a></td>
|
|
46
|
+
</tr>
|
|
47
|
+
</tbody>
|
|
48
|
+
</table>
|
|
49
|
+
<div class="links text-center">
|
|
50
|
+
<a href="#" class="add_one btn btn-sm btn-success">Ajouter un numéro de téléphone</a>
|
|
51
|
+
</div>
|
|
52
|
+
<div class="item_add_one">
|
|
53
|
+
<div class="alert alert-danger pt-1 pb-1 errors hide"></div>
|
|
54
|
+
<div class="form-inline">
|
|
55
|
+
<input type="text" class="form-control" value="" placeholder="Numéro de téléphone" size="40" />
|
|
56
|
+
<a href="#" title="Ajouter" class="add btn btn-success"><i class="fas fa-plus"></i></a>
|
|
57
|
+
<a href="#" class="cancel">Annuler</a>
|
|
58
|
+
</div>
|
|
59
|
+
</div>
|
|
60
|
+
<div class="item_add_mutli">
|
|
61
|
+
<div class="alert alert-danger pt-1 pb-1 errors hide"></div>
|
|
62
|
+
<div class="form-group">
|
|
63
|
+
<label>Pays des numéros de téléphone :</label>
|
|
64
|
+
<select name="country" class="form-control"></select>
|
|
65
|
+
</div>
|
|
66
|
+
<div class="form-group">
|
|
67
|
+
<label>Liste des numéros de téléphone :</label>
|
|
68
|
+
<textarea name="phone_numbers" class="form-control" rows="10"></textarea>
|
|
69
|
+
<span class="form-text">Un numéro de téléphone par ligne.</span>
|
|
70
|
+
</div>
|
|
71
|
+
<div class="form-inline">
|
|
72
|
+
<a href="#" title="Ajouter" class="add btn btn-success"><i class="fas fa-plus"></i></a>
|
|
73
|
+
<a href="#" class="cancel">Annuler</a>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
</div>
|
|
77
|
+
</form>
|
|
78
|
+
|
|
79
|
+
<script>
|
|
80
|
+
const form = $('form');
|
|
81
|
+
ArrayField.init(form.find('.field_emails'), ['email@company.com'], {
|
|
82
|
+
entering_field_in_table: false,
|
|
83
|
+
item_name: 'Adresse email',
|
|
84
|
+
get_errors_callback: (email, itemsList) => {
|
|
85
|
+
if (Array.isArray(email) && email.length > 1) {
|
|
86
|
+
const emails = email;
|
|
87
|
+
if (emails.length === 0) {
|
|
88
|
+
return 'Veuillez saisir au moins une adresse email.';
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
let errors = [];
|
|
92
|
+
for (let i = 0; i < emails.length; i++) {
|
|
93
|
+
if (false === Email.validateEmail(emails[i])) {
|
|
94
|
+
errors.push(('L’adresse email n°{0} est incorrecte.').format(i + 1));
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (itemsList.indexOf(emails[i]) !== -1) {
|
|
99
|
+
errors.push(('L’adresse email n°{0} a déjà été ajoutée.').format(i + 1));
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return errors.length ? errors : null;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
email = Array.isArray(email) ? email[0] : email;
|
|
108
|
+
|
|
109
|
+
if (null == email || '' === email) {
|
|
110
|
+
return 'Veuillez saisir une adresse email.';
|
|
111
|
+
}
|
|
112
|
+
if (false === Email.validateEmail(email)) {
|
|
113
|
+
return 'Cette adresse email est incorrecte.';
|
|
114
|
+
}
|
|
115
|
+
if (itemsList.indexOf(email) !== -1) {
|
|
116
|
+
return 'Cette adresse email a déjà été ajoutée.';
|
|
117
|
+
}
|
|
118
|
+
return null;
|
|
119
|
+
},
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
ArrayField.init(form.find('field_phone_numbers'), ['+33601020304'], {
|
|
123
|
+
entering_field_in_table: false,
|
|
124
|
+
item_name: 'Numéro de téléphone',
|
|
125
|
+
init_callback: (div) => {
|
|
126
|
+
let itiPhoneNumberField = TelephoneNumber.setIntlTelInput(div.find('.item_add_one input[type="text"]'));
|
|
127
|
+
Location.fillCountrySelect(div.find('select[name="country"]'), 'FR');
|
|
128
|
+
},
|
|
129
|
+
format_entered_value_callback: (value, addDiv) => {
|
|
130
|
+
if (addDiv.hasClass('item_add_one')) {
|
|
131
|
+
const itiPhoneNumberField = TelephoneNumber.getIntlTelInputInstance(addDiv.find('input[type="text"]'));
|
|
132
|
+
return TelephoneNumber.getEnteredNumberInInternationalFormat(itiPhoneNumberField);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
let country = addDiv.find('select[name="country"]').val();
|
|
136
|
+
if (false !== TelephoneNumber.check(value, country)) {
|
|
137
|
+
return TelephoneNumber.parse(value, country);
|
|
138
|
+
}
|
|
139
|
+
return value;
|
|
140
|
+
},
|
|
141
|
+
get_errors_callback: (phoneNumber, itemsList, addDiv) => {
|
|
142
|
+
if (Array.isArray(phoneNumber) && phoneNumber.length > 1) {
|
|
143
|
+
const phoneNumbers = phoneNumber;
|
|
144
|
+
|
|
145
|
+
if (phoneNumbers.length === 0) {
|
|
146
|
+
return 'Veuillez saisir au moins un numéro de téléphone.';
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
let country = addDiv.find('select[name="country"]').val();
|
|
150
|
+
|
|
151
|
+
let errors = [];
|
|
152
|
+
for (let i = 0; i < phoneNumbers.length; i++) {
|
|
153
|
+
if (false === TelephoneNumber.check(phoneNumbers[i], country)) {
|
|
154
|
+
errors.push(('Le numéro de téléphone n°{0} est incorrect.').format(i + 1));
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
let phoneNumber = TelephoneNumber.parse(phoneNumbers[i], country);
|
|
159
|
+
if (null === phoneNumber) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (itemsList.indexOf(phoneNumber) !== -1) {
|
|
164
|
+
errors.push(('Le numéro de téléphone n°{0} a déjà été ajouté.').format(i + 1));
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
return errors.length ? errors : null;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
phoneNumber = Array.isArray(phoneNumber) ? phoneNumber[0] : phoneNumber;
|
|
173
|
+
|
|
174
|
+
const itiPhoneNumberField = TelephoneNumber.getIntlTelInputInstance(addDiv.find('input[type="text"]'));
|
|
175
|
+
if (null == phoneNumber || '' === phoneNumber) {
|
|
176
|
+
return 'Veuillez saisir un numéro de téléphone.';
|
|
177
|
+
}
|
|
178
|
+
if (!TelephoneNumber.check(phoneNumber, itiPhoneNumberField.defaultCountry)) {
|
|
179
|
+
return 'Ce numéro de téléphone est incorrect.';
|
|
180
|
+
}
|
|
181
|
+
if (itemsList.indexOf(phoneNumber) !== -1) {
|
|
182
|
+
return 'Ce numéro de téléphone a déjà été ajouté.';
|
|
183
|
+
}
|
|
184
|
+
},
|
|
185
|
+
});
|
|
186
|
+
</script>
|
package/form_helper.js
CHANGED
|
@@ -500,6 +500,7 @@ class ArrayField {
|
|
|
500
500
|
formGroupDiv.find('a.add_one, a.add_multi').addClass('hide').closest('.links').addClass('hide');
|
|
501
501
|
formGroupDiv.find('.item_add_one, .item_add_multi').addClass('hide');
|
|
502
502
|
formGroupDiv.find('.item_add_one, .item_add_multi').find('input[type="text"], textarea').val('');
|
|
503
|
+
formGroupDiv.find('.item_add_one, .item_add_multi').find('.errors').addClass('hide');
|
|
503
504
|
}
|
|
504
505
|
function cancelAdd() {
|
|
505
506
|
formGroupDiv.find('a.add_one, a.add_multi').removeClass('hide').closest('.links').removeClass('hide');
|
|
@@ -522,12 +523,10 @@ class ArrayField {
|
|
|
522
523
|
}
|
|
523
524
|
|
|
524
525
|
function submitAddNewItem(item, divAdd) {
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
divAdd.find('.errors').addClass('hide');
|
|
526
|
+
let items = Array.isArray(item) ? item : [item];
|
|
528
527
|
|
|
529
528
|
if (typeof options['format_entered_value_callback'] == 'function') {
|
|
530
|
-
items.map(item => options['format_entered_value_callback'](item, divAdd));
|
|
529
|
+
items = items.map(item => options['format_entered_value_callback'](item, divAdd));
|
|
531
530
|
}
|
|
532
531
|
|
|
533
532
|
if (typeof options['get_errors_callback'] == 'function') {
|
package/location.js
CHANGED
|
@@ -427,6 +427,16 @@ class GeographicCoordinates {
|
|
|
427
427
|
static check(str) {
|
|
428
428
|
return /^[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)$/.test(str);
|
|
429
429
|
}
|
|
430
|
+
|
|
431
|
+
static parse(str) {
|
|
432
|
+
if (null === str || '' === str || 'NaN,NaN' === str) {
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return str.split(',').map(str => str.trim()).join(',');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
|
|
430
440
|
}
|
|
431
441
|
|
|
432
442
|
module.exports = { Country, PostalAddress, GeographicCoordinates };
|