@osimatic/helpers-js 1.0.710 → 1.1.0

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 CHANGED
@@ -1,26 +1,58 @@
1
1
  class FormHelper {
2
- static resetSelectOption(form, selectName) {
3
- form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
4
- }
5
- static setSelectedSelectOption(form, selectName, optionValue) {
6
- form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('selected', true);
7
- }
8
- static setSelectedSelectOptions(form, selectName, optionValues) {
9
- $.each(optionValues, function(idx, id) {
10
- FormHelper.setSelectedSelectOption(form, selectName, id);
2
+ static init(form, onSubmitCallback, submitButton) {
3
+ FormHelper.reset(form, submitButton);
4
+ submitButton = typeof submitButton != 'undefined' && null != submitButton ? submitButton : form.find('button[name="validate"]');
5
+ submitButton.off('click').click(function(e) {
6
+ e.preventDefault();
7
+ FormHelper.buttonLoader($(this), 'loading');
8
+ FormHelper.hideFormErrors(form);
9
+ if (typeof onSubmitCallback == 'function') {
10
+ onSubmitCallback(form, submitButton);
11
+ }
11
12
  });
13
+ return form;
12
14
  }
13
- static disableSelectOption(form, selectName, optionValue) {
14
- form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
15
+
16
+ static reset(form, submitButton) {
17
+ submitButton = typeof submitButton != 'undefined' && null != submitButton ? submitButton : form.find('button[name="validate"]');
18
+ form.find('input[name]:not([type="checkbox"], [type="radio"]), select:not(.selectpicker), textarea').each((idx, el) => $(el).val('')).off('change');
19
+ form.find('select.selectpicker').each((idx, el) => $(el).val(''));
20
+ FormHelper.buttonLoader(submitButton, 'reset');
21
+ FormHelper.hideFormErrors(form);
22
+ return form;
15
23
  }
16
- static disableSelectOptions(form, selectName, optionValues) {
17
- $.each(optionValues, function(idx, id) {
18
- FormHelper.disableSelectOption(form, selectName, id);
24
+
25
+ static populateForm(form, data) {
26
+ form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
27
+
28
+ $.each(data, function(key, value) {
29
+ if (value == null) {
30
+ return;
31
+ }
32
+
33
+ if (typeof value == 'object') {
34
+ var select = form.find('[name="'+key+'[]"]');
35
+ select.find('option').prop('selected', false);
36
+ select.data('default_id', value.join(','));
37
+ $.each(value, function(key, val) {
38
+ select.find('option[value="'+val+'"]').prop('selected', true);
39
+ });
40
+ return;
41
+ }
42
+
43
+ var input = form.find('[name="'+key+'"]');
44
+
45
+ if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
46
+ input.prop('checked', false);
47
+ input.filter('[value="'+value+'"]').prop('checked', true);
48
+ return;
49
+ }
50
+
51
+ input.val(value);
52
+ // console.log(form.find('[name="'+key+'"]').length);
19
53
  });
20
54
  }
21
- static countSelectOptions(form, selectName) {
22
- return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
23
- }
55
+
24
56
 
25
57
  static getFormData(form) {
26
58
  // var formElement = document.getElementById("myFormElement");
@@ -48,36 +80,57 @@ class FormHelper {
48
80
  }
49
81
 
50
82
 
51
- static populateForm(form, data) {
52
- form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
53
-
54
- $.each(data, function(key, value) {
55
- if (value == null) {
56
- return;
57
- }
58
83
 
59
- if (typeof value == 'object') {
60
- var select = form.find('[name="'+key+'[]"]');
61
- select.find('option').prop('selected', false);
62
- select.data('default_id', value.join(','));
63
- $.each(value, function(key, val) {
64
- select.find('option[value="'+val+'"]').prop('selected', true);
65
- });
66
- return;
67
- }
84
+ // ------------------------------------------------------------
85
+ // Input text
86
+ // ------------------------------------------------------------
68
87
 
69
- var input = form.find('[name="'+key+'"]');
88
+ static getInputValue(input) {
89
+ if (typeof input == 'undefined') {
90
+ return null;
91
+ }
92
+ let value = $(input).val();
93
+ if (value === null || value === '') {
94
+ return null;
95
+ }
96
+ return value;
97
+ }
70
98
 
71
- if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
72
- input.prop('checked', false);
73
- input.filter('[value="'+value+'"]').prop('checked', true);
74
- return;
75
- }
99
+ static getLinesOfTextarea(textarea) {
100
+ return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
101
+ }
76
102
 
77
- input.val(value);
78
- // console.log(form.find('[name="'+key+'"]').length);
103
+ // ------------------------------------------------------------
104
+ // Select
105
+ // ------------------------------------------------------------
106
+
107
+ static resetSelectOption(form, selectName) {
108
+ form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
109
+ }
110
+ static setSelectedSelectOption(form, selectName, optionValue) {
111
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('selected', true);
112
+ }
113
+ static setSelectedSelectOptions(form, selectName, optionValues) {
114
+ $.each(optionValues, function(idx, id) {
115
+ FormHelper.setSelectedSelectOption(form, selectName, id);
116
+ });
117
+ }
118
+ static disableSelectOption(form, selectName, optionValue) {
119
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
120
+ }
121
+ static disableSelectOptions(form, selectName, optionValues) {
122
+ $.each(optionValues, function(idx, id) {
123
+ FormHelper.disableSelectOption(form, selectName, id);
79
124
  });
80
125
  }
126
+ static countSelectOptions(form, selectName) {
127
+ return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
128
+ }
129
+
130
+
131
+ // ------------------------------------------------------------
132
+ // Checkbox
133
+ // ------------------------------------------------------------
81
134
 
82
135
  static getCheckedValues(inputs) {
83
136
  return inputs.map(function() {
@@ -99,10 +152,6 @@ class FormHelper {
99
152
  }).get().filter(word => word.length > 0);
100
153
  }
101
154
 
102
- static getLinesOfTextarea(textarea) {
103
- return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
104
- }
105
-
106
155
 
107
156
  // ------------------------------------------------------------
108
157
  // Champs
@@ -112,33 +161,35 @@ class FormHelper {
112
161
  //if ( $('[type="date"]').prop('type') != 'date' ) {
113
162
  // $('[type="date"]').datepicker();
114
163
  //}
115
- if (!Modernizr.inputtypes.date) {
116
- // $.fn.datepicker.defaults.language = 'fr';
117
- // $.datepicker.setDefaults( $.datepicker.regional["fr"]);
118
- form.find('input[type="date"]')
119
- .css('max-width', '120px')
120
- // 28/06/2021 : désactivation du datepicker car safari le gere en natif
121
- /*
122
- .datepicker({
123
- dateFormat: 'yy-mm-dd',
124
- changeMonth: true,
125
- changeYear: true,
126
- showOn: "both",
127
- buttonImage: ROOT_PATH+'images/icons/calendar-alt.png',
128
- buttonImageOnly: true,
129
- })
130
- */
131
- ;
132
- //form.find('input[type="date"]').datepicker({dateFormat: 'yy-mm-dd', minDate: "-10Y", maxDate: "+3Y"});
133
- // $("#date_conf").datepicker("option", $.datepicker.regional["fr"]);
134
- // $("#date_conf").datepicker("option", "dateFormat", "yy-mm-dd");
135
- }
136
- if (!Modernizr.inputtypes.time) {
137
- form.find('input[type="time"]')
138
- .css('max-width', '100px')
139
- .attr('placeholder', 'hh:mm')
140
- ;
141
- form.find('input[type="time"][step="1"]').attr('placeholder', 'hh:mm:ss');
164
+ if (typeof Modernizr != 'undefined') {
165
+ if (!Modernizr.inputtypes.date) {
166
+ // $.fn.datepicker.defaults.language = 'fr';
167
+ // $.datepicker.setDefaults( $.datepicker.regional["fr"]);
168
+ form.find('input[type="date"]')
169
+ .css('max-width', '120px')
170
+ // 28/06/2021 : désactivation du datepicker car safari le gere en natif
171
+ /*
172
+ .datepicker({
173
+ dateFormat: 'yy-mm-dd',
174
+ changeMonth: true,
175
+ changeYear: true,
176
+ showOn: "both",
177
+ buttonImage: ROOT_PATH+'images/icons/calendar-alt.png',
178
+ buttonImageOnly: true,
179
+ })
180
+ */
181
+ ;
182
+ //form.find('input[type="date"]').datepicker({dateFormat: 'yy-mm-dd', minDate: "-10Y", maxDate: "+3Y"});
183
+ // $("#date_conf").datepicker("option", $.datepicker.regional["fr"]);
184
+ // $("#date_conf").datepicker("option", "dateFormat", "yy-mm-dd");
185
+ }
186
+ if (!Modernizr.inputtypes.time) {
187
+ form.find('input[type="time"]')
188
+ .css('max-width', '100px')
189
+ .attr('placeholder', 'hh:mm')
190
+ ;
191
+ form.find('input[type="time"][step="1"]').attr('placeholder', 'hh:mm:ss');
192
+ }
142
193
  }
143
194
 
144
195
  // Show/Hide password
@@ -153,8 +204,12 @@ class FormHelper {
153
204
  form.find('input[type="password"]').wrap('<div class="input-group password"></div>').after(linkTogglePassword);
154
205
  }
155
206
 
207
+ static hideField(inputOrSelect) {
208
+ inputOrSelect.closest('.form-group').addClass('hide');
209
+ }
210
+
156
211
  // ------------------------------------------------------------
157
- // Messages
212
+ // Messages erreur
158
213
  // ------------------------------------------------------------
159
214
 
160
215
  static extractErrorKeyOfJson(json, onlyIfUniqueError) {
@@ -174,6 +229,10 @@ class FormHelper {
174
229
  return json[0].error;
175
230
  }
176
231
 
232
+ if (typeof json[0] != 'undefined' && Array.isArray(json[0]) && json[0].length === 2) {
233
+ return json[0][0];
234
+ }
235
+
177
236
  return null;
178
237
  }
179
238
 
@@ -200,11 +259,12 @@ class FormHelper {
200
259
  static displayFormErrors(form, btnSubmit, errors, errorWrapperDiv) {
201
260
  this.displayFormErrorsFromText(form, this.getFormErrorText(errors), errorWrapperDiv);
202
261
  if (btnSubmit != null) {
203
- if (btnSubmit.buttonLoader != null) {
204
- btnSubmit.buttonLoader('reset');
205
- } else {
206
- btnSubmit.attr('disabled', false).button('reset');
207
- }
262
+ FormHelper.buttonLoader(btnSubmit, 'reset');
263
+ //if (btnSubmit.buttonLoader != null) {
264
+ // btnSubmit.buttonLoader('reset');
265
+ //} else {
266
+ // btnSubmit.attr('disabled', false).button('reset');
267
+ //}
208
268
  }
209
269
  }
210
270
 
@@ -241,6 +301,10 @@ class FormHelper {
241
301
  }
242
302
 
243
303
 
304
+ // ------------------------------------------------------------
305
+ // Bouton valider
306
+ // ------------------------------------------------------------
307
+
244
308
  static buttonLoader(button, action) {
245
309
  button = $(button);
246
310
  if (action === 'start' || action === 'loading') {
@@ -248,7 +312,7 @@ class FormHelper {
248
312
  return self;
249
313
  }
250
314
  button.attr('disabled', true);
251
- button.attr('data-btn-text', button.html());
315
+ button.data('btn-text', button.html());
252
316
  //let text = '<span class="spinner"><i class=\'fa fa-circle-notch fa-spin\'></i></span>Traitement en cours…';
253
317
  let text = '<i class=\'fa fa-circle-notch fa-spin\'></i> Traitement en cours…';
254
318
  if (button.data('load-text') != undefined && button.data('load-text') != null && button.data('load-text') != '') {
@@ -261,7 +325,7 @@ class FormHelper {
261
325
  button.addClass('disabled');
262
326
  }
263
327
  if (action === 'stop' || action === 'reset') {
264
- button.html(button.attr('data-btn-text'));
328
+ button.html(button.data('btn-text'));
265
329
  button.removeClass('disabled');
266
330
  button.attr('disabled', false);
267
331
  //button.removeAttr("disabled");
@@ -269,20 +333,52 @@ class FormHelper {
269
333
  return button;
270
334
  }
271
335
 
272
-
273
-
274
-
275
336
 
276
- /** @deprecated **/
277
- static logRequestFailure(status, exception) {
278
- console.log('request failure. Status: '+status+' ; Exception: '+exception);
279
- }
337
+ }
280
338
 
281
- /** @deprecated **/
282
- static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
283
- this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
284
- }
339
+ class EditValue {
340
+ static init(valueDiv, onSubmitCallback, getInputCallback) {
341
+ let link = $('<a href="#" class="text-warning"><i class="fas fa-pencil-alt"></i></a>');
342
+ valueDiv.parent().append('&nbsp;').append(link);
343
+
344
+ function cancelEdit(valueParentDiv) {
345
+ valueParentDiv.find('a, span').removeClass('hide');
346
+ valueParentDiv.find('form').remove();
347
+ }
285
348
 
349
+ link.click(function (e) {
350
+ e.preventDefault();
351
+
352
+ let parent = $(this).parent();
353
+
354
+ parent.find('a, span').addClass('hide');
355
+
356
+ let form = $('<form class="form-inline"></form>');
357
+
358
+ let value = parent.find('span').data('value') || parent.find('span').text();
359
+ let input = $( typeof getInputCallback == 'function' ? getInputCallback(value) : '<input type="text" />');
360
+ form.append(input);
361
+ form.find('input').addClass('form-control').css('width', 'auto').val(value);
362
+
363
+ let button = $('<button type="submit" class="btn btn-success ms-2" data-loading-text="<i class=\'fa fa-circle-notch fa-spin\'></i>" style="vertical-align: baseline;"><i class="fas fa-check"></i></button>');
364
+ button.click(function (e) {
365
+ FormHelper.buttonLoader(parent.find('button'), 'loading');
366
+ let newValue = parent.find('input').val();
367
+ onSubmitCallback(newValue, parent,
368
+ (isSuccess, valueFormatCallback) => {
369
+ cancelEdit(parent);
370
+ if (isSuccess) {
371
+ parent.find('span').data('value', newValue).text(typeof valueFormatCallback == 'function' ? valueFormatCallback(newValue) : newValue);
372
+ }
373
+ }
374
+ );
375
+ });
376
+ form.append(button);
377
+
378
+ parent.append(form);
379
+ return false;
380
+ });
381
+ }
286
382
  }
287
383
 
288
- module.exports = { FormHelper };
384
+ module.exports = { FormHelper, EditValue };
package/google_charts.js CHANGED
@@ -13,7 +13,7 @@ class GoogleCharts {
13
13
  // 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é)
14
14
  chartsList = chartsList.filter(chartData => typeof chartData.div_id != 'undefined' && $('#'+chartData.div_id).length);
15
15
 
16
- var nbChartsCompleted = 0;
16
+ let nbChartsCompleted = 0;
17
17
  chartsList.forEach(chartData => {
18
18
  //console.log(chartData);
19
19
  GoogleCharts.draw(
@@ -41,17 +41,18 @@ class GoogleCharts {
41
41
 
42
42
  static draw(div, typeGraph, titre, libelleAbs, tabDataAbsParam, listeLibelleOrd, listeTabDataOrd, tabColor, formatData, height, width, onComplete) {
43
43
  if (typeof div == 'undefined' || !div.length) {
44
+ console.error('div not found');
44
45
  return;
45
46
  }
46
47
 
47
48
  height = height || null;
48
49
  width = width || null;
49
50
 
50
- var htmlDomDiv = div[0];
51
+ let htmlDomDiv = div[0];
51
52
 
52
- var afficherLibelleOrd = false;
53
+ let afficherLibelleOrd = false;
53
54
 
54
- var isStacked = false;
55
+ let isStacked = false;
55
56
  if (typeGraph === 'stacked_bar_chart') {
56
57
  typeGraph = 'bar_chart';
57
58
  isStacked = true;
@@ -65,7 +66,7 @@ class GoogleCharts {
65
66
  isStacked = true;
66
67
  }
67
68
 
68
- var isDualChart = false;
69
+ let isDualChart = false;
69
70
  if (typeGraph === 'dual_column_chart') {
70
71
  typeGraph = 'column_chart';
71
72
  isDualChart = true;
@@ -75,7 +76,7 @@ class GoogleCharts {
75
76
  isDualChart = true;
76
77
  }
77
78
 
78
- var data = null;
79
+ let data = null;
79
80
  if (typeGraph === 'pie_chart') {
80
81
  //data = google.visualization.arrayToDataTable(tabDataAbsParam);
81
82
 
@@ -83,7 +84,7 @@ class GoogleCharts {
83
84
  data.addColumn('string', libelleAbs);
84
85
  data.addColumn('number', '');
85
86
 
86
- var numRow = 0;
87
+ let numRow = 0;
87
88
  $.each(tabDataAbsParam, function(idx, value) {
88
89
  data.addRows(1);
89
90
  data.setCell(numRow, 0, idx);
@@ -100,8 +101,8 @@ class GoogleCharts {
100
101
  });
101
102
 
102
103
  // Remplissage des données
103
- var nbCells = 0;
104
- var numRow = 0;
104
+ let nbCells = 0;
105
+ let numRow = 0;
105
106
  $.each(tabDataAbsParam, function(idx, dataAbs) {
106
107
  // dataOrd = tabDataOrd[idx];
107
108
  // data.addRow([dataAbs, dataOrd]);
@@ -109,7 +110,7 @@ class GoogleCharts {
109
110
 
110
111
  data.setCell(numRow, 0, dataAbs);
111
112
 
112
- var numCell = 1;
113
+ let numCell = 1;
113
114
  $.each(listeTabDataOrd, function(idx2, tabDataOrd) {
114
115
  data.setCell(numRow, numCell, tabDataOrd[idx]);
115
116
  //data.setCell(numRow, numCell, Math.round(tabDataOrd[idx], 2));
@@ -126,7 +127,7 @@ class GoogleCharts {
126
127
  // console.log('drawGraph : '+div+' ; type : '+typeGraph);
127
128
 
128
129
  // Options générales
129
- var options = {
130
+ let options = {
130
131
  colors: tabColor,
131
132
  fontName: 'Trebuchet MS',
132
133
  fontSize: 12,
@@ -221,7 +222,7 @@ class GoogleCharts {
221
222
  // console.log(idx);
222
223
  if (idx <= 1) {
223
224
  // key = 'series_'+idx;
224
- var key = idx;
225
+ let key = idx;
225
226
  // options.series[idx] = {axis: key, targetAxisIndex: key};
226
227
  options.series[idx] = {axis: key, targetAxisIndex: idx};
227
228
  if (typeGraph === 'column_chart') {
@@ -278,7 +279,7 @@ class GoogleCharts {
278
279
  // console.log(options);
279
280
 
280
281
  // Création du graphique
281
- var chart = null;
282
+ let chart = null;
282
283
  if (typeGraph === 'column_chart') {
283
284
  // chart = new google.visualization.ColumnChart(htmlDomDiv);
284
285
  chart = new google.charts.Bar(htmlDomDiv);
@@ -301,7 +302,7 @@ class GoogleCharts {
301
302
  div.removeClass('loading');
302
303
 
303
304
  if (chart === null) {
304
- console.log('erreur graphique');
305
+ console.error('error during creating chart');
305
306
  div.addClass('graphique_error');
306
307
  htmlDomDiv.innerHTML = 'Une erreur s\'est produite lors du chargement du graphique.';
307
308
  return;
@@ -317,7 +318,7 @@ class GoogleCharts {
317
318
  tabPaneDiv = div.closest('.tab-pane');
318
319
  }
319
320
 
320
- var hasClassActive = tabPaneDiv.hasClass('active');
321
+ let hasClassActive = tabPaneDiv.hasClass('active');
321
322
  if (!hasClassActive) {
322
323
  tabPaneDiv.addClass('active');
323
324
  }