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

Add a destroy method #11

Merged
merged 3 commits into from
Sep 26, 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
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Your manager has the following signature :
on: Function,
off: Function,
get: Function, // get a specific joystick
destroy: Function,
options: {
zone: Element,
multitouch: Boolean,
Expand All @@ -143,15 +144,17 @@ manager.on('event#1 event#2', function (evt, data) {
Note that you can listen to multiple events at once by separating
them either with a space or a comma (or both, I don't care).

#### `manager.off(type [, handler])`
#### `manager.off([type, handler])`

To remove an event handler :

```javascript
manager.off('event', handler);
```

If you don't specify the handler, all handlers for that type will be removed.
If you call off without arguments, all handlers will be removed.

If you don't specify the handler but just a type, all handlers for that type will be removed.

#### `manager.get(identifier)`

Expand All @@ -162,6 +165,14 @@ An helper to get an instance via its identifier.
manager.get(0);
```

#### `manager.destroy()`

Gently remove all nipples from the DOM and unbind all events.

```javascript
manager.destroy();
```

### nipple instance (joystick)

Each joystick has the following signature :
Expand All @@ -175,6 +186,7 @@ Each joystick has the following signature :
hide: Function, // fade-out
add: Function, // inject into dom
remove: Function, // remove from dom
destroy: Function,
identifier: Number,
trigger: Function,
position: { // position of the center
Expand Down Expand Up @@ -238,6 +250,10 @@ Add the joystick's element to the dom.

Remove the joystick's element from the dom.

### `joystick.destroy()`

Gently remove this nipple from the DOM and unbind all related events.

### `joystick.identifier`

Returns the unique identifier of the joystick.
Expand Down Expand Up @@ -412,6 +428,12 @@ Is triggered at the end of the fade-out animation.

Will pass the instance alongside the event.

#### `destroyed`

Is trigger at the end of destroy.

Will pass the instance alongside the event.

----
## Contributing
Your help is more than welcome, I would be very honored to have you on my side.
Expand Down
21 changes: 21 additions & 0 deletions src/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ var Manager = function (options) {
self.config(options);
self.nipples = [];
self.bindEvt(self.options.zone, 'start');
self.on('destroyed', this.ondestroyed);

self.nipples.on = self.on.bind(self);
self.nipples.off = self.off.bind(self);
self.nipples.options = self.options;
self.nipples.destroy = self.destroy.bind(self);
self.nipples.get = function (id) {
for (var i = 0, max = self.nipples.length; i < max; i += 1) {
if (self.nipples[i].identifier === id) {
Expand Down Expand Up @@ -270,3 +272,22 @@ Manager.prototype.processOnEnd = function (evt) {
var index = self.nipples.indexOf(nipple);
self.nipples.splice(index, 1);
};

// Remove destroyed nipple from the list
Manager.prototype.ondestroyed = function(evt, nipple) {
nipple = this.nipples.get(nipple.identifier);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue here, and might be the case for all events: the nipple instance passing along is not the same instance in nipples manager list. Got to get the manager side instance by identifier. This comes from Nipple#constructor returning a custom object.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very weird.
Can you add a comment to explain it above the line nipple = this.nipples.get[...] to act as a reminder for me, please.
Can you also log an issue about this with the info you've collected please?

I'll look at this later on. This is not a blocker for your PR, if your patch is working correctly.

After your comment, I'll be able to merge it.
👍 Good job and thank you very much for your help !

if (nipple) {
this.nipples.splice(this.nipples.indexOf(nipple), 1);
}
};

// Cleanly destroy the manager
Manager.prototype.destroy = function () {
this.unbindEvt(this.options.zone, 'start');
this.unbindEvt(document, 'move');
this.unbindEvt(document, 'end');
this.nipples.forEach(function(nipple) {
nipple.destroy();
});
this.off();
};
11 changes: 11 additions & 0 deletions src/nipple.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var Nipple = function (manager, options) {
hide: this.hide.bind(this),
add: this.addToDom.bind(this),
remove: this.removeFromDom.bind(this),
destroy: this.destroy.bind(this),
computeDirection: this.computeDirection.bind(this),
trigger: this.trigger.bind(this),
position: this.position,
Expand Down Expand Up @@ -136,6 +137,16 @@ Nipple.prototype.removeFromDom = function () {
return this;
};

// Entirely destroy this nipple
Nipple.prototype.destroy = function () {
clearTimeout(this.removeTimeout);
clearTimeout(this.showTimeout);
this.off();
this.removeFromDom();
this.trigger('destroyed', this);
this.manager.trigger('destroyed ' + this.identifier + ':destroyed', this);
};

// Fade in the Nipple instance.
Nipple.prototype.show = function (cb) {
var self = this;
Expand Down
6 changes: 4 additions & 2 deletions src/super.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ Super.prototype.on = function (arg, cb) {

Super.prototype.off = function (type, cb) {
var self = this;
if (cb === undefined) {
self.handlers[type] = [];
if (type === undefined) {
self.handlers = {};
} else if (cb === undefined) {
self.handlers[type] = null;
} else if (self.handlers[type] && self.handlers[type].indexOf(cb) >= 0) {
self.handlers[type].splice(self.handlers[type].indexOf(cb), 1);
}
Expand Down