PageSourceSearch

https://www.domrz.ru/bitrix/js/main/core/core_timer.js

js domrz.ru collected 2026-10-02 10:03:03 UTC 6,316 bytes, 316 lines download raw bytes

1(function(window){
2if (window.BX.timer) return;
3
4var timers = [],
5	timeout = 200,
6	obTimer = null,
7	last_index = 0;
8
9BX.timer = function(container, params)
10{
11	params = params || {};
12	if (BX.type.isString(container) || BX.type.isElementNode(container))
13		params.container = container;
14	else if (typeof (container) == "object")
15		params = container;
16
17	if (!params.container)
18		return false;
19
20	var ob = new BX.CTimer(params);
21	BX.timer.start(ob);
22
23	if (null == obTimer)
24	{
25		obTimer = setInterval(_RunTimer, timeout);
26		BX.garbage(BX.timer.clear);
27	}
28
29	return ob;
30}
31
32BX.timer.stop = function(timer)
33{
34	timers[timer.TIMER_INDEX] = null;
35}
36
37BX.timer.start = function(timer)
38{
39	timer.TIMER_INDEX = last_index;
40	timers[last_index++] = timer;
41}
42
43BX.timer.clock = function(cont, dt)
44{
45	return BX.timer({container: cont, dt: dt});
46}
47
48BX.timer.clear = function()
49{
50	clearInterval(obTimer);
51	timers = null;
52}
53
54BX.timer.registerFormat = function(format_name, format_handler)
55{
56	BX.CTimer.prototype.formatValueHandlers[format_name] = format_handler
57}
58
59BX.timer.getHandler = function(format_name)
60{
61	return BX.CTimer.prototype.formatValueHandlers[format_name];
62}
63
64BX.CTimer = function(params, index)
65{
66	this.container = params.container;
67	this.from = params.from ? parseInt(params.from.valueOf()) : null;
68	this.to = params.to ? parseInt(params.to.valueOf()) : null;
69	this.index = index;
70
71	this.dt = parseInt(params.dt);
72	if (isNaN(this.dt))
73		this.dt = 0;
74
75	this.display = params.display || (BX.isAmPmMode() ? 'clock_am_pm' : 'clock'); // other variants - 'simple', 'worktime'
76
77	this.accuracy = params.accuracy || 60; // default timing acccuracy is 1 minute
78
79	// if (this.from)
80		// this.from = new Date(parseInt(this.from.valueOf() / this.accuracy) * this.accuracy);
81	// if (this.to)
82		// this.to = new Date(parseInt(this.to.valueOf() / this.accuracy) * this.accuracy);
83
84	this.callback = this.from ? this._callback_from : (this.to ? this._callback_to : this._callback);
85	this.callback_finish = params.callback_finish;
86
87	this.formatValue = this.formatValueHandlers.clock;
88
89	this.bInited = false;
90	BX.ready(BX.delegate(this.Init, this));
91}
92
93BX.CTimer.prototype.Init = function()
94{
95	if (this.bInited)
96		return;
97
98	this.container = BX(this.container);
99	this.container_value_fld = this.container.tagName.toUpperCase() == 'INPUT' ? 'value' : 'innerHTML';
100	if (this.container_value_fld == 'value' && (this.display == 'clock' || this.display == 'clock_am_pm'))
101	{
102		if (this.display == 'clock')
103		{
104			this.display = 'simple';
105		}
106		else if (this.display == 'clock_am_pm')
107		{
108			this.display = 'simple_am_pm';
109		}
110	}
111
112	this.formatValue = this.formatValueHandlers[this.display] ? this.formatValueHandlers[this.display] : this.formatValueHandlers.clock;
113
114	this.bInited = true;
115}
116
117BX.CTimer.prototype.setFrom = function(from)
118{
119	if (!this.from) return;
120	this.from = from;
121}
122
123BX.CTimer.prototype.setTo = function(to)
124{
125	if (!this.to) return;
126	this.to = to;
127}
128
129BX.CTimer.prototype._callback = function(date)
130{
131	if (this.dt !== 0)
132		var date = new Date(date.valueOf() + this.dt);
133
134	this.setValue(this.formatValue(date.getHours(), date.getMinutes(), date.getSeconds()));
135}
136
137BX.CTimer.prototype._callback_from = function(date)
138{
139	var diff = (date.valueOf() - this.from.valueOf() + this.dt)/1000;
140	this.setValue(
141		this.formatValue(
142			parseInt(diff / 3600),
143			parseInt((diff % 3600) / 60),
144			parseInt(diff % 60)
145		)
146	);
147}
148
149BX.CTimer.prototype._callback_to = function(date)
150{
151	var diff = (this.to.valueOf() - date.valueOf())/1000;
152	if (diff > 0)
153	{
154		this.setValue(
155			this.formatValue(
156				parseInt(diff / 3600),
157				parseInt((diff % 3600) / 60),
158				parseInt(diff % 60)
159			)
160		);
161	}
162	else
163	{
164		this.Finish();
165	}
166}
167
168BX.CTimer.prototype.formatValueHandlers = {
169	clock: function(h, m, s)
170	{
171		var d = '<span class="bx-timer-semicolon">:</span>';
172
173		return BX.util.str_pad(h, 2, '0', 'left')
174			+ d
175			+ (this.accuracy >= 3600
176				? '00'
177				: BX.util.str_pad(m, 2, '0', 'left'))
178			+ (this.accuracy >= 60
179				? ''
180				:
181				(d + BX.util.str_pad(s, 2, '0', 'left'))
182			);
183	},
184	clock_am_pm: function(h, m, s)
185	{
186		var mt = 'am';
187		var d = '<span class="bx-timer-semicolon">:</span>';
188
189		if (h > 12)
190		{
191			h = h - 12;
192			mt = 'pm';
193		}
194		else if (h == 0)
195		{
196			h = 12;
197			mt = 'am';
198		}
199		else if (h == 12)
200		{
201			mt = 'pm';
202		}
203
204		return h
205			+ d
206			+ (this.accuracy >= 3600
207				? '00'
208				: BX.util.str_pad(m, 2, '0', 'left'))
209			+ (this.accuracy >= 60
210				? ''
211				:
212				(d + BX.util.str_pad(s, 2, '0', 'left'))
213			)
214			+ ' ' + mt;
215	},
216	simple: function(h, m, s)
217	{
218		return BX.util.str_pad(h, 2, '0', 'left')
219			+ ':'
220			+ (this.accuracy >= 3600
221				? '00'
222				: BX.util.str_pad(m, 2, '0', 'left'))
223
224			+ (this.accuracy >= 60
225				? ''
226				:
227				(':' + BX.util.str_pad(s, 2, '0', 'left'))
228			);
229	},
230	simple_am_pm: function(h, m, s)
231	{
232		var mt = 'am';
233
234		if (h > 12)
235		{
236			h = h - 12;
237			mt = 'pm';
238		}
239		else if (h == 0)
240		{
241			h = 12;
242			mt = 'am';
243		}
244		else if (h == 12)
245		{
246			mt = 'pm';
247		}
248		return h
249			+ ':'
250			+ (this.accuracy >= 3600
251				? '00'
252				: BX.util.str_pad(m, 2, '0', 'left'))
253
254			+ (this.accuracy >= 60
255				? ''
256				:
257				(':' + BX.util.str_pad(s, 2, '0', 'left'))
258			) + ' ' + mt;
259	},
260	worktime: function(h, m, s)
261	{
262		return h + BX.message('JS_CORE_H') + ' '
263			+ (this.accuracy >= 3600
264				? ''
265				: m + BX.message('JS_CORE_M')
266					+ (this.accuracy >= 60
267						? ''
268						: ' ' + s + BX.message('JS_CORE_S')
269					)
270				);
271	},
272	worktime_short: function(h, m, s)
273	{
274		return BX.util.rtrim((h > 0 ? h + BX.message('JS_CORE_H') + ' ' : '')
275			+ (m > 0 && this.accuracy < 3600 ? m + BX.message('JS_CORE_M') + ' ' : '')
276			+ (this.accuracy >= 60
277				? ''
278				: (s > 0 ? s + BX.message('JS_CORE_S') : '')
279			));
280	}
281}
282
283BX.CTimer.prototype.setValue = function(value)
284{
285	if (this.bInited)
286	{
287		if (value != this._last_value)
288			this.container[this.container_value_fld] = value;
289
290		this._last_value = value;
291	}
292}
293
294BX.CTimer.prototype.Finish = function()
295{
296	BX.timer.stop(this);
297
298	if (this.callback_finish)
299		this.callback_finish.apply(this);
300
301	BX.cleanNode(this.container.parentNode);
302}
303
304function _RunTimer()
305{
306	var current_moment = new Date();
307
308	for (var i=0,len=last_index;i<len;i++)
309	{
310		if (timers[i] && timers[i].callback)
311			timers[i].callback.apply(timers[i], [current_moment]);
312	}
313
314	current_moment = null;
315}
316})(window)

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.