Thibault Delavallée (OpenERP) has proposed merging
lp:~openerp-dev/openerp-web/trunk-stage-state-tde into lp:openerp-web.
Requested reviews:
OpenERP R&D Web Team (openerp-dev-web)
For more details, see:
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-stage-state-tde/+merge/107999
FieldStatus widget (also known as statusbar)
============================================
This branch proposes to update the FieldStatus widget to be able to work on
many2one fields. Example of application: stages on opportunities, tasks,
project issues, ...
Open questions
--------------
- management of deferred for get_selection (selection field: no call vs
many2one: call)
- folded attribute (hide a stage based on the 'fold' column of the related
model)
--
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-stage-state-tde/+merge/107999
Your team OpenERP R&D Team is subscribed to branch
lp:~openerp-dev/openerp-web/trunk-stage-state-tde.
=== modified file 'addons/web/static/src/js/view_form.js'
--- addons/web/static/src/js/view_form.js 2012-05-30 09:39:16 +0000
+++ addons/web/static/src/js/view_form.js 2012-05-30 15:50:31 +0000
@@ -1485,7 +1485,6 @@
if (! v_context) {
v_context = (this.field || {}).context || {};
}
-
if (v_context.__ref || true) { //TODO: remove true
var fields_values = this._build_eval_context(blacklist);
v_context = new instance.web.CompoundContext(v_context).set_eval_context(fields_values);
@@ -4093,63 +4092,120 @@
this._super();
this.selected_value = null;
- this.render_list();
+ /** preview in start only for selection fields, because of
+ * the dynamic behavior of many2one fields. */
+ if (this.field.type in ['selection']) {
+ this.render_list();
+ }
},
set_value: function(value_) {
+ var self = this;
this._super(value_);
- this.selected_value = value_;
+ /** find selected value: ex:
+ * - many2one: [2, "New"] -> 2
+ * - selection: new -> new */
+ if (this.field.type == "many2one") {
+ this.selected_value = value_[0];
+ }
+ else {
+ this.selected_value = value_;
+ }
+ // trick to be sure all values are loaded in the form, therefore
+ // enabling the evaluation of dynamic domains
+ $.async_when().then(function() {
+ return self.render_list();
+ });
+ },
- this.render_list();
- },
+ /** Get the status list and render them
+ * to_show: [[identifier, value_to_display]] where
+ * - identifier = key for a selection, id for a many2one
+ * - display_val = label that will be displayed
+ * - ex: [[0, "New"]] (many2one) or [["new", "In Progress"]] (selection)
+ */
render_list: function() {
var self = this;
+ // get selection values, filter them and render them
+ var selection_done = this.get_selection();
+ return $.when(selection_done).pipe(self.proxy('filter_selection')).pipe(self.proxy('render_elements'));
+ },
+
+ /** Get the selection list to be displayed in the statusbar widget.
+ * For selection fields: this is directly given by this.field.selection
+ * For many2one fields :
+ * - perform a search on the relation of the many2one field (given by
+ * field.relation )
+ * - get the field domain for the search
+ * - self.build_domain() gives the domain given by the view or by
+ * the field
+ * - if the optional statusbar_fold attribute is set to true, make
+ * an AND with build_domain to hide all 'fold=true' columns
+ * - make an OR with current value, to be sure it is displayed,
+ * with the correct order, even if it is folded
+ */
+ get_selection: function() {
+ var self = this;
+ if (this.field.type == "many2one") {
+ this.selection = [];
+ // get fold information from widget
+ var fold = ((this.node.attrs || {}).statusbar_fold || true);
+ // build final domain: if fold option required, add the
+ if (fold == true)
+ var domain = new instance.web.CompoundDomain(['|'], ['&'], self.build_domain(), [['fold', '=', false]], [['id', '=', self.selected_value]])
+ else {
+ var domain = new instance.web.CompoundDomain(['|'], self.build_domain(), [['id', '=', self.selected_value]])
+ }
+ // get a DataSetSearch on the current field relation (ex: crm.lead.stage_id -> crm.case.stage)
+ var model_ext = new instance.web.DataSetSearch(this, this.field.relation, self.build_context(), domain);
+ // fetch selection
+ var read_defer = model_ext.read_slice(['name'], {}).pipe( function (records) {
+ _(records).each(function (record) {
+ self.selection.push([record.id, record.name]);
+ });
+ });
+ }
+ else {
+ this.selection = this.field.selection;
+ var read_defer = true;
+ }
+ return read_defer;
+ },
+
+ /** Filters this.selection, according to values coming from the statusbar_visible
+ * attribute of the field. For example: statusbar_visible="draft,open"
+ * Currently, the key of (key, label) pairs has to be used in the
+ * selection of visible items. This feature is not meant to be used
+ * with many2one fields.
+ */
+ filter_selection: function() {
+ var self = this;
var shown = _.map(((this.node.attrs || {}).statusbar_visible || "").split(","),
function(x) { return _.str.trim(x); });
shown = _.select(shown, function(x) { return x.length > 0; });
-
+
if (shown.length == 0) {
- this.to_show = this.field.selection;
+ this.to_show = this.selection;
} else {
- this.to_show = _.select(this.field.selection, function(x) {
+ this.to_show = _.select(this.selection, function(x) {
return _.indexOf(shown, x[0]) !== -1 || x[0] === self.selected_value;
});
}
+ },
+ /** Renders the widget. This function also checks for statusbar_colors='{"pending": "blue"}'
+ * attribute in the widget. This allows to set a given color to a given
+ * state (given by the key of (key, label)). */
+ render_elements: function () {
var content = instance.web.qweb.render("FieldStatus.content", {widget: this, _:_});
this.$element.html(content);
var colors = JSON.parse((this.node.attrs || {}).statusbar_colors || "{}");
var color = colors[this.selected_value];
if (color) {
- var elem = this.$element.find("li.oe-arrow-list-selected span");
- elem.css("border-color", color);
- if (this.check_white(color))
- elem.css("color", "white");
- elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-before");
- elem.css("border-left-color", "rgba(0,0,0,0)");
- elem = this.$element.find("li.oe-arrow-list-selected .oe-arrow-list-after");
- elem.css("border-color", "rgba(0,0,0,0)");
- elem.css("border-left-color", color);
+ var elem = this.$element.find("li.oe_form_steps_active span");
+ elem.css("color", color);
}
},
- check_white: function(color) {
- var div = $("<div></div>");
- div.css("display", "none");
- div.css("color", color);
- div.appendTo($("body"));
- var ncolor = div.css("color");
- div.remove();
- var res = /^\s*rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)\s*$/.exec(ncolor);
- if (!res) {
- return false;
- }
- var comps = [parseInt(res[1]), parseInt(res[2]), parseInt(res[3])];
- var lum = comps[0] * 0.3 + comps[1] * 0.59 + comps[1] * 0.11;
- if (lum < 128) {
- return true;
- }
- return false;
- }
});
/**
@@ -4181,7 +4237,7 @@
'progressbar': 'instance.web.form.FieldProgressBar',
'image': 'instance.web.form.FieldBinaryImage',
'binary': 'instance.web.form.FieldBinaryFile',
- 'statusbar': 'instance.web.form.FieldStatus'
+ 'statusbar': 'instance.web.form.FieldStatus',
});
/**
=== modified file 'addons/web/static/src/xml/base.xml'
--- addons/web/static/src/xml/base.xml 2012-05-25 12:52:39 +0000
+++ addons/web/static/src/xml/base.xml 2012-05-30 15:50:31 +0000
@@ -1075,21 +1075,11 @@
</div>
</t>
<t t-name="FieldStatus.content">
- <ul class="oe-arrow-list">
- <t t-set="size" t-value="widget.to_show.length"/>
- <t t-foreach="_.range(size)" t-as="i">
- <li t-att-class="widget.to_show[i][0] === widget.selected_value ? 'oe-arrow-list-selected' : ''">
- <span class="oe-arrow-list-before" t-if="i > 0"></span><span><t t-esc="widget.to_show[i][1]"/></span><span class="oe-arrow-list-after" t-if="i < size - 1"></span>
- </li>
- </t>
- </ul>
-</t>
-<t t-name="FieldStatus.content">
<ul class="oe_form_steps">
<t t-set="size" t-value="widget.to_show.length"/>
<t t-foreach="_.range(size)" t-as="i">
<li t-att-class="widget.to_show[i][0] === widget.selected_value ? 'oe_form_steps_active' : ''">
- <t t-esc="widget.to_show[i][1]"/>
+ <span><t t-esc="widget.to_show[i][1]"/></span>
<img t-att-src='_s + "/web/static/src/img/form_steps.png"' class="oe_form_steps_arrow" t-if="i < size - 1"/>
</li>
</t>
_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help : https://help.launchpad.net/ListHelp