ui.calendar.base_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. /**
  2. * DevExtreme (ui/calendar/ui.calendar.base_view.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 _renderer = require("../../core/renderer");
  11. var _renderer2 = _interopRequireDefault(_renderer);
  12. var _dom_adapter = require("../../core/dom_adapter");
  13. var _dom_adapter2 = _interopRequireDefault(_dom_adapter);
  14. var _events_engine = require("../../events/core/events_engine");
  15. var _events_engine2 = _interopRequireDefault(_events_engine);
  16. var _element_data = require("../../core/element_data");
  17. var _element_data2 = _interopRequireDefault(_element_data);
  18. var _dom = require("../../core/utils/dom");
  19. var _dom2 = _interopRequireDefault(_dom);
  20. var _ui = require("../widget/ui.widget");
  21. var _ui2 = _interopRequireDefault(_ui);
  22. var _date = require("../../core/utils/date");
  23. var _extend = require("../../core/utils/extend");
  24. var _common = require("../../core/utils/common");
  25. var _date_serialization = require("../../core/utils/date_serialization");
  26. var _message = require("../../localization/message");
  27. var _utils = require("../../events/utils");
  28. var _click = require("../../events/click");
  29. function _interopRequireDefault(obj) {
  30. return obj && obj.__esModule ? obj : {
  31. "default": obj
  32. }
  33. }
  34. var abstract = _ui2.default.abstract;
  35. var CALENDAR_OTHER_VIEW_CLASS = "dx-calendar-other-view";
  36. var CALENDAR_CELL_CLASS = "dx-calendar-cell";
  37. var CALENDAR_EMPTY_CELL_CLASS = "dx-calendar-empty-cell";
  38. var CALENDAR_TODAY_CLASS = "dx-calendar-today";
  39. var CALENDAR_SELECTED_DATE_CLASS = "dx-calendar-selected-date";
  40. var CALENDAR_CONTOURED_DATE_CLASS = "dx-calendar-contoured-date";
  41. var CALENDAR_DXCLICK_EVENT_NAME = (0, _utils.addNamespace)(_click.name, "dxCalendar");
  42. var CALENDAR_DATE_VALUE_KEY = "dxDateValueKey";
  43. var BaseView = _ui2.default.inherit({
  44. _getViewName: function() {
  45. return "base"
  46. },
  47. _getDefaultOptions: function() {
  48. return (0, _extend.extend)(this.callBase(), {
  49. date: new Date,
  50. focusStateEnabled: false,
  51. cellTemplate: null,
  52. disabledDates: null,
  53. onCellClick: null,
  54. rowCount: 3,
  55. colCount: 4,
  56. allowValueSelection: true
  57. })
  58. },
  59. _init: function() {
  60. this.callBase();
  61. var value = this.option("value");
  62. this.option("value", new Date(value));
  63. if (!this.option("value").valueOf()) {
  64. this.option("value", new Date(0, 0, 0, 0, 0, 0))
  65. }
  66. },
  67. _initMarkup: function() {
  68. this.callBase();
  69. this._renderImpl()
  70. },
  71. _renderImpl: function() {
  72. this.$element().append(this._createTable());
  73. this._createDisabledDatesHandler();
  74. this._renderBody();
  75. this._renderContouredDate();
  76. this._renderValue();
  77. this._renderEvents()
  78. },
  79. _createTable: function() {
  80. this._$table = (0, _renderer2.default)("<table>");
  81. this.setAria({
  82. label: (0, _message.format)("dxCalendar-ariaWidgetName"),
  83. role: "grid"
  84. }, this._$table);
  85. return this._$table
  86. },
  87. _renderBody: function() {
  88. this.$body = (0, _renderer2.default)("<tbody>").appendTo(this._$table);
  89. var rowData = {
  90. cellDate: this._getFirstCellData(),
  91. prevCellDate: null
  92. };
  93. for (var rowIndex = 0, rowCount = this.option("rowCount"); rowIndex < rowCount; rowIndex++) {
  94. rowData.row = this._createRow();
  95. for (var colIndex = 0, colCount = this.option("colCount"); colIndex < colCount; colIndex++) {
  96. this._renderCell(rowData, colIndex)
  97. }
  98. }
  99. },
  100. _createRow: function() {
  101. var row = _dom_adapter2.default.createElement("tr");
  102. this.setAria("role", "row", (0, _renderer2.default)(row));
  103. this.$body.get(0).appendChild(row);
  104. return row
  105. },
  106. _appendCell: function(row, cell) {
  107. if (!this._appendMethodName) {
  108. this._cacheAppendMethodName()
  109. }(0, _renderer2.default)(row)[this._appendMethodName](cell)
  110. },
  111. _cacheAppendMethodName: function(rtlEnabled) {
  112. this._appendMethodName = (null !== rtlEnabled && void 0 !== rtlEnabled ? rtlEnabled : this.option("rtlEnabled")) ? "prepend" : "append"
  113. },
  114. _createCell: function(cellDate) {
  115. var cell = _dom_adapter2.default.createElement("td");
  116. var $cell = (0, _renderer2.default)(cell);
  117. cell.className = this._getClassNameByDate(cellDate);
  118. cell.setAttribute("data-value", (0, _date_serialization.serializeDate)(cellDate, (0, _date.getShortDateFormat)()));
  119. _element_data2.default.data(cell, CALENDAR_DATE_VALUE_KEY, cellDate);
  120. this.setAria({
  121. role: "gridcell",
  122. label: this.getCellAriaLabel(cellDate)
  123. }, $cell);
  124. return {
  125. cell: cell,
  126. $cell: $cell
  127. }
  128. },
  129. _renderCell: function(params, cellIndex) {
  130. var cellDate = params.cellDate,
  131. prevCellDate = params.prevCellDate,
  132. row = params.row;
  133. if (prevCellDate) {
  134. (0, _date.fixTimezoneGap)(prevCellDate, cellDate)
  135. }
  136. params.prevCellDate = cellDate;
  137. var _this$_createCell = this._createCell(cellDate),
  138. cell = _this$_createCell.cell,
  139. $cell = _this$_createCell.$cell;
  140. var cellTemplate = this.option("cellTemplate");
  141. this._appendCell(row, cell);
  142. if (cellTemplate) {
  143. cellTemplate.render(this._prepareCellTemplateData(cellDate, cellIndex, $cell))
  144. } else {
  145. cell.innerHTML = this._getCellText(cellDate)
  146. }
  147. params.cellDate = this._getNextCellData(cellDate)
  148. },
  149. _getClassNameByDate: function(cellDate) {
  150. var className = CALENDAR_CELL_CLASS;
  151. if (this._isTodayCell(cellDate)) {
  152. className += " ".concat(CALENDAR_TODAY_CLASS)
  153. }
  154. if (this._isDateOutOfRange(cellDate) || this.isDateDisabled(cellDate)) {
  155. className += " ".concat(CALENDAR_EMPTY_CELL_CLASS)
  156. }
  157. if (this._isOtherView(cellDate)) {
  158. className += " ".concat(CALENDAR_OTHER_VIEW_CLASS)
  159. }
  160. return className
  161. },
  162. _prepareCellTemplateData: function(cellDate, cellIndex, $cell) {
  163. return {
  164. model: {
  165. text: this._getCellText(cellDate),
  166. date: cellDate,
  167. view: this._getViewName()
  168. },
  169. container: _dom2.default.getPublicElement($cell),
  170. index: cellIndex
  171. }
  172. },
  173. _renderEvents: function() {
  174. var _this = this;
  175. this._createCellClickAction();
  176. _events_engine2.default.off(this._$table, CALENDAR_DXCLICK_EVENT_NAME);
  177. _events_engine2.default.on(this._$table, CALENDAR_DXCLICK_EVENT_NAME, "td", function(e) {
  178. if (!(0, _renderer2.default)(e.currentTarget).hasClass(CALENDAR_EMPTY_CELL_CLASS)) {
  179. _this._cellClickAction({
  180. event: e,
  181. value: (0, _renderer2.default)(e.currentTarget).data(CALENDAR_DATE_VALUE_KEY)
  182. })
  183. }
  184. })
  185. },
  186. _createCellClickAction: function() {
  187. this._cellClickAction = this._createActionByOption("onCellClick")
  188. },
  189. _createDisabledDatesHandler: function() {
  190. var disabledDates = this.option("disabledDates");
  191. this._disabledDatesHandler = Array.isArray(disabledDates) ? this._getDefaultDisabledDatesHandler(disabledDates) : disabledDates || _common.noop
  192. },
  193. _getDefaultDisabledDatesHandler: function(disabledDates) {
  194. return _common.noop
  195. },
  196. _isTodayCell: abstract,
  197. _isDateOutOfRange: abstract,
  198. isDateDisabled: function(cellDate) {
  199. var dateParts = {
  200. date: cellDate,
  201. view: this._getViewName()
  202. };
  203. return this._disabledDatesHandler(dateParts)
  204. },
  205. _isOtherView: abstract,
  206. _getCellText: abstract,
  207. _getFirstCellData: abstract,
  208. _getNextCellData: abstract,
  209. _renderContouredDate: function(contouredDate) {
  210. if (!this.option("focusStateEnabled")) {
  211. return
  212. }
  213. contouredDate = contouredDate || this.option("contouredDate");
  214. var $oldContouredCell = this._$table.find(".".concat(CALENDAR_CONTOURED_DATE_CLASS));
  215. var $newContouredCell = this._getCellByDate(contouredDate);
  216. $oldContouredCell.removeClass(CALENDAR_CONTOURED_DATE_CLASS);
  217. $newContouredCell.addClass(CALENDAR_CONTOURED_DATE_CLASS)
  218. },
  219. _dispose: function() {
  220. this._keyboardProcessor = void 0;
  221. this.callBase()
  222. },
  223. _changeValue: function(cellDate) {
  224. if (cellDate) {
  225. var value = this.option("value");
  226. var newValue = value ? new Date(value) : new Date;
  227. newValue.setDate(cellDate.getDate());
  228. newValue.setMonth(cellDate.getMonth());
  229. newValue.setFullYear(cellDate.getFullYear());
  230. newValue.setDate(cellDate.getDate());
  231. this.option("value", newValue)
  232. } else {
  233. this.option("value", null)
  234. }
  235. },
  236. _renderValue: function() {
  237. if (!this.option("allowValueSelection")) {
  238. return
  239. }
  240. var value = this.option("value");
  241. var selectedCell = this._getCellByDate(value);
  242. if (this._selectedCell) {
  243. this._selectedCell.removeClass(CALENDAR_SELECTED_DATE_CLASS)
  244. }
  245. selectedCell.addClass(CALENDAR_SELECTED_DATE_CLASS);
  246. this._selectedCell = selectedCell
  247. },
  248. getCellAriaLabel: function(date) {
  249. return this._getCellText(date)
  250. },
  251. _getFirstAvailableDate: function() {
  252. var date = this.option("date");
  253. var min = this.option("min");
  254. date = (0, _date.getFirstDateView)(this._getViewName(), date);
  255. return new Date(min && date < min ? min : date)
  256. },
  257. _getCellByDate: abstract,
  258. isBoundary: abstract,
  259. _optionChanged: function(args) {
  260. var name = args.name,
  261. value = args.value;
  262. switch (name) {
  263. case "value":
  264. this._renderValue();
  265. break;
  266. case "contouredDate":
  267. this._renderContouredDate(value);
  268. break;
  269. case "onCellClick":
  270. this._createCellClickAction();
  271. break;
  272. case "disabledDates":
  273. case "cellTemplate":
  274. this._invalidate();
  275. break;
  276. case "rtlEnabled":
  277. this._cacheAppendMethodName(value);
  278. this.callBase(args);
  279. break;
  280. default:
  281. this.callBase(args)
  282. }
  283. }
  284. });
  285. module.exports = BaseView;