11var Plotly = require ( '@lib/index' ) ;
2+ var Plots = require ( '@src/plots/plots' ) ;
23var Images = require ( '@src/components/images' ) ;
34var createGraphDiv = require ( '../assets/create_graph_div' ) ;
45var destroyGraphDiv = require ( '../assets/destroy_graph_div' ) ;
56var mouseEvent = require ( '../assets/mouse_event' ) ;
67
8+ var jsLogo = 'https://images.plot.ly/language-icons/api-home/js-logo.png' ;
9+ var pythonLogo = 'https://images.plot.ly/language-icons/api-home/python-logo.png' ;
10+
711describe ( 'Layout images' , function ( ) {
812
913 describe ( 'supplyLayoutDefaults' , function ( ) {
@@ -13,11 +17,11 @@ describe('Layout images', function() {
1317
1418 beforeEach ( function ( ) {
1519 layoutIn = { images : [ ] } ;
16- layoutOut = { } ;
20+ layoutOut = { _has : Plots . _hasPlotType } ;
1721 } ) ;
1822
1923 it ( 'should reject when there is no `source`' , function ( ) {
20- layoutIn . images [ 0 ] = { opacity : 0.5 , width : 0.2 , height : 0.2 } ;
24+ layoutIn . images [ 0 ] = { opacity : 0.5 , sizex : 0.2 , sizey : 0.2 } ;
2125
2226 Images . supplyLayoutDefaults ( layoutIn , layoutOut ) ;
2327
@@ -26,10 +30,10 @@ describe('Layout images', function() {
2630
2731 it ( 'should reject when not an array' , function ( ) {
2832 layoutIn . images = {
29- source : 'http://www.someimagesource.com' ,
33+ source : jsLogo ,
3034 opacity : 0.5 ,
31- width : 0.2 ,
32- height : 0.2
35+ sizex : 0.2 ,
36+ sizey : 0.2
3337 } ;
3438
3539 Images . supplyLayoutDefaults ( layoutIn , layoutOut ) ;
@@ -38,17 +42,17 @@ describe('Layout images', function() {
3842 } ) ;
3943
4044 it ( 'should coerce the correct defaults' , function ( ) {
41- layoutIn . images [ 0 ] = { source : 'http://www.someimagesource.com' } ;
45+ layoutIn . images [ 0 ] = { source : jsLogo } ;
4246
4347 var expected = {
44- source : 'http://www.someimagesource.com' ,
48+ source : jsLogo ,
4549 layer : 'above' ,
4650 x : 0 ,
4751 y : 0 ,
4852 xanchor : 'left' ,
4953 yanchor : 'top' ,
50- width : 0 ,
51- height : 0 ,
54+ sizex : 0 ,
55+ sizey : 0 ,
5256 sizing : 'contain' ,
5357 opacity : 1 ,
5458 xref : 'paper' ,
@@ -119,7 +123,7 @@ describe('Layout images', function() {
119123 sizing : sizing
120124 } ] } ) ;
121125
122- var image = Plotly . d3 . select ( '[href="' + anchorName + '"] ') ,
126+ var image = Plotly . d3 . select ( 'image ' ) ,
123127 parValue = image . attr ( 'preserveAspectRatio' ) ;
124128
125129 expect ( parValue ) . toBe ( expected ) ;
@@ -160,24 +164,23 @@ describe('Layout images', function() {
160164 afterEach ( destroyGraphDiv ) ;
161165
162166 it ( 'should not move when referencing the paper' , function ( done ) {
163- var source = 'http://www.placekitten.com/200' ,
164- image = {
165- source : source ,
166- xref : 'paper' ,
167- yref : 'paper' ,
168- x : 0 ,
169- y : 0 ,
170- width : 0.1 ,
171- height : 0.1
172- } ;
167+ var image = {
168+ source : jsLogo ,
169+ xref : 'paper' ,
170+ yref : 'paper' ,
171+ x : 0 ,
172+ y : 0 ,
173+ sizex : 0.1 ,
174+ sizey : 0.1
175+ } ;
173176
174177 Plotly . plot ( gd , data , {
175178 images : [ image ] ,
176179 dragmode : 'pan' ,
177180 width : 600 ,
178181 height : 400
179182 } ) . then ( function ( ) {
180- var img = Plotly . d3 . select ( '[href="' + source + '"] ') . node ( ) ,
183+ var img = Plotly . d3 . select ( 'image ' ) . node ( ) ,
181184 oldPos = img . getBoundingClientRect ( ) ;
182185
183186 mouseEvent ( 'mousedown' , 250 , 200 ) ;
@@ -193,24 +196,23 @@ describe('Layout images', function() {
193196 } ) ;
194197
195198 it ( 'should move when referencing axes' , function ( done ) {
196- var source = 'http://www.placekitten.com/200' ,
197- image = {
198- source : source ,
199- xref : 'x' ,
200- yref : 'y' ,
201- x : 2 ,
202- y : 2 ,
203- width : 1 ,
204- height : 1
205- } ;
199+ var image = {
200+ source : jsLogo ,
201+ xref : 'x' ,
202+ yref : 'y' ,
203+ x : 2 ,
204+ y : 2 ,
205+ sizex : 1 ,
206+ sizey : 1
207+ } ;
206208
207209 Plotly . plot ( gd , data , {
208210 images : [ image ] ,
209211 dragmode : 'pan' ,
210212 width : 600 ,
211213 height : 400
212214 } ) . then ( function ( ) {
213- var img = Plotly . d3 . select ( '[href="' + source + '"] ') . node ( ) ,
215+ var img = Plotly . d3 . select ( 'image ' ) . node ( ) ,
214216 oldPos = img . getBoundingClientRect ( ) ;
215217
216218 mouseEvent ( 'mousedown' , 250 , 200 ) ;
@@ -227,4 +229,47 @@ describe('Layout images', function() {
227229
228230 } ) ;
229231
232+ describe ( 'when relayout' , function ( ) {
233+
234+ var gd ,
235+ data = [ { x : [ 1 , 2 , 3 ] , y : [ 1 , 2 , 3 ] } ] ;
236+
237+ beforeEach ( function ( done ) {
238+ gd = createGraphDiv ( ) ;
239+ Plotly . plot ( gd , data , {
240+ images : [ {
241+ source : jsLogo ,
242+ x : 2 ,
243+ y : 2 ,
244+ sizex : 1 ,
245+ sizey : 1
246+ } ]
247+ } ) . then ( done ) ;
248+ } ) ;
249+
250+ afterEach ( destroyGraphDiv ) ;
251+
252+ it ( 'should update the image if changed' , function ( done ) {
253+ var img = Plotly . d3 . select ( 'image' ) ,
254+ url = img . attr ( 'xlink:href' ) ;
255+
256+ Plotly . relayout ( gd , 'images[0].source' , pythonLogo ) . then ( function ( ) {
257+ var newImg = Plotly . d3 . select ( 'image' ) ,
258+ newUrl = newImg . attr ( 'xlink:href' ) ;
259+ expect ( url ) . not . toBe ( newUrl ) ;
260+ } ) . then ( done ) ;
261+ } ) ;
262+
263+ it ( 'should remove the image tag if an invalid source' , function ( done ) {
264+
265+ var selection = Plotly . d3 . select ( 'image' ) ;
266+ expect ( selection . size ( ) ) . toBe ( 1 ) ;
267+
268+ Plotly . relayout ( gd , 'images[0].source' , 'invalidUrl' ) . then ( function ( ) {
269+ var newSelection = Plotly . d3 . select ( 'image' ) ;
270+ expect ( newSelection . size ( ) ) . toBe ( 0 ) ;
271+ } ) . then ( done ) ;
272+ } ) ;
273+ } ) ;
274+
230275} ) ;
0 commit comments