Skip to content

Commit

Permalink
Fixed #236: Implemented kill renderers method
Browse files Browse the repository at this point in the history
Also implemented the `kill` method for the existing captors.
  • Loading branch information
jacomyal committed Apr 2, 2014
1 parent 65f23cc commit f2262f5
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 14 deletions.
17 changes: 17 additions & 0 deletions src/captors/sigma.captors.mouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@



/**
* This method unbinds every handlers that makes the captor work.
*/
this.kill = function() {
sigma.utils.unbindDoubleClick(_target, 'click');
_target.removeEventListener('DOMMouseScroll', _wheelHandler);
_target.removeEventListener('mousewheel', _wheelHandler);
_target.removeEventListener('mousemove', _moveHandler);
_target.removeEventListener('mousedown', _downHandler);
_target.removeEventListener('click', _clickHandler);
_target.removeEventListener('mouseout', _outHandler);
document.removeEventListener('mouseup', _upHandler);
};




// MOUSE EVENTS:
// *************

Expand Down
26 changes: 21 additions & 5 deletions src/captors/sigma.captors.touch.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@
sigma.classes.dispatcher.extend(this);

sigma.utils.doubleClick(_target, 'touchstart', _doubleTapHandler);
_target.addEventListener('touchstart', _handleStart);
_target.addEventListener('touchend', _handleLeave);
_target.addEventListener('touchcancel', _handleLeave);
_target.addEventListener('touchleave', _handleLeave);
_target.addEventListener('touchmove', _handleMove);
_target.addEventListener('touchstart', _handleStart, false);
_target.addEventListener('touchend', _handleLeave, false);
_target.addEventListener('touchcancel', _handleLeave, false);
_target.addEventListener('touchleave', _handleLeave, false);
_target.addEventListener('touchmove', _handleMove, false);

function position(e) {
var offset = sigma.utils.getOffset(_target);
Expand All @@ -75,6 +75,22 @@




/**
* This method unbinds every handlers that makes the captor work.
*/
this.kill = function() {
sigma.utils.unbindDoubleClick(_target, 'touchstart');
_target.addEventListener('touchstart', _handleStart);
_target.addEventListener('touchend', _handleLeave);
_target.addEventListener('touchcancel', _handleLeave);
_target.addEventListener('touchleave', _handleLeave);
_target.addEventListener('touchmove', _handleMove);
};




// TOUCH EVENTS:
// *************
/**
Expand Down
33 changes: 30 additions & 3 deletions src/renderers/sigma.renderers.canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@
}

// Bind resize:
window.addEventListener('resize', function() {
self.resize();
});
window.addEventListener(
'resize',
this.boundResize = this.resize.bind(this),
false
);

// Deal with sigma events:
sigma.misc.bindEvents.call(this, this.options.prefix);
Expand Down Expand Up @@ -362,6 +364,31 @@
return this;
};

/**
* This method kills contexts and other attributes.
*/
sigma.renderers.canvas.prototype.kill = function() {
var k,
captor;

// Unbind resize:
window.removeEventListener('resize', this.boundResize);

// Kill captors:
while ((captor = this.captors.pop()))
captor.kill();
delete this.captors;

// Kill contexts:
for (k in this.domElements) {
this.domElements[k].parentNode.removeChild(this.domElements[k]);
delete this.domElements[k];
delete this.contexts[k];
}
delete this.domElements;
delete this.contexts;
};




Expand Down
33 changes: 30 additions & 3 deletions src/renderers/sigma.renderers.webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@
}

// Bind resize:
window.addEventListener('resize', function() {
_self.resize();
});
window.addEventListener(
'resize',
this.boundResize = this.resize.bind(this),
false
);

// Deal with sigma events:
sigma.misc.bindEvents.call(this, this.camera.prefix);
Expand Down Expand Up @@ -566,6 +568,31 @@
return this;
};

/**
* This method kills contexts and other attributes.
*/
sigma.renderers.webgl.prototype.kill = function() {
var k,
captor;

// Unbind resize:
window.removeEventListener('resize', this.boundResize);

// Kill captors:
while ((captor = this.captors.pop()))
captor.kill();
delete this.captors;

// Kill contexts:
for (k in this.domElements) {
this.domElements[k].parentNode.removeChild(this.domElements[k]);
delete this.domElements[k];
delete this.contexts[k];
}
delete this.domElements;
delete this.contexts;
};




Expand Down
30 changes: 27 additions & 3 deletions src/utils/sigma.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,14 @@
*/
sigma.utils.doubleClick = function(target, type, callback) {
var clicks = 0,
self = this;
self = this,
handlers;

target.addEventListener(type, function(e) {
target._doubleClickHandler = target._doubleClickHandler || {};
target._doubleClickHandler[type] = target._doubleClickHandler[type] || [];
handlers = target._doubleClickHandler[type];

handlers.push(function(e) {
clicks++;

if (clicks === 2) {
Expand All @@ -254,7 +259,26 @@
clicks = 0;
}, sigma.settings.doubleClickTimeout);
}
}, false);
});

target.addEventListener(type, handlers[handlers.length - 1], false);
};

/**
* Unbind simulated "double click" events.
*
* @param {HTMLElement} target The event target.
* @param {string} type The event type.
*/
sigma.utils.unbindDoubleClick = function(target, type) {
var handler,
handlers = (target._doubleClickHandler || {})[type] || [];

while ((handler = handlers.pop())) {
target.removeEventListener(type, handler);
}

delete (target._doubleClickHandler || {})[type];
};


Expand Down

0 comments on commit f2262f5

Please sign in to comment.