1 /*---------------------------------------------------------
3 *---------------------------------------------------------*/
5 openerp.web.view_tree = function(openerp) {
6 var QWeb = openerp.web.qweb;
8 openerp.web.views.add('tree', 'openerp.web.TreeView');
9 openerp.web.TreeView = openerp.web.View.extend(/** @lends openerp.web.TreeView# */{
11 * Indicates that this view is not searchable, and thus that no search
12 * view should be displayed (if there is one active).
16 * Genuine tree view (the one displayed as a tree, not the list)
18 * @constructs openerp.web.TreeView
19 * @extends openerp.web.View
26 init: function(parent, dataset, view_id, options) {
28 this.dataset = dataset;
29 this.model = dataset.model;
30 this.view_id = view_id;
34 this.options = _.extend({}, this.defaults, options || {});
39 return this.rpc("/web/treeview/load", {
41 view_id: this.view_id,
43 toolbar: this.view_manager ? !!this.view_manager.sidebar : false
47 * Returns the list of fields needed to correctly read objects.
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
53 * @returns {Array} an array of fields which can be provided to DataSet.read_slice and others
55 fields_list: function () {
56 var fields = _.keys(this.fields);
57 if (!_(fields).contains(this.children_field)) {
58 fields.push(this.children_field);
62 on_loaded: function (fields_view) {
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
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);
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
84 this.dataset.read_slice(this.fields_list(), {}, function (records) {
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'));
92 var $select = self.$element.find('select')
94 var $option = $(this).find(':selected');
95 self.getdata($option.val(), $option.data('children'));
97 _(records).each(function (record) {
98 self.records[record.id] = record;
102 .data('children', record[self.children_field])
106 if (!_.isEmpty(records)) {
112 * Sets up opening a row
114 hook_row_click: function () {
116 this.$element.delegate('.treeview-td span, .treeview-tr span', 'click', function (e) {
117 e.stopImmediatePropagation();
118 self.activate($(this).closest('tr').data('id'));
121 this.$element.delegate('.treeview-tr', 'click', function () {
124 record_id = $this.data('id'),
125 record = self.records[record_id],
126 children_ids = record[self.children_field];
128 _(children_ids).each(function(childid) {
129 if (self.$element.find('#treerow_' + childid).length) {
130 if (self.$element.find('#treerow_' + childid).is(':hidden')) {
137 if (is_loaded === 0) {
138 if (!$this.parent().hasClass('oe-open')) {
139 self.getdata(record_id, children_ids);
142 self.showcontent(record_id, is_loaded < 0);
146 // get child data of selected value
147 getdata: function (id, children_ids) {
150 self.dataset.read_ids(children_ids, this.fields_list(), function (records) {
151 _(records).each(function (record) {
152 self.records[record.id] = record;
155 var $curr_node = self.$element.find('#treerow_' + id);
156 var children_rows = QWeb.render('TreeView.rows', {
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
165 if ($curr_node.length) {
166 $curr_node.addClass('oe-open');
167 $curr_node.after(children_rows);
169 self.$element.find('tbody').html(children_rows);
174 // Get details in listview
175 activate: function(id) {
177 this.rpc('/web/treeview/action', {
179 model: this.dataset.model,
180 context: new openerp.web.CompoundContext(
181 this.dataset.get_context(), {
182 active_model: this.dataset.model,
185 }, function (actions) {
186 if (!actions.length) { return; }
187 var action = actions[0][2];
188 self.do_action(action);
192 // show & hide the contents
193 showcontent: function (record_id, show) {
194 this.$element.find('#treerow_' + record_id)
195 .toggleClass('oe-open', show);
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);
202 $child_row.toggle(show);
206 do_show: function () {
207 this.$element.show();
210 do_hide: function () {
211 this.$element.hide();