Skip to content

Commit 95bd7a9

Browse files
committed
[MIG] web_datetime_picker_default_time: Migration to 18.0
1 parent 56dfa31 commit 95bd7a9

14 files changed

+817
-86
lines changed

eslint.config.cjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ const config = [{
191191
},
192192

193193
}, {
194-
files: ["**/*.esm.js"],
194+
files: ["**/*.esm.js", "**/*.test.js"],
195195

196196
languageOptions: {
197197
ecmaVersion: 2024,

web_datetime_picker_default_time/README.rst

+30-12
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ Web Datetime Picker Default Time
1717
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
1818
:alt: License: AGPL-3
1919
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
20-
:target: https://github.com/OCA/web/tree/16.0/web_datetime_picker_default_time
20+
:target: https://github.com/OCA/web/tree/18.0/web_datetime_picker_default_time
2121
:alt: OCA/web
2222
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23-
:target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_datetime_picker_default_time
23+
:target: https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_datetime_picker_default_time
2424
:alt: Translate me on Weblate
2525
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26-
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=16.0
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=18.0
2727
:alt: Try me on Runboat
2828

2929
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -45,14 +45,22 @@ browser.
4545
Usage
4646
=====
4747

48-
You can define the default time as follows for a static value:
48+
**Static Default Time** You can define the default time as follows for a
49+
static value For ``widget="datetime"``:
4950

5051
.. code:: xml
5152
52-
<field name="your_datetime_field" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
53+
<field name="your_datetime_field" widget="datetime" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
5354
54-
Otherwise you can also use a JSON field to make it dynamic through a
55-
compute function, and reference this field in the view:
55+
For ``widget="daterange"``:
56+
57+
.. code:: xml
58+
59+
<field name="your_start_datetime_field" widget="datetime" options="{'end_date_field': 'your_end_datetime_field', 'defaultStartTime': {'hour': 2, 'minute': 22, 'second': 22,}, 'defaultEndTime': {'hour': 3, 'minute': 33, 'second': 33,}}"/>
60+
61+
**Dynamic Default Time** Otherwise you can also use a JSON field to make
62+
it dynamic through a compute function, and reference this field in the
63+
view:
5664

5765
.. code:: python
5866
@@ -70,15 +78,15 @@ compute function, and reference this field in the view:
7078
Known issues / Roadmap
7179
======================
7280

73-
- Handle Timezone related to the default time
81+
- Handle Timezone related to the default time
7482

7583
Bug Tracker
7684
===========
7785

7886
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
7987
In case of trouble, please check there if your issue has already been reported.
8088
If you spotted it first, help us to smash it by providing a detailed and welcomed
81-
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_datetime_picker_default_time%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
89+
`feedback <https://github.com/OCA/web/issues/new?body=module:%20web_datetime_picker_default_time%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
8290

8391
Do not contact contributors directly about support or help with technical issues.
8492

@@ -93,8 +101,18 @@ Authors
93101
Contributors
94102
------------
95103

96-
- Akim Juillerat [email protected]
97-
- Iván Todorovich [email protected]
104+
- Akim Juillerat [email protected]
105+
- Iván Todorovich [email protected]
106+
107+
- `Trobz <https://trobz.com>`__:
108+
109+
- Tuan Nguyen <[email protected]>
110+
111+
Other credits
112+
-------------
113+
114+
The migration of this module from 16.0 to 18.0 was financially supported
115+
by Camptocamp.
98116

99117
Maintainers
100118
-----------
@@ -117,6 +135,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:
117135

118136
|maintainer-grindtildeath|
119137

120-
This module is part of the `OCA/web <https://github.com/OCA/web/tree/16.0/web_datetime_picker_default_time>`_ project on GitHub.
138+
This module is part of the `OCA/web <https://github.com/OCA/web/tree/18.0/web_datetime_picker_default_time>`_ project on GitHub.
121139

122140
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

web_datetime_picker_default_time/__manifest__.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{
44
"name": "Web Datetime Picker Default Time",
55
"summary": "Allows to define a default time on datetime picker",
6-
"version": "16.0.1.0.0",
6+
"version": "18.0.1.0.0",
77
"category": "web",
88
"website": "https://github.com/OCA/web",
99
"author": "Camptocamp, Odoo Community Association (OCA)",
@@ -15,7 +15,9 @@
1515
"assets": {
1616
"web.assets_backend": [
1717
"/web_datetime_picker_default_time/static/src/js/*.js",
18-
"/web_datetime_picker_default_time/static/src/xml/*.xml",
18+
],
19+
"web.assets_unit_tests": [
20+
"web_datetime_picker_default_time/static/tests/web_datetime_picker_default_time.test.js",
1921
],
2022
},
2123
}
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
* Akim Juillerat <[email protected]>
22
* Iván Todorovich <[email protected]>
3+
- [Trobz](https://trobz.com):
4+
- Tuan Nguyen \<<[email protected]>\>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
The migration of this module from 16.0 to 18.0 was financially supported by Camptocamp.

web_datetime_picker_default_time/readme/USAGE.md

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
You can define the default time as follows for a static value:
1+
**Static Default Time**
2+
You can define the default time as follows for a static value
3+
For `widget="datetime"`:
4+
```xml
5+
<field name="your_datetime_field" widget="datetime" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
6+
```
27

8+
For `widget="daterange"`:
39
```xml
4-
<field name="your_datetime_field" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
10+
<field name="your_start_datetime_field" widget="datetime" options="{'end_date_field': 'your_end_datetime_field', 'defaultStartTime': {'hour': 2, 'minute': 22, 'second': 22,}, 'defaultEndTime': {'hour': 3, 'minute': 33, 'second': 33,}}"/>
511
```
612

13+
**Dynamic Default Time**
714
Otherwise you can also use a JSON field to make it dynamic through a compute function,
815
and reference this field in the view:
916

web_datetime_picker_default_time/static/description/index.html

+25-9
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ <h1 class="title">Web Datetime Picker Default Time</h1>
369369
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
370370
!! source digest: sha256:a5ffb697bdf4c26817212f783a9d4d617e91fcdc912a7750382d3eddaff05f7b
371371
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
372-
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web/tree/16.0/web_datetime_picker_default_time"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_datetime_picker_default_time"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
372+
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web/tree/18.0/web_datetime_picker_default_time"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_datetime_picker_default_time"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
373373
<p>This module customizes the datetime picker widget and allows to define a
374374
default time to be applied in case the user selects only a Date.</p>
375375
<p>For example, if a user wants to define a commitment date without having
@@ -386,19 +386,26 @@ <h1 class="title">Web Datetime Picker Default Time</h1>
386386
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
387387
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
388388
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
389-
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
389+
<li><a class="reference internal" href="#other-credits" id="toc-entry-7">Other credits</a></li>
390+
<li><a class="reference internal" href="#maintainers" id="toc-entry-8">Maintainers</a></li>
390391
</ul>
391392
</li>
392393
</ul>
393394
</div>
394395
<div class="section" id="usage">
395396
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
396-
<p>You can define the default time as follows for a static value:</p>
397+
<p><strong>Static Default Time</strong> You can define the default time as follows for a
398+
static value For <tt class="docutils literal"><span class="pre">widget=&quot;datetime&quot;</span></tt>:</p>
397399
<pre class="code xml literal-block">
398-
<span class="nt">&lt;field</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;your_datetime_field&quot;</span><span class="w"> </span><span class="na">options=</span><span class="s">&quot;{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}&quot;</span><span class="nt">/&gt;</span>
400+
<span class="nt">&lt;field</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;your_datetime_field&quot;</span><span class="w"> </span><span class="na">widget=</span><span class="s">&quot;datetime&quot;</span><span class="w"> </span><span class="na">options=</span><span class="s">&quot;{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}&quot;</span><span class="nt">/&gt;</span>
399401
</pre>
400-
<p>Otherwise you can also use a JSON field to make it dynamic through a
401-
compute function, and reference this field in the view:</p>
402+
<p>For <tt class="docutils literal"><span class="pre">widget=&quot;daterange&quot;</span></tt>:</p>
403+
<pre class="code xml literal-block">
404+
<span class="nt">&lt;field</span><span class="w"> </span><span class="na">name=</span><span class="s">&quot;your_start_datetime_field&quot;</span><span class="w"> </span><span class="na">widget=</span><span class="s">&quot;datetime&quot;</span><span class="w"> </span><span class="na">options=</span><span class="s">&quot;{'end_date_field': 'your_end_datetime_field', 'defaultStartTime': {'hour': 2, 'minute': 22, 'second': 22,}, 'defaultEndTime': {'hour': 3, 'minute': 33, 'second': 33,}}&quot;</span><span class="nt">/&gt;</span>
405+
</pre>
406+
<p><strong>Dynamic Default Time</strong> Otherwise you can also use a JSON field to make
407+
it dynamic through a compute function, and reference this field in the
408+
view:</p>
402409
<pre class="code python literal-block">
403410
<span class="n">start_time</span> <span class="o">=</span> <span class="n">field</span><span class="o">.</span><span class="n">Json</span><span class="p">(</span><span class="n">compute</span><span class="o">=</span><span class="s2">&quot;_compute_start_time&quot;</span><span class="p">)</span><span class="w">
404411

@@ -422,7 +429,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
422429
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/web/issues">GitHub Issues</a>.
423430
In case of trouble, please check there if your issue has already been reported.
424431
If you spotted it first, help us to smash it by providing a detailed and welcomed
425-
<a class="reference external" href="https://github.com/OCA/web/issues/new?body=module:%20web_datetime_picker_default_time%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
432+
<a class="reference external" href="https://github.com/OCA/web/issues/new?body=module:%20web_datetime_picker_default_time%0Aversion:%2018.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
426433
<p>Do not contact contributors directly about support or help with technical issues.</p>
427434
</div>
428435
<div class="section" id="credits">
@@ -438,10 +445,19 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
438445
<ul class="simple">
439446
<li>Akim Juillerat <a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a></li>
440447
<li>Iván Todorovich <a class="reference external" href="mailto:ivan.todorovich&#64;camptocamp.com">ivan.todorovich&#64;camptocamp.com</a></li>
448+
<li><a class="reference external" href="https://trobz.com">Trobz</a>:<ul>
449+
<li>Tuan Nguyen &lt;<a class="reference external" href="mailto:tuanna&#64;trobz.com">tuanna&#64;trobz.com</a>&gt;</li>
441450
</ul>
451+
</li>
452+
</ul>
453+
</div>
454+
<div class="section" id="other-credits">
455+
<h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
456+
<p>The migration of this module from 16.0 to 18.0 was financially supported
457+
by Camptocamp.</p>
442458
</div>
443459
<div class="section" id="maintainers">
444-
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
460+
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
445461
<p>This module is maintained by the OCA.</p>
446462
<a class="reference external image-reference" href="https://odoo-community.org">
447463
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
@@ -451,7 +467,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
451467
promote its widespread use.</p>
452468
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
453469
<p><a class="reference external image-reference" href="https://github.com/grindtildeath"><img alt="grindtildeath" src="https://github.com/grindtildeath.png?size=40px" /></a></p>
454-
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web/tree/16.0/web_datetime_picker_default_time">OCA/web</a> project on GitHub.</p>
470+
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/web/tree/18.0/web_datetime_picker_default_time">OCA/web</a> project on GitHub.</p>
455471
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
456472
</div>
457473
</div>
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,58 @@
1-
/** @odoo-module **/
21
/* Copyright 2024 Camptocamp
32
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) */
4-
5-
import {DateTimePicker} from "@web/core/datepicker/datepicker";
3+
import {DateTimePicker} from "@web/core/datetime/datetime_picker";
4+
import {DateTimePickerPopover} from "@web/core/datetime/datetime_picker_popover";
65
import {patch} from "@web/core/utils/patch";
7-
import {localization} from "@web/core/l10n/localization";
8-
9-
patch(DateTimePicker.prototype, "DateTimePickerDefaultTime", {
10-
onMounted() {
11-
this._super.apply(this, arguments);
12-
this.addPickerListener("change", ({date, oldDate}) => {
13-
const default_time = this.props.defaultTime;
14-
if (date && !oldDate && default_time) {
15-
// FIXME: Consider TZ
16-
date.set({
17-
hour: default_time.hour,
18-
minute: default_time.minute,
19-
second: default_time.second,
20-
});
21-
window.$(this.rootRef.el).datetimepicker("date", date);
22-
}
23-
});
24-
},
25-
isStrDate(input_string) {
26-
return input_string.trim().length == localization.dateFormat.length;
27-
},
28-
customParseValue(input_value, options) {
29-
const default_time = this.props.defaultTime;
30-
let [res, error] = this.parseValueOriginal(input_value, options);
31-
if (default_time && this.isStrDate(input_value)) {
32-
const new_value = res.set({
33-
hour: default_time.hour,
34-
minute: default_time.minute,
35-
second: default_time.second,
36-
});
37-
res = new_value;
6+
const {DateTime} = luxon;
7+
8+
/**
9+
* @typedef {import("@web/core/datetime/datetime_picker").DateTimePickerProps & {
10+
* defaultTime?: { hour: number, minute: number, second: number },
11+
* defaultStartTime?: { hour: number, minute: number, second: number },
12+
* defaultEndTime?: { hour: number, minute: number, second: number },
13+
* }} DateTimePickerProps
14+
*/
15+
16+
patch(DateTimePicker.prototype, {
17+
/**
18+
* @param {DateTimePickerProps} props
19+
*/
20+
onPropsUpdated(props) {
21+
super.onPropsUpdated(props);
22+
23+
const timeValues = this.values.map((val, index) =>
24+
this.getCustomTimeValues(val, index)
25+
);
26+
27+
if (props.range) {
28+
this.state.timeValues = timeValues;
29+
} else {
30+
this.state.timeValues = [];
31+
this.state.timeValues[props.focusedDateIndex] =
32+
timeValues[props.focusedDateIndex];
3833
}
39-
return [res, error];
34+
35+
this.adjustFocus(this.values, props.focusedDateIndex);
36+
this.handle12HourSystem();
37+
this.state.timeValues = this.state.timeValues.map((timeValue) =>
38+
timeValue.map(String)
39+
);
4040
},
41-
initFormat() {
42-
this._super.apply(this, arguments);
43-
this.parseValueOriginal = this.parseValue;
44-
this.parseValue = this.customParseValue;
41+
42+
getCustomTimeValues(val, index) {
43+
const defaultTime =
44+
this.props.defaultTime || this.props.defaultStartTime || DateTime.local();
45+
const defaultEndTime =
46+
this.props.defaultEndTime || DateTime.local().plus({hour: 1});
47+
48+
const timeSource = index === 1 ? val || defaultEndTime : val || defaultTime;
49+
50+
return [timeSource.hour, timeSource.minute || 0, timeSource.second || 0];
4551
},
4652
});
4753

48-
DateTimePicker.props = _.extend({}, DateTimePicker.props, {
54+
DateTimePicker.props = {
55+
...DateTimePicker.props,
4956
defaultTime: {
5057
type: Object,
5158
shape: {
@@ -55,4 +62,24 @@ DateTimePicker.props = _.extend({}, DateTimePicker.props, {
5562
},
5663
optional: true,
5764
},
58-
});
65+
defaultStartTime: {
66+
type: Object,
67+
shape: {
68+
hour: Number,
69+
minute: Number,
70+
second: Number,
71+
},
72+
optional: true,
73+
},
74+
defaultEndTime: {
75+
type: Object,
76+
shape: {
77+
hour: Number,
78+
minute: Number,
79+
second: Number,
80+
},
81+
optional: true,
82+
},
83+
};
84+
85+
DateTimePickerPopover.props.pickerProps.shape = DateTimePicker.props;

0 commit comments

Comments
 (0)