PageSourceSearch

https://paginas.solutions/builder/elements/scripts/jquery.jCounter-0.1.4.js

js paginas.solutions collected 2026-10-07 18:02:37 UTC 13,375 bytes, 346 lines download raw bytes

1/**********************************
2* jCounter Script v0.1.4 (beta)
3* Author: Catalin Berta
4* Official page and documentation: http://devingredients.com/jcounter
5* Licensed under the MIT license
6**********************************/
7;(function($,document,window,undefined) {
8	//once upon a time...
9	$.fn.jCounter = function(options,callback) {
10		var jCounterDirection = 'down'; // points out whether it should count down or up | handled via customRange setting
11		
12		var customRangeDownCount; //if true, it will tell countdown_proc() it's a down count and not an up count
13		var days,hours,minutes,seconds;
14		var endCounter = false; //stops jCounter if true
15		var eventDate; //time target (holds a number of seconds)
16		var pausedTime; //stores the time (in seconds) when pausing
17		var thisEl = this; //custom 'this' selector
18		var thisLength = this.length; //number of multiple elements per selector
19
20		var pluralLabels = new Array('DAYS','HOURS','MINUTES','SECONDS'); //plural labels - used for localization
21		var singularLabels = new Array('DAY','HOUR','MINUTE','SECOND');	//singular labels - used for localization
22
23		this.options = options; //stores jCounter's options parameter to verify against specified methods
24		this.version = '0.1.4';
25
26		//default settings
27		var settings = {
28			animation: null,
29			callback: null,
30			customDuration: null,
31			customRange: null,
32			date: null,
33			debugLog: false,
34			serverDateSource: '/builder/elements/scripts/dateandtime.php', //path to dateandtime.php file (i.e. http://my-domain.com/dateandtime.php)
35			format: 'dd:hh:mm:ss',
36			timezone: 'America/Sao_Paulo',
37			twoDigits: 'on'
38		};
39
40		//merge the settings with the options values
41		if (typeof options === 'object')
42		    $.extend(settings,options);
43
44	    //push the settings to applied elements (they're used by methods)
45		thisEl.data("userOptions", settings);
46
47		if(thisEl.data('userOptions').debugLog == true &&  window['console'] !== undefined ) {
48			var consoleLog = true;	//shows debug messages via console.log() if true
49		}
50
51		//METHODS
52		var jC_methods = {
53			//initialize
54			init : function() {
55				thisEl.each(function(i,el) {
56					initCounter(el);
57				});
58			},
59			//pause method: $.jCounter('pause')
60			pause : function() {
61				if(consoleLog) { console.log("(jC) Activity: Counter paused."); }
62				endCounter = true;
63				return thisEl.each(function(i,el) {
64					clearInterval($(el).data("jC_interval"));
65				});
66			},
67			//stop method: $.jCounter('stop')
68			stop : function() {
69				//if(consoleLog) { console.log("(jC) Activity: Counter stopped."); }
70				endCounter = true;
71				return thisEl.each(function(i,el) {
72					clearInterval($(el).data("jC_interval"));
73					$(el).removeData("jC_pausedTime");
74					resetHTMLCounter(el);
75				});
76			},
77			//reset method: $.jCounter('reset')
78			reset : function() {
79				if(consoleLog) { console.log("(jC) Activity: Counter reset."); }
80				return thisEl.each(function(i,el) {
81					clearInterval($(el).data("jC_interval"));
82					resetHTMLCounter(el);
83					initCounter(el);
84				});
85			},
86			//start method: $.jCounter('start')
87			
88			start : function() {
89				if(consoleLog) { console.log("(jC) Activity: Counter started."); }
90				return thisEl.each(function(i,el) {
91					pausedTime = $(el).data("jC_pausedTime");
92					endCounter = false;
93					clearInterval($(el).data("jC_interval"));
94					initCounter(el);
95				});
96			}
97		}
98		
99	    //checks whether customDuration is used
100		if(thisEl.data("userOptions") != undefined && thisEl.data("userOptions").customDuration) {
101			if(!isNaN(thisEl.data("userOptions").customDuration)) {
102				var customDuration = true;
103			} else {
104				var customDuration = false;
105				if(consoleLog) { console.log("(jC) Error: The customDuration value is not a number! NOTE: 'customDuration' accepts a number of seconds."); }
106			}
107		}
108		
109		//checks whether customRange is used
110		if (thisEl.data("userOptions") != undefined && thisEl.data("userOptions").customRange) {
111			var customRangeValues = thisEl.data("userOptions").customRange.split(":");
112			var rangeVal0 = parseInt(customRangeValues[0]);
113			var rangeVal1 = parseInt(customRangeValues[1]);
114			if(!isNaN(rangeVal0) && !isNaN(rangeVal1)) {
115				var customRange = true;
116				if(rangeVal0 > rangeVal1) {
117					var customRangeDownCount = true;
118				} else {
119					var customRangeDownCount = false;
120					jCounterDirection = 'up';
121				}
122			} else {
123				var customRange = false;
124				if(consoleLog) { console.log("(jC) Error: The customRange value is not a valid range! Example: customRange: '0:30' or '30:0'"); }
125			}
126		}
127
128		//checks whether animation is set to slide
129		if (thisEl.data("userOptions") != undefined && thisEl.data("userOptions").animation == 'slide') {
130			thisEl.data("jCanimation","slide");
131		}
132
133		//FUNCTIONS
134		
135		//jCounter initializer
136		function initCounter(el) {
137			if(customDuration) {
138				if (pausedTime) {
139					if (!isNaN(pausedTime)) {
140						eventDate = Math.round(pausedTime);
141					}
142				} else {
143					eventDate = Math.round($(el).data("userOptions").customDuration);
144				}
145				currentTime = 0;
146				countdown_proc(currentTime,el);
147				$(el).data("jC_interval", setInterval(function(){
148					if(endCounter == false) {
149						currentTime = parseInt(currentTime) + 1;
150						countdown_proc(currentTime,el)
151					}				
152				},1000));
153			} else if(customRange) {
154				eventDate = Math.round(customRangeValues[1]);
155				if (pausedTime) {
156					if (!isNaN(pausedTime)) {
157						var currentTime = eventDate - pausedTime;
158					}
159				} else {
160					var currentTime = Math.round(customRangeValues[0]);
161				}
162				countdown_proc(currentTime,el);
163				$(el).data("jC_interval", setInterval(function(){
164					if(endCounter == false) {
165						var ifRangeDownCount = (customRangeDownCount) ? currentTime = parseInt(currentTime) - 1 : currentTime = parseInt(currentTime) + 1;
166						countdown_proc(currentTime,el);
167					}				
168				},1000));
169			} else {
170				eventDate = Date.parse($(el).data("userOptions").date) / 1000;
171				dateSource = thisEl.data("userOptions").serverDateSource + '?timezone=' + thisEl.data("userOptions").timezone + '&callback=?';
172				$.ajax({
173                	url: dateSource,
174	                dataType : 'json',
175	                data : {},
176	                success : function(data, textStatus){
177						var currentDate = Date.parse(data.currentDate) / 1000;
178						startCounter(currentDate,el);
179	                },
180	                error : function(){
181						if(consoleLog) { console.log("(jC) Error: Couldn't find dateandtime.php from serverDateSource: " + thisEl.data('userOptions').serverDateSource + "\n(jC) - Make sure the path is correct! \n(jC) - Now using the client-side time (not recommended).") }
182						var currentDate = Math.floor($.now() / 1000);
183						startCounter(currentDate,el);
184	                }
185            	});
186			}
187		}
188
189		function startCounter(currentDate,el) {
190			countdown_proc(currentDate,el);
191			if (eventDate > currentDate) {
192				$(el).data("jC_interval", setInterval(function(){
193					if(endCounter == false) {
194						currentDate = parseInt(currentDate) + 1;
195						countdown_proc(currentDate,el)
196					}				
197				},1000));
198			} else {
199				resetHTMLCounter(el)
200			}
201		}
202
203		//jCslider - adds the slide effect layer
204		//Note: this requires a jCounter slide-ready theme! (i.e. iOS dark or iOS light)
205		function jCslider(el,unitClass,timeUnit,eventDate,duration) {
206			$(el).find(unitClass + " u").each(function(i,el) {
207				var twoDigits = (thisEl.data("userOptions").twoDigits == 'on') ? '0' : '';
208				var newIndex = (jCounterDirection == 'up') ? newIndex = -i : newIndex = i;		
209				currNo = parseInt(timeUnit,10) + (newIndex);
210				if (String(parseInt(timeUnit,10)).length >= 2) { 
211					$(el).text(parseInt(timeUnit,10) + (newIndex))
212				} else if(String(parseInt(timeUnit,10)).length == 1 && currNo == 10) {
213					$(el).text(parseInt(timeUnit,10) + (newIndex))
214				} else {
215					$(el).text(twoDigits + (parseInt(timeUnit,10) + (newIndex)));
216				}
217			})
218			$(el).find(unitClass).animate({
219				top: '0.15em'
220			},200, function() {
221				$(el).find(unitClass + " u:eq(1)").remove();
222				$(el).find(unitClass).prepend('<u></u>');
223				$(el).find(unitClass).css({'top':'-1.24em'})					
224			});
225		}
226
227		//resets jCounter's HTML values to 0 or 00, based on the twoDigits setting
228		function resetHTMLCounter(el) {
229			if(thisEl.data("userOptions").twoDigits == 'on') {
230				$(el).find(".days,.hours,.minutes,.seconds").text('00');
231			} else if(thisEl.data("userOptions").twoDigits == 'off') {
232				$(el).find(".days,.hours,.minutes,.seconds").text('0');
233			}
234			if(thisEl.data("jCanimation") == 'slide') {
235				$(el).find(".daysSlider u,.hoursSlider u,.minutesSlider u,.secondsSlider u").text('00');
236			}
237		}
238
239		//main jCounter processor
240		function countdown_proc(duration,el) {
241			//check if the counter needs to count down or up
242			if(customRangeDownCount) {
243				if(eventDate >= duration) {
244					clearInterval($(el).data("jC_interval"));
245					if(thisEl.data("userOptions").callback) {
246						thisEl.data("userOptions").callback.call(this);
247					}
248				}
249
250			} else {
251				if(eventDate <= duration) {
252					clearInterval($(el).data("jC_interval"));
253					if(thisEl.data("userOptions").callback) {
254						thisEl.data("userOptions").callback.call(this);
255					}
256				}
257			}
258			
259			//if customRange is used, update the seconds variable
260			var seconds = (customRange) ? duration : eventDate - duration;
261
262			var thisInstanceFormat = thisEl.data("userOptions").format;
263			
264			//calculate seconds into days,hours,minutes,seconds
265			//if dd (days) is specified in the format setting (i.e. format: 'dd:hh:mm:ss')
266			if(thisInstanceFormat.indexOf('dd') != -1)  {
267				var days = Math.floor(seconds / (60 * 60 * 24)); //calculate the number of days
268				seconds -= days * 60 * 60 * 24; //update the seconds variable with no. of days removed
269			}
270			//if hh (hours) is specified
271			if(thisInstanceFormat.indexOf('hh') != -1)  {
272				var hours = Math.floor(seconds / (60 * 60));
273				seconds -= hours * 60 * 60; //update the seconds variable with no. of hours removed
274			}
275			//if mm (minutes) is specified
276			if(thisInstanceFormat.indexOf('mm') != -1)  {
277				var minutes = Math.floor(seconds / 60);
278				seconds -= minutes * 60; //update the seconds variable with no. of minutes removed
279			}
280			//if ss (seconds) is specified
281			if(thisInstanceFormat.indexOf('ss') == -1)  {
282				seconds -= seconds; //if ss is unspecified in format, update the seconds variable to 0;
283			}
284
285			//conditional Ss
286			//updates the plural and singular labels accordingly
287			if (days == 1) { $(el).find(".textDays").text(singularLabels[0]); } else { $(el).find(".textDays").text(pluralLabels[0]); }
288			if (hours == 1) { $(el).find(".textHours").text(singularLabels[1]); } else { $(el).find(".textHours").text(pluralLabels[1]); }
289			if (minutes == 1) { $(el).find(".textMinutes").text(singularLabels[2]); } else { $(el).find(".textMinutes").text(pluralLabels[2]); }
290			if (seconds == 1) { $(el).find(".textSeconds").text(singularLabels[3]); } else { $(el).find(".textSeconds").text(pluralLabels[3]); }
291			
292			//twoDigits ON setting
293			//if the twoDigits setting is set to ON, jCounter will always diplay a minimum number of 2 digits
294			if(thisEl.data("userOptions").twoDigits == 'on') {
295				days = (String(days).length >= 2) ? days : "0" + days;
296				hours = (String(hours).length >= 2) ? hours : "0" + hours;
297				minutes = (String(minutes).length >= 2) ? minutes : "0" + minutes;
298				seconds = (String(seconds).length >= 2) ? seconds : "0" + seconds;
299			}
300
301			//updates the jCounter's html values
302			if(!isNaN(eventDate)) {
303				$(el).find(".days").text(days);
304				$(el).find(".hours").text(hours);
305				$(el).find(".minutes").text(minutes);
306				$(el).find(".seconds").text(seconds);
307
308
309				if(thisEl.data("jCanimation") == 'slide') {
310					$(el).find(".daysSlider u:eq(1)").text(days);
311					$(el).find(".hoursSlider u:eq(1)").text(hours);
312					$(el).find(".minutesSlider u:eq(1)").text(minutes);
313					$(el).find(".secondsSlider u:eq(1)").text(seconds);
314					jCslider(el,'.secondsSlider',seconds,eventDate,duration); 
315					if(parseInt(seconds,10) == 59) { 
316						jCslider(el,'.minutesSlider',minutes,eventDate,duration) 
317						if(parseInt(minutes,10) == 59) { 
318							jCslider(el,'.hoursSlider',hours,eventDate,duration) 
319							if(parseInt(hours,10) == 23) { 
320								jCslider(el,'.daysSlider',days,eventDate,duration) 
321							}
322						}
323					}	
324				}
325			} else { 
326				if(consoleLog) { console.log("(jC) Error: Invalid date! Here's an example: 01 January 1970 12:00:00"); }
327				clearInterval($(el).data("jC_interval"));
328			}
329			//stores the remaining time when pausing jCounter
330			$(el).data("jC_pausedTime", eventDate-duration);
331		}
332		
333		
334		
335		//method calling logic
336		if ( jC_methods[this.options] ) {
337			return jC_methods[ this.options ].apply( this, Array.prototype.slice.call( arguments, 1 ));
338		} else if ( typeof this.options === 'object' || ! this.options ) {
339			return jC_methods.init.apply( this, arguments );
340		} else {
341			console.log('(jC) Error: Method >>> ' +  this.options + ' <<< does not exist.' );
342		} 
343
344	}
345	//the end;
346}) (jQuery,document,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.