[REF] web, *: move QWeb odoo init to web.QWeb

this js module centralizes all odoo initialization
This commit is contained in:
Géry Debongnie
2016-06-17 13:27:18 +02:00
parent 611322c36b
commit d56b35e26e
10 changed files with 97 additions and 100 deletions
@@ -27,9 +27,6 @@ var _t = core._t;
var QWeb = core.qweb;
var bus = core.bus;
var _ = require('_');
var $ = require('$');
function defaultIfUndef(variable, defaultValue) {
return variable === undefined ? defaultValue : variable;
}
+27 -5
View File
@@ -36,11 +36,7 @@
var job_deps = [];
var job_deferred = [];
var services = Object.create({
qweb: new QWeb2.Engine(),
$: $,
_: _,
});
var services = Object.create({});
var commentRegExp = /(\/\*([\s\S]*?)\*\/|([^:]|^)\/\/(.*)$)/mg;
var cjsRequireRegExp = /[^.]\s*require\s*\(\s*["']([^'"\s]+)["']\s*\)/g;
@@ -271,4 +267,30 @@
};
$(log_when_loaded);
// IE patch
//-------------------------------------------------------------------------
if (typeof(console) === "undefined") {
// Even IE9 only exposes console object if debug window opened
window.console = {};
('log error debug info warn assert clear dir dirxml trace group'
+ ' groupCollapsed groupEnd time timeEnd profile profileEnd count'
+ ' exception').split(/\s+/).forEach(function(property) {
console[property] = _.identity;
});
}
/**
Some hack to make placeholders work in ie9.
*/
if (!('placeholder' in document.createElement('input'))) {
document.addEventListener("DOMNodeInserted",function(event){
var nodename = event.target.nodeName.toLowerCase();
if ( nodename === "input" || nodename === "textarea" ) {
$(event.target).placeholder();
}
});
}
})();
+2 -59
View File
@@ -6,9 +6,7 @@ var mixins = require('web.mixins');
var Registry = require('web.Registry');
var translation = require('web.translation');
var qweb = require('qweb');
var _ = require('_');
var $ = require('$');
var QWeb = require('web.QWeb');
var debug = $.deparam($.param.querystring()).debug !== undefined;
@@ -37,31 +35,6 @@ _.each('resize,scroll'.split(','), function(evtype) {
});
});
// IE patch
//-------------------------------------------------------------------------
if (typeof(console) === "undefined") {
// Even IE9 only exposes console object if debug window opened
window.console = {};
('log error debug info warn assert clear dir dirxml trace group'
+ ' groupCollapsed groupEnd time timeEnd profile profileEnd count'
+ ' exception').split(/\s+/).forEach(function(property) {
console[property] = _.identity;
});
}
/**
Some hack to make placeholders work in ie9.
*/
if (!('placeholder' in document.createElement('input'))) {
document.addEventListener("DOMNodeInserted",function(event){
var nodename = event.target.nodeName.toLowerCase();
if ( nodename === "input" || nodename == "textarea" ) {
$(event.target).placeholder();
}
});
}
/**
* Lazy translation function, only performs the translation when actually
@@ -74,39 +47,9 @@ if (!('placeholder' in document.createElement('input'))) {
* @param {String} s string to translate
* @returns {Object} lazy translation object
*/
qweb.debug = debug;
_.extend(qweb.default_dict, {
'__debug__': debug,
'moment': function(date) { return new moment(date); },
});
var qweb = new QWeb(debug);
qweb.default_dict.csrf_token = odoo.csrf_token;
qweb.preprocess_node = function() {
// Note that 'this' is the Qweb Node
switch (this.node.nodeType) {
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
// Text and CDATAs
var translation = this.node.parentNode.attributes['t-translation'];
if (translation && translation.value === 'off') {
return;
}
var match = /^(\s*)([\s\S]+?)(\s*)$/.exec(this.node.data);
if (match) {
this.node.data = match[1] + _t(match[2]) + match[3];
}
break;
case Node.ELEMENT_NODE:
// Element
var attr, attrs = ['label', 'title', 'alt', 'placeholder'];
while ((attr = attrs.pop())) {
if (this.attributes[attr]) {
this.attributes[attr] = _t(this.attributes[attr]);
}
}
}
};
/** Setup jQuery timeago */
/*
@@ -0,0 +1,48 @@
odoo.define('web.QWeb', function (require) {
"use strict";
var translation = require('web.translation');
function QWeb(debug) {
var qweb = new QWeb2.Engine();
qweb.default_dict = {
'_' : _,
'JSON': JSON,
'_t' : translation._t,
'__debug__': debug,
'moment': function(date) { return new moment(date); },
'csrf_token': odoo.csrf_token,
};
qweb.debug = debug;
return qweb;
}
QWeb.prototype.preprocess_node = function() {
// Note that 'this' is the Qweb Node
switch (this.node.nodeType) {
case Node.TEXT_NODE:
case Node.CDATA_SECTION_NODE:
// Text and CDATAs
var translation = this.node.parentNode.attributes['t-translation'];
if (translation && translation.value === 'off') {
return;
}
var match = /^(\s*)([\s\S]+?)(\s*)$/.exec(this.node.data);
if (match) {
this.node.data = match[1] + _t(match[2]) + match[3];
}
break;
case Node.ELEMENT_NODE:
// Element
var attr, attrs = ['label', 'title', 'alt', 'placeholder'];
while ((attr = attrs.pop())) {
if (this.attributes[attr]) {
this.attributes[attr] = _t(this.attributes[attr]);
}
}
}
};
return QWeb;
});
@@ -3,8 +3,6 @@ odoo.define('web.translation', function (require) {
"use strict";
var Class = require('web.Class');
var qweb = require('qweb');
var TranslationDataBase = Class.extend(/** @lends instance.TranslationDataBase# */{
init: function() {
@@ -80,12 +78,6 @@ var _lt = function (s) {
return {toString: function () { return _t(s); }};
};
qweb.default_dict = {
'_' : _,
'JSON': JSON,
'_t' : _t,
};
return {
_t: _t,
_lt: _lt,
@@ -93,4 +85,3 @@ return {
};
});
+2 -7
View File
@@ -10,11 +10,7 @@ odoo.testing.start_services = function () {
deps: factory.deps,
};
});
var services = Object.create({
qweb: new QWeb2.Engine(),
$: $,
_: _,
});
var services = Object.create({});
return odoo.process_jobs(jobs, services);
};
@@ -95,7 +91,7 @@ odoo.define_section = function (name, section_deps) {
QUnit.test(name, function (assert) {
var services = odoo.testing.start_services();
var deps = _.map(section_deps.concat(dep_names), function (name) { return services[name]; });
services.qweb.add_template(odoo.testing.templates);
services['web.core'].qweb.add_template(odoo.testing.templates);
mock.clear();
mock.interceptRPC(services['web.session']);
var info = {
@@ -144,4 +140,3 @@ QUnit.moduleDone(function(result) {
}
});
+1
View File
@@ -67,6 +67,7 @@
<script type="text/javascript" src="/web/static/src/js/framework/model.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/dom_utils.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/utils.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/qweb.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/core.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/dialog.js"></script>
<script type="text/javascript" src="/web/static/src/js/framework/local_storage.js"></script>
+3 -2
View File
@@ -41,8 +41,9 @@
if(window['odoo']) {
odoo.define('web.csrf', function (require) {
var token = "<t t-esc="request.csrf_token(None)"/>";
require('web.core').csrf_token = token;
require('qweb').default_dict.csrf_token = token;
var core = require('web.core');
core.csrf_token = token;
core.qweb.default_dict.csrf_token = token;
});
}
</script>
+10 -13
View File
@@ -9,6 +9,7 @@ var Dialog = require('web.Dialog');
var form_common = require('web.form_common');
var Pager = require('web.Pager');
var pyeval = require('web.pyeval');
var QWeb = require('web.QWeb');
var session = require('web.session');
var utils = require('web.utils');
var View = require('web.View');
@@ -18,7 +19,7 @@ var quick_create = require('web_kanban.quick_create');
var KanbanRecord = require('web_kanban.Record');
var kanban_widgets = require('web_kanban.widgets');
var QWeb = core.qweb;
var qweb = core.qweb;
var _lt = core._lt;
var _t = core._t;
var ColumnQuickCreate = quick_create.ColumnQuickCreate;
@@ -59,10 +60,7 @@ var KanbanView = View.extend({
init: function () {
this._super.apply(this, arguments);
// qweb setup
this.qweb = new QWeb2.Engine();
this.qweb.debug = session.debug;
this.qweb.default_dict = _.clone(QWeb.default_dict);
this.qweb = new QWeb(session.debug);
this.limit = this.options.limit || 40;
this.fields = {};
@@ -323,7 +321,7 @@ var KanbanView = View.extend({
render_buttons: function($node) {
var self = this;
if (this.options.action_buttons !== false && this.is_action_enabled('create')) {
this.$buttons = $(QWeb.render("KanbanView.buttons", {'widget': this}));
this.$buttons = $(qweb.render("KanbanView.buttons", {'widget': this}));
this.$buttons.on('click', 'button.o-kanban-button-new', function () {
if (self.grouped && self.widgets.length && self.on_create === 'quick_create') {
// Activate the quick create in the first column
@@ -347,7 +345,6 @@ var KanbanView = View.extend({
$button_new.removeClass('btn-default').addClass('btn-primary');
});
}
this.$buttons.appendTo($node);
}
},
@@ -423,7 +420,7 @@ var KanbanView = View.extend({
},
render_no_content: function (fragment) {
var content = QWeb.render('KanbanView.nocontent', {content: this.no_content_msg});
var content = qweb.render('KanbanView.nocontent', {content: this.no_content_msg});
$(content).appendTo(fragment);
},
@@ -788,10 +785,10 @@ var KanbanView = View.extend({
});
function qweb_add_if(node, condition) {
if (node.attrs[QWeb.prefix + '-if']) {
condition = _.str.sprintf("(%s) and (%s)", node.attrs[QWeb.prefix + '-if'], condition);
if (node.attrs[qweb.prefix + '-if']) {
condition = _.str.sprintf("(%s) and (%s)", node.attrs[qweb.prefix + '-if'], condition);
}
node.attrs[QWeb.prefix + '-if'] = condition;
node.attrs[qweb.prefix + '-if'] = condition;
}
function transform_qweb_template (node, fvg, many2manys) {
@@ -815,8 +812,8 @@ function transform_qweb_template (node, fvg, many2manys) {
} else if (fields_registry.contains(ftype)) {
// do nothing, the kanban record will handle it
} else {
node.tag = QWeb.prefix;
node.attrs[QWeb.prefix + '-esc'] = 'record.' + node.attrs.name + '.value';
node.tag = qweb.prefix;
node.attrs[qweb.prefix + '-esc'] = 'record.' + node.attrs.name + '.value';
}
break;
case 'button':
@@ -1,11 +1,13 @@
odoo.define('website_mail.thread', function(require) {
'use strict';
var web_editor_base = require('web_editor.base')
var qweb = require('qweb');
var web_editor_base = require('web_editor.base');
var ajax = require('web.ajax');
var core = require('web.core');
var Widget = require('web.Widget');
var qweb = core.qweb;
// load qweb template
ajax.loadXML('/website_mail/static/src/xml/chatter_message.xml', qweb);