odoo.define('web.TreeView', function (require) { "use strict"; /*--------------------------------------------------------- * Odoo Tree view *---------------------------------------------------------*/ var core = require('web.core'); var data = require('web.data'); var formats = require('web.formats'); var pyeval = require('web.pyeval'); var session = require('web.session'); var View = require('web.View'); var _lt = core._lt; var QWeb = core.qweb; var TreeView = View.extend({ display_name: _lt('Tree'), icon: 'fa-align-left', // Indicates that this view is not searchable, and thus that no search view should be displayed. searchable: false, /** * Genuine tree view (the one displayed as a tree, not the list) */ init: function() { this._super.apply(this, arguments); this.records = {}; _.bindAll(this, 'color_for'); this.fields = this.fields_view.fields; this.children_field = this.fields_view.field_parent; }, /** * Returns the list of fields needed to correctly read objects. * * Gathers the names of all fields in fields_view_get, and adds the * field_parent (children_field in the tree view) if it's not already one * of the fields to fetch * * @returns {Array} an array of fields which can be provided to DataSet.read_slice and others */ fields_list: function () { var fields = _.keys(this.fields); if (!_(fields).contains(this.children_field)) { fields.push(this.children_field); } return fields; }, willStart: function () { _(this.fields_view.arch.children).each(function (field) { if (field.attrs.modifiers && typeof field.attrs.modifiers === "string") { field.attrs.modifiers = JSON.parse(field.attrs.modifiers); } }); if (this.fields_view.arch.attrs.colors) { this.colors = _(this.fields_view.arch.attrs.colors.split(';')).chain() .compact() .map(function(color_pair) { var pair = color_pair.split(':'), color = pair[0], expr = pair[1]; return [color, py.parse(py.tokenize(expr)), expr]; }).value(); } return this._super(); }, start: function () { var self = this; var has_toolbar = !!this.fields_view.arch.attrs.toolbar; this.hook_row_click(); this.$el.html(QWeb.render('TreeView', { 'title': this.fields_view.arch.attrs.string, 'fields_view': this.fields_view.arch.children, 'fields': this.fields, 'toolbar': has_toolbar })); this.$el.addClass(this.fields_view.arch.attrs['class']); this.dataset.read_slice(this.fields_list()).done(function(records) { if (!has_toolbar) { // WARNING: will do a second read on the same ids, but only on // first load so not very important self.getdata(null, _(records).pluck('id')); return; } var $select = self.$('select') .change(function () { var $option = $(this).find(':selected'); self.getdata($option.val(), $option.data('children')); }); _(records).each(function (record) { self.records[record.id] = record; $('