hoodcms 6.0.7 → 6.0.8

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.
@@ -3,120 +3,119 @@ import { Inline } from "./Inline";
3
3
  import { Response } from "./Response";
4
4
 
5
5
  export interface ValidatorOptions {
6
- errorAlert?: string;
6
+ errorAlert?: string;
7
7
 
8
- /**
9
- * Called before the submit of the form data, you can return the data to be serialised.
10
- */
11
- onSubmit?: (sender: Validator) => void;
8
+ /**
9
+ * Called before the submit of the form data, you can return the data to be serialised.
10
+ */
11
+ onSubmit?: (sender: Validator) => void;
12
12
 
13
- /**
14
- * Called when submit is complete.
15
- */
16
- onComplete?: (data: Response, sender?: Validator) => void;
13
+ /**
14
+ * Called when submit is complete.
15
+ */
16
+ onComplete?: (data: Response, sender?: Validator) => void;
17
17
 
18
- /**
19
- * Called when an error occurs.
20
- */
21
- onError?: (jqXHR: any, textStatus: any, errorThrown: any) => void;
18
+ /**
19
+ * Called when an error occurs.
20
+ */
21
+ onError?: (jqXHR: any, textStatus: any, errorThrown: any) => void;
22
22
 
23
- serializationFunction?: () => string;
24
-
25
- useAjax?: boolean;
23
+ serializationFunction?: () => string;
26
24
 
25
+ useAjax?: boolean;
27
26
  }
28
27
 
29
28
  export class Validator {
30
- element: HTMLFormElement;
31
- options: ValidatorOptions = {
32
- errorAlert: 'There are errors on the form, please check your answers and try again.',
33
- useAjax: true
34
- };
35
-
36
- /**
37
- * @param element The datalist element. The element must have a data-url attribute to connect to a feed.
38
- */
39
- constructor(element: HTMLFormElement, options: ValidatorOptions) {
40
-
41
- this.element = element;
42
- if (!this.element) {
43
- return;
44
- }
45
-
46
- this.options.serializationFunction = function (this: Validator) {
47
- let rtn = $(this.element).serialize();
48
- return rtn;
49
- }.bind(this);
50
-
51
- this.options = { ...this.options, ...options };
52
-
53
- this.element.addEventListener('submit', function (this: Validator, e: Event) {
54
- e.preventDefault();
55
- e.stopImmediatePropagation();
56
- this.submitForm();
57
- }.bind(this));
58
- var tag = '[data-submit="#' + this.element.id + '"]';
59
- let submitButtons = $(tag);
60
- if (submitButtons) {
61
- submitButtons.on('click', function (this: Validator, e: Event) {
62
- e.preventDefault();
63
- e.stopImmediatePropagation();
64
- let exit = $(e.currentTarget).data('exit');
65
- if (exit) {
66
- $(this.element).find("input#exit").remove();
67
- $("<input id='exit' />").attr("type", "hidden")
68
- .attr("name", "exit")
69
- .attr("value", "true")
70
- .appendTo(this.element);
71
- }
72
- this.submitForm();
73
- }.bind(this));
74
- }
75
- }
76
-
77
- submitForm() {
78
- this.element.classList.add('was-validated');
79
- if (this.element.checkValidity()) {
80
-
81
- this.element.classList.add('loading');
82
-
83
- let checkboxes = this.element.querySelector('input[type=checkbox]');
84
- if (checkboxes) {
85
- Array.prototype.slice.call(checkboxes)
86
- .forEach(function (checkbox: HTMLInputElement) {
87
- if ($(this).is(':checked')) {
88
- $(this).val('true');
89
- }
90
- });
91
- }
92
-
93
- if (this.options.onSubmit) {
94
- this.options.onSubmit(this);
95
- }
96
-
97
- if (this.options.useAjax) {
98
-
99
- let formData = this.options.serializationFunction();
100
-
101
- $.post(this.element.action, formData, function (this: Validator, data: any) {
102
- if (this.options.onComplete) {
103
- this.options.onComplete(data, this);
104
- }
105
- }.bind(this))
106
- .fail(this.options.onError ?? Inline.handleError);
107
-
108
- } else {
109
-
110
- this.element.submit();
111
-
112
- }
113
-
114
- } else {
115
-
116
- if (this.options.errorAlert) {
117
- Alerts.error(this.options.errorAlert, null, 5000);
118
- }
119
- }
120
-
121
- }
122
- }
29
+ element: HTMLFormElement;
30
+ options: ValidatorOptions = {
31
+ errorAlert: "There are errors on the form, please check your answers and try again.",
32
+ useAjax: true,
33
+ };
34
+
35
+ /**
36
+ * @param element The datalist element. The element must have a data-url attribute to connect to a feed.
37
+ */
38
+ constructor(element: HTMLFormElement, options: ValidatorOptions) {
39
+ this.element = element;
40
+ if (!this.element) {
41
+ return;
42
+ }
43
+
44
+ this.options.serializationFunction = function (this: Validator) {
45
+ let rtn = $(this.element).serialize();
46
+ return rtn;
47
+ }.bind(this);
48
+
49
+ this.options = { ...this.options, ...options };
50
+
51
+ this.element.addEventListener(
52
+ "submit",
53
+ function (this: Validator, e: Event) {
54
+ e.preventDefault();
55
+ e.stopImmediatePropagation();
56
+ this.submitForm();
57
+ }.bind(this)
58
+ );
59
+ var tag = '[data-submit="#' + this.element.id + '"]';
60
+ let submitButtons = $(tag);
61
+ if (submitButtons) {
62
+ submitButtons.on(
63
+ "click",
64
+ function (this: Validator, e: Event) {
65
+ e.preventDefault();
66
+ e.stopImmediatePropagation();
67
+ let exit = $(e.currentTarget).data("exit");
68
+ if (exit) {
69
+ $(this.element).find("input#exit").remove();
70
+ $("<input id='exit' />").attr("type", "hidden").attr("name", "exit").attr("value", "true").appendTo(this.element);
71
+ }
72
+ this.submitForm();
73
+ }.bind(this)
74
+ );
75
+ }
76
+ }
77
+
78
+ submitForm() {
79
+ this.element.classList.add("was-validated");
80
+ if (this.element.checkValidity()) {
81
+ this.element.classList.add("loading");
82
+
83
+ let checkboxes = this.element.querySelector("input[type=checkbox]");
84
+ if (checkboxes) {
85
+ Array.prototype.slice.call(checkboxes).forEach(function (checkbox: HTMLInputElement) {
86
+ if ($(this).is(":checked")) {
87
+ $(this).val("true");
88
+ }
89
+ });
90
+ }
91
+
92
+ if (this.options.onSubmit) {
93
+ this.options.onSubmit(this);
94
+ }
95
+
96
+ if (this.options.useAjax) {
97
+ let formData = this.options.serializationFunction();
98
+
99
+ $.post(
100
+ this.element.action,
101
+ formData,
102
+ function (this: Validator, data: any) {
103
+ if (this.options.onComplete) {
104
+ this.options.onComplete(data, this);
105
+ }
106
+ }.bind(this)
107
+ )
108
+ .fail(this.options.onError ?? Inline.handleError)
109
+ .always(function (this: Validator) {
110
+ this.element.classList.remove("loading");
111
+ }.bind(this));
112
+ } else {
113
+ this.element.submit();
114
+ }
115
+ } else {
116
+ if (this.options.errorAlert) {
117
+ Alerts.error(this.options.errorAlert, null, 5000);
118
+ }
119
+ }
120
+ }
121
+ }