watchEventRegistration

[ Function ]

public static watchEventRegistration(
    name: string,
    callback: (data: EventProvider.IEventRegResultObject) => void
): DestroyFunction

Monitors an event name for registration events and triggers the associated callback when a registration takes place, e.g. in order to be able to respond to event-specific registration options (see register).

Parameter

Name

Type

Description

name

string

Name of the event

callback

data: EventProvider.IEventRegResultObject) => void

Callback with information about the event registration event.

Return value

Type

Description

TcHmi.DestroyFunction

Deregisters event registration monitoring and releases all associated resources when called.

watchEventRegistration 1:

Available from version 1.10

Example - JavaScript 1

TcHmi.EventProvider.watchEventRegistration('MyEvent', function (data) {
    if (data.error === TcHmi.Errors.NONE) {
        switch (data.type) {
            case TcHmi.EventProvider.EventRegWatchType.REGISTER:
            {
                // Event registration
                console.log(data);
            }
            break;
            case TcHmi.EventProvider.EventRegWatchType.DESTROY:
            {
                // Event destroy
                console.log(data);
            }
            break;
        }
    }
});

var e1 = TcHmi.EventProvider.register('MyEvent', function () { }, { myOption: true });
var e2 = TcHmi.EventProvider.register('MyEvent', function () { });
e2();
e1();

Example - JavaScript 2

Extract from the control class TcHmi.Controls.System.TcHmiControl as implementation example using the event [Id].onMouseDown.

The class TcHmi.Controls.System.TcHmiControl monitors the HMI event [Id].onMouseDown for registration events to allow the user to use different options for each registration.

All HMI events at the level TcHmi.Controls.System.TcHmiControl, which are based on DOM events, enable the DOM event properties passive (Boolean) and capture (Boolean) to be activated or deactivated during registration.

The constant TCHMI_EVENT_OPTION_OBJECT_SUPPORTED determines whether the browser supports a current addEventListener API or whether one is limited to the capture (Boolean) option.

//...
function TcHmiControl(element, pcElement, attrs) {
    //...
    _this.__resourcesDomEventRegWatchOnMouseDown = {};
    //...
}

TcHmiControl.prototype.__attach = function () {
    //...
    __this.__destroyDomEventRegWatchOnMouseDown = TcHmi.EventProvider.watchEventRegistration(this.getId() + '.onMouseDown', function (data) {
        if (data.error === TcHmi.Errors.NONE) {
            switch (data.type) {
                case TcHmi.EventProvider.EventRegWatchType.REGISTER:
                    {
                        __this.__resourcesDomEventRegWatchOnMouseDown[data.event.id] = {
                            event: data.event,
                            domEventListener: {
                            eventName: 'mousedown',
                            listener: __this.__onElementMouseDown.call(__this, data.event),
                            options: TCHMI_EVENT_OPTION_OBJECT_SUPPORTED ? (
                                                        data.event.options ? data.event.options : { passive: false, capture: false }
                                                        ) : (
                                                         (data.event.options && data.event.options.capture) ? data.event.options.capture : false
                                                        )
                            }
                        };
                        var list = __this.__resourcesDomEventRegWatchOnMouseDown[data.event.id].domEventListener;
                        __this.getElement()[0].addEventListener(list.eventName, list.listener, list.options);
                    }
                    break;
                case TcHmi.EventProvider.EventRegWatchType.DESTROY:
                    {
                        var list = __this.__resourcesDomEventRegWatchOnMouseDown[data.event.id].domEventListener;
                        __this.getElement()[0].removeEventListener(list.eventName, list.listener, list.options);
                    }
                    break;
            }
        }
    });
    //...
};

TcHmiControl.prototype.__detach = function () {
    //...
    for (var key in this.__resourcesDomEventRegWatchOnMouseDown) {
        var list = this.__resourcesDomEventRegWatchOnMouseDown[key].domEventListener;
        this.__element[0].removeEventListener(list.eventName, list.listener, list.options);
    }
    //...
    if (this.__destroyDomEventRegWatchOnMouseDown) {
        this.__destroyDomEventRegWatchOnMouseDown();
        this.__destroyDomEventRegWatchOnMouseDown = null;
    }
    //...
};

TcHmiControl.prototype.__onElementMouseDown = function (event) {
    var _this = this;
    return function (e) {
        //...
        TcHmi.EventProvider.raiseEx(event, e);
        //...
    };
};
//...