Skip to content

Commit

Permalink
[MIG] web_datetime_picker_default_time: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
natuan9 committed Jan 15, 2025
1 parent 56dfa31 commit 39dc87c
Show file tree
Hide file tree
Showing 11 changed files with 428 additions and 86 deletions.
2 changes: 1 addition & 1 deletion eslint.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ const config = [{
},

}, {
files: ["**/*.esm.js"],
files: ["**/*.esm.js", "**/*.test.js"],

languageOptions: {
ecmaVersion: 2024,
Expand Down
42 changes: 30 additions & 12 deletions web_datetime_picker_default_time/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ Web Datetime Picker Default Time
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github
:target: https://github.com/OCA/web/tree/16.0/web_datetime_picker_default_time
:target: https://github.com/OCA/web/tree/18.0/web_datetime_picker_default_time
:alt: OCA/web
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/web-16-0/web-16-0-web_datetime_picker_default_time
:target: https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_datetime_picker_default_time
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=16.0
:target: https://runboat.odoo-community.org/builds?repo=OCA/web&target_branch=18.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|
Expand All @@ -45,14 +45,22 @@ browser.
Usage
=====

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

.. code:: xml
<field name="your_datetime_field" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
<field name="your_datetime_field" widget="datetime" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
Otherwise you can also use a JSON field to make it dynamic through a
compute function, and reference this field in the view:
For ``widget="daterange"``:

.. code:: xml
<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,}}"/>
**Dynamic Default Time** Otherwise you can also use a JSON field to make
it dynamic through a compute function, and reference this field in the
view:

.. code:: python
Expand All @@ -70,15 +78,15 @@ compute function, and reference this field in the view:
Known issues / Roadmap
======================

- Handle Timezone related to the default time
- Handle Timezone related to the default time

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`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**>`_.
`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**>`_.

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

Expand All @@ -93,8 +101,18 @@ Authors
Contributors
------------

- Akim Juillerat [email protected]
- Iván Todorovich [email protected]
- Akim Juillerat [email protected]
- Iván Todorovich [email protected]

- `Trobz <https://trobz.com>`__:

- Tuan Nguyen <[email protected]>

Other credits
-------------

The migration of this module from 16.0 to 18.0 was financially supported
by Camptocamp.

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

|maintainer-grindtildeath|

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

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
6 changes: 4 additions & 2 deletions web_datetime_picker_default_time/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
"name": "Web Datetime Picker Default Time",
"summary": "Allows to define a default time on datetime picker",
"version": "16.0.1.0.0",
"version": "18.0.1.0.0",
"category": "web",
"website": "https://github.com/OCA/web",
"author": "Camptocamp, Odoo Community Association (OCA)",
Expand All @@ -15,7 +15,9 @@
"assets": {
"web.assets_backend": [
"/web_datetime_picker_default_time/static/src/js/*.js",
"/web_datetime_picker_default_time/static/src/xml/*.xml",
],
"web.assets_unit_tests": [
"web_datetime_picker_default_time/static/tests/web_datetime_picker_default_time.test.js",
],
},
}
2 changes: 2 additions & 0 deletions web_datetime_picker_default_time/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* Akim Juillerat <[email protected]>
* Iván Todorovich <[email protected]>
- [Trobz](https://trobz.com):
- Tuan Nguyen \<<[email protected]>\>
1 change: 1 addition & 0 deletions web_datetime_picker_default_time/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The migration of this module from 16.0 to 18.0 was financially supported by Camptocamp.
11 changes: 9 additions & 2 deletions web_datetime_picker_default_time/readme/USAGE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
You can define the default time as follows for a static value:
**Static Default Time**
You can define the default time as follows for a static value
For `widget="datetime"`:
```xml
<field name="your_datetime_field" widget="datetime" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
```

For `widget="daterange"`:
```xml
<field name="your_datetime_field" options="{'defaultTime': {'hour': 8, 'minute': 30, 'second': 15 }}"/>
<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,}}"/>
```

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

Expand Down
34 changes: 25 additions & 9 deletions web_datetime_picker_default_time/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ <h1 class="title">Web Datetime Picker Default Time</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:a5ffb697bdf4c26817212f783a9d4d617e91fcdc912a7750382d3eddaff05f7b
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<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>
<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>
<p>This module customizes the datetime picker widget and allows to define a
default time to be applied in case the user selects only a Date.</p>
<p>For example, if a user wants to define a commitment date without having
Expand All @@ -386,19 +386,26 @@ <h1 class="title">Web Datetime Picker Default Time</h1>
<li><a class="reference internal" href="#credits" id="toc-entry-4">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="toc-entry-5">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="toc-entry-6">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-7">Maintainers</a></li>
<li><a class="reference internal" href="#other-credits" id="toc-entry-7">Other credits</a></li>
<li><a class="reference internal" href="#maintainers" id="toc-entry-8">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#toc-entry-1">Usage</a></h1>
<p>You can define the default time as follows for a static value:</p>
<p><strong>Static Default Time</strong> You can define the default time as follows for a
static value For <tt class="docutils literal"><span class="pre">widget=&quot;datetime&quot;</span></tt>:</p>
<pre class="code xml literal-block">
<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>
<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>
</pre>
<p>Otherwise you can also use a JSON field to make it dynamic through a
compute function, and reference this field in the view:</p>
<p>For <tt class="docutils literal"><span class="pre">widget=&quot;daterange&quot;</span></tt>:</p>
<pre class="code xml literal-block">
<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>
</pre>
<p><strong>Dynamic Default Time</strong> Otherwise you can also use a JSON field to make
it dynamic through a compute function, and reference this field in the
view:</p>
<pre class="code python literal-block">
<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">

Expand All @@ -422,7 +429,7 @@ <h1><a class="toc-backref" href="#toc-entry-3">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/web/issues">GitHub Issues</a>.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
<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>
<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>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand All @@ -438,10 +445,19 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<ul class="simple">
<li>Akim Juillerat <a class="reference external" href="mailto:akim.juillerat&#64;camptocamp.com">akim.juillerat&#64;camptocamp.com</a></li>
<li>Iván Todorovich <a class="reference external" href="mailto:ivan.todorovich&#64;camptocamp.com">ivan.todorovich&#64;camptocamp.com</a></li>
<li><a class="reference external" href="https://trobz.com">Trobz</a>:<ul>
<li>Tuan Nguyen &lt;<a class="reference external" href="mailto:tuanna&#64;trobz.com">tuanna&#64;trobz.com</a>&gt;</li>
</ul>
</li>
</ul>
</div>
<div class="section" id="other-credits">
<h2><a class="toc-backref" href="#toc-entry-7">Other credits</a></h2>
<p>The migration of this module from 16.0 to 18.0 was financially supported
by Camptocamp.</p>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
<h2><a class="toc-backref" href="#toc-entry-8">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org">
<img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" />
Expand All @@ -451,7 +467,7 @@ <h2><a class="toc-backref" href="#toc-entry-7">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<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>
<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>
<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>
<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>
</div>
</div>
Expand Down
109 changes: 68 additions & 41 deletions web_datetime_picker_default_time/static/src/js/datepicker.esm.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,58 @@
/** @odoo-module **/
/* Copyright 2024 Camptocamp
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl) */

import {DateTimePicker} from "@web/core/datepicker/datepicker";
import {DateTimePicker} from "@web/core/datetime/datetime_picker";
import {DateTimePickerPopover} from "@web/core/datetime/datetime_picker_popover";
import {patch} from "@web/core/utils/patch";
import {localization} from "@web/core/l10n/localization";

patch(DateTimePicker.prototype, "DateTimePickerDefaultTime", {
onMounted() {
this._super.apply(this, arguments);
this.addPickerListener("change", ({date, oldDate}) => {
const default_time = this.props.defaultTime;
if (date && !oldDate && default_time) {
// FIXME: Consider TZ
date.set({
hour: default_time.hour,
minute: default_time.minute,
second: default_time.second,
});
window.$(this.rootRef.el).datetimepicker("date", date);
}
});
},
isStrDate(input_string) {
return input_string.trim().length == localization.dateFormat.length;
},
customParseValue(input_value, options) {
const default_time = this.props.defaultTime;
let [res, error] = this.parseValueOriginal(input_value, options);
if (default_time && this.isStrDate(input_value)) {
const new_value = res.set({
hour: default_time.hour,
minute: default_time.minute,
second: default_time.second,
});
res = new_value;
const {DateTime} = luxon;

/**
* @typedef {import("@web/core/datetime/datetime_picker").DateTimePickerProps & {
* defaultTime?: { hour: number, minute: number, second: number },
* defaultStartTime?: { hour: number, minute: number, second: number },
* defaultEndTime?: { hour: number, minute: number, second: number },
* }} DateTimePickerProps
*/

patch(DateTimePicker.prototype, {
/**
* @param {DateTimePickerProps} props
*/
onPropsUpdated(props) {
super.onPropsUpdated(props);

const timeValues = this.values.map((val, index) =>
this.getCustomTimeValues(val, index)
);

if (props.range) {
this.state.timeValues = timeValues;
} else {
this.state.timeValues = [];
this.state.timeValues[props.focusedDateIndex] =
timeValues[props.focusedDateIndex];
}
return [res, error];

this.adjustFocus(this.values, props.focusedDateIndex);
this.handle12HourSystem();
this.state.timeValues = this.state.timeValues.map((timeValue) =>
timeValue.map(String)
);
},
initFormat() {
this._super.apply(this, arguments);
this.parseValueOriginal = this.parseValue;
this.parseValue = this.customParseValue;

getCustomTimeValues(val, index) {
const defaultTime =
this.props.defaultTime || this.props.defaultStartTime || DateTime.local();
const defaultEndTime =
this.props.defaultEndTime || DateTime.local().plus({hour: 1});

const timeSource = index === 1 ? val || defaultEndTime : val || defaultTime;

return [timeSource.hour, timeSource.minute || 0, timeSource.second || 0];
},
});

DateTimePicker.props = _.extend({}, DateTimePicker.props, {
DateTimePicker.props = {
...DateTimePicker.props,
defaultTime: {
type: Object,
shape: {
Expand All @@ -55,4 +62,24 @@ DateTimePicker.props = _.extend({}, DateTimePicker.props, {
},
optional: true,
},
});
defaultStartTime: {
type: Object,
shape: {
hour: Number,
minute: Number,
second: Number,
},
optional: true,
},
defaultEndTime: {
type: Object,
shape: {
hour: Number,
minute: Number,
second: Number,
},
optional: true,
},
};

DateTimePickerPopover.props.pickerProps.shape = DateTimePicker.props;
Loading

0 comments on commit 39dc87c

Please sign in to comment.