ui.time_view.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /**
  2. * DevExtreme (ui/date_box/ui.time_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 _editor = require("../editor/editor");
  13. var _editor2 = _interopRequireDefault(_editor);
  14. var _number_box = require("../number_box");
  15. var _number_box2 = _interopRequireDefault(_number_box);
  16. var _select_box = require("../select_box");
  17. var _select_box2 = _interopRequireDefault(_select_box);
  18. var _box = require("../box");
  19. var _box2 = _interopRequireDefault(_box);
  20. var _extend = require("../../core/utils/extend");
  21. var _component_registrator = require("../../core/component_registrator");
  22. var _component_registrator2 = _interopRequireDefault(_component_registrator);
  23. var _date = require("../../localization/date");
  24. var _ui = require("./ui.date_utils");
  25. function _interopRequireDefault(obj) {
  26. return obj && obj.__esModule ? obj : {
  27. "default": obj
  28. }
  29. }
  30. var TIMEVIEW_CLASS = "dx-timeview";
  31. var TIMEVIEW_CLOCK_CLASS = "dx-timeview-clock";
  32. var TIMEVIEW_FIELD_CLASS = "dx-timeview-field";
  33. var TIMEVIEW_HOURARROW_CLASS = "dx-timeview-hourarrow";
  34. var TIMEVIEW_TIME_SEPARATOR_CLASS = "dx-timeview-time-separator";
  35. var TIMEVIEW_FORMAT12_CLASS = "dx-timeview-format12";
  36. var TIMEVIEW_FORMAT12_AM = -1;
  37. var TIMEVIEW_FORMAT12_PM = 1;
  38. var TIMEVIEW_MINUTEARROW_CLASS = "dx-timeview-minutearrow";
  39. var rotateArrow = function($arrow, angle, offset) {
  40. cssRotate($arrow, angle, offset)
  41. };
  42. var cssRotate = function($arrow, angle, offset) {
  43. $arrow.css("transform", "rotate(" + angle + "deg) translate(0," + offset + "px)")
  44. };
  45. var TimeView = _editor2.default.inherit({
  46. _getDefaultOptions: function() {
  47. return (0, _extend.extend)(this.callBase(), {
  48. value: new Date(Date.now()),
  49. use24HourFormat: true,
  50. _showClock: true,
  51. _arrowOffset: 0,
  52. stylingMode: void 0
  53. })
  54. },
  55. _defaultOptionsRules: function() {
  56. return this.callBase().concat([{
  57. device: {
  58. platform: "android"
  59. },
  60. options: {
  61. _arrowOffset: 15
  62. }
  63. }, {
  64. device: {
  65. platform: "generic"
  66. },
  67. options: {
  68. _arrowOffset: 5
  69. }
  70. }])
  71. },
  72. _getValue: function() {
  73. return this.option("value") || new Date
  74. },
  75. _init: function() {
  76. this.callBase();
  77. this.$element().addClass(TIMEVIEW_CLASS)
  78. },
  79. _render: function() {
  80. this.callBase();
  81. this._renderBox();
  82. this._updateTime()
  83. },
  84. _renderBox: function() {
  85. var $box = (0, _renderer2.default)("<div>").appendTo(this.$element());
  86. var items = [];
  87. if (this.option("_showClock")) {
  88. items.push({
  89. ratio: 1,
  90. shrink: 0,
  91. baseSize: "auto",
  92. template: this._renderClock.bind(this)
  93. })
  94. }
  95. items.push({
  96. ratio: 0,
  97. shrink: 0,
  98. baseSize: 50,
  99. template: this._renderField.bind(this)
  100. });
  101. this._createComponent($box, _box2.default, {
  102. height: "100%",
  103. width: "100%",
  104. direction: "col",
  105. items: items
  106. })
  107. },
  108. _renderClock: function(_, __, container) {
  109. this._$hourArrow = (0, _renderer2.default)("<div>").addClass(TIMEVIEW_HOURARROW_CLASS);
  110. this._$minuteArrow = (0, _renderer2.default)("<div>").addClass(TIMEVIEW_MINUTEARROW_CLASS);
  111. var $container = (0, _renderer2.default)(container);
  112. $container.addClass(TIMEVIEW_CLOCK_CLASS).append(this._$hourArrow).append(this._$minuteArrow);
  113. this.setAria("role", "presentation", $container)
  114. },
  115. _updateClock: function() {
  116. var time = this._getValue();
  117. var hourArrowAngle = time.getHours() / 12 * 360 + time.getMinutes() / 60 * 30;
  118. var minuteArrowAngle = time.getMinutes() / 60 * 360;
  119. rotateArrow(this._$hourArrow, hourArrowAngle, this.option("_arrowOffset"));
  120. rotateArrow(this._$minuteArrow, minuteArrowAngle, this.option("_arrowOffset"))
  121. },
  122. _getBoxItems: function(is12HourFormat) {
  123. var _this = this;
  124. var items = [{
  125. ratio: 0,
  126. shrink: 0,
  127. baseSize: "auto",
  128. template: function() {
  129. return _this._hourBox.$element()
  130. }
  131. }, {
  132. ratio: 0,
  133. shrink: 0,
  134. baseSize: "auto",
  135. template: (0, _renderer2.default)("<div>").addClass(TIMEVIEW_TIME_SEPARATOR_CLASS).text((0, _date.getTimeSeparator)())
  136. }, {
  137. ratio: 0,
  138. shrink: 0,
  139. baseSize: "auto",
  140. template: function() {
  141. return _this._minuteBox.$element()
  142. }
  143. }];
  144. if (is12HourFormat) {
  145. items.push({
  146. ratio: 0,
  147. shrink: 0,
  148. baseSize: "auto",
  149. template: function() {
  150. return _this._format12.$element()
  151. }
  152. })
  153. }
  154. return items
  155. },
  156. _renderField: function() {
  157. var is12HourFormat = !this.option("use24HourFormat");
  158. this._createHourBox();
  159. this._createMinuteBox();
  160. if (is12HourFormat) {
  161. this._createFormat12Box()
  162. }
  163. return this._createComponent((0, _renderer2.default)("<div>").addClass(TIMEVIEW_FIELD_CLASS), _box2.default, {
  164. direction: "row",
  165. align: "center",
  166. crossAlign: "center",
  167. items: this._getBoxItems(is12HourFormat)
  168. }).$element()
  169. },
  170. _attachKeyboardProcessorToEditor: function(editor) {
  171. var keyboardProcessor = editor._keyboardProcessor;
  172. if (keyboardProcessor) {
  173. keyboardProcessor.attachChildProcessor().reinitialize(this._keyboardHandler, this)
  174. }
  175. },
  176. _createHourBox: function() {
  177. var editor = this._hourBox = this._createComponent((0, _renderer2.default)("<div>"), _number_box2.default, (0, _extend.extend)({
  178. min: -1,
  179. max: 24,
  180. value: this._getValue().getHours(),
  181. onValueChanged: this._onHourBoxValueChanged.bind(this)
  182. }, this._getNumberBoxConfig()));
  183. editor.setAria("label", "hours");
  184. this._attachKeyboardProcessorToEditor(editor)
  185. },
  186. _isPM: function() {
  187. return !this.option("use24HourFormat") && 1 === this._format12.option("value")
  188. },
  189. _onHourBoxValueChanged: function(args) {
  190. var currentValue = this._getValue();
  191. var newValue = new Date(currentValue);
  192. var newHours = this._convertMaxHourToMin(args.value);
  193. if (this._isPM()) {
  194. newHours += 12
  195. }
  196. newValue.setHours(newHours);
  197. (0, _ui.normalizeTime)(newValue);
  198. this.option("value", newValue)
  199. },
  200. _convertMaxHourToMin: function(hours) {
  201. var maxHoursValue = this.option("use24HourFormat") ? 24 : 12;
  202. return (maxHoursValue + hours) % maxHoursValue
  203. },
  204. _createMinuteBox: function() {
  205. var _this2 = this;
  206. var editor = this._minuteBox = this._createComponent((0, _renderer2.default)("<div>"), _number_box2.default, (0, _extend.extend)({
  207. min: -1,
  208. max: 60,
  209. value: this._getValue().getMinutes(),
  210. onValueChanged: function(_ref) {
  211. var value = _ref.value,
  212. component = _ref.component;
  213. var newMinutes = (60 + value) % 60;
  214. component.option("value", newMinutes);
  215. var time = new Date(_this2._getValue());
  216. time.setMinutes(newMinutes);
  217. (0, _ui.normalizeTime)(time);
  218. _this2.option("value", time)
  219. }
  220. }, this._getNumberBoxConfig()));
  221. editor.setAria("label", "minutes");
  222. this._attachKeyboardProcessorToEditor(editor)
  223. },
  224. _createFormat12Box: function() {
  225. var _this3 = this;
  226. var periodNames = (0, _date.getPeriodNames)();
  227. var editor = this._format12 = this._createComponent((0, _renderer2.default)("<div>").addClass(TIMEVIEW_FORMAT12_CLASS), _select_box2.default, {
  228. items: [{
  229. value: TIMEVIEW_FORMAT12_AM,
  230. text: periodNames[0]
  231. }, {
  232. value: TIMEVIEW_FORMAT12_PM,
  233. text: periodNames[1]
  234. }],
  235. valueExpr: "value",
  236. displayExpr: "text",
  237. onValueChanged: function(_ref2) {
  238. var value = _ref2.value;
  239. var hours = _this3._getValue().getHours();
  240. var time = new Date(_this3._getValue());
  241. var newHours = (hours + 12 * value) % 24;
  242. time.setHours(newHours);
  243. _this3.option("value", time)
  244. },
  245. value: this._getValue().getHours() >= 12 ? TIMEVIEW_FORMAT12_PM : TIMEVIEW_FORMAT12_AM,
  246. stylingMode: this.option("stylingMode")
  247. });
  248. this._attachKeyboardProcessorToEditor(editor);
  249. editor.setAria("label", "type")
  250. },
  251. _refreshFormat12: function() {
  252. if (this.option("use24HourFormat")) {
  253. return
  254. }
  255. var value = this._getValue();
  256. var hours = value.getHours();
  257. var isPM = hours >= 12;
  258. var newValue = isPM ? TIMEVIEW_FORMAT12_PM : TIMEVIEW_FORMAT12_AM;
  259. this._silentEditorValueUpdate(this._format12, newValue)
  260. },
  261. _silentEditorValueUpdate: function(editor, value) {
  262. if (editor) {
  263. editor._suppressValueChangeAction();
  264. editor.option("value", value);
  265. editor._resumeValueChangeAction()
  266. }
  267. },
  268. _getNumberBoxConfig: function() {
  269. return {
  270. showSpinButtons: true,
  271. valueFormat: function(value) {
  272. return (value < 10 ? "0" : "") + value
  273. },
  274. stylingMode: this.option("stylingMode")
  275. }
  276. },
  277. _normalizeHours: function(hours) {
  278. return this.option("use24HourFormat") ? hours : hours % 12 || 12
  279. },
  280. _updateField: function() {
  281. var hours = this._normalizeHours(this._getValue().getHours());
  282. this._silentEditorValueUpdate(this._hourBox, hours);
  283. this._silentEditorValueUpdate(this._minuteBox, this._getValue().getMinutes());
  284. this._refreshFormat12()
  285. },
  286. _updateTime: function() {
  287. if (this.option("_showClock")) {
  288. this._updateClock()
  289. }
  290. this._updateField()
  291. },
  292. _visibilityChanged: function(visible) {
  293. if (visible) {
  294. this._updateTime()
  295. }
  296. },
  297. _optionChanged: function(args) {
  298. switch (args.name) {
  299. case "value":
  300. this._updateTime();
  301. this.callBase(args);
  302. break;
  303. case "_arrowOffset":
  304. break;
  305. case "use24HourFormat":
  306. case "_showClock":
  307. case "stylingMode":
  308. this._invalidate();
  309. break;
  310. default:
  311. this.callBase(args)
  312. }
  313. }
  314. });
  315. (0, _component_registrator2.default)("dxTimeView", TimeView);
  316. module.exports = TimeView;