swipeable.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * DevExtreme (events/gesture/swipeable.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 swipeEvents = require("../swipe");
  11. var eventsEngine = require("../../events/core/events_engine");
  12. var DOMComponent = require("../../core/dom_component");
  13. var each = require("../../core/utils/iterator").each;
  14. var eventUtils = require("../utils");
  15. var extend = require("../../core/utils/extend").extend;
  16. var publicComponentUtils = require("../../core/utils/public_component");
  17. var DX_SWIPEABLE = "dxSwipeable";
  18. var SWIPEABLE_CLASS = "dx-swipeable";
  19. var ACTION_TO_EVENT_MAP = {
  20. onStart: swipeEvents.start,
  21. onUpdated: swipeEvents.swipe,
  22. onEnd: swipeEvents.end,
  23. onCancel: "dxswipecancel"
  24. };
  25. var Swipeable = DOMComponent.inherit({
  26. _getDefaultOptions: function() {
  27. return extend(this.callBase(), {
  28. elastic: true,
  29. immediate: false,
  30. direction: "horizontal",
  31. itemSizeFunc: null,
  32. onStart: null,
  33. onUpdated: null,
  34. onEnd: null,
  35. onCancel: null
  36. })
  37. },
  38. _render: function() {
  39. this.callBase();
  40. this.$element().addClass(SWIPEABLE_CLASS);
  41. this._attachEventHandlers()
  42. },
  43. _attachEventHandlers: function() {
  44. this._detachEventHandlers();
  45. if (this.option("disabled")) {
  46. return
  47. }
  48. var NAME = this.NAME;
  49. this._createEventData();
  50. each(ACTION_TO_EVENT_MAP, function(actionName, eventName) {
  51. var action = this._createActionByOption(actionName, {
  52. context: this
  53. });
  54. eventName = eventUtils.addNamespace(eventName, NAME);
  55. eventsEngine.on(this.$element(), eventName, this._eventData, function(e) {
  56. return action({
  57. event: e
  58. })
  59. })
  60. }.bind(this))
  61. },
  62. _createEventData: function() {
  63. this._eventData = {
  64. elastic: this.option("elastic"),
  65. itemSizeFunc: this.option("itemSizeFunc"),
  66. direction: this.option("direction"),
  67. immediate: this.option("immediate")
  68. }
  69. },
  70. _detachEventHandlers: function() {
  71. eventsEngine.off(this.$element(), "." + DX_SWIPEABLE)
  72. },
  73. _optionChanged: function(args) {
  74. switch (args.name) {
  75. case "disabled":
  76. case "onStart":
  77. case "onUpdated":
  78. case "onEnd":
  79. case "onCancel":
  80. case "elastic":
  81. case "immediate":
  82. case "itemSizeFunc":
  83. case "direction":
  84. this._detachEventHandlers();
  85. this._attachEventHandlers();
  86. break;
  87. case "rtlEnabled":
  88. break;
  89. default:
  90. this.callBase(args)
  91. }
  92. }
  93. });
  94. publicComponentUtils.name(Swipeable, DX_SWIPEABLE);
  95. module.exports = Swipeable;