[REF] account_asset: convert widget to new API

Also change the view template as according to the doc, the widget
attribute is only available for field tagnames.
This commit is contained in:
Aaron Bohy
2017-04-11 19:44:38 +02:00
parent 6448568954
commit aa5700bb7c
5 changed files with 238 additions and 30 deletions
@@ -1,36 +1,76 @@
/*
Purpose : show toggle button on depreciation/installment lines for posted/unposted line.
Details : called in list view with "<button name="create_move" type="object" widget="widgetonbutton"/>",
this will call the method create_move on the object account.asset.depreciation.line
*/
odoo.define('account_asset.widget', function(require) {
"use strict";
/**
* The purpose of this widget is to shows a toggle button on depreciation and
* installment lines for posted/unposted line. When clicked, it calls the method
* create_move on the object account.asset.depreciation.line.
* Note that this widget can only work on the account.asset.depreciation.line
* model as some of its fields are harcoded.
*/
var AbstractField = require('web.AbstractField');
var core = require('web.core');
var QWeb = core.qweb;
var session = require('web.session');
var registry = require('web.field_registry');
var _t = core._t;
var WidgetOnButton = core.list_widget_registry.get('field').extend({
format: function(row_data, options) {
this._super.apply(this, arguments);
this.is_posted = !!row_data.move_posted_check.value;
this.parent_state = row_data.parent_state.value;
var class_color = '';
if (this.is_posted){class_color = 'o_is_posted';}
else{
if (row_data.move_check.value){class_color = 'o_unposted';}
var AccountAssetWidget = AbstractField.extend({
events: _.extend({}, AbstractField.prototype.events, {
'click': '_onClick',
}),
description: "",
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
* @private
*/
_render: function () {
var className = '';
var disabled = true;
var title;
if (this.recordData.move_posted_check) {
className = 'o_is_posted';
title = _t('Posted');
} else if (this.recordData.move_check) {
className = 'o_unposted';
title = _t('Accounting entries waiting for manual verification');
} else {
disabled = false;
title = _t('Unposted');
}
return $('<div/>').append((this.parent_state === 'open')? $('<button/>', {
var $button = $('<button/>', {
type: 'button',
title: (this.has_value)? _t('Posted') : _t('Unposted'),
disabled: !!this.has_value,
'class': 'btn btn-sm btn-link fa fa-circle o_widgetonbutton ' + class_color,
}) : '').html();
title: title,
disabled: disabled,
}).addClass('btn btn-sm btn-link fa fa-circle o_deprec_lines_toggler ' + className);
this.$el.html($button);
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @private
* @param {MouseEvent} event
*/
_onClick: function (event) {
event.stopPropagation();
this.trigger_up('button_clicked', {
attrs: {
name: 'create_move',
type: 'object',
},
record: this.record,
});
},
});
core.list_widget_registry.add("button.widgetonbutton", WidgetOnButton);
registry.add("deprec_lines_toggler", AccountAssetWidget);
});
@@ -1,9 +1,9 @@
.o_web_client .o_widgetonbutton {
.o_web_client .o_deprec_lines_toggler {
color: @brand-danger;
&.o_is_posted {
color: @brand-success;
}
&.o_unposted{
&.o_unposted {
color: @brand-warning;
}
}
@@ -0,0 +1,165 @@
odoo.define('account_asset.widget_tests', function (require) {
"use strict";
var FormView = require('web.FormView');
var testUtils = require('web.test_utils');
var createView = testUtils.createView;
QUnit.module('fields', {}, function () {
QUnit.module('account_asset', {
beforeEach: function () {
this.data = {
asset: {
fields: {
display_name: { string: "Displayed name", type: "char" },
line_ids: {
string: "Lines",
type: "one2many",
relation: 'line',
relation_field: 'asset_id',
},
},
records: [{
id: 1,
display_name: "asset name",
line_ids: [1, 2, 3, 4],
}],
},
line: {
fields: {
move_check: {string: "Move Check", type: 'boolean'},
move_posted_check: {string: "Move Posted Check", type: 'boolean'},
asset_id: {string: "Asset", type: 'many2one', relation: 'asset'},
},
records: [{
id: 1,
move_check: true,
move_posted_check: true,
}, {
id: 2,
move_check: false,
move_posted_check: true,
}, {
id: 3,
move_check: true,
move_posted_check: false,
}, {
id: 4,
move_check: false,
move_posted_check: false,
}],
},
};
}
});
QUnit.test('basic rendering', function (assert) {
assert.expect(17);
var form = createView({
View: FormView,
model: 'asset',
data: this.data,
arch: '<form string="Asset">' +
'<sheet>' +
'<field name="display_name"/>' +
'<field name="line_ids">' +
'<tree>' +
'<field name="move_check" widget="deprec_lines_toggler"/>' +
'<field name="move_posted_check" invisible="1"/>' +
'</tree>' +
'</field>' +
'</sheet>' +
'</form>',
res_id: 1,
});
// check the header
assert.strictEqual(form.$('thead th').text(), "", "toggler column should have no title");
// check the classnames
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(0) button').hasClass('o_is_posted'),
"first line toggler should have classname 'o_is_posted'");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(0) button').hasClass('o_unposted'),
"first line toggler should not have classname 'o_unposted'");
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(1) button').hasClass('o_is_posted'),
"second line toggler should have classname 'o_is_posted'");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(1) button').hasClass('o_unposted'),
"second line toggler should not have classname 'o_unposted'");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(2) button').hasClass('o_is_posted'),
"third line toggler should not have classname 'o_is_posted'");
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(2) button').hasClass('o_unposted'),
"third line toggler should have classname 'o_unposted'");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').hasClass('o_is_posted'),
"fourth line toggler should not have classname 'o_is_posted'");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').hasClass('o_unposted'),
"fourth line toggler should not have classname 'o_unposted'");
// check the titles
assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(0) button').attr('title'),
'Posted', "first line toggler should have correct title");
assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(1) button').attr('title'),
'Posted', "second line toggler should have correct title");
assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(2) button').attr('title'),
'Accounting entries waiting for manual verification',
"third line toggler should have correct title");
assert.strictEqual(form.$('.o_deprec_lines_toggler_cell:nth(3) button').attr('title'),
'Unposted', "fourth line toggler should have correct title");
// check disabled property
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(0) button').attr('disabled'),
"first line toggle should be disabled");
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(1) button').attr('disabled'),
"second line toggle should be disabled");
assert.ok(form.$('.o_deprec_lines_toggler_cell:nth(2) button').attr('disabled'),
"third line toggle should be disabled");
assert.ok(!form.$('.o_deprec_lines_toggler_cell:nth(3) button').attr('disabled'),
"fourth line toggle should not be disabled");
form.destroy();
});
QUnit.test('click events are correctly triggered', function (assert) {
assert.expect(3);
var form = createView({
View: FormView,
model: 'asset',
data: this.data,
arch: '<form string="Asset">' +
'<sheet>' +
'<field name="display_name"/>' +
'<field name="line_ids">' +
'<tree>' +
'<field name="move_check" widget="deprec_lines_toggler"/>' +
'<field name="move_posted_check" invisible="1"/>' +
'</tree>' +
'</field>' +
'</sheet>' +
'</form>',
res_id: 1,
intercepts: {
execute_action: function (event) {
var data = event.data;
assert.strictEqual(data.model, 'line', "should have correct model");
assert.strictEqual(data.record_id, 4, "should have correct res_id");
assert.strictEqual(data.action_data.name, 'create_move',
"should call correct method");
},
}
});
// click on last row toggler
form.$('.o_deprec_lines_toggler_cell:nth(3) button').click();
form.destroy();
});
});
});
@@ -6,6 +6,10 @@
<script type="text/javascript" src="/account_asset/static/src/js/account_asset.js"></script>
</xpath>
</template>
<template id="qunit_suite" name="account_asset_tests" inherit_id="web.qunit_suite">
<xpath expr="//t[@t-set='head']" position="inside">
<script type="text/javascript" src="/account_asset/static/tests/account_asset_tests.js"></script>
</xpath>
</template>
</odoo>
@@ -144,12 +144,11 @@
<field name="depreciated_value" readonly="1"/>
<field name="amount" widget="monetary" string="Depreciation"/>
<field name="remaining_value" readonly="1" widget="monetary" string="Residual"/>
<field name="move_check" invisible="1"/>
<field name="move_check" widget="deprec_lines_toggler" attrs="{'invisible': [('parent_state', '!=', 'open')]}"/>
<field name="move_posted_check" invisible="1"/>
<field name="parent_state" invisible="1"/>
<button name="create_move" type="object" widget="widgetonbutton"/>
</tree>
<form string="Depreciation Lines">
<form string="Depreciation Lines">
<group>
<group>
<field name="parent_state" invisible="1"/>