[FIX] web: kanban quick create properly disabled

when grouped on field types for which it isn't supported.

Rev. 42e1efd disabled the quick create feature when the view is
grouped by date(time) fields. However, it hasn't been correctly
forwardported to 11.0 (with the new views). Indeed, the check was
done only once, at the initilization of the view. So if the user
selected another field to group by afterwards, the quick create
feature wasn't enabled/disabled accordingly.

Moreover, we didn't check if it was available when the user clicked
on CREATE in the control panel. So even if it wasn't (and thus if
there were no '+' icon in the columns), when the user clicked on
CREATE, the quick create widget was inserted in the first column.

This rev. fixes both issues.

Task 1878254

closes odoo/odoo#27867
This commit is contained in:
Aaron Bohy
2018-10-19 06:56:40 +00:00
parent 433666b2d9
commit 22a13073f0
6 changed files with 125 additions and 11 deletions
@@ -280,3 +280,35 @@ var AbstractView = Class.extend({
return AbstractView;
});
odoo.define('web.viewUtils', function () {
"use strict";
/**
* FIXME: move this module to its own file in master
*/
var utils = {
/**
* States whether or not the quick create feature is available for the given
* datapoint, depending on its groupBy field.
*
* @param {Object} list dataPoint of type list
* @returns {Boolean} true iff the kanban quick create feature is available
*/
isQuickCreateEnabled: function (list) {
var groupByField = list.groupedBy[0] && list.groupedBy[0].split(':')[0];
if (!groupByField) {
return false;
}
var availableTypes = ['char', 'boolean', 'many2one'];
if (!_.contains(availableTypes, list.fields[groupByField].type)) {
return false;
}
return true;
},
};
return utils;
});
@@ -12,6 +12,7 @@ var Context = require('web.Context');
var core = require('web.core');
var Domain = require('web.Domain');
var view_dialogs = require('web.view_dialogs');
var viewUtils = require('web.viewUtils');
var _t = core._t;
var qweb = core.qweb;
@@ -35,6 +36,8 @@ var KanbanController = BasicController.extend({
/**
* @override
* @param {Object} params
* @param {boolean} params.quickCreateEnabled set to false to disable the
* quick create feature
*/
init: function (parent, model, renderer, params) {
this._super.apply(this, arguments);
@@ -43,6 +46,7 @@ var KanbanController = BasicController.extend({
this.hasButtons = params.hasButtons;
this.createColumnEnabled = this._isCreateColumnEnabled();
this.quickCreateEnabled = params.quickCreateEnabled;
},
//--------------------------------------------------------------------------
@@ -303,8 +307,8 @@ var KanbanController = BasicController.extend({
*/
_onButtonNew: function () {
var state = this.model.get(this.handle, {raw: true});
var hasColumns = state.groupedBy.length > 0 && state.data.length > 0;
if (hasColumns && this.on_create === 'quick_create') {
var quickCreateEnabled = this.quickCreateEnabled && viewUtils.isQuickCreateEnabled(state);
if (this.on_create === 'quick_create' && quickCreateEnabled) {
// Activate the quick create in the first column
this.renderer.addQuickCreate();
} else if (this.on_create && this.on_create !== 'quick_create') {
@@ -9,6 +9,7 @@ var quick_create = require('web.kanban_quick_create');
var QWeb = require('web.QWeb');
var session = require('web.session');
var utils = require('web.utils');
var viewUtils = require('web.viewUtils');
var ColumnQuickCreate = quick_create.ColumnQuickCreate;
@@ -89,6 +90,9 @@ var KanbanRenderer = BasicRenderer.extend({
/**
* @override
* @param {Object} params
* @param {boolean} params.quickCreateEnabled set to false to disable the
* quick create feature
*/
init: function (parent, state, params) {
this._super.apply(this, arguments);
@@ -107,6 +111,7 @@ var KanbanRenderer = BasicRenderer.extend({
if (this.columnOptions.hasProgressBar) {
this.columnOptions.progressBarStates = {};
}
this.quickCreateEnabled = params.quickCreateEnabled;
this._setState(state);
},
@@ -359,6 +364,7 @@ var KanbanRenderer = BasicRenderer.extend({
draggable: draggable,
group_by_tooltip: groupByTooltip,
grouped_by_m2o: this.groupedByM2O,
quick_create: this.quickCreateEnabled && viewUtils.isQuickCreateEnabled(state),
relation: grouped_by_field,
});
this.createColumnEnabled = this.groupedByM2O && this.columnOptions.group_creatable;
@@ -63,7 +63,6 @@ var KanbanView = BasicView.extend({
editable: activeActions.group_edit,
deletable: activeActions.group_delete,
group_creatable: activeActions.group_create && !config.device.isMobile,
quick_create: params.isQuickCreateEnabled || this._isQuickCreateEnabled(viewInfo),
hasProgressBar: !!progressBar,
};
this.rendererParams.record_options = {
@@ -71,11 +70,12 @@ var KanbanView = BasicView.extend({
deletable: activeActions.delete,
read_only_mode: params.readOnlyMode,
};
this.rendererParams.quickCreateEnabled = this._isQuickCreateEnabled(viewInfo);
this.controllerParams.on_create = arch.attrs.on_create;
this.controllerParams.readOnlyMode = false;
this.controllerParams.hasButtons = true;
this.controllerParams.quickCreateEnabled = this.rendererParams.quickCreateEnabled;
if (config.device.isMobile) {
this.jsLibs.push('/web/static/lib/jquery.touchSwipe/jquery.touchSwipe.js');
@@ -89,13 +89,10 @@ var KanbanView = BasicView.extend({
/**
* @private
* @param {Object} viewInfo
* @returns {boolean} true iff the quick create feature is not explicitely
* disabled (with create="False" or quick_create="False" in the arch)
*/
_isQuickCreateEnabled: function (viewInfo) {
var groupBy = this.loadParams.groupBy[0];
groupBy = groupBy !== undefined ? groupBy.split(':')[0] : undefined;
if (groupBy !== undefined && !_.contains(['char', 'boolean', 'many2one'], viewInfo.fields[groupBy].type)) {
return false;
}
if (!this.controllerParams.activeActions.create) {
return false;
}
+76 -1
View File
@@ -769,6 +769,81 @@ QUnit.module('Views', {
kanban.destroy();
});
QUnit.test('quick create record in grouped on date(time) field', function (assert) {
assert.expect(6);
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban class="o_kanban_test" on_create="quick_create">' +
'<templates><t t-name="kanban-box">' +
'<div><field name="display_name"/></div>' +
'</t></templates>' +
'</kanban>',
groupBy: ['date'],
intercepts: {
switch_view: function (ev) {
assert.deepEqual(_.pick(ev.data, 'res_id', 'view_type'), {
res_id: undefined,
view_type: 'form',
}, "should trigger an event to open the form view (twice)");
},
},
});
assert.strictEqual(kanban.$('.o_kanban_header .o_kanban_quick_add i').length, 0,
"quick create should be disabled when grouped on a date field");
// clicking on CREATE in control panel should not open a quick create
kanban.$buttons.find('.o-kanban-button-new').click();
assert.strictEqual(kanban.$('.o_kanban_quick_create').length, 0,
"should not have opened the quick create widget");
kanban.reload({groupBy: ['datetime']});
assert.strictEqual(kanban.$('.o_kanban_header .o_kanban_quick_add i').length, 0,
"quick create should be disabled when grouped on a datetime field");
// clicking on CREATE in control panel should not open a quick create
kanban.$buttons.find('.o-kanban-button-new').click();
assert.strictEqual(kanban.$('.o_kanban_quick_create').length, 0,
"should not have opened the quick create widget");
kanban.destroy();
});
QUnit.test('quick create record feature is properly enabled/disabled at reload', function (assert) {
assert.expect(3);
var kanban = createView({
View: KanbanView,
model: 'partner',
data: this.data,
arch: '<kanban class="o_kanban_test" on_create="quick_create">' +
'<templates><t t-name="kanban-box">' +
'<div><field name="display_name"/></div>' +
'</t></templates>' +
'</kanban>',
groupBy: ['foo'],
});
assert.strictEqual(kanban.$('.o_kanban_header .o_kanban_quick_add i').length, 3,
"quick create should be enabled when grouped on a char field");
kanban.reload({groupBy: ['date']});
assert.strictEqual(kanban.$('.o_kanban_header .o_kanban_quick_add i').length, 0,
"quick create should now be disabled (grouped on date field)");
kanban.reload({groupBy: ['bar']});
assert.strictEqual(kanban.$('.o_kanban_header .o_kanban_quick_add i').length, 2,
"quick create should be enabled again (grouped on boolean field)");
kanban.destroy();
});
QUnit.test('many2many_tags in kanban views', function (assert) {
assert.expect(12);
@@ -2833,7 +2908,7 @@ QUnit.module('Views', {
'<div><field name="foo"/></div>' +
'</t></templates>' +
'</kanban>',
groupBy: ['int_field'],
groupBy: ['foo'],
mockRPC: function (route, args) {
if (route === '/web/dataset/resequence') {
return $.when(true);
+1 -1
View File
@@ -852,7 +852,7 @@ attributes:
``quick_create``
whether it should be possible to create records without switching to the
form view. By default, ``quick_create`` is enabled when the Kanban view is
grouped, and disabled when not.
grouped by many2one, char or boolean fields, and disabled when not.
Set to ``true`` to always enable it, and to ``false`` to always disable it.