@osimatic/helpers-js 1.1.77 → 1.1.78

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.
@@ -1,274 +1,274 @@
1
-
2
- class ImportFromCsv {
3
-
4
- static initForm(div, importColumns, requestImportData, specificDescDiv, additionalFormField) {
5
- div.empty().append($('.import_form_base').clone().removeClass('import_form_base hide'));
6
-
7
- let formUpload = div.find('.form_upload');
8
- let formMatching = div.find('.form_matching');
9
- let divResult = div.find('.csv_result');
10
-
11
- function resetUi() {
12
- formMatching.addClass('hide');
13
- formUpload.removeClass('hide');
14
- formUpload.find('div.errors').addClass('hide');
15
- }
16
-
17
- if (typeof specificDescDiv != 'undefined' && specificDescDiv != null) {
18
- div.find('.specific_desc').append(specificDescDiv);
19
- }
20
-
21
- if (typeof additionalFormField != 'undefined' && additionalFormField != null) {
22
- div.find('.import_matching_select_content').after(additionalFormField);
23
- }
24
-
25
- formUpload.find('button[type="submit"]').click(function(event) {
26
- event.preventDefault();
27
- FormHelper.buttonLoader($(this), 'loading');
28
- formUpload.find('div.errors').addClass('hide');
29
-
30
- let isFileParsed = false;
31
- let hasHeader = formUpload.find('input[name="header"][value="1"]:checked').length;
32
- let encoding = formUpload.find('select[name="encoding"]').val();
33
-
34
- formUpload.find('input[type="file"]').parse({
35
- config: {
36
- header: hasHeader,
37
- encoding: encoding,
38
- dynamicTyping: false,
39
- skipEmptyLines: true,
40
- beforeFirstChunk: function(chunk) {
41
- return chunk.trim();
42
- },
43
- complete: function(results, file) {
44
- isFileParsed = true;
45
- //console.log(file, results);
46
-
47
- if (false === CSV.checkFile(file.name, file.type)) {
48
- $('#form_import_upload div.errors').html(errorMessageFileNotValid).removeClass('hide');
49
- return;
50
- }
51
-
52
- let parsedImportList = results.data;
53
- let header = hasHeader?results.meta.fields:results.data[0];
54
-
55
- ImportFromCsv.displayData(divResult, parsedImportList, (hasHeader?header:null), formMatching);
56
- ImportFromCsv.displayFormMatching(formMatching, importColumns, header, hasHeader);
57
-
58
- formUpload.addClass('hide');
59
- }
60
- },
61
- before: function(file, inputElem) {
62
- },
63
- error: function(err, file, inputElem, reason) {
64
- isFileParsed = true;
65
- formUpload.find('div.errors').html(errorMessageFileNotValid).removeClass('hide');
66
- console.error(err, file, reason);
67
- },
68
- complete: function() {
69
- if (!isFileParsed) {
70
- formUpload.find('div.errors').html(errorMessageFileEmpty).removeClass('hide');
71
- }
72
- FormHelper.buttonLoader(formUpload.find('button[type="submit"]'), 'reset');
73
- }
74
- });
75
- event.preventDefault();
76
- });
77
-
78
- formMatching.find('button[type="submit"]').click(function (event) {
79
- event.preventDefault();
80
- FormHelper.buttonLoader($(this), 'loading');
81
- formMatching.find('div.errors').addClass('hide').empty();
82
- divResult.find('table tr').removeClass('danger');
83
-
84
- let tabLink = ImportFromCsv.getTabLink(formMatching);
85
- //console.log('tabLink', tabLink);
86
-
87
- if ($.isEmptyObject(tabLink)) {
88
- formMatching.find('div.errors').html(errorMessageImportSelectColumns).removeClass('hide');
89
- FormHelper.buttonLoader($(this), 'reset');
90
- return false;
91
- }
92
-
93
- let dataToImport = ImportFromCsv.getDataToImport(divResult, tabLink);
94
- //console.log('dataToImport', dataToImport);
95
-
96
- requestImportData(dataToImport,
97
- // fonction callback en cas d'erreur de formulaire
98
- (json) => {
99
- //console.log(json);
100
- if (typeof json['import_list'] !== 'undefined') {
101
- formMatching.find('div.errors').html(json['import_list']).removeClass('hide');
102
- }
103
- else {
104
- formMatching.find('div.errors').html(ImportFromCsv.getErrorsHtmlOfImportData(json, divResult)).removeClass('hide');
105
- }
106
- FormHelper.buttonLoader(formMatching.find('button[type="submit"]'), 'reset');
107
- }
108
- );
109
- });
110
-
111
- formMatching.find('a.cancel_link').click(function (event) {
112
- resetUi();
113
- });
114
-
115
- resetUi();
116
- }
117
-
118
- static getDataToImport(divResult, tabLink) {
119
- let importListWithFieldNames = [];
120
- $.each(divResult.find('table tbody tr'), function(index, line) {
121
- // console.log('line', line);
122
- if (!$(line).find('input.import_line_checkbox:checked').length) {
123
- // if (!divResult.find('table tr[data-line="'+(index+1)+'"] input.import_line_checkbox:checked').length) {
124
- return;
125
- }
126
-
127
- let lineData = {line: (index+1)};
128
- $.each(tabLink, function(key, listeImportIndex) {
129
- if (listeImportIndex != -1) {
130
- var td = $(line).find('td[data-key="'+listeImportIndex+'"]');
131
- if (td.length) {
132
- lineData[key] = td.text();
133
- }
134
- }
135
- });
136
- //console.log('lineData', lineData);
137
- importListWithFieldNames.push(lineData);
138
- });
139
- return importListWithFieldNames;
140
- }
141
-
142
- static displayData(divResult, data, header, formMatching) {
143
- let table = divResult.find('table');
144
- if (table.length === 0) {
145
- divResult.append('<table class="table table-sm table-bordered"></table>');
146
- table = divResult.find('table');
147
- }
148
- table.empty();
149
-
150
- let tableContent = '';
151
- if (null !== header) {
152
- tableContent += '<thead><tr>';
153
- //tableContent += '<th><input type="checkbox" class="import_line_select_all" /></th>';
154
- tableContent += '<th></th>';
155
- $.each(header, function (index, value) {
156
- tableContent += '<th>'+value+'</th>';
157
- });
158
- tableContent += '<th></th>';
159
- tableContent += '</tr></thead>';
160
- }
161
-
162
- tableContent += '<tbody>';
163
- $.each(data, function (index, line) {
164
- tableContent += '<tr data-line="'+(index+1)+'">';
165
- tableContent += '<td class="text-bold text-end select_line_checkbox"><input type="checkbox" class="import_line_checkbox pull-left" checked="checked" /> '+(index+1)+'.</td>';
166
- $.each(line, function (key, value) {
167
- tableContent += '<td data-key="'+key+'">'+(value!==null?value:'')+'</td>';
168
- });
169
- tableContent += '<td class="text-center edit_line_button"></td>';
170
- tableContent +='</tr>';
171
- });
172
- tableContent += '</tbody>';
173
-
174
- table.html(tableContent);
175
-
176
- table.find('td.edit_line_button').each(function(idx, el) {
177
- ImportFromCsv.initEditLink(formMatching, $(el));
178
- });
179
-
180
- divResult.removeClass('hide');
181
- }
182
-
183
- static initValidateLine(formMatching, td) {
184
- td.html($('<a href="#" class="import_validate_line text-success"><i class="fas fa-check"></i></a>'));
185
- td.find('a.import_validate_line').click(function () {
186
- let tr = $(this).parent().parent();
187
- tr.find('td').each(function(key, el) {
188
- let td = $(el);
189
- if (td.hasClass('select_line_checkbox') || td.hasClass('edit_line_button')) {
190
- return;
191
- }
192
- td.html(td.find('input').val());
193
- });
194
-
195
- if (!td.closest('table').find('td input[type="text"]').length) {
196
- formMatching.find('button[type="submit"]').prop('disabled', false);
197
- }
198
- ImportFromCsv.initEditLink(formMatching, td);
199
- return false;
200
- });
201
- }
202
-
203
- static initEditLink(formMatching, td) {
204
- td.html($('<a href="#" class="import_edit_line text-danger"><i class="fas fa-pencil-alt"></i></a>'));
205
- td.find('a.import_edit_line').click(function () {
206
- let tr = $(this).parent().parent();
207
- tr.find('td').each(function(key, el) {
208
- let td = $(el);
209
- if (td.hasClass('select_line_checkbox') || td.hasClass('edit_line_button')) {
210
- return;
211
- }
212
- td.data('original_value', td.html());
213
- td.html($('<input type="text" class="form-control" value="'+td.html()+'" />'));
214
- });
215
- formMatching.find('button[type="submit"]').prop('disabled', true);
216
- ImportFromCsv.initValidateLine(formMatching, td);
217
- return false;
218
- });
219
- }
220
-
221
- static getErrorsHtmlOfImportData(json, divResult) {
222
- let resultError = errorMessageImportFailed;
223
- resultError += '<ul>';
224
- $.each(json, function(idx, errorData) {
225
- console.error(errorData);
226
- divResult.find('table tr[data-line="'+errorData.line+'"]').addClass('danger');
227
-
228
- resultError += '<li>'+lineLabel.format(errorData.line)+'<ul>';
229
- $.each(errorData.errors, function(index, error) {
230
- resultError += '<li>'+error+'</li>';
231
- });
232
- resultError += '</ul></li>';
233
- });
234
- resultError +='</ul>';
235
- return resultError;
236
- }
237
-
238
- static getTabLink(formMatching) {
239
- let tabLink = {};
240
- formMatching.find('select').each(function(idx, select) {
241
- var listeImportIndex = $(select).val();
242
- if (listeImportIndex != -1) {
243
- let key = $(select).prop('name');
244
- tabLink[key] = listeImportIndex;
245
- }
246
- });
247
- return tabLink;
248
- }
249
-
250
- static displayFormMatching(formMatching, importColumns, header, hasHeader) {
251
- let options = '<option value="-1">'+selectDefaultOptionLabel+'</option>';
252
- $.each(header, function (index, value) {
253
- options += '<option value="'+(hasHeader?value:index)+'">' + value + '</option>';
254
- });
255
-
256
- let selectContent = formMatching.find('.import_matching_select_content').addClass('row').empty();
257
- $.each(importColumns, function (key, label) {
258
- let selectFormGroup = $(
259
- '<div class="form-group col-md-3">' +
260
- '<label for="form_import_'+key+'">'+label+'</label>' +
261
- '<select class="form-control" name="'+key+'" id="form_import_'+key+'">'+options+'</select>' +
262
- '</div>'
263
- );
264
- selectFormGroup.find('select option:contains('+label+')').prop('selected', true);
265
- selectContent.append(selectFormGroup);
266
- });
267
-
268
- formMatching.find('div.errors').addClass('hide');
269
- formMatching.removeClass('hide');
270
- }
271
-
272
- }
273
-
1
+
2
+ class ImportFromCsv {
3
+
4
+ static initForm(div, importColumns, requestImportData, specificDescDiv, additionalFormField) {
5
+ div.empty().append($('.import_form_base').clone().removeClass('import_form_base hide'));
6
+
7
+ let formUpload = div.find('.form_upload');
8
+ let formMatching = div.find('.form_matching');
9
+ let divResult = div.find('.csv_result');
10
+
11
+ function resetUi() {
12
+ formMatching.addClass('hide');
13
+ formUpload.removeClass('hide');
14
+ formUpload.find('div.errors').addClass('hide');
15
+ }
16
+
17
+ if (typeof specificDescDiv != 'undefined' && specificDescDiv != null) {
18
+ div.find('.specific_desc').append(specificDescDiv);
19
+ }
20
+
21
+ if (typeof additionalFormField != 'undefined' && additionalFormField != null) {
22
+ div.find('.import_matching_select_content').after(additionalFormField);
23
+ }
24
+
25
+ formUpload.find('button[type="submit"]').click(function(event) {
26
+ event.preventDefault();
27
+ FormHelper.buttonLoader($(this), 'loading');
28
+ formUpload.find('div.errors').addClass('hide');
29
+
30
+ let isFileParsed = false;
31
+ let hasHeader = formUpload.find('input[name="header"][value="1"]:checked').length;
32
+ let encoding = formUpload.find('select[name="encoding"]').val();
33
+
34
+ formUpload.find('input[type="file"]').parse({
35
+ config: {
36
+ header: hasHeader,
37
+ encoding: encoding,
38
+ dynamicTyping: false,
39
+ skipEmptyLines: true,
40
+ beforeFirstChunk: function(chunk) {
41
+ return chunk.trim();
42
+ },
43
+ complete: function(results, file) {
44
+ isFileParsed = true;
45
+ //console.log(file, results);
46
+
47
+ if (false === CSV.checkFile(file.name, file.type)) {
48
+ $('#form_import_upload div.errors').html(errorMessageFileNotValid).removeClass('hide');
49
+ return;
50
+ }
51
+
52
+ let parsedImportList = results.data;
53
+ let header = hasHeader?results.meta.fields:results.data[0];
54
+
55
+ ImportFromCsv.displayData(divResult, parsedImportList, (hasHeader?header:null), formMatching);
56
+ ImportFromCsv.displayFormMatching(formMatching, importColumns, header, hasHeader);
57
+
58
+ formUpload.addClass('hide');
59
+ }
60
+ },
61
+ before: function(file, inputElem) {
62
+ },
63
+ error: function(err, file, inputElem, reason) {
64
+ isFileParsed = true;
65
+ formUpload.find('div.errors').html(errorMessageFileNotValid).removeClass('hide');
66
+ console.error(err, file, reason);
67
+ },
68
+ complete: function() {
69
+ if (!isFileParsed) {
70
+ formUpload.find('div.errors').html(errorMessageFileEmpty).removeClass('hide');
71
+ }
72
+ FormHelper.buttonLoader(formUpload.find('button[type="submit"]'), 'reset');
73
+ }
74
+ });
75
+ event.preventDefault();
76
+ });
77
+
78
+ formMatching.find('button[type="submit"]').click(function (event) {
79
+ event.preventDefault();
80
+ FormHelper.buttonLoader($(this), 'loading');
81
+ formMatching.find('div.errors').addClass('hide').empty();
82
+ divResult.find('table tr').removeClass('danger');
83
+
84
+ let tabLink = ImportFromCsv.getTabLink(formMatching);
85
+ //console.log('tabLink', tabLink);
86
+
87
+ if ($.isEmptyObject(tabLink)) {
88
+ formMatching.find('div.errors').html(errorMessageImportSelectColumns).removeClass('hide');
89
+ FormHelper.buttonLoader($(this), 'reset');
90
+ return false;
91
+ }
92
+
93
+ let dataToImport = ImportFromCsv.getDataToImport(divResult, tabLink);
94
+ //console.log('dataToImport', dataToImport);
95
+
96
+ requestImportData(dataToImport,
97
+ // fonction callback en cas d'erreur de formulaire
98
+ (json) => {
99
+ //console.log(json);
100
+ if (typeof json['import_list'] !== 'undefined') {
101
+ formMatching.find('div.errors').html(json['import_list']).removeClass('hide');
102
+ }
103
+ else {
104
+ formMatching.find('div.errors').html(ImportFromCsv.getErrorsHtmlOfImportData(json, divResult)).removeClass('hide');
105
+ }
106
+ FormHelper.buttonLoader(formMatching.find('button[type="submit"]'), 'reset');
107
+ }
108
+ );
109
+ });
110
+
111
+ formMatching.find('a.cancel_link').click(function (event) {
112
+ resetUi();
113
+ });
114
+
115
+ resetUi();
116
+ }
117
+
118
+ static getDataToImport(divResult, tabLink) {
119
+ let importListWithFieldNames = [];
120
+ $.each(divResult.find('table tbody tr'), function(index, line) {
121
+ // console.log('line', line);
122
+ if (!$(line).find('input.import_line_checkbox:checked').length) {
123
+ // if (!divResult.find('table tr[data-line="'+(index+1)+'"] input.import_line_checkbox:checked').length) {
124
+ return;
125
+ }
126
+
127
+ let lineData = {line: (index+1)};
128
+ $.each(tabLink, function(key, listeImportIndex) {
129
+ if (listeImportIndex != -1) {
130
+ var td = $(line).find('td[data-key="'+listeImportIndex+'"]');
131
+ if (td.length) {
132
+ lineData[key] = td.text();
133
+ }
134
+ }
135
+ });
136
+ //console.log('lineData', lineData);
137
+ importListWithFieldNames.push(lineData);
138
+ });
139
+ return importListWithFieldNames;
140
+ }
141
+
142
+ static displayData(divResult, data, header, formMatching) {
143
+ let table = divResult.find('table');
144
+ if (table.length === 0) {
145
+ divResult.append('<table class="table table-sm table-bordered"></table>');
146
+ table = divResult.find('table');
147
+ }
148
+ table.empty();
149
+
150
+ let tableContent = '';
151
+ if (null !== header) {
152
+ tableContent += '<thead><tr>';
153
+ //tableContent += '<th><input type="checkbox" class="import_line_select_all" /></th>';
154
+ tableContent += '<th></th>';
155
+ $.each(header, function (index, value) {
156
+ tableContent += '<th>'+value+'</th>';
157
+ });
158
+ tableContent += '<th></th>';
159
+ tableContent += '</tr></thead>';
160
+ }
161
+
162
+ tableContent += '<tbody>';
163
+ $.each(data, function (index, line) {
164
+ tableContent += '<tr data-line="'+(index+1)+'">';
165
+ tableContent += '<td class="text-bold text-end select_line_checkbox"><input type="checkbox" class="import_line_checkbox pull-left" checked="checked" /> '+(index+1)+'.</td>';
166
+ $.each(line, function (key, value) {
167
+ tableContent += '<td data-key="'+key+'">'+(value!==null?value:'')+'</td>';
168
+ });
169
+ tableContent += '<td class="text-center edit_line_button"></td>';
170
+ tableContent +='</tr>';
171
+ });
172
+ tableContent += '</tbody>';
173
+
174
+ table.html(tableContent);
175
+
176
+ table.find('td.edit_line_button').each(function(idx, el) {
177
+ ImportFromCsv.initEditLink(formMatching, $(el));
178
+ });
179
+
180
+ divResult.removeClass('hide');
181
+ }
182
+
183
+ static initValidateLine(formMatching, td) {
184
+ td.html($('<a href="#" class="import_validate_line text-success"><i class="fas fa-check"></i></a>'));
185
+ td.find('a.import_validate_line').click(function () {
186
+ let tr = $(this).parent().parent();
187
+ tr.find('td').each(function(key, el) {
188
+ let td = $(el);
189
+ if (td.hasClass('select_line_checkbox') || td.hasClass('edit_line_button')) {
190
+ return;
191
+ }
192
+ td.html(td.find('input').val());
193
+ });
194
+
195
+ if (!td.closest('table').find('td input[type="text"]').length) {
196
+ formMatching.find('button[type="submit"]').prop('disabled', false);
197
+ }
198
+ ImportFromCsv.initEditLink(formMatching, td);
199
+ return false;
200
+ });
201
+ }
202
+
203
+ static initEditLink(formMatching, td) {
204
+ td.html($('<a href="#" class="import_edit_line text-danger"><i class="fas fa-pencil-alt"></i></a>'));
205
+ td.find('a.import_edit_line').click(function () {
206
+ let tr = $(this).parent().parent();
207
+ tr.find('td').each(function(key, el) {
208
+ let td = $(el);
209
+ if (td.hasClass('select_line_checkbox') || td.hasClass('edit_line_button')) {
210
+ return;
211
+ }
212
+ td.data('original_value', td.html());
213
+ td.html($('<input type="text" class="form-control" value="'+td.html()+'" />'));
214
+ });
215
+ formMatching.find('button[type="submit"]').prop('disabled', true);
216
+ ImportFromCsv.initValidateLine(formMatching, td);
217
+ return false;
218
+ });
219
+ }
220
+
221
+ static getErrorsHtmlOfImportData(json, divResult) {
222
+ let resultError = errorMessageImportFailed;
223
+ resultError += '<ul>';
224
+ $.each(json, function(idx, errorData) {
225
+ console.error(errorData);
226
+ divResult.find('table tr[data-line="'+errorData.line+'"]').addClass('danger');
227
+
228
+ resultError += '<li>'+lineLabel.format(errorData.line)+'<ul>';
229
+ $.each(errorData.errors, function(index, error) {
230
+ resultError += '<li>'+error+'</li>';
231
+ });
232
+ resultError += '</ul></li>';
233
+ });
234
+ resultError +='</ul>';
235
+ return resultError;
236
+ }
237
+
238
+ static getTabLink(formMatching) {
239
+ let tabLink = {};
240
+ formMatching.find('select').each(function(idx, select) {
241
+ var listeImportIndex = $(select).val();
242
+ if (listeImportIndex != -1) {
243
+ let key = $(select).prop('name');
244
+ tabLink[key] = listeImportIndex;
245
+ }
246
+ });
247
+ return tabLink;
248
+ }
249
+
250
+ static displayFormMatching(formMatching, importColumns, header, hasHeader) {
251
+ let options = '<option value="-1">'+selectDefaultOptionLabel+'</option>';
252
+ $.each(header, function (index, value) {
253
+ options += '<option value="'+(hasHeader?value:index)+'">' + value + '</option>';
254
+ });
255
+
256
+ let selectContent = formMatching.find('.import_matching_select_content').addClass('row').empty();
257
+ $.each(importColumns, function (key, label) {
258
+ let selectFormGroup = $(
259
+ '<div class="form-group col-md-3">' +
260
+ '<label for="form_import_'+key+'">'+label+'</label>' +
261
+ '<select class="form-control" name="'+key+'" id="form_import_'+key+'">'+options+'</select>' +
262
+ '</div>'
263
+ );
264
+ selectFormGroup.find('select option:contains('+label+')').prop('selected', true);
265
+ selectContent.append(selectFormGroup);
266
+ });
267
+
268
+ formMatching.find('div.errors').addClass('hide');
269
+ formMatching.removeClass('hide');
270
+ }
271
+
272
+ }
273
+
274
274
  module.exports = { ImportFromCsv };