Refactoring

bzr revid: fme@openerp.com-20110317172508-fb1yxc1bcunqm2gt
This commit is contained in:
Fabien Meghazi 2011-03-17 18:25:08 +01:00
parent c27fc11c2f
commit 8daad6a2ec
2 changed files with 102 additions and 140 deletions

View File

@ -94,46 +94,6 @@
</div>
<table id="todo_use_unique_id" class="jqGrid"></table>
</t>
<t t-name="FormViewold">
<h3 class="title"><t t-esc="fields_view.arch.attrs.string"/></h3>
<div style="text-align:right;">
<input type="button" value="First"/>
<input type="button" value="&lt;&lt;"/>
0 / 0
<input type="button" value="&gt;&gt;"/>
<input type="button" value="Last"/>
</div>
<t t-call="FormView.node">
<t t-set="node" t-value="fields_view.arch"/>
</t>
<div style="text-align:right;">
<input type="button" value="Save"/>
</div>
</t>
<t t-name="FormView.node">
<!--
tag: <t t-esc="node.tag"/> <t t-esc="node['@name']"/> <br/>
<t t-if="node.tag == 'filter'">
<input type="button" t-att-value="node['@string']"/>
<t t-esc="node['@name']"/>
<br/>
</t>
<t t-if="node.tag == 'separator'">
<hr/>
<t t-esc="node['@name']"/>
<t t-esc="node['@string']"/>
</t>
-->
<t t-if="node.tag == 'field'">
<t t-esc="node.attrs.string"/>
<t t-esc="node.attrs.name"/>
<input type="text" t-att-name="node.attrs.name"/>
<br/>
</t>
<t t-foreach="node.children || []" t-as="node">
<t t-call="FormView.node"/>
</t>
</t>
<t t-name="FormView">
<h3 class="title"><t t-esc="view.fields_view.arch.attrs.string"/></h3>
<div style="text-align:right;">
@ -148,11 +108,11 @@
<input type="button" value="Save"/>
</div>
</t>
<t t-name="FormView.widget">
<t t-name="Widget">
Unhandled widget
<t t-raw="console.log('Unhandled widget', widget)"/>
</t>
<t t-name="FormView.frame">
<t t-name="WidgetFrame">
<table border="0" width="100%" cellpadding="2" cellspacing="2">
<tr t-foreach="widget.table" t-as="row">
<t t-foreach="row" t-as="td">
@ -166,7 +126,7 @@
</tr>
</table>
</t>
<t t-name="FormView.notebook">
<t t-name="WidgetNotebook">
<t t-foreach="widget.pages" t-as="page">
<fieldset>
<legend><t t-esc="page.string"/></legend>
@ -174,31 +134,31 @@
</fieldset>
</t>
</t>
<t t-name="FormView.separator">
<t t-name="WidgetSeparator">
<div t-att-class="'separator ' + (widget.node.attrs.orientation || 'horizontal')">
<t t-esc="widget.string"/>
</div>
</t>
<t t-name="FormView.label">
<label t-att-for="widget.for.element_id" t-att-title="widget.help" style="display: block; text-align: right;" t-att-ondblclick="'console.log(openerp.screen.oe_main.formview.widgets[\'' + widget.element_id + '\'])'">
<t t-esc="widget.string"/>
<span t-if="widget.help">?</span>
<t t-name="WidgetLabel">
<label t-att-for="widget.for.element_id" t-att-title="widget.for.help" style="display: block; text-align: right;" t-att-ondblclick="'console.log(openerp.screen.oe_main.formview.widgets[\'' + widget.for.element_id + '\'])'">
<t t-esc="widget.for.string"/>
<span t-if="widget.for.help">?</span>
:
</label>
</t>
<t t-name="FormView.field.char">
<t t-name="FieldChar">
<input type="text" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%"/>
</t>
<t t-name="FormView.field.text">
<t t-name="FieldText">
<textarea rows="6" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%;"></textarea>
</t>
<t t-name="FormView.field.date">
<t t-name="FieldDate">
<input type="text" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%" placeholder="Widget Date"/>
</t>
<t t-name="FormView.field.datetime">
<t t-name="FieldDatetime">
<input type="text" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%" placeholder="Widget Datetime"/>
</t>
<t t-name="FormView.field.selection">
<t t-name="FieldSelection">
<select t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%">
<t t-foreach="widget.field.selection" t-as="options">
<option t-att-value="options[0]">
@ -207,21 +167,21 @@
</t>
</select>
</t>
<t t-name="FormView.field.many2one">
<t t-name="FieldMany2One">
<input type="text" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%; border: 1px solid red;" placeholder="Many 2 One: Please, enter the ID of the object. Thank you !"/>
</t>
<t t-name="FormView.field.one2many">
<t t-name="FieldOne2Many">
<div style="background: #ccc; padding: 20px">
One2Many widget
</div>
</t>
<t t-name="FormView.field.reference">
<t t-name="FieldReference">
<input type="text" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type" style="width: 100%" placeholder="Widget Reference"/>
</t>
<t t-name="FormView.field.boolean">
<t t-name="FieldBoolean">
<input type="checkbox" t-att-name="widget.name" t-att-id="widget.element_id" t-att-class="'field_' + widget.type"/>
</t>
<t t-name="FormView.button">
<t t-name="WidgetButton">
<button type="button" t-att-id="widget.element_id" t-att-title="widget.help" style="width: 100%" class="button">
<img t-if="widget.node.attrs.icon" t-att-src="'/base/static/openerp/img/icons/' + widget.node.attrs.icon + '.png'" width="16" height="16"/>
<t t-esc="widget.string"/>

View File

@ -223,6 +223,7 @@ openerp.base.FormView = openerp.base.Controller.extend({
this.view_id = view_id;
this.fields_views = {};
this.widgets = {};
this.widgets_counter = 0;
this.fields = {};
this.datarecord = {};
},
@ -234,7 +235,7 @@ openerp.base.FormView = openerp.base.Controller.extend({
this.fields_view = data.fields_view;
//this.log(this.fields_view);
var frame = new openerp.base.WidgetFrame(this.session, null, this, this.fields_view.arch);
var frame = new openerp.base.WidgetFrame(this, this.fields_view.arch);
this.$element.html(QWeb.render("FormView", { "frame": frame, "view": this }));
for (var i in this.widgets) {
@ -259,7 +260,7 @@ openerp.base.FormView = openerp.base.Controller.extend({
},
on_record_loaded: function() {
for (var f in this.fields) {
this.fields[f].update_from_datarecord()
this.fields[f].set_value()
}
},
});
@ -345,31 +346,25 @@ openerp.base.TreeView = openerp.base.Controller.extend({
openerp.base.Widget = openerp.base.Controller.extend({
// TODO Change this to init: function(view, node) { and use view.session and a new element_id for the super
// it means that widgets are special controllers
init: function(session, element_id, view, node) {
var type = view.fields_view.fields[node.attrs.name] || {};
this.type = node.attrs.widget || type.type || node.tag;
this.name = node.attrs.name;
var type = view.fields_view.fields[node.attrs.name] || {};
this.type = node.attrs.widget || type.type || node.tag;
this.element_id = (node.tag == this.type ? node.tag : node.tag + '_' + this.type) + '_';
this.element_id += (this.name ? this.name + (this.is_field_label ? '_label' : '') + '_' : '');
this.element_id += Math.round(Math.random() * (new Date()).getTime());
this._super(session, this.element_id);
init: function(view, node) {
this.view = view;
this.view.widgets[this.element_id] = this;
this.node = node;
this.type = this.type || node.tag;
this.element_name = this.element_name || this.type;
this.element_id = [this.view.element_id, this.element_name, this.view.widgets_counter++].join("_");
this._super(this.view.session, this.element_id);
this.view.widgets[this.element_id] = this;
this.children = node.children;
this.colspan = parseInt(node.attrs.colspan || 1);
this.field = view.fields_view.fields[node.attrs.name];
this.template = "FormView.widget";
this.template = "Widget";
this.string = this.string || node.attrs.string;
this.help = this.help || node.attrs.help;
this.invisible = (node.attrs.invisible == '1');
this.string = node.attrs.string || (this.field ? this.field.string : undefined);
this.help = node.attrs.help || (this.field ? this.field.help : undefined);
this.nolabel = (node.attrs.nolabel == '1');
},
start: function() {
console.log("jquery", this.element_id)
this.$element = $('#' + this.element_id);
},
render: function() {
@ -379,9 +374,9 @@ openerp.base.Widget = openerp.base.Controller.extend({
});
openerp.base.WidgetFrame = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.frame";
init: function(view, node) {
this._super(view, node);
this.template = "WidgetFrame";
this.columns = node.attrs.col || 4;
this.x = 0;
this.y = 0;
@ -424,15 +419,15 @@ openerp.base.WidgetFrame = openerp.base.Widget.extend({
var type = this.view.fields_view.fields[n.attrs.name] || {};
var widget_type = n.attrs.widget || type.type || n.tag;
if (openerp.base.widgets[widget_type]) {
var widget = new openerp.base.widgets[widget_type](this.session, null, this.view, n);
var widget = new openerp.base.widgets[widget_type](this.view, n);
if (n.tag == 'field' && n.attrs.nolabel != '1') {
var label = new openerp.base.widgets['label'](this.session, null, this.view, n);
var label = new openerp.base.widgets['label'](this.view, n);
label["for"] = widget;
this.add_widget(label);
}
this.add_widget(widget);
} else {
console.log("Unhandled widget type : " + widget_type, n);
this.log("Unhandled widget type : " + widget_type, n);
}
},
add_widget: function(w) {
@ -449,14 +444,14 @@ openerp.base.WidgetFrame = openerp.base.Widget.extend({
});
openerp.base.WidgetNotebook = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.notebook";
init: function(view, node) {
this._super(view, node);
this.template = "WidgetNotebook";
this.pages = [];
for (var i = 0; i < node.children.length; i++) {
var n = node.children[i];
if (n.tag == "page") {
var page = new openerp.base.WidgetFrame(this.session, null, this.view, n);
var page = new openerp.base.WidgetFrame(this.view, n);
this.pages.push(page);
}
}
@ -464,53 +459,60 @@ openerp.base.WidgetNotebook = openerp.base.Widget.extend({
});
openerp.base.WidgetSeparator = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.separator";
init: function(view, node) {
this._super(view, node);
this.template = "WidgetSeparator";
}
});
openerp.base.WidgetLabel = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
init: function(view, node) {
this.is_field_label = true;
this._super(session, element_id, view, node);
this.template = "FormView.label";
this._super(view, node);
this.template = "WidgetLabel";
this.colspan = 1;
}
});
openerp.base.WidgetButton = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.button";
init: function(view, node) {
this._super(view, node);
this.template = "WidgetButton";
}
});
openerp.base.Field = openerp.base.Widget.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.view.fields[node.attrs.name] = this;
init: function(view, node) {
this.name = node.attrs.name;
view.fields[this.name] = this;
this.type = node.attrs.widget || view.fields_view.fields[node.attrs.name].type;
this.element_name = "field_" + this.name + "_" + this.type;
this._super(view, node);
if (node.attrs.nolabel != '1' && this.colspan > 1) {
this.colspan--;
}
// this.datarecord = this.view.datarecord ??
},
update_from_datarecord: function() {
this.set_value(); // ????
this.field = view.fields_view.fields[node.attrs.name];
this.string = node.attrs.string || this.field.string;
this.help = node.attrs.help || this.field.help;
this.nolabel = (node.attrs.nolabel == '1');
},
set_value: function() {
}
});
openerp.base.FieldChar = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.char";
init: function(view, node) {
this._super(view, node);
this.template = "FieldChar";
},
start: function() {
this._super.apply(this, arguments);
// this.$element.bind('leaving_focus',)
// this.$element.bind('change',) ... blur, focus, ...
},
set_value: function() {
this.$element.val(this.view.datarecord.values[this.name]);
@ -521,44 +523,44 @@ openerp.base.FieldChar = openerp.base.Field.extend({
});
openerp.base.FieldEmail = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.char";
init: function(view, node) {
this._super(view, node);
this.template = "FieldEmail";
}
});
openerp.base.FieldFloat = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.char";
init: function(view, node) {
this._super(view, node);
this.template = "FieldChar";
}
});
openerp.base.FieldBoolean = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.boolean";
init: function(view, node) {
this._super(view, node);
this.template = "FieldBoolean";
}
});
openerp.base.FieldDate = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.date";
init: function(view, node) {
this._super(view, node);
this.template = "FieldDate";
}
});
openerp.base.FieldDatetime = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.datetime";
init: function(view, node) {
this._super(view, node);
this.template = "FieldDatetime";
}
});
openerp.base.FieldText = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.text";
init: function(view, node) {
this._super(view, node);
this.template = "FieldText";
}
});
@ -567,37 +569,37 @@ openerp.base.FieldTextXml = openerp.base.Field.extend({
});
openerp.base.FieldSelection = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.selection";
init: function(view, node) {
this._super(view, node);
this.template = "FieldSelection";
}
});
openerp.base.FieldMany2One = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.many2one";
init: function(view, node) {
this._super(view, node);
this.template = "FieldMany2One";
}
});
openerp.base.FieldOne2Many = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.one2many";
init: function(view, node) {
this._super(view, node);
this.template = "FieldOne2Many";
}
});
openerp.base.FieldMany2Many = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.one2many";
init: function(view, node) {
this._super(view, node);
this.template = "FieldMany2Many";
}
});
openerp.base.FieldReference = openerp.base.Field.extend({
init: function(session, element_id, view, node) {
this._super(session, element_id, view, node);
this.template = "FormView.field.reference";
init: function(view, node) {
this._super(view, node);
this.template = "FieldReference";
}
});