dateEditor.js 11 KB

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