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_' + key); 411 textElement.css("top", Math.round(0.35 * this.data.attributes.item_size)); 412 textElement.css("left", Math.round(i++ * this.data.attributes.item_size)); 413 textElement.css("width", this.data.attributes.item_size); 414 textElement.appendTo(this.container); 415 416 var headerElement = $("<h4>"); 417 headerElement.text(this.config.time[key].text); // Options 418 headerElement.css("font-size", Math.round(this.config.text_size * this.data.attributes.item_size)); 419 headerElement.appendTo(textElement); 420 421 var numberElement = $("<span>"); 422 numberElement.css("font-size", Math.round(this.config.number_size * this.data.attributes.item_size)); 423 numberElement.appendTo(textElement); 424 425 this.data.text_elements[key] = numberElement; 426 } 427 428 this.start(); 429 if (!this.config.start) { 430 this.data.paused = true; 431 } 432 433 // Set up interval fallback 434 var _this = this; 435 this.data.interval_fallback = useWindow.setInterval(function(){ 436 _this.update.call(_this, true); 437 }, 100); 438 }; 439 440 TC_Instance.prototype.update = function(nodraw) { 441 if(typeof nodraw === "undefined") { 442 nodraw = false; 443 } 444 else if(nodraw && this.data.paused) { 445 return; 446 } 447 448 if(limited_mode) { 449 //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 450 this.data.attributes.context.clearRect(0, 0, this.data.attributes.canvas[0].width, this.data.attributes.canvas[0].hright); 451 } 452 var diff, old_diff; 453 454 var prevDate = this.data.prev_time; 455 var curDate = new Date(); 456 this.data.prev_time = curDate; 457 458 if (prevDate === null) 459 prevDate = curDate; 460 461 // If not counting past zero, and time < 0, then simply draw the zero point once, and call stop 462 if (!this.config.count_past_zero) { 463 if (curDate > this.data.attributes.ref_date) { 464 for(var i = 0; i < this.data.drawn_units.length; i++) { 465 var key = this.data.drawn_units[i]; 466 467 // Set the text value 468 this.data.text_elements[key].text("0"); 469 var x = (i * this.data.attributes.item_size) + (this.data.attributes.item_size / 2); 470 var y = this.data.attributes.item_size / 2; 471 var color = this.config.time[key].color; 472 this.drawArc(x, y, color, 0); 473 } 474 this.stop(); 475 return; 476 } 477 } 478 479 // Compare current time with reference 480 diff = (this.data.attributes.ref_date - curDate) / 1000; 481 old_diff = (this.data.attributes.ref_date - prevDate) / 1000; 482 483 var floor = this.config.animation !== "smooth"; 484 485 var visible_times = parse_times(diff, old_diff, this.data.total_duration, this.data.drawn_units, floor); 486 var all_times = parse_times(diff, old_diff, secondsIn["Years"], allUnits, floor); 487 488 var i = 0; 489 var j = 0; 490 var lastKey = null; 491 492 var cur_shown = this.data.drawn_units.slice(); 493 for (var i in allUnits) { 494 var key = allUnits[i]; 495 496 // Notify (all) listeners 497 if (Math.floor(all_times.raw_time[key]) !== Math.floor(all_times.raw_old_time[key])) { 498 this.notifyListeners(key, Math.floor(all_times.time[key]), Math.floor(diff), "all"); 499 } 500 501 if (cur_shown.indexOf(key) < 0) 502 continue; 503 504 // Notify (visible) listeners 505 if (Math.floor(visible_times.raw_time[key]) !== Math.floor(visible_times.raw_old_time[key])) { 506 this.notifyListeners(key, Math.floor(visible_times.time[key]), Math.floor(diff), "visible"); 507 } 508 509 if(!nodraw) { 510 // Set the text value 511 this.data.text_elements[key].text(Math.floor(Math.abs(visible_times.time[key]))); 512 513 var x = (j * this.data.attributes.item_size) + (this.data.attributes.item_size / 2); 514 var y = this.data.attributes.item_size / 2; 515 var color = this.config.time[key].color; 516 517 if (this.config.animation === "smooth") { 518 if (lastKey !== null && !limited_mode) { 519 if (Math.floor(visible_times.time[lastKey]) > Math.floor(visible_times.old_time[lastKey])) { 520 this.radialFade(x, y, color, 1, key); 521 this.data.state.fading[key] = true; 522 } 523 else if (Math.floor(visible_times.time[lastKey]) < Math.floor(visible_times.old_time[lastKey])) { 524 this.radialFade(x, y, color, 0, key); 525 this.data.state.fading[key] = true; 526 } 527 } 528 if (!this.data.state.fading[key]) { 529 this.drawArc(x, y, color, visible_times.pct[key]); 530 } 531 } 532 else { 533 this.animateArc(x, y, color, visible_times.pct[key], visible_times.old_pct[key], (new Date()).getTime() + tick_duration); 534 } 535 } 536 lastKey = key; 537 j++; 538 } 539 540 // Dont request another update if we should be paused 541 if(this.data.paused || nodraw) { 542 return; 543 } 544 545 // We need this for our next frame either way 546 var _this = this; 547 var update = function() { 548 _this.update.call(_this); 549 }; 550 551 // Either call next update immediately, or in a second 552 if (this.config.animation === "smooth") { 553 // Smooth animation, Queue up the next frame
554 this.data.animation_frame = useWindow.requestAnimationFrame(update, _this.element, _this); 555 } 556 else { 557 // Tick animation, Don't queue until very slightly after the next second happens 558 var delay = (diff % 1) * 1000; 559 if (delay < 0) 560 delay = 1000 + delay; 561 delay += 50; 562 563 _this.data.animation_frame = useWindow.setTimeout(function() { 564 _this.data.animation_frame = useWindow.requestAnimationFrame(update, _this.element, _this); 565 }, delay); 566 } 567 }; 568 569 TC_Instance.prototype.animateArc = function(x, y, color, target_pct, cur_pct, animation_end) { 570 if (this.data.attributes.context === null) 571 return; 572 573 var diff = cur_pct - target_pct; 574 if (Math.abs(diff) > 0.5) { 575 if (target_pct === 0) { 576 this.radialFade(x, y, color, 1); 577 } 578 else { 579 this.radialFade(x, y, color, 0); 580 } 581 } 582 else { 583 var progress = (tick_duration - (animation_end - (new Date()).getTime())) / tick_duration; 584 if (progress > 1) 585 progress = 1; 586 587 var pct = (cur_pct * (1 - progress)) + (target_pct * progress); 588 this.drawArc(x, y, color, pct); 589 590 //var show_pct = 591 if (progress >= 1) 592 return; 593 var _this = this; 594 useWindow.requestAnimationFrame(function() { 595 _this.animateArc(x, y, color, target_pct, cur_pct, animation_end); 596 }, this.element); 597 } 598 }; 599 600 TC_Instance.prototype.drawArc = function(x, y, color, pct) { 601 if (this.data.attributes.context === null) 602 return; 603 604 var clear_radius = Math.max(this.data.attributes.outer_radius, this.data.attributes.item_size / 2); 605 if(!limited_mode) { 606 this.data.attributes.context.clearRect( 607 x - clear_radius, 608 y - clear_radius, 609 clear_radius * 2, 610 clear_radius * 2 611 ); 612 } 613 614 if (this.config.use_background) { 615 this.data.attributes.context.beginPath(); 616 this.data.attributes.context.arc(x, y, this.data.attributes.radius, 0, 2 * Math.PI, false); 617 this.data.attributes.context.lineWidth = this.data.attributes.line_width * this.config.bg_width; 618 619 // line color 620 this.data.attributes.context.strokeStyle = this.config.circle_bg_color; 621 this.data.attributes.context.stroke(); 622 } 623 624 // Direction 625 var startAngle, endAngle, counterClockwise; 626 var defaultOffset = (-0.5 * Math.PI); 627 var fullCircle = 2 * Math.PI; 628 startAngle = defaultOffset + (this.config.start_angle / 360 * fullCircle); 629 var offset = (2 * pct * Math.PI); 630 631 if (this.config.direction === "Both") { 632 counterClockwise = false; 633 startAngle -= (offset / 2); 634 endAngle = startAngle + offset; 635 } 636 else { 637 if (this.config.direction === "Clockwise") { 638 counterClockwise = false; 639 endAngle = startAngle + offset; 640 } 641 else { 642 counterClockwise = true; 643 endAngle = startAngle - offset; 644 } 645 } 646 647 this.data.attributes.context.beginPath(); 648 this.data.attributes.context.arc(x, y, this.data.attributes.radius, startAngle, endAngle, counterClockwise); 649 this.data.attributes.context.lineWidth = this.data.attributes.line_width; 650 651 // line color 652 this.data.attributes.context.strokeStyle = color; 653 this.data.attributes.context.stroke(); 654 }; 655 656 TC_Instance.prototype.radialFade = function(x, y, color, from, key) { 657 // TODO: Make fade_time option 658 var rgb = hexToRgb(color); 659 var _this = this; // We have a few inner scopes here that will need access to our instance 660 661 var step = 0.2 * ((from === 1) ? -1 : 1); 662 var i; 663 for (i = 0; from <= 1 && from >= 0; i++) { 664 // Create inner scope so our variables are not changed by the time the Timeout triggers 665 (function() {
666 var delay = 50 * i; 667 var rgba = "rgba(" + rgb.r + ", " + rgb.g + ", " + rgb.b + ", " + (Math.round(from * 10) / 10) + ")"; 668 useWindow.setTimeout(function() { 669 _this.drawArc(x, y, rgba, 1); 670 }, delay); 671 }()); 672 from += step; 673 } 674 if (typeof key !== undefined) { 675 useWindow.setTimeout(function() { 676 _this.data.state.fading[key] = false; 677 }, 50 * i); 678 } 679 }; 680 681 TC_Instance.prototype.timeLeft = function() { 682 if (this.data.paused && typeof this.data.timer === "number") { 683 return this.data.timer; 684 } 685 var now = new Date(); 686 return ((this.data.attributes.ref_date - now) / 1000); 687 }; 688 689 TC_Instance.prototype.start = function() { 690 useWindow.cancelAnimationFrame(this.data.animation_frame); 691 useWindow.clearTimeout(this.data.animation_frame) 692 693 // Check if a date was passed in html attribute or jquery data 694 var attr_data_date = $(this.element).data('date'); 695 if (typeof attr_data_date === "undefined") { 696 attr_data_date = $(this.element).attr('data-date'); 697 } 698 if (typeof attr_data_date === "string") { 699 this.data.attributes.ref_date = parse_date(attr_data_date); 700 } 701 // Check if this is an unpause of a timer 702 else if (typeof this.data.timer === "number") { 703 if (this.data.paused) { 704 this.data.attributes.ref_date = (new Date()).getTime() + (this.data.timer * 1000); 705 } 706 } 707 else { 708 // Try to get data-timer 709 var attr_data_timer = $(this.element).data('timer'); 710 if (typeof attr_data_timer === "undefined") { 711 attr_data_timer = $(this.element).attr('data-timer'); 712 } 713 if (typeof attr_data_timer === "string") { 714 attr_data_timer = parseFloat(attr_data_timer); 715 } 716 if (typeof attr_data_timer === "number") { 717 this.data.timer = attr_data_timer; 718 this.data.attributes.ref_date = (new Date()).getTime() + (attr_data_timer * 1000); 719 } 720 else { 721 // data-timer and data-date were both not set 722 // use config date 723 this.data.attributes.ref_date = this.config.ref_date; 724 } 725 } 726 727 // Start running 728 this.data.paused = false; 729 this.update.call(this); 730 }; 731 732 TC_Instance.prototype.restart = function() { 733 this.data.timer = false; 734 this.start(); 735 }; 736 737 TC_Instance.prototype.stop = function() { 738 if (typeof this.data.timer === "number") { 739 this.data.timer = this.timeLeft(this); 740 } 741 // Stop running 742 this.data.paused = true; 743 useWindow.cancelAnimationFrame(this.data.animation_frame); 744 }; 745 746 TC_Instance.prototype.destroy = function() { 747 this.clearListeners(); 748 this.stop(); 749 useWindow.clearInterval(this.data.interval_fallback); 750 this.data.interval_fallback = null; 751 752 this.container.remove(); 753 $(this.element).removeAttr('data-tc-id'); 754 $(this.element).removeData('tc-id'); 755 }; 756 757 TC_Instance.prototype.setOptions = function(options) { 758 if (this.config === null) { 759 this.default_options.ref_date = new Date(); 760 this.config = $.extend(true, {}, this.default_options); 761 } 762 $.extend(true, this.config, options); 763 764 // Use window.top if use_top_frame is true 765 if(this.config.use_top_frame) { 766 useWindow = window.top; 767 } 768 else { 769 useWindow = window; 770 } 771 updateUsedWindow(); 772 773 this.data.total_duration = this.config.total_duration; 774 if (typeof this.data.total_duration === "string") { 775 if (typeof secondsIn[this.data.total_duration] !== "undefined") { 776 // If set to Years, Months, Days, Hours or Minutes, fetch the secondsIn value for that 777 this.data.total_duration = secondsIn[this.data.total_duration]; 778 } 779 else if (this.data.total_duration === "Auto") { 780 // If set to auto, total_duration is the size of 1 unit, of the unit type bigger than the largest shown 781 for(var i = 0; i < Object.keys(this.config.time).length; i++) { 782 var unit = Object.keys(this.config.time)[i]; 783 if (this.config.time[unit].show) { 784 this.data.total_duration = secondsIn[nextUnits[unit]]; 785 break; 786 } 787 } 788 } 789 else { 790 // If it's a string, but neither of the above, user screwed up. 791 this.data.total_duration = secondsIn["Years"]; 792 console.error("Valid values for TimeCircles config.total_duration are either numeric, or (string) Years, Months, Days, Hours, Minutes, Auto"); 793 } 794 } 795 }; 796 797 TC_Instance.prototype.addListener = function(f, context, type) { 798 if (typeof f !== "function") 799 return; 800 if (typeof type === "undefined") 801 type = "visible"; 802 this.listeners[type].push({func: f, scope: context});
803 }; 804 805 TC_Instance.prototype.notifyListeners = function(unit, value, total, type) { 806 for (var i = 0; i < this.listeners[type].length; i++) { 807 var listener = this.listeners[type][i]; 808 listener.func.apply(listener.scope, [unit, value, total]); 809 } 810 }; 811 812 TC_Instance.prototype.default_options = { 813 ref_date: new Date(), 814 start: true, 815 animation: "smooth", 816 count_past_zero: true, 817 circle_bg_color: "#60686F", 818 use_background: true, 819 fg_width: 0.1, 820 bg_width: 1.2, 821 text_size: 0.07, 822 number_size: 0.28, 823 total_duration: "Auto", 824 direction: "Clockwise", 825 use_top_frame: false, 826 start_angle: 0, 827 time: { 828 Days: { 829 show: true, 830 text: "Days", 831 color: "#FC6" 832 }, 833 Hours: { 834 show: true, 835 text: "Hours", 836 color: "#9CF" 837 }, 838 Minutes: { 839 show: true, 840 text: "Minutes", 841 color: "#BFB" 842 }, 843 Seconds: { 844 show: true, 845 text: "Seconds", 846 color: "#F99" 847 } 848 } 849 }; 850 851 // Time circle class 852 var TC_Class = function(elements, options) { 853 this.elements = elements; 854 this.options = options; 855 this.foreach(); 856 }; 857 858 TC_Class.prototype.getInstance = function(element) { 859 var instance; 860 861 var cur_id = $(element).data("tc-id"); 862 if (typeof cur_id === "undefined") { 863 cur_id = guid(); 864 $(element).attr("data-tc-id", cur_id); 865 } 866 if (typeof TC_Instance_List[cur_id] === "undefined") { 867 var options = this.options; 868 var element_options = $(element).data('options'); 869 if (typeof element_options === "string") { 870 element_options = JSON.parse(element_options); 871 } 872 if (typeof element_options === "object") { 873 options = $.extend(true, {}, this.options, element_options); 874 } 875 instance = new TC_Instance(element, options); 876 TC_Instance_List[cur_id] = instance; 877 } 878 else { 879 instance = TC_Instance_List[cur_id]; 880 if (typeof this.options !== "undefined") { 881 instance.setOptions(this.options); 882 } 883 } 884 return instance; 885 }; 886 887 TC_Class.prototype.addTime = function(seconds_to_add) { 888 this.foreach(function(instance) { 889 instance.addTime(seconds_to_add); 890 }); 891 }; 892 893 TC_Class.prototype.foreach = function(callback) { 894 var _this = this; 895 this.elements.each(function() { 896 var instance = _this.getInstance(this); 897 if (typeof callback === "function") { 898 callback(instance); 899 } 900 }); 901 return this; 902 }; 903 904 TC_Class.prototype.start = function() { 905 this.foreach(function(instance) { 906 instance.start(); 907 }); 908 return this; 909 }; 910 911 TC_Class.prototype.stop = function() { 912 this.foreach(function(instance) { 913 instance.stop(); 914 }); 915 return this; 916 }; 917 918 TC_Class.prototype.restart = function() { 919 this.foreach(function(instance) { 920 instance.restart(); 921 }); 922 return this; 923 }; 924 925 TC_Class.prototype.rebuild = function() { 926 this.foreach(function(instance) { 927 instance.initialize(false); 928 }); 929 return this; 930 }; 931 932 TC_Class.prototype.getTime = function() { 933 return this.getInstance(this.elements[0]).timeLeft(); 934 }; 935 936 TC_Class.prototype.addListener = function(f, type) { 937 if (typeof type === "undefined") 938 type = "visible"; 939 var _this = this; 940 this.foreach(function(instance) { 941 instance.addListener(f, _this.elements, type); 942 }); 943 return this; 944 }; 945 946 TC_Class.prototype.destroy = function() { 947 this.foreach(function(instance) { 948 instance.destroy(); 949 }); 950 return this; 951 }; 952 953 TC_Class.prototype.end = function() { 954 return this.elements; 955 }; 956 957 $.fn.TimeCircles = function(options) { 958 return new TC_Class(this, options); 959 }; 960}(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.