Skip to content

Commit

Permalink
Implement a method to destroy detached widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Oct 3, 2021
1 parent 33c633e commit 98af7a2
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ The general purpose of the plugin is to provide some lengthy containers on the p

## Details & API

There is the only public method used to instantiate and control a floating scrollbar — `.floatingScroll()`. The plugin method `.floatingScroll()` should be called in context of a scrollable container. It takes an optional argument `method`. The currently supported methods are
There is the only public method used to instantiate and control a floating scrollbar — `.floatingScroll()`. Unless otherwise indicated, the plugin method `.floatingScroll()` should be called in context of a scrollable container. It takes an optional argument `method`. The currently supported methods are

* [`init`](#initialisation) (default value) — used to initialize a floating scrollbar widget;
* [`update`](#updating-scrollbar) — used t force update of the floating scrollbar parameters (size and position);
* [`destroy`](#destroying-floating-scrollbar) — removes a scrollbar and all related event handlers.
* [`destroy`](#destroying-floating-scrollbar) — removes a scrollbar and all related event handlers;
* [`destroyDetached`](#destroying-detached-widgets) — destroys floating scrollbar instances whose containers were removed from the document (requires a different context, see below).

You may also [trigger](https://api.jquery.com/trigger/) events `update.fscroll` and `destroy.fscroll` on containers with attached floating scrollbar: this does the same as invoking the corresponding methods.

Expand Down Expand Up @@ -82,6 +83,18 @@ To detach a scrollbar and remove all related event handlers, use the method `des
$(".spacious-container").floatingScroll("destroy");
```

### Destroying detached widgets

If your app completely re-renders a large portion of DOM where floating scrollbar widgets were mounted, actual container references are lost, and therefore you cannot destroy the widgets and perform related cleanup using the `destroy` method. In this case, you may just call the `destroyDetached` method, and the pluign will find all “zombie” instances and will destroy them for you. Unlike the other methods, this one needs to be called in context of `$(window)`:

```javascript
$(".main-view .spacious-container").floatingScroll("init");
// ... the app re-renders the main view ...
$(".main-view").html("...");
// destroy floating scrollbar widgets whose containers are not in the document anymore
$(window).floatingScroll("destroyDetached");
```

### Special cases

If you want to attach a floating scrollbar to a container living in a positioned box (e.g. a modal popup with `position: fixed`) then you need to apply two special indicating class names in the markup. The plugin detects these indicating class names (they are prefixed with `fl-scrolls-`) and switches to a special functioning mode.
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.floatingscroll.es6.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dist/jquery.floatingscroll.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/jquery.floatingscroll.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ let floatingScrollProto = {
addEventHandlers() {
let instance = this;
let eventHandlers = instance.eventHandlers = [
{
$el: $(window),
handlers: {
"destroyDetached.fscroll"({namespace}) {
if (namespace === "fscroll") {
instance.destroyDetachedAPI();
}
}
}
},
{
$el: instance.scrollBody || $(window),
handlers: {
Expand Down Expand Up @@ -182,6 +192,12 @@ let floatingScrollProto = {
instance.eventHandlers.forEach(({$el, handlers}) => $el.unbind(handlers));
instance.widget.remove();
instance.eventHandlers = instance.widget = instance.container = instance.scrollBody = null;
},

destroyDetachedAPI() {
if (!$.contains(document.body, this.container)) {
this.destroyAPI();
}
}
};

Expand Down

0 comments on commit 98af7a2

Please sign in to comment.