Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Event Modules to TS #27779

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
181 changes: 181 additions & 0 deletions packages/devextreme/js/__internal/events/core/m_emitter.feedback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import Class from '@js/core/class';
import devices from '@js/core/devices';
import { ensureDefined, noop } from '@js/core/utils/common';
import { contains } from '@js/core/utils/dom';
import Emitter from '@js/events/core/emitter';
import registerEmitter from '@js/events/core/emitter_registrator';
import pointerEvents from '@js/events/pointer';
import { isMouseEvent } from '@js/events/utils/index';

const ACTIVE_EVENT_NAME = 'dxactive';
const INACTIVE_EVENT_NAME = 'dxinactive';

const ACTIVE_TIMEOUT = 30;
const INACTIVE_TIMEOUT = 400;

const FeedbackEvent = Class.inherit({

ctor(timeout, fire) {
this._timeout = timeout;
this._fire = fire;
},

start() {
const that = this;

this._schedule(() => {
that.force();
});
},

_schedule(fn) {
this.stop();
this._timer = setTimeout(fn, this._timeout);
},

stop() {
clearTimeout(this._timer);
},

force() {
if (this._fired) {
return;
}

this.stop();
this._fire();
this._fired = true;
},

fired() {
return this._fired;
},

});

let activeFeedback;

const FeedbackEmitter = Emitter.inherit({

ctor() {
this.callBase.apply(this, arguments);

this._active = new FeedbackEvent(0, noop);
this._inactive = new FeedbackEvent(0, noop);
},

/* eslint-disable default-case */
configure(data, eventName) {
switch (eventName) {
case ACTIVE_EVENT_NAME:
data.activeTimeout = data.timeout;
break;
case INACTIVE_EVENT_NAME:
data.inactiveTimeout = data.timeout;
break;
}

this.callBase(data);
},

start(e) {
if (activeFeedback) {
const activeChildExists = contains(this.getElement().get(0), activeFeedback.getElement().get(0));
const childJustActivated = !activeFeedback._active.fired();

if (activeChildExists && childJustActivated) {
this._cancel();
return;
}

activeFeedback._inactive.force();
}
activeFeedback = this;

this._initEvents(e);
this._active.start();
},

_initEvents(e) {
const that = this;

const eventTarget = this._getEmitterTarget(e);

const mouseEvent = isMouseEvent(e);
const isSimulator = devices.isSimulator();
const deferFeedback = isSimulator || !mouseEvent;

const activeTimeout = ensureDefined(this.activeTimeout, ACTIVE_TIMEOUT);
const inactiveTimeout = ensureDefined(this.inactiveTimeout, INACTIVE_TIMEOUT);

this._active = new FeedbackEvent(deferFeedback ? activeTimeout : 0, () => {
that._fireEvent(ACTIVE_EVENT_NAME, e, { target: eventTarget });
});
this._inactive = new FeedbackEvent(deferFeedback ? inactiveTimeout : 0, () => {
that._fireEvent(INACTIVE_EVENT_NAME, e, { target: eventTarget });
activeFeedback = null;
});
},

cancel(e) {
this.end(e);
},

end(e) {
const skipTimers = e.type !== pointerEvents.up;

if (skipTimers) {
this._active.stop();
} else {
this._active.force();
}

this._inactive.start();

if (skipTimers) {
this._inactive.force();
}
},

dispose() {
this._active.stop();
this._inactive.stop();

if (activeFeedback === this) {
activeFeedback = null;
}

this.callBase();
},

lockInactive() {
this._active.force();
this._inactive.stop();
activeFeedback = null;
this._cancel();

return this._inactive.force.bind(this._inactive);
},

});
// @ts-expect-error
FeedbackEmitter.lock = function (deferred) {
const lockInactive = activeFeedback ? activeFeedback.lockInactive() : noop;

deferred.done(lockInactive);
};

registerEmitter({
emitter: FeedbackEmitter,
events: [
ACTIVE_EVENT_NAME,
INACTIVE_EVENT_NAME,
],
});

// @ts-expect-error
export const { lock } = FeedbackEmitter;
export {
ACTIVE_EVENT_NAME as active,
INACTIVE_EVENT_NAME as inactive,
};
106 changes: 106 additions & 0 deletions packages/devextreme/js/__internal/events/core/m_emitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import Class from '@js/core/class';
import $ from '@js/core/renderer';
import Callbacks from '@js/core/utils/callbacks';
import { noop } from '@js/core/utils/common';
import { extend } from '@js/core/utils/extend';
import { fireEvent, hasTouches, isDxMouseWheelEvent } from '@js/events/utils/index';

const Emitter = Class.inherit({

ctor(element) {
this._$element = $(element);

this._cancelCallback = Callbacks();
this._acceptCallback = Callbacks();
},

getElement() {
return this._$element;
},

validate(e) {
return !isDxMouseWheelEvent(e);
},

validatePointers(e) {
return hasTouches(e) === 1;
},

allowInterruptionByMouseWheel() {
return true;
},

configure(data) {
extend(this, data);
},

addCancelCallback(callback) {
this._cancelCallback.add(callback);
},

removeCancelCallback() {
this._cancelCallback.empty();
},

_cancel(e) {
this._cancelCallback.fire(this, e);
},

addAcceptCallback(callback) {
this._acceptCallback.add(callback);
},

removeAcceptCallback() {
this._acceptCallback.empty();
},

_accept(e) {
this._acceptCallback.fire(this, e);
},

_requestAccept(e) {
this._acceptRequestEvent = e;
},

_forgetAccept() {
this._accept(this._acceptRequestEvent);
this._acceptRequestEvent = null;
},

start: noop,
move: noop,
end: noop,

cancel: noop,
reset() {
if (this._acceptRequestEvent) {
this._accept(this._acceptRequestEvent);
}
},

_fireEvent(eventName, e, params) {
const eventData = extend({
type: eventName,
originalEvent: e,
target: this._getEmitterTarget(e),
delegateTarget: this.getElement().get(0),
}, params);

e = fireEvent(eventData);

if (e.cancel) {
this._cancel(e);
}

return e;
},

_getEmitterTarget(e) {
return (this.delegateSelector ? $(e.target).closest(this.delegateSelector) : this.getElement()).get(0);
},

dispose: noop,

});

export default Emitter;
Loading
Loading