Skip to content

Commit

Permalink
add: lazyload Private functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hilongjw committed Mar 29, 2017
1 parent 0a4866b commit 475c72e
Showing 1 changed file with 66 additions and 63 deletions.
129 changes: 66 additions & 63 deletions src/lazy.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function (Vue) {
filter: filter || {},
adapter: adapter || {}
}
this.initEvent()
this._initEvent()

this.lazyLoadHandler = throttle(() => {
let catIn = false
Expand All @@ -59,6 +59,20 @@ export default function (Vue) {
assign(this.options, options)
}

/**
* output listener's load performance
* @return {Array}
*/
performance () {
let list = []

this.ListenerQueue.map(item => {
list.push(item.performance())
})

return list
}

/**
* add lazy component to queue
* @param {Vue} vm lazy component instance
Expand All @@ -74,47 +88,6 @@ export default function (Vue) {
}
}

/**
* add listener target
* @param {DOM} el listener target
* @return
*/
_addListenerTarget (el) {
if (!el) return
let target = find(this.TargetQueue, target => target.el === el)
if (!target) {
target = {
el: el,
id: ++this.TargetIndex,
childrenCount: 1,
listened: true
}
this.initListen(target.el, true)
this.TargetQueue.push(target)
} else {
target.childrenCount++
}
return this.TargetIndex
}

/**
* remove listener target or reduce target childrenCount
* @param {DOM} el or window
* @return
*/
_removeListenerTarget (el) {
this.TargetQueue.forEach((target, index) => {
if (target.el === el) {
target.childrenCount--
if (!target.childrenCount) {
this.initListen(target.el, false)
this.TargetQueue.splice(index, 1)
target = null
}
}
})
}

/**
* add image listener to queue
* @param {DOM} el
Expand All @@ -128,7 +101,7 @@ export default function (Vue) {
return Vue.nextTick(this.lazyLoadHandler)
}

let { src, loading, error } = this.valueFormatter(binding.value)
let { src, loading, error } = this._valueFormatter(binding.value)

Vue.nextTick(() => {
src = getBestSelectionFromSrcset(el, this.options.scale) || src
Expand All @@ -153,7 +126,7 @@ export default function (Vue) {
loading,
error,
src,
elRenderer: this.elRenderer.bind(this),
elRenderer: this._elRenderer.bind(this),
options: this.options
})

Expand All @@ -168,14 +141,14 @@ export default function (Vue) {
})
}

/**
/**
* update image src
* @param {DOM} el
* @param {object} vue directive binding
* @return
*/
update (el, binding) {
let { src, loading, error } = this.valueFormatter(binding.value)
let { src, loading, error } = this._valueFormatter(binding.value)

const exist = find(this.ListenerQueue, item => item.el === el)

Expand Down Expand Up @@ -216,18 +189,61 @@ export default function (Vue) {
}
this._removeListenerTarget(window)
}

/**** Private functions ****/

/**
* add listener target
* @param {DOM} el listener target
* @return
*/
_addListenerTarget (el) {
if (!el) return
let target = find(this.TargetQueue, target => target.el === el)
if (!target) {
target = {
el: el,
id: ++this.TargetIndex,
childrenCount: 1,
listened: true
}
this._initListen(target.el, true)
this.TargetQueue.push(target)
} else {
target.childrenCount++
}
return this.TargetIndex
}

/**
* remove listener target or reduce target childrenCount
* @param {DOM} el or window
* @return
*/
_removeListenerTarget (el) {
this.TargetQueue.forEach((target, index) => {
if (target.el === el) {
target.childrenCount--
if (!target.childrenCount) {
this._initListen(target.el, false)
this.TargetQueue.splice(index, 1)
target = null
}
}
})
}

/**
* add or remove eventlistener
* @param {DOM} el DOM or Window
* @param {boolean} start flag
* @return
*/
initListen (el, start) {
_initListen (el, start) {
this.options.ListenEvents.forEach((evt) => _[start ? 'on' : 'off'](el, evt, this.lazyLoadHandler))
}

initEvent () {
_initEvent () {
this.Event = {
listeners: {
loading: [],
Expand Down Expand Up @@ -262,19 +278,6 @@ export default function (Vue) {
}
}

/**
* output listener's load performance
* @return {Array}
*/
performance () {
let list = []

this.ListenerQueue.map(item => {
list.push(item.performance())
})

return list
}

/**
* set element attribute with image'url and state
Expand All @@ -283,7 +286,7 @@ export default function (Vue) {
* @param {bool} inCache is rendered from cache
* @return
*/
elRenderer (listener, state, cache) {
_elRenderer (listener, state, cache) {
if (!listener.el) return
const { el, bindType } = listener

Expand Down Expand Up @@ -317,7 +320,7 @@ export default function (Vue) {
* @param {string} image's src
* @return {object} image's loading, loaded, error url
*/
valueFormatter (value) {
_valueFormatter (value) {
let src = value
let loading = this.options.loading
let error = this.options.error
Expand Down

0 comments on commit 475c72e

Please sign in to comment.