[ADD] UI for set_default in form view sidebar

bzr revid: xmo@openerp.com-20120208122153-k11n7u47n1bo63sv
This commit is contained in:
Xavier Morel 2012-02-08 13:21:53 +01:00
parent 320b2d2e2e
commit fe31030bd2
3 changed files with 156 additions and 22 deletions

View File

@ -121,6 +121,15 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
this.sidebar.attachments = new openerp.web.form.SidebarAttachments(this.sidebar, this);
this.sidebar.add_toolbar(this.fields_view.toolbar);
this.set_common_sidebar_sections(this.sidebar);
this.sidebar.add_section(_t('Customize'), 'customize');
this.sidebar.add_items('customize', [{
label: _t('Set Default'),
form: this,
callback: function (item) {
item.form.open_defaults_dialog();
}
}]);
}
this.has_been_loaded.resolve();
},
@ -631,6 +640,67 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
},
sidebar_context: function () {
return this.do_save().pipe(_.bind(function() {return this.get_fields_values();}, this));
},
open_defaults_dialog: function () {
var self = this;
var fields = _.chain(this.fields)
.map(function (field, name) {
var value = field.get_value();
// ignore fields which are empty, invisible, readonly, o2m
// or m2m
if (!value
|| field.invisible
|| field.readonly
|| field.field.type === 'one2many'
|| field.field.type === 'many2many') {
return false;
}
return {
name: name,
string: field.string,
value: value,
// convert undefined to false
change_default: !!field.field.change_default
}
})
.compact()
.sortBy(function (field) { return field.string; })
.value();
var conditions = _.chain(fields)
.filter(function (field) { return field.change_default; })
.value();
var d = new openerp.web.Dialog(this, {
title: _t("Set Default"),
args: {
fields: fields,
conditions: conditions
},
buttons: [
{text: _t("Close"), click: function () { d.close(); }},
{text: _t("Save default"), click: function () {
var $defaults = d.$element.find('#formview_default_fields');
var field_to_set = $defaults.val();
if (!field_to_set) {
$defaults.parent().addClass('invalid');
return;
}
var condition = d.$element.find('#formview_default_conditions').val(),
all_users = d.$element.find('#formview_default_all').is(':checked');
new openerp.web.DataSet(self, 'ir.values').call(
'set_default', [
self.dataset.model,
field_to_set,
self.fields[field_to_set].get_value(),
all_users,
false,
condition || false
]).then(function () { d.close(); });
}}
]
});
d.template = 'FormView.set_default';
d.open();
}
});
openerp.web.FormDialog = openerp.web.Dialog.extend({

View File

@ -821,10 +821,6 @@ session.web.Sidebar = session.web.OldWidget.extend({
}, {
label: _t("Export"),
callback: view.on_sidebar_export
}, {
label: _t("View Log"),
callback: view.on_sidebar_view_log,
classname: 'oe_hide oe_sidebar_view_log'
}
]);
},
@ -863,18 +859,32 @@ session.web.Sidebar = session.web.OldWidget.extend({
}
return $section;
},
/**
* For each item added to the section:
*
* ``label``
* will be used as the item's name in the sidebar
*
* ``action``
* descriptor for the action which will be executed, ``action`` and
* ``callback`` should be exclusive
*
* ``callback``
* function to call when the item is clicked in the sidebar, called
* with the item descriptor as its first argument (so information
* can be stored as additional keys on the object passed to
* ``add_items``)
*
* ``classname`` (optional)
* ``@class`` set on the sidebar serialization of the item
*
* ``title`` (optional)
* will be set as the item's ``@title`` (tooltip)
*
* @param {String} section_code
* @param {Array<{label, action | callback[, classname][, title]}>} items
*/
add_items: function(section_code, items) {
// An item is a dictonary : {
// label: label to be displayed for the link,
// action: action to be launch when the link is clicked,
// callback: a function to be executed when the link is clicked,
// classname: optional dom class name for the line,
// title: optional title for the link
// }
// Note: The item should have one action or/and a callback
//
var self = this,
$section = this.add_section(_.str.titleize(section_code.replace('_', ' ')), section_code),
section_id = $section.attr('id');
@ -1222,8 +1232,6 @@ session.web.View = session.web.Widget.extend(/** @lends session.web.View# */{
view_mode : "list"
});
},
on_sidebar_view_log: function() {
},
sidebar_context: function () {
return $.when();
},

View File

@ -529,11 +529,11 @@
</div>
</t>
<t t-name="Sidebar.section.items">
<li t-foreach="items" t-as="item" t-att-class="item.classname">
<a class="oe_sidebar_action_a" t-att-id="item.element_id" t-att-title="item.title" href="#">
<t t-esc="item.label"/>
</a>
</li>
<li t-foreach="items" t-as="item" t-att-class="item.classname">
<a class="oe_sidebar_action_a" t-att-id="item.element_id" t-att-title="item.title" href="#">
<t t-esc="item.label"/>
</a>
</li>
</t>
<t t-name="TranslateDialog">
@ -791,6 +791,62 @@
</li>
</ul>
</t>
<form t-name="FormView.set_default" class="oe_forms oe_frame">
<t t-set="args" t-value="widget.dialog_options.args"/>
<table style="width: 100%">
<tr>
<td>
<label for="formview_default_fields"
class="oe_label oe_align_right">
Default:
</label>
</td>
<td class="required">
<select id="formview_default_fields">
<option value=""/>
<option t-foreach="args.fields" t-as="field"
t-att-value="field.name">
<t t-esc="field.string"/> = <t t-esc="field.value"/>
</option>
</select>
</td>
</tr>
<tr t-if="args.conditions.length">
<td>
<label for="formview_default_conditions"
class="oe_label oe_align_right">
Condition:
</label>
</td>
<td>
<select id="formview_default_conditions">
<option value=""/>
<option t-foreach="args.conditions" t-as="cond"
t-att-value="cond.name + '=' + cond.value">
<t t-esc="cond.string"/>=<t t-esc="cond.value"/>
</option>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="radio" id="formview_default_self"
value="self" name="scope" checked="checked"/>
<label for="formview_default_self" class="oe_label"
style="display: inline;">
Only you
</label>
<br/>
<input type="radio" id="formview_default_all"
value="all" name="scope"/>
<label for="formview_default_all" class="oe_label"
style="display: inline;">
All users
</label>
</td>
</tr>
</table>
</form>
<t t-name="Widget">
Unhandled widget
<t t-js="dict">console.warn('Unhandled widget', dict.widget);</t>