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

bugfix(issue #14): add in destroy method #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,24 @@ disabledSlider.disable(true);

> #### Slider.prototype.disable
>
> Makes the slider handle unmovable
> Disabled makes the slider handle unmovable
> Pass true to disable the slider
> Pass false to reenable the slider
> Defaults to true

### Destory the slider

```javascript
var memoryLeak = new Slider(document.getElementById('leakingSlider'));
memoryLeak.destroy(true);
Copy link

Choose a reason for hiding this comment

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

Non-blocking question, @chrisrng , can we expand the .destroy accepted parameter to accept as object instead of a boolean flag? Perhaps define some good names as accepted object keys to be expressive by native object key names, and the values of the object keys would be your choice of appropriate data type, i.e.:

memoryLeak.destroy({
  isCompleteDestroy: true
});

or

memoryLeak.destroy({
  isDestroyRefrencesOnly: true
});

Think if one sets up the accepted parameter with object with meaningful named object keys to control the .destroy API, it would pave the scalability of the .destroy API, if the .destroy API ever expands.

```

> #### Slider.prototype.destroy
>
> Cleaning up any stored DOM references and unsubscribing any registered callbacks
> Pass true to remove the Slider from DOM and delete references
> Pass false to remove references only keeping the slider in the DOM without any listeners
> Defaults to false

## Development

Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "omni-slider",
"main": "omni-slider.js",
"version": "1.2.0",
"version": "1.2.2",
"authors": [
"Chris Ng <[email protected]>"
],
Expand Down
34 changes: 32 additions & 2 deletions demo/omni-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Slider = function () {
_classCallCheck(this, Slider);

// Validation of element, the only required argument
if (!elementContainer || elementContainer.nodeName !== 'DIV' && elementContainer.tagName !== 'DIV') return;
if (!elementContainer.nodeName && !elementContainer.tagName) return;

// Contains the options for this slider
this.options = {
Expand Down Expand Up @@ -479,7 +479,9 @@ var Slider = function () {

}, {
key: 'disable',
value: function disable(boolean) {
value: function disable() {
var boolean = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];

this.isDisabled = boolean;
if (this.isDisabled) {
this.UI.slider.classList.add('slider-disabled');
Expand Down Expand Up @@ -539,6 +541,34 @@ var Slider = function () {
event(data);
});
}

/* Destory
* Remove all traces of the slider in memory and dereference listeners
*/

}, {
key: 'destroy',
value: function destroy() {
var deleteElem = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];

// Deference all listeners
document.removeEventListener('mousemove', this.movingHandler, true);
document.removeEventListener('mouseup', this.stopHandler, true);
document.removeEventListener('touchmove', this.movingHandler, true);
document.removeEventListener('touchend', this.stopHandler, true);
this.UI.handleLeft.onmousedown = null;
this.UI.handleLeft.ontouchstart = null;
this.UI.handleRight.onmousedown = null;
this.UI.handleRight.ontouchstart = null;

// DOM element itself
if (!!deleteElem) {
this.UI.slider.parentNode.removeChild(this.UI.slider);
}

// Reference to DOM
this.UI = null;
}
}, {
key: 'defaultOptions',
get: function get() {
Expand Down
34 changes: 32 additions & 2 deletions omni-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var Slider = function () {
_classCallCheck(this, Slider);

// Validation of element, the only required argument
if (!elementContainer || elementContainer.nodeName !== 'DIV' && elementContainer.tagName !== 'DIV') return;
if (!elementContainer.nodeName && !elementContainer.tagName) return;

// Contains the options for this slider
this.options = {
Expand Down Expand Up @@ -479,7 +479,9 @@ var Slider = function () {

}, {
key: 'disable',
value: function disable(boolean) {
value: function disable() {
var boolean = arguments.length <= 0 || arguments[0] === undefined ? true : arguments[0];

this.isDisabled = boolean;
if (this.isDisabled) {
this.UI.slider.classList.add('slider-disabled');
Expand Down Expand Up @@ -539,6 +541,34 @@ var Slider = function () {
event(data);
});
}

/* Destory
* Remove all traces of the slider in memory and dereference listeners
*/

}, {
key: 'destroy',
value: function destroy() {
var deleteElem = arguments.length <= 0 || arguments[0] === undefined ? false : arguments[0];

// Deference all listeners
document.removeEventListener('mousemove', this.movingHandler, true);
document.removeEventListener('mouseup', this.stopHandler, true);
document.removeEventListener('touchmove', this.movingHandler, true);
document.removeEventListener('touchend', this.stopHandler, true);
this.UI.handleLeft.onmousedown = null;
this.UI.handleLeft.ontouchstart = null;
this.UI.handleRight.onmousedown = null;
this.UI.handleRight.ontouchstart = null;

// DOM element itself
if (!!deleteElem) {
this.UI.slider.parentNode.removeChild(this.UI.slider);
}

// Reference to DOM
this.UI = null;
}
}, {
key: 'defaultOptions',
get: function get() {
Expand Down
2 changes: 1 addition & 1 deletion omni-slider.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "omni-slider",
"version": "1.2.1",
"version": "1.2.2",
"description": "A multirange vanilla js slider",
"main": "omni-slider.js",
"scripts": {
Expand Down
26 changes: 24 additions & 2 deletions src/omni-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Slider {
constructor(elementContainer = {}, options = {}) {

// Validation of element, the only required argument
if (!elementContainer || (elementContainer.nodeName !== 'DIV' && elementContainer.tagName !== 'DIV')) return;
if (!elementContainer.nodeName && !elementContainer.tagName) return;

// Contains the options for this slider
this.options = {
Expand Down Expand Up @@ -446,7 +446,7 @@ class Slider {
}

/* Accessor for disable property */
disable(boolean) {
disable(boolean = true) {
this.isDisabled = boolean;
if (this.isDisabled) {
this.UI.slider.classList.add('slider-disabled');
Expand Down Expand Up @@ -495,7 +495,29 @@ class Slider {
this.topics[topic].forEach(function(event) {
event(data);
});
}

/* Destory
* Remove all traces of the slider in memory and dereference listeners
*/
destroy(deleteElem = false) {
// Deference all listeners
document.removeEventListener('mousemove', this.movingHandler, true);
document.removeEventListener('mouseup', this.stopHandler, true);
document.removeEventListener('touchmove', this.movingHandler, true);
document.removeEventListener('touchend', this.stopHandler, true);
this.UI.handleLeft.onmousedown = null;
this.UI.handleLeft.ontouchstart = null;
this.UI.handleRight.onmousedown = null;
this.UI.handleRight.ontouchstart = null;

// DOM element itself
if (!!deleteElem) {
this.UI.slider.parentNode.removeChild(this.UI.slider);
}

// Reference to DOM
this.UI = null;
}

}
Expand Down