diff --git a/inc/TimeCircles.js b/inc/TimeCircles.js index 24b7be2..04af628 100644 --- a/inc/TimeCircles.js +++ b/inc/TimeCircles.js @@ -14,18 +14,18 @@ * of the private classes within the relevant element collection **/ (function($) { - - // Used to disable drawing on IE8, which can't use canvas - var cant_draw = false; + + // Used to disable some features on IE8 + var limited_mode = false; var tick_duration = 200; // in ms - + var debug = (location.hash === "#debug"); function debug_log(msg) { - if(debug) { + if (debug) { console.log(msg); } } - + var allUnits = ["Days", "Hours", "Minutes", "Seconds"]; var nextUnits = { Seconds: "Minutes", @@ -41,7 +41,7 @@ Months: 2678400, Years: 31536000 }; - + /** * Converts hex color code into object containing integer values for the r,g,b use * This function (hexToRgb) originates from: @@ -62,6 +62,11 @@ b: parseInt(result[3], 16) } : null; } + + function isCanvasSupported() { + var elem = document.createElement('canvas'); + return !!(elem.getContext && elem.getContext('2d')); + } /** * Function s4() and guid() originate from: @@ -69,8 +74,8 @@ */ function s4() { return Math.floor((1 + Math.random()) * 0x10000) - .toString(16) - .substring(1); + .toString(16) + .substring(1); } /** @@ -79,9 +84,37 @@ */ function guid() { return s4() + s4() + '-' + s4() + '-' + s4() + '-' + - s4() + '-' + s4() + s4() + s4(); + s4() + '-' + s4() + s4() + s4(); } - + + /** + * Array.prototype.indexOf fallback for IE8 + * @param {Mixed} mixed + * @returns {Number} + */ + if (!Array.prototype.indexOf) + { + Array.prototype.indexOf = function(elt /*, from*/) + { + var len = this.length >>> 0; + + var from = Number(arguments[1]) || 0; + from = (from < 0) + ? Math.ceil(from) + : Math.floor(from); + if (from < 0) + from += len; + + for (; from < len; from++) + { + if (from in this && + this[from] === elt) + return from; + } + return -1; + }; + } + function parse_date(str) { var match = str.match(/^[0-9]{4}-[0-9]{2}-[0-9]{2}\s[0-9]{1,2}:[0-9]{2}:[0-9]{2}$/); if (match !== null && match.length > 0) { @@ -92,13 +125,15 @@ } // Fallback for different date formats var d = Date.parse(str); - if(!isNaN(d)) return d; + if (!isNaN(d)) + return d; d = Date.parse(str.replace(/-/g, '/').replace('T', ' ')); - if(!isNaN(d)) return d; + if (!isNaN(d)) + return d; // Cant find anything return new Date(); } - + function parse_times(diff, old_diff, total_duration, units, floor) { var raw_time = {}; var raw_old_time = {}; @@ -106,39 +141,41 @@ var pct = {}; var old_pct = {}; var old_time = {}; - + var greater_unit = null; - for(var i in units) { + for (var i in units) { var unit = units[i]; var maxUnits; - - if(greater_unit === null) { + + if (greater_unit === null) { maxUnits = total_duration / secondsIn[unit]; } else { maxUnits = secondsIn[greater_unit] / secondsIn[unit]; } - + var curUnits = (diff / secondsIn[unit]); var oldUnits = (old_diff / secondsIn[unit]); - if(floor) curUnits = Math.floor(curUnits); - if(floor) oldUnits = Math.floor(oldUnits); - - if(unit !== "Days"){ + if (floor) + curUnits = Math.floor(curUnits); + if (floor) + oldUnits = Math.floor(oldUnits); + + if (unit !== "Days") { curUnits = curUnits % maxUnits; oldUnits = oldUnits % maxUnits; } - + raw_time[unit] = curUnits; time[unit] = Math.abs(curUnits); raw_old_time[unit] = oldUnits; old_time[unit] = Math.abs(oldUnits); pct[unit] = Math.abs(curUnits) / maxUnits; old_pct[unit] = Math.abs(oldUnits) / maxUnits; - + greater_unit = unit; } - + return { raw_time: raw_time, raw_old_time: raw_old_time, @@ -148,26 +185,27 @@ old_pct: old_pct }; } - + var TC_Instance_List = {}; // Try fetch/share instance - if(window !== window.top && typeof window.top.TC_Instance_List !== "undefined") { + if (window !== window.top && typeof window.top.TC_Instance_List !== "undefined") { TC_Instance_List = window.top.TC_Instance_List; } else { window.top.TC_Instance_List = TC_Instance_List; } - - (function(){ + + (function() { var vendors = ['webkit', 'moz']; - for(var x = 0; x < vendors.length && !window.top.requestAnimationFrame; ++x) { - window.top.requestAnimationFrame = window.top[vendors[x]+'RequestAnimationFrame']; - window.top.cancelAnimationFrame = window.top[vendors[x]+'CancelAnimationFrame']; - } - + for (var x = 0; x < vendors.length && !window.top.requestAnimationFrame; ++x) { + window.top.requestAnimationFrame = window.top[vendors[x] + 'RequestAnimationFrame']; + window.top.cancelAnimationFrame = window.top[vendors[x] + 'CancelAnimationFrame']; + } + if (!window.top.requestAnimationFrame || !window.top.cancelAnimationFrame) { window.top.requestAnimationFrame = function(callback, element, instance) { - if(typeof instance === "undefined") instance = { data: { last_frame: 0 } }; + if (typeof instance === "undefined") + instance = {data: {last_frame: 0}}; var currTime = new Date().getTime(); var timeToCall = Math.max(0, 16 - (currTime - instance.data.last_frame)); var id = window.top.setTimeout(function() { @@ -181,7 +219,7 @@ }; } })(); - + var TC_Instance = function(element, options) { this.element = element; @@ -218,53 +256,69 @@ } } }; - + this.config = null; this.setOptions(options); this.initialize(); }; - + TC_Instance.prototype.initialize = function(clear_listeners) { // Initialize drawn units this.data.drawn_units = []; - for(var unit in this.config.time) { - if(this.config.time[unit].show){ + for (var unit in this.config.time) { + if (this.config.time[unit].show) { this.data.drawn_units.push(unit); } } - + // Avoid stacking $(this.element).children('div.time_circles').remove(); - - if(typeof clear_listeners === "undefined") clear_listeners = true; - if(clear_listeners || this.listeners === null) { - this.listeners = { all: [], visible: [] }; + + if (typeof clear_listeners === "undefined") + clear_listeners = true; + if (clear_listeners || this.listeners === null) { + this.listeners = {all: [], visible: []}; } this.container = $("