Skip to content

Commit

Permalink
[FIX] web: calendar first day of week from res.lang domain
Browse files Browse the repository at this point in the history
The feature that allows res.lang's first day of week parameter
to be the reference point of web calendar
has been introduced at 27b30da
A few other came on top of it to fix a few things

Before this current commit though, only Fullcalendar was
rendered using that initial value, nothing was done
to actually fetch the right data

After this current commit, we fetch data from the server
according to the range displayed by the calendar

OPW 2072872

closes odoo#37202

Signed-off-by: Lucas Perais (lpe) <[email protected]>
  • Loading branch information
kebeclibre committed Oct 10, 2019
1 parent ba7b523 commit 0bd82ef
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 10 deletions.
30 changes: 22 additions & 8 deletions addons/web/static/src/js/views/calendar/calendar_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ return AbstractModel.extend({
init: function () {
this._super.apply(this, arguments);
this.end_date = null;
var week_start = _t.database.parameters.week_start;
// calendar uses index 0 for Sunday but Odoo stores it as 7
this.week_start = week_start !== undefined && week_start !== false ? week_start % 7 : moment().startOf('week').day();
this.week_stop = this.week_start + 6;
},

//--------------------------------------------------------------------------
Expand Down Expand Up @@ -274,12 +278,25 @@ return AbstractModel.extend({

switch (this.data.scale) {
case 'month':
this.data.start_date = this.data.start_date.clone().startOf('month').startOf('week');
this.data.end_date = this.data.start_date.clone().add(5, 'week').endOf('week');
var monthStart = this.data.start_date.clone().startOf('month');

var monthStartDay;
if (monthStart.day() >= this.week_start) {
// the month's first day is after our week start
// Then we are in the right week
monthStartDay = this.week_start;
} else {
// The month's first day is before our week start
// Then we should go back to the the previous week
monthStartDay = this.week_start - 7;
}

this.data.start_date = monthStart.day(monthStartDay).startOf('day');
this.data.end_date = this.data.start_date.clone().add(5, 'week').day(this.week_stop).endOf('day');
break;
case 'week':
this.data.start_date = this.data.start_date.clone().startOf('week');
this.data.end_date = this.data.end_date.clone().endOf('week');
this.data.start_date = this.data.start_date.clone().day(this.week_start).startOf('day');
this.data.end_date = this.data.end_date.clone().day(this.week_stop).endOf('day');
break;
default:
this.data.start_date = this.data.start_date.clone().startOf('day');
Expand Down Expand Up @@ -392,9 +409,6 @@ return AbstractModel.extend({
* @returns {Object}
*/
_getFullCalendarOptions: function () {
var week_start = _t.database.parameters.week_start || 0;
// calendar uses index 0 for Sunday but Odoo stores it as 7
week_start = week_start % 7;
return {
defaultView: (this.mode === "month")? "month" : ((this.mode === "week")? "agendaWeek" : ((this.mode === "day")? "agendaDay" : "agendaWeek")),
header: false,
Expand All @@ -414,7 +428,7 @@ return AbstractModel.extend({
monthNamesShort: moment.monthsShort(),
dayNames: moment.weekdays(),
dayNamesShort: moment.weekdaysShort(),
firstDay: week_start,
firstDay: this.week_start,
slotLabelFormat: _t.database.parameters.time_format.search("%H") != -1 ? 'H:mm': 'h(:mm)a',
};
},
Expand Down
144 changes: 142 additions & 2 deletions addons/web/static/tests/views/calendar_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ CalendarRenderer.include({
var createView = testUtils.createView;
var createAsyncView = testUtils.createAsyncView;

// 2016-12-12 08:00:00
var initialDate = new Date(2016, 11, 12, 8, 0, 0);
initialDate = new Date(initialDate.getTime() - initialDate.getTimezoneOffset()*60*1000);

Expand Down Expand Up @@ -563,7 +564,7 @@ QUnit.module('Views', {

QUnit.test('default week start (US)', function (assert) {
// if not given any option, default week start is on Sunday
assert.expect(1);
assert.expect(3);
var done = assert.async();

createAsyncView({
Expand All @@ -581,17 +582,29 @@ QUnit.module('Views', {
viewOptions: {
initialDate: initialDate,
},
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'event') {
assert.deepEqual(args.kwargs.domain, [
["start","<=","2016-12-17 23:59:59"],
["stop",">=","2016-12-11 00:00:00"]
],
'The domain to search events in should be correct');
}
return this._super.apply(this, arguments);
}
}).then(function (calendar) {
assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Sun 12/11",
"The first day of the week should be Sunday");
assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sat 12/17",
"The last day of the week should be Saturday");
calendar.destroy();
done();
});
});

QUnit.test('European week start', function (assert) {
// the week start depends on the locale
assert.expect(1);
assert.expect(3);
var done = assert.async();

createAsyncView({
Expand All @@ -612,9 +625,21 @@ QUnit.module('Views', {
translateParameters: {
week_start: 1,
},
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'event') {
assert.deepEqual(args.kwargs.domain, [
["start","<=","2016-12-18 23:59:59"],
["stop",">=","2016-12-12 00:00:00"]
],
'The domain to search events in should be correct');
}
return this._super.apply(this, arguments);
}
}).then(function (calendar) {
assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Mon 12/12",
"The first day of the week should be Monday");
assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sun 12/18",
"The last day of the week should be Sunday");
calendar.destroy();
done();
});
Expand Down Expand Up @@ -2644,6 +2669,121 @@ QUnit.module('Views', {
calendar.destroy();
});

QUnit.test('default week start (US) month mode', function (assert) {
// if not given any option, default week start is on Sunday
assert.expect(7);
var done = assert.async();

// 2019-09-12 08:00:00
var initDate = new Date(2019, 8, 12, 8, 0, 0);
initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000);

createAsyncView({
View: CalendarView,
model: 'event',
data: this.data,
arch:
'<calendar class="o_calendar_test" '+
'date_start="start" '+
'date_stop="stop" '+
'mode="month">'+
'</calendar>',
archs: archs,

viewOptions: {
initialDate: initDate,
},
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'event') {
assert.deepEqual(args.kwargs.domain, [
["start","<=","2019-10-12 23:59:59"],
["stop",">=","2019-09-01 00:00:00"]
],
'The domain to search events in should be correct');
}
return this._super.apply(this, arguments);
}
}).then(function (calendar) {
assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Sun",
"The first day of the week should be Sunday");
assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sat",
"The last day of the week should be Saturday");

var $firstDay = calendar.$('.fc-day-top').first();
assert.strictEqual($firstDay.text(), "1",
"The first day of the week should be 2019-09-01");
assert.strictEqual($firstDay.data('date'), "2019-09-01",
"The first day of the week should be 2019-09-01");

var $lastDay = calendar.$('.fc-day-top').last();
assert.strictEqual($lastDay.text(), "12",
"The last day of the week should be 2019-10-12");
assert.strictEqual($lastDay.data('date'), "2019-10-12",
"The first day of the week should be 2019-10-12");

calendar.destroy();
done();
});
});

QUnit.test('European week start month mode', function (assert) {
assert.expect(7);
var done = assert.async();

// 2019-09-12 08:00:00
var initDate = new Date(2019, 8, 12, 8, 0, 0);
initDate = new Date(initDate.getTime() - initDate.getTimezoneOffset()*60*1000);

createAsyncView({
View: CalendarView,
model: 'event',
data: this.data,
arch:
'<calendar class="o_calendar_test" '+
'date_start="start" '+
'date_stop="stop" '+
'mode="month">'+
'</calendar>',
archs: archs,

viewOptions: {
initialDate: initDate,
},
translateParameters: {
week_start: 1,
},
mockRPC: function (route, args) {
if (args.method === 'search_read' && args.model === 'event') {
assert.deepEqual(args.kwargs.domain, [
["start","<=","2019-10-06 23:59:59"],
["stop",">=","2019-08-26 00:00:00"]
],
'The domain to search events in should be correct');
}
return this._super.apply(this, arguments);
}
}).then(function (calendar) {
assert.strictEqual(calendar.$('.fc-day-header').first().text(), "Mon",
"The first day of the week should be Monday");
assert.strictEqual(calendar.$('.fc-day-header').last().text(), "Sun",
"The last day of the week should be Sunday");

var $firstDay = calendar.$('.fc-day-top').first();
assert.strictEqual($firstDay.text(), "26",
"The first day of the week should be 2019-08-26");
assert.strictEqual($firstDay.data('date'), "2019-08-26",
"The first day of the week should be 2019-08-26");

var $lastDay = calendar.$('.fc-day-top').last();
assert.strictEqual($lastDay.text(), "6",
"The last day of the week should be 2019-10-06");
assert.strictEqual($lastDay.data('date'), "2019-10-06",
"The first day of the week should be 2019-10-06");

calendar.destroy();
done();
});
});
});

});

0 comments on commit 0bd82ef

Please sign in to comment.