[IMP] Add QWeb preprocess_node callback. Translate @label, @title and @alt attributes in web client

bzr revid: fme@openerp.com-20120207132901-ifwnfj6c4mp451tl
This commit is contained in:
Fabien Meghazi 2012-02-07 14:29:01 +01:00
parent b5579eee20
commit ce65bc3ac3
2 changed files with 36 additions and 18 deletions

View File

@ -55,7 +55,7 @@ var QWeb2 = {
if (o !== null && o !== undefined) {
if (o.constructor === Array) {
if (o[1] !== null && o[1] !== undefined) {
return ' ' + o[0] + '="' + this.html_escape(o[1], true) + '"';
return this.format_attribute(o[0], o[1]);
}
} else if (typeof o === 'object') {
var r = '';
@ -69,6 +69,9 @@ var QWeb2 = {
}
return '';
},
format_attribute: function(name, value) {
return ' ' + name + '="' + this.html_escape(value, true) + '"';
},
extend: function(dst, src, exclude) {
for (var p in src) {
if (src.hasOwnProperty(p) && !(exclude && this.arrayIndexOf(exclude, p) !== -1)) {
@ -193,7 +196,7 @@ QWeb2.Engine = (function() {
this.reserved_words = QWeb2.RESERVED_WORDS.slice(0);
this.actions_precedence = QWeb2.ACTIONS_PRECEDENCE.slice(0);
this.word_replacement = QWeb2.tools.extend({}, QWeb2.WORD_REPLACEMENT);
this.format_text_node = null;
this.preprocess_node = null;
for (var i = 0; i < arguments.length; i++) {
this.add_template(arguments[i]);
}
@ -437,6 +440,9 @@ QWeb2.Element = (function() {
}
}
}
if (this.engine.preprocess_node) {
this.engine.preprocess_node.call(this);
}
}
QWeb2.tools.extend(Element.prototype, {
@ -472,11 +478,7 @@ QWeb2.Element = (function() {
switch (this.node.nodeType) {
case 3:
case 4:
var text = this.node.data;
if (this.engine.format_text_node) {
text = this.engine.format_text_node.call(this, text);
}
this.top_string(text);
this.top_string(this.node.data);
break;
case 1:
this.compile_element();

View File

@ -1233,18 +1233,34 @@ openerp.web.qweb.default_dict = {
'_' : _,
'_t' : openerp.web._t
};
openerp.web.qweb.format_text_node = function (s) {
// Note that 'this' is the Qweb Node of the text
var translation = this.node.parentNode.attributes['t-translation'];
if (translation && translation.value === 'off') {
return s;
openerp.web.qweb.preprocess_node = function() {
// Note that 'this' is the Qweb Node
switch (this.node.nodeType) {
case 3:
case 4:
// Text and CDATAs
var translation = this.node.parentNode.attributes['t-translation'];
if (translation && translation.value === 'off') {
return;
}
var ts = _.str.trim(this.node.data);
if (ts.length === 0) {
return;
}
var tr = openerp.web._t(ts);
if (tr !== ts) {
this.node.data = tr;
}
break;
case 1:
// Element
var attr, attrs = ['label', 'title', 'alt'];
while (attr = attrs.pop()) {
if (this.attributes[attr]) {
this.attributes[attr] = openerp.web._t(this.attributes[attr]);
}
}
}
var ts = _.str.trim(s);
if (ts.length === 0) {
return s;
}
var tr = openerp.web._t(ts);
return tr === ts ? s : tr;
};
/** Jquery extentions */