@osimatic/helpers-js 1.0.79 → 1.0.82

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 -73
  2. package/package.json +1 -1
package/form_helper.js CHANGED
@@ -1,33 +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
16
  static reset(form) {
26
17
  form.find('[name]').each((idx, el) => $(el).val(''));
18
+ FormHelper.buttonLoader(form.find('button'), 'reset');
27
19
  FormHelper.hideFormErrors(form);
28
20
  return form;
29
21
  }
30
22
 
23
+ static populateForm(form, data) {
24
+ form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
25
+
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
+ });
52
+ }
53
+
54
+
31
55
  static getFormData(form) {
32
56
  // var formElement = document.getElementById("myFormElement");
33
57
  return new FormData(form[0]);
@@ -54,36 +78,57 @@ class FormHelper {
54
78
  }
55
79
 
56
80
 
57
- static populateForm(form, data) {
58
- form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
59
-
60
- $.each(data, function(key, value) {
61
- if (value == null) {
62
- return;
63
- }
64
81
 
65
- if (typeof value == 'object') {
66
- var select = form.find('[name="'+key+'[]"]');
67
- select.find('option').prop('selected', false);
68
- select.data('default_id', value.join(','));
69
- $.each(value, function(key, val) {
70
- select.find('option[value="'+val+'"]').prop('selected', true);
71
- });
72
- return;
73
- }
82
+ // ------------------------------------------------------------
83
+ // Input text
84
+ // ------------------------------------------------------------
74
85
 
75
- 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
+ }
76
96
 
77
- if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
78
- input.prop('checked', false);
79
- input.filter('[value="'+value+'"]').prop('checked', true);
80
- return;
81
- }
97
+ static getLinesOfTextarea(textarea) {
98
+ return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
99
+ }
82
100
 
83
- input.val(value);
84
- // 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);
114
+ });
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);
85
122
  });
86
123
  }
124
+ static countSelectOptions(form, selectName) {
125
+ return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
126
+ }
127
+
128
+
129
+ // ------------------------------------------------------------
130
+ // Checkbox
131
+ // ------------------------------------------------------------
87
132
 
88
133
  static getCheckedValues(inputs) {
89
134
  return inputs.map(function() {
@@ -105,21 +150,6 @@ class FormHelper {
105
150
  }).get().filter(word => word.length > 0);
106
151
  }
107
152
 
108
- static getInputValue(input) {
109
- if (typeof input == 'undefined') {
110
- return null;
111
- }
112
- let value = $(input).val();
113
- if (value === null || value === '') {
114
- return null;
115
- }
116
- return value;
117
- }
118
-
119
- static getLinesOfTextarea(textarea) {
120
- return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
121
- }
122
-
123
153
 
124
154
  // ------------------------------------------------------------
125
155
  // Champs
@@ -171,7 +201,7 @@ class FormHelper {
171
201
  }
172
202
 
173
203
  // ------------------------------------------------------------
174
- // Messages
204
+ // Messages erreur
175
205
  // ------------------------------------------------------------
176
206
 
177
207
  static extractErrorKeyOfJson(json, onlyIfUniqueError) {
@@ -259,6 +289,10 @@ class FormHelper {
259
289
  }
260
290
 
261
291
 
292
+ // ------------------------------------------------------------
293
+ // Bouton valider
294
+ // ------------------------------------------------------------
295
+
262
296
  static buttonLoader(button, action) {
263
297
  button = $(button);
264
298
  if (action === 'start' || action === 'loading') {
@@ -287,19 +321,6 @@ class FormHelper {
287
321
  return button;
288
322
  }
289
323
 
290
-
291
-
292
-
293
-
294
- /** @deprecated **/
295
- static logRequestFailure(status, exception) {
296
- console.log('request failure. Status: '+status+' ; Exception: '+exception);
297
- }
298
-
299
- /** @deprecated **/
300
- static displayFormErrorsFromXhr(form, btnSubmit, xhr) {
301
- this.displayFormErrors(form, btnSubmit, xhr.responseJSON);
302
- }
303
324
 
304
325
  }
305
326
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@osimatic/helpers-js",
3
- "version": "1.0.79",
3
+ "version": "1.0.82",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"