@osimatic/helpers-js 1.0.80 → 1.0.81

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 +90 -78
  2. package/package.json +1 -1
package/form_helper.js CHANGED
@@ -1,39 +1,54 @@
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
+ onSubmitCallback(form, submitButton);
19
10
  });
20
11
  }
21
- static countSelectOptions(form, selectName) {
22
- return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
23
- }
24
12
 
25
- static reset(form, submitButton) {
13
+ static reset(form) {
26
14
  form.find('[name]').each((idx, el) => $(el).val(''));
27
15
  FormHelper.buttonLoader(form.find('button'), 'reset');
28
16
  FormHelper.hideFormErrors(form);
17
+ return form;
18
+ }
29
19
 
30
- if (typeof submitButton != 'undefined' && null != submitButton) {
31
- FormHelper.buttonLoader(submitButton, 'reset');
32
- }
20
+ static populateForm(form, data) {
21
+ form.find('[name="employees_display_type"][value="NONE"]').prop('checked', true); //todo à retirer
33
22
 
34
- return form;
23
+ $.each(data, function(key, value) {
24
+ if (value == null) {
25
+ return;
26
+ }
27
+
28
+ if (typeof value == 'object') {
29
+ var select = form.find('[name="'+key+'[]"]');
30
+ select.find('option').prop('selected', false);
31
+ select.data('default_id', value.join(','));
32
+ $.each(value, function(key, val) {
33
+ select.find('option[value="'+val+'"]').prop('selected', true);
34
+ });
35
+ return;
36
+ }
37
+
38
+ var input = form.find('[name="'+key+'"]');
39
+
40
+ if (input.prop('type') === 'radio' || input.prop('type') === 'checkbox') {
41
+ input.prop('checked', false);
42
+ input.filter('[value="'+value+'"]').prop('checked', true);
43
+ return;
44
+ }
45
+
46
+ input.val(value);
47
+ // console.log(form.find('[name="'+key+'"]').length);
48
+ });
35
49
  }
36
50
 
51
+
37
52
  static getFormData(form) {
38
53
  // var formElement = document.getElementById("myFormElement");
39
54
  return new FormData(form[0]);
@@ -60,36 +75,57 @@ class FormHelper {
60
75
  }
61
76
 
62
77
 
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
78
 
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
- }
79
+ // ------------------------------------------------------------
80
+ // Input text
81
+ // ------------------------------------------------------------
80
82
 
81
- var input = form.find('[name="'+key+'"]');
83
+ static getInputValue(input) {
84
+ if (typeof input == 'undefined') {
85
+ return null;
86
+ }
87
+ let value = $(input).val();
88
+ if (value === null || value === '') {
89
+ return null;
90
+ }
91
+ return value;
92
+ }
82
93
 
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
- }
94
+ static getLinesOfTextarea(textarea) {
95
+ return textarea.val().replace(/(\r\n|\n|\r)/g, "\n").split("\n").filter(word => word.length > 0);
96
+ }
88
97
 
89
- input.val(value);
90
- // console.log(form.find('[name="'+key+'"]').length);
98
+ // ------------------------------------------------------------
99
+ // Select
100
+ // ------------------------------------------------------------
101
+
102
+ static resetSelectOption(form, selectName) {
103
+ form.find('select[name="'+selectName+'"] option').prop('disabled', false).prop('selected', false);
104
+ }
105
+ static setSelectedSelectOption(form, selectName, optionValue) {
106
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('selected', true);
107
+ }
108
+ static setSelectedSelectOptions(form, selectName, optionValues) {
109
+ $.each(optionValues, function(idx, id) {
110
+ FormHelper.setSelectedSelectOption(form, selectName, id);
111
+ });
112
+ }
113
+ static disableSelectOption(form, selectName, optionValue) {
114
+ form.find('select[name="'+selectName+'"] option[value="'+optionValue+'"]').prop('disabled', true);
115
+ }
116
+ static disableSelectOptions(form, selectName, optionValues) {
117
+ $.each(optionValues, function(idx, id) {
118
+ FormHelper.disableSelectOption(form, selectName, id);
91
119
  });
92
120
  }
121
+ static countSelectOptions(form, selectName) {
122
+ return form.find('select[name="'+selectName+'"] option:not([disabled])').length;
123
+ }
124
+
125
+
126
+ // ------------------------------------------------------------
127
+ // Checkbox
128
+ // ------------------------------------------------------------
93
129
 
94
130
  static getCheckedValues(inputs) {
95
131
  return inputs.map(function() {
@@ -111,21 +147,6 @@ class FormHelper {
111
147
  }).get().filter(word => word.length > 0);
112
148
  }
113
149
 
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
150
 
130
151
  // ------------------------------------------------------------
131
152
  // Champs
@@ -177,7 +198,7 @@ class FormHelper {
177
198
  }
178
199
 
179
200
  // ------------------------------------------------------------
180
- // Messages
201
+ // Messages erreur
181
202
  // ------------------------------------------------------------
182
203
 
183
204
  static extractErrorKeyOfJson(json, onlyIfUniqueError) {
@@ -265,6 +286,10 @@ class FormHelper {
265
286
  }
266
287
 
267
288
 
289
+ // ------------------------------------------------------------
290
+ // Bouton valider
291
+ // ------------------------------------------------------------
292
+
268
293
  static buttonLoader(button, action) {
269
294
  button = $(button);
270
295
  if (action === 'start' || action === 'loading') {
@@ -293,19 +318,6 @@ class FormHelper {
293
318
  return button;
294
319
  }
295
320
 
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
321
 
310
322
  }
311
323
 
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.81",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"