~vauxoo/web-addons/7.0-web_hideleftmenu

« back to all changes in this revision

Viewing changes to web_export_view/static/js/web_advanced_export.js

  • Committer: Leonardo Pistone
  • Date: 2013-06-04 07:29:47 UTC
  • mto: (3.9.2 web-addons-merge169000)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: leonardo.pistone@domsense.com-20130604072947-7qwuf6xkgqjmig2m
[add] module web_export_view copied from 6.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  @@@ web_export_view custom JS @@@
 
2
//#############################################################################
 
3
//    
 
4
//    Copyright (C) 2012 Agile Business Group sagl (<http://www.agilebg.com>)
 
5
//    Copyright (C) 2012 Therp BV (<http://therp.nl>)
 
6
//
 
7
//    This program is free software: you can redistribute it and/or modify
 
8
//    it under the terms of the GNU Affero General Public License as published
 
9
//    by the Free Software Foundation, either version 3 of the License, or
 
10
//    (at your option) any later version.
 
11
//
 
12
//    This program is distributed in the hope that it will be useful,
 
13
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
//    GNU Affero General Public License for more details.
 
16
//
 
17
//    You should have received a copy of the GNU Affero General Public License
 
18
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
//
 
20
//#############################################################################
 
21
 
 
22
openerp.web_export_view = function(openerp) {
 
23
 
 
24
    _t = openerp.web._t;
 
25
 
 
26
    openerp.web.Sidebar = openerp.web.Sidebar.extend({
 
27
 
 
28
        add_default_sections: function() {
 
29
            // IMHO sections should be registered objects
 
30
            // as views and retrieved using a specific registry
 
31
            // so that we don't have to override this
 
32
            
 
33
            var self = this,
 
34
            view = this.widget_parent,
 
35
            view_manager = view.widget_parent,
 
36
            action = view_manager.action;
 
37
            if (this.session.uid === 1) {
 
38
                this.add_section(_t('Customize'), 'customize');
 
39
                this.add_items('customize', [{
 
40
                    label: _t("Translate"),
 
41
                    callback: view.on_sidebar_translate,
 
42
                    title: _t("Technical translation")
 
43
                }]);
 
44
            }
 
45
 
 
46
            this.add_section(_t('Other Options'), 'other');
 
47
            this.add_items('other', [
 
48
                {
 
49
                    label: _t("Export"),
 
50
                    callback: view.on_sidebar_export
 
51
                },
 
52
                {
 
53
                    label: _t("Export current view"),
 
54
                    callback: this.on_sidebar_export_view
 
55
                }
 
56
            ]);
 
57
        },
 
58
 
 
59
        on_sidebar_export_view: function() {
 
60
            // Select the first list of the current (form) view
 
61
            // or assume the main view is a list view and use that
 
62
            var self = this,
 
63
            view = this.widget_parent; // valid for list view
 
64
            if (view.widget_children) {
 
65
                view.widget_children.every(function(child) {
 
66
                    if (child.field && child.field.type == 'one2many') {
 
67
                        view = child.viewmanager.views.list.controller;
 
68
                        return false; // break out of the loop
 
69
                    }
 
70
                    if (child.field && child.field.type == 'many2many') {
 
71
                        view = child.list_view;
 
72
                        return false; // break out of the loop
 
73
                    }
 
74
                    return true;
 
75
                });
 
76
            }
 
77
            var columns = view.visible_columns;
 
78
            export_columns_keys = [];
 
79
            export_columns_names = [];
 
80
            $.each(columns,function(){
 
81
                if(this.tag=='field'){
 
82
                    // non-fields like `_group` or buttons
 
83
                    export_columns_keys.push(this.id);
 
84
                    export_columns_names.push(this.string);
 
85
                }
 
86
            });
 
87
            rows = view.$element.find('.ui-widget-content tr');
 
88
            export_rows = [];
 
89
            $.each(rows,function(){
 
90
                $row = $(this);
 
91
                // find only rows with data
 
92
                if($row.attr('data-id')){
 
93
                    export_row = [];
 
94
                    $.each(export_columns_keys,function(){
 
95
                        cell = $row.find('td[data-field="'+this+'"]').get(0);
 
96
                        text = cell.text || cell.textContent || cell.innerHTML || "";
 
97
                        export_row.push(text.trim());
 
98
                    });
 
99
                    export_rows.push(export_row);
 
100
                }
 
101
            });
 
102
            $.blockUI();
 
103
            view.session.get_file({
 
104
                url: '/web/export/xls_view',
 
105
                data: {data: JSON.stringify({
 
106
                    model : view.model,
 
107
                    headers : export_columns_names,
 
108
                    rows : export_rows,
 
109
                })},
 
110
                complete: $.unblockUI
 
111
            });
 
112
        },
 
113
 
 
114
    });
 
115
 
 
116
}