PageSourceSearch

https://erus.ru/Content/js/jq.etc.setterbuilder-1.0.js?v20230111

js erus.ru collected 2026-10-07 19:34:01 UTC 4,022 bytes, 144 lines download raw bytes

1(function()
2{
3	'use strict';
4	$.ETC						= $.ETC || {};
5	if ($.ETC.SetterBuilder) return;
6
7	$.ETC.SetterBuilder			= _build;
8
9	/*
10	Построение функций раскладки данных из JSON
11	Возвращает объект для установки значений
12		
13	типовой пример JSON:
14	{ 
15		"Key"        : "Model_Employee_Key", 
16		"Version"    : "Model_Employee_Version", 
17		"DisplayText":
18		[
19			"Model_Employee_DisplayText", 
20			"Model_Employee_DisplayText_lookup.lookuptext"
21		]
22	}
23
24	prepModel - функция, вызываемая перед вызовом первого setterа для предварительной обработки модели.
25	*/
26	function _build(targets, container, prepModel)
27	{
28		var setters				= [];
29		if (prepModel)
30			setters.push(typeof(prepModel)==='string'?new Function('m', prepModel):prepModel);
31
32		if (targets)
33		{
34			var target;
35			for (var t in targets)
36			{
37				if ($.isArray(targets[t])) 
38				{
39					// будем класть одно значение в несколько полей
40					for (var v in targets[t])
41					{
42						v		= targets[t][v];
43						if (!v || typeof(v)!=='string') continue;
44
45						target	= v.split('.');		// Берем элемент и пытаемся отделить ID от метода
46						_addsetter(setters, t, target, container);
47					}
48				} else
49				{
50					//одно значение - одно поле
51					target		= targets[t].split('.');		// Берем элемент и пытаемся отделить ID от метода
52					_addsetter(setters, t, target, container);
53				}
54			}
55		}
56
57		if(setters.length>0)
58		{
59			return function(val, changeIfEmpty)
60			{					
61				var sval		= val || {},
62					cie			= changeIfEmpty && true;
63
64				//заполняем поля
65				for (var i = 0; i < setters.length; i++)
66				{
67					setters[i](sval, cie);
68				}
69			};
70		} else
71		{
72			return function(){/* do nothing */};
73		}
74	}
75
76	// Дополнительные методы для управления данными из data-targets
77	var _customMethods			= 
78	{
79		'class':				function(val)
80		{
81			if (val === undefined)
82				return this.attr('class');
83			else
84				this.attr('class', val);
85		},
86		'checked':				function(val)
87		{
88			if (val === undefined)
89				return this.attr('checked')=='checked';
90			//else 
91			//	this.attr('checked', val ? 'checked' : '' );
92			else if (val == true)
93				this.attr('checked', 'checked');
94			else
95				this.attr('checked', false);
96		}
97	};
98
99	// Поиск элемента и
100	// создание функции раскладки данных из поля id в элемент el с помощью метода method
101	function _addsetter(setters, t, target, container)
102	{
103		var el					= (container==null || container.length==0)?$(document.getElementById(target[0])):$('#'+target[0], container);	// Ищем элемент по документу
104		if(el.length==0)
105			return;
106
107		var method				= target[1] || 'val';						// Берем метод, по-умолчнию это val
108		var cm					= _customMethods[method];
109		if(cm!=null)
110			el[method]			= cm;
111		
112		var valFx;
113		if (t.indexOf('.')>0)
114		{
115			t					= t.split('.');
116			valFx				= function(val)
117			{
118				for (var i=0; i<t.length; i++)
119				{
120					val			= val[t[i]];
121					if (val==null) break;
122				}
123				return val;
124			};
125		} else
126		{
127			valFx				= function(val)
128			{
129				return val[t];
130			};
131		}
132
133		setters.push(function (val, changeIfEmpty)	// Создаем функцию раскладки
134		{
135			var v				= valFx(val);
136			if (v===0 || v===false || v==='')
137				el[method](v);
138			else if (v || changeIfEmpty)
139				el[method](v || '');
140				
141			el.trigger('change');	// TODO: ПОКА ДЛЯ СОВМЕСТИМОСТИ! инициируем событие на элементе во избежание невыполнения скриптов на нем
142		});
143	}
144})();

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.