@@ -63,6 +63,124 @@ QUnit.module('linkTools', function(hooks) {
63
63
} ) ;
64
64
} ) ;
65
65
66
+ QUnit . module ( 'Visibility' , function ( ) {
67
+
68
+ QUnit . test ( 'isVisible()' , function ( assert ) {
69
+ const remove = new joint . linkTools . Remove ( ) ;
70
+ assert . ok ( remove . isVisible ( ) ) ;
71
+ remove . hide ( ) ;
72
+ assert . notOk ( remove . isVisible ( ) ) ;
73
+ remove . show ( ) ;
74
+ assert . ok ( remove . isVisible ( ) ) ;
75
+ } ) ;
76
+
77
+ QUnit . test ( 'updateVisibility()' , function ( assert ) {
78
+ const remove = new joint . linkTools . Remove ( ) ;
79
+ const toolsView = new joint . dia . ToolsView ( { tools : [ remove ] } ) ;
80
+ linkView . addTools ( toolsView ) ;
81
+ assert . notEqual ( getComputedStyle ( remove . el ) . display , 'none' ) ;
82
+ remove . hide ( ) ;
83
+ assert . equal ( getComputedStyle ( remove . el ) . display , 'none' ) ;
84
+ remove . show ( ) ;
85
+ assert . notEqual ( getComputedStyle ( remove . el ) . display , 'none' ) ;
86
+ } ) ;
87
+
88
+ QUnit . module ( 'option: visibility' , function ( assert ) {
89
+
90
+ QUnit . test ( 'is visible or hidden' , function ( assert ) {
91
+ let isVisible = true ;
92
+ const visibilitySpy = sinon . spy ( ( ) => isVisible ) ;
93
+ const removeButton = new joint . linkTools . Remove ( {
94
+ visibility : visibilitySpy
95
+ } ) ;
96
+ const otherButton = new joint . linkTools . Button ( ) ;
97
+ const toolsView = new joint . dia . ToolsView ( {
98
+ tools : [
99
+ removeButton ,
100
+ otherButton
101
+ ]
102
+ } ) ;
103
+ linkView . addTools ( toolsView ) ;
104
+
105
+ // Initial state.
106
+ assert . notEqual ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
107
+ assert . ok ( removeButton . isVisible ( ) ) ;
108
+ assert . equal ( visibilitySpy . callCount , 1 ) ;
109
+ assert . ok ( visibilitySpy . calledWithExactly ( linkView , removeButton ) ) ;
110
+ assert . ok ( visibilitySpy . calledOn ( removeButton ) ) ;
111
+
112
+ // Visibility function should be called on update.
113
+ isVisible = false ;
114
+ toolsView . update ( ) ;
115
+ assert . equal ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
116
+ assert . notOk ( removeButton . isVisible ( ) ) ;
117
+ assert . ok ( removeButton . isExplicitlyVisible ( ) ) ;
118
+ assert . equal ( visibilitySpy . callCount , 2 ) ;
119
+ assert . ok ( visibilitySpy . calledWithExactly ( linkView , removeButton ) ) ;
120
+ assert . ok ( visibilitySpy . calledOn ( removeButton ) ) ;
121
+
122
+ // Other button should not be affected by the visibility function.
123
+ assert . notEqual ( getComputedStyle ( otherButton . el ) . display , 'none' ) ;
124
+ assert . ok ( otherButton . isVisible ( ) ) ;
125
+
126
+ // Focus & blur on other button should not change the visibility of
127
+ // the remove button.
128
+ toolsView . focusTool ( otherButton ) ;
129
+ assert . equal ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
130
+ assert . notOk ( removeButton . isVisible ( ) ) ;
131
+ assert . notOk ( removeButton . isExplicitlyVisible ( ) ) ;
132
+ toolsView . blurTool ( otherButton ) ;
133
+ assert . equal ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
134
+ assert . notOk ( removeButton . isVisible ( ) ) ;
135
+ assert . ok ( removeButton . isExplicitlyVisible ( ) ) ;
136
+
137
+ isVisible = true ;
138
+ toolsView . update ( ) ;
139
+ toolsView . focusTool ( otherButton ) ;
140
+ assert . equal ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
141
+ assert . notOk ( removeButton . isVisible ( ) ) ;
142
+ assert . notOk ( removeButton . isExplicitlyVisible ( ) ) ;
143
+ toolsView . blurTool ( otherButton ) ;
144
+ assert . notEqual ( getComputedStyle ( removeButton . el ) . display , 'none' ) ;
145
+ assert . ok ( removeButton . isVisible ( ) ) ;
146
+ assert . ok ( removeButton . isExplicitlyVisible ( ) ) ;
147
+ } ) ;
148
+
149
+ QUnit . test ( 'it\'s not updated when hidden' , function ( assert ) {
150
+ const button1 = new joint . linkTools . Button ( {
151
+ visibility : ( ) => false
152
+ } ) ;
153
+ const button2 = new joint . linkTools . Button ( {
154
+ visibility : ( ) => true
155
+ } ) ;
156
+ const button1UpdateSpy = sinon . spy ( button1 , 'update' ) ;
157
+ const button2UpdateSpy = sinon . spy ( button2 , 'update' ) ;
158
+ const toolsView = new joint . dia . ToolsView ( {
159
+ tools : [ button1 , button2 ]
160
+ } ) ;
161
+ linkView . addTools ( toolsView ) ;
162
+ assert . equal ( button1 . update . callCount , 0 ) ;
163
+ assert . equal ( button2 . update . callCount , 1 ) ;
164
+ toolsView . update ( ) ;
165
+ assert . equal ( button1 . update . callCount , 0 ) ;
166
+ assert . equal ( button2 . update . callCount , 2 ) ;
167
+ button1 . show ( ) ;
168
+ button2 . hide ( ) ;
169
+ toolsView . update ( ) ;
170
+ assert . equal ( button1 . update . callCount , 0 ) ;
171
+ assert . equal ( button2 . update . callCount , 2 ) ;
172
+ toolsView . focusTool ( null ) ; // hide all
173
+ assert . equal ( button1 . update . callCount , 0 ) ;
174
+ assert . equal ( button2 . update . callCount , 2 ) ;
175
+ toolsView . blurTool ( null ) ; // show all
176
+ assert . equal ( button1 . update . callCount , 0 ) ;
177
+ assert . equal ( button2 . update . callCount , 3 ) ;
178
+ button1UpdateSpy . restore ( ) ;
179
+ button2UpdateSpy . restore ( ) ;
180
+ } ) ;
181
+ } ) ;
182
+ } ) ;
183
+
66
184
QUnit . module ( 'TargetAnchor' , function ( ) {
67
185
[ {
68
186
resetAnchor : true ,
0 commit comments