TiddlyWiki5/editions/tw5.com/tiddlers/widgets/EventCatcherWidget.tid

86 lines
4.0 KiB
Plaintext
Raw Normal View History

2020-11-23 17:07:41 +00:00
created: 20201123113532200
modified: 20201202200719126
2020-11-23 17:07:41 +00:00
tags: Widgets
title: EventCatcherWidget
type: text/vnd.tiddlywiki
! Introduction
2020-11-24 21:38:18 +00:00
<<.from-version "5.1.23">>
2020-11-23 17:07:41 +00:00
The event catcher widget traps JavaScript events dispatched within its child content, and allows invoking a series of ActionWidgets in response to the events.
In order for the events to be trapped they must:
* be of one of the types specified as a parameter to the event catcher widget.
2020-11-23 17:07:41 +00:00
* arise within a DOM node matching the selector specified as a parameter to the widget.
Use of the event catcher widget is beneficial when using large numbers of other trigger widgets such as the ButtonWidget is causing performance problems. The workflow it enables is akin to what is referred to as "event delegation" in JavaScript parlance.
2020-11-23 17:07:41 +00:00
//This is an advanced widget intended to be used by those familiar with HTML, CSS and JavaScript.//
! Content and Attributes
The content of the `<$eventcatcher>` widget is displayed normally.
|!Attribute |!Description |
|events |Space separated list of JavaScript events to be trapped, for example "click" or "click dblclick" |
2020-11-23 17:07:41 +00:00
|selector |A CSS selector. Only events originating inside a DOM node with this selector will be trapped. |
|actions-* |Action strings to be invoked when a matching event is trapped. Each event is mapped to an action attribute name of the form `actions-"event"` where `event` represents the type of the event. For example: `actions-click` or `actions-dblclick` |
2020-11-23 17:07:41 +00:00
|class |An optional CSS class name to be assigned to the HTML element |
|tag |Optional. The html element the widget creates to capture the events, defaults to:<br>» `span` when parsed in inline-mode<br>» `div` when parsed in block-mode |
! Variables
The following variables are made available to the actions:
|!Variables |!Description |
|`dom-*` |All DOM attributes of the node matching the given selector are made available as variables, with the prefix `dom-` |
|`modifier` |The [[modifier Variable]] contains the Modifier Key held during the event (can be "normal", "ctrl", "shift", "alt" or combinations thereof) |
|`event-mousebutton`|The mouse button (if any) used to trigger the event (can be "left", "right" or "middle"). Note that not all event types support the mousebutton property |
|`event-type`|The type property of the JavaScript event |
|`event-detail-*`|Any properties in the detail attribute of the event are made available with the prefix `event-detail-` |
2020-11-23 17:07:41 +00:00
|`tv-popup-coords`|A co-ordinate string that can be used with the ActionPopupWidget to trigger a popup at the DOM node matching the selector where the event originated |
|`tv-selectednode-posx`|`x` offset position of the selected DOM node |
|`tv-selectednode-posy`|`y` offset position of the selected DOM node |
|`tv-selectednode-width`|`offsetWidth` of the selected DOM node |
|`tv-selectednode-height`|`offsetHeight` of the selected DOM node |
|`event-fromselected-posx`|`x` position of the event relative to the selected DOM node |
|`event-fromselected-posy`|`y` position of the event relative to the selected DOM node |
|`event-fromcatcher-posx`|`x` position of the event relative to the event catcher DOM node |
|`event-fromcatcher-posy`|`y` position of the event relative to the event catcher DOM node |
! Example
This example uses the ActionLogWidget and will log the `data-item-id` attribute of the clicked DOM node to the browser's JavaScript [[console|Web Developer Tools]]
```
\define clickactions()
<$action-log item=<<dom-data-item-id>> event=<<event-type>>/>
2020-11-23 17:07:41 +00:00
\end
\define contextmenu-actions()
<$action-log item=<<dom-data-item-id>> event=<<event-type>>/>
\end
<$eventcatcher events="click contextmenu" selector=".item" actions-click=<<clickactions>> actions-contextmenu=<<contextmenu-actions>> tag="div">
2020-11-23 17:07:41 +00:00
<div class="item" data-item-id="item1">
Click events here will be trapped
</div>
<div class="item" data-item-id="item2">
And here too
</div>
<div data-item-id="item3">
Not here
</div>
<div class="item" data-item-id="item4">
And here
</div>
</$eventcatcher>
2020-11-23 17:07:41 +00:00
```