Skip to content

Commit

Permalink
v1.2.0 (#1)
Browse files Browse the repository at this point in the history
* Updating package dependencies

* Adding init config and correcting JSDoc comments

* Adding unit tests for public methods

* Using const keyword, removing debugger

* adding circleci for testing

* updating circleci config

* circle ci fix

* 1.2.0

* updating readme v1.2.0
  • Loading branch information
dgautsch committed Apr 27, 2019
1 parent db3fa54 commit 0b5932d
Show file tree
Hide file tree
Showing 7 changed files with 5,814 additions and 1,217 deletions.
22 changes: 22 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
version: 2
executorType: docker
containerInfo:
- image: node:8
jobs:
build:
workDir: ~/scrollfire
steps:
- type: checkout
- type: cache-restore
key: scrollfire-{{checksum "package.json"}}
- run: |
set -x
node -v
npm i
npm test
./node_modules/.bin/standard
- type: cache-save
key: cli-engine-{{checksum "package.json"}}
paths:
- /usr/local/share/.cache/npm
- ~/scrollfire/node_modules
20 changes: 16 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Scrollfire
[![Known Vulnerabilities](https://snyk.io/test/github/dgautsch/scrollfire/badge.svg?targetFile=package.json)](https://snyk.io/test/github/dgautsch/scrollfire?targetFile=package.json) [![npm version](https://badge.fury.io/js/scrollfire.svg)](https://badge.fury.io/js/scrollfire)
[![Known Vulnerabilities](https://snyk.io/test/github/dgautsch/scrollfire/badge.svg?targetFile=package.json)](https://snyk.io/test/github/dgautsch/scrollfire?targetFile=package.json) [![npm version](https://badge.fury.io/js/scrollfire.svg)](https://badge.fury.io/js/scrollfire) [![CircleCI](https://circleci.com/gh/dgautsch/scrollfire/tree/master.svg?style=svg)](https://circleci.com/gh/dgautsch/scrollfire/tree/master)

Scrollfire allows you to set functions that will fire when specific DOM elements of your website come into a user's viewport. A typical use case would be an animation that needs to fire when the element comes into view.

Expand Down Expand Up @@ -28,8 +28,20 @@ You can also remove actions later on if for some reason you don't want them to r

### Configuration Options

In addition to `name` and `method` you can pass in `persist` if you wish the action to fire for an unlimited number of times.
You can initialize and pass a config option to to the init method. The two parameters that can be changed are `viewportTop` and `viewportBottom`.

```javascript
var scrollfire = require('scrollfire')
scrollfire.init({
viewportTop: 0.1,
viewportBottom: 1
})
```
This allows you to widen the viewport window for when the event actions fire. They are percentages of the top and bottom of the viewport. In the example above 0.1 would be the top 10% of the viewport and 1 would be the very bottom. The defaults are 0.3 and 0.6 respectively.

## To Do
In addition to `name` and `method` you can pass in `persist` if you wish the action to fire for an unlimited number of times.

- [ ] Unit Tests
```javascript
var scrollfire = require('scrollfire')
scrollfire.addAction(Elem, {name: 'myName', method: myFunction, persist: true})
```
93 changes: 57 additions & 36 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* Scrollfire
* @module Scrollfire
*
*/
var Scrollfire = (function () {
/**
* Polyfill for CustomEvent on IE9+
Expand All @@ -18,12 +23,17 @@ var Scrollfire = (function () {
window.CustomEvent = CustomEvent
})()

var actionQueue = []
var initialized = false
/**
* An array of actions created
*
*/
const actionStore = []
let initialized = false

/**
* scrollActionAdd - Creates a custom event to add a ScrollAction
*
* @param {Object} scrollAction - The scroll action object
* @returns {event} scrollActionAdd event
*/
function scrollActionAdd (scrollAction) {
Expand All @@ -35,6 +45,7 @@ var Scrollfire = (function () {
/**
* scrollActionRemove - Creates a custom event to remove a ScrollAction
*
* @param {string} actionName - The action name to remove
* @returns {event} scrollActionRemove event
*/
function scrollActionRemove (actionName) {
Expand All @@ -48,10 +59,10 @@ var Scrollfire = (function () {
*
* @type constructor
*
* @param {object} element - A DOM element or jQuery element object
* @param {object} action - action - An object with the action parameters
* @param {string} action.name - The name of the action
* @param {callback} action.method - The method to fire
* @param {Object} element - A DOM element or jQuery element object
* @param {Object} config - config - An object with the config parameters
* @param {string} config.name - The name of the config
* @param {callback} config.method - The method to fire
*/
function ScrollAction (element, config) {
if (!config.name) {
Expand Down Expand Up @@ -79,50 +90,52 @@ var Scrollfire = (function () {
* @param {Object} elem - A DOM element or jQuery element object
* @param {Object} config - An object with the action parameters
* @param {string} config.name - The name of the action
* @param {callback} config.method - The method to fire
* @param {function} config.method - The method to fire
* @param {boolean} config.persist - True to fire action endlessly
*/
function addAction (elem, config) {
if (!initialized) throw new Error('Scrollfire has not been initialized, you must call scrollfire.init()')
var taskAction = new ScrollAction(elem, config)
const taskAction = new ScrollAction(elem, config)
document.dispatchEvent(scrollActionAdd(taskAction))
}

/**
* removeAction - Removes an action from the action queue
*
* @param {string} actionMethodName - The name of the action to remove
* @param {string} actionName - The name of the action to remove
*/
function removeAction (actionName) {
if (!initialized) throw new Error('Scrollfire has not been initialized, you must call scrollfire.init()')
document.dispatchEvent(scrollActionRemove(actionName))
}

function actionController () {
/**
* scrollWatch - Watches for scroll events and checks the y position
*
*/
function bootstrap (config) {
document.addEventListener('scrollActionAdd', function (e) {
var data = e.detail
actionQueue.push(data)
const data = e.detail
actionStore.push(data)
})

document.addEventListener('scrollActionRemove', function (e) {
var data = e.detail
actionQueue.find(function (elem, idx, arr) {
const data = e.detail
actionStore.forEach(function (elem, idx, arr) {
if (!elem || !elem.hasOwnProperty('actionName')) return
if (elem.actionName === data) {
arr.splice(idx, 1)
}
})
})
}

/**
* scrollWatch - Watches for scroll events and checks the y position
*
*/
function scrollWatch () {
var viewportWatch = debounce(function () {
var viewportTop = window.innerHeight * 0.3
var viewportBottom = window.innerHeight * 0.6
actionQueue.find(function (elem) {
window.addEventListener('scroll', debounce(function () {
const viewportTop = window.innerHeight * config.viewportTop
const viewportBottom = window.innerHeight * config.viewportBottom

actionStore.find(function (elem) {
if (elem.element) {
var elemTop = elem.element.getBoundingClientRect().top
const elemTop = elem.element.getBoundingClientRect().top
if (elemTop >= viewportTop && elemTop <= viewportBottom && !elem.hasFired) {
if (!elem.persist) {
elem.hasFired = true // only fire the animation once
Expand All @@ -131,8 +144,7 @@ var Scrollfire = (function () {
}
}
})
}, 10, true)
window.addEventListener('scroll', viewportWatch)
}, 10, true))
}

/**
Expand All @@ -147,15 +159,15 @@ var Scrollfire = (function () {
* @return {function}
*/
function debounce (func, wait, immediate) {
var timeout
let timeout
return function () {
var context = this
var args = arguments
var later = function () {
const context = this
const args = arguments
const later = function () {
timeout = null
if (!immediate) func.apply(context, args)
}
var callNow = immediate && !timeout
const callNow = immediate && !timeout
clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) func.apply(context, args)
Expand All @@ -166,22 +178,31 @@ var Scrollfire = (function () {
* init - initializes the Scrollfire module
*
*/
function init () {
function init (config) {
const defaults = Object.assign({}, {
viewportTop: 0.3,
viewportBottom: 0.6
}, config)

if (!initialized) {
initialized = true
} else {
console.warn('Scrollfire has already been initialized')
return
}
actionController()
scrollWatch()
bootstrap(defaults)
}

return {
actionStore: actionStore,
addAction: addAction,
removeAction: removeAction,
init: init
}
})()

module.exports = Scrollfire
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined') {
module.exports = Scrollfire
} else {
window.Scrollfire = Scrollfire
}
7 changes: 7 additions & 0 deletions jsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"typeAcquisition": {
"include": [
"jest"
]
}
}

0 comments on commit 0b5932d

Please sign in to comment.