devextreme
Version:
HTML5 JavaScript Component Suite for Responsive Web Development
154 lines (153 loc) • 4.64 kB
JavaScript
/**
* DevExtreme (esm/__internal/events/core/m_emitter.feedback.js)
* Version: 24.2.6
* Build date: Mon Mar 17 2025
*
* Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED
* Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
*/
import Emitter from "../../../common/core/events/core/emitter";
import registerEmitter from "../../../common/core/events/core/emitter_registrator";
import pointerEvents from "../../../common/core/events/pointer";
import {
isMouseEvent
} from "../../../common/core/events/utils/index";
import Class from "../../../core/class";
import {
ensureDefined,
noop
} from "../../../core/utils/common";
import {
contains
} from "../../../core/utils/dom";
import devices from "../../core/m_devices";
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)
},
configure(data, eventName) {
switch (eventName) {
case "dxactive":
data.activeTimeout = data.timeout;
break;
case "dxinactive":
data.inactiveTimeout = data.timeout
}
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, 30);
const inactiveTimeout = ensureDefined(this.inactiveTimeout, 400);
this._active = new FeedbackEvent(deferFeedback ? activeTimeout : 0, (() => {
that._fireEvent("dxactive", e, {
target: eventTarget
})
}));
this._inactive = new FeedbackEvent(deferFeedback ? inactiveTimeout : 0, (() => {
that._fireEvent("dxinactive", 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)
}
});
FeedbackEmitter.lock = function(deferred) {
const lockInactive = activeFeedback ? activeFeedback.lockInactive() : noop;
deferred.done(lockInactive)
};
registerEmitter({
emitter: FeedbackEmitter,
events: ["dxactive", "dxinactive"]
});
export const {
lock: lock
} = FeedbackEmitter;
export {
ACTIVE_EVENT_NAME as active, INACTIVE_EVENT_NAME as inactive
};