events_strategy.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. /**
  2. * DevExtreme (core/events_strategy.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 Callbacks = require("./utils/callbacks");
  11. var isFunction = require("./utils/type").isFunction;
  12. var each = require("./utils/iterator").each;
  13. var Class = require("./class");
  14. module.exports = Class.inherit({
  15. ctor: function(owner) {
  16. this._events = {};
  17. this._owner = owner
  18. },
  19. hasEvent: function(eventName) {
  20. var callbacks = this._events[eventName];
  21. if (callbacks) {
  22. return callbacks.has()
  23. }
  24. return false
  25. },
  26. fireEvent: function(eventName, eventArgs) {
  27. var callbacks = this._events[eventName];
  28. if (callbacks) {
  29. callbacks.fireWith(this._owner, eventArgs)
  30. }
  31. },
  32. on: function(eventName, eventHandler) {
  33. var callbacks = this._events[eventName];
  34. if (!callbacks) {
  35. callbacks = Callbacks();
  36. this._events[eventName] = callbacks
  37. }
  38. var addFn = callbacks.originalAdd || callbacks.add;
  39. addFn.call(callbacks, eventHandler)
  40. },
  41. off: function(eventName, eventHandler) {
  42. var callbacks = this._events[eventName];
  43. if (callbacks) {
  44. if (isFunction(eventHandler)) {
  45. callbacks.remove(eventHandler)
  46. } else {
  47. callbacks.empty()
  48. }
  49. }
  50. },
  51. dispose: function() {
  52. each(this._events, function() {
  53. this.empty()
  54. })
  55. }
  56. });