Merge lp:~therp-nl/web-addons/7.0-add_autoswitch into lp:~webaddons-core-editors/web-addons/7.0

Proposed by Stefan Rijnhart (Opener) on 2013-11-08
Status: Work in progress
Proposed branch: lp:~therp-nl/web-addons/7.0-add_autoswitch
Merge into: lp:~webaddons-core-editors/web-addons/7.0
Diff against target: 187 lines (+173/-0)
2 files modified
web_autoswitch_to_form/__openerp__.py (+56/-0)
web_autoswitch_to_form/static/src/js/web_autoswitch_to_form.js (+117/-0)
To merge this branch: bzr merge lp:~therp-nl/web-addons/7.0-add_autoswitch
Reviewer Review Type Date Requested Status
Web-Addons Core Editors 2013-11-08 Pending
Review via email: mp+194539@code.launchpad.net

Description of the change

Experimental, don't merge yet.

To post a comment you must log in.
18. By Stefan Rijnhart (Opener) on 2013-11-08

[RFR] Additional line break

19. By Stefan Rijnhart (Opener) on 2013-11-08

[IMP] Variable name

20. By Stefan Rijnhart (Opener) on 2013-11-08

[RFR] Comments

21. By Stefan Rijnhart (Opener) on 2013-11-08

[ADD] Example XML stanza

22. By Stefan Rijnhart (Opener) on 2013-11-08

[FIX] Missing, albeit optional colon

23. By Stefan Rijnhart (Opener) on 2013-11-11

[IMP] Missing colon

Unmerged revisions

23. By Stefan Rijnhart (Opener) on 2013-11-11

[IMP] Missing colon

22. By Stefan Rijnhart (Opener) on 2013-11-08

[FIX] Missing, albeit optional colon

21. By Stefan Rijnhart (Opener) on 2013-11-08

[ADD] Example XML stanza

20. By Stefan Rijnhart (Opener) on 2013-11-08

[RFR] Comments

19. By Stefan Rijnhart (Opener) on 2013-11-08

[IMP] Variable name

18. By Stefan Rijnhart (Opener) on 2013-11-08

[RFR] Additional line break

17. By Stefan Rijnhart (Opener) on 2013-11-08

[IMP] Comments, spacing

16. By Stefan Rijnhart (Opener) on 2013-11-08

[RFR] Eliminate unnecessary variable

15. By Stefan Rijnhart (Opener) on 2013-11-08

[DEL] Debug statements

14. By Stefan Rijnhart (Opener) on 2013-11-08

[IMP] Restrict autoswitching to search views with the autoswitch attribute

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== added directory 'web_autoswitch_to_form'
2=== added file 'web_autoswitch_to_form/__init__.py'
3=== added file 'web_autoswitch_to_form/__openerp__.py'
4--- web_autoswitch_to_form/__openerp__.py 1970-01-01 00:00:00 +0000
5+++ web_autoswitch_to_form/__openerp__.py 2013-11-11 11:35:35 +0000
6@@ -0,0 +1,56 @@
7+# -*- coding: utf-8 -*-
8+##############################################################################
9+#
10+# OpenERP, Open Source Management Solution
11+# This module copyright (C) 2013 Therp BV (<http://therp.nl>).
12+#
13+# This program is free software: you can redistribute it and/or modify
14+# it under the terms of the GNU Affero General Public License as
15+# published by the Free Software Foundation, either version 3 of the
16+# License, or (at your option) any later version.
17+#
18+# This program is distributed in the hope that it will be useful,
19+# but WITHOUT ANY WARRANTY; without even the implied warranty of
20+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21+# GNU Affero General Public License for more details.
22+#
23+# You should have received a copy of the GNU Affero General Public License
24+# along with this program. If not, see <http://www.gnu.org/licenses/>.
25+#
26+##############################################################################
27+{
28+ "name" : "Auto-switch to form mode for singleton search results",
29+ "version" : "0.1",
30+ "author" : "Therp BV",
31+ "category": 'Tools',
32+ "description": """
33+When the 'autoswitch' attribute is defined on a search view, searches with only
34+one result will automatically open the form view for the record.
35+
36+Note that the 'autoswitch' attribute on search elements in XML views is not
37+allowed by the RNG validation. You can circumvent the validation by setting
38+an attribute version="7.0" as well.
39+
40+As an example, here is the necessary code to enable this functionality on
41+the product view:
42+
43+ <record id="product_search_form_view" model="ir.ui.view">
44+ <field name="name">product.search.form</field>
45+ <field name="model">product.product</field>
46+ <field name="inherit_id"
47+ ref="product.product_search_form_view"/>
48+ <field name="arch" type="xml">
49+ <xpath expr="/search[@string='Product']" position="attributes">
50+ <attribute name="version">7.0</attribute>
51+ <attribute name="autoswitch">1</attribute>
52+ </xpath>
53+ </field>
54+ </record>
55+
56+ """,
57+ 'website': 'http://therp.nl',
58+ 'depends' : ['web_kanban'],
59+ 'js': ['static/src/js/web_autoswitch_to_form.js'],
60+ 'data': [
61+ ],
62+}
63
64=== added directory 'web_autoswitch_to_form/static'
65=== added directory 'web_autoswitch_to_form/static/src'
66=== added directory 'web_autoswitch_to_form/static/src/js'
67=== added file 'web_autoswitch_to_form/static/src/js/web_autoswitch_to_form.js'
68--- web_autoswitch_to_form/static/src/js/web_autoswitch_to_form.js 1970-01-01 00:00:00 +0000
69+++ web_autoswitch_to_form/static/src/js/web_autoswitch_to_form.js 2013-11-11 11:35:35 +0000
70@@ -0,0 +1,117 @@
71+/*
72+
73+ Copyright (C) 2013 Therp BV
74+ License: GNU AFFERO GENERAL PUBLIC LICENSE
75+ Version 3 or any later version
76+
77+*/
78+
79+openerp.web_autoswitch_to_form = function(instance) {
80+
81+ instance.web.ViewManager.include({
82+ switch_mode: function(view_type, no_store, view_options) {
83+ /*
84+ Suppress autoswitch after manually changing modes, or
85+ it could become annoying when the user attempts to
86+ switch to a list view of a single item
87+ */
88+ this.flags.suppress_autoswitch = true;
89+ return this._super(view_type, no_store, view_options);
90+ },
91+ });
92+
93+ instance.web.SearchView.include({
94+ disable_autoswitch: function() {
95+ if (this.__parentedParent && this.__parentedParent.flags !== undefined) {
96+ this.__parentedParent.flags.suppress_autoswitch = true;
97+ }
98+ },
99+
100+ check_autoswitch: function(data) {
101+ if (this.__parentedParent
102+ && this.__parentedParent.flags !== undefined
103+ && data.arch && data.arch.attrs) {
104+ this.__parentedParent.flags.autoswitch = data.arch.attrs.autoswitch;
105+ }
106+ },
107+
108+ start: function() {
109+ /*
110+ When a filter is removed, suppress autoswitch if the search
111+ that is then triggered has only one result
112+
113+ Auto-switching is enabled per search view by the 'autoswitch'
114+ attribute on the search element of the search view
115+ */
116+ res = this._super();
117+ this.query.on('remove', this.proxy(this.disable_autoswitch));
118+ this.on('search_view_loaded', this, this.check_autoswitch);
119+ return res;
120+ },
121+ });
122+
123+ instance.web.View.include({
124+ start: function() {
125+ /*
126+ Undo suppressing once a view is loaded
127+ */
128+ res = this._super();
129+ if (this.__parentedParent && this.__parentedParent.flags !== undefined) {
130+ this.__parentedParent.flags.suppress_autoswitch = false;
131+ }
132+ return res;
133+ },
134+ });
135+
136+
137+ instance.web.ListView.include({
138+ do_search: function(domain, context, group_by) {
139+ /*
140+ Switch from list view to form view in case of a single
141+ record when do_search's deferred is resolved
142+ */
143+ var res = this._super(domain, context, group_by)
144+ if ( ! (this.__parentedParent
145+ && this.__parentedParent.flags
146+ && this.__parentedParent.flags.autoswitch)) {
147+ return res;
148+ }
149+ if (this.__parentedParent.flags.suppress_autoswitch) {
150+ this.__parentedParent.flags.suppress_autoswitch = false;
151+ return res;
152+ }
153+ var self = this;
154+ return res.done(
155+ function() {
156+ if (self.records.length === 1) {
157+ self.trigger('switch_mode', 'form');
158+ }
159+ });
160+ },
161+ });
162+
163+ instance.web_kanban.KanbanView.include({
164+ on_groups_started: function() {
165+ /*
166+ Switch from kanban view to form view in case of a single
167+ record after calling the super of this method.
168+ */
169+ var res = this._super();
170+ if ( ! (this.__parentedParent
171+ && this.__parentedParent.flags
172+ && this.__parentedParent.flags.autoswitch)) {
173+ return res;
174+ }
175+ if (this.__parentedParent.flags.suppress_autoswitch) {
176+ this.__parentedParent.flags.suppress_autoswitch = false;
177+ return res;
178+ }
179+ if (this.dataset.size() === 1) {
180+ this.dataset.select_id(this.dataset.ids[0]);
181+ this.do_switch_view('form', null, { mode: undefined });
182+ }
183+ return res;
184+ },
185+ });
186+
187+};

Subscribers

People subscribed via source and target branches