From 95f1f412d5719b2c8f4e4600fed2f3630f3772e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20=C3=89vrard?= Date: Fri, 16 Mar 2018 17:13:12 +0100 Subject: [PATCH] Ensure that Date and DateTime widget uses the same keybindings as the GTk client issue7176 review44071002 --- CHANGELOG | 1 + src/common.js | 17 +++++++++++++++++ src/view/form.js | 29 ++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 89e6a45856..564005a73f 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add shortcuts to Date and DateTime widgets * Use limit to expand tree * Use spellcheck and lang attributes for textbox widget * Support icon on page diff --git a/src/common.js b/src/common.js index 072cf76842..15c8407f5e 100644 --- a/src/common.js +++ b/src/common.js @@ -185,6 +185,23 @@ ; }; + Sao.common.DATE_OPERATORS = [ + ['S', moment.duration(-1, 'seconds')], + ['s', moment.duration(1, 'seconds')], + ['I', moment.duration(-1, 'minutes')], + ['i', moment.duration(1, 'minutes')], + ['H', moment.duration(-1, 'hours')], + ['h', moment.duration(1, 'hours')], + ['D', moment.duration(-1, 'days')], + ['d', moment.duration(1, 'days')], + ['W', moment.duration(-1, 'weeks')], + ['w', moment.duration(1, 'weeks')], + ['M', moment.duration(-1, 'months')], + ['m', moment.duration(1, 'months')], + ['Y', moment.duration(-1, 'years')], + ['y', moment.duration(1, 'years')], + ]; + Sao.common.date_format = function(format) { if (jQuery.isEmptyObject(format)) { format = '%Y-%m-%d'; diff --git a/src/view/form.js b/src/view/form.js index 060cfda533..64b1b9dd66 100644 --- a/src/view/form.js +++ b/src/view/form.js @@ -1401,11 +1401,38 @@ function eval_pyson(value){ }).append(jQuery('', { 'class': 'glyphicon glyphicon-calendar' }))).appendTo(this.date); + var noop = function () {}; + var reset_keyBinds = {}; + var default_keyBinds = this.date.datetimepicker.defaults.keyBinds; + for (var key in default_keyBinds) { + if (default_keyBinds.hasOwnProperty(key)) { + reset_keyBinds[key] = noop; + } + } this.date.datetimepicker({ - 'locale': moment.locale() + 'locale': moment.locale(), + 'keyBinds': reset_keyBinds, }); this.date.css('width', this._width); this.date.on('dp.change', this.focus_out.bind(this)); + var mousetrap = new Mousetrap(this.el[0]); + + mousetrap.bind(['enter', '='], function(e, combo) { + if (e.which != Sao.common.RETURN_KEYCODE) { + e.preventDefault(); + } + this.date.data('DateTimePicker').date(moment()); + }.bind(this)); + + Sao.common.DATE_OPERATORS.forEach(function(operator) { + mousetrap.bind(operator[0], function(e, combo) { + e.preventDefault(); + var dp = this.date.data('DateTimePicker'); + var date = dp.date(); + date.add(operator[1]); + dp.date(date); + }.bind(this)); + }.bind(this)); }, get_format: function(record, field) { return field.date_format(record);