subscribe-ui-event
Advanced tools
Comparing version 0.2.10 to 1.0.0
@@ -7,2 +7,7 @@ /** | ||
var globalVars = require('./globalVars'); | ||
var resize = { | ||
width: 0, | ||
height: 0 | ||
}; | ||
var scroll = { | ||
@@ -12,7 +17,16 @@ delta: 0, | ||
}; | ||
var resize = { | ||
width: 0, | ||
height: 0 | ||
}; | ||
// global variables | ||
var doc; | ||
var docBody; | ||
var docEl; | ||
var win; | ||
if (typeof window !== 'undefined') { | ||
win = window; | ||
doc = win.document || document; | ||
docEl = doc.documentElement; | ||
docBody = doc.body; | ||
} | ||
/** | ||
@@ -30,2 +44,22 @@ * ArgmentedEvent will hold some global information, such like window scroll postion, | ||
ArgmentedEvent.prototype = { | ||
update: function update(mainType) { | ||
var top; | ||
if (globalVars.enableScrollInfo && (mainType === 'scroll' || mainType === 'touchmove')) { | ||
top = docEl.scrollTop + docBody.scrollTop; | ||
// Prevent delta from being 0 | ||
if (top !== this.scroll.top) { | ||
this.scroll.delta = top - this.scroll.top; | ||
this.scroll.top = top; | ||
console.log(top); | ||
} | ||
} | ||
if (globalVars.enableResizeInfo && mainType === 'resize') { | ||
this.resize.width = win.innerWidth || docEl.clientWidth; | ||
this.resize.height = win.innerHeight || docEl.clientHeight; | ||
} | ||
} | ||
}; | ||
module.exports = ArgmentedEvent; |
@@ -17,2 +17,4 @@ /** | ||
throttle = throttle || 15; | ||
var later = function later() { | ||
@@ -19,0 +21,0 @@ var now = getTime(); |
@@ -7,15 +7,13 @@ /** | ||
var ee = require('./eventEmitter').eventEmitter; | ||
var ehs = require('./eventEmitter').eventHandlers; | ||
var emptyFunction = function emptyFunction() {}; | ||
var eventHandlers = require('./eventHandlers'); | ||
var globalVars = require('./globalVars'); | ||
var leIE8 = require('./lib/leIE8'); // less then or equal to IE8 | ||
var rAFThrottle = require('./lib/rAFThrottle'); | ||
var subscriptions = require('./eventEmitter').subscriptions; | ||
var throttle = require('lodash.throttle'); | ||
var mainEventConnectors = require('./mainEventConnectors'); | ||
// constants | ||
var DEFAULT_THROTTLE_RATE = require('./constants').DEFAULT_THROTTLE_RATE; | ||
/** | ||
* Subscribe to UI events. | ||
* @method subscribe | ||
* @param {String} eventType - The type of event. | ||
* @param {String} type - The type of event. | ||
* @param {Function} cb - The callback function. | ||
@@ -27,25 +25,16 @@ * @param {Object} options.context - The caller. | ||
*/ | ||
function subscribe(eventType, cb, options) { | ||
if (!eventHandlers[eventType] || !cb) { | ||
return { | ||
unsubscribe: emptyFunction | ||
}; | ||
} | ||
function subscribe(type, cb, options) { | ||
options = options || {}; | ||
var context = options.context || null; | ||
var eeType; // emitEmitterType = eventType + ':' + throttle | ||
var enableScrollInfo = options.enableScrollInfo || false; | ||
var enableResizeInfo = options.enableResizeInfo || false; | ||
var sub; | ||
var throttleFunc; | ||
var throttleRate = parseInt(options.throttleRate); | ||
var useRAF = options.useRAF || false; | ||
var throttleRate = parseInt(options.throttleRate, 10); | ||
throttleFunc = useRAF ? rAFThrottle : throttle; | ||
if (isNaN(throttleRate)) { | ||
throttleRate = useRAF ? 15 : 50; // 15ms will be equivalent to 1 rAF | ||
throttleRate = DEFAULT_THROTTLE_RATE; | ||
} | ||
if (useRAF) { | ||
throttleRate = 'raf'; | ||
} | ||
// turn off throttle if the browser is IE8 or less, because window.event will be reset | ||
@@ -57,38 +46,9 @@ // when using any delayed function, i.g., setTimeout, or rAF. | ||
// eeType is throttled event type, such like "scroll:50", meaning scroll event with 50ms throttle rate | ||
eeType = eventType + ':' + throttleRate + (useRAF ? ':raf' : ''); | ||
// once those variables enabled, then never disabled. | ||
globalVars.enableScrollInfo = globalVars.enableScrollInfo || options.enableScrollInfo || false; | ||
globalVars.enableResizeInfo = globalVars.enableResizeInfo || options.enableResizeInfo || false; | ||
// wire UI event to throttled event, for example, wire "window.scroll" to "scroll:50" | ||
// add event listeners to UI event for the same throttled event | ||
eventHandlers[eventType](eeType, { | ||
enableScrollInfo: enableScrollInfo, | ||
enableResizeInfo: enableResizeInfo, | ||
throttleFunc: throttleFunc, | ||
throttleRate: throttleRate | ||
}); | ||
// wire to throttled event | ||
ee.on(eeType, cb, context); | ||
// append sub to subscriptions | ||
sub = { | ||
_cb: cb, | ||
_eventType: eventType, | ||
unsubscribe: function unsubscribe() { | ||
var i = subscriptions.indexOf(sub); | ||
ee.removeListener(eeType, cb); | ||
if (i !== -1) { | ||
subscriptions.splice(i, 1); | ||
} | ||
if (!ee.listeners(eeType, true) && ehs[eeType]) { | ||
ehs[eeType].remove(); | ||
ehs[eeType] = undefined; | ||
} | ||
} | ||
}; | ||
subscriptions.push(sub); | ||
return sub; | ||
return mainEventConnectors[type](throttleRate, cb, options.context); | ||
} | ||
module.exports = subscribe; |
@@ -131,2 +131,3 @@ // jscs:disable maximumLineLength | ||
src: [ | ||
'<%= project.unit %>/../lib/**/*.*', | ||
'<%= project.unit %>/**/*.*' | ||
@@ -133,0 +134,0 @@ ], |
@@ -15,10 +15,8 @@ /** | ||
module.exports = { | ||
subscribe: require('./dist/subscribe'), | ||
unsubscribe: require('./dist/unsubscribe') | ||
subscribe: require('./dist/subscribe') | ||
}; | ||
} else { | ||
module.exports = { | ||
subscribe: warn, | ||
unsubscribe: warn | ||
subscribe: warn | ||
}; | ||
} |
{ | ||
"name": "subscribe-ui-event", | ||
"version": "0.2.10", | ||
"version": "1.0.0", | ||
"description": "A single, throttle built-in solution to subscribe to browser UI Events.", | ||
@@ -28,2 +28,3 @@ "main": "index.js", | ||
"lodash.throttle": "^3.0.3", | ||
"lodash.clone": "^3.0.3", | ||
"raf": "^3.0.0" | ||
@@ -49,3 +50,2 @@ }, | ||
"istanbul": "^0.3.0", | ||
"jsdom": "^6.0.0", | ||
"jshint": "^2.5.1", | ||
@@ -55,2 +55,3 @@ "minimist": "^1.0.0", | ||
"mockery": "^1.4.0", | ||
"node-jsdom": "^3.0.0", | ||
"pre-commit": "^1.0.0", | ||
@@ -57,0 +58,0 @@ "react": "^0.14.0", |
@@ -47,3 +47,3 @@ # subscribe-ui-event | ||
Provide throttled version of window or document events, such like `scroll`, `resize` and `visibilitychange` to subscribe. It also provides some higher, compound events, such like `viewportchange`, which combines `scroll`, `resize` and `visibilitychange` events. | ||
Provide throttled version of window or document events, such like `scroll`, `resize` and `visibilitychange` to subscribe. | ||
@@ -106,13 +106,4 @@ **Note on IE8 or the below, the throttle will be turned off because the event object is global and will be deleted for setTimeout or rAF.** | ||
6. resizeEnd - The end window.resize | ||
7. visibilitychange - document.visibilitychange | ||
8. viewportchange - scroll + resize + visibilitychange | ||
7. visibilitychange - document.visibilitychange (IE8 doesn't support) | ||
### unsubscribe | ||
```js | ||
Void unsubscribe(String eventType, Function callback) | ||
``` | ||
Unsubscribe an event. **Note that all subscriptions with the same eventHandler and the same event type will be unsubscribed together even if they have different options**. | ||
## License | ||
@@ -119,0 +110,0 @@ |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
15
1
31495
4
675
111
+ Addedlodash.clone@^3.0.3
+ Addedlodash._arraycopy@3.0.0(transitive)
+ Addedlodash._arrayeach@3.0.0(transitive)
+ Addedlodash._baseassign@3.2.0(transitive)
+ Addedlodash._baseclone@3.3.0(transitive)
+ Addedlodash._basecopy@3.0.1(transitive)
+ Addedlodash._basefor@3.0.3(transitive)
+ Addedlodash._bindcallback@3.0.1(transitive)
+ Addedlodash._isiterateecall@3.0.9(transitive)
+ Addedlodash.clone@3.0.3(transitive)
+ Addedlodash.isarguments@3.1.0(transitive)
+ Addedlodash.isarray@3.0.4(transitive)
+ Addedlodash.keys@3.1.2(transitive)