From 5ddf3688f91d4cc919f2ac6c8f5ec350452bf7ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Todorovich?= Date: Mon, 12 Feb 2018 19:58:01 -0300 Subject: [PATCH] Fix 'Add to Dashboard Tile' menu (MIG 9.0) --- web_dashboard_tile/static/src/css/tile.css | 15 +- web_dashboard_tile/static/src/js/custom_js.js | 188 +++++++++++------- .../static/src/xml/custom_xml.xml | 25 +-- 3 files changed, 133 insertions(+), 95 deletions(-) diff --git a/web_dashboard_tile/static/src/css/tile.css b/web_dashboard_tile/static/src/css/tile.css index 623a2813..7f9b61d5 100644 --- a/web_dashboard_tile/static/src/css/tile.css +++ b/web_dashboard_tile/static/src/css/tile.css @@ -5,8 +5,8 @@ } .o_kanban_view.o_kanban_ungrouped .o_kanban_record.oe_dashboard_tile { - /* override max-width rules */ - margin: 4px !important; + /* override max-width rules */ + margin: 4px !important; /* flex width */ -ms-flex: 1 1 150px; -moz-flex: 1 1 150px; @@ -53,11 +53,8 @@ display: block; } -/* SearchView Drawer */ -.oe_searchview_drawer .oe_searchview_dashboard .oe_dashboard_tile_form { - display: none; -} - -.oe_searchview_drawer .oe_opened .oe_dashboard_tile_form { - display: block; +/* Favorites menu in control panel */ +.o_add_to_dashboard_tile { + /* hidden by default */ + display: none; } diff --git a/web_dashboard_tile/static/src/js/custom_js.js b/web_dashboard_tile/static/src/js/custom_js.js index 615a74d6..66094df8 100644 --- a/web_dashboard_tile/static/src/js/custom_js.js +++ b/web_dashboard_tile/static/src/js/custom_js.js @@ -3,6 +3,7 @@ // // Copyright (C) 2010-2013 OpenERP s.a. () // Copyright (C) 2014 initOS GmbH & Co. KG () +// Copyright (C) 2018 Iván Todorovich () // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Affero General Public License as published @@ -19,83 +20,122 @@ // //############################################################################# -odoo.web_dashboard_tile = function (require) -{ -var QWeb = require('web.qweb') -var _t = require('web._t') -var _lt = require('web._lt') +odoo.define('web_dashboard_tile', function (require) { +"use strict"; -_.mixin({ - sum: function (obj) { return _.reduce(obj, function (a, b) { return a + b; }, 0); } -}); - var module = instance.board.AddToDashboard; - - module.include({ - start: function () { - this._super(); - var self = this; - this.$('#add_dashboard_tile').on('click', this, function (){ - self.save_tile(); +var core = require('web.core'); +var data = require('web.data'); +var FavoriteMenu = require('web.FavoriteMenu'); +var ActionManager = require('web.ActionManager'); +var ViewManager = require('web.ViewManager'); +var Model = require('web.DataModel'); +var session = require('web.session'); +var pyeval = require('web.pyeval'); +var _t = core._t; +var QWeb = core.qweb; + + +FavoriteMenu.include({ + + prepare_dropdown_menu: function (filters) { + var self = this; + this._super(filters); + var am = this.findAncestor(function (a) { + return a instanceof ActionManager; + }); + if (am && am.get_inner_widget() instanceof ViewManager) { + this.view_manager = am.get_inner_widget(); + this.add_to_dashboard_tile_available = true; + this.$('.o_favorites_menu').append(QWeb.render('SearchView.addtodashboardtile')); + this.$add_to_dashboard_tile = this.$('.o_add_to_dashboard_tile'); + this.$add_dashboard_tile_btn = this.$add_to_dashboard_tile.eq(1).find('button'); + this.$add_dashboard_tile_input = this.$add_to_dashboard_tile.eq(0).find('input'); + this.$add_dashboard_tile_link = this.$('.o_add_to_dashboard_tile_link'); + var title = this.searchview.get_title(); + this.$add_dashboard_tile_input.val(title); + this.$add_dashboard_tile_link.click(function (e) { + e.preventDefault(); + self.toggle_dashboard_tile_menu(); }); - }, - render_data: function(dashboard_choices){ - var selection = instance.web.qweb.render( - "SearchView.addtodashboard.selection", { - selections: dashboard_choices}); - this.$("form input").before(selection); - }, - save_tile: function () { - var self = this; - var view_parent = this.getParent().getParent(); - - var $name = this.$('#dashboard_tile_new_name'); - - this.tile = new instance.web.Model('tile.tile'); - - var private_filter = !this.$('#oe_searchview_custom_public').prop('checked'); - if (_.isEmpty($name.val())){ - this.do_warn(_t("Error"), _t("Filter name is required.")); - return false; - } - var search = this.view.build_search_data(); - var context = new instance.web.CompoundContext(view_parent.dataset.get_context() || []); - var domain = new instance.web.CompoundDomain(view_parent.dataset.get_domain() || []); - _.each(search.contexts, context.add, context); - _.each(search.domains, domain.add, domain); - - var c = instance.web.pyeval.eval('context', context); - for(var k in c) { - if (c.hasOwnProperty(k) && /^search_default_/.test(k)) { - delete c[k]; - } + this.$add_dashboard_tile_btn.click(this.proxy('add_dashboard_tile')); + } + }, + + toggle_dashboard_tile_menu: function (is_open) { + this.$add_dashboard_tile_link + .toggleClass('o_closed_menu', !(_.isUndefined(is_open)) ? !is_open : undefined) + .toggleClass('o_open_menu', is_open); + this.$add_to_dashboard_tile.toggle(is_open); + if (this.$add_dashboard_tile_link.hasClass('o_open_menu')) { + this.$add_dashboard_tile_input.focus(); + } + }, + + close_menus: function () { + if (this.add_to_dashboard_tile_available) { + this.toggle_dashboard_tile_menu(false); + } + this._super(); + }, + + add_dashboard_tile: function () { + var self = this; + + var search_data = this.searchview.build_search_data(), + context = new data.CompoundContext(this.searchview.dataset.get_context() || []), + domain = new data.CompoundDomain(this.searchview.dataset.get_domain() || []); + _.each(search_data.contexts, context.add, context); + _.each(search_data.domains, domain.add, domain); + + context.add({ + group_by: pyeval.eval('groupbys', search_data.groupbys || []) + }); + + context.add(this.view_manager.active_view.controller.get_context()); + + var c = pyeval.eval('context', context); + for(var k in c) { + if (c.hasOwnProperty(k) && /^search_default_/.test(k)) { + delete c[k]; } - // TODO: replace this 6.1 workaround by attribute on - c.dashboard_merge_domains_contexts = false; - var d = instance.web.pyeval.eval('domain', domain); + } - context.add({ - group_by: instance.web.pyeval.eval('groupbys', search.groupbys || []) - }); - // Don't save user_context keys in the custom filter, otherwise end - // up with e.g. wrong uid or lang stored *and used in subsequent - // reqs* - var ctx = context; - _(_.keys(instance.session.user_context)).each(function (key) { - delete ctx[key]; - }); - var filter = { - name: $name.val(), - user_id: private_filter ? instance.session.uid : false, - model_id: self.view.model, - //context: context, - domain: d, - action_id: view_parent.action.id, - }; - // FIXME: current context? - return self.tile.call('add', [filter]).done(function (id) { - self.do_warn(_t("Success"), _t("Tile is created")); - }); + this.toggle_dashboard_tile_menu(false); + + c.dashboard_merge_domains_contexts = false; + var d = pyeval.eval('domain', domain), + tile = new Model('tile.tile'), + name = self.$add_dashboard_tile_input.val(); + var private_filter = !this.$('#oe_searchview_custom_public').prop('checked'); + if (_.isEmpty(name)){ + this.do_warn(_t("Error"), _t("Filter name is required.")); + return false; } - }); -}; + + // Don't save user_context keys in the custom filter, otherwise end + // up with e.g. wrong uid or lang stored *and used in subsequent + // reqs* + var ctx = context; + _(_.keys(session.user_context)).each(function (key) { + delete ctx[key]; + }); + + var vals = { + name: name, + user_id: private_filter ? session.uid : false, + model_id: self.view_manager.active_view.controller.model, + //context: context, + domain: d, + action_id: self.action_id || false, + }; + + // FIXME: current context? + return tile.call('add', [vals]).done(function (id) { + self.do_notify(_t("Success"), _t("Tile is created")); + }); + }, +}); + +}); + diff --git a/web_dashboard_tile/static/src/xml/custom_xml.xml b/web_dashboard_tile/static/src/xml/custom_xml.xml index 38a0e3e9..ba60a244 100644 --- a/web_dashboard_tile/static/src/xml/custom_xml.xml +++ b/web_dashboard_tile/static/src/xml/custom_xml.xml @@ -1,12 +1,13 @@ - - - - -
- - - -
-
-
-
+