PageSourceSearch

https://binar-shop.ru/local/templates/main/js/checkForm.js

js binar-shop.ru collected 2026-10-04 09:20:24 UTC 8,912 bytes, 373 lines download raw bytes

1(function(){
2	var CheckForm = function(element, option){
3		this.option = option;
4		this.$element = $(element);
5
6		this.timeout;
7		this.reset_time = 2000;
8
9		this.items = [];
10
11		this.init();
12	}
13
14	CheckForm.prototype = {
15		init: function(){
16			var _this = this;
17
18			this.errors_count = null;
19			this.items = [];
20
21			this.$element_form = this.$element.find('> form');
22			this.$items = this.$element.find('.req');
23			this.$items_wrap = this.$element.find('.form-card__body');
24
25			this.$submit_btn = this.$element.find('[data-submit]');
26			this.$reset_btn = this.$element.find('.reply-fc');
27			this.$alert_block = this.$element.find('.form-card__ctrl-alert .alert');
28
29			this.reply = this.$element.attr('data-reply');
30
31			this.getItems();
32
33			if (this.items == "") {
34				return false
35			}
36
37			this.checkItemsStatus();
38
39			this.setFormStatus();
40			this.setBtnStatus();
41
42			this.calcErrors();
43
44			this.setEvents();
45
46		},
47
48		update: function(){
49			this.destroy();
50			this.init()
51		},
52
53		destroy: function(){
54			for (index in this.items){
55				this.items[index].unbind('focusin.checkform focusout.checkform change.checkform');
56				this.items[index].removeClass('is-error is-success');
57			}
58			this.$submit_btn.unbind('click.checkform').removeClass('is-disable');
59			this.$reset_btn.unbind('click.checkform');
60		},
61
62		getItems: function(){
63			var _this = this;
64
65			_this.$items.each(function(key){
66				$(this).data('index', key);
67				_this.items.push($(this));
68				_this.items[key]['index'] = key;
69				_this.items[key].pristine = true;
70
71				_this.setItemType(key);
72				//_this.setItemStatus(item);
73			});
74		},
75
76		getFormStatus: function(){
77			return this.form_status;
78		},
79
80		validate: function(){
81			this.checkItemsStatus();
82
83			this.setFormStatus();
84			this.setBtnStatus();
85
86			for (index in this.items){
87				this.setItemStyle(index);
88			}
89
90			this.calcErrors();
91		},
92
93		validateItem: function($item){
94			var index = $item.data('index');
95
96			this.setItemStatus(index);
97			this.setFormStatus();
98			this.setBtnStatus();
99			this.setItemStyle(index);
100			this.calcErrors();
101		},
102
103		checkItemsStatus: function(){
104			for (index in this.items){
105				this.setItemStatus(index);
106			}
107		},
108
109		setItemStatus: function(index){
110			this.items[index]['success'] = this.checkInput(index);
111		},
112
113		setItemType: function(index){
114			if (this.items[index].attr('type') == 'checkbox' || this.items[index].attr('type') == 'radio') {
115				this.items[index]['type'] = 'check';
116			}
117			else if(this.items[index].attr('data-type') == 'rate'){
118				this.items[index]['type'] = 'rate';
119			}
120			else if(this.items[index].attr('data-type') == 'select'){
121				this.items[index]['type'] = 'select';
122			}
123			else {
124				this.items[index]['type'] = 'text';
125			}
126		},
127
128		setFormStatus: function(){
129			for(item in this.items) {
130				if (!this.items[item]['success']){
131					this.form_status = false;
132					this.$element.trigger('checkForm.unvalidated');
133					break;
134				}
135				this.form_status = true;
136				this.$element.trigger('checkForm.validated');
137			}
138
139			if(this.items.length == 0){
140				this.form_status = true;
141			}
142		},
143
144		setBtnStatus: function(){
145			if(this.form_status){
146				this.btn_status = true;
147				this.$submit_btn.removeClass('is-disable');
148			}
149			else {
150				this.btn_status = true;
151				//this.$submit_btn.addClass('is-disable');
152			}
153		},
154
155		setEvents: function(){
156			var _this = this;
157
158			for (item in _this.items){
159				switch (_this.items[item]['type']) {
160
161					case 'text':
162						_this.items[item].on({
163							'focusin.checkform': function(index){
164								return function(){
165									_this.removeItemStyle(index);
166									if (_this.errors_count <= 1 && _this.items.length !== 1) {
167										$(this).on('keyup.checkform', function(){
168											_this.removeItemStyle(index);
169											_this.setItemStatus(_this.items[index]['index']);
170											_this.setFormStatus();
171											_this.setBtnStatus();
172											_this.setItemStyle(index);
173										}).on('focusout.checkform.spec', function(){
174											$(this).off('focusout.checkform.spec');
175											$(this).off('keyup.checkform');
176										});
177									}
178								}
179							}(item),
180							'focusout.checkform': function(index){
181								return function(){
182									if(_this.items[index].val() !== ''){
183										_this.items[index].pristine = false;
184									}
185
186									if(!_this.items[index].pristine){
187										_this.setItemStatus(_this.items[index]['index']);
188										_this.setFormStatus();
189										_this.setBtnStatus();
190										_this.setItemStyle(index);
191
192										_this.calcErrors();
193									}
194
195								}
196							}(item)
197						});
198
199						if(_this.items[item][0].tagName == 'SELECT'){
200							_this.items[item].on({
201								'change.checkform': function(index){
202									return function(){
203										_this.removeItemStyle(index);
204										_this.setItemStatus(_this.items[index]['index']);
205										_this.setFormStatus();
206										_this.setBtnStatus();
207										_this.setItemStyle(index);
208									}
209								}(item)
210							});
211						}
212					break
213
214					case 'check':
215						_this.items[item].on('change.checkform', function(index){
216							return function(){
217								_this.setItemStatus(_this.items[index]['index']);
218								_this.setFormStatus();
219								_this.setBtnStatus(index);
220								_this.removeItemStyle(index);
221								_this.setItemStyle(index);
222								_this.calcErrors();
223							}
224						}(item))
225					break
226				}
227
228			}
229			this.$submit_btn.on('click.checkform', function(){
230				if (this.btn_status) {
231					_this.submitForm();
232				}
233			})
234			this.$submit_btn.on('click.checkform', function(){
235				if (this.reply) {
236					_this.resetForm();
237				}
238			})
239		},
240
241		checkInput: function(index){
242
243			var inputSelectType = this.items[index].attr('data-type');
244			if(inputSelectType == undefined && this.items[index]['type'] == 'check'){
245				inputSelectType = 'checkbox';
246			}
247			if (this.items[index]['type'] == "text" && this.items[index][0].value == "") {
248				return false;
249			}
250			else if (this.items[index]['type'] == "check" && !this.items[index][0].checked && !this.items[index]['success']){
251				return false;
252			}
253			else {
254				switch (inputSelectType) {
255					case 'email':
256						var pattern = /^([a-z0-9_\.-])+@[a-z0-9-]+\.([a-z]{2,4}\.)?[a-z]{2,4}$/i;
257						if( pattern.test(this.items[index][0].value) ){
258							return true;
259						}
260						else {
261							return false;
262						}
263					break
264
265					case 'phone':
266						var pattern = /^\+7 \d{3}\ \d{3} \d{2} \d{2}$/i;
267		                if(!pattern.test(this.items[index][0].value)){
268		                	return false
269		                }
270		                else {
271		                	return true
272		                }
273
274
275						/*if(this.items[index][0].value.length < 20) {
276							this.items[index].removeClass('is-mask-check');
277						}
278						if(this.items[index].hasClass('is-mask-check')) {
279							return true
280						}
281						else {
282							return false
283						}*/
284					break
285
286					case 'radio':
287						var radio_name = this.items[index].attr('name');
288						for (index in this.items){
289							if (this.items[index].attr('name') == radio_name){
290								this.items[index]['success'] = true;
291								this.setItemStyle(index);
292							}
293						}
294						return true;
295					break
296
297					case 'checkbox':
298						return !!this.items[index][0].checked;
299					break
300
301					default:
302						return true;
303					break
304				}
305			}
306		},
307
308		removeItemStyle: function(index){
309			this.items[index].removeClass('is-success is-error');
310		},
311
312		setItemStyle: function(index){
313			if(this.items[index]['success']) {
314				this.items[index].removeClass('is-error').addClass('is-success');
315			}
316			else if(!this.items[index]['success']) {
317				this.items[index].removeClass('is-success').addClass('is-error');
318			}
319		},
320
321		submitForm: function(){
322			var _this = this;
323			this.$alert_block.removeClass('hide');
324
325			if(this.reply) {
326				this.$items_wrap.addClass('hide');
327				this.$submit_btn.addClass('hide');
328				this.$reset_btn.removeClass('hide');
329
330				this.timeout = setTimeout(function(){
331					_this.resetForm();
332				}, this.reset_time);
333			}
334		},
335
336		resetForm: function(){
337			clearTimeout(this.timeout);
338
339			this.$alert_block.addClass('hide');
340			this.$items_wrap.removeClass('hide');
341
342			this.$submit_btn.removeClass('hide');
343			this.$reset_btn.addClass('hide');
344
345			this.$element_form.trigger('reset');
346
347			this.$element.find('input, textarea').removeClass('is-active');
348			this.$element.find('.txt-input__field-remove').removeClass('is-show');
349
350			this.setFormStatus();
351			this.setBtnStatus();
352		},
353
354		calcErrors: function(){
355			this.errors_count = 0;
356			for (index in this.items){
357				if(this.items[index]['success'] == false){
358					this.errors_count ++;
359				}
360			}
361		}
362	}
363
364	$.fn.checkForm = function (option, param) {
365		return this.each(function () {
366			var $this = $(this),
367				data  = $this.data('my.checkForm');
368
369			if (!data) $this.data('my.checkForm', (data = new CheckForm(this, option)));
370			if (typeof option == 'string') data[option](param);
371		});
372	}
373})();

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.