1
1
import BImg from './img'
2
- import { isVisible , getBCR , eventOn , eventOff } from '../../utils/dom'
2
+ import { getBCR , eventOn , eventOff } from '../../utils/dom'
3
3
const THROTTLE = 100
4
4
5
5
// @vue /component
@@ -41,6 +41,10 @@ export default {
41
41
type : [ Number , String ] ,
42
42
default : null
43
43
} ,
44
+ show : {
45
+ type : Boolean ,
46
+ default : false
47
+ } ,
44
48
fluid : {
45
49
type : Boolean ,
46
50
default : false
@@ -93,7 +97,7 @@ export default {
93
97
return ( ! this . blankSrc || this . isShown ) ? this . src : this . blankSrc
94
98
} ,
95
99
computedBlank ( ) {
96
- return ! ( ( this . isShown || this . blankSrc ) )
100
+ return ! ( this . isShown || this . blankSrc )
97
101
} ,
98
102
computedWidth ( ) {
99
103
return this . isShown ? this . width : ( this . blankWidth || this . width )
@@ -102,15 +106,47 @@ export default {
102
106
return this . isShown ? this . height : ( this . blankHeight || this . height )
103
107
}
104
108
} ,
109
+ watch : {
110
+ show ( newVal , oldVal ) {
111
+ if ( newVal !== oldVal ) {
112
+ this . isShown = newVal
113
+ if ( ! newVal ) {
114
+ // Make sure listeners are re-enabled if img is force set to blank
115
+ this . setListeners ( true )
116
+ }
117
+ }
118
+ } ,
119
+ isShown ( newVal , oldVal ) {
120
+ if ( newVal !== oldVal ) {
121
+ // Update synched show prop
122
+ this . $emit ( 'update:show' , newVal )
123
+ }
124
+ }
125
+ } ,
126
+ created ( ) {
127
+ this . isShown = this . show
128
+ } ,
105
129
mounted ( ) {
106
- this . setListeners ( true )
107
- this . checkView ( )
130
+ if ( this . isShown ) {
131
+ this . setListeners ( false )
132
+ } else {
133
+ this . setListeners ( true )
134
+ this . $nextTick ( this . checkView )
135
+ }
108
136
} ,
109
137
activated ( ) {
110
- this . setListeners ( true )
111
- this . checkView ( )
138
+ /* istanbul ignore if */
139
+ if ( ! this . isShown ) {
140
+ this . setListeners ( true )
141
+ this . $nextTick ( this . checkView )
142
+ }
112
143
} ,
113
144
deactivated ( ) {
145
+ /* istanbul ignore next */
146
+ this . setListeners ( false )
147
+ } ,
148
+ beforeDestroy ( ) {
149
+ /* istanbul ignore next */
114
150
this . setListeners ( false )
115
151
} ,
116
152
methods : {
@@ -119,19 +155,23 @@ export default {
119
155
this . scrollTimeout = null
120
156
const root = window
121
157
if ( on ) {
158
+ eventOn ( this . $el , 'load' , this . checkView )
122
159
eventOn ( root , 'scroll' , this . onScroll )
123
160
eventOn ( root , 'resize' , this . onScroll )
124
161
eventOn ( root , 'orientationchange' , this . onScroll )
162
+ eventOn ( document , 'transitionend' , this . onScroll )
125
163
} else {
164
+ eventOff ( this . $el , 'load' , this . checkView )
126
165
eventOff ( root , 'scroll' , this . onScroll )
127
166
eventOff ( root , 'resize' , this . onScroll )
128
167
eventOff ( root , 'orientationchange' , this . onScroll )
168
+ eventOff ( document , 'transitionend' , this . onScroll )
129
169
}
130
170
} ,
131
- checkView ( ) {
171
+ checkView ( ) /* istanbul ignore next: can't test getBoundingClientRect in JSDOM */ {
132
172
// check bounding box + offset to see if we should show
133
- if ( ! isVisible ( this . $el ) ) {
134
- // Element is hidden, so skip for now
173
+ if ( this . isShown ) {
174
+ this . setListeners ( false )
135
175
return
136
176
}
137
177
const offset = parseInt ( this . offset , 10 ) || 0
@@ -142,7 +182,9 @@ export default {
142
182
b : docElement . clientHeight + offset ,
143
183
r : docElement . clientWidth + offset
144
184
}
185
+ /* istanbul ignore next */
145
186
const box = getBCR ( this . $el )
187
+ /* istanbul ignore if */
146
188
if ( box . right >= view . l && box . bottom >= view . t && box . left <= view . r && box . top <= view . b ) {
147
189
// image is in view (or about to be in view)
148
190
this . isShown = true
@@ -180,8 +222,5 @@ export default {
180
222
}
181
223
}
182
224
)
183
- } ,
184
- beforeDdestroy ( ) {
185
- this . setListeners ( false )
186
225
}
187
226
}
0 commit comments