Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jun 26, 2014
0 parents commit d1920c9
Show file tree
Hide file tree
Showing 9 changed files with 211 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store

node_modules/
npm-debug.log
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright (C) 2014 Kevin Ingersoll

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Viewport plugin for [Vue](http://vuejs.org/)

This [Vue plugin](http://vuejs.org/guide/plugin.html) adds a `detect-viewport` directive, allowing you to detect viewport enter and leave events in your VM.

## Usage

Follow the [official Vue plugin](http://vuejs.org/guide/plugin.html) documentation for installation instructions.

Once the plugin is added to your project, you can add a `v-detect-viewport` attribute to any VM element and then listen to `viewportenter` and `viewportleave` events.

```html
<div v-component="my-component" v-detect-viewport>
<p>Am I in the viewport?</p>
</div>
```

```js
Vue.component('my-component', {
created: function () {
this.$on('viewportenter', function () {
console.log('I have entered the viewport.')
})
this.$on('viewportleave', function () {
console.log('I have left the viewport.')
})
}
})
```
62 changes: 62 additions & 0 deletions dist/vue-viewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
function isElementInViewport (el) {
var rect = el.getBoundingClientRect()
return rect.bottom > 0
&& rect.right > 0
&& rect.top < (window.innerHeight || document.documentElement.clientHeight)
&& rect.left < (window.innerWidth || document.documentElement.clientWidth)
}


var directives = []

function notify (directive) {
if (!directive.el) return

var inViewport = isElementInViewport(directive.el)
if (directive.inViewport == null || directive.inViewport !== inViewport) {
directive.inViewport = inViewport
var direction = inViewport ? 'enter' : 'leave'
directive.vm.$emit('viewport' + direction, directive.el)
}
}

function notifyAll () {
directives.forEach(notify)
}


['DOMContentLoaded', 'load', 'scroll', 'resize', 'popstate'].forEach(function (event) {
window.addEventListener(event, notifyAll, false)
})


module.exports = {
isEmpty: true,

bind: function () {
this.vm.$on('hook:attached', notifyAll)
this.vm.$on('hook:detached', notifyAll)

if (directives.indexOf(this) === -1) {
directives.push(this)
}
},

unbind: function () {
this.vm.$off('hook:attached', notifyAll)
this.vm.$off('hook:detached', notifyAll)

var index = directives.indexOf(this)
if (index > -1) {
directives.splice(index, 1)
}
}
}

},{}],2:[function(require,module,exports){
exports.install = function (Vue, options) {
Vue.directive('detect-viewport', require('./directives/detect-viewport'))
}

},{"./directives/detect-viewport":1}]},{},[2])
1 change: 1 addition & 0 deletions dist/vue-viewport.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var gulp = require('gulp')
var browserify = require('browserify')
var source = require('vinyl-source-stream')
var uglify = require('gulp-uglify')
var rename = require('gulp-rename')

gulp.task('build:js', function () {
return browserify('./src/index.js')
.external(['vue'])
.bundle()
.pipe(source('vue-viewport.js'))
.pipe(gulp.dest('dist'))
})

gulp.task('build', ['build:js'], function () {
return gulp.src(['dist/**/*.js', '!**/*.min.js'])
.pipe(uglify())
.pipe(rename({ suffix: '.min' }))
.pipe(gulp.dest('dist'))
})

gulp.task('default', ['build'])
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "vue-viewport",
"version": "0.0.1",
"description": "Vue plugin for detecting when elements enter and leave the viewport",
"keywords": [
"vue",
"viewport"
],
"main": "src/index.js",
"repository": {
"type": "git",
"url": "https://github.com/holic/vue-viewport.git"
},
"bugs": "https://github.com/holic/vue-viewport/issues",
"homepage": "https://github.com/holic/vue-viewport",
"author": {
"name": "Kevin Ingersoll",
"email": "kingersoll@gmail.com",
"url": "http://keviningersoll.com/"
},
"license": "MIT",
"devDependencies": {
"vue": "~0.10.5",
"gulp": "~3.8.2",
"vinyl-source-stream": "~0.1.1",
"browserify": "~4.1.11",
"gulp-rename": "~1.2.0",
"gulp-uglify": "~0.3.1"
}
}
54 changes: 54 additions & 0 deletions src/directives/detect-viewport.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function isElementInViewport (el) {
var rect = el.getBoundingClientRect()
return rect.bottom > 0
&& rect.right > 0
&& rect.top < (window.innerHeight || document.documentElement.clientHeight)
&& rect.left < (window.innerWidth || document.documentElement.clientWidth)
}


var directives = []

function notify (directive) {
if (!directive.el) return

var inViewport = isElementInViewport(directive.el)
if (directive.inViewport == null || directive.inViewport !== inViewport) {
directive.inViewport = inViewport
var direction = inViewport ? 'enter' : 'leave'
directive.vm.$emit('viewport' + direction, directive.el)
}
}

function notifyAll () {
directives.forEach(notify)
}


['DOMContentLoaded', 'load', 'scroll', 'resize', 'popstate'].forEach(function (event) {
window.addEventListener(event, notifyAll, false)
})


module.exports = {
isEmpty: true,

bind: function () {
this.vm.$on('hook:attached', notifyAll)
this.vm.$on('hook:detached', notifyAll)

if (directives.indexOf(this) === -1) {
directives.push(this)
}
},

unbind: function () {
this.vm.$off('hook:attached', notifyAll)
this.vm.$off('hook:detached', notifyAll)

var index = directives.indexOf(this)
if (index > -1) {
directives.splice(index, 1)
}
}
}
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
exports.install = function (Vue, options) {
Vue.directive('detect-viewport', require('./directives/detect-viewport'))
}

0 comments on commit d1920c9

Please sign in to comment.