Click actions
The classes and the data attributes that call a function or change the URL on a click, a double click and a right click.
On this page
The library handles three CSS classes in the whole page, also in elements added later: exec for a click, exec2 for a double click and exec3 for a click with the right button. What happens is written in data- attributes. An introduction with examples is in the guide Click actions.
When a component named exec is declared in the page, it takes over this work and the library does nothing.
API
property
data-exec="name"
Calls a function when an element with the class exec is clicked.
data-exec="name"The function receives the element as a jQuery object and the event. The name has the forms of $(selector).EXEC():
| Name | Calls |
|---|---|
FUNC.name | A function found at a path |
?/method | A method of the closest plugin |
plugin/method | A method of a plugin |
@method | A method of the closest component. It receives the component ahead of the element and the event |
-component/method | A component method |
&command | A command of components |
#event | An event |
<button id="save" class="exec" data-exec="FUNC.save">Save</button>
<script>
FUNC.save = function(element, e) {
console.log(element.attr('id'), e.type); // save click
};
ON('ready', () => $('#save').trigger('click'));
</script>
<ui-component name="box"><button id="close" class="exec" data-exec="@close">Close</button></ui-component>
<script>
COMPONENT('box', function(self) {
self.close = function(component, element) {
console.log(component.name, element.attr('id')); // box close
};
});
setTimeout(() => $('#close').trigger('click'), 300);
</script>
property
data-href="url"
Changes the URL when an element with the class exec is clicked.
data-href="url"The address is passed to REDIRECT(). With data-exec on the same element, the function runs first.
<button id="orders" class="exec" data-href="/orders/">Orders</button>
<script>
ROUTE('/orders/', function() {
console.log('orders'); // orders
});
setTimeout(() => $('#orders').trigger('click'), 300);
</script>
property
data-prevent="true"
Stops the default action of the browser and the propagation of the click.
data-prevent="true"The values are true and 1. Use it for links and for buttons that submit a form.
<a id="help" class="exec" data-exec="FUNC.help" data-prevent="true" href="/help/">Help</a>
<script>
FUNC.help = function(element, e) {
console.log(e.isDefaultPrevented()); // true
};
ON('ready', () => $('#help').trigger('click'));
</script>
property
data-exec2="name"
Calls a function when an element with the class exec2 is double-clicked.
data-exec2="name"The names and the arguments are those of data-exec. data-href2 and data-prevent2 work in the same way for the double click.
An element can have both classes, exec and exec2. A click then waits 300 milliseconds, and a double click in that time calls only the function of data-exec2.
<div id="row" class="exec2" data-exec2="FUNC.open">Row</div>
<script>
FUNC.open = function(element, e) {
console.log(element.attr('id'), e.type); // row dblclick
};
ON('ready', () => $('#row').trigger('dblclick'));
</script>
property
data-exec3="name"
Calls a function when an element with the class exec3 is clicked with the right button.
data-exec3="name"The names and the arguments are those of data-exec, and data-href3 changes the URL. The menu of the browser is not displayed.
<div id="file" class="exec3" data-exec3="FUNC.menu">File</div>
<script>
FUNC.menu = function(element, e) {
console.log(element.attr('id'), e.isDefaultPrevented()); // file true
};
ON('ready', () => $('#file').trigger('contextmenu'));
</script>