ui.date_box.strategy.calendar.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * DevExtreme (ui/date_box/ui.date_box.strategy.calendar.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 Calendar = require("../calendar");
  11. var DateBoxStrategy = require("./ui.date_box.strategy");
  12. var dateUtils = require("../../core/utils/date");
  13. var commonUtils = require("../../core/utils/common");
  14. var isFunction = require("../../core/utils/type").isFunction;
  15. var extend = require("../../core/utils/extend").extend;
  16. var messageLocalization = require("../../localization/message");
  17. var CalendarStrategy = DateBoxStrategy.inherit({
  18. NAME: "Calendar",
  19. supportedKeys: function() {
  20. var homeEndHandler = function(e) {
  21. if (this.option("opened")) {
  22. e.preventDefault();
  23. return true
  24. }
  25. return false
  26. };
  27. return {
  28. rightArrow: function() {
  29. if (this.option("opened")) {
  30. return true
  31. }
  32. },
  33. leftArrow: function() {
  34. if (this.option("opened")) {
  35. return true
  36. }
  37. },
  38. enter: function(e) {
  39. if (this.dateBox.option("opened")) {
  40. e.preventDefault();
  41. if (this._widget.option("zoomLevel") === this._widget.option("maxZoomLevel")) {
  42. var contouredDate = this._widget._view.option("contouredDate");
  43. contouredDate && this.dateBoxValue(contouredDate, e);
  44. this.dateBox.close();
  45. this.dateBox._valueChangeEventHandler(e)
  46. } else {
  47. return true
  48. }
  49. } else {
  50. this.dateBox._valueChangeEventHandler(e)
  51. }
  52. }.bind(this),
  53. home: homeEndHandler,
  54. end: homeEndHandler
  55. }
  56. },
  57. getDisplayFormat: function(displayFormat) {
  58. return displayFormat || "shortdate"
  59. },
  60. _getWidgetName: function() {
  61. return Calendar
  62. },
  63. _getWidgetOptions: function() {
  64. var disabledDates = this.dateBox.option("disabledDates");
  65. return extend(this.dateBox.option("calendarOptions"), {
  66. value: this.dateBoxValue() || null,
  67. dateSerializationFormat: null,
  68. _keyboardProcessor: this._widgetKeyboardProcessor,
  69. min: this.dateBox.dateOption("min"),
  70. max: this.dateBox.dateOption("max"),
  71. onValueChanged: this._valueChangedHandler.bind(this),
  72. onCellClick: this._cellClickHandler.bind(this),
  73. tabIndex: null,
  74. disabledDates: isFunction(disabledDates) ? this._injectComponent(disabledDates.bind(this.dateBox)) : disabledDates,
  75. onContouredChanged: this._refreshActiveDescendant.bind(this),
  76. hasFocus: function() {
  77. return true
  78. }
  79. })
  80. },
  81. _injectComponent: function(func) {
  82. var that = this;
  83. return function(params) {
  84. extend(params, {
  85. component: that.dateBox
  86. });
  87. return func(params)
  88. }
  89. },
  90. _refreshActiveDescendant: function(e) {
  91. this.dateBox.setAria("activedescendant", e.actionValue)
  92. },
  93. popupConfig: function(_popupConfig) {
  94. var toolbarItems = _popupConfig.toolbarItems;
  95. var buttonsLocation = this.dateBox.option("buttonsLocation");
  96. var position = [];
  97. if ("default" !== buttonsLocation) {
  98. position = commonUtils.splitPair(buttonsLocation)
  99. } else {
  100. position = ["bottom", "center"]
  101. }
  102. if ("useButtons" === this.dateBox.option("applyValueMode")) {
  103. toolbarItems.unshift({
  104. widget: "dxButton",
  105. toolbar: position[0],
  106. location: "after" === position[1] ? "before" : position[1],
  107. options: {
  108. onInitialized: function(e) {
  109. e.component.registerKeyHandler("escape", this._escapeHandler.bind(this))
  110. }.bind(this),
  111. onClick: function() {
  112. this._widget._toTodayView()
  113. }.bind(this),
  114. text: messageLocalization.format("dxCalendar-todayButtonText"),
  115. type: "today"
  116. }
  117. })
  118. }
  119. return extend(true, _popupConfig, {
  120. toolbarItems: toolbarItems,
  121. position: {
  122. collision: "flipfit flip"
  123. }
  124. })
  125. },
  126. _escapeHandler: function() {
  127. this.dateBox.close();
  128. this.dateBox.focus()
  129. },
  130. _valueChangedHandler: function(e) {
  131. var dateBox = this.dateBox;
  132. var value = e.value;
  133. var prevValue = e.previousValue;
  134. if (dateUtils.sameDate(value, prevValue)) {
  135. return
  136. }
  137. if ("instantly" === dateBox.option("applyValueMode")) {
  138. this.dateBoxValue(this.getValue(), e.event)
  139. }
  140. },
  141. _updateValue: function() {
  142. if (!this._widget) {
  143. return
  144. }
  145. this._widget.option("value", this.dateBoxValue())
  146. },
  147. textChangedHandler: function() {
  148. if (this.dateBox.option("opened") && this._widget) {
  149. this._updateValue(true)
  150. }
  151. },
  152. _cellClickHandler: function(e) {
  153. var dateBox = this.dateBox;
  154. if ("instantly" === dateBox.option("applyValueMode")) {
  155. dateBox.option("opened", false);
  156. this.dateBoxValue(this.getValue(), e.event)
  157. }
  158. }
  159. });
  160. module.exports = CalendarStrategy;