@osimatic/helpers-js 1.0.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/form_helper.js ADDED
@@ -0,0 +1,237 @@
1
+ class FormHelper {
2
+ static logRequestFailure(status, exception) {
3
+ console.log('request failure. Status: '+status+' ; Exception: '+exception);
4
+ }
5
+
6
+ static resetSelectOption(form, selectName) {
7
+ form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
8
+ }
9
+ static setSelectedSelectOption(form, selectName, optionValue) {
10
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('selected', true);
11
+ }
12
+ static setSelectedSelectOptions(form, selectName, optionValues) {
13
+ $.each(optionValues, function(idx, id) {
14
+ FormHelper.setSelectedSelectOption(form, selectName, id);
15
+ });
16
+ }
17
+ static disableSelectOption(form, selectName, optionValue) {
18
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
19
+ }
20
+ static disableSelectOptions(form, selectName, optionValues) {
21
+ $.each(optionValues, function(idx, id) {
22
+ FormHelper.disableSelectOption(form, selectName, id);
23
+ });
24
+ }
25
+ static countSelectOptions(form, selectName) {
26
+ return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
27
+ }
28
+
29
+ static getFormData(form) {
30
+ // var formElement = document.getElementById("myFormElement");
31
+ return new FormData(form[0]);
32
+ }
33
+
34
+ static getDataFromFormData(formData) {
35
+ var data = {};
36
+ for(let pair of formData.entries()) {
37
+ //console.log(pair[0]+ ', '+ pair[1]);
38
+ data[pair[0]] = pair[1];
39
+ }
40
+ return data;
41
+ }
42
+
43
+ static getFormDataQueryString(form) {
44
+ return form.serialize();
45
+ // cette soluce marche pas pour les clés sous forme de tableau : ids[]=1&ids[]=2
46
+ /*
47
+ return form.serializeArray().reduce(function(obj, item) {
48
+ obj[item.name] = item.value;
49
+ return obj;
50
+ }, {});
51
+ */
52
+ }
53
+
54
+
55
+ static populateForm(form, data) {
56
+ form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
57
+
58
+ $.each(data, function(key, value) {
59
+ if (value == null) {
60
+ return;
61
+ }
62
+
63
+ if (typeof value == 'object') {
64
+ var select = form.find('[name="'+key+'[]"]');
65
+ select.find('option').prop('selected', false);
66
+ select.data('default_id', value.join(','));
67
+ $.each(value, function(key, val) {
68
+ select.find('option[value="'+val+'"]').prop('selected', true);
69
+ });
70
+ return;
71
+ }
72
+
73
+ var input = form.find('[name="'+key+'"]');
74
+
75
+ if (input.prop('type') == 'radio' || input.prop('type') == 'checkbox') {
76
+ input.prop('checked', false);
77
+ input.filter('[value="'+value+'"]').prop('checked', true);
78
+ return;
79
+ }
80
+
81
+ input.val(value);
82
+ // console.log(form.find('[name="'+key+'"]').length);
83
+ });
84
+ }
85
+
86
+ static getCheckedValues(inputs) {
87
+ return inputs.map(function() {
88
+ if (this.checked) {
89
+ return this.value;
90
+ }
91
+ }).get();
92
+ }
93
+
94
+ static setCheckedValues(inputs, defaultValues) {
95
+ $.each(defaultValues, function(idx, value) {
96
+ inputs.parent().find('[value="'+value+'"]').prop('checked', true);
97
+ });
98
+ }
99
+
100
+ static getInputListValues(inputs) {
101
+ return inputs.map(function() {
102
+ return this.value;
103
+ }).get().filter(word => word.length > 0);
104
+ }
105
+
106
+ static getLinesOfTextarea(textarea) {
107
+ return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
108
+ }
109
+
110
+
111
+ // ------------------------------------------------------------
112
+ // Champs
113
+ // ------------------------------------------------------------
114
+
115
+ static initTypeFields(form) {
116
+ //if ( $('[type="date"]').prop('type') != 'date' ) {
117
+ // $('[type="date"]').datepicker();
118
+ //}
119
+ if (!Modernizr.inputtypes.date) {
120
+ // $.fn.datepicker.defaults.language = 'fr';
121
+ // $.datepicker.setDefaults( $.datepicker.regional["fr"]);
122
+ form.find('input[type="date"]')
123
+ .css('max-width', '120px')
124
+ // 28/06/2021 : désactivation du datepicker car safari le gere en natif
125
+ /*
126
+ .datepicker({
127
+ dateFormat: 'yy-mm-dd',
128
+ changeMonth: true,
129
+ changeYear: true,
130
+ showOn: "both",
131
+ buttonImage: ROOT_PATH+'images/icons/calendar-alt.png',
132
+ buttonImageOnly: true,
133
+ })
134
+ */
135
+ ;
136
+ //form.find('input[type="date"]').datepicker({dateFormat: 'yy-mm-dd', minDate: "-10Y", maxDate: "+3Y"});
137
+ // $("#date_conf").datepicker("option", $.datepicker.regional["fr"]);
138
+ // $("#date_conf").datepicker("option", "dateFormat", "yy-mm-dd");
139
+ }
140
+ if (!Modernizr.inputtypes.time) {
141
+ form.find('input[type="time"]')
142
+ .css('max-width', '100px')
143
+ .attr('placeholder', 'hh:mm')
144
+ ;
145
+ form.find('input[type="time"][step="1"]').attr('placeholder', 'hh:mm:ss');
146
+ }
147
+
148
+ // Show/Hide password
149
+ let linkTogglePassword = $('<span class="input-group-text"><i class="fas fa-eye toggle_password"></i></span>');
150
+ linkTogglePassword.find('i').click(function(e) {
151
+ e.preventDefault();
152
+ let input = $(this).closest('.input-group').find('input');
153
+ let passwordHidden = input.attr('type') === 'password';
154
+ input.attr('type', passwordHidden ? 'text' : 'password');
155
+ $(this).removeClass('fa-eye fa-eye-slash').addClass(passwordHidden ? 'fa-eye-slash' : 'fa-eye');
156
+ });
157
+ form.find('input[type="password"]').wrap('<div class="input-group password"></div>').after(linkTogglePassword);
158
+ }
159
+
160
+ // ------------------------------------------------------------
161
+ // Messages
162
+ // ------------------------------------------------------------
163
+
164
+ static hideFormErrors(form) {
165
+ form.find('div.form_errors').remove();
166
+ return form;
167
+ }
168
+
169
+ static getFormErrorText(errors) {
170
+ var errorLabels = '';
171
+ for (var property in errors) {
172
+ if (typeof errors[property] != 'function') {
173
+ errorLabels += '<span>' + errors[property] + '</span><br>';
174
+ }
175
+ }
176
+ return errorLabels;
177
+ }
178
+
179
+ static getFormErrorTextBis(errors) {
180
+ var errorLabels = '';
181
+ for (var property in errors) {
182
+ // console.log(property);
183
+ if (typeof errors[property] != 'function') {
184
+ if (typeof errors[property]['error_description'] === 'undefined') {
185
+ errorLabels += '<span>' + errors[property] + '</span><br>';
186
+ } else {
187
+ errorLabels += '<span>' + errors[property]['error_description'] + '</span><br>';
188
+ }
189
+ }
190
+ }
191
+ return errorLabels;
192
+ }
193
+
194
+ static displayFormErrors(form, btnSubmit, errors) {
195
+ this.displayFormErrorsFromText(form, this.getFormErrorTextBis(errors));
196
+ if (btnSubmit != null) {
197
+ if (btnSubmit.buttonLoader != null) {
198
+ btnSubmit.buttonLoader('reset');
199
+ } else {
200
+ btnSubmit.attr('disabled', false).button('reset');
201
+ }
202
+ }
203
+ }
204
+
205
+ static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
206
+ this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
207
+ }
208
+
209
+ static displayFormErrorsFromText(form, errorLabels) {
210
+ var errorDiv = '<div class="alert alert-danger form_errors">'+errorLabels+'</div>';
211
+ var errorsParentDiv = form;
212
+
213
+ if (form.find('.form_errors_content').length) {
214
+ form.find('.form_errors_content').append(errorDiv);
215
+ return;
216
+ }
217
+
218
+ if (form.find('.modal-body').length) {
219
+ errorsParentDiv = form.find('.modal-body');
220
+ }
221
+
222
+ var firstFormGroup = errorsParentDiv.find('.form-group:first');
223
+ if (firstFormGroup.length) {
224
+ if (firstFormGroup.parent().parent().hasClass('row')) {
225
+ firstFormGroup.parent().parent().before(errorDiv);
226
+ }
227
+ else {
228
+ errorsParentDiv.find('.form-group:first').before(errorDiv);
229
+ }
230
+ return;
231
+ }
232
+
233
+ errorsParentDiv.prepend(errorDiv);
234
+ }
235
+ }
236
+
237
+ exports.FormHelper = FormHelper;
@@ -0,0 +1,344 @@
1
+
2
+ // google.load("visualization", "1", {packages:["corechart"]});
3
+ google.charts.load('current', {'packages':['bar','line','corechart']});
4
+ // google.charts.load('current', {packages:['corechart']});
5
+
6
+ google.charts.setOnLoadCallback(drawAllCharts);
7
+
8
+ class GoogleCharts
9
+ {
10
+ static drawCharts(chartsList, onComplete) {
11
+ // on supprime du tableau la liste des graphiques dont l'id div n'a pas été trouvé (le graphique ne pourra pas être généré)
12
+ chartsList = chartsList.filter(chartData => typeof chartData.div_id != 'undefined' && $('#'+chartData.div_id).length);
13
+
14
+ var nbChartsCompleted = 0;
15
+ chartsList.forEach(chartData => {
16
+ //console.log(chartData);
17
+ GoogleCharts.draw(
18
+ $('#'+chartData.div_id),
19
+ chartData.chart_type,
20
+ chartData.title,
21
+ chartData.abscissa_label,
22
+ chartData.abscissa_data,
23
+ chartData.ordinate_label,
24
+ chartData.ordinate_data,
25
+ chartData.colors,
26
+ chartData.ordinate_format,
27
+ chartData.height,
28
+ chartData.width,
29
+ chart => {
30
+ nbChartsCompleted++;
31
+ // si tous les graphiques ont été chargés, on appelle le callback onComplete transmis en paramètre
32
+ if (chartsList.length === nbChartsCompleted && typeof onComplete == 'function') {
33
+ onComplete();
34
+ }
35
+ },
36
+ );
37
+ });
38
+ }
39
+
40
+ static draw(div, typeGraph, titre, libelleAbs, tabDataAbsParam, listeLibelleOrd, listeTabDataOrd, tabColor, formatData, height, width, onComplete) {
41
+ if (typeof div == 'undefined' || !div.length) {
42
+ return;
43
+ }
44
+
45
+ height = height || null;
46
+ width = width || null;
47
+
48
+ var htmlDomDiv = div[0];
49
+
50
+ var afficherLibelleOrd = false;
51
+
52
+ var isStacked = false;
53
+ if (typeGraph === 'stacked_bar_chart') {
54
+ typeGraph = 'bar_chart';
55
+ isStacked = true;
56
+ }
57
+ if (typeGraph === 'stacked_column_chart') {
58
+ typeGraph = 'column_chart';
59
+ isStacked = true;
60
+ }
61
+ if (typeGraph === 'stacked_combo_chart') {
62
+ typeGraph = 'combo_chart';
63
+ isStacked = true;
64
+ }
65
+
66
+ var isDualChart = false;
67
+ if (typeGraph === 'dual_column_chart') {
68
+ typeGraph = 'column_chart';
69
+ isDualChart = true;
70
+ }
71
+ if (typeGraph === 'dual_bar_chart') {
72
+ typeGraph = 'bar_chart';
73
+ isDualChart = true;
74
+ }
75
+
76
+ var data = null;
77
+ if (typeGraph === 'pie_chart') {
78
+ //data = google.visualization.arrayToDataTable(tabDataAbsParam);
79
+
80
+ data = new google.visualization.DataTable();
81
+ data.addColumn('string', libelleAbs);
82
+ data.addColumn('number', '');
83
+
84
+ var numRow = 0;
85
+ $.each(tabDataAbsParam, function(idx, value) {
86
+ data.addRows(1);
87
+ data.setCell(numRow, 0, idx);
88
+ data.setCell(numRow, 1, value);
89
+ numRow++;
90
+ });
91
+ }
92
+ else {
93
+ // Déclaration du tableau de données
94
+ data = new google.visualization.DataTable();
95
+ data.addColumn('string', libelleAbs);
96
+ $.each(listeLibelleOrd, function(idx, libelleOrd) {
97
+ data.addColumn('number', libelleOrd);
98
+ });
99
+
100
+ // Remplissage des données
101
+ var nbCells = 0;
102
+ var numRow = 0;
103
+ $.each(tabDataAbsParam, function(idx, dataAbs) {
104
+ // dataOrd = tabDataOrd[idx];
105
+ // data.addRow([dataAbs, dataOrd]);
106
+ data.addRows(1);
107
+
108
+ data.setCell(numRow, 0, dataAbs);
109
+
110
+ var numCell = 1;
111
+ $.each(listeTabDataOrd, function(idx2, tabDataOrd) {
112
+ data.setCell(numRow, numCell, tabDataOrd[idx]);
113
+ //data.setCell(numRow, numCell, Math.round(tabDataOrd[idx], 2));
114
+ numCell++;
115
+ });
116
+
117
+ nbCells = numCell;
118
+ numRow++;
119
+ });
120
+ nbCells -= 2;
121
+ }
122
+
123
+ // console.log(data);
124
+ // console.log('drawGraph : '+div+' ; type : '+typeGraph);
125
+
126
+ // Options générales
127
+ var options = {
128
+ colors: tabColor,
129
+ fontName: 'Trebuchet MS',
130
+ fontSize: 12,
131
+ hAxis: {maxAlternation: 1},
132
+ vAxis: {minValue: 0, textPosition: 'out'},
133
+ //gridlines: {color: '#333', count: 1}
134
+ };
135
+
136
+ if (formatData != null) {
137
+ options.vAxis.format = formatData;
138
+ }
139
+
140
+ // Options sur le titre du graphique
141
+ options.title = titre;
142
+ if (typeGraph === 'pie_chart') {
143
+ // options.titlePosition = 'none';
144
+ }
145
+ else {
146
+ options.titlePosition = 'none';
147
+ }
148
+
149
+ // Options sur la taille du graphique
150
+ if (typeGraph === 'bar_chart') {
151
+ options.chartArea = {left:120, top:30};
152
+ //options.chartArea = {left:"auto", top:"auto"};
153
+ //options.chartArea = {};
154
+ if (height != null) {
155
+ options.chartArea.height = (height - 60);
156
+ //options.chartArea.height = '100%';
157
+ }
158
+ options.chartArea.width = "85%";
159
+ //options.chartArea.width = "100%";
160
+ }
161
+ else {
162
+ options.chartArea = {left:"auto", top:"auto"};
163
+ if (height != null) {
164
+ options.chartArea.height = height+"%";
165
+ }
166
+ else {
167
+ options.chartArea.height = "80%";
168
+ }
169
+ options.chartArea.width = "85%";
170
+ }
171
+ // options.chartArea = {};
172
+ // options.chartArea.height = "100%";
173
+ // options.chartArea.width = "100%";
174
+
175
+ //options.width = width;
176
+ //options.width = "100%";
177
+ if (typeof height != 'undefined' && height != null) {
178
+ options.height = height;
179
+ }
180
+ //console.log(div);
181
+ //console.log(div.width());
182
+ //options.width = div.width();
183
+
184
+ // Options sur la légende
185
+ options.legend = {};
186
+ if (typeGraph === 'pie_chart') {
187
+ options.legend.position = 'right';
188
+ }
189
+ else if (typeGraph === 'bar_chart') {
190
+ options.legend.position = 'bottom';
191
+ }
192
+ else {
193
+ options.legend.position = 'top';
194
+ }
195
+
196
+ // Options sur l'affichage des labels en absisse / ordonnée
197
+ if (typeGraph === 'bar_chart') {
198
+ // options.hAxis.title = libelleOrd;
199
+ options.vAxis.title = libelleAbs;
200
+ }
201
+ else {
202
+ options.hAxis.title = libelleAbs;
203
+ // options.vAxis.title = libelleOrd;
204
+ }
205
+
206
+ // Options sur les graphiques "dual bar chart / dual column chart"
207
+ if (isDualChart) {
208
+ options.series = {};
209
+ options.axes = {};
210
+ if (typeGraph === 'column_chart') {
211
+ options.axes.y = {};
212
+ options.vAxes = {};
213
+ }
214
+ else {
215
+ options.axes.x = {};
216
+ options.hAxes = {};
217
+ }
218
+ $.each(listeLibelleOrd, function(idx, libelleOrd) {
219
+ // console.log(idx);
220
+ if (idx <= 1) {
221
+ // key = 'series_'+idx;
222
+ var key = idx;
223
+ // options.series[idx] = {axis: key, targetAxisIndex: key};
224
+ options.series[idx] = {axis: key, targetAxisIndex: idx};
225
+ if (typeGraph === 'column_chart') {
226
+ options.axes.y[key] = {label: libelleOrd};
227
+ if (idx === 1) {
228
+ options.axes.y[key].side = 'right';
229
+ }
230
+ if (formatData != null) {
231
+ options.vAxes[key] = {format: formatData};
232
+ }
233
+ }
234
+ else {
235
+ options.axes.x[key] = {label: libelleOrd};
236
+ if (idx === 1) {
237
+ options.axes.x[key].side = 'top';
238
+ }
239
+ if (formatData != null) {
240
+ options.hAxes[key] = {format: formatData};
241
+ }
242
+ }
243
+ }
244
+ });
245
+ // console.log(options.series);
246
+ // console.log(options.vAxes);
247
+ }
248
+
249
+ // Options sur les graphiques "combo chart"
250
+ if (typeGraph === 'combo_chart') {
251
+ options.seriesType = "bars";
252
+ options.series = {};
253
+ options.series[nbCells] = {type: "line"};
254
+ }
255
+
256
+ // Options sur le style des lignes pour les "line chart"
257
+ if (typeGraph === 'line_chart') {
258
+ options.series = [{lineWidth: 3}, {lineWidth: 1.5}];
259
+ options.curveType = 'function';
260
+ }
261
+
262
+ // Options sur le style pour les "pie chart"
263
+ if (typeGraph === 'pie_chart') {
264
+ options.is3D = false;
265
+ options.pieResidueSliceLabel = 'Autre';
266
+ }
267
+
268
+ if (typeGraph === 'bar_chart') {
269
+ options.bars = 'horizontal';
270
+ }
271
+
272
+ if (isStacked) {
273
+ options.isStacked = true;
274
+ }
275
+
276
+ // console.log(options);
277
+
278
+ // Création du graphique
279
+ var chart = null;
280
+ if (typeGraph === 'column_chart') {
281
+ // chart = new google.visualization.ColumnChart(htmlDomDiv);
282
+ chart = new google.charts.Bar(htmlDomDiv);
283
+ }
284
+ else if (typeGraph === 'bar_chart') {
285
+ // chart = new google.visualization.BarChart(htmlDomDiv);
286
+ chart = new google.charts.Bar(htmlDomDiv);
287
+ }
288
+ else if (typeGraph === 'line_chart') {
289
+ // chart = new google.visualization.LineChart(htmlDomDiv);
290
+ chart = new google.charts.Line(htmlDomDiv);
291
+ }
292
+ else if (typeGraph === 'combo_chart') {
293
+ chart = new google.visualization.ComboChart(htmlDomDiv);
294
+ }
295
+ else if (typeGraph === 'pie_chart') {
296
+ chart = new google.visualization.PieChart(htmlDomDiv);
297
+ }
298
+
299
+ div.removeClass('loading');
300
+
301
+ if (chart === null) {
302
+ console.log('erreur graphique');
303
+ div.addClass('graphique_error');
304
+ htmlDomDiv.innerHTML = 'Une erreur s\'est produite lors du chargement du graphique.';
305
+ return;
306
+ }
307
+
308
+ //div.addClass('graphique');
309
+ div.addClass('chart');
310
+ htmlDomDiv.innerHTML = '';
311
+
312
+ // htmlDomDiv.style.display = 'block';
313
+ let tabPaneDiv = div;
314
+ if (!div.hasClass('tab-pane')) {
315
+ tabPaneDiv = div.closest('.tab-pane');
316
+ }
317
+
318
+ var hasClassActive = tabPaneDiv.hasClass('active');
319
+ if (!hasClassActive) {
320
+ tabPaneDiv.addClass('active');
321
+ }
322
+
323
+ google.visualization.events.addListener(chart, 'ready', function () {
324
+ //console.log('ready');
325
+ //console.log(chart);
326
+ // htmlDomDiv.style.display = 'none';
327
+ // div.hide();
328
+ if (!hasClassActive) {
329
+ tabPaneDiv.removeClass('active');
330
+ }
331
+
332
+ if (typeof onComplete == 'function') {
333
+ onComplete(chart);
334
+ }
335
+ });
336
+
337
+ // console.log($("ul li.ui-state-active").index()
338
+ //console.log(options);
339
+
340
+ chart.draw(data, options);
341
+ }
342
+
343
+ }
344
+