-
Notifications
You must be signed in to change notification settings - Fork 871
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
awe
committed
Feb 10, 2018
1 parent
ca05689
commit 65019a5
Showing
14 changed files
with
974 additions
and
767 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,13 @@ | ||
{ | ||
"presets": [ | ||
["env", { "modules": false }] | ||
[ | ||
"env", | ||
{ | ||
"modules": false | ||
} | ||
] | ||
], | ||
"plugins": [ | ||
"external-helpers" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const DEBUG = !!process.env.DEBUG | ||
if (!DEBUG) process.env.CHROME_BIN = require('puppeteer').executablePath() | ||
|
||
module.exports = function (config) { | ||
config.set({ | ||
basePath: '', | ||
frameworks: ['mocha', 'chai'], | ||
plugins: [ | ||
require('karma-mocha'), | ||
require('karma-chai'), | ||
require('karma-chrome-launcher'), | ||
require('karma-rollup-preprocessor'), | ||
// require('karma-coverage') | ||
], | ||
files: [ | ||
{ pattern: 'test/*-spec.js', watched: false } | ||
], | ||
exclude: [], | ||
preprocessors: { | ||
'test/*-spec.js': ['rollup'] | ||
}, | ||
// coverageReporter: { | ||
// type: 'html', | ||
// dir: 'coverage/' | ||
// }, | ||
rollupPreprocessor: { | ||
plugins: [ | ||
require('rollup-plugin-babel')(), | ||
require('rollup-plugin-node-resolve')({ | ||
jsnext: true, | ||
browser: true | ||
}), | ||
require('rollup-plugin-replace')({ | ||
'process.env.NODE_ENV': JSON.stringify( 'production' ) | ||
}) | ||
], | ||
format: 'iife', // Helps prevent naming collisions. | ||
name: 'lib', // Required for 'iife' format. | ||
sourcemap: 'inline' // Sensible for testing. | ||
}, | ||
reporters: ['progress'], | ||
port: 9876, | ||
colors: true, | ||
logLevel: config.LOG_INFO, | ||
autoWatch: true, | ||
browsers: DEBUG ? ['Chrome'] : ['ChromeHeadless'], | ||
singleRun: !DEBUG, | ||
concurrency: Infinity | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,73 @@ | ||
import Lazy from './lazy' | ||
import LazyComponent from './lazy-component' | ||
import LazyContainer from './lazy-container' | ||
import { assign } from './util' | ||
|
||
export default { | ||
/** | ||
* install function | ||
* @param {Vue} Vue | ||
* @param {object} options lazyload options | ||
*/ | ||
install (Vue, options = {}) { | ||
const LazyClass = Lazy(Vue) | ||
const lazy = new LazyClass(options) | ||
/* | ||
* install function | ||
* @param {Vue} Vue | ||
* @param {object} options lazyload options | ||
*/ | ||
install (Vue, options = {}) { | ||
const LazyClass = Lazy(Vue) | ||
const lazy = new LazyClass(options) | ||
const lazyContainer = new LazyContainer({ lazy }) | ||
|
||
const isVueNext = Vue.version.split('.')[0] === '2' | ||
const isVue2 = Vue.version.split('.')[0] === '2' | ||
|
||
Vue.prototype.$Lazyload = lazy | ||
Vue.prototype.$Lazyload = lazy | ||
|
||
if (options.lazyComponent) { | ||
Vue.component('lazy-component', LazyComponent(lazy)) | ||
if (options.lazyComponent) { | ||
Vue.component('lazy-component', LazyComponent(lazy)) | ||
} | ||
|
||
if (isVue2) { | ||
Vue.directive('lazy', { | ||
bind: lazy.add.bind(lazy), | ||
update: lazy.update.bind(lazy), | ||
componentUpdated: lazy.lazyLoadHandler.bind(lazy), | ||
unbind: lazy.remove.bind(lazy) | ||
}) | ||
Vue.directive('lazy-container', { | ||
bind: lazyContainer.bind.bind(lazyContainer), | ||
update: lazyContainer.update.bind(lazyContainer), | ||
unbind: lazyContainer.unbind.bind(lazyContainer) | ||
}) | ||
} else { | ||
Vue.directive('lazy', { | ||
bind: lazy.lazyLoadHandler.bind(lazy), | ||
update (newValue, oldValue) { | ||
assign(this.vm.$refs, this.vm.$els) | ||
lazy.add(this.el, { | ||
modifiers: this.modifiers || {}, | ||
arg: this.arg, | ||
value: newValue, | ||
oldValue: oldValue | ||
}, { | ||
context: this.vm | ||
}) | ||
}, | ||
unbind () { | ||
lazy.remove(this.el) | ||
} | ||
}) | ||
|
||
if (isVueNext) { | ||
Vue.directive('lazy', { | ||
bind: lazy.add.bind(lazy), | ||
update: lazy.update.bind(lazy), | ||
componentUpdated: lazy.lazyLoadHandler.bind(lazy), | ||
unbind : lazy.remove.bind(lazy) | ||
}) | ||
} else { | ||
Vue.directive('lazy', { | ||
bind: lazy.lazyLoadHandler.bind(lazy), | ||
update (newValue, oldValue) { | ||
assign(this.vm.$refs, this.vm.$els) | ||
lazy.add(this.el, { | ||
modifiers: this.modifiers || {}, | ||
arg: this.arg, | ||
value: newValue, | ||
oldValue: oldValue | ||
}, { | ||
context: this.vm | ||
}) | ||
}, | ||
unbind () { | ||
lazy.remove(this.el) | ||
} | ||
}) | ||
Vue.directive('lazy-container', { | ||
update (newValue, oldValue) { | ||
lazyContainer.update(this.el, { | ||
modifiers: this.modifiers || {}, | ||
arg: this.arg, | ||
value: newValue, | ||
oldValue: oldValue | ||
}, { | ||
context: this.vm | ||
}) | ||
}, | ||
unbind () { | ||
lazyContainer.unbind(this.el) | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,52 @@ | ||
import { inBrowser } from './util' | ||
|
||
export default (lazy) => { | ||
return { | ||
props: { | ||
tag: { | ||
type: String, | ||
default: 'div' | ||
} | ||
return { | ||
props: { | ||
tag: { | ||
type: String, | ||
default: 'div' | ||
} | ||
}, | ||
render (h) { | ||
if (this.show === false) { | ||
return h(this.tag) | ||
} | ||
return h(this.tag, null, this.$slots.default) | ||
}, | ||
data () { | ||
return { | ||
el: null, | ||
state: { | ||
loaded: false | ||
}, | ||
render (h) { | ||
if (this.show === false) { | ||
return h(this.tag) | ||
} | ||
return h(this.tag, null, this.$slots.default) | ||
}, | ||
data () { | ||
return { | ||
el: null, | ||
state: { | ||
loaded: false | ||
}, | ||
rect: {}, | ||
show: false | ||
} | ||
}, | ||
mounted () { | ||
this.el = this.$el | ||
lazy.addLazyBox(this) | ||
lazy.lazyLoadHandler() | ||
}, | ||
beforeDestroy () { | ||
lazy.removeComponent(this) | ||
}, | ||
methods: { | ||
getRect () { | ||
this.rect = this.$el.getBoundingClientRect() | ||
}, | ||
checkInView () { | ||
this.getRect() | ||
return inBrowser && | ||
rect: {}, | ||
show: false | ||
} | ||
}, | ||
mounted () { | ||
this.el = this.$el | ||
lazy.addLazyBox(this) | ||
lazy.lazyLoadHandler() | ||
}, | ||
beforeDestroy () { | ||
lazy.removeComponent(this) | ||
}, | ||
methods: { | ||
getRect () { | ||
this.rect = this.$el.getBoundingClientRect() | ||
}, | ||
checkInView () { | ||
this.getRect() | ||
return inBrowser && | ||
(this.rect.top < window.innerHeight * lazy.options.preLoad && this.rect.bottom > 0) && | ||
(this.rect.left < window.innerWidth * lazy.options.preLoad && this.rect.right > 0) | ||
}, | ||
load () { | ||
this.show = true | ||
this.state.loaded = true | ||
this.$emit('show', this) | ||
} | ||
} | ||
}, | ||
load () { | ||
this.show = true | ||
this.state.loaded = true | ||
this.$emit('show', this) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.