From d56b35e26e4b578c5f6f434d336edd006d321860 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 16 Jun 2016 13:19:15 +0200 Subject: [PATCH] [REF] web, *: move QWeb odoo init to web.QWeb this js module centralizes all odoo initialization --- .../src/js/account_reconciliation_widgets.js | 3 - addons/web/static/src/js/boot.js | 32 ++++++++-- addons/web/static/src/js/framework/core.js | 61 +------------------ addons/web/static/src/js/framework/qweb.js | 48 +++++++++++++++ .../static/src/js/framework/translation.js | 9 --- addons/web/static/test/testing.js | 9 +-- addons/web/views/webclient_templates.xml | 1 + addons/web_editor/views/iframe.xml | 5 +- .../web_kanban/static/src/js/kanban_view.js | 23 +++---- .../static/src/js/message_post.js | 6 +- 10 files changed, 97 insertions(+), 100 deletions(-) create mode 100644 addons/web/static/src/js/framework/qweb.js diff --git a/addons/account/static/src/js/account_reconciliation_widgets.js b/addons/account/static/src/js/account_reconciliation_widgets.js index 5491f2fddee..cfd21ea7987 100644 --- a/addons/account/static/src/js/account_reconciliation_widgets.js +++ b/addons/account/static/src/js/account_reconciliation_widgets.js @@ -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; } diff --git a/addons/web/static/src/js/boot.js b/addons/web/static/src/js/boot.js index 2cc4abe29bb..f070730886d 100644 --- a/addons/web/static/src/js/boot.js +++ b/addons/web/static/src/js/boot.js @@ -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(); + } + }); + } + + })(); diff --git a/addons/web/static/src/js/framework/core.js b/addons/web/static/src/js/framework/core.js index 8f0453151e8..29b461cbe8d 100644 --- a/addons/web/static/src/js/framework/core.js +++ b/addons/web/static/src/js/framework/core.js @@ -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 */ /* diff --git a/addons/web/static/src/js/framework/qweb.js b/addons/web/static/src/js/framework/qweb.js new file mode 100644 index 00000000000..b29f0f06d65 --- /dev/null +++ b/addons/web/static/src/js/framework/qweb.js @@ -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; + +}); diff --git a/addons/web/static/src/js/framework/translation.js b/addons/web/static/src/js/framework/translation.js index 69743e9787d..5d747869935 100644 --- a/addons/web/static/src/js/framework/translation.js +++ b/addons/web/static/src/js/framework/translation.js @@ -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 { }; }); - diff --git a/addons/web/static/test/testing.js b/addons/web/static/test/testing.js index 4fea9f9337b..a5f85a23029 100644 --- a/addons/web/static/test/testing.js +++ b/addons/web/static/test/testing.js @@ -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) { } }); - diff --git a/addons/web/views/webclient_templates.xml b/addons/web/views/webclient_templates.xml index f14e5cd2f57..c9b268ae55a 100644 --- a/addons/web/views/webclient_templates.xml +++ b/addons/web/views/webclient_templates.xml @@ -67,6 +67,7 @@ + diff --git a/addons/web_editor/views/iframe.xml b/addons/web_editor/views/iframe.xml index 6d44ba43b7f..24983557818 100644 --- a/addons/web_editor/views/iframe.xml +++ b/addons/web_editor/views/iframe.xml @@ -41,8 +41,9 @@ if(window['odoo']) { odoo.define('web.csrf', function (require) { var token = ""; - 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; }); } diff --git a/addons/web_kanban/static/src/js/kanban_view.js b/addons/web_kanban/static/src/js/kanban_view.js index eb9c7e99b03..0f243e20cdf 100644 --- a/addons/web_kanban/static/src/js/kanban_view.js +++ b/addons/web_kanban/static/src/js/kanban_view.js @@ -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': diff --git a/addons/website_mail/static/src/js/message_post.js b/addons/website_mail/static/src/js/message_post.js index 3ddb5e2ce90..2e93e8708a0 100644 --- a/addons/website_mail/static/src/js/message_post.js +++ b/addons/website_mail/static/src/js/message_post.js @@ -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);