-
Notifications
You must be signed in to change notification settings - Fork 425
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 `Scope`. In support of iterating through target tokens, export the `TokenListObserver` module's `parseTokenString` function. [#336]: https://3.basecamp.com/2914079/buckets/20224425/todos/3391985862
- Loading branch information
1 parent
c47f551
commit c89e2cc
Showing
6 changed files
with
122 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { Context } from "./context" | ||
import { ElementObserver, ElementObserverDelegate, parseTokenString } from "@stimulus/mutation-observers" | ||
|
||
export class TargetObserver implements ElementObserverDelegate { | ||
readonly context: Context | ||
readonly receiver: any | ||
private elementObserver: ElementObserver | ||
|
||
constructor(context: Context, receiver: any) { | ||
this.context = context | ||
this.receiver = receiver | ||
this.elementObserver = new ElementObserver(this.element, this) | ||
} | ||
|
||
start() { | ||
this.elementObserver.start() | ||
} | ||
|
||
stop() { | ||
this.elementObserver.stop() | ||
} | ||
|
||
matchElement(element: Element): boolean { | ||
return element.matches(this.selector) | ||
} | ||
|
||
matchElementsInTree(tree: Element): Element[] { | ||
const match = this.matchElement(tree) ? [tree] : [] | ||
const matches = Array.from(tree.querySelectorAll(this.selector)) | ||
return match.concat(matches) | ||
} | ||
|
||
elementMatched(element: Element): void { | ||
const value = element.getAttribute(this.attributeName) || "" | ||
const tokens = parseTokenString(value, element, this.attributeName) | ||
|
||
tokens.forEach((token) => this.dispatchCallback(`${token.content}TargetAdded`, element)) | ||
} | ||
|
||
elementUnmatched(element: Element): void { | ||
const value = element.getAttribute(this.attributeName) || "" | ||
const tokens = parseTokenString(value, element, this.attributeName) | ||
|
||
tokens.forEach((token) => this.dispatchCallback(`${token.content}TargetRemoved`, element)) | ||
} | ||
|
||
private dispatchCallback(method: string, element: Element) { | ||
const callback = this.receiver[method] | ||
if (typeof callback == "function") { | ||
callback.call(this.receiver, element) | ||
} | ||
} | ||
|
||
private get selector() { | ||
return `[${this.attributeName}]` | ||
} | ||
|
||
private get attributeName() { | ||
return `data-${this.identifier}-target` | ||
} | ||
|
||
private get identifier() { | ||
return this.context.identifier | ||
} | ||
|
||
private get element() { | ||
return this.context.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