X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-utils.js;h=5bd58d7c852f08d62ee0766854030a8e298b4cbe;hb=685bae09233c9b19038965283653bc793d8bc338;hp=797adce8d1f5bb1e76c1deab4668d77a689cf207;hpb=a96b8ba34c08682182bde256d505f7ae5ed9eee4;p=dygraphs.git diff --git a/dygraph-utils.js b/dygraph-utils.js index 797adce..5bd58d7 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -76,18 +76,28 @@ Dygraph.log = function(severity, message) { } if (typeof(window.console) != 'undefined') { + // In older versions of Firefox, only console.log is defined. + var console = window.console; + var log = function(console, method, msg) { + if (method) { + method.call(console, msg); + } else { + console.log(msg); + } + }; + switch (severity) { case Dygraph.DEBUG: - window.console.debug('dygraphs: ' + message); + log(console, console.debug, 'dygraphs: ' + message); break; case Dygraph.INFO: - window.console.info('dygraphs: ' + message); + log(console, console.info, 'dygraphs: ' + message); break; case Dygraph.WARNING: - window.console.warn('dygraphs: ' + message); + log(console, console.warn, 'dygraphs: ' + message); break; case Dygraph.ERROR: - window.console.error('dygraphs: ' + message); + log(console, console.error, 'dygraphs: ' + message); break; } } @@ -183,7 +193,7 @@ Dygraph.addEvent = function addEvent(elem, type, fn) { * on the event. The function takes one parameter: the event object. * @private */ -Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { +Dygraph.prototype.addAndTrackEvent = function(elem, type, fn) { Dygraph.addEvent(elem, type, fn); this.registeredEvents_.push({ elem : elem, type : type, fn : fn }); }; @@ -197,7 +207,7 @@ Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { * on the event. The function takes one parameter: the event object. * @private */ -Dygraph.removeEvent = function addEvent(elem, type, fn) { +Dygraph.removeEvent = function(elem, type, fn) { if (elem.removeEventListener) { elem.removeEventListener(type, fn, false); } else { @@ -211,6 +221,17 @@ Dygraph.removeEvent = function addEvent(elem, type, fn) { } }; +Dygraph.prototype.removeTrackedEvents_ = function() { + if (this.registeredEvents_) { + for (var idx = 0; idx < this.registeredEvents_.length; idx++) { + var reg = this.registeredEvents_[idx]; + Dygraph.removeEvent(reg.elem, reg.type, reg.fn); + } + } + + this.registeredEvents_ = []; +} + /** * Cancels further processing of an event. This is useful to prevent default * browser actions, e.g. highlighting text on a double-click. @@ -291,6 +312,12 @@ Dygraph.findPosX = function(obj) { if(obj.offsetParent) { var copyObj = obj; while(1) { + // NOTE: the if statement here is for IE8. + var borderLeft = "0"; + if (window.getComputedStyle) { + borderLeft = window.getComputedStyle(copyObj, null).borderLeft || "0"; + } + curleft += parseInt(borderLeft, 10) ; curleft += copyObj.offsetLeft; if(!copyObj.offsetParent) { break; @@ -322,6 +349,12 @@ Dygraph.findPosY = function(obj) { if(obj.offsetParent) { var copyObj = obj; while(1) { + // NOTE: the if statement here is for IE8. + var borderTop = "0"; + if (window.getComputedStyle) { + borderTop = window.getComputedStyle(copyObj, null).borderTop || "0"; + } + curtop += parseInt(borderTop, 10) ; curtop += copyObj.offsetTop; if(!copyObj.offsetParent) { break; @@ -816,7 +849,9 @@ Dygraph.createIterator = function(array, start, length, opt_predicate) { // Shim layer with setTimeout fallback. // From: http://paulirish.com/2011/requestanimationframe-for-smart-animating/ -window.requestAnimFrame = (function(){ +// Should be called with the window context: +// Dygraph.requestAnimFrame.call(window, function() {}) +Dygraph.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || @@ -828,45 +863,52 @@ window.requestAnimFrame = (function(){ })(); /** - * @private - * Call a function at most N times at an attempted given interval, then call a - * cleanup function once. repeat_fn is called once immediately, then (times - 1) - * times asynchronously. If times=1, then cleanup_fn() is also called - * synchronously. - * @param repeat_fn {Function} Called repeatedly -- takes the number of calls - * (from 0 to times-1) as an argument. - * @param times {number} The number of times max to call repeat_fn - * @param every_ms {number} Milliseconds to schedule between calls - * @param cleanup_fn {Function} A function to call after all repeat_fn calls. + * Call a function at most maxFrames times at an attempted interval of + * framePeriodInMillis, then call a cleanup function once. repeatFn is called + * once immediately, then at most (maxFrames - 1) times asynchronously. If + * maxFrames==1, then cleanup_fn() is also called synchronously. This function + * is used to sequence animation. + * @param {function(number)} repeatFn Called repeatedly -- takes the frame + * number (from 0 to maxFrames-1) as an argument. + * @param {number} maxFrames The max number of times to call repeatFn + * @param {number} framePeriodInMillis Max requested time between frames. + * @param {function()} cleanupFn A function to call after all repeatFn calls. * @private */ -Dygraph.repeatAndCleanup = function(repeat_fn, times, every_ms, cleanup_fn) { - var count = 0; - var previous_count; - var start_time = new Date().getTime(); - repeat_fn(count); - if (times == 1) { - cleanup_fn(); +Dygraph.repeatAndCleanup = function(repeatFn, maxFrames, framePeriodInMillis, + cleanupFn) { + var frameNumber = 0; + var previousFrameNumber; + var startTime = new Date().getTime(); + repeatFn(frameNumber); + if (maxFrames == 1) { + cleanupFn(); return; } + var maxFrameArg = maxFrames - 1; (function loop() { - if (count >= times) return; - window.requestAnimFrame(function(scheduled_epoch_time_ms) { - var delay = (scheduled_epoch_time_ms - start_time); - previous_count = count; - count = Math.floor(delay / every_ms); - if ((count - previous_count) > (times / 2)) { - count = previous_count + (times / 2); - } - if (count > times - 1) { - // Ensure call at max times. - if (previous_count !== (times - 1)) { - repeat_fn(times - 1); - } - cleanup_fn(); + if (frameNumber >= maxFrames) return; + Dygraph.requestAnimFrame.call(window, function() { + // Determine which frame to draw based on the delay so far. Will skip + // frames if necessary. + var currentTime = new Date().getTime(); + var delayInMillis = currentTime - startTime; + previousFrameNumber = frameNumber; + frameNumber = Math.floor(delayInMillis / framePeriodInMillis); + var frameDelta = frameNumber - previousFrameNumber; + // If we predict that the subsequent repeatFn call will overshoot our + // total frame target, so our last call will cause a stutter, then jump to + // the last call immediately. If we're going to cause a stutter, better + // to do it faster than slower. + var predictOvershootStutter = (frameNumber + frameDelta) > maxFrameArg; + if (predictOvershootStutter || (frameNumber >= maxFrameArg)) { + repeatFn(maxFrameArg); // Ensure final call with maxFrameArg. + cleanupFn(); } else { - repeat_fn(count); + if (frameDelta !== 0) { // Don't call repeatFn with duplicate frames. + repeatFn(frameNumber); + } loop(); } }); @@ -1020,7 +1062,6 @@ Dygraph.regularShape_ = function( delta = delta || Math.PI * 2 / sides; ctx.beginPath(); - var first = true; var initialAngle = rotationRadians; var angle = initialAngle; @@ -1204,3 +1245,62 @@ Dygraph.detectLineDelimiter = function(data) { return null; }; + +/** + * Is one element contained by another? + * @param {Element} containee The contained element. + * @param {Element} container The container element. + * @return {boolean} Whether containee is inside (or equal to) container. + * @private + */ +Dygraph.isElementContainedBy = function(containee, container) { + if (container === null || containee === null) { + return false; + } + while (containee && containee !== container) { + containee = containee.parentNode; + } + return (containee === container); +}; + + +// This masks some numeric issues in older versions of Firefox, +// where 1.0/Math.pow(10,2) != Math.pow(10,-2). +/** @type {function(number,number):number} */ +Dygraph.pow = function(base, exp) { + if (exp < 0) { + return 1.0 / Math.pow(base, -exp); + } + return Math.pow(base, exp); +}; + +// For Dygraph.setDateSameTZ, below. +Dygraph.dateSetters = { + ms: Date.prototype.setMilliseconds, + s: Date.prototype.setSeconds, + m: Date.prototype.setMinutes, + h: Date.prototype.setHours +}; + +/** + * This is like calling d.setSeconds(), d.setMinutes(), etc, except that it + * adjusts for time zone changes to keep the date/time parts consistent. + * + * For example, d.getSeconds(), d.getMinutes() and d.getHours() will all be + * the same before/after you call setDateSameTZ(d, {ms: 0}). The same is not + * true if you call d.setMilliseconds(0). + * + * @type {function(!Date, Object.)} + */ +Dygraph.setDateSameTZ = function(d, parts) { + var tz = d.getTimezoneOffset(); + for (var k in parts) { + if (!parts.hasOwnProperty(k)) continue; + var setter = Dygraph.dateSetters[k]; + if (!setter) throw "Invalid setter: " + k; + setter.call(d, parts[k]); + if (d.getTimezoneOffset() != tz) { + d.setTime(d.getTime() + (tz - d.getTimezoneOffset()) * 60 * 1000); + } + } +};