From e8a00bc50da435d3859118ca127d20a2eb9a0cab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Mon, 9 Mar 2015 16:34:27 +0100 Subject: [REF] web: add module system to the web client This commit introduces a new module system for the javascript code. Instead of using global (namespaced) variables everywhere (like openerp.web.whatever, ...), each file declare one (or more) module with its dependencies. Each module will be loaded after its dependencies, and will be given a reference to the return values. This change is a massive departure from the way the client worked previously. It will sadly break some/most history, but I am sure that the future git ecosystem will allow you to easily browse the history past this revision (one can dream). This commit feature: * main namespace is now odoo instead of openerp * a way to define modules and dependencies (inspired by requirejs) * many files have been splitted in simpler subfiles * basic support for debugging in odoo.__DEBUG__ * no global variable (i.e. nothing in window.openerp) * many bug fixes, many new bugs Future vision: this modularization should simplify future code development, simplify debugging and will allow a serious testing effort --- addons/web/static/src/js/widgets/user_menu.js | 112 ++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 addons/web/static/src/js/widgets/user_menu.js (limited to 'addons/web/static/src/js/widgets/user_menu.js') diff --git a/addons/web/static/src/js/widgets/user_menu.js b/addons/web/static/src/js/widgets/user_menu.js new file mode 100644 index 00000000000..450b0191486 --- /dev/null +++ b/addons/web/static/src/js/widgets/user_menu.js @@ -0,0 +1,112 @@ +odoo.define('web.UserMenu', ['web.core', 'web.Dialog', 'web.framework', 'web.Model', 'web.session', 'web.Widget'], function (require) { +"use strict"; + +var core = require('web.core'); +var Dialog = require('web.Dialog'); +var framework = require('web.framework'); +var Model = require('web.Model'); +var session = require('web.session'); +var Widget = require('web.Widget'); + +var _t = core._t; +var QWeb = core.qweb; + +var SystrayMenu = Widget.extend({ + template: "UserMenu", + init: function(parent) { + this._super(parent); + this.update_promise = $.Deferred().resolve(); + }, + start: function() { + var self = this; + this.$el.on('click', '.dropdown-menu li a[data-menu]', function(ev) { + ev.preventDefault(); + var f = self['on_menu_' + $(this).data('menu')]; + if (f) { + f($(this)); + } + }); + this.$el.parent().show(); + return this._super.apply(this, arguments); + }, + do_update: function () { + var self = this; + var fct = function() { + var $avatar = self.$el.find('.oe_topbar_avatar'); + $avatar.attr('src', $avatar.data('default-src')); + if (!session.uid) + return; + var func = new Model("res.users").get_func("read"); + return self.alive(func(session.uid, ["name", "company_id"])).then(function(res) { + var topbar_name = res.name; + if(session.debug) + topbar_name = _.str.sprintf("%s (%s)", topbar_name, session.db); + if(res.company_id[0] > 1) + topbar_name = _.str.sprintf("%s (%s)", topbar_name, res.company_id[1]); + self.$el.find('.oe_topbar_name').text(topbar_name); + if (!session.debug) { + topbar_name = _.str.sprintf("%s (%s)", topbar_name, session.db); + } + var avatar_src = session.url('/web/binary/image', {model:'res.users', field: 'image_small', id: session.uid}); + $avatar.attr('src', avatar_src); + + core.bus.trigger('resize'); // Re-trigger the reflow logic + }); + }; + this.update_promise = this.update_promise.then(fct, fct); + }, + on_menu_help: function() { + window.open('http://help.odoo.com', '_blank'); + }, + on_menu_logout: function() { + this.trigger('user_logout'); + }, + on_menu_settings: function() { + var self = this; + if (!this.getParent().has_uncommitted_changes()) { + self.rpc("/web/action/load", { action_id: "base.action_res_users_my" }).done(function(result) { + result.res_id = session.uid; + self.getParent().action_manager.do_action(result); + }); + } + }, + on_menu_account: function() { + if (!this.getParent().has_uncommitted_changes()) { + var P = new Model('ir.config_parameter'); + P.call('get_param', ['database.uuid']).then(function(dbuuid) { + var state = { + 'd': session.db, + 'u': window.location.protocol + '//' + window.location.host, + }; + var params = { + response_type: 'token', + client_id: dbuuid || '', + state: JSON.stringify(state), + scope: 'userinfo', + }; + framework.redirect('https://accounts.odoo.com/oauth2/auth?'+$.param(params)); + }).fail(function(result, ev){ + ev.preventDefault(); + framework.redirect('https://accounts.odoo.com/web'); + }); + } + }, + on_menu_about: function() { + var self = this; + self.rpc("/web/webclient/version_info", {}).done(function(res) { + var $help = $(QWeb.render("UserMenu.about", {version_info: res})); + $help.find('a.oe_activate_debug_mode').click(function (e) { + e.preventDefault(); + window.location = $.param.querystring( window.location.href, 'debug'); + }); + new Dialog(this, { + size: 'medium', + dialogClass: 'oe_act_window', + title: _t("About"), + }, $help).open(); + }); + }, +}); + +return SystrayMenu; +}); -- cgit v1.2.3