vendor: 2,387 bytes, lines 1-63
1/** 2 * Basic structure: TC_Class is the public class that is returned upon being called 3 * 4 * So, if you do 5 * var tc = $(".timer").TimeCircles(); 6 * 7 * tc will contain an instance of the public TimeCircles class. It is important to 8 * note that TimeCircles is not chained in the conventional way, check the 9 * documentation for more info on how TimeCircles can be chained. 10 * 11 * After being called/created, the public TimerCircles class will then- for each element 12 * within it's collection, either fetch or create an instance of the private class. 13 * Each function called upon the public class will be forwarded to each instance 14 * of the private classes within the relevant element collection 15 **/ 16(function($) { 17 18 var useWindow = window; 19 20 // From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys 21 if (!Object.keys) { 22 Object.keys = (function() { 23 'use strict'; 24 var hasOwnProperty = Object.prototype.hasOwnProperty, 25 hasDontEnumBug = !({toString: null}).propertyIsEnumerable('toString'), 26 dontEnums = [ 27 'toString', 28 'toLocaleString', 29 'valueOf', 30 'hasOwnProperty', 31 'isPrototypeOf', 32 'propertyIsEnumerable', 33 'constructor' 34 ], 35 dontEnumsLength = dontEnums.length; 36 37 return function(obj) { 38 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { 39 throw new TypeError('Object.keys called on non-object'); 40 } 41 42 var result = [], prop, i; 43 44 for (prop in obj) { 45 if (hasOwnProperty.call(obj, prop)) { 46 result.push(prop); 47 } 48 } 49 50 if (hasDontEnumBug) { 51 for (i = 0; i < dontEnumsLength; i++) { 52 if (hasOwnProperty.call(obj, dontEnums[i])) { 53 result.push(dontEnums[i]); 54 } 55 } 56 } 57 return result; 58 }; 59 }()); 60 } 61 62 // Used to disable some features on IE8 63 var limited_mode = false;
64 var tick_duration = 200; // in ms 65 66 var debug = (location.hash === "#debug"); 67 function debug_log(msg) { 68 if (debug) { 69 console.log(msg); 70 } 71 } 72 73 var allUnits = ["Days", "Hours", "Minutes", "Seconds"]; 74 var nextUnits = { 75 Seconds: "Minutes", 76 Minutes: "Hours", 77 Hours: "Days", 78 Days: "Years" 79 }; 80 var secondsIn = { 81 Seconds: 1, 82 Minutes: 60, 83 Hours: 3600, 84 Days: 86400, 85 Months: 2678400, 86 Years: 31536000 87 }; 88 89 /** 90 * Converts hex color code into object containing integer values for the r,g,b use 91 * This function (hexToRgb) originates from: 92 * http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb 93 * @param {string} hex color code 94 */ 95 function hexToRgb(hex) { 96 // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") 97 var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; 98 hex = hex.replace(shorthandRegex, function(m, r, g, b) { 99 return r + r + g + g + b + b; 100 }); 101 102 var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); 103 return result ? { 104 r: parseInt(result[1], 16), 105 g: parseInt(result[2], 16), 106 b: parseInt(result[3], 16) 107 } : null; 108 } 109 110 function isCanvasSupported() { 111 var elem = document.createElement('canvas'); 112 return !!(elem.getContext && elem.getContext('2d')); 113 } 114 115 /** 116 * Function s4() and guid() originate from: 117 * http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript 118 */ 119 function s4() { 120 return Math.floor((1 + Math.random()) * 0x10000) 121 .toString(16) 122 .substring(1); 123 } 124 125 /** 126 * Creates a unique id 127 * @returns {String} 128 */ 129 function guid() { 130 return s4() + s4() + '-' + s4() + '-' + s4() + '-' + 131 s4() + '-' + s4() + s4() + s4(); 132 } 133 134 /** 135 * Array.prototype.indexOf fallback for IE8 136 * @param {Mixed} mixed 137 * @returns {Number} 138 */ 139 if (!Array.prototype.indexOf) { 140 Array.prototype.indexOf = function(elt /*, from*/) 141 { 142 var len = this.length >>> 0; 143 144 var from = Number(arguments[1]) || 0; 145 from = (from < 0) 146 ? Math.ceil(from) 147 : Math.floor(from); 148 if (from < 0) 149 from += len; 150 151 for (; from < len; from++) 152 { 153 if (from in this && 154 this[from] === elt) 155 return from; 156 } 157 return -1; 158 }; 159 } 160 161 function parse_date(str) { 162 var match = str.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{1,2}:[0-9]{2}:[0-9]{2}$/); 163 if (match !== null && match.length > 0) { 164 var parts = str.split(" "); 165 var date = parts[0].split("-"); 166 var time = parts[1].split(":"); 167 return new Date(date[0], date[1] - 1, date[2], time[0], time[1], time[2]); 168 } 169 // Fallback for different date formats 170 var d = Date.parse(str); 171 if (!isNaN(d)) 172 return d; 173 d = Date.parse(str.replace(/-/g, '/').replace('T', ' ')); 174 if (!isNaN(d)) 175 return d; 176 // Cant find anything 177 return new Date(); 178 } 179 180 function parse_times(diff, old_diff, total_duration, units, floor) { 181 var raw_time = {}; 182 var raw_old_time = {}; 183 var time = {}; 184 var pct = {}; 185 var old_pct = {}; 186 var old_time = {}; 187 188 var greater_unit = null; 189 for(var i = 0; i < units.length; i++) { 190 var unit = units[i]; 191 var maxUnits; 192 193 if (greater_unit === null) { 194 maxUnits = total_duration / secondsIn[unit]; 195 } 196 else { 197 maxUnits = secondsIn[greater_unit] / secondsIn[unit]; 198 } 199 200 var curUnits = (diff / secondsIn[unit]); 201 var oldUnits = (old_diff / secondsIn[unit]); 202 203 if(floor) { 204 if(curUnits > 0) curUnits = Math.floor(curUnits); 205 else curUnits = Math.ceil(curUnits); 206 if(oldUnits > 0) oldUnits = Math.floor(oldUnits); 207 else oldUnits = Math.ceil(oldUnits); 208 } 209 210 if (unit !== "Days") { 211 curUnits = curUnits % maxUnits; 212 oldUnits = oldUnits % maxUnits; 213 } 214 215 raw_time[unit] = curUnits; 216 time[unit] = Math.abs(curUnits); 217 raw_old_time[unit] = oldUnits; 218 old_time[unit] = Math.abs(oldUnits); 219 pct[unit] = Math.abs(curUnits) / maxUnits; 220 old_pct[unit] = Math.abs(oldUnits) / maxUnits; 221 222 greater_unit = unit; 223 } 224 225 return { 226 raw_time: raw_time, 227 raw_old_time: raw_old_time, 228 time: time, 229 old_time: old_time, 230 pct: pct, 231 old_pct: old_pct 232 }; 233 } 234 235 var TC_Instance_List = {}; 236 function updateUsedWindow() { 237 if(typeof useWindow.TC_Instance_List !== "undefined") {
238 TC_Instance_List = useWindow.TC_Instance_List; 239 } 240 else { 241 useWindow.TC_Instance_List = TC_Instance_List; 242 } 243 initializeAnimationFrameHandler(useWindow); 244 }; 245 246 function initializeAnimationFrameHandler(w) { 247 var vendors = ['webkit', 'moz']; 248 for (var x = 0; x < vendors.length && !w.requestAnimationFrame; ++x) { 249 w.requestAnimationFrame = w[vendors[x] + 'RequestAnimationFrame']; 250 w.cancelAnimationFrame = w[vendors[x] + 'CancelAnimationFrame']; 251 } 252 253 if (!w.requestAnimationFrame || !w.cancelAnimationFrame) { 254 w.requestAnimationFrame = function(callback, element, instance) { 255 if (typeof instance === "undefined") 256 instance = {data: {last_frame: 0}}; 257 var currTime = new Date().getTime(); 258 var timeToCall = Math.max(0, 16 - (currTime - instance.data.last_frame)); 259 var id = w.setTimeout(function() { 260 callback(currTime + timeToCall); 261 }, timeToCall); 262 instance.data.last_frame = currTime + timeToCall; 263 return id; 264 }; 265 w.cancelAnimationFrame = function(id) { 266 clearTimeout(id); 267 }; 268 } 269 }; 270 271 272 var TC_Instance = function(element, options) { 273 this.element = element; 274 this.container; 275 this.listeners = null; 276 this.data = { 277 paused: false, 278 last_frame: 0, 279 animation_frame: null, 280 interval_fallback: null, 281 timer: false, 282 total_duration: null, 283 prev_time: null, 284 drawn_units: [], 285 text_elements: { 286 Days: null, 287 Hours: null, 288 Minutes: null, 289 Seconds: null 290 }, 291 attributes: { 292 canvas: null, 293 context: null, 294 item_size: null, 295 line_width: null, 296 radius: null, 297 outer_radius: null 298 }, 299 state: { 300 fading: { 301 Days: false, 302 Hours: false, 303 Minutes: false, 304 Seconds: false 305 } 306 } 307 }; 308 309 this.config = null; 310 this.setOptions(options); 311 this.initialize(); 312 }; 313 314 TC_Instance.prototype.clearListeners = function() { 315 this.listeners = { all: [], visible: [] }; 316 }; 317 318 TC_Instance.prototype.addTime = function(seconds_to_add) { 319 if(this.data.attributes.ref_date instanceof Date) { 320 var d = this.data.attributes.ref_date; 321 d.setSeconds(d.getSeconds() + seconds_to_add); 322 } 323 else if(!isNaN(this.data.attributes.ref_date)) { 324 this.data.attributes.ref_date += (seconds_to_add * 1000); 325 } 326 }; 327 328 TC_Instance.prototype.initialize = function(clear_listeners) { 329 // Initialize drawn units 330 this.data.drawn_units = []; 331 for(var i = 0; i < Object.keys(this.config.time).length; i++) { 332 var unit = Object.keys(this.config.time)[i]; 333 if (this.config.time[unit].show) { 334 this.data.drawn_units.push(unit); 335 } 336 } 337 338 // Avoid stacking 339 $(this.element).children('div.time_circles').remove(); 340 341 if (typeof clear_listeners === "undefined") 342 clear_listeners = true; 343 if (clear_listeners || this.listeners === null) { 344 this.clearListeners(); 345 } 346 this.container = $("<div>"); 347 this.container.addClass('time_circles'); 348 this.container.appendTo(this.element); 349 350 // Determine the needed width and height of TimeCircles 351 var height = this.element.offsetHeight; 352 var width = this.element.offsetWidth; 353 if (height === 0) 354 height = $(this.element).height(); 355 if (width === 0) 356 width = $(this.element).width(); 357 358 if (height === 0 && width > 0) 359 height = width / this.data.drawn_units.length; 360 else if (width === 0 && height > 0) 361 width = height * this.data.drawn_units.length; 362 363 // Create our canvas and set it to the appropriate size 364 var canvasElement = document.createElement('canvas'); 365 canvasElement.width = width; 366 canvasElement.height = height; 367 368 // Add canvas elements 369 this.data.attributes.canvas = $(canvasElement); 370 this.data.attributes.canvas.appendTo(this.container); 371 372 // Check if the browser has browser support 373 var canvasSupported = isCanvasSupported(); 374 // If the browser doesn't have browser support, check if explorer canvas is loaded 375 // (A javascript library that adds canvas support to browsers that don't have it) 376 if(!canvasSupported && typeof G_vmlCanvasManager !== "undefined") { 377 G_vmlCanvasManager.initElement(canvasElement); 378 limited_mode = true; 379 canvasSupported = true; 380 } 381 if(canvasSupported) { 382 this.data.attributes.context = canvasElement.getContext('2d'); 383 } 384 385 this.data.attributes.item_size = Math.min(width / this.data.drawn_units.length, height); 386 this.data.attributes.line_width = this.data.attributes.item_size * this.config.fg_width; 387 this.data.attributes.radius = ((this.data.attributes.item_size * 0.8) - this.data.attributes.line_width) / 2; 388 this.data.attributes.outer_radius = this.data.attributes.radius + 0.5 * Math.max(this.data.attributes.line_width, this.data.attributes.line_width * this.config.bg_width); 389 390 // Prepare Time Elements 391 var i = 0; 392 for (var key in this.data.text_elements) { 393 if (!this.config.time[key].show) 394 continue; 395 396 var textElement = $("<div>"); 397 textElement.addClass('textDiv_' + key); 398 textElement.css("top", Math.round(0.35 * this.data.attributes.item_size)); 399 textElement.css("left", Math.round(i++ * this.data.attributes.item_size)); 400 textElement.css("width", this.data.attributes.item_size); 401 textElement.appendTo(this.container); 402 403 var headerElement = $("<h4>"); 404 headerElement.text(this.config.time[key].text); // Options 405 headerElement.css("font-size", Math.round(this.config.text_size * this.data.attributes.item_size)); 406 headerElement.css("line-height", Math.round(this.config.text_size * this.data.attributes.item_size) + "px"); 407 headerElement.appendTo(textElement); 408 409 var numberElement = $("<span>"); 410 numberElement.css("font-size", Math.round(3 * this.config.text_size * this.data.attributes.item_size)); 411 numberElement.css("line-height", Math.round(this.config.text_size * this.data.attributes.item_size) + "px"); 412 numberElement.appendTo(textElement); 413 414 this.data.text_elements[key] = numberElement; 415 } 416 417 this.start(); 418 if (!this.config.start) { 419 this.data.paused = true; 420 } 421 422 // Set up interval fallback 423 var _this = this; 424 this.data.interval_fallback = useWindow.setInterval(function(){ 425 _this.update.call(_this, true); 426 }, 100); 427 }; 428 429 TC_Instance.prototype.update = function(nodraw) { 430 if(typeof nodraw === "undefined") { 431 nodraw = false;
432 } 433 else if(nodraw && this.data.paused) { 434 return; 435 } 436 437 if(limited_mode) { 438 //Per unit clearing doesn't work in IE8 using explorer canvas, so do it in one time. The downside is that radial fade cant be used 439 this.data.attributes.context.clearRect(0, 0, this.data.attributes.canvas[0].width, this.data.attributes.canvas[0].hright); 440 } 441 var diff, old_diff; 442 443 var prevDate = this.data.prev_time; 444 var curDate = new Date(); 445 this.data.prev_time = curDate; 446 447 if (prevDate === null) 448 prevDate = curDate; 449 450 // If not counting past zero, and time < 0, then simply draw the zero point once, and call stop 451 452 if (!this.config.count_past_zero) { 453 if (curDate > this.data.attributes.ref_date) { 454 for(var i = 0; i < this.data.drawn_units.length; i++) { 455 var key = this.data.drawn_units[i]; 456 457 // Set the text value 458 this.data.text_elements[key].text("0"); 459 var x = (i * this.data.attributes.item_size) + (this.data.attributes.item_size / 2); 460 var y = this.data.attributes.item_size / 2; 461 var color = this.config.time[key].color; 462 this.drawArc(x, y, color, 0); 463 } 464 this.stop(); 465 $('#countdown').hide(); 466 return; 467 } 468 } 469 470 // Compare current time with reference 471 diff = (this.data.attributes.ref_date - curDate) / 1000; 472 old_diff = (this.data.attributes.ref_date - prevDate) / 1000; 473 474 var floor = this.config.animation !== "smooth"; 475 476 var visible_times = parse_times(diff, old_diff, this.data.total_duration, this.data.drawn_units, floor); 477 var all_times = parse_times(diff, old_diff, secondsIn["Years"], allUnits, floor); 478 479 var i = 0; 480 var j = 0; 481 var lastKey = null; 482 483 var cur_shown = this.data.drawn_units.slice(); 484 for (var i in allUnits) { 485 var key = allUnits[i]; 486 487 // Notify (all) listeners 488 if (Math.floor(all_times.raw_time[key]) !== Math.floor(all_times.raw_old_time[key])) { 489 this.notifyListeners(key, Math.floor(all_times.time[key]), Math.floor(diff), "all"); 490 } 491 492 if (cur_shown.indexOf(key) < 0) 493 continue; 494 495 // Notify (visible) listeners 496 if (Math.floor(visible_times.raw_time[key]) !== Math.floor(visible_times.raw_old_time[key])) { 497 this.notifyListeners(key, Math.floor(visible_times.time[key]), Math.floor(diff), "visible"); 498 } 499 500 if(!nodraw) { 501 // Set the text value 502 this.data.text_elements[key].text(Math.floor(Math.abs(visible_times.time[key]))); 503 504 var x = (j * this.data.attributes.item_size) + (this.data.attributes.item_size / 2); 505 var y = this.data.attributes.item_size / 2; 506 var color = this.config.time[key].color; 507 508 if (this.config.animation === "smooth") { 509 if (lastKey !== null && !limited_mode) { 510 if (Math.floor(visible_times.time[lastKey]) > Math.floor(visible_times.old_time[lastKey])) { 511 this.radialFade(x, y, color, 1, key); 512 this.data.state.fading[key] = true; 513 } 514 else if (Math.floor(visible_times.time[lastKey]) < Math.floor(visible_times.old_time[lastKey])) { 515 this.radialFade(x, y, color, 0, key); 516 this.data.state.fading[key] = true; 517 } 518 } 519 if (!this.data.state.fading[key]) { 520 this.drawArc(x, y, color, visible_times.pct[key]); 521 } 522 } 523 else { 524 this.animateArc(x, y, color, visible_times.pct[key], visible_times.old_pct[key], (new Date()).getTime() + tick_duration); 525 } 526 } 527 lastKey = key; 528 j++; 529 } 530 531 // Dont request another update if we should be paused 532 if(this.data.paused || nodraw) { 533 return; 534 } 535 536 // We need this for our next frame either way 537 var _this = this; 538 var update = function() { 539 _this.update.call(_this); 540 }; 541 542 // Either call next update immediately, or in a second 543 if (this.config.animation === "smooth") { 544 // Smooth animation, Queue up the next frame
545 this.data.animation_frame = useWindow.requestAnimationFrame(update, _this.element, _this); 546 } 547 else { 548 // Tick animation, Don't queue until very slightly after the next second happens 549 var delay = (diff % 1) * 1000; 550 if (delay < 0) 551 delay = 1000 + delay; 552 delay += 50; 553 554 _this.data.animation_frame = useWindow.setTimeout(function() { 555 _this.data.animation_frame = useWindow.requestAnimationFrame(update, _this.element, _this); 556 }, delay); 557 } 558 }; 559 560 TC_Instance.prototype.animateArc = function(x, y, color, target_pct, cur_pct, animation_end) { 561 if (this.data.attributes.context === null) 562 return; 563 564 var diff = cur_pct - target_pct; 565 if (Math.abs(diff) > 0.5) { 566 if (target_pct === 0) { 567 this.radialFade(x, y, color, 1); 568 } 569 else { 570 this.radialFade(x, y, color, 0); 571 } 572 } 573 else { 574 var progress = (tick_duration - (animation_end - (new Date()).getTime())) / tick_duration; 575 if (progress > 1) 576 progress = 1; 577 578 var pct = (cur_pct * (1 - progress)) + (target_pct * progress); 579 this.drawArc(x, y, color, pct); 580 581 //var show_pct = 582 if (progress >= 1) 583 return; 584 var _this = this; 585 useWindow.requestAnimationFrame(function() { 586 _this.animateArc(x, y, color, target_pct, cur_pct, animation_end); 587 }, this.element); 588 } 589 }; 590 591 TC_Instance.prototype.drawArc = function(x, y, color, pct) { 592 if (this.data.attributes.context === null) 593 return; 594 595 var clear_radius = Math.max(this.data.attributes.outer_radius, this.data.attributes.item_size / 2); 596 if(!limited_mode) { 597 this.data.attributes.context.clearRect( 598 x - clear_radius, 599 y - clear_radius, 600 clear_radius * 2, 601 clear_radius * 2 602 ); 603 } 604 605 if (this.config.use_background) { 606 this.data.attributes.context.beginPath(); 607 this.data.attributes.context.arc(x, y, this.data.attributes.radius, 0, 2 * Math.PI, false); 608 this.data.attributes.context.lineWidth = this.data.attributes.line_width * this.config.bg_width; 609 610 // line color 611 this.data.attributes.context.strokeStyle = this.config.circle_bg_color; 612 this.data.attributes.context.stroke(); 613 } 614 615 // Direction 616 var startAngle, endAngle, counterClockwise; 617 var defaultOffset = (-0.5 * Math.PI); 618 var fullCircle = 2 * Math.PI; 619 startAngle = defaultOffset + (this.config.start_angle / 360 * fullCircle); 620 var offset = (2 * pct * Math.PI); 621 622 if (this.config.direction === "Both") { 623 counterClockwise = false; 624 startAngle -= (offset / 2); 625 endAngle = startAngle + offset; 626 } 627 else { 628 if (this.config.direction === "Clockwise") { 629 counterClockwise = false; 630 endAngle = startAngle + offset; 631 } 632 else { 633 counterClockwise = true; 634 endAngle = startAngle - offset; 635 } 636 } 637 638 this.data.attributes.context.beginPath(); 639 this.data.attributes.context.arc(x, y, this.data.attributes.radius, startAngle, endAngle, counterClockwise); 640 this.data.attributes.context.lineWidth = this.data.attributes.line_width; 641 642 // line color 643 this.data.attributes.context.strokeStyle = color; 644 this.data.attributes.context.stroke(); 645 }; 646 647 TC_Instance.prototype.radialFade = function(x, y, color, from, key) { 648 // TODO: Make fade_time option 649 var rgb = hexToRgb(color); 650 var _this = this; // We have a few inner scopes here that will need access to our instance 651 652 var step = 0.2 * ((from === 1) ? -1 : 1); 653 var i; 654 for (i = 0; from <= 1 && from >= 0; i++) { 655 // Create inner scope so our variables are not changed by the time the Timeout triggers 656 (function() {
657 var delay = 50 * i; 658 var rgba = "rgba(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ", " + (Math.round(from * 10) / 10) + ")"; 659 useWindow.setTimeout(function() { 660 _this.drawArc(x, y, rgba, 1); 661 }, delay); 662 }()); 663 from += step; 664 } 665 if (typeof key !== undefined) { 666 useWindow.setTimeout(function() { 667 _this.data.state.fading[key] = false; 668 }, 50 * i); 669 } 670 }; 671 672 TC_Instance.prototype.timeLeft = function() { 673 if (this.data.paused && typeof this.data.timer === "number") { 674 return this.data.timer; 675 } 676 var now = new Date(); 677 return ((this.data.attributes.ref_date - now) / 1000); 678 }; 679 680 TC_Instance.prototype.start = function() { 681 useWindow.cancelAnimationFrame(this.data.animation_frame); 682 useWindow.clearTimeout(this.data.animation_frame) 683 684 // Check if a date was passed in html attribute or jquery data 685 var attr_data_date = $(this.element).data('date'); 686 if (typeof attr_data_date === "undefined") { 687 attr_data_date = $(this.element).attr('data-date'); 688 } 689 if (typeof attr_data_date === "string") { 690 this.data.attributes.ref_date = parse_date(attr_data_date); 691 } 692 // Check if this is an unpause of a timer 693 else if (typeof this.data.timer === "number") { 694 if (this.data.paused) { 695 this.data.attributes.ref_date = (new Date()).getTime() + (this.data.timer * 1000); 696 } 697 } 698 else { 699 // Try to get data-timer 700 var attr_data_timer = $(this.element).data('timer'); 701 if (typeof attr_data_timer === "undefined") { 702 attr_data_timer = $(this.element).attr('data-timer'); 703 } 704 if (typeof attr_data_timer === "string") { 705 attr_data_timer = parseFloat(attr_data_timer); 706 } 707 if (typeof attr_data_timer === "number") { 708 this.data.timer = attr_data_timer; 709 this.data.attributes.ref_date = (new Date()).getTime() + (attr_data_timer * 1000); 710 } 711 else { 712 // data-timer and data-date were both not set 713 // use config date 714 this.data.attributes.ref_date = this.config.ref_date; 715 } 716 } 717 718 // Start running 719 this.data.paused = false; 720 this.update.call(this); 721 }; 722 723 TC_Instance.prototype.restart = function() { 724 this.data.timer = false; 725 this.start(); 726 }; 727 728 TC_Instance.prototype.stop = function() { 729 if (typeof this.data.timer === "number") { 730 this.data.timer = this.timeLeft(this); 731 } 732 // Stop running 733 this.data.paused = true; 734 useWindow.cancelAnimationFrame(this.data.animation_frame); 735 }; 736 737 TC_Instance.prototype.destroy = function() { 738 this.clearListeners(); 739 this.stop(); 740 useWindow.clearInterval(this.data.interval_fallback); 741 this.data.interval_fallback = null; 742 743 this.container.remove(); 744 $(this.element).removeAttr('data-tc-id'); 745 $(this.element).removeData('tc-id'); 746 }; 747 748 TC_Instance.prototype.setOptions = function(options) { 749 if (this.config === null) { 750 this.default_options.ref_date = new Date(); 751 this.config = $.extend(true, {}, this.default_options); 752 } 753 $.extend(true, this.config, options); 754 755 // Use window.top if use_top_frame is true 756 if(this.config.use_top_frame) { 757 useWindow = window.top; 758 } 759 else { 760 useWindow = window; 761 } 762 updateUsedWindow(); 763 764 this.data.total_duration = this.config.total_duration; 765 if (typeof this.data.total_duration === "string") { 766 if (typeof secondsIn[this.data.total_duration] !== "undefined") { 767 // If set to Years, Months, Days, Hours or Minutes, fetch the secondsIn value for that 768 this.data.total_duration = secondsIn[this.data.total_duration]; 769 } 770 else if (this.data.total_duration === "Auto") { 771 // If set to auto, total_duration is the size of 1 unit, of the unit type bigger than the largest shown 772 for(var i = 0; i < Object.keys(this.config.time).length; i++) { 773 var unit = Object.keys(this.config.time)[i]; 774 if (this.config.time[unit].show) { 775 this.data.total_duration = secondsIn[nextUnits[unit]]; 776 break; 777 } 778 } 779 } 780 else { 781 // If it's a string, but neither of the above, user screwed up. 782 this.data.total_duration = secondsIn["Years"]; 783 console.error("Valid values for TimeCircles config.total_duration are either numeric, or (string) Years, Months, Days, Hours, Minutes, Auto"); 784 } 785 } 786 }; 787 788 TC_Instance.prototype.addListener = function(f, context, type) { 789 if (typeof f !== "function") 790 return; 791 if (typeof type === "undefined") 792 type = "visible"; 793 this.listeners[type].push({func: f, scope: context}); 794 }; 795 796 TC_Instance.prototype.notifyListeners = function(unit, value, total, type) { 797 for (var i = 0; i < this.listeners[type].length; i++) { 798 var listener = this.listeners[type][i]; 799 listener.func.apply(listener.scope, [unit, value, total]); 800 } 801 }; 802 803 TC_Instance.prototype.default_options = { 804 ref_date: new Date(), 805 start: true, 806 animation: "smooth",
807 count_past_zero: false, 808 circle_bg_color: "#60686F", 809 use_background: true, 810 fg_width: 0.1, 811 bg_width: 1.2, 812 text_size: 0.07, 813 total_duration: "Auto", 814 direction: "Clockwise", 815 use_top_frame: false, 816 start_angle: 0, 817 time: { 818 Days: { 819 show: true, 820 text: "Days", 821 color: "#FC6" 822 }, 823 Hours: { 824 show: true, 825 text: "Hours", 826 color: "#9CF" 827 }, 828 Minutes: { 829 show: true, 830 text: "Minutes", 831 color: "#BFB" 832 }, 833 Seconds: { 834 show: true, 835 text: "Seconds", 836 color: "#F99" 837 } 838 } 839 }; 840 841 // Time circle class 842 var TC_Class = function(elements, options) { 843 this.elements = elements; 844 this.options = options; 845 this.foreach(); 846 }; 847 848 TC_Class.prototype.getInstance = function(element) { 849 var instance; 850 851 var cur_id = $(element).data("tc-id"); 852 if (typeof cur_id === "undefined") { 853 cur_id = guid(); 854 $(element).attr("data-tc-id", cur_id); 855 } 856 if (typeof TC_Instance_List[cur_id] === "undefined") { 857 var options = this.options; 858 var element_options = $(element).data('options'); 859 if (typeof element_options === "string") { 860 element_options = JSON.parse(element_options); 861 } 862 if (typeof element_options === "object") { 863 options = $.extend(true, {}, this.options, element_options); 864 } 865 instance = new TC_Instance(element, options); 866 TC_Instance_List[cur_id] = instance; 867 } 868 else { 869 instance = TC_Instance_List[cur_id]; 870 if (typeof this.options !== "undefined") { 871 instance.setOptions(this.options); 872 } 873 } 874 return instance; 875 }; 876 877 TC_Class.prototype.addTime = function(seconds_to_add) { 878 this.foreach(function(instance) { 879 instance.addTime(seconds_to_add); 880 }); 881 }; 882 883 TC_Class.prototype.foreach = function(callback) { 884 var _this = this; 885 this.elements.each(function() { 886 var instance = _this.getInstance(this); 887 if (typeof callback === "function") { 888 callback(instance); 889 } 890 }); 891 return this; 892 }; 893 894 TC_Class.prototype.start = function() { 895 this.foreach(function(instance) { 896 instance.start(); 897 }); 898 return this; 899 }; 900 901 TC_Class.prototype.stop = function() { 902 this.foreach(function(instance) { 903 instance.stop(); 904 }); 905 return this; 906 }; 907 908 TC_Class.prototype.restart = function() { 909 this.foreach(function(instance) { 910 instance.restart(); 911 }); 912 return this; 913 }; 914 915 TC_Class.prototype.rebuild = function() { 916 this.foreach(function(instance) { 917 instance.initialize(false); 918 }); 919 return this; 920 }; 921 922 TC_Class.prototype.getTime = function() { 923 return this.getInstance(this.elements[0]).timeLeft(); 924 }; 925 926 TC_Class.prototype.addListener = function(f, type) { 927 if (typeof type === "undefined") 928 type = "visible"; 929 var _this = this; 930 this.foreach(function(instance) { 931 instance.addListener(f, _this.elements, type); 932 }); 933 return this; 934 }; 935 936 TC_Class.prototype.destroy = function() { 937 this.foreach(function(instance) { 938 instance.destroy(); 939 }); 940 return this; 941 }; 942 943 TC_Class.prototype.end = function() { 944 return this.elements; 945 }; 946 947 $.fn.TimeCircles = function(options) { 948 return new TC_Class(this, options); 949 }; 950}(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.