[REM] Remove code for redirect page on refresh click.
[odoo/odoo.git] / addons / web / static / src / js / view_tree.js
1 /*---------------------------------------------------------
2  * OpenERP web library
3  *---------------------------------------------------------*/
4
5 openerp.web.view_tree = function(openerp) {
6 var QWeb = openerp.web.qweb;
7
8 openerp.web.views.add('tree', 'openerp.web.TreeView');
9 openerp.web.TreeView = openerp.web.View.extend(/** @lends openerp.web.TreeView# */{
10     /**
11      * Indicates that this view is not searchable, and thus that no search
12      * view should be displayed (if there is one active).
13      */
14     searchable : false,
15     /**
16      * Genuine tree view (the one displayed as a tree, not the list)
17      *
18      * @constructs openerp.web.TreeView
19      * @extends openerp.web.View
20      *
21      * @param parent
22      * @param dataset
23      * @param view_id
24      * @param options
25      */
26     init: function(parent, dataset, view_id, options) {
27         this._super(parent);
28         this.dataset = dataset;
29         this.model = dataset.model;
30         this.view_id = view_id;
31
32         this.records = {};
33
34         this.options = _.extend({}, this.defaults, options || {});
35     },
36
37     start: function () {
38         this._super();
39         return this.rpc("/web/treeview/load", {
40             model: this.model,
41             view_id: this.view_id,
42             view_type: "tree",
43             toolbar: this.view_manager ? !!this.view_manager.sidebar : false
44         }, this.on_loaded);
45     },
46     /**
47      * Returns the list of fields needed to correctly read objects.
48      *
49      * Gathers the names of all fields in fields_view_get, and adds the
50      * field_parent (children_field in the tree view) if it's not already one
51      * of the fields to fetch
52      *
53      * @returns {Array} an array of fields which can be provided to DataSet.read_slice and others
54      */
55     fields_list: function () {
56         var fields = _.keys(this.fields);
57         if (!_(fields).contains(this.children_field)) {
58             fields.push(this.children_field);
59         }
60         return fields;
61     },
62     on_loaded: function (fields_view) {
63         var self = this;
64         var has_toolbar = !!fields_view.arch.attrs.toolbar;
65         // field name in OpenERP is kinda stupid: this is the name of the field
66         // holding the ids to the children of the current node, why call it
67         // field_parent?
68         this.children_field = fields_view['field_parent'];
69         this.fields_view = fields_view;
70         _(this.fields_view.arch.children).each(function (field) {
71             if (field.attrs.modifiers) {
72                 field.attrs.modifiers = JSON.parse(field.attrs.modifiers);
73             }
74         });
75         this.fields = fields_view.fields;
76         this.hook_row_click();
77         this.$element.html(QWeb.render('TreeView', {
78             'title': this.fields_view.arch.attrs.string,
79             'fields_view': this.fields_view.arch.children,
80             'fields': this.fields,
81             'toolbar': has_toolbar
82         }));
83
84         this.dataset.read_slice(this.fields_list(), {}, function (records) {
85             if (!has_toolbar) {
86                 // WARNING: will do a second read on the same ids, but only on
87                 //          first load so not very important
88                 self.getdata(null, _(records).pluck('id'));
89                 return;
90             }
91
92             var $select = self.$element.find('select')
93                 .change(function () {
94                     var $option = $(this).find(':selected');
95                     self.getdata($option.val(), $option.data('children'));
96                 });
97             _(records).each(function (record) {
98                 self.records[record.id] = record;
99                 $('<option>')
100                         .val(record.id)
101                         .text(record.name)
102                         .data('children', record[self.children_field])
103                     .appendTo($select);
104             });
105
106             if (!_.isEmpty(records)) {
107                 $select.change();
108             }
109         });
110     },
111     /**
112      * Sets up opening a row
113      */
114     hook_row_click: function () {
115         var self = this;
116         this.$element.delegate('.treeview-td span, .treeview-tr span', 'click', function (e) {
117             e.stopImmediatePropagation();
118             self.activate($(this).closest('tr').data('id'));
119         });
120
121         this.$element.delegate('.treeview-tr', 'click', function () {
122             var is_loaded = 0,
123                 $this = $(this),
124                 record_id = $this.data('id'),
125                 record = self.records[record_id],
126                 children_ids = record[self.children_field];
127
128             _(children_ids).each(function(childid) {
129                 if (self.$element.find('#treerow_' + childid).length) {
130                     if (self.$element.find('#treerow_' + childid).is(':hidden')) {
131                         is_loaded = -1;
132                     } else {
133                         is_loaded++;
134                     }
135                 }
136             });
137             if (is_loaded === 0) {
138                 if (!$this.parent().hasClass('oe-open')) {
139                     self.getdata(record_id, children_ids);
140                 }
141             } else {
142                 self.showcontent(record_id, is_loaded < 0);
143             }
144         });
145     },
146     // get child data of selected value
147     getdata: function (id, children_ids) {
148         var self = this;
149
150         self.dataset.read_ids(children_ids, this.fields_list(), function (records) {
151             _(records).each(function (record) {
152                 self.records[record.id] = record;
153             });
154
155             var $curr_node = self.$element.find('#treerow_' + id);
156             var children_rows = QWeb.render('TreeView.rows', {
157                 'records': records,
158                 'children_field': self.children_field,
159                 'fields_view': self.fields_view.arch.children,
160                 'fields': self.fields,
161                 'level': $curr_node.data('level') || 0,
162                 'render': openerp.web.format_value
163             });
164
165             if ($curr_node.length) {
166                 $curr_node.addClass('oe-open');
167                 $curr_node.after(children_rows);
168             } else {
169                 self.$element.find('tbody').html(children_rows);
170             }
171         });
172     },
173
174     // Get details in listview
175     activate: function(id) {
176         var self = this;
177         this.rpc('/web/treeview/action', {
178             id: id,
179             model: this.dataset.model,
180             context: new openerp.web.CompoundContext(
181                 this.dataset.get_context(), {
182                     active_model: this.dataset.model,
183                     active_id: id,
184                     active_ids: [id]})
185         }, function (actions) {
186             if (!actions.length) { return; }
187             var action = actions[0][2];
188             self.do_action(action);
189         });
190     },
191
192     // show & hide the contents
193     showcontent: function (record_id, show) {
194         this.$element.find('#treerow_' + record_id)
195                 .toggleClass('oe-open', show);
196
197         _(this.records[record_id][this.children_field]).each(function (child_id) {
198             var $child_row = this.$element.find('#treerow_' + child_id);
199             if ($child_row.hasClass('oe-open')) {
200                 this.showcontent(child_id, false);
201             }
202             $child_row.toggle(show);
203         }, this);
204     },
205
206     do_show: function () {
207         this.$element.show();
208     },
209
210     do_hide: function () {
211         this.$element.hide();
212         this.hidden = true;
213     }
214 });
215 };