X-Git-Url: https://adrianiainlam.tk/git/?a=blobdiff_plain;f=dygraph-utils.js;h=907069f58dd7535a6cab46d6923a11acd443c20f;hb=1fdef78645031ce82164e8dc72f71ce53a2ab2f4;hp=c6c3a60345712b76cd023fa212011b3fb0611c17;hpb=9d31b3fe6bb776384da58c37dc9b2ba1304f1fd0;p=dygraphs.git diff --git a/dygraph-utils.js b/dygraph-utils.js index c6c3a60..907069f 100644 --- a/dygraph-utils.js +++ b/dygraph-utils.js @@ -147,6 +147,21 @@ Dygraph.addEvent = function addEvent(elem, type, fn) { /** * @private + * Add an event handler. This event handler is kept until the graph is + * destroyed with a call to graph.destroy(). + * + * @param { DOM element } elem The element to add the event to. + * @param { String } type The type of the event, e.g. 'click' or 'mousemove'. + * @param { Function } fn The function to call on the event. The function takes + * one parameter: the event object. + */ +Dygraph.prototype.addEvent = function addEvent(elem, type, fn) { + Dygraph.addEvent(elem, type, fn); + this.registeredEvents_.push({ elem : elem, type : type, fn : fn }); +}; + +/** + * @private * Remove an event handler. This smooths a difference between IE and the rest of * the world. * @param { DOM element } elem The element to add the event to. @@ -583,7 +598,7 @@ Dygraph.updateDeep = function (self, o) { // DOM objects are shallowly-copied. self[k] = o[k]; } else if (typeof(o[k]) == 'object') { - if (typeof(self[k]) != 'object') { + if (typeof(self[k]) != 'object' || self[k] === null) { self[k] = {}; } Dygraph.updateDeep(self[k], o[k]); @@ -668,6 +683,66 @@ Dygraph.isAndroid = function() { /** * @private + * Returns a new iterator over array, between indexes start and + * start + length, and only returns entries that pass the accept function + * + * @param array the array to iterate over. + * @param start the first index to iterate over + * @param length the number of elements in the array to iterate over. + * This, along with start, defines a slice of the array, and so length + * doesn't imply the number of elements in the iterator when accept + * doesn't always accept all values. + * @param predicate a function that takes parameters array and idx, which + * returns true when the element should be returned. If omitted, all + * elements are accepted. + * + * TODO(konigsberg): add default vlues to start and length. + */ +Dygraph.createIterator = function(array, start, length, predicate) { + var iter = new function() { + this.end_ = Math.min(array.length, start + length); + this.nextIdx_ = start - 1; // use -1 so initial call to advance works. + this.hasNext_ = true; + this.peek_ = null; + var self = this; + + this.hasNext = function() { + return self.hasNext_; + } + + this.next = function() { + if (self.hasNext_) { + var obj = self.peek_; + self.advance_(); + return obj; + } + return null; + } + this.peek = function() { + return self.peek_; + } + this.advance_ = function() { + var nextIdx = self.nextIdx_; + nextIdx++; + while(nextIdx < self.end_) { + if (!predicate || predicate(array, nextIdx)) { + self.peek_ = array[nextIdx]; + self.nextIdx_ = nextIdx; + return; + } + nextIdx++; + } + self.nextIdx_ = nextIdx; + self.hasNext_ = false; + self.peek_ = null; + } + }; + iter.advance_(); + return iter; +}; + +/** + * @private * Call a function N times at a 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.