1// ------------------------------------------------------------------------------------------- 2// 3// AVIA Countdown 4// 5// @since 4.5.6 Countdown is UTC based to reflect different browser timezones 6// @since 5.5 flip clocks added 7// 8// ------------------------------------------------------------------------------------------- 9 10var aviaJS = aviaJS || {}; 11 12(function($) 13{ 14 "use strict"; 15 16 var _units = ['years','months','weeks','days','hours','minutes','seconds'], 17 _second = 1000, 18 _minute = _second * 60, 19 _hour = _minute * 60, 20 _day = _hour * 24, 21 _week = _day * 7, 22 23 getDaysInMonth = function( month, year ) 24 { 25 // January is 1 based 26 return new Date( year, month, 0 ).getDate(); 27 }, 28 29 splitStartDate = function( date ) 30 { 31 var result = { 32 year: date.getFullYear(), 33 month: date.getMonth() + 1, 34 day: date.getDate(), 35 hours: date.getHours(), 36 minutes: date.getMinutes(), 37 seconds: date.getSeconds() 38 }; 39 40 return result; 41 }, 42 43 getYears = function( start, endDate ) 44 { 45 var diff = endDate.getFullYear() - start.year; 46 47 if( diff > 0 ) 48 { 49 // take care of leap year 50 var check = new Date( start.year + diff, start.month - 1, start.day, start.hours, start.minutes, start.seconds ); 51 if( check > endDate ) 52 { 53 diff --; 54 } 55 } 56 57 return ( diff >= 0 ) ? diff : 0; 58 }, 59 60 getMonths = function( start, endDate ) 61 { 62 var endMonth = endDate.getMonth() + 1; 63 var diff = endMonth - start.month; 64 if( diff < 0 ) 65 { 66 diff = 12 - start.month + endMonth; 67 } 68 69 if( diff > 0 ) 70 { 71 // take care of leap year 72 var check = new Date( start.year, start.month - 1 + diff, start.day, start.hours, start.minutes, start.seconds ); 73 if( check > endDate ) 74 { 75 diff --; 76 } 77 } 78 79 return ( diff >= 0 ) ? diff : 0; 80 }, 81 82 getDays = function( start, endDate ) 83 { 84 var endDay = endDate.getDate(); 85 var diff = endDay - start.day; 86 if( diff < 0 ) 87 { 88 diff = getDaysInMonth( start.month, start.year ) - start.day + endDay; 89 } 90 91 if( diff > 0 ) 92 { 93 var check = new Date( start.year, start.month - 1, start.day + diff, start.hours, start.minutes, start.seconds ); 94 if( check > endDate ) 95 { 96 diff --; 97 } 98 } 99 100 return ( diff >= 0 ) ? diff : 0; 101 }, 102 103 getBetween = function( startDate, endDate ) 104 { 105 var start = splitStartDate( startDate ), 106 result = { 107 years: 0, 108 year_months: 0, 109 month_months: 0, 110 days: 0 111 }; 112 113 114 result.years = getYears( start, endDate ); 115 start.year += result.years; 116 117 result.year_months = getMonths( start, endDate ); 118 start.month += result.year_months; 119 120 result.days = getDays( start, endDate ); 121 start.day += result.days; 122 123 result.month_months = result.years * 12 + result.year_months; 124 125 return result; 126 }, 127 128 setLabelMinWidth = function( _self, reset ) 129 { 130 if( true === reset ) 131 { 132 for( let i in _self.time ) 133 { 134 if( typeof _self.update[i] == "object" && _self.update[i].label_container.length ) 135 { 136 _self.update[i].label_container.css( { "min-width": '' } ); 137 } 138 } 139 140 _self.labelMinWidth = 0; 141 142 return; 143 } 144 145 let newMin = _self.labelMinWidth; 146 147 for( let i in _self.time ) 148 { 149 if( typeof _self.update[i] == "object" && _self.update[i].label_container.length ) 150 { 151 let curr = _self.update[i].label_container.outerWidth( true ); 152 153 if( newMin < curr ) 154 { 155 newMin = curr; 156 } 157 } 158 } 159 160 if( newMin > _self.labelMinWidth ) 161 { 162 for( let i in _self.time ) 163 { 164 if( typeof _self.update[i] == "object" && _self.update[i].label_container.length ) 165 { 166 _self.update[i].label_container.css( { "min-width": newMin + "px" } ); 167 } 168 } 169 170 _self.labelMinWidth = newMin; 171 } 172 }, 173 174 ticker = function( _self ) 175 { 176 var tmLoc = new Date(), 177 _now = new Date( tmLoc.getTime() + tmLoc.getTimezoneOffset() * 60000 ), // get UTC time 178 _timestamp = _self.end - _now; 179 180 if( _timestamp <= 0 ) 181 { 182 if( _self.firstrun && ! _self.container.hasClass( 'av-finished-msg' ) ) 183 { 184 _timestamp = 0; 185 } 186 else 187 { 188 _self.container.addClass('av-countdown-finished'); 189 clearInterval( _self.countdown ); 190 191 return; 192 } 193 } 194 195 _self.time.years = 0; 196 _self.time.months = 0; 197 _self.time.weeks = Math.floor( _timestamp / _week ); 198 _self.time.days = Math.floor( ( _timestamp % _week ) / _day ); 199 _self.time.hours = Math.floor( ( _timestamp % _day ) / _hour ); 200 _self.time.minutes = Math.floor( ( _timestamp % _hour ) / _minute ); 201 _self.time.seconds = Math.floor( ( _timestamp % _minute ) / _second ); 202 203 var between = getBetween( _now, _self.end ); 204 205 switch( _self.data.maximum ) 206 { 207 case 1: 208 _self.time.seconds = Math.floor( _timestamp / _second ); 209 break; 210 case 2: 211 _self.time.minutes = Math.floor( _timestamp / _minute ); 212 break; 213 case 3: 214 _self.time.hours = Math.floor( _timestamp / _hour ); 215 break; 216 case 4: 217 _self.time.days = Math.floor( _timestamp / _day ); 218 break; 219 case 6: 220 _self.time.days = between.days; 221 _self.time.months = between.month_months; 222 break; 223 case 7: 224 _self.time.days = between.days; 225 _self.time.months = between.year_months; 226 _self.time.years = between.years; 227 break; 228 } 229 230 for( let i in _self.time ) 231 { 232 if( typeof _self.update[i] == "object" && _self.update[i].label_container.length ) 233 { 234 if(_self.firstrun || _self.oldtime[i] != _self.time[i]) 235 { 236 let labelkey = ( _self.time[i] === 1 ) ? "single" : "multi", 237 new_label = _self.update[i][labelkey], 238 old_label = _self.update[i].label_container.text(); 239 240 if( old_label != new_label ) 241 { 242 _self.update[i].label_container.text( new_label ); 243 } 244 245 if( _self.isFlipNumbers ) 246 { 247 if( _self.update[i].unitContainer.length ) 248 { 249 if( ! _self.firstrun ) 250 { 251 _self.update[i].backCard.attr( 'data-value', _self.oldtime[i] ); 252 _self.update[i].bottomCard.attr( 'data-value', _self.oldtime[i] ); 253 } 254 255 _self.update[i].topCard.text( _self.time[i] ); 256 _self.update[i].backBottomCard.attr( 'data-value', _self.time[i] ); 257 258 _self.update[i].unitContainer.removeClass( 'flip' ); 259 void _self.update[i].unitContainer[0].offsetWidth; 260 _self.update[i].unitContainer.addClass( 'flip' ); 261 } 262 } 263 else if( _self.isFlipClock ) 264 { 265 if( _self.update[i].unitContainer.length ) 266 { 267 if( ! _self.firstrun ) 268 { 269 _self.update[i].currentTop.text( _self.oldtime[i] ); 270 _self.update[i].currentBottom.text( _self.oldtime[i] ); 271 } 272 273 _self.update[i].nextTop.text( _self.time[i] ); 274 _self.update[i].nextBottom.text( _self.time[i] ); 275 276 _self.update[i].unitContainer.removeClass( 'flip' ); 277 void _self.update[i].unitContainer[0].offsetWidth; 278 _self.update[i].unitContainer.addClass( 'flip' ); 279 } 280 } 281 else 282 { 283 _self.update[i].time_container.text(_self.time[i]); 284 } 285 } 286 } 287 } 288 289 //show ticker 290 if( _self.firstrun ) 291 { 292 _self.container.addClass('av-countdown-active'); 293 } 294 295 _self.oldtime = $.extend( {}, _self.time ); 296 _self.firstrun = false;
297 298 if( ( _self.isFlipNumbers || _self.isFlipClock ) && _self.number_space ) 299 { 300 setTimeout( function() 301 { 302 setLabelMinWidth( _self ); 303 }, 50 ); 304 } 305 }; 306 307 308 $.fn.aviaCountdown = function( options ) 309 { 310 if( ! this.length ) 311 { 312 return; 313 } 314 315 return this.each( function() 316 { 317 var _self = {}; 318 319 _self.update = {}; 320 _self.time = {}; 321 _self.oldtime = {}; 322 _self.firstrun = true; 323 _self.container = $(this); 324 _self.data = _self.container.data(); 325 _self.end = new Date( _self.data.year, _self.data.month, _self.data.day, _self.data.hour, _self.data.minute ); 326 _self.isFlipNumbers = _self.container.hasClass('av-flip-numbers'); 327 _self.isFlipClock = _self.container.hasClass('av-flip-clock'); 328 _self.number_space = _self.container.hasClass('av-number-space-equal'); 329 _self.labelMinWidth = 0; 330 331 var onResize = function( e ) 332 { 333 if( _self.number_space ) 334 { 335 setLabelMinWidth( _self, true ); 336 } 337 }; 338 339 if( _self.data.timezone != '0' ) 340 { 341 _self.end = new Date( _self.end.getTime() - _self.data.timezone * 60000 ); 342 } 343 344 for( var i in _units ) 345 { 346 let unitContainer = _self.container.find('.av-countdown-' + _units[i] ); 347 348 _self.update[_units[i]] = { 349 unitContainer: unitContainer, 350 time_container: _self.container.find('.av-countdown-' + _units[i] + ' .av-countdown-time'), 351 label_container: _self.container.find('.av-countdown-' + _units[i] + ' .av-countdown-time-label') 352 }; 353 354 if( _self.update[_units[i]].label_container.length ) 355 { 356 _self.update[_units[i]].single = _self.update[_units[i]].label_container.data('label'); 357 _self.update[_units[i]].multi = _self.update[_units[i]].label_container.data('label-multi'); 358 } 359 360 if( _self.isFlipNumbers ) 361 { 362 _self.update[_units[i]].topCard = unitContainer.find('.card__top'); 363 _self.update[_units[i]].bottomCard = unitContainer.find('.card__bottom'); 364 _self.update[_units[i]].backCard = unitContainer.find('.card__back'); 365 _self.update[_units[i]].backBottomCard = unitContainer.find('.card__back .card__bottom'); 366 } 367 else if( _self.isFlipClock ) 368 { 369 _self.update[_units[i]].currentTop = unitContainer.find('.curr.top'); 370 _self.update[_units[i]].nextTop = unitContainer.find('.next.top'); 371 _self.update[_units[i]].nextBottom = unitContainer.find('.next.bottom'); 372 _self.update[_units[i]].currentBottom = unitContainer.find('.curr.bottom'); 373 } 374 } 375 376 ticker( _self ); 377 _self.countdown = setInterval( function(){ ticker( _self ); }, 1000 ); 378 379 window.addEventListener( 'resize', aviaJS.aviaJSHelpers.debounce( onResize.bind( this ), 50 ) ); 380 }); 381 }; 382 383}(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.