@osimatic/helpers-js 1.0.80 → 1.0.83

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.
Files changed (2) hide show
  1. package/form_helper.js +94 -79
  2. package/package.json +1 -1
package/form_helper.js CHANGED
@@ -1,39 +1,57 @@
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);
11
- });
12
- }
13
- static disableSelectOption(form, selectName, optionValue) {
14
- form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
15
- }
16
- static disableSelectOptions(form, selectName, optionValues) {
17
- $.each(optionValues, function(idx, id) {
18
- FormHelper.disableSelectOption(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
+ }
19
12
  });
20
- }
21
- static countSelectOptions(form, selectName) {
22
- return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
13
+ return form;
23
14
  }
24
15
 
25
- static reset(form, submitButton) {
26
- form.find('[name]').each((idx, el) => $(el).val(''));
16
+ static reset(form) {
17
+ form.find('input[name]:not([type="checkbox"], [type="radio"]), select, textarea').each((idx, el) => $(el).val(''));
27
18
  FormHelper.buttonLoader(form.find('button'), 'reset');
28
19
  FormHelper.hideFormErrors(form);
20
+ return form;
21
+ }
29
22
 
30
- if (typeof submitButton != 'undefined' && null != submitButton) {
31
- FormHelper.buttonLoader(submitButton, 'reset');
32
- }
23
+ static populateForm(form, data) {
24
+ form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
33
25
 
34
- return form;
26
+ $.each(data, function(key, value) {
27
+ if (value == null) {
28
+ return;
29
+ }
30
+
31
+ if (typeof value == 'object') {
32
+ var select = form.find('[name="'+key+'[]"]');
33
+ select.find('option').prop('selected', false);
34
+ select.data('default_id', value.join(','));
35
+ $.each(value, function(key, val) {
36
+ select.find('option[value="'+val+'"]').prop('selected', true);
37
+ });
38
+ return;
39
+ }
40
+
41
+ var input = form.find('[name="'+key+'"]');
42
+
43
+ if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
44
+ input.prop('checked', false);
45
+ input.filter('[value="'+value+'"]').prop('checked', true);
46
+ return;
47
+ }
48
+
49
+ input.val(value);
50
+ // console.log(form.find('[name="'+key+'"]').length);
51
+ });
35
52
  }
36
53
 
54
+
37
55
  static getFormData(form) {
38
56
  // var formElement = document.getElementById("myFormElement");
39
57
  return new FormData(form[0]);
@@ -60,36 +78,57 @@ class FormHelper {
60
78
  }
61
79
 
62
80
 
63
- static populateForm(form, data) {
64
- form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
65
-
66
- $.each(data, function(key, value) {
67
- if (value == null) {
68
- return;
69
- }
70
81
 
71
- if (typeof value == 'object') {
72
- var select = form.find('[name="'+key+'[]"]');
73
- select.find('option').prop('selected', false);
74
- select.data('default_id', value.join(','));
75
- $.each(value, function(key, val) {
76
- select.find('option[value="'+val+'"]').prop('selected', true);
77
- });
78
- return;
79
- }
82
+ // ------------------------------------------------------------
83
+ // Input text
84
+ // ------------------------------------------------------------
80
85
 
81
- var input = form.find('[name="'+key+'"]');
86
+ static getInputValue(input) {
87
+ if (typeof input == 'undefined') {
88
+ return null;
89
+ }
90
+ let value = $(input).val();
91
+ if (value === null || value === '') {
92
+ return null;
93
+ }
94
+ return value;
95
+ }
82
96
 
83
- if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
84
- input.prop('checked', false);
85
- input.filter('[value="'+value+'"]').prop('checked', true);
86
- return;
87
- }
97
+ static getLinesOfTextarea(textarea) {
98
+ return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
99
+ }
88
100
 
89
- input.val(value);
90
- // console.log(form.find('[name="'+key+'"]').length);
101
+ // ------------------------------------------------------------
102
+ // Select
103
+ // ------------------------------------------------------------
104
+
105
+ static resetSelectOption(form, selectName) {
106
+ form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
107
+ }
108
+ static setSelectedSelectOption(form, selectName, optionValue) {
109
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('selected', true);
110
+ }
111
+ static setSelectedSelectOptions(form, selectName, optionValues) {
112
+ $.each(optionValues, function(idx, id) {
113
+ FormHelper.setSelectedSelectOption(form, selectName, id);
91
114
  });
92
115
  }
116
+ static disableSelectOption(form, selectName, optionValue) {
117
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
118
+ }
119
+ static disableSelectOptions(form, selectName, optionValues) {
120
+ $.each(optionValues, function(idx, id) {
121
+ FormHelper.disableSelectOption(form, selectName, id);
122
+ });
123
+ }
124
+ static countSelectOptions(form, selectName) {
125
+ return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
126
+ }
127
+
128
+
129
+ // ------------------------------------------------------------
130
+ // Checkbox
131
+ // ------------------------------------------------------------
93
132
 
94
133
  static getCheckedValues(inputs) {
95
134
  return inputs.map(function() {
@@ -111,21 +150,6 @@ class FormHelper {
111
150
  }).get().filter(word => word.length > 0);
112
151
  }
113
152
 
114
- static getInputValue(input) {
115
- if (typeof input == 'undefined') {
116
- return null;
117
- }
118
- let value = $(input).val();
119
- if (value === null || value === '') {
120
- return null;
121
- }
122
- return value;
123
- }
124
-
125
- static getLinesOfTextarea(textarea) {
126
- return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
127
- }
128
-
129
153
 
130
154
  // ------------------------------------------------------------
131
155
  // Champs
@@ -177,7 +201,7 @@ class FormHelper {
177
201
  }
178
202
 
179
203
  // ------------------------------------------------------------
180
- // Messages
204
+ // Messages erreur
181
205
  // ------------------------------------------------------------
182
206
 
183
207
  static extractErrorKeyOfJson(json, onlyIfUniqueError) {
@@ -265,6 +289,10 @@ class FormHelper {
265
289
  }
266
290
 
267
291
 
292
+ // ------------------------------------------------------------
293
+ // Bouton valider
294
+ // ------------------------------------------------------------
295
+
268
296
  static buttonLoader(button, action) {
269
297
  button = $(button);
270
298
  if (action === 'start' || action === 'loading') {
@@ -293,19 +321,6 @@ class FormHelper {
293
321
  return button;
294
322
  }
295
323
 
296
-
297
-
298
-
299
-
300
- /** @deprecated **/
301
- static logRequestFailure(status, exception) {
302
- console.log('request failure. Status: '+status+' ; Exception: '+exception);
303
- }
304
-
305
- /** @deprecated **/
306
- static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
307
- this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
308
- }
309
324
 
310
325
  }
311
326
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.80",
3
+ "version": "1.0.83",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"