Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | 24x 39x 24x 17x 24x 11x 24x 3x 3x 24x 2x 24x 9x 24x 2x 24x 2x 24x 3x 24x 2x 24x 25x 24x 4x 4x 24x 1x 1x 24x 9x 9x 24x 2x 2x 24x 2x 24x 12x 12x 24x 5x 5x 24x 4x 24x 1x 24x | import {
TagInstance,
TagInterface,
TagOpts,
TagRefs,
NestedTags,
ObservableCallback,
TagMixin,
} from 'riot';
import { toHTML, toJSON } from '../transform';
import Simulate, { FireEvent } from '../Simulate';
import find from './find';
import WeakWrapper from './WeakWrapper';
/**
* Wrapper of tag instance shallow-rendered.
*
* @see shallow
*/
export default class RiotWrapper<
TOpts extends TagOpts = TagOpts,
TRefs extends TagRefs = TagRefs,
TTags extends NestedTags = NestedTags
> implements TagInterface {
/**
* Constructor
*
* @param tagInstance - tag instance to wrap
*/
constructor(private readonly tagInstance: TagInstance) {}
/** Get tag instance */
get instance() {
return this.tagInstance;
}
/** Get the root element */
get root() {
return this.tagInstance.root;
}
/**
* Get opts of the instance.
* It is typically passed opts with "dataIs" property
*/
get opts() {
const opts = this.tagInstance.opts;
return opts as TOpts & { dataIs: string };
}
/** Get refs of the instance */
get refs(): TRefs {
return this.tagInstance.refs as TRefs;
}
/** Get tags */
get tags(): TTags {
return this.tagInstance.tags as TTags;
}
/**
* Parent tag being always `null`
* Not undefined but null(type definition of riot **is** wrong.)
*/
get parent(): TagInstance | undefined {
return this.tagInstance.parent;
}
/** Update this tag and its children */
update<T extends {}>(data?: T): void {
this.tagInstance.update(data);
}
/** Is the tag mounted? */
get isMounted() {
return this.tagInstance.isMounted!;
}
/** Mount the tag */
mount() {
this.tagInstance.mount();
}
/** Unmount the tag */
unmount(keepTheParent?: boolean) {
this.tagInstance.unmount(keepTheParent);
}
/**
* Register callback.
*
* Note: `this` is unwrapped instance as well.
*/
on(event: string, callback: ObservableCallback): this {
this.tagInstance.on(event, callback);
return this;
}
/**
* Register callback once.
*
* Note: `this` is unwrapped instance as well.
*/
one(event: string, callback: ObservableCallback): this {
this.tagInstance.one(event, callback);
return this;
}
/**
* Execute callbacks
*/
trigger(event: string, ...args: any[]): this {
this.tagInstance.trigger(event, ...args);
return this;
}
/**
* Unregister callback(s)
*/
off(event: string, callback?: ObservableCallback): this {
this.tagInstance.off(event, callback);
return this;
}
/** Apply mixin */
mixin(mixin: string | TagMixin) {
this.instance.mixin(mixin);
}
/** Get outer-HTML string */
html() {
const root = this.tagInstance.root;
return root !== undefined ? toHTML(root) : '';
}
toJSON(): object | null {
const root = this.tagInstance.root;
return toJSON(root !== undefined ? (root as any) : null);
}
/** Find elements by selector under this node */
find(selector: string): WeakWrapper {
return find(selector, this.root);
}
/**
* Simulate firing an event
*
* @param type event type
* @param options options to override event object
*/
simulate<T extends {}>(type: string, options?: T) {
return ((Simulate as any) as { [type: string]: FireEvent })[type](
this.tagInstance.root,
options
);
}
}
|