Skip to content
This repository has been archived by the owner on Sep 19, 2024. It is now read-only.

Commit

Permalink
Merge branch 'feature/initial' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Grabski committed Nov 28, 2013
2 parents bfc2a06 + 0bb8c3d commit 641c225
Show file tree
Hide file tree
Showing 10 changed files with 591 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Auto detect text files and perform LF normalization
* text=auto

# Custom for Visual Studio
*.cs diff=csharp
*.sln merge=union
*.csproj merge=union
*.vbproj merge=union
*.fsproj merge=union
*.dbproj merge=union

# Standard to msysgit
*.doc diff=astextplain
*.DOC diff=astextplain
*.docx diff=astextplain
*.DOCX diff=astextplain
*.dot diff=astextplain
*.DOT diff=astextplain
*.pdf diff=astextplain
*.PDF diff=astextplain
*.rtf diff=astextplain
*.RTF diff=astextplain
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ results
npm-debug.log
node_modules

bower_components

.DS_Store
71 changes: 70 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,73 @@
ng-idle
=======

Services and directives for responding to idle users
## About
Your user may be sitting at the bottom of the ocean like an addled schoolboy (his/her orders are 7 bloody hours old!). You may wish to detect these guys and respond, for example, to log them out so their sensitive data is protected, or taunt them, or whatever. I don't care.

This module will include a variety of services and directives to help you in this task.

_**Warning:** This is still in active development and subject to change without noticed. Consider that carefully before including in your production projects. I expect the beta phase to last 1 to 20 years, and that should start in a decade or so._

========

Authored by Mike Grabski
Licensed under [MIT](http://www.opensource.org/licenses/mit-license.php)

## Getting Started

First, you'll need AngularJS 1.2.1 or later (earlier possible, but not tested yet). You can then inject the `$idle` service into your app `run` or in a controller and call `$idle.watch()` when you want to start watching for idleness. You can stop watching anytime by calling `$idle.unwatch()`. `$idle` communicates through events broadcasted on `$rootScope`.

// include the `ngIdle` module
var app = angular.module('demo', ['ngIdle']);

app
.controller('EventsCtrl', function($scope, $idle) {
$scope.events = [];

$scope.$on('$idleStart', function() {
// the user appears to have gone idle
});

$scope.$on('$idleWarn', function(e, countdown) {
// follows after the $idleStart event, but includes a countdown until the user is considered timed out
// the countdown arg is the number of seconds remaining until then.
// you can change the title or display a warning dialog from here.
// you can let them resume their session by calling $idle.watch()
});

$scope.$on('$idleTimeout', function() {
// the user has timed out (meaning idleDuration + warningDuration has passed without any activity)
// this is where you'd log them
})

$scope.$on('$idleEnd', function() {
// the user has come back from AFK and is doing stuff. if you are warning them, you can use this to hide the dialog
});


})
.config(function($idleProvider) {
// configure $idle settings
$idleProvider.idleDuration(5);
$idleProvider.warningDuration(5);
})
.run(function($idle){
// start watching when the app runs
$idle.watch();
});

You can stop watching for idleness at any time by calling `$idle.unwatch()`.

## Roadmap

* **0.1**: The basic `$idle` service and `$idleProvider`.

TBD

## Contributing

TBD

## Developing

TBD
27 changes: 27 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "ng-idle",
"version": "0.1.0",
"homepage": "https://github.com/HackedByChinese/ng-idle",
"description": "Directives and services for handling idle users in AngularJS",
"main": "src/angular-idle.js",
"keywords": [
"angularjs",
"idle",
"ng-idle"
],
"license": "MIT",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"angular": "~1.2.1"
},
"devDependencies": {
"angular-mocks": "~1.2.1",
"jquery": "~2.0.3"
}
}
22 changes: 22 additions & 0 deletions gruntfile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module.exports = function(grunt) {
// load all grunt tasks
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks);

grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
karma: {
options: {
configFile: 'karma.conf.js'
},
unit: {
singleRun: true
},
server: {
autoWatch: true
}
}
});

grunt.registerTask('test', ['karma:unit']);
grunt.registerTask('test-server', ['karma:server']);
};
71 changes: 71 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Karma configuration
// Generated on Tue Nov 19 2013 23:15:01 GMT-0500 (Eastern Standard Time)

module.exports = function(config) {
config.set({

// base path, that will be used to resolve files and exclude
basePath: '',


// frameworks to use
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
'bower_components/jquery/jquery.js',
'bower_components/angular/angular.js',
'bower_components/angular-mocks/angular-mocks.js',
'src/*.js'
],


// list of files to exclude
exclude: [

],


// test results reporter to use
// possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
reporters: ['progress'],


// web server port
port: 9876,


// enable / disable colors in the output (reporters and logs)
colors: true,


// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// Start these browsers, currently available:
// - Chrome
// - ChromeCanary
// - Firefox
// - Opera (has to be installed with `npm install karma-opera-launcher`)
// - Safari (only Mac; has to be installed with `npm install karma-safari-launcher`)
// - PhantomJS
// - IE (only Windows; has to be installed with `npm install karma-ie-launcher`)
browsers: ['Chrome'],


// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,


// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun: false
});
};
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "ng-idle",
"version": "0.1.0",
"description": "Directives and services for responding to idle users in AngularJS",
"scripts": {
"test": "grunt test"
},
"repository": {
"type": "git",
"url": "https://github.com/HackedByChinese/ng-idle.git"
},
"author": "Mike Grabski <me@mikegrabski.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/HackedByChinese/ng-idle/issues"
},
"devDependencies": {
"grunt": "~0.4.1",
"grunt-contrib-jshint": "~0.6.3",
"grunt-contrib-nodeunit": "~0.2.0",
"grunt-contrib-uglify": "~0.2.2",
"grunt-karma": "~0.7.0",
"matchdep": "~0.1.2",
"karma": "~0.10",
"karma-jasmine": "*",
"karma-chrome-launcher": "*",
"karma-firefox-launcher": "*",
"karma-ng-html2js-preprocessor": "*"
}
}
122 changes: 122 additions & 0 deletions src/angular-idle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
* Respond to idle users in AngularJS
* @version v0.1.0
* @link http://hackedbychinese.github.io/ng-idle
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
(function (window, angular, undefined) {
'use strict';

// register modules
var ngIdleSvc = angular.module('ngIdle.services', []);
angular.module('ngIdle', ['ngIdle.services']);

// $idle service and provider
function $IdleProvider() {

var options = {
idleDuration: 20 * 60, // in seconds (default is 20min)
warningDuration: 30, // in seconds (default is 30sec)
autoResume: true, // lets events automatically resume (unsets idle state/resets warning)
events: 'mousemove keydown DOMMouseScroll mousewheel mousedown'
};

this.activeOn = activeOn;
function activeOn (events) {
options.events = events;
};

this.idleDuration = idleDuration;
function idleDuration(seconds) {
if (seconds < 0) throw new Error("idleDuration must be a value in seconds, greatner than 0.");

options.idleDuration = seconds;
}

this.warningDuration = warningDuration;
function warningDuration(seconds) {
if (seconds < 0) throw new Error("warning must be a value in seconds, greatner than 0.");

options.warningDuration = seconds;
}

this.autoResume = autoResume;
function autoResume(value) {
options.autoResume = value === true;
}

this.$get = $get;
$get.$inject = ['$timeout', '$log', '$rootScope', '$document'];

function $get($timeout, $log, $rootScope, $document) {
var state = {idle: null, warning: null, idling: false, running: false, countdown: null};

function toggleState() {
state.idling = !state.idling;
var name = state.idling ? 'Start' : 'End';

$rootScope.$broadcast('$idle' + name);

if (state.idling) {
state.countdown = options.warningDuration;
countdown();
}
}

function countdown() {
if (state.countdown <= 0) {
$rootScope.$broadcast('$idleTimeout');
} else {
$rootScope.$broadcast('$idleWarn', state.countdown);

state.warning = $timeout(countdown, 1000);
}

state.countdown--;
}

var svc = {
_options: function() {
return options;
},
_t: function() {
return state.t;
},
running: function() {
return state.running;
},
idling: function() {
return state.idling;
},
watch: function() {
$timeout.cancel(state.idle);
$timeout.cancel(state.warning);

state.running = true;

if (state.idling) toggleState();

state.idle = $timeout(toggleState, options.idleDuration * 1000);
},
unwatch: function() {
$timeout.cancel(state.idle);
$timeout.cancel(state.warning);

state.idling = false;
state.running = false;
}
};

var interrupt = function () {
if (state.running && options.autoResume) svc.watch();
};

$document.find('body').on(options.events, interrupt);

return svc;
};
}

ngIdleSvc.provider('$idle', $IdleProvider);

})(window, window.angular);
Loading

0 comments on commit 641c225

Please sign in to comment.