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