Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual firing now invokes callback #89

Merged
merged 1 commit into from
Jun 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
{
// JSHint Default Configuration File (as on JSHint website)
// See http://jshint.com/docs/ for more details

"maxerr" : 50, // {int} Maximum error before stopping

// Enforcing
"bitwise" : true, // true: Prohibit bitwise operators (&, |, ^, etc.)
"camelcase" : false, // true: Identifiers must be in camelCase
"curly" : true, // true: Require {} for every new block or scope
"eqeqeq" : true, // true: Require triple equals (===) for comparison
"forin" : true, // true: Require filtering for..in loops with obj.hasOwnProperty()
"freeze" : true, // true: prohibits overwriting prototypes of native objects such as Array, Date etc.
"immed" : false, // true: Require immediate invocations to be wrapped in parens e.g. `(function () { } ());`
"indent" : 4, // {int} Number of spaces to use for indentation
"latedef" : false, // true: Require variables/functions to be defined before being used
"newcap" : false, // true: Require capitalization of all constructor functions e.g. `new F()`
"noarg" : true, // true: Prohibit use of `arguments.caller` and `arguments.callee`
"noempty" : true, // true: Prohibit use of empty blocks
"nonbsp" : true, // true: Prohibit "non-breaking whitespace" characters.
"nonew" : false, // true: Prohibit use of constructors for side-effects (without assignment)
"plusplus" : false, // true: Prohibit use of `++` & `--`
"quotmark" : false, // Quotation mark consistency:
// false : do nothing (default)
// true : ensure whatever is used is consistent
// "single" : require single quotes
// "double" : require double quotes
"undef" : true, // true: Require all non-global variables to be declared (prevents global leaks)
"unused" : true, // true: Require all defined variables be used
"strict" : true, // true: Requires all functions run in ES5 Strict Mode
"maxparams" : false, // {int} Max number of formal params allowed per function
"maxdepth" : false, // {int} Max depth of nested blocks (within functions)
"maxstatements" : false, // {int} Max number statements per function
"maxcomplexity" : false, // {int} Max cyclomatic complexity per function
"maxlen" : false, // {int} Max number of characters per line

// Relaxing
"asi" : false, // true: Tolerate Automatic Semicolon Insertion (no semicolons)
"boss" : false, // true: Tolerate assignments where comparisons would be expected
"debug" : false, // true: Allow debugger statements e.g. browser breakpoints.
"eqnull" : false, // true: Tolerate use of `== null`
"es5" : false, // true: Allow ES5 syntax (ex: getters and setters)
"esnext" : false, // true: Allow ES.next (ES6) syntax (ex: `const`)
"moz" : false, // true: Allow Mozilla specific syntax (extends and overrides esnext features)
// (ex: `for each`, multiple try/catch, function expression…)
"evil" : false, // true: Tolerate use of `eval` and `new Function()`
"expr" : false, // true: Tolerate `ExpressionStatement` as Programs
"funcscope" : false, // true: Tolerate defining variables inside control statements
"globalstrict" : false, // true: Allow global "use strict" (also enables 'strict')
"iterator" : false, // true: Tolerate using the `__iterator__` property
"lastsemic" : false, // true: Tolerate omitting a semicolon for the last statement of a 1-line block
"laxbreak" : false, // true: Tolerate possibly unsafe line breakings
"laxcomma" : false, // true: Tolerate comma-first style coding
"loopfunc" : false, // true: Tolerate functions being defined in loops
"multistr" : false, // true: Tolerate multi-line strings
"noyield" : false, // true: Tolerate generator functions with no yield statement in them.
"notypeof" : false, // true: Tolerate invalid typeof operator values
"proto" : false, // true: Tolerate using the `__proto__` property
"scripturl" : false, // true: Tolerate script-targeted URLs
"shadow" : false, // true: Allows re-define variables later in code e.g. `var x=1; x=2;`
"sub" : false, // true: Tolerate using `[]` notation when it can still be expressed in dot notation
"supernew" : false, // true: Tolerate `new function () { ... };` and `new Object;`
"validthis" : false, // true: Tolerate using this in a non-constructor function

// Environments
"browser" : true, // Web Browser (window, document, etc)
"browserify" : false, // Browserify (node.js code in the browser)
"couch" : false, // CouchDB
"devel" : true, // Development/debugging (alert, confirm, etc)
"dojo" : false, // Dojo Toolkit
"jasmine" : false, // Jasmine
"jquery" : true, // jQuery
"mocha" : true, // Mocha
"mootools" : false, // MooTools
"node" : false, // Node.js
"nonstandard" : false, // Widely adopted globals (escape, unescape, etc)
"prototypejs" : false, // Prototype and Scriptaculous
"qunit" : false, // QUnit
"rhino" : false, // Rhino
"shelljs" : false, // ShellJS
"worker" : false, // Web Workers
"wsh" : false, // Windows Scripting Host
"yui" : false, // Yahoo User Interface

// Custom Globals
"globals" : { } // additional predefined global variables
}
44 changes: 27 additions & 17 deletions build/ouibounce.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
}
}(this, function(require,exports,module) {

return function ouibounce(el, config) {
var config = config || {},
return function ouibounce(el, custom_config) {
"use strict";

var config = custom_config || {},
aggressive = config.aggressive || false,
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
Expand Down Expand Up @@ -40,18 +42,20 @@ return function ouibounce(el, config) {

setTimeout(attachOuiBounce, timer);
function attachOuiBounce() {
if (isDisabled()) { return; }

_html.addEventListener('mouseleave', handleMouseleave);
_html.addEventListener('mouseenter', handleMouseenter);
_html.addEventListener('keydown', handleKeydown);
}

function handleMouseleave(e) {
if (e.clientY > sensitivity || (checkCookieValue(cookieName, 'true') && !aggressive)) return;
if (e.clientY > sensitivity) { return; }

_delayTimer = setTimeout(_fireAndCallback, delay);
_delayTimer = setTimeout(fire, delay);
}

function handleMouseenter(e) {
function handleMouseenter() {
if (_delayTimer) {
clearTimeout(_delayTimer);
_delayTimer = null;
Expand All @@ -60,11 +64,11 @@ return function ouibounce(el, config) {

var disableKeydown = false;
function handleKeydown(e) {
if (disableKeydown || checkCookieValue(cookieName, 'true') && !aggressive) return;
else if(!e.metaKey || e.keyCode !== 76) return;
if (disableKeydown) { return; }
else if(!e.metaKey || e.keyCode !== 76) { return; }

disableKeydown = true;
_delayTimer = setTimeout(_fireAndCallback, delay);
_delayTimer = setTimeout(fire, delay);
}

function checkCookieValue(cookieName, value) {
Expand All @@ -83,20 +87,23 @@ return function ouibounce(el, config) {
return ret;
}

function _fireAndCallback() {
fire();
callback();
function isDisabled() {
return checkCookieValue(cookieName, 'true') && !aggressive;
}

// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
function fire() {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
if (el) el.style.display = 'block';
if (isDisabled()) { return; }

if (el) { el.style.display = 'block'; }

callback();
disable();
}

function disable(options) {
var options = options || {};
function disable(custom_options) {
var options = custom_options || {};

// you can pass a specific cookie expiration when using the OuiBounce API
// ex: _ouiBounce.disable({ cookieExpire: 5 });
Expand Down Expand Up @@ -130,9 +137,12 @@ return function ouibounce(el, config) {

return {
fire: fire,
disable: disable
disable: disable,
isDisabled: isDisabled
};
}

/*exported ouibounce */
;

}));
2 changes: 1 addition & 1 deletion build/ouibounce.min.js

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

4 changes: 2 additions & 2 deletions contributing.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ To get OuiBounce ready locally you'll need to:
1. Run `npm install`
1. Make sure `gulp` is installed globally
- You can do that by running `npm install -g gulp`
1. Open the index file in the `/test/` folder --
1. Open the index file in the `/test/` folder --
- Note: Cookies won't work if you simply open the file. You'll need to have a server ready to serve the page. The easiest way is to
- run: `python -m SimpleHTTPServer`
- [http://127.0.0.1:8000/test/index.html](http://127.0.0.1:8000/test/index.html)

### Dev Build
To minify OuiBounce run `gulp build` from the command line.
To minify OuiBounce run `gulp build` from the command line.
30 changes: 19 additions & 11 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ var gulp = require('gulp'),
minifyCSS = require('gulp-minify-css'),
umd_wrap = require('gulp-wrap-umd'),
stylus = require('gulp-stylus'),
rename = require('gulp-rename');
rename = require('gulp-rename'),
jshint = require('gulp-jshint'),
stylish = require('jshint-stylish');

gulp.task('build', function() {
// Because it's always best to have your code checked
// If this task fails, build will fail too
gulp.task('jshint', function() {

gulp.src('source/ouibounce.js')
.pipe(umd_wrap({ namespace: 'ouibounce' }))
.pipe(gulp.dest('build'));
gulp.src('source/ouibounce.js')
.pipe( jshint( '.jshintrc' ) )
.pipe( jshint.reporter( stylish ) )
.pipe( jshint.reporter( 'fail' ) );

});

gulp.task('build', ['jshint'], function() {

gulp.src('source/ouibounce.js')
.pipe(umd_wrap({ namespace: 'ouibounce' }))
.pipe(gulp.dest('build'))
.pipe(uglify())
.pipe(rename('ouibounce.min.js'))
.pipe(gulp.dest('build'));
Expand All @@ -22,18 +32,16 @@ gulp.task('build', function() {
.pipe(stylus())
.pipe(prefix())
.pipe(rename('ouibounce.css'))
.pipe(gulp.dest('test'));

gulp.src('test/ouibounce.styl')
.pipe(stylus())
.pipe(prefix())
.pipe(gulp.dest('test'))
.pipe(minifyCSS())
.pipe(rename('ouibounce.min.css'))
.pipe(gulp.dest('test'));
});



// Rerun the task when a file changes
gulp.task('watch', function () {
gulp.task('watch', function() {
gulp.watch('test/ouibounce.styl', ['build']);
gulp.watch('source/ouibounce.js', ['build']);
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
"mocha": "~1.17.1",
"should": "~3.1.2",
"zombie": "~2.0.0-alpha29",
"gulp-stylus": "0.0.13"
"gulp-stylus": "0.0.13",
"jshint-stylish": "~2.0.0",
"gulp-jshint": "~1.11.0"
}
}
44 changes: 27 additions & 17 deletions source/ouibounce.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function ouibounce(el, config) {
var config = config || {},
function ouibounce(el, custom_config) {
"use strict";

var config = custom_config || {},
aggressive = config.aggressive || false,
sensitivity = setDefault(config.sensitivity, 20),
timer = setDefault(config.timer, 1000),
Expand Down Expand Up @@ -28,18 +30,20 @@ function ouibounce(el, config) {

setTimeout(attachOuiBounce, timer);
function attachOuiBounce() {
if (isDisabled()) { return; }

_html.addEventListener('mouseleave', handleMouseleave);
_html.addEventListener('mouseenter', handleMouseenter);
_html.addEventListener('keydown', handleKeydown);
}

function handleMouseleave(e) {
if (e.clientY > sensitivity || (checkCookieValue(cookieName, 'true') && !aggressive)) return;
if (e.clientY > sensitivity) { return; }

_delayTimer = setTimeout(_fireAndCallback, delay);
_delayTimer = setTimeout(fire, delay);
}

function handleMouseenter(e) {
function handleMouseenter() {
if (_delayTimer) {
clearTimeout(_delayTimer);
_delayTimer = null;
Expand All @@ -48,11 +52,11 @@ function ouibounce(el, config) {

var disableKeydown = false;
function handleKeydown(e) {
if (disableKeydown || checkCookieValue(cookieName, 'true') && !aggressive) return;
else if(!e.metaKey || e.keyCode !== 76) return;
if (disableKeydown) { return; }
else if(!e.metaKey || e.keyCode !== 76) { return; }

disableKeydown = true;
_delayTimer = setTimeout(_fireAndCallback, delay);
_delayTimer = setTimeout(fire, delay);
}

function checkCookieValue(cookieName, value) {
Expand All @@ -71,20 +75,23 @@ function ouibounce(el, config) {
return ret;
}

function _fireAndCallback() {
fire();
callback();
function isDisabled() {
return checkCookieValue(cookieName, 'true') && !aggressive;
}

// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
function fire() {
// You can use ouibounce without passing an element
// https://github.com/carlsednaoui/ouibounce/issues/30
if (el) el.style.display = 'block';
if (isDisabled()) { return; }

if (el) { el.style.display = 'block'; }

callback();
disable();
}

function disable(options) {
var options = options || {};
function disable(custom_options) {
var options = custom_options || {};

// you can pass a specific cookie expiration when using the OuiBounce API
// ex: _ouiBounce.disable({ cookieExpire: 5 });
Expand Down Expand Up @@ -118,6 +125,9 @@ function ouibounce(el, config) {

return {
fire: fire,
disable: disable
disable: disable,
isDisabled: isDisabled
};
}

/*exported ouibounce */
19 changes: 19 additions & 0 deletions test/fixtures/manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
</head>
<script src="../../build/ouibounce.js"></script>
</head>

<body>
<div id="ouibounce-modal"></div>

<script>
window.ouibounceFired = false;

window.modal = ouibounce(document.getElementById('ouibounce-modal'), {
aggressive: true,
callback: function() { window.ouibounceFired = true; }
});
</script>
</body>
</html>
Loading