PluginProbe ʕ •ᴥ•ʔ
Strong Testimonials / 3.3.4
Strong Testimonials v3.3.4
3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 trunk 1.0.1 2.30.9 2.31.10 2.32 2.32.1 2.32.2 2.32.3 2.32.4 2.33 2.34 2.35 2.36 2.37 2.38 2.38.1 2.39 2.39.1 2.39.2 2.39.3 2.40.0 2.40.1 2.40.2 2.40.3 2.40.4 2.40.5 2.40.6 2.40.7 2.41.0 2.41.1 2.50.0 2.50.1 2.50.2 2.50.3 2.50.4 2.51.0 2.51.1 2.51.2 2.51.3 2.51.4 2.51.5 2.51.6 2.51.7 2.51.8 2.51.9 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.1.1 3.1.10 3.1.11 3.1.12 3.1.13 3.1.14 3.1.15 3.1.16 3.1.17 3.1.18 3.1.19 3.1.2 3.1.20 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 3.2.0 3.2.1 3.2.10 3.2.11 3.2.12 3.2.13 3.2.14 3.2.15 3.2.16 3.2.17 3.2.18 3.2.19 3.2.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8 3.2.9 3.3.0
strong-testimonials / assets / public / js / lib / form-validation / form-validation.js
strong-testimonials / assets / public / js / lib / form-validation Last commit date
form-validation.js 3 weeks ago form-validation.min.js 3 weeks ago
form-validation.js
281 lines
1 /**
2 * Submission form validation
3 */
4
5 class strongValidation {
6
7 constructor(form) {
8 this.form = jQuery(form).find('form');
9 this.formID = this.form.data('formid');
10
11 this.defaults = {
12 ajaxUrl: '',
13 display: {
14 successMessage: false
15 },
16 scroll : {
17 onError : true,
18 onErrorOffset : 100,
19 onSuccess : true,
20 onSuccessOffset: 100
21 },
22 fields : {},
23 };
24
25 this.settings = {};
26 this.rules = {};
27
28 this.init();
29 }
30
31 setOpts(options) {
32 this.settings = jQuery.extend({}, this.defaults, options);
33 }
34 /**
35 * Add custom validation rule to star-rating pseudo elements.
36 */
37 setRules() {
38 for (var i = 0; i < this.settings.fields.length; i++) {
39
40 if ('rating' === this.settings.fields[i].type) {
41 if (1 === this.settings.fields[i].required) {
42 this.rules[this.settings.fields[i].name] = {ratingRequired: true};
43 }
44 }
45
46 }
47 }
48 /**
49 * Initialize.
50 */
51 init() {
52
53 var strongForm = {};
54
55 if (typeof window['strongForm'] !== 'undefined') {
56 strongForm = this.form.data("config");
57 }
58
59 this.setOpts(strongForm);
60
61 if (this.settings.display.successMessage) {
62
63 this.scrollOnSuccess();
64
65 } else {
66
67 this.setRules();
68 this.changeEvents();
69 this.customValidators();
70 this.validateForm();
71
72 }
73
74 }
75
76 changeEvents() {
77
78 // Trim blanks
79 jQuery('input[type="text"], input[type="url"], input[type="email"], textarea', '.wpmtst-submission-form').on('change blur', function (e) {
80 e.target.value = e.target.value.trim();
81 });
82
83 // Add protocol if missing
84 // Thanks http://stackoverflow.com/a/36429927/51600
85 jQuery('input[type=url]').on('change', function () {
86 if (this.value.length && !/^https*:\/\//.test(this.value)) {
87 this.value = 'https://' + this.value;
88 }
89 });
90
91 // Star ratings
92 var ratings = document.getElementsByClassName('strong-rating');
93 for (var i = 0; i < ratings.length; i++) {
94 // Handle keystrokes
95 ratings[i].addEventListener('click', this.handleRadioEvent, true);
96 ratings[i].addEventListener('keyup', this.handleRadioEvent, true);
97 // Validate on change
98 ratings[i].addEventListener('change', function () {
99 jQuery(this).valid();
100 }, true);
101 }
102
103 }
104 /**
105 * Show overlay during form submission.
106 */
107 disableForm() {
108 //apply form wait buffer only for the submited form
109 jQuery('.strong-form-wait[data-formid="' + this.formID + '"]').show();
110 this.form.find('.wpmtst_submit_testimonial').prop('disabled', true);
111 }
112 /**
113 * Hide overlay after form submission.
114 */
115 enableForm() {
116 jQuery('.strong-form-wait[data-formid="' + this.formID + '"]').hide();
117 this.form.find('.wpmtst_submit_testimonial').prop('disabled', false);
118 }
119
120 handleRadioEvent(e) {
121 // If key 0-5 fired the event, trigger click on that star (including hidden zero).
122 if (e.keyCode >= 48 && e.keyCode <= 53) {
123 var key = e.keyCode - 48;
124 jQuery(this).find('input[type="radio"][value=' + key + ']').trigger('click');
125 }
126 }
127
128 customValidators() {
129 /**
130 * Only use elements that can legitimately have a 'name' attribute:
131 * <button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>,
132 * <output>, <select>, <textarea>, <map>, <meta>, <param>
133 *
134 * @link https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes
135 *
136 * jQuery Validate v1.16.0
137 * As of 6/10/2017
138 */
139 jQuery.validator.addMethod('ratingRequired', function (value, element) {
140 return jQuery(element).find('input:checked').val() > 0;
141 }, jQuery.validator.messages.required);
142 }
143
144 validateForm() {
145
146 //remember current object for function calls later
147 var instance = this;
148 /**
149 * Validate the form
150 */
151 this.form.validate(
152 {
153
154 onfocusout: false,
155
156 focusInvalid: false,
157
158 invalidHandler: function (form, validator) {
159
160 // Focus first invalid input
161 var errors = validator.numberOfInvalids();
162 if (errors) {
163 if (instance.settings.scroll.onError) {
164 if (typeof validator.errorList[0] !== 'undefined') {
165 var firstError = jQuery(validator.errorList[0].element);
166 var fieldOffset = firstError.closest('.form-field, .wpmtst-form-field').offset();
167 var scrollTop = fieldOffset.top - instance.settings.scroll.onErrorOffset;
168 jQuery('html, body').animate({scrollTop: scrollTop}, 800, function () {
169 firstError.focus();
170 });
171 }
172 } else {
173 validator.errorList[0].element.focus();
174 }
175 }
176 },
177
178 submitHandler: function () {
179 instance.disableForm();
180 // If Ajax
181 if (instance.settings.ajaxUrl !== '') {
182
183 window.onbeforeunload = function () {
184 return "Please wait while the form is submitted.";
185 }
186
187 var formOptions = {
188 url : instance.settings.ajaxUrl,
189 data : {
190 action: 'wpmtst_form2'
191 },
192 success: function (success) {
193 instance.showResponse(success);
194 },
195 };
196 instance.form.ajaxSubmit(formOptions);
197
198 } else {
199
200 instance.form.get(0).submit();
201 }
202 },
203
204 /* Normalizer not working */
205 // normalizer: function( value ) {
206 // return jQuery.trim( value )
207 // },
208
209 rules: this.rules,
210
211 errorPlacement: function (error, element) {
212 error.appendTo(element.closest('div.form-field, div.wpmtst-form-field'));
213 },
214
215 highlight: function (element, errorClass, validClass) {
216 if (element.type === 'checkbox') {
217 jQuery(element).closest('.field-wrap, .wpmtst-field-wrap').addClass(errorClass).removeClass(validClass);
218 } else if ('rating' === jQuery(element).data('fieldType')) {
219 jQuery(element).closest('.field-wrap, .wpmtst-field-wrap').addClass(errorClass).removeClass(validClass);
220 } else {
221 jQuery(element).addClass(errorClass).removeClass(validClass);
222 }
223 },
224
225 unhighlight: function (element, errorClass, validClass) {
226 if (element.type === 'checkbox') {
227 jQuery(element).closest('.field-wrap, .wpmtst-field-wrap').removeClass(errorClass).addClass(validClass);
228 } else if ('rating' === jQuery(element).data('fieldType')) {
229 jQuery(element).closest('.field-wrap, .wpmtst-field-wrap').removeClass(errorClass).addClass(validClass);
230 } else {
231 jQuery(element).removeClass(errorClass).addClass(validClass);
232 }
233 }
234
235 });
236
237
238 }
239 /**
240 * Display message/errors upon Ajax submission
241 *
242 * @param response
243 */
244 showResponse(response) {
245 window.onbeforeunload = null;
246 this.enableForm();
247 var obj = JSON.parse(response);
248 if (obj.success) {
249 this.form.parent().html(obj.message);
250 this.scrollOnSuccess();
251 } else {
252 for (var key in obj.errors) {
253 if (obj.errors.hasOwnProperty(key)) {
254 this.form.children('.field-' + key + ', .wpmtst-field-' + key)
255 .find('span.error, span.wpmtst-error')
256 .remove()
257 .end()
258 .append('<span class="error wpmtst-error">' + obj.errors[key] + '</span>');
259 }
260 }
261 }
262 }
263 /**
264 * Scroll to success message
265 */
266 scrollOnSuccess() {
267 if (this.settings.scroll.onSuccess) {
268 var containerOffset, scrollTop;
269 containerOffset = jQuery('.wpmtst-form-id-' + this.formID).find('.wpmtst-testimonial-success').offset();
270 if (containerOffset) {
271 scrollTop = containerOffset.top - this.settings.scroll.onSuccessOffset;
272 // is WordPress admin bar showing?
273 if (jQuery('#wpadminbar').length) {
274 scrollTop -= 32;
275 }
276 jQuery('html, body').animate({scrollTop: scrollTop}, 800);
277 }
278 }
279 }
280 }
281