PageSourceSearch

https://school.dress-to-impress.ru/nassets/d40f6009/js/localstore.js?v=1765633901

js dress-to-impress.ru collected 2026-09-28 09:03:02 UTC 6,959 bytes, 210 lines download raw bytes

1/**
2 * Для использования
3 * $(document).ready(function(){
4 *     $('form').FormCache({'form': 'селектор формы', attr: 'Элемент который хранит постоянный id формы'});
5 * });
6 */
7// console.log('модуль кеширования');
8(function ($) {
9	$.fn.FormCache = function (options) {
10		var settings = $.extend({
11			'form' : 'form:first',
12			// id формы/ не всегда хранится в id
13			'attr' : 'id',
14			// Признак того что анкета создана в редакторе страницы и нужно обновить surveyData и отделить анкету
15			// от данных клиента
16			'surveyData': null,
17			'autoResetCache': true
18		}, options);
19		var multidata = {};
20		function on_change(event) {
21			var input = $(event.target);
22			window.Xin = input;
23			var formID = input.parents(settings.form).attr(settings.attr);
24			var currentpartindex = input.parents('[data-block-id=' + formID + ']').attr('data-currentpartindex');
25			// Данные по всем полям
26			var data = loadLocalStore(formID);
27			multidata = data.multidata !== undefined ? data.multidata : {};
28
29			if (input.attr('id') !== undefined) {
30				if (input.attr('type') === 'radio' || input.attr('type') === 'checkbox') {
31					data[input.attr('id')] = input.is(':checked');
32				} else {
33					data[input.attr('id')] = input.val();
34				}
35			} else {
36				if (input.attr('type') === 'checkbox' || input.attr('type') === 'radio') {
37					// Это множественное поле выбора
38					var name = input.attr('name');
39					if (!(name in multidata)) {
40						multidata[name] = [];
41					}
42					input.parents(settings.form).find('input[name="' + name + '"]').each(function (index, el) {
43						var val = $(el).is(':checked');
44						multidata[name][index] = val;
45					})
46					data.multidata = multidata;
47					window.data = data;
48				} else if (input.attr('type') === 'text') {
49					var name = input.attr('name');
50					if (!(name in multidata)) {
51						multidata[name] = [];
52					}
53					input.parents(settings.form).find('input[name="' + name + '"]').each(function (index, el) {
54						multidata[name][index] = $(el).val();
55					})
56					data.multidata = multidata;
57					window.data = data;
58				}
59			}
60			if (currentpartindex !== undefined) {
61				data['currentpartindex'] = currentpartindex;
62			}
63
64			updateLocalStore(formID, data);
65		}
66		function on_submit(event) {
67			var input = $(event.target);
68			var formID = input.parents(settings.form).attr(settings.attr);
69			if (options.autoResetCache) {
70				localStorage.removeItem(formID);
71			}
72		}
73
74		function updateLocalStore(formID, data) {
75			localStorage[formID] = JSON.stringify(data);
76		}
77
78		function loadLocalStore(formID) {
79			var data = false;
80			if (localStorage[formID]) {
81				data = JSON.parse(localStorage[formID]);
82			}
83			if (!data) {
84				localStorage[formID] = JSON.stringify({});
85				data = JSON.parse(localStorage[formID]);
86			}
87			return data;
88		}
89
90		return this.each(function () {
91			var element = $(this);
92			if (typeof (Storage) !== "undefined") {
93				var formID = element.attr(settings.attr);
94				var data = loadLocalStore(formID);
95
96				var cacheInputs = element.find('input, select, textarea');
97				if (settings.surveyData !== null) {
98					cacheInputs = element.find(settings.surveyData).find('input, select, textarea');
99				}
100
101				cacheInputs.change(on_change);
102				cacheInputs.keyup(on_change);
103				// Эксперементально в ContextHelpLink элементы могут добавляться динамически
104				element.on('change','input', on_change);
105
106				if (element.find('button.btn-send').length > 0) {
107					element.find('button.btn-send').on('click', on_submit);
108				} else {
109					element.find('button[type="submit"]').on('click', on_submit);
110				}
111				cacheInputs.each(function () {
112					if ($(this).attr('type') !== 'submit') {
113						var input = $(this);
114						if (input.attr('readonly') === 'readonly') {
115							return;
116						}
117						var value = data[input.attr('id')];
118						// Есть данные только с бэка
119						var onlyDataFromBack = input.attr('id') !== undefined && value === undefined;
120
121						// Есть введенные ранее значения которые подгрузились с формой
122						// инпуты
123						if (
124							onlyDataFromBack
125							&& input.attr('type') !== 'checkbox' && input.attr('type') !== 'radio'
126							&& input.val() !== ''
127						) {
128							return;
129						}
130						// чекбоксы
131						if (
132							onlyDataFromBack
133							&& (input.attr('type') === 'checkbox' || input.attr('type') === 'radio')
134							&& input.is(':checked')
135						) {
136							return;
137						}
138						//
139						if (input.attr('id') !== undefined) {
140							if (input.attr('type') === 'radio') {
141								if (value) {
142									input.attr('checked', input.is(':checked'));
143								} else {
144									input.removeAttr('checked');
145								}
146							} else if (input.attr('type') === 'checkbox') {
147								if (value) {
148									input.attr('checked', 'checked');
149								} else {
150									input.attr('checked', false);
151								}
152							} else {
153								input.val(value);
154								if (
155									input.attr('id') !== 'CustomFormValue'
156									&& input.parent().find('div.uploadifive-button').length === 1
157								) {
158									if (value !== undefined && input.fileWidget !== undefined) {
159										input.fileWidget('showPreview');
160									}
161								}
162							}
163						}
164					}
165				});
166				if (data.multidata !== undefined ) {
167					for (var i in data.multidata) {
168						if (i === 'ContextHelpLink[]') {
169							var countEl = data.multidata[i].length;
170							var countPage = element.find('input[name="' + i + '"]').length;
171							var def = countEl - countPage;
172
173							if (def > 0 && window.addRowLink !== undefined) {
174								for (var j = 0; j < def; j++) {
175									window.addRowLink();
176								}
177							}
178						}
179						if (element.find('input[name="' + i + '"]').length > 0) {
180							element.find('input[name="' + i + '"]').each(function (index, el) {
181								if ($(el).attr('type') === 'text') {
182									$(el).val(data.multidata[i][index]);
183								} else {
184									$(el).prop('checked', data.multidata[i][index]);
185								}
186							})
187						}
188					}
189				}
190				// установить нужную вкладку
191				if (data['currentpartindex'] !== undefined) {
192					$('#' + element.parents('[data-block-id=' + formID + ']').attr('id'))
193						.liteSinglyForm({currentPartIndex: parseInt(data['currentpartindex'])});
194				}
195				var widget = element.find('.custom-form');
196				if (settings.surveyData !== null && widget.length) {
197					$('#' + widget.attr('id')).customForm('changed')
198				}
199			} else {
200				console.log('local storage is not available');
201			}
202		});
203	};
204	$.fn.resetLocalCache = function (options) {
205		var settings = $.extend({
206			'attr' : 'id'
207		}, options);
208		localStorage.removeItem(this.attr(settings.attr));
209	};
210}(jQuery));

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.