Skip to content

Commit 2d226f7

Browse files
committed
Reapply "RS-20085: Only set rhtmlwidget-status to ready once images have loaded (#133)"
This reverts commit 4091c89.
1 parent 4091c89 commit 2d226f7

15 files changed

Lines changed: 53 additions & 46 deletions

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: rhtmlPictographs
22
Type: Package
33
Title: Create Pictograph html widgets
4-
Version: 1.0.8
4+
Version: 1.0.9
55
Author: Displayr <opensource@displayr.com>
66
Maintainer: Displayr <opensource@displayr.com>
77
Description: An R HTMLWidget that can generate single image graphics, mutli

inst/htmlwidgets/rhtmlPictographs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/htmlwidgets/rhtmlPictographs.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

theSrc/scripts/BaseCell.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,9 @@ class BaseCell {
128128
}
129129

130130
draw () {
131-
this._draw()
132-
this._generateDynamicCss()
131+
return Promise.resolve()
132+
.then(() => this._draw())
133+
.then(() => this._generateDynamicCss())
133134
}
134135

135136
setCss (cssLocation, cssAttr, cssValue) {

theSrc/scripts/GraphicCell.js

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -538,38 +538,31 @@ class GraphicCell extends BaseCell {
538538
throw error // NB This is thrown because displayr wants the error too
539539
}
540540

541-
let baseImageCompletePromise = Promise.resolve()
541+
const imageRenderPromises = []
542542
if (this.config.baseImage != null) {
543543
const baseImageConfig = this.config.baseImage
544544
const imageFactory = this.imageFactory
545-
const baseImageRenderPromises = []
546545
enteringLeafNodes.each(function (dataAttributes) {
547546
const d3Node = d3.select(this)
548-
baseImageRenderPromises.push(
547+
imageRenderPromises.push(
549548
imageFactory.addBaseImageTo(d3Node, baseImageConfig, imageWidth, imageHeight, dataAttributes)
550549
)
551550
})
552-
baseImageCompletePromise = Promise.all(baseImageRenderPromises).catch(imageErrorHandler)
553551
}
554552

555-
// TODO this should be tied to base image promise unconditioanlly (ie. swap lines below)
556-
// let variableImageCompletePromise = baseImageCompletePromise
557-
let variableImageCompletePromise = Promise.resolve()
558553
if (this.config.variableImage != null) {
559554
const variableImageConfig = this.config.variableImage
560555
const imageFactory = this.imageFactory
561-
variableImageCompletePromise = baseImageCompletePromise.then(function () {
562-
const variableImageRenderPromises = []
563-
enteringLeafNodes.each(function (dataAttributes) {
564-
const d3Node = d3.select(this)
565-
variableImageRenderPromises.push(
566-
imageFactory.addVarImageTo(d3Node, variableImageConfig, imageWidth, imageHeight, dataAttributes)
567-
)
568-
})
569-
return Promise.all(variableImageRenderPromises).catch(imageErrorHandler)
556+
enteringLeafNodes.each(function (dataAttributes) {
557+
const d3Node = d3.select(this)
558+
imageRenderPromises.push(
559+
imageFactory.addVarImageTo(d3Node, variableImageConfig, imageWidth, imageHeight, dataAttributes)
560+
)
570561
})
571562
}
572563

564+
const variableImageCompletePromise = Promise.all(imageRenderPromises).catch(imageErrorHandler)
565+
573566
return variableImageCompletePromise.then(() => {
574567
if (this.config.tooltip) {
575568
enteringLeafNodes.append('svg:title')

theSrc/scripts/ImageFactory.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,7 @@ class ImageFactory {
220220
return null
221221
}
222222
})
223-
.then((clipId) => {
224-
const imageHandle = imageInstance.appendToSvg()
225-
if (clipId) {
226-
imageHandle.attr('clip-path', `url(#${clipId})`)
227-
}
228-
})
223+
.then((clipId) => imageInstance.appendToSvg(clipId))
229224
}
230225

231226
calculateAspectRatio (config) {

theSrc/scripts/Pictograph.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ class Pictograph {
1414
constructor (el) {
1515
this.rootElement = _.has(el, 'length') ? el[0] : el
1616
d3.select(this.rootElement).attr(`rhtmlwidget-status`, 'loading')
17-
d3.select(this.rootElement).attr(`rhtmlPictographs-status`, 'loading') // to be removed once regression testing code checks for rhtmlwidget-status
1817
const actualDimensions = this.getContainerDimensions()
1918
log.info('Pictograph() called. Container dimensions:', actualDimensions, 'element:', el)
2019

@@ -42,7 +41,6 @@ class Pictograph {
4241
if (error.type === InsufficientContainerSizeError.type) {
4342
console.log(error.message)
4443
d3.select(this.rootElement).attr(`rhtmlwidget-status`, 'ready')
45-
d3.select(this.rootElement).attr(`rhtmlPictographs-status`, 'ready') // to be removed once regression testing code checks for rhtmlwidget-status
4644
} else {
4745
console.error(`error in pictograph draw: ${error.message}`)
4846
console.error(error.stack)
@@ -79,7 +77,6 @@ class Pictograph {
7977
if (error.type === InsufficientContainerSizeError.type) {
8078
console.log(error.message)
8179
d3.select(this.rootElement).attr(`rhtmlwidget-status`, 'ready')
82-
d3.select(this.rootElement).attr(`rhtmlPictographs-status`, 'ready') // to be removed once regression testing code checks for rhtmlwidget-status
8380
} else {
8481
console.error(`error in pictograph resize: ${error.message}`)
8582
console.error(error.stack)
@@ -497,6 +494,8 @@ class Pictograph {
497494
.attr('class', 'table-cell')
498495
.attr('transform', d => `translate(${d.x},${d.y})`)
499496

497+
const cellDrawingPromises = []
498+
500499
enteringCells.each(function (d) {
501500
const instance = d.instance
502501
log.debug(`rendering entering cell`, instance)
@@ -508,11 +507,11 @@ class Pictograph {
508507
instance.setWidth(d.width)
509508
instance.setHeight(d.height)
510509
instance.setDynamicMargins(d.dynamicMargins)
511-
instance.draw()
510+
511+
cellDrawingPromises.push(instance.draw())
512512
})
513513

514-
d3.select(this.rootElement).attr(`rhtmlwidget-status`, 'ready')
515-
d3.select(this.rootElement).attr(`rhtmlPictographs-status`, 'ready') // to be removed once regression testing code checks for rhtmlwidget-status
514+
Promise.all(cellDrawingPromises).then(() => d3.select(this.rootElement).attr(`rhtmlwidget-status`, 'ready'))
516515
}
517516

518517
// TODO Duplicated code from graphicCell

theSrc/scripts/imageTypes/base.imagetype.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ class BaseImageType {
6464
get baseShapeHiding () {
6565
return (this.config.baseShapeScale != null) ? this.config.baseShapeScale : 1
6666
}
67+
68+
addClipId (clipId) {
69+
if (clipId) {
70+
this.imageHandle.attr('clip-path', `url(#${clipId})`)
71+
}
72+
}
6773
}
6874

6975
module.exports = BaseImageType

theSrc/scripts/imageTypes/circle.imagetype.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CircleType extends BaseImageType {
1616
return Promise.resolve(1)
1717
}
1818

19-
appendToSvg () {
19+
appendToSvg (clipId) {
2020
this.imageHandle = this.d3Node.append('svg:circle')
2121
.classed('circle', true)
2222
.attr('cx', this.containerWidth / 2)
@@ -25,7 +25,9 @@ class CircleType extends BaseImageType {
2525
.style('fill', this.color)
2626
.style('opacity', this.opacity)
2727

28-
return this.imageHandle
28+
this.addClipId(clipId)
29+
30+
return Promise.resolve()
2931
}
3032
}
3133

theSrc/scripts/imageTypes/ellipse.imagetype.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class EllipseType extends BaseImageType {
99
return Promise.resolve(null)
1010
}
1111

12-
appendToSvg () {
12+
appendToSvg (clipId) {
1313
this.imageHandle = this.d3Node.append('svg:ellipse')
1414
.classed('ellipse', true)
1515
.attr('cx', this.containerWidth / 2)
@@ -19,7 +19,9 @@ class EllipseType extends BaseImageType {
1919
.style('fill', this.color)
2020
.style('opacity', this.opacity)
2121

22-
return this.imageHandle
22+
this.addClipId(clipId)
23+
24+
return Promise.resolve()
2325
}
2426
}
2527

0 commit comments

Comments
 (0)