<commit_before># Ember User Activity

[![Build Status](https://travis-ci.org/elwayman02/ember-user-activity.svg?branch=master)](https://travis-ci.org/elwayman02/ember-user-activity)
[![Ember Observer Score](https://emberobserver.com/badges/ember-user-activity.svg)](https://emberobserver.com/addons/ember-user-activity)
[![Code Climate](https://codeclimate.com/github/elwayman02/ember-user-activity/badges/gpa.svg)](https://codeclimate.com/github/elwayman02/ember-user-activity)

This addon provides services for detecting user activity & idling across the entire application.

Check out the [Demo](http://github.jhawk.co/ember-user-activity/)!

## Installation

`ember install ember-user-activity`

## Usage

### User Activity Service

This service fires events from global window listeners. 
These listeners trigger on capture, meaning they are not affected by event cancellation. 

Three `window` events are enabled by default:

* `keydown` - Fires when a key is pressed
* `mousedown` - Fires when a mouse is clicked
* `scroll` - Fires when the user scrolls
* ~~`mousemove` - Fires when the user moves the mouse~~ [Removed as of v0.2.0](https://github.com/elwayman02/ember-user-activity/issues/16)

A custom event, `userActive` is fired for ALL enabled events.

To catch these events, simply inject the service and subscribe to the events you care about:

```javascript
// any file where services can be injected
userActivity: Ember.inject.service(),
setupListeners() {
  this.get('userActivity').on('userActive', this, this.activeHandler);
},
activeHandler(event) {
  // do stuff
}
```

Each event handler will receive the standard DOM `event` object 
(ex: [mousemove](https://developer.mozilla.org/en-US/docs/Web/Events/mousemove)).

If you would like to listen to a different set of events, extend the service in your app:

```javascript
// app/services/user-activity.js
import UserActivityService from 'ember-user-activity/services/user-activity';

export default UserActivityService.extend({
  defaultEvents: ['keypress', 'mouseenter', 'mousemove']
});
```

Additionally, you can enable/disable events after the service has been initialized.

```javascript
this.get('userActivity').enableEvent('keyup');
this.get('userActivity').disableEvent('mousedown');
```

Event names must be from the [DOM Event](https://developer.mozilla.org/en-US/docs/Web/Events) list. 
Custom events are not currently supported. If you enable an event name 
that was not set up by default, a new listener will be created automatically.

You can find out if an event is currently enabled:

```javascript
this.get('userActivity').isEnabled('foo'); // false
this.get('userActivity').isEnabled('keydown'); // true
```

Each individual event is throttled by 100ms for performance reasons, 
to avoid clogging apps with a firehose of activity events. The length of 
the throttling can be configured by setting `EVENT_THROTTLE` on the activity service.

```javascript
// app/services/user-activity.js
import UserActivityService from 'ember-user-activity/services/user-activity';

export default UserActivityService.extend({
  EVENT_THROTTLE: 200 // 200 ms
});
```

Setting `EVENT_THROTTLE` to 0 will enable the full firehose of events. 
This may cause performance issues in your application if non-trivial 
amounts of code are being executed for each event being fired.

### User Idle Service

This service tracks user activity to decide when a user has gone idle by 
not interacting with the page for a set amount of time.

```javascript
userIdle: Ember.inject.service(),
isIdle: Ember.computed.readOnly('userIdle.isIdle')
```

The default timeout is set for 10 minutes but can be overridden by extending the service:
 
```javascript
// app/services/user-idle.js
import UserIdleService from 'ember-user-activity/services/user-idle';

export default UserIdleService.extend({
  IDLE_TIMEOUT: 300000 // 5 minutes
});
```

By default, the idle service listens to the `userActive` event, but it can be 
configured to listen to a custom set of events from the `user-activity` service:

```javascript
// app/services/user-idle.js
import UserIdleService from 'ember-user-activity/services/user-idle';

export default UserIdleService.extend({
  activeEvents: ['mousedown', 'keydown']
});
```

Note that the `userActive` event is a superset of all events fired from `user-activity`, 
so in most cases you won't need to change this.

### Scroll Activity Service

This service tracks scrolling within the application by periodically checking 
(via [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)) 
for changes in scroll position for the various scrollable elements in the page. By default, it only 
checks `document`, but the [Scroll Activity Mixin](#scroll-activity-mixin) provides an easy 
way to register your components as well. Any elements can be subscribed to this service:

```javascript
this.get('scrollActivity').subscribe(this, this.$());
```

`subscribe` requires at least two parameters:

* `target` - Usually `this`, target just needs to be a unique identifier/object 
that can be used to unsubscribe from the service
* `element` - The scrollable element (can be a DOM or jQuery element)

A third optional parameter can be included:

* `callback` - A callback to execute when scrolling has been detected in the element

Conversely, elements can also be unsubscribed:

```javascript
this.get('scrollActivity').unsubscribe(this);
```

`unsubscribe` only requires the `target` parameter that was initially used to `subscribe`.

### Scroll Activity Mixin

This mixin automatically subscribes and unsubscribes a scrollable component to the `scrollActivity` service.<commit_msg>Add note that scroll-activity service usage is implicit<commit_after># Ember User Activity

[![Build Status](https://travis-ci.org/elwayman02/ember-user-activity.svg?branch=master)](https://travis-ci.org/elwayman02/ember-user-activity)
[![Ember Observer Score](https://emberobserver.com/badges/ember-user-activity.svg)](https://emberobserver.com/addons/ember-user-activity)
[![Code Climate](https://codeclimate.com/github/elwayman02/ember-user-activity/badges/gpa.svg)](https://codeclimate.com/github/elwayman02/ember-user-activity)

This addon provides services for detecting user activity & idling across the entire application.

Check out the [Demo](http://github.jhawk.co/ember-user-activity/)!

## Installation

`ember install ember-user-activity`

## Usage

### User Activity Service

This service fires events from global window listeners. 
These listeners trigger on capture, meaning they are not affected by event cancellation. 

Three `window` events are enabled by default:

* `keydown` - Fires when a key is pressed
* `mousedown` - Fires when a mouse is clicked
* `scroll` - Fires when the user scrolls
* ~~`mousemove` - Fires when the user moves the mouse~~ [Removed as of v0.2.0](https://github.com/elwayman02/ember-user-activity/issues/16)

A custom event, `userActive` is fired for ALL enabled events.

To catch these events, simply inject the service and subscribe to the events you care about:

```javascript
// any file where services can be injected
userActivity: Ember.inject.service(),
setupListeners() {
  this.get('userActivity').on('userActive', this, this.activeHandler);
},
activeHandler(event) {
  // do stuff
}
```

Each event handler will receive the standard DOM `event` object 
(ex: [mousemove](https://developer.mozilla.org/en-US/docs/Web/Events/mousemove)).

If you would like to listen to a different set of events, extend the service in your app:

```javascript
// app/services/user-activity.js
import UserActivityService from 'ember-user-activity/services/user-activity';

export default UserActivityService.extend({
  defaultEvents: ['keypress', 'mouseenter', 'mousemove']
});
```

Additionally, you can enable/disable events after the service has been initialized.

```javascript
this.get('userActivity').enableEvent('keyup');
this.get('userActivity').disableEvent('mousedown');
```

Event names must be from the [DOM Event](https://developer.mozilla.org/en-US/docs/Web/Events) list. 
Custom events are not currently supported. If you enable an event name 
that was not set up by default, a new listener will be created automatically.

You can find out if an event is currently enabled:

```javascript
this.get('userActivity').isEnabled('foo'); // false
this.get('userActivity').isEnabled('keydown'); // true
```

Each individual event is throttled by 100ms for performance reasons, 
to avoid clogging apps with a firehose of activity events. The length of 
the throttling can be configured by setting `EVENT_THROTTLE` on the activity service.

```javascript
// app/services/user-activity.js
import UserActivityService from 'ember-user-activity/services/user-activity';

export default UserActivityService.extend({
  EVENT_THROTTLE: 200 // 200 ms
});
```

Setting `EVENT_THROTTLE` to 0 will enable the full firehose of events. 
This may cause performance issues in your application if non-trivial 
amounts of code are being executed for each event being fired.

### User Idle Service

This service tracks user activity to decide when a user has gone idle by 
not interacting with the page for a set amount of time.

```javascript
userIdle: Ember.inject.service(),
isIdle: Ember.computed.readOnly('userIdle.isIdle')
```

The default timeout is set for 10 minutes but can be overridden by extending the service:
 
```javascript
// app/services/user-idle.js
import UserIdleService from 'ember-user-activity/services/user-idle';

export default UserIdleService.extend({
  IDLE_TIMEOUT: 300000 // 5 minutes
});
```

By default, the idle service listens to the `userActive` event, but it can be 
configured to listen to a custom set of events from the `user-activity` service:

```javascript
// app/services/user-idle.js
import UserIdleService from 'ember-user-activity/services/user-idle';

export default UserIdleService.extend({
  activeEvents: ['mousedown', 'keydown']
});
```

Note that the `userActive` event is a superset of all events fired from `user-activity`, 
so in most cases you won't need to change this.

### Scroll Activity Service

This service tracks scrolling within the application by periodically checking 
(via [requestAnimationFrame](https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame)) 
for changes in scroll position for the various scrollable elements in the page. By default, it only 
checks `document`, but the [Scroll Activity Mixin](#scroll-activity-mixin) provides an easy 
way to register your components as well. The [User Activity Service](#user-activity-service) subscribes to these scrolling events by default, so you do not need to do anything to use this service for global scroll events if you are already injecting the user-activity service.

Any elements can be subscribed to this service:

```javascript
this.get('scrollActivity').subscribe(this, this.$());
```

`subscribe` requires at least two parameters:

* `target` - Usually `this`, target just needs to be a unique identifier/object 
that can be used to unsubscribe from the service
* `element` - The scrollable element (can be a DOM or jQuery element)

A third optional parameter can be included:

* `callback` - A callback to execute when scrolling has been detected in the element

Conversely, elements can also be unsubscribed:

```javascript
this.get('scrollActivity').unsubscribe(this);
```

`unsubscribe` only requires the `target` parameter that was initially used to `subscribe`.

### Scroll Activity Mixin

This mixin automatically subscribes and unsubscribes a scrollable component to the `scrollActivity` service.
{'fraction_non_alphanumeric': 0.10289966641005902, 'fraction_numerical': 0.005816439996578565, 'mean_word_length': 5.011311053984576, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 20, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4165587', 'n_tokens_mistral': 3347, 'n_tokens_neox': 3196, 'n_words': 1249}