[WIP] broken state

bzr revid: fme@openerp.com-20120313155852-1d9fb2ky4pvlja8u
This commit is contained in:
Fabien Meghazi 2012-03-13 16:58:52 +01:00
parent 42f813f7dc
commit 91cf25ae07
2 changed files with 104 additions and 95 deletions

View File

@ -98,10 +98,9 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
} }
this.fields_order = []; this.fields_order = [];
this.fields_view = data; this.fields_view = data;
this.arch = this.preprocess_arch(data);
var html_view = openerp.web.xml_to_str(this.arch); var $form = this.create_view(data);
this.$element.find('.oe_form_content').append(html_view); this.$element.find('.oe_form_content').appendTo(this.$form);
//this.root_frame = instanciate_widget(this.registry.get_object('frame'), this, this.fields_view.arch); //this.root_frame = instanciate_widget(this.registry.get_object('frame'), this, this.fields_view.arch);
//var to_append = $(".oe_form_header", this.$element); //var to_append = $(".oe_form_header", this.$element);
@ -136,75 +135,64 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
} }
this.has_been_loaded.resolve(); this.has_been_loaded.resolve();
}, },
preprocess_arch: function(data) { create_view: function(fvg) {
// TODO: extract embeded views before preprocessing // TODO: extract embeded views before preprocessing
// Cut down this function into rendering API amongst form, page and kanban // Cut down this function into rendering API amongst form, page and kanban
// //
// OpenERP views spec : // OpenERP views spec :
// - @width is obsolete ? // - @width is obsolete ?
var self = this, var view = this,
baseclass = 'oe_layout_', fields = fvg.fields,
fields = data.fields, $form = $(fvg.arch_string)
arch = $.parseXML(data.arch_string); field_prefix = view.dataset ? view.dataset.model : '';
// Step #1: // Step #1:
// - If field has no @string, fetch it from fvg.fields. // - If field has no @string, fetch it from fvg.fields.
// - If field has no label, create one. // - If field has no label, create one.
// - Unless @nolabel, create unique @id and @for pair // - Unless @nolabel, create unique @id and @for pair
$(arch).find('field').each(function() { $form.find('field').each(function() {
var $field = $(this), var $field = $(this),
name = $field.attr('name'), name = $field.attr('name'),
field_orm = fields[name], field_orm = fields[name],
field_id = _.uniqueId([self.dataset.model, name, '#'].join('.')); field_id = _.uniqueId([field_prefix, name, '#'].join('.'));
if (!field_orm) { if (!field_orm) {
throw new Error("Field '" + name + "' specified in view could not be found."); throw new Error("Field '" + name + "' specified in view could not be found.");
} }
var $label = $(arch).find('label[for="' + name + '"]'), // TODO: manage @nolabel
var $label = $form.find('label[for="' + name + '"]'),
label_string = $field.attr('string') || field_orm.string || ''; label_string = $field.attr('string') || field_orm.string || '';
if ($label.length) { if ($label.length) {
label_string = $label.attr('string') || $label.text(); label_string = $label.attr('string') || $label.text();
} else { } else {
// TODO: find a way to prevent @xmlns.
$label = $('<label/>').insertBefore($field); $label = $('<label/>').insertBefore($field);
} }
$field.attr('id', field_id); $field.attr({
$label.attr('for', field_id).text(label_string); 'id' : field_id,
'data-widget' : $field.attr('widget') || field_orm.widget
// debug });
$field.text('INPUT: ' + name) $label.attr({
'for' : field_id,
'string' : label_string,
'help' : $field.attr('help') || field_orm.help || '',
'data-widget' : 'label'
});
// TODO: Add custom attribute in orer to differenciate from a real label
}); });
// Step #2: // TODO: modifiers invisible. Add a special attribute, eg: data-invisible that should be used in order to create openerp.form.InvisibleWidgetG
// - Convert groups to tables // TODO: split registry ? tags and fields types ?
var $group, $table, $tr, $td, $child, cols, row_cols, colspan;
while (true) { $form.find('*').each(function() {
$group = $(arch).find('group:first'); var $elem = $(this),
if (!$group.length) { key = $elem.attr('data-widget') || $elem[0].tagName.toLowerCase();
break; if (view.registry.contains(key)) {
var obj = view.registry.get_object(key);
var w = new (obj)(view, $elem[0]);
w.replace($elem);
} }
});
$table = $('<table border="1"/>').addClass(baseclass + 'group'); return $form
$tr = null;
cols = parseInt($group.attr('col') || 4, 10);
row_cols = cols;
$group.children().each(function() {
$child = $(this);
colspan = parseInt($child.attr('colspan') || 1, 10);
if ($child[0].tagName === 'newline') {
$tr = null;
return;
}
if (!$tr || row_cols < colspan) {
$tr = $('<tr/>').addClass(baseclass + 'group_row').appendTo($table);
row_cols = cols;
}
row_cols -= colspan;
$td = $('<td/>').addClass(baseclass + 'group_cell');
$tr.append($td.append($child));
});
$group.replaceWith($table);
}
// Step #3: // Step #3:
// - Process <button>'s // - Process <button>'s
@ -416,7 +404,7 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
var response = {}, can_process_onchange = $.Deferred(); var response = {}, can_process_onchange = $.Deferred();
processed = processed || []; processed = processed || [];
processed.push(widget.name); processed.push(widget.name);
var on_change = widget.node.attrs.on_change; var on_change = widget.node_attrs.on_change;
if (on_change) { if (on_change) {
var change_spec = self.parse_on_change(on_change, widget); var change_spec = self.parse_on_change(on_change, widget);
if (change_spec) { if (change_spec) {
@ -515,9 +503,9 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
} }
if (result.domain) { if (result.domain) {
function edit_domain(node) { function edit_domain(node) {
var new_domain = result.domain[node.attrs.name]; var new_domain = result.domain[node_attrs.name];
if (new_domain) { if (new_domain) {
node.attrs.domain = new_domain; node_attrs.domain = new_domain;
} }
_(node.children).each(edit_domain); _(node.children).each(edit_domain);
} }
@ -624,7 +612,7 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
var msg = "<ul>"; var msg = "<ul>";
_.each(this.fields, function(f) { _.each(this.fields, function(f) {
if (!f.is_valid()) { if (!f.is_valid()) {
msg += "<li>" + f.string + "</li>"; msg += "<li>" + f.node_attrs.string + "</li>";
} }
}); });
msg += "</ul>"; msg += "</ul>";
@ -762,7 +750,7 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
return { return {
name: name, name: name,
string: field.string, string: field.node_atts.string,
value: value, value: value,
displayed: displayed, displayed: displayed,
// convert undefined to false // convert undefined to false
@ -770,7 +758,7 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
} }
}) })
.compact() .compact()
.sortBy(function (field) { return field.string; }) .sortBy(function (field) { return field.node_atts.string; })
.value(); .value();
var conditions = _.chain(fields) var conditions = _.chain(fields)
.filter(function (field) { return field.change_default; }) .filter(function (field) { return field.change_default; })
@ -966,11 +954,10 @@ openerp.web.form.compute_domain = function(expr, fields) {
return _.all(stack, _.identity); return _.all(stack, _.identity);
}; };
openerp.web.form.Widget = openerp.web.OldWidget.extend(/** @lends openerp.web.form.Widget# */{ openerp.web.form.Widget = openerp.web.Widget.extend(/** @lends openerp.web.form.Widget# */{
form_template: 'Widget',
/** /**
* @constructs openerp.web.form.Widget * @constructs openerp.web.form.Widget
* @extends openerp.web.OldWidget * @extends openerp.web.Widget
* *
* @param view * @param view
* @param node * @param node
@ -978,32 +965,17 @@ openerp.web.form.Widget = openerp.web.OldWidget.extend(/** @lends openerp.web.fo
init: function(view, node) { init: function(view, node) {
this.view = view; this.view = view;
this.node = node; this.node = node;
this.modifiers = JSON.parse(this.node.attrs.modifiers || '{}'); this.node_attrs = {};
for (var attr, i = 0, attrs = this.node.attributes, l = attrs.length; i < l; i++) {
attr = attrs.item(i)
this.node_attrs[attr.nodeName] = attr.nodeValue;
}
this.modifiers = JSON.parse(this.node_attrs.modifiers || '{}');
this.always_invisible = (this.modifiers.invisible && this.modifiers.invisible === true); this.always_invisible = (this.modifiers.invisible && this.modifiers.invisible === true);
this.type = this.type || node.tag;
this.element_name = this.element_name || this.type;
this._super(view); this._super(view);
this.children = node.children;
this.colspan = parseInt(node.attrs.colspan || 1, 10);
this.decrease_max_width = 0;
this.string = this.string || node.attrs.string;
this.help = this.help || node.attrs.help;
this.invisible = this.modifiers['invisible'] === true; this.invisible = this.modifiers['invisible'] === true;
this.classname = 'oe_form_' + this.type;
this.align = parseFloat(this.node.attrs.align);
if (isNaN(this.align) || this.align === 1) {
this.align = 'right';
} else if (this.align === 0) {
this.align = 'left';
} else {
this.align = 'center';
}
this.width = this.node.attrs.width;
}, },
destroy: function() { destroy: function() {
this._super.apply(this, arguments); this._super.apply(this, arguments);
@ -1018,9 +990,6 @@ openerp.web.form.Widget = openerp.web.OldWidget.extend(/** @lends openerp.web.fo
update_dom: function() { update_dom: function() {
this.$element.toggle(!this.invisible); this.$element.toggle(!this.invisible);
}, },
renderElement: function() {
this.$element.html(QWeb.render(this.form_template, { "widget": this }));
},
do_attach_tooltip: function(widget, trigger, options) { do_attach_tooltip: function(widget, trigger, options) {
widget = widget || this; widget = widget || this;
trigger = trigger || this.$element; trigger = trigger || this.$element;
@ -1204,8 +1173,41 @@ openerp.web.form.WidgetFrame = openerp.web.form.Widget.extend({
} }
}); });
openerp.web.form.WidgetGroup = openerp.web.form.WidgetFrame.extend({ openerp.web.form.WidgetGroup = openerp.web.form.Widget.extend({
form_template: 'WidgetGroup' template: 'WidgetGroup',
init: function(view, node) {
this._super(view, node);
},
start: function() {
this._super();
var self = this,
$table;
if (this.$element.is('table')) {
$table = this.$element;
} else {
$table = this.$element.find('table:first');
}
var $tr, $td, $child, cols, row_cols, colspan;
$tr = null;
cols = parseInt(self.node_attrs('col') || 4, 10);
row_cols = cols;
$(node).children().each(function() {
$child = $(this);
colspan = parseInt($child.attr('colspan') || 1, 10);
if ($child[0].tagName.toLowerCase() === 'newline') {
$tr = null;
return;
}
if (!$tr || row_cols < colspan) {
$tr = $('<tr/>').addClass(baseclass + 'group_row').appendTo($table);
row_cols = cols;
}
row_cols -= colspan;
$td = $('<td/>').addClass(baseclass + 'group_cell');
$tr.append($td.append($child));
});
}
}), }),
openerp.web.form.WidgetNotebook = openerp.web.form.Widget.extend({ openerp.web.form.WidgetNotebook = openerp.web.form.Widget.extend({
@ -1299,15 +1301,12 @@ openerp.web.form.WidgetSeparator = openerp.web.form.Widget.extend({
}); });
openerp.web.form.WidgetButton = openerp.web.form.Widget.extend({ openerp.web.form.WidgetButton = openerp.web.form.Widget.extend({
form_template: 'WidgetButton', template: 'WidgetButton',
init: function(view, node) { init: function(view, node) {
this._super(view, node); this._super(view, node);
this.force_disabled = false; this.force_disabled = false;
if (this.string) { this.string = (this.node_attrs.string || '').replace(/_/g, '');
// We don't have button key bindings in the webclient if (this.node_attrs.default_focus == '1') {
this.string = this.string.replace(/_/g, '');
}
if (node.attrs.default_focus == '1') {
// TODO fme: provide enter key binding to widgets // TODO fme: provide enter key binding to widgets
this.view.default_focus_button = this; this.view.default_focus_button = this;
} }
@ -1318,6 +1317,7 @@ openerp.web.form.WidgetButton = openerp.web.form.Widget.extend({
if (this.help || openerp.connection.debug) { if (this.help || openerp.connection.debug) {
this.do_attach_tooltip(); this.do_attach_tooltip();
} }
debugger
}, },
on_click: function() { on_click: function() {
var self = this; var self = this;
@ -1395,6 +1395,15 @@ openerp.web.form.WidgetLabel = openerp.web.form.Widget.extend({
this._super(view, node); this._super(view, node);
this.align = parseFloat(this.node.attrs.align);
if (isNaN(this.align) || this.align === 1) {
this.align = 'right';
} else if (this.align === 0) {
this.align = 'left';
} else {
this.align = 'center';
}
if (this.node.tag == 'label' && !this.string && this.node.children.length) { if (this.node.tag == 'label' && !this.string && this.node.children.length) {
this.string = this.node.children[0]; this.string = this.node.children[0];
this.align = 'left'; this.align = 'left';

View File

@ -857,14 +857,14 @@
</t> </t>
</t> </t>
<t t-name="WidgetGroup"> <t t-name="WidgetGroup">
<t t-if="widget.string"> <t t-if="widget.node_attrs.string">
<fieldset class="oe_group_box"> <fieldset class="oe_group_box">
<legend><t t-esc="widget.string"/></legend> <legend><t t-esc="widget.node_attrs.string"/></legend>
<t t-call="WidgetFrame"/> <table border="1"/>
</fieldset> </fieldset>
</t> </t>
<t t-if="!widget.string"> <t t-if="!widget.node_attrs.string">
<t t-call="WidgetFrame"/> <table border="1"/>
</t> </t>
</t> </t>
<t t-name="WidgetNotebook"> <t t-name="WidgetNotebook">
@ -1209,7 +1209,7 @@
</t> </t>
<t t-name="WidgetButton"> <t t-name="WidgetButton">
<button type="button" class="oe_button"> <button type="button" class="oe_button">
<img t-if="widget.node.attrs.icon" t-att-src="_s + '/web/static/src/img/icons/' + widget.node.attrs.icon + '.png'" width="16" height="16"/> <img t-if="widget.node_attrs.icon" t-att-src="_s + '/web/static/src/img/icons/' + widget.node_attrs.icon + '.png'" width="16" height="16"/>
<span t-if="widget.string"><t t-esc="widget.string"/></span> <span t-if="widget.string"><t t-esc="widget.string"/></span>
</button> </button>
</t> </t>