emitter.feedback.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * DevExtreme (events/core/emitter.feedback.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var Class = require("../../core/class");
  11. var commonUtils = require("../../core/utils/common");
  12. var contains = require("../../core/utils/dom").contains;
  13. var devices = require("../../core/devices");
  14. var eventUtils = require("../utils");
  15. var pointerEvents = require("../pointer");
  16. var Emitter = require("./emitter");
  17. var registerEmitter = require("./emitter_registrator");
  18. var ACTIVE_EVENT_NAME = "dxactive";
  19. var INACTIVE_EVENT_NAME = "dxinactive";
  20. var ACTIVE_TIMEOUT = 30;
  21. var INACTIVE_TIMEOUT = 400;
  22. var FeedbackEvent = Class.inherit({
  23. ctor: function(timeout, fire) {
  24. this._timeout = timeout;
  25. this._fire = fire
  26. },
  27. start: function() {
  28. var that = this;
  29. this._schedule(function() {
  30. that.force()
  31. })
  32. },
  33. _schedule: function(fn) {
  34. this.stop();
  35. this._timer = setTimeout(fn, this._timeout)
  36. },
  37. stop: function() {
  38. clearTimeout(this._timer)
  39. },
  40. force: function() {
  41. if (this._fired) {
  42. return
  43. }
  44. this.stop();
  45. this._fire();
  46. this._fired = true
  47. },
  48. fired: function() {
  49. return this._fired
  50. }
  51. });
  52. var activeFeedback;
  53. var FeedbackEmitter = Emitter.inherit({
  54. ctor: function() {
  55. this.callBase.apply(this, arguments);
  56. this._active = new FeedbackEvent(0, commonUtils.noop);
  57. this._inactive = new FeedbackEvent(0, commonUtils.noop)
  58. },
  59. configure: function(data, eventName) {
  60. switch (eventName) {
  61. case ACTIVE_EVENT_NAME:
  62. data.activeTimeout = data.timeout;
  63. break;
  64. case INACTIVE_EVENT_NAME:
  65. data.inactiveTimeout = data.timeout
  66. }
  67. this.callBase(data)
  68. },
  69. start: function(e) {
  70. if (activeFeedback) {
  71. var activeChildExists = contains(this.getElement().get(0), activeFeedback.getElement().get(0));
  72. var childJustActivated = !activeFeedback._active.fired();
  73. if (activeChildExists && childJustActivated) {
  74. this._cancel();
  75. return
  76. }
  77. activeFeedback._inactive.force()
  78. }
  79. activeFeedback = this;
  80. this._initEvents(e);
  81. this._active.start()
  82. },
  83. _initEvents: function(e) {
  84. var that = this;
  85. var eventTarget = this._getEmitterTarget(e);
  86. var mouseEvent = eventUtils.isMouseEvent(e);
  87. var isSimulator = devices.isSimulator();
  88. var deferFeedback = isSimulator || !mouseEvent;
  89. var activeTimeout = commonUtils.ensureDefined(this.activeTimeout, ACTIVE_TIMEOUT);
  90. var inactiveTimeout = commonUtils.ensureDefined(this.inactiveTimeout, INACTIVE_TIMEOUT);
  91. this._active = new FeedbackEvent(deferFeedback ? activeTimeout : 0, function() {
  92. that._fireEvent(ACTIVE_EVENT_NAME, e, {
  93. target: eventTarget
  94. })
  95. });
  96. this._inactive = new FeedbackEvent(deferFeedback ? inactiveTimeout : 0, function() {
  97. that._fireEvent(INACTIVE_EVENT_NAME, e, {
  98. target: eventTarget
  99. });
  100. activeFeedback = null
  101. })
  102. },
  103. cancel: function(e) {
  104. this.end(e)
  105. },
  106. end: function(e) {
  107. var skipTimers = e.type !== pointerEvents.up;
  108. if (skipTimers) {
  109. this._active.stop()
  110. } else {
  111. this._active.force()
  112. }
  113. this._inactive.start();
  114. if (skipTimers) {
  115. this._inactive.force()
  116. }
  117. },
  118. dispose: function() {
  119. this._active.stop();
  120. this._inactive.stop();
  121. this.callBase()
  122. },
  123. lockInactive: function() {
  124. this._active.force();
  125. this._inactive.stop();
  126. activeFeedback = null;
  127. this._cancel();
  128. return this._inactive.force.bind(this._inactive)
  129. }
  130. });
  131. FeedbackEmitter.lock = function(deferred) {
  132. var lockInactive = activeFeedback ? activeFeedback.lockInactive() : commonUtils.noop;
  133. deferred.done(lockInactive)
  134. };
  135. registerEmitter({
  136. emitter: FeedbackEmitter,
  137. events: [ACTIVE_EVENT_NAME, INACTIVE_EVENT_NAME]
  138. });
  139. exports.lock = FeedbackEmitter.lock;
  140. exports.active = ACTIVE_EVENT_NAME;
  141. exports.inactive = INACTIVE_EVENT_NAME;