-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fire callbacks when targets are added or removed
Closes [#336][] --- Implements the `TargetObserver` to monitor when elements declaring `[data-${identifier}-target]` are added or removed from a `Context` _after_ being connected and _before_ being disconnected. In support of iterating through target tokens, export the `TokenListObserver` module's `parseTokenString` function. [#336]: #336
- Loading branch information
1 parent
3bfd886
commit 4d7f686
Showing
10 changed files
with
204 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Context } from "./context" | ||
import { Controller } from "./controller" | ||
import { TokenListObserver, TokenListObserverDelegate, Token, parseTokenString } from "@stimulus/mutation-observers" | ||
|
||
export class TargetObserver implements TokenListObserverDelegate { | ||
readonly context: Context | ||
readonly controller: Controller | ||
readonly attributeName: string | ||
private tokenListObserver: TokenListObserver | ||
|
||
constructor(context: Context, controller: Controller) { | ||
this.context = context | ||
this.controller = controller | ||
this.attributeName = `data-${context.identifier}-target` | ||
this.tokenListObserver = new TokenListObserver(this.context.element, this.attributeName, this) | ||
} | ||
|
||
start() { | ||
this.tokenListObserver.start() | ||
} | ||
|
||
stop() { | ||
this.tokenListObserver.stop() | ||
} | ||
|
||
tokenMatched(token: Token): void { | ||
if (this.controller.isConnected && this.containsDescendantWithToken(token.element, token.content)) { | ||
this.dispatchCallback(`${token.content}TargetAdded`, token.element) | ||
} | ||
} | ||
|
||
tokenUnmatched(token: Token): void { | ||
if (this.controller.isConnected && !this.containsDescendantWithToken(token.element, token.content)) { | ||
this.dispatchCallback(`${token.content}TargetRemoved`, token.element) | ||
} | ||
} | ||
|
||
private containsDescendantWithToken(element: Element, content: string): boolean { | ||
const targetTokens = parseTokenString(element.getAttribute(this.attributeName) || "", element, this.attributeName) | ||
|
||
return targetTokens.map(token => token.content).includes(content) && this.context.element.contains(element) | ||
} | ||
|
||
private dispatchCallback(method: string, element: Element) { | ||
const callback = (this.controller as any)[method] | ||
if (typeof callback == "function") { | ||
callback.call(this.controller, element) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters