/*--------------------------------------------------------- * OpenERP web library *---------------------------------------------------------*/ openerp.web.view_tree = function(instance) { var QWeb = instance.web.qweb, _lt = instance.web._lt; instance.web.views.add('tree', 'instance.web.TreeView'); instance.web.TreeView = instance.web.View.extend(/** @lends instance.web.TreeView# */{ display_name: _lt('Tree'), /** * Indicates that this view is not searchable, and thus that no search * view should be displayed (if there is one active). */ searchable : false, /** * Genuine tree view (the one displayed as a tree, not the list) * * @constructs instance.web.TreeView * @extends instance.web.View * * @param parent * @param dataset * @param view_id * @param options */ init: function(parent, dataset, view_id, options) { this._super(parent); this.dataset = dataset; this.model = dataset.model; this.view_id = view_id; this.records = {}; this.options = _.extend({}, this.defaults, options || {}); _.bindAll(this, 'color_for'); }, start: function () { return this.rpc("/web/treeview/load", { model: this.model, view_id: this.view_id, view_type: "tree", toolbar: this.view_manager ? !!this.view_manager.sidebar : false, context: this.dataset.get_context() }).then(this.on_loaded); }, /** * 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; }, store_record:function(records){ var self = this; _(records).each(function (record) { self.records[record.id] = record; }); }, on_loaded: function (fields_view) { var self = this; var has_toolbar = !!fields_view.arch.attrs.toolbar; // field name in OpenERP is kinda stupid: this is the name of the field // holding the ids to the children of the current node, why call it // field_parent? this.children_field = fields_view['field_parent']; this.fields_view = fields_view; _(this.fields_view.arch.children).each(function (field) { if (field.attrs.modifiers) { field.attrs.modifiers = JSON.parse(field.attrs.modifiers); } }); this.fields = fields_view.fields; 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()).then(function(records) { self.store_record(records); if (!has_toolbar) { // WARNING: will do a second read on the same ids, but only on // first load so not very important self.render_data({'null':records}) self.getdata(_.pluck(records,"id")); return; } var $select = self.$el.find('select') .change(function () { var $option = $(this).find(':selected'); self.getdata($option.val()); }); _(records).each(function (record) { self.records[record.id] = record; $('