[FIX] web,base: document template widget in settings

Go to General Settings, click on 'Change Document Template', and
in the opened dialog, click on a template.

Before this rev., it automatically closed the dialog, which was
not really convenient.

This rev. introduces a new field widget (image_selection) for
this use case, and ensures that the dialog doesn't close when
a template is selected.

Test written by @mba-odoo
This commit is contained in:
Deep Patel
2017-09-20 10:56:57 +02:00
committed by Aaron Bohy
parent 48b8be29fa
commit 9b9fcfe628
5 changed files with 111 additions and 46 deletions
@@ -2391,6 +2391,56 @@ var AceEditor = DebouncedField.extend({
},
});
var ImageSelection = AbstractField.extend({
supportedFieldTypes: ['selection'],
events: _.extend({}, AbstractField.prototype.events, {
'click img': '_onImgClicked',
}),
//--------------------------------------------------------------------------
// Private
//--------------------------------------------------------------------------
/**
* @override
* @private
*/
_render: function () {
var self = this;
this.$el.empty();
_.each(this.nodeOptions, function (val, key) {
var $container = $('<div>').addClass('col-xs-3 text-center');
var $img = $('<img>')
.addClass('img img-responsive img-thumbnail ml16')
.toggleClass('btn-info', key === self.value)
.attr('src', val.image_link)
.data('key', key);
$container.append($img);
if (val.preview_link) {
var $previewLink = $('<a>')
.text('Preview')
.attr('href', val.preview_link)
.attr('target', '_blank');
$container.append($previewLink);
}
self.$el.append($container);
});
},
//--------------------------------------------------------------------------
// Handlers
//--------------------------------------------------------------------------
/**
* @override
* @private
* @param {MouseEvent} event
*/
_onImgClicked: function (event) {
this._setValue($(event.currentTarget).data('key'));
},
});
return {
TranslatableFieldMixin: TranslatableFieldMixin,
DebouncedField: DebouncedField,
@@ -2416,6 +2466,7 @@ return {
HandleWidget: HandleWidget,
InputField: InputField,
AttachmentImage: AttachmentImage,
ImageSelection: ImageSelection,
LabelSelection: LabelSelection,
StateSelectionWidget: StateSelectionWidget,
FavoriteWidget: FavoriteWidget,
@@ -37,6 +37,7 @@ registry
.add('monetary', basic_fields.FieldMonetary)
.add('priority', basic_fields.PriorityWidget)
.add('attachment_image', basic_fields.AttachmentImage)
.add('image_selection', basic_fields.ImageSelection)
.add('label_selection', basic_fields.LabelSelection)
.add('state_selection', basic_fields.StateSelectionWidget)
.add('kanban_state_selection', basic_fields.StateSelectionWidget)
@@ -38,6 +38,8 @@ QUnit.module('basic_fields', {
selection: {string: "Selection", type: "selection", searchable:true,
selection: [['normal', 'Normal'],['blocked', 'Blocked'],['done', 'Done']]},
document: {string: "Binary", type: "binary"},
image_selection: {string: "Image Selection", type: "selection", searchable:true,
selection: [['background', 'Background'],['boxed', 'Boxed'],['clean', 'Clean'],['standard', 'Standard']]},
},
records: [{
id: 1,
@@ -3995,6 +3997,45 @@ QUnit.module('basic_fields', {
form.destroy();
});
QUnit.module('FieldImageSelection');
QUnit.test('image selection widget in form view', function (assert) {
assert.expect(3);
var nodeOptions = {
background: {
image_link: '/base/static/img/preview_background.png',
preview_link: '/base/static/pdf/preview_background.pdf',
},
boxed: {
image_link: '/base/static/img/preview_boxed.png',
preview_link: '/base/static/pdf/preview_boxed.pdf',
},
};
var form = createView({
View: FormView,
model: 'partner',
data: this.data,
arch: '<form>' +
'<field name="image_selection" widget="image_selection"' +
' options=\'' + JSON.stringify(nodeOptions) + '\'/> '+
'</form>',
res_id: 2,
});
assert.strictEqual(form.$('.img.img-responsive').length, 2,
"Two images should be rendered");
assert.strictEqual(form.$('.img.btn-info').length, 0,
"No image should be selected");
// select first image
form.$(".img.img-responsive:first").click();
assert.ok(form.$(".img.img-responsive:first").hasClass('btn-info'),
"First image should be selected");
form.destroy();
});
});
});
});
-12
View File
@@ -244,15 +244,3 @@ class Company(models.Model):
def open_company_edit_report(self):
self.ensure_one()
return self.env['res.config.settings'].open_company()
@api.multi
def set_report_template(self):
self.ensure_one()
if self.env.context.get('report_template', False):
self.external_report_layout = self.env.context['report_template']
if self.env.context.get('default_report_name'):
document = self.env.get(self.env.context['active_model']).browse(self.env.context['active_id'])
report_name = self.env.context['default_report_name']
report_action = self.env['ir.actions.report'].search([('report_name', '=', report_name)], limit=1)
return report_action.report_action(document, config=False)
return False
+18 -34
View File
@@ -135,40 +135,24 @@
<xpath expr="//sheet" position="inside">
<hr />
<div class="row mt16">
<field name="external_report_layout" invisible="1"/>
<div class="col-xs-3 text-center">
<button type="object" name="set_report_template" context="{'report_template': 'background'}" attrs="{'invisible': [('external_report_layout', '=', 'background')]}">
<img src="/base/static/img/preview_background.png" class="img img-responsive img-thumbnail" alt="Preview Background Template"/>
</button>
<button class="btn-info" type="object" name="set_report_template" context="{'report_template': 'background'}" attrs="{'invisible': [('external_report_layout', '!=', 'background')]}">
<img src="/base/static/img/preview_background.png" class="img img-responsive img-thumbnail" alt="Preview Background Template"/>
</button>
<a href="/base/static/pdf/preview_background.pdf" target="_blank">preview</a>
</div><div class="col-xs-3 text-center">
<button type="object" name="set_report_template" context="{'report_template': 'boxed'}" attrs="{'invisible': [('external_report_layout', '=', 'boxed')]}">
<img src="/base/static/img/preview_boxed.png" class="img img-responsive img-thumbnail" alt="Preview Boxed Template"/>
</button>
<button class="btn-info" type="object" name="set_report_template" context="{'report_template': 'boxed'}" attrs="{'invisible': [('external_report_layout', '!=', 'boxed')]}">
<img src="/base/static/img/preview_boxed.png" class="img img-responsive img-thumbnail" alt="Preview Boxed Template"/>
</button>
<a href="/base/static/pdf/preview_boxed.pdf" target="_blank">preview</a>
</div><div class="col-xs-3 text-center">
<button type="object" name="set_report_template" context="{'report_template': 'clean'}" attrs="{'invisible': [('external_report_layout', '=', 'clean')]}">
<img src="/base/static/img/preview_clean.png" class="img img-responsive img-thumbnail" alt="Preview Clean Template"/>
</button>
<button class="btn-info" type="object" name="set_report_template" context="{'report_template': 'clean'}" attrs="{'invisible': [('external_report_layout', '!=', 'clean')]}">
<img src="/base/static/img/preview_clean.png" class="img img-responsive img-thumbnail" alt="Preview Clean Template"/>
</button>
<a href="/base/static/pdf/preview_clean.pdf" target="_blank">preview</a>
</div><div class="col-xs-3 text-center">
<button type="object" name="set_report_template" context="{'report_template': 'standard'}" attrs="{'invisible': [('external_report_layout', '=', 'standard')]}">
<img src="/base/static/img/preview_standard.png" class="img img-responsive img-thumbnail" alt="Preview Minimalist Template"/>
</button>
<button class="btn-info" type="object" name="set_report_template" context="{'report_template': 'standard'}" attrs="{'invisible': [('external_report_layout', '!=', 'standard')]}">
<img src="/base/static/img/preview_standard.png" class="img img-responsive img-thumbnail" alt="Preview Minimalist Template"/>
</button>
<a href="/base/static/pdf/preview_standard.pdf" target="_blank">preview</a>
</div>
<field name="external_report_layout" widget="image_selection" options="{
'background': {
'image_link': '/base/static/img/preview_background.png',
'preview_link': '/base/static/pdf/preview_background.pdf'
},
'boxed': {
'image_link': '/base/static/img/preview_boxed.png',
'preview_link': '/base/static/pdf/preview_boxed.pdf'
},
'clean': {
'image_link': '/base/static/img/preview_clean.png',
'preview_link': '/base/static/pdf/preview_clean.pdf'
},
'standard': {
'image_link': '/base/static/img/preview_standard.png',
'preview_link': '/base/static/pdf/preview_standard.pdf'
}
}"/>
</div>
<footer>
<button string="Save" type="object" class="oe_highlight" name="write"/>