From 3ac48d2e20a52d1b2b3dad39521d84ff1b12482b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Tue, 23 Sep 2014 18:41:32 +0200 Subject: [PATCH 1/6] Replaced jQuery.extend() with an emulated version. --- lib/morris.area.coffee | 2 +- lib/morris.bar.coffee | 2 +- lib/morris.coffee | 12 ++++++++++++ lib/morris.donut.coffee | 2 +- lib/morris.grid.coffee | 2 +- lib/morris.hover.coffee | 2 +- lib/morris.line.coffee | 2 +- 7 files changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/morris.area.coffee b/lib/morris.area.coffee index 86f8595a..da508323 100644 --- a/lib/morris.area.coffee +++ b/lib/morris.area.coffee @@ -7,7 +7,7 @@ class Morris.Area extends Morris.Line constructor: (options) -> return new Morris.Area(options) unless (@ instanceof Morris.Area) - areaOptions = $.extend {}, areaDefaults, options + areaOptions = Morris.extend {}, areaDefaults, options @cumulative = not areaOptions.behaveLikeLine diff --git a/lib/morris.bar.coffee b/lib/morris.bar.coffee index 4c312978..3d1891ad 100644 --- a/lib/morris.bar.coffee +++ b/lib/morris.bar.coffee @@ -1,7 +1,7 @@ class Morris.Bar extends Morris.Grid constructor: (options) -> return new Morris.Bar(options) unless (@ instanceof Morris.Bar) - super($.extend {}, options, parseTime: false) + super(Morris.extend {}, options, parseTime: false) init: -> @cumulative = @options.stacked diff --git a/lib/morris.coffee b/lib/morris.coffee index f2cd2df8..e4fbcd5e 100644 --- a/lib/morris.coffee +++ b/lib/morris.coffee @@ -41,3 +41,15 @@ Morris.commas = (num) -> # @example # Morris.pad2(1) -> '01' Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number + +# Copy all properties from objects in second argument to last onto the first +# object given and return the first object. This should emulate jQuery's +# $.extend(). +# +# @example +# Morris.extend({}, { a:1 }, { b:2 }) -> '{ a:1, b:2 }' +Morris.extend = (object, objects...) -> + for properties in objects + for key, val of properties + object[key] = val + object diff --git a/lib/morris.donut.coffee b/lib/morris.donut.coffee index e40314d9..d6ce07b4 100644 --- a/lib/morris.donut.coffee +++ b/lib/morris.donut.coffee @@ -31,7 +31,7 @@ class Morris.Donut extends Morris.EventEmitter # constructor: (options) -> return new Morris.Donut(options) unless (@ instanceof Morris.Donut) - @options = $.extend {}, @defaults, options + @options = Morris.extend {}, @defaults, options if typeof options.element is 'string' @el = $ document.getElementById(options.element) diff --git a/lib/morris.grid.coffee b/lib/morris.grid.coffee index 0a27623c..ab227d75 100644 --- a/lib/morris.grid.coffee +++ b/lib/morris.grid.coffee @@ -15,7 +15,7 @@ class Morris.Grid extends Morris.EventEmitter if @el.css('position') == 'static' @el.css('position', 'relative') - @options = $.extend {}, @gridDefaults, (@defaults || {}), options + @options = Morris.extend {}, @gridDefaults, (@defaults || {}), options # backwards compatibility for units -> postUnits if typeof @options.units is 'string' diff --git a/lib/morris.hover.coffee b/lib/morris.hover.coffee index fe04178e..1ed48ccb 100644 --- a/lib/morris.hover.coffee +++ b/lib/morris.hover.coffee @@ -5,7 +5,7 @@ class Morris.Hover class: 'morris-hover morris-default-style' constructor: (options = {}) -> - @options = $.extend {}, Morris.Hover.defaults, options + @options = Morris.extend {}, Morris.Hover.defaults, options @el = $ "
" @el.hide() @options.parent.append(@el) diff --git a/lib/morris.line.coffee b/lib/morris.line.coffee index 3a725670..e2628b55 100644 --- a/lib/morris.line.coffee +++ b/lib/morris.line.coffee @@ -392,7 +392,7 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) -> spec = Morris.LABEL_SPECS["second"] # check if there's a user-defined formatting function if xLabelFormat - spec = $.extend({}, spec, {fmt: xLabelFormat}) + spec = Morris.extend({}, spec, {fmt: xLabelFormat}) # calculate labels d = spec.start(d0) ret = [] From 6576a6c91cf4988a631679bac56c8c1ecfcd2b2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Sat, 27 Sep 2014 18:39:28 +0200 Subject: [PATCH 2/6] Replaced all dependencies on jQuery --- lib/morris.bar.coffee | 4 ++-- lib/morris.coffee | 11 +++++++-- lib/morris.donut.coffee | 16 ++++++------- lib/morris.grid.coffee | 46 +++++++++++++++++++------------------- lib/morris.hover.coffee | 24 +++++++++++--------- lib/morris.line.coffee | 2 +- spec/lib/hover_spec.coffee | 20 ++++++++--------- 7 files changed, 66 insertions(+), 57 deletions(-) diff --git a/lib/morris.bar.coffee b/lib/morris.bar.coffee index 3d1891ad..558bb1ca 100644 --- a/lib/morris.bar.coffee +++ b/lib/morris.bar.coffee @@ -95,11 +95,11 @@ class Morris.Bar extends Morris.Grid if not @options.horizontal startPos = labelBox.x size = labelBox.width - maxSize = @el.width() + maxSize = @el.offsetWidth else startPos = labelBox.y size = labelBox.height - maxSize = @el.height() + maxSize = @el.offsetHeight # try to avoid overlaps if (not prevLabelMargin? or diff --git a/lib/morris.coffee b/lib/morris.coffee index e4fbcd5e..c45dbf54 100644 --- a/lib/morris.coffee +++ b/lib/morris.coffee @@ -1,7 +1,5 @@ Morris = window.Morris = {} -$ = jQuery - # Very simple event-emitter class. # # @private @@ -53,3 +51,12 @@ Morris.extend = (object, objects...) -> for key, val of properties object[key] = val object + +# Emulate jQuery's $el.offset() (http://youmightnotneedjquery.com/#offset) +Morris.offset = (el) -> + rect = el.getBoundingClientRect() + top: rect.top + document.body.scrollTop, + left: rect.left + document.body.scrollLeft + +# Emulate jQuery's $el.css() (http://youmightnotneedjquery.com/#get_style) +Morris.css = (el, ruleName) -> getComputedStyle(el)[ruleName] diff --git a/lib/morris.donut.coffee b/lib/morris.donut.coffee index d6ce07b4..2299746a 100644 --- a/lib/morris.donut.coffee +++ b/lib/morris.donut.coffee @@ -34,18 +34,18 @@ class Morris.Donut extends Morris.EventEmitter @options = Morris.extend {}, @defaults, options if typeof options.element is 'string' - @el = $ document.getElementById(options.element) + @el = document.getElementById(options.element) else - @el = $ options.element + @el = options.element - if @el == null || @el.length == 0 + if @el == null throw new Error("Graph placeholder not found.") # bail if there's no data if options.data is undefined or options.data.length is 0 return - @raphael = new Raphael(@el[0]) + @raphael = new Raphael(@el) if @options.resize $(window).bind 'resize', (evt) => @@ -59,8 +59,8 @@ class Morris.Donut extends Morris.EventEmitter redraw: -> @raphael.clear() - cx = @el.width() / 2 - cy = @el.height() / 2 + cx = @el.offsetWidth / 2 + cy = @el.offsetHeight / 2 w = (Math.min(cx, cy) - 10) / 3 total = 0 @@ -117,7 +117,7 @@ class Morris.Donut extends Morris.EventEmitter # @private setLabels: (label1, label2) -> - inner = (Math.min(@el.width() / 2, @el.height() / 2) - 10) * 2 / 3 + inner = (Math.min(@el.offsetWidth / 2, @el.offsetHeight / 2) - 10) * 2 / 3 maxWidth = 1.8 * inner maxHeightTop = inner / 2 maxHeightBottom = inner / 3 @@ -139,7 +139,7 @@ class Morris.Donut extends Morris.EventEmitter resizeHandler: => @timeoutId = null - @raphael.setSize @el.width(), @el.height() + @raphael.setSize @el.offsetWidth, @el.offsetHeight @redraw() diff --git a/lib/morris.grid.coffee b/lib/morris.grid.coffee index ab227d75..0cde9e1b 100644 --- a/lib/morris.grid.coffee +++ b/lib/morris.grid.coffee @@ -6,14 +6,14 @@ class Morris.Grid extends Morris.EventEmitter constructor: (options) -> # find the container to draw the graph in if typeof options.element is 'string' - @el = $ document.getElementById(options.element) + @el = document.getElementById(options.element) else - @el = $ options.element - if not @el? or @el.length == 0 + @el = options.element + if not @el? throw new Error("Graph container element not found") - if @el.css('position') == 'static' - @el.css('position', 'relative') + if Morris.css(@el, 'position') == 'static' + @el.style.position = 'relative' @options = Morris.extend {}, @gridDefaults, (@defaults || {}), options @@ -22,7 +22,7 @@ class Morris.Grid extends Morris.EventEmitter @options.postUnits = options.units # the raphael drawing instance - @raphael = new Raphael(@el[0]) + @raphael = new Raphael(@el) # some redraw stuff @elementWidth = null @@ -39,8 +39,8 @@ class Morris.Grid extends Morris.EventEmitter @setData @options.data # hover - @el.bind 'mousemove', (evt) => - offset = @el.offset() + @el.addEventListener 'mousemove', (evt) => + offset = Morris.offset(@el) x = evt.pageX - offset.left if @selectFrom left = @data[@hitTest(Math.min(x, @selectFrom))]._x @@ -50,44 +50,44 @@ class Morris.Grid extends Morris.EventEmitter else @fire 'hovermove', x, evt.pageY - offset.top - @el.bind 'mouseleave', (evt) => + @el.addEventListener 'mouseleave', (evt) => if @selectFrom @selectionRect.hide() @selectFrom = null @fire 'hoverout' - @el.bind 'touchstart touchmove touchend', (evt) => + @el.addEventListener 'touchstart touchmove touchend', (evt) => touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0] - offset = @el.offset() + offset = Morris.offset(@el) @fire 'hovermove', touch.pageX - offset.left, touch.pageY - offset.top - @el.bind 'click', (evt) => - offset = @el.offset() + @el.addEventListener 'click', (evt) => + offset = Morris.offset(@el) @fire 'gridclick', evt.pageX - offset.left, evt.pageY - offset.top if @options.rangeSelect - @selectionRect = @raphael.rect(0, 0, 0, @el.innerHeight()) + @selectionRect = @raphael.rect(0, 0, 0, @el.offsetHeight) # @el.innerHeight() .attr({ fill: @options.rangeSelectColor, stroke: false }) .toBack() .hide() - @el.bind 'mousedown', (evt) => - offset = @el.offset() + @el.addEventListener 'mousedown', (evt) => + offset = Morris.offset(@el) @startRange evt.pageX - offset.left - @el.bind 'mouseup', (evt) => - offset = @el.offset() + @el.addEventListener 'mouseup', (evt) => + offset = Morris.offset(@el) @endRange evt.pageX - offset.left @fire 'hovermove', evt.pageX - offset.left, evt.pageY - offset.top if @options.resize - $(window).bind 'resize', (evt) => + window.addEventListener 'resize', (evt) => if @timeoutId? window.clearTimeout @timeoutId @timeoutId = window.setTimeout @resizeHandler, 100 # Disable tap highlight on iOS. - @el.css('-webkit-tap-highlight-color', 'rgba(0,0,0,0)') + @el.style.webkitTapHighlightColor = 'rgba(0,0,0,0)' @postInit() if @postInit @@ -271,8 +271,8 @@ class Morris.Grid extends Morris.EventEmitter grid _calc: -> - w = @el.width() - h = @el.height() + w = @el.offsetWidth + h = @el.offsetHeight if @elementWidth != w or @elementHeight != h or @dirty @elementWidth = w @@ -482,7 +482,7 @@ class Morris.Grid extends Morris.EventEmitter resizeHandler: => @timeoutId = null - @raphael.setSize @el.width(), @el.height() + @raphael.setSize @el.offsetWidth, @el.offsetHeight @redraw() hasToShow: (i) => diff --git a/lib/morris.hover.coffee b/lib/morris.hover.coffee index 1ed48ccb..27186a2d 100644 --- a/lib/morris.hover.coffee +++ b/lib/morris.hover.coffee @@ -6,9 +6,10 @@ class Morris.Hover constructor: (options = {}) -> @options = Morris.extend {}, Morris.Hover.defaults, options - @el = $ "
" - @el.hide() - @options.parent.append(@el) + @el = document.createElement 'div' + @el.className = @options.class + @el.style.display = 'none' + (@options.parent[0] or @options.parent).appendChild @el update: (html, x, y, centre_y) -> if not html @@ -19,13 +20,13 @@ class Morris.Hover @moveTo(x, y, centre_y) html: (content) -> - @el.html(content) + @el.innerHTML = content moveTo: (x, y, centre_y) -> - parentWidth = @options.parent.innerWidth() - parentHeight = @options.parent.innerHeight() - hoverWidth = @el.outerWidth() - hoverHeight = @el.outerHeight() + parentWidth = @options.parent.offsetWidth # @el.innerWidth() + parentHeight = @options.parent.offsetHeight # @el.innerHeight() + hoverWidth = @el.offsetWidth # @el.outerWidth() + hoverHeight = @el.offsetHeight # @el.outerHeight() left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth) if y? if centre_y is true @@ -40,10 +41,11 @@ class Morris.Hover top = parentHeight / 2 - hoverHeight / 2 else top = parentHeight / 2 - hoverHeight / 2 - @el.css(left: left + "px", top: parseInt(top) + "px") + @el.style.left = parseInt(left) + "px" + @el.style.top = parseInt(top) + "px" show: -> - @el.show() + @el.style.display = '' hide: -> - @el.hide() + @el.style.display = 'none' diff --git a/lib/morris.line.coffee b/lib/morris.line.coffee index e2628b55..583a49be 100644 --- a/lib/morris.line.coffee +++ b/lib/morris.line.coffee @@ -167,7 +167,7 @@ class Morris.Line extends Morris.Grid if (not prevLabelMargin? or prevLabelMargin >= labelBox.x + labelBox.width or prevAngleMargin? and prevAngleMargin >= labelBox.x) and - labelBox.x >= 0 and (labelBox.x + labelBox.width) < @el.width() + labelBox.x >= 0 and (labelBox.x + labelBox.width) < @el.offsetWidth if @options.xLabelAngle != 0 margin = 1.25 * @options.gridTextSize / Math.sin(@options.xLabelAngle * Math.PI / 180.0) diff --git a/spec/lib/hover_spec.coffee b/spec/lib/hover_spec.coffee index 868f1a3c..a8a76840 100644 --- a/spec/lib/hover_spec.coffee +++ b/spec/lib/hover_spec.coffee @@ -35,23 +35,23 @@ describe "Morris.Hover", -> it "should place the popup directly above the given point", -> @hover.moveTo(100, 150) - @element.should.have.css('left', '50px') - @element.should.have.css('top', '40px') + getComputedStyle(@element[0])['left'].should.be '50px' + getComputedStyle(@element[0])['top'].should.be '40px' it "should place the popup below the given point if it does not fit above", -> @hover.moveTo(100, 50) - @element.should.have.css('left', '50px') - @element.should.have.css('top', '60px') + getComputedStyle(@element[0])['left'].should.be '50px' + getComputedStyle(@element[0])['top'].should.be '60px' it "should center the popup vertically if it will not fit above or below", -> @hover.moveTo(100, 100) - @element.should.have.css('left', '50px') - @element.should.have.css('top', '40px') + getComputedStyle(@element[0])['left'].should.be '50px' + getComputedStyle(@element[0])['top'].should.be '40px' it "should center the popup vertically if no y value is supplied", -> @hover.moveTo(100) - @element.should.have.css('left', '50px') - @element.should.have.css('top', '40px') + getComputedStyle(@element[0])['left'].should.be '50px' + getComputedStyle(@element[0])['top'].should.be '40px' describe "#update", -> it "should update content, show and reposition the popup", -> @@ -59,6 +59,6 @@ describe "Morris.Hover", -> html = "
Hello, Everyone!
" hover.update(html, 150, 200) el = $('#test .morris-hover') - el.should.have.css('left', '100px') - el.should.have.css('top', '90px') + getComputedStyle(@element[0])['left'].should.be '100px' + getComputedStyle(@element[0])['top'].should.be '90px' el.should.have.text('Hello, Everyone!') From b51d62c26c63d5c8325bde40b5bf454bea7617ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Sat, 27 Sep 2014 18:58:47 +0200 Subject: [PATCH 3/6] Moved jQuery to devDependencies --- bower.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bower.json b/bower.json index c222c3c7..311e7f75 100644 --- a/bower.json +++ b/bower.json @@ -6,10 +6,10 @@ "./morris.css" ], "dependencies": { - "jquery": ">= 1.7.0", "raphael": ">= 2.0" }, "devDependencies": { + "jquery": ">= 1.7.0", "mocha": "~1.17.1", "chai": "~1.9.0", "chai-jquery": "~1.2.1", From 156959921cb5b4cedd1ff2203e7ad4dbd44d50cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Sun, 28 Sep 2014 14:26:50 +0200 Subject: [PATCH 4/6] Better browser compatibility (IE8+) --- lib/morris.bar.coffee | 6 +++--- lib/morris.coffee | 34 +++++++++++++++++++++++++++++++++- lib/morris.donut.coffee | 11 +++++++---- lib/morris.grid.coffee | 22 +++++++++++----------- lib/morris.hover.coffee | 10 +++++----- lib/morris.line.coffee | 3 ++- 6 files changed, 61 insertions(+), 25 deletions(-) diff --git a/lib/morris.bar.coffee b/lib/morris.bar.coffee index 558bb1ca..4e878286 100644 --- a/lib/morris.bar.coffee +++ b/lib/morris.bar.coffee @@ -91,15 +91,15 @@ class Morris.Bar extends Morris.Grid Math.cos(angle * Math.PI / 180.0) label.transform("t#{offset},0...") - + {width, height} = Morris.dimensions @el if not @options.horizontal startPos = labelBox.x size = labelBox.width - maxSize = @el.offsetWidth + maxSize = width else startPos = labelBox.y size = labelBox.height - maxSize = @el.offsetHeight + maxSize = height # try to avoid overlaps if (not prevLabelMargin? or diff --git a/lib/morris.coffee b/lib/morris.coffee index c45dbf54..7ffd594d 100644 --- a/lib/morris.coffee +++ b/lib/morris.coffee @@ -1,5 +1,14 @@ Morris = window.Morris = {} +# Compute element style +compStyle = (el) -> + if getComputedStyle # standard (includes ie9) + getComputedStyle(el, null) + else if el.currentStyle # IE older + el.currentStyle + else # inline style + el.style + # Very simple event-emitter class. # # @private @@ -59,4 +68,27 @@ Morris.offset = (el) -> left: rect.left + document.body.scrollLeft # Emulate jQuery's $el.css() (http://youmightnotneedjquery.com/#get_style) -Morris.css = (el, ruleName) -> getComputedStyle(el)[ruleName] +Morris.css = (el, prop) -> compStyle(el)[prop] + +# Emulate jQuery's $el.on() +Morris.on = (el, eventName, fn) -> + if el.addEventListener + el.addEventListener(eventName, fn) + else + el.attachEvent('on'+eventName, fn) + +# Emulate jQuery's $el.width() and $el.height() +Morris.dimensions = (el) -> + style = compStyle el + width: parseInt(style.width), + height: parseInt(style.height) + +# Emulate jQuery's $el.innerWidth() and $el.innerHeight() +Morris.innerDimensions = (el) -> + style = compStyle el + width: parseInt(style.width) + + parseInt(style.paddingLeft) + + parseInt(style.paddingRight), + height: parseInt(style.height) + + parseInt(style.paddingTop) + + parseInt(style.paddingBottom) diff --git a/lib/morris.donut.coffee b/lib/morris.donut.coffee index 2299746a..a3cdfc1c 100644 --- a/lib/morris.donut.coffee +++ b/lib/morris.donut.coffee @@ -59,8 +59,9 @@ class Morris.Donut extends Morris.EventEmitter redraw: -> @raphael.clear() - cx = @el.offsetWidth / 2 - cy = @el.offsetHeight / 2 + {width, height} = Morris.dimensions @el + cx = width / 2 + cy = height / 2 w = (Math.min(cx, cy) - 10) / 3 total = 0 @@ -117,7 +118,8 @@ class Morris.Donut extends Morris.EventEmitter # @private setLabels: (label1, label2) -> - inner = (Math.min(@el.offsetWidth / 2, @el.offsetHeight / 2) - 10) * 2 / 3 + {width, height} = Morris.dimensions(@el) + inner = (Math.min(width / 2, height / 2) - 10) * 2 / 3 maxWidth = 1.8 * inner maxHeightTop = inner / 2 maxHeightBottom = inner / 3 @@ -139,7 +141,8 @@ class Morris.Donut extends Morris.EventEmitter resizeHandler: => @timeoutId = null - @raphael.setSize @el.offsetWidth, @el.offsetHeight + {width, height} = Morris.dimensions @el + @raphael.setSize width, height @redraw() diff --git a/lib/morris.grid.coffee b/lib/morris.grid.coffee index 0cde9e1b..5c67ce62 100644 --- a/lib/morris.grid.coffee +++ b/lib/morris.grid.coffee @@ -39,7 +39,7 @@ class Morris.Grid extends Morris.EventEmitter @setData @options.data # hover - @el.addEventListener 'mousemove', (evt) => + Morris.on @el, 'mousemove', (evt) => offset = Morris.offset(@el) x = evt.pageX - offset.left if @selectFrom @@ -50,38 +50,38 @@ class Morris.Grid extends Morris.EventEmitter else @fire 'hovermove', x, evt.pageY - offset.top - @el.addEventListener 'mouseleave', (evt) => + Morris.on @el, 'mouseleave', (evt) => if @selectFrom @selectionRect.hide() @selectFrom = null @fire 'hoverout' - @el.addEventListener 'touchstart touchmove touchend', (evt) => + Morris.on @el, 'touchstart touchmove touchend', (evt) => touch = evt.originalEvent.touches[0] or evt.originalEvent.changedTouches[0] offset = Morris.offset(@el) @fire 'hovermove', touch.pageX - offset.left, touch.pageY - offset.top - @el.addEventListener 'click', (evt) => + Morris.on @el, 'click', (evt) => offset = Morris.offset(@el) @fire 'gridclick', evt.pageX - offset.left, evt.pageY - offset.top if @options.rangeSelect - @selectionRect = @raphael.rect(0, 0, 0, @el.offsetHeight) # @el.innerHeight() + @selectionRect = @raphael.rect(0, 0, 0, Morris.innerDimensions(@el).height) .attr({ fill: @options.rangeSelectColor, stroke: false }) .toBack() .hide() - @el.addEventListener 'mousedown', (evt) => + Morris.on @el, 'mousedown', (evt) => offset = Morris.offset(@el) @startRange evt.pageX - offset.left - @el.addEventListener 'mouseup', (evt) => + Morris.on @el, 'mouseup', (evt) => offset = Morris.offset(@el) @endRange evt.pageX - offset.left @fire 'hovermove', evt.pageX - offset.left, evt.pageY - offset.top if @options.resize - window.addEventListener 'resize', (evt) => + Morris.on window, 'resize', (evt) => if @timeoutId? window.clearTimeout @timeoutId @timeoutId = window.setTimeout @resizeHandler, 100 @@ -271,8 +271,7 @@ class Morris.Grid extends Morris.EventEmitter grid _calc: -> - w = @el.offsetWidth - h = @el.offsetHeight + {width:w, height:h} = Morris.dimensions @el if @elementWidth != w or @elementHeight != h or @dirty @elementWidth = w @@ -482,7 +481,8 @@ class Morris.Grid extends Morris.EventEmitter resizeHandler: => @timeoutId = null - @raphael.setSize @el.offsetWidth, @el.offsetHeight + {width, height} = Morris.dimensions @el + @raphael.setSize width, height @redraw() hasToShow: (i) => diff --git a/lib/morris.hover.coffee b/lib/morris.hover.coffee index 27186a2d..7993ed8f 100644 --- a/lib/morris.hover.coffee +++ b/lib/morris.hover.coffee @@ -9,7 +9,7 @@ class Morris.Hover @el = document.createElement 'div' @el.className = @options.class @el.style.display = 'none' - (@options.parent[0] or @options.parent).appendChild @el + (@options.parent = @options.parent[0] or @options.parent).appendChild @el update: (html, x, y, centre_y) -> if not html @@ -23,10 +23,10 @@ class Morris.Hover @el.innerHTML = content moveTo: (x, y, centre_y) -> - parentWidth = @options.parent.offsetWidth # @el.innerWidth() - parentHeight = @options.parent.offsetHeight # @el.innerHeight() - hoverWidth = @el.offsetWidth # @el.outerWidth() - hoverHeight = @el.offsetHeight # @el.outerHeight() + {width:parentWidth, height:parentHeight} = + Morris.innerDimensions @options.parent + hoverWidth = @el.offsetWidth + hoverHeight = @el.offsetHeight left = Math.min(Math.max(0, x - hoverWidth / 2), parentWidth - hoverWidth) if y? if centre_y is true diff --git a/lib/morris.line.coffee b/lib/morris.line.coffee index 583a49be..d182ed1b 100644 --- a/lib/morris.line.coffee +++ b/lib/morris.line.coffee @@ -167,7 +167,8 @@ class Morris.Line extends Morris.Grid if (not prevLabelMargin? or prevLabelMargin >= labelBox.x + labelBox.width or prevAngleMargin? and prevAngleMargin >= labelBox.x) and - labelBox.x >= 0 and (labelBox.x + labelBox.width) < @el.offsetWidth + labelBox.x >= 0 and + (labelBox.x + labelBox.width) < Morris.dimensions(@el).width if @options.xLabelAngle != 0 margin = 1.25 * @options.gridTextSize / Math.sin(@options.xLabelAngle * Math.PI / 180.0) From 4228f9e70b49515d84161fac1d269a7418e5abbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Sun, 28 Sep 2014 15:25:19 +0200 Subject: [PATCH 5/6] Better emulation of $.extend --- lib/morris.coffee | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/morris.coffee b/lib/morris.coffee index 7ffd594d..afc12ae7 100644 --- a/lib/morris.coffee +++ b/lib/morris.coffee @@ -55,9 +55,9 @@ Morris.pad2 = (number) -> (if number < 10 then '0' else '') + number # # @example # Morris.extend({}, { a:1 }, { b:2 }) -> '{ a:1, b:2 }' -Morris.extend = (object, objects...) -> - for properties in objects - for key, val of properties +Morris.extend = (object={}, objects...) -> + for properties in objects when properties? + for key, val of properties when properties.hasOwnProperty key object[key] = val object From ebc2b5d9aae1db3f99e723818ff695de1a6271fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christopher=20Fred=C3=A9n?= Date: Sun, 28 Sep 2014 15:32:21 +0200 Subject: [PATCH 6/6] Enable API compatibility by accepting jQuery objects as element --- lib/morris.donut.coffee | 2 +- lib/morris.grid.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/morris.donut.coffee b/lib/morris.donut.coffee index a3cdfc1c..e9c4c9ac 100644 --- a/lib/morris.donut.coffee +++ b/lib/morris.donut.coffee @@ -36,7 +36,7 @@ class Morris.Donut extends Morris.EventEmitter if typeof options.element is 'string' @el = document.getElementById(options.element) else - @el = options.element + @el = options.element[0] or options.element if @el == null throw new Error("Graph placeholder not found.") diff --git a/lib/morris.grid.coffee b/lib/morris.grid.coffee index 5c67ce62..e142e78a 100644 --- a/lib/morris.grid.coffee +++ b/lib/morris.grid.coffee @@ -8,7 +8,7 @@ class Morris.Grid extends Morris.EventEmitter if typeof options.element is 'string' @el = document.getElementById(options.element) else - @el = options.element + @el = options.element[0] or options.element if not @el? throw new Error("Graph container element not found")