PageSourceSearch

https://germet.pro/g/templates/shop2/2.130.2/default-3/js/timer.js

js germet.pro collected 2026-10-08 08:24:46 UTC 6,935 bytes, 193 lines download raw bytes

1function grTimer( options ) {
2
3  var extend = function(a, b){ 
4      for(var key in b)
5          if(b.hasOwnProperty(key))
6              a[key] = b[key];
7      return a;
8  };
9
10  this.options = { 
11    container : '.timer-block__date',
12    dataEndTimer: 'data-timer-end',
13    element : null,
14    update_time : 1000,
15    wrapChar : false,
16    date_over : '',
17    template : '', 
18    language : 'ru',
19    onTimeChange : function(){},
20    onEnd : function(){}
21  };
22
23  extend( this.options, options ); 
24
25  var _this = this; 
26
27  var now = new Date(); 
28
29  
30  var htmlLang = _this.options.language;
31
32  var timerDays = '**d**';
33  var timerHours = '**h**';
34  var timerMinutes = '**m**';
35  var timerSeconds = '**s**';
36
37  var lang = {
38      days: ['day', 'days', 'days'],
39      hours: ['hour', 'hours', 'hours'],
40      minuts: ['minut', 'minuts', 'minuts'],
41      seconds: ['second', 'seconds', 'seconds']
42    };
43
44  if (htmlLang == 'ua' || htmlLang == 'ru') {
45    lang = {
46      days: ['день', 'дня', 'дней'],
47      hours: ['час', 'часа', 'часов'],
48      minuts: ['минута', 'минуты', 'минут'],
49      seconds: ['секунда', 'секунды', 'секунд']
50    };
51  };
52  
53
54  this.declension = function(number, one, two, five) { 
55    number = Math.abs(number);
56    number %= 100;
57    if (number >= 5 && number <= 20) {
58      return five;
59    }
60    number %= 10;
61    if (number == 1) {
62      return one;
63    }
64    if (number >= 2 && number <= 4) {
65      return two;
66    }
67    return five;
68  };
69  this.modifier_spellcount = function(num) { 
70    var num_null = num < 10 ? '0' : '';
71    if (_this.options.wrapChar) {
72      var num = (num_null + num).toString().split('');
73      var res = '';
74
75      for (i = 0; i < num.length; i++) {
76        res += '<' + settings.wrapChar + '>' + num[i] + '</' + settings.wrapChar + '>';
77      }
78
79      return res;
80    }
81    return num_null + num;
82  };
83
84  this.init = function(){ 
85    function timerStarter(element, data_end_time) { 
86      var timer, days, hours, minutes, seconds;
87
88      var interval;
89
90      var inner = data_end_time.replace(/[^0-9]/g," ").split(' ');
91
92      var inner_format = '%d.%M.%y %h:%m:%s'.replace(/[^%dMyhms]/g," ").split(' ');
93      var inner_array = [];
94
95      for (var i = 0; i < inner_format.length; i++) {
96        inner_array[inner_format[i]] = inner[i];
97      }
98
99      if (!inner_array['%y']) {
100        if(now > new Date(now.getFullYear(), inner_array['%M']-1, inner_array['%d'], inner_array['%h'],inner_array['%m'],inner_array['%s'])){
101          inner_array['%y'] = now.getFullYear() + 1;
102        }
103      }
104
105
106      var date_to = new Date(inner_array['%y'], (inner_array['%M']-1>=0?inner_array['%M']-1:now.getMonth()), inner_array['%d']||now.getDate()+1, inner_array['%h']||0, inner_array['%m']||0, inner_array['%s']||0);
107
108      function set_time() { 
109        now = new Date();
110        var time_intervar = date_to - now - ((-now.getTimezoneOffset()*60000) - ( ( (-now.getTimezoneOffset())/60 ) *3600*1000));
111        var days = Math.floor(time_intervar/3600/1000/24);
112        var hours = Math.floor(time_intervar/3600/1000) - Math.floor(time_intervar/3600/1000/24)*24;
113        var minuts = Math.floor(time_intervar/60/1000) - Math.floor(time_intervar/3600/1000)*60;
114        var seconds = Math.floor(time_intervar/1000) - Math.floor(time_intervar/60/1000)*60;
115
116        if (time_intervar > 0) { 
117          
118
119          if( _this.options.template ){
120            var outputFormat = _this.options.template;
121          }else {
122            var outputFormat =  '<span class="gr-timer-elem gr-timer-days"> <span class="gr-timer-number">%d </span> <em>' + timerDays + '</em> </span> <span class="gr-timer-delim"><svg class="gr-svg-icon"><use xlink:href="#icon_shop_timer_dots"></use></svg><svg class="gr-svg-icon gr_small_icon"><use xlink:href="#icon_shop_timer_dots_small"></use></svg></span> <span class="gr-timer-elem"> <span class="gr-timer-number">%h </span> <em>' + timerHours + '</em> </span> <span class="gr-timer-delim"><svg class="gr-svg-icon"><use xlink:href="#icon_shop_timer_dots"></use></svg><svg class="gr-svg-icon gr_small_icon"><use xlink:href="#icon_shop_timer_dots_small"></use></svg></span> <span class="gr-timer-elem"> <span class="gr-timer-number">%m </span> <em>' + timerMinutes + '</em> </span> <span class="gr-timer-delim"><svg class="gr-svg-icon"><use xlink:href="#icon_shop_timer_dots"></use></svg><svg class="gr-svg-icon gr_small_icon"><use xlink:href="#icon_shop_timer_dots_small"></use></svg></span> <span class="gr-timer-elem"> <span class="gr-timer-number">%s </span> <em>' + timerSeconds + '</em> </span>' ;
123
124          }
125          
126
127
128
129          daysName = _this.declension(days, lang.days[0], lang.days[1], lang.days[2]);
130          hoursName = _this.declension(hours, lang.hours[0], lang.hours[1], lang.hours[2]);
131          minutesName = _this.declension(minuts, lang.minuts[0], lang.minuts[1], lang.minuts[2]);
132          secondsName = _this.declension(seconds, lang.seconds[0], lang.seconds[1], lang.seconds[2]);
133          days = _this.modifier_spellcount(days);
134          hours = _this.modifier_spellcount(hours);
135          minuts = _this.modifier_spellcount(minuts);
136          seconds = _this.modifier_spellcount(seconds);
137
138          
139          time_output = outputFormat
140            .replace('%d', days)
141            .replace('%h', hours)
142            .replace('%m', minuts)
143            .replace('%s', seconds)
144            .replace(timerDays, daysName)
145            .replace(timerHours, hoursName)
146            .replace(timerMinutes, minutesName)
147            .replace(timerSeconds, secondsName);
148          
149
150          element.innerHTML = time_output; 
151
152          _this.options.onTimeChange(element); 
153
154        } else {
155          clearInterval(interval);
156          _this.options.onEnd(element); 
157          return;
158        }
159      }
160
161      interval = window.setInterval(set_time, _this.options.update_time); 
162    };
163
164    if( _this.options.element ){ 
165      var date_over = _this.options.element.getAttribute( _this.options.dataEndTimer ) ? _this.options.element.getAttribute( _this.options.dataEndTimer ) : _this.options.date_over;
166      if( !date_over ) { 
167        return; 
168      }
169    
170      timerStarter( _this.options.element, date_over ); 
171
172    }else {
173
174        let element_timer_items = document.querySelectorAll( _this.options.container ); 
175      
176
177        Array.prototype.forEach.call( element_timer_items, function( item , i){ 
178      var date_over = item.getAttribute( _this.options.dataEndTimer ) ? item.getAttribute( _this.options.dataEndTimer ) : _this.options.date_over
179      
180      if( date_over ) { 
181      
182              timerStarter( item, date_over ); 
183            }
184
185        });
186
187    };
188
189  };
190
191  this.init();
192
193};

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.