dateEditor.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
  2. var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } };
  3. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  4. function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
  5. function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
  6. import moment from 'moment';
  7. import Pikaday from 'pikaday';
  8. import { addClass, outerHeight } from './../helpers/dom/element';
  9. import { deepExtend } from './../helpers/object';
  10. import EventManager from './../eventManager';
  11. import { isMetaKey } from './../helpers/unicode';
  12. import { stopPropagation } from './../helpers/dom/event';
  13. import TextEditor from './textEditor';
  14. /**
  15. * @private
  16. * @editor DateEditor
  17. * @class DateEditor
  18. * @dependencies TextEditor moment pikaday
  19. */
  20. var DateEditor = function (_TextEditor) {
  21. _inherits(DateEditor, _TextEditor);
  22. /**
  23. * @param {Core} hotInstance Handsontable instance
  24. * @private
  25. */
  26. function DateEditor(hotInstance) {
  27. _classCallCheck(this, DateEditor);
  28. // TODO: Move this option to general settings
  29. var _this = _possibleConstructorReturn(this, (DateEditor.__proto__ || Object.getPrototypeOf(DateEditor)).call(this, hotInstance));
  30. _this.defaultDateFormat = 'DD/MM/YYYY';
  31. _this.isCellEdited = false;
  32. _this.parentDestroyed = false;
  33. return _this;
  34. }
  35. _createClass(DateEditor, [{
  36. key: 'init',
  37. value: function init() {
  38. var _this2 = this;
  39. if (typeof moment !== 'function') {
  40. throw new Error('You need to include moment.js to your project.');
  41. }
  42. if (typeof Pikaday !== 'function') {
  43. throw new Error('You need to include Pikaday to your project.');
  44. }
  45. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'init', this).call(this);
  46. this.instance.addHook('afterDestroy', function () {
  47. _this2.parentDestroyed = true;
  48. _this2.destroyElements();
  49. });
  50. }
  51. /**
  52. * Create data picker instance
  53. */
  54. }, {
  55. key: 'createElements',
  56. value: function createElements() {
  57. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'createElements', this).call(this);
  58. this.datePicker = document.createElement('DIV');
  59. this.datePickerStyle = this.datePicker.style;
  60. this.datePickerStyle.position = 'absolute';
  61. this.datePickerStyle.top = 0;
  62. this.datePickerStyle.left = 0;
  63. this.datePickerStyle.zIndex = 9999;
  64. addClass(this.datePicker, 'htDatepickerHolder');
  65. document.body.appendChild(this.datePicker);
  66. this.$datePicker = new Pikaday(this.getDatePickerConfig());
  67. var eventManager = new EventManager(this);
  68. /**
  69. * Prevent recognizing clicking on datepicker as clicking outside of table
  70. */
  71. eventManager.addEventListener(this.datePicker, 'mousedown', function (event) {
  72. return stopPropagation(event);
  73. });
  74. this.hideDatepicker();
  75. }
  76. /**
  77. * Destroy data picker instance
  78. */
  79. }, {
  80. key: 'destroyElements',
  81. value: function destroyElements() {
  82. this.$datePicker.destroy();
  83. }
  84. /**
  85. * Prepare editor to appear
  86. *
  87. * @param {Number} row Row index
  88. * @param {Number} col Column index
  89. * @param {String} prop Property name (passed when datasource is an array of objects)
  90. * @param {HTMLTableCellElement} td Table cell element
  91. * @param {*} originalValue Original value
  92. * @param {Object} cellProperties Object with cell properties ({@see Core#getCellMeta})
  93. */
  94. }, {
  95. key: 'prepare',
  96. value: function prepare(row, col, prop, td, originalValue, cellProperties) {
  97. this._opened = false;
  98. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'prepare', this).call(this, row, col, prop, td, originalValue, cellProperties);
  99. }
  100. /**
  101. * Open editor
  102. *
  103. * @param {Event} [event=null]
  104. */
  105. }, {
  106. key: 'open',
  107. value: function open() {
  108. var event = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
  109. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'open', this).call(this);
  110. this.showDatepicker(event);
  111. }
  112. /**
  113. * Close editor
  114. */
  115. }, {
  116. key: 'close',
  117. value: function close() {
  118. var _this3 = this;
  119. this._opened = false;
  120. this.instance._registerTimeout(setTimeout(function () {
  121. _this3.instance.selection.refreshBorders();
  122. }, 0));
  123. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'close', this).call(this);
  124. }
  125. /**
  126. * @param {Boolean} [isCancelled=false]
  127. * @param {Boolean} [ctrlDown=false]
  128. */
  129. }, {
  130. key: 'finishEditing',
  131. value: function finishEditing() {
  132. var isCancelled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  133. var ctrlDown = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
  134. if (isCancelled) {
  135. // pressed ESC, restore original value
  136. // var value = this.instance.getDataAtCell(this.row, this.col);
  137. var value = this.originalValue;
  138. if (value !== void 0) {
  139. this.setValue(value);
  140. }
  141. }
  142. this.hideDatepicker();
  143. _get(DateEditor.prototype.__proto__ || Object.getPrototypeOf(DateEditor.prototype), 'finishEditing', this).call(this, isCancelled, ctrlDown);
  144. }
  145. /**
  146. * Show data picker
  147. *
  148. * @param {Event} event
  149. */
  150. }, {
  151. key: 'showDatepicker',
  152. value: function showDatepicker(event) {
  153. this.$datePicker.config(this.getDatePickerConfig());
  154. var offset = this.TD.getBoundingClientRect();
  155. var dateFormat = this.cellProperties.dateFormat || this.defaultDateFormat;
  156. var datePickerConfig = this.$datePicker.config();
  157. var dateStr = void 0;
  158. var isMouseDown = this.instance.view.isMouseDown();
  159. var isMeta = event ? isMetaKey(event.keyCode) : false;
  160. this.datePickerStyle.top = window.pageYOffset + offset.top + outerHeight(this.TD) + 'px';
  161. this.datePickerStyle.left = window.pageXOffset + offset.left + 'px';
  162. this.$datePicker._onInputFocus = function () {};
  163. datePickerConfig.format = dateFormat;
  164. if (this.originalValue) {
  165. dateStr = this.originalValue;
  166. if (moment(dateStr, dateFormat, true).isValid()) {
  167. this.$datePicker.setMoment(moment(dateStr, dateFormat), true);
  168. }
  169. // workaround for date/time cells - pikaday resets the cell value to 12:00 AM by default, this will overwrite the value.
  170. if (this.getValue() !== this.originalValue) {
  171. this.setValue(this.originalValue);
  172. }
  173. if (!isMeta && !isMouseDown) {
  174. this.setValue('');
  175. }
  176. } else if (this.cellProperties.defaultDate) {
  177. dateStr = this.cellProperties.defaultDate;
  178. datePickerConfig.defaultDate = dateStr;
  179. if (moment(dateStr, dateFormat, true).isValid()) {
  180. this.$datePicker.setMoment(moment(dateStr, dateFormat), true);
  181. }
  182. if (!isMeta && !isMouseDown) {
  183. this.setValue('');
  184. }
  185. } else {
  186. // if a default date is not defined, set a soft-default-date: display the current day and month in the
  187. // datepicker, but don't fill the editor input
  188. this.$datePicker.gotoToday();
  189. }
  190. this.datePickerStyle.display = 'block';
  191. this.$datePicker.show();
  192. }
  193. /**
  194. * Hide data picker
  195. */
  196. }, {
  197. key: 'hideDatepicker',
  198. value: function hideDatepicker() {
  199. this.datePickerStyle.display = 'none';
  200. this.$datePicker.hide();
  201. }
  202. /**
  203. * Get date picker options.
  204. *
  205. * @returns {Object}
  206. */
  207. }, {
  208. key: 'getDatePickerConfig',
  209. value: function getDatePickerConfig() {
  210. var _this4 = this;
  211. var htInput = this.TEXTAREA;
  212. var options = {};
  213. if (this.cellProperties && this.cellProperties.datePickerConfig) {
  214. deepExtend(options, this.cellProperties.datePickerConfig);
  215. }
  216. var origOnSelect = options.onSelect;
  217. var origOnClose = options.onClose;
  218. options.field = htInput;
  219. options.trigger = htInput;
  220. options.container = this.datePicker;
  221. options.bound = false;
  222. options.format = options.format || this.defaultDateFormat;
  223. options.reposition = options.reposition || false;
  224. options.onSelect = function (dateStr) {
  225. if (!isNaN(dateStr.getTime())) {
  226. dateStr = moment(dateStr).format(_this4.cellProperties.dateFormat || _this4.defaultDateFormat);
  227. }
  228. _this4.setValue(dateStr);
  229. _this4.hideDatepicker();
  230. if (origOnSelect) {
  231. origOnSelect();
  232. }
  233. };
  234. options.onClose = function () {
  235. if (!_this4.parentDestroyed) {
  236. _this4.finishEditing(false);
  237. }
  238. if (origOnClose) {
  239. origOnClose();
  240. }
  241. };
  242. return options;
  243. }
  244. }]);
  245. return DateEditor;
  246. }(TextEditor);
  247. export default DateEditor;