2011-05-23 14:46:40 +00:00
|
|
|
openerp.base.tree = function(openerp) {
|
2011-05-11 15:25:48 +00:00
|
|
|
openerp.base.views.add('tree', 'openerp.base.TreeView');
|
2011-06-13 09:54:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Genuine tree view (the one displayed as a tree, not the list)
|
|
|
|
*/
|
2011-06-07 07:10:34 +00:00
|
|
|
openerp.base.TreeView = openerp.base.View.extend({
|
2011-06-29 11:46:52 +00:00
|
|
|
/**
|
|
|
|
* Indicates that this view is not searchable, and thus that no search
|
|
|
|
* view should be displayed (if there is one active).
|
|
|
|
*/
|
|
|
|
searchable : false,
|
|
|
|
|
2011-06-07 07:10:34 +00:00
|
|
|
init: function(view_manager, session, element_id, dataset, view_id, options) {
|
|
|
|
this._super(session, element_id);
|
|
|
|
this.view_manager = view_manager || new openerp.base.NullViewManager();
|
|
|
|
this.dataset = dataset;
|
|
|
|
this.model = dataset.model;
|
|
|
|
this.view_id = view_id;
|
2011-06-29 11:46:52 +00:00
|
|
|
this.session = session;
|
2011-08-09 10:04:38 +00:00
|
|
|
|
|
|
|
this.records = {};
|
2011-06-07 07:10:34 +00:00
|
|
|
|
2011-07-05 06:57:36 +00:00
|
|
|
this.options = _.extend({}, this.defaults, options || {});
|
2011-06-30 06:42:07 +00:00
|
|
|
|
2011-06-07 07:10:34 +00:00
|
|
|
this.flags = this.view_manager.action.flags;
|
2011-07-05 06:57:36 +00:00
|
|
|
|
2011-06-30 06:42:07 +00:00
|
|
|
this.view_manager.flags.search_view = this.view_manager.action.flags.search_view = false;
|
|
|
|
this.view_manager.flags.sidebar = this.view_manager.action.flags.sidebar = false;
|
2011-07-05 06:55:33 +00:00
|
|
|
|
|
|
|
this.actionmanager = new openerp.base.ActionManager(this.session, this.element_id);
|
|
|
|
this.actionmanager.start();
|
2011-06-07 07:10:34 +00:00
|
|
|
},
|
2011-06-14 06:39:43 +00:00
|
|
|
|
2011-05-11 15:25:48 +00:00
|
|
|
start: function () {
|
|
|
|
this._super();
|
2011-06-07 07:10:34 +00:00
|
|
|
return this.rpc("/base/treeview/load", {
|
|
|
|
model: this.model,
|
|
|
|
view_id: this.view_id,
|
|
|
|
toolbar: this.view_manager ? !!this.view_manager.sidebar : false
|
|
|
|
}, this.on_loaded);
|
2011-05-11 15:25:48 +00:00
|
|
|
},
|
2011-08-09 08:56:28 +00:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
},
|
2011-08-08 14:37:18 +00:00
|
|
|
on_loaded: function (fields_view) {
|
2011-06-07 07:10:34 +00:00
|
|
|
var self = this;
|
2011-08-09 08:56:28 +00:00
|
|
|
// field name in OpenERP is kinda stupid: this is the field holding
|
|
|
|
// the ids to the children of the current node
|
|
|
|
this.children_field = fields_view['field_parent'];
|
2011-08-08 14:37:18 +00:00
|
|
|
this.fields_view = fields_view;
|
2011-08-08 15:23:43 +00:00
|
|
|
_(this.fields_view.arch.children).each(function (field) {
|
|
|
|
if (field.attrs.modifiers) {
|
|
|
|
field.attrs.modifiers = JSON.parse(field.attrs.modifiers);
|
|
|
|
}
|
|
|
|
});
|
2011-08-08 14:37:18 +00:00
|
|
|
this.fields = fields_view.fields;
|
2011-07-21 12:25:34 +00:00
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
this.dataset.read_slice(this.fields_list(), 0, false, function (records) {
|
2011-08-08 14:37:18 +00:00
|
|
|
self.$element.html(QWeb.render('TreeView', {
|
|
|
|
'title': self.fields_view.arch.attrs.string
|
|
|
|
}));
|
2011-08-09 08:56:28 +00:00
|
|
|
var $select = self.$element.find('select')
|
|
|
|
.change(function () {
|
|
|
|
var $option = $(this).find(':selected');
|
|
|
|
self.getdata($option.val(), $option.data('children'));
|
|
|
|
});
|
|
|
|
_(records).each(function (record) {
|
2011-08-09 10:04:38 +00:00
|
|
|
self.records[record.id] = record;
|
2011-08-09 08:56:28 +00:00
|
|
|
$('<option>')
|
|
|
|
.val(record.id)
|
|
|
|
.text(record.name)
|
|
|
|
.data('children', record[self.children_field])
|
|
|
|
.appendTo($select);
|
2011-06-07 07:10:34 +00:00
|
|
|
});
|
2011-08-09 08:56:28 +00:00
|
|
|
|
|
|
|
$select.change();
|
2011-06-07 07:10:34 +00:00
|
|
|
});
|
2011-08-09 10:04:38 +00:00
|
|
|
this.hook_row_click();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* Sets up opening a row
|
|
|
|
*/
|
|
|
|
hook_row_click: function () {
|
|
|
|
var self = this;
|
|
|
|
this.$element.delegate('.treeview-tr img', 'click', function () {
|
|
|
|
var is_loaded = false,
|
|
|
|
$this = $(this),
|
|
|
|
record_id = $this.data('id'),
|
|
|
|
record = self.records[record_id],
|
|
|
|
children_ids = record[self.children_field];
|
|
|
|
|
|
|
|
_(children_ids).each(function(childid) {
|
|
|
|
if ($('tr #treerow_' + childid).length) {
|
|
|
|
if ($('tr #treerow_' + childid).is(':hidden')) {
|
|
|
|
is_loaded = -1;
|
|
|
|
} else {
|
|
|
|
is_loaded++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
if (is_loaded == 0) {
|
|
|
|
if ($this.attr('src') == '/base/static/src/img/expand.gif') {
|
|
|
|
self.getdata(record_id, children_ids, true);
|
|
|
|
}
|
|
|
|
} else {
|
2011-08-09 10:29:51 +00:00
|
|
|
self.showcontent(record_id, is_loaded < 0);
|
2011-08-09 10:04:38 +00:00
|
|
|
}
|
|
|
|
});
|
2011-05-11 15:25:48 +00:00
|
|
|
},
|
2011-06-13 05:51:02 +00:00
|
|
|
// get child data of selected value
|
2011-08-09 08:56:28 +00:00
|
|
|
getdata: function (id, children_ids, flag) {
|
2011-06-13 09:54:43 +00:00
|
|
|
var self = this;
|
2011-06-27 11:38:57 +00:00
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
self.dataset.read_ids(children_ids, this.fields_list(), function (records) {
|
2011-08-09 10:04:38 +00:00
|
|
|
_(records).each(function (record) {
|
|
|
|
self.records[record.id] = record;
|
|
|
|
});
|
2011-07-29 11:56:54 +00:00
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
var is_padding, row_id;
|
2011-06-28 13:25:07 +00:00
|
|
|
var curr_node = $('tr #treerow_' + id);
|
|
|
|
|
|
|
|
if (curr_node.length == 1) {
|
2011-07-29 11:56:54 +00:00
|
|
|
curr_node.find('td:first').children(':first-child').attr('src','/base/static/src/img/collapse.gif');
|
2011-08-08 15:23:43 +00:00
|
|
|
curr_node.after(QWeb.render('TreeView.rows', {
|
2011-08-09 08:56:28 +00:00
|
|
|
'records': records,
|
|
|
|
'children_field': self.children_field,
|
2011-08-08 14:37:18 +00:00
|
|
|
'fields_view' : self.fields_view.arch.children,
|
|
|
|
'field' : self.fields
|
|
|
|
}));
|
2011-07-27 11:26:17 +00:00
|
|
|
|
2011-06-28 13:25:07 +00:00
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
for (var i = 0; i < records.length; i++) {
|
|
|
|
row_id = $('tr #treerow_' + records[i].id);
|
2011-07-29 11:56:54 +00:00
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
if (row_id && row_id.find('td:first').children(':first-child').attr('id') == 'parentimg_' + records[i].id) {
|
2011-07-29 11:56:54 +00:00
|
|
|
row_id.find('td:first').append('<span>'+row_id.find('td:eq(1)').text()+'</span>');
|
|
|
|
row_id.find('td:eq(1)').remove();
|
2011-06-28 13:25:07 +00:00
|
|
|
is_padding = true;
|
2011-06-15 09:10:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-30 05:28:02 +00:00
|
|
|
var padding = curr_node.find('td').css('paddingLeft');
|
|
|
|
var padd = parseInt(padding.replace('px',''), 10);
|
2011-06-30 06:42:07 +00:00
|
|
|
var fixpadding;
|
|
|
|
|
2011-08-09 08:56:28 +00:00
|
|
|
for (var i = 0; i < records.length; i++) {
|
|
|
|
row_id = $('tr #treerow_' + records[i].id);
|
2011-06-16 06:50:35 +00:00
|
|
|
if (row_id) {
|
2011-06-28 13:25:07 +00:00
|
|
|
if (!is_padding) {
|
|
|
|
fixpadding = padd + 40;
|
2011-07-13 07:13:08 +00:00
|
|
|
row_id.find('td:first').css('paddingLeft', fixpadding);
|
2011-06-14 06:39:43 +00:00
|
|
|
} else {
|
2011-06-28 13:25:07 +00:00
|
|
|
if (padd == 1) {
|
|
|
|
fixpadding = padd + 17;
|
2011-06-15 09:10:11 +00:00
|
|
|
} else {
|
2011-06-28 13:25:07 +00:00
|
|
|
fixpadding = padd + 20;
|
2011-06-15 09:10:11 +00:00
|
|
|
}
|
2011-07-13 07:13:08 +00:00
|
|
|
var curr_node_elem = row_id.find('td:first');
|
2011-06-28 13:25:07 +00:00
|
|
|
curr_node_elem.children(':first-child').addClass("parent_top");
|
2011-08-09 08:56:28 +00:00
|
|
|
if (curr_node_elem.children(':first-child').attr('id') == "parentimg_" + records[i].id) {
|
2011-06-28 13:25:07 +00:00
|
|
|
curr_node_elem.css('paddingLeft', fixpadding );
|
2011-06-16 05:34:04 +00:00
|
|
|
} else {
|
2011-06-28 13:25:07 +00:00
|
|
|
curr_node_elem.css('paddingLeft', (fixpadding + 20));
|
2011-06-15 09:10:11 +00:00
|
|
|
}
|
2011-06-14 06:39:43 +00:00
|
|
|
}
|
2011-06-13 09:22:52 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-13 09:54:43 +00:00
|
|
|
} else {
|
2011-06-28 13:25:07 +00:00
|
|
|
if (!flag) {
|
2011-07-21 12:25:34 +00:00
|
|
|
self.$element.find('table').remove();
|
2011-06-07 07:10:34 +00:00
|
|
|
}
|
2011-08-08 15:23:43 +00:00
|
|
|
self.$element.append(QWeb.render('TreeView.architecture', {
|
2011-08-09 08:56:28 +00:00
|
|
|
'records': records,
|
|
|
|
'children_field': self.children_field,
|
2011-08-08 15:23:43 +00:00
|
|
|
'fields_view': self.fields_view.arch.children,
|
|
|
|
'fields': self.fields
|
2011-08-08 14:37:18 +00:00
|
|
|
}));
|
2011-07-27 11:26:17 +00:00
|
|
|
|
2011-06-16 05:34:04 +00:00
|
|
|
self.$element.find('tr[id ^= treerow_]').each( function() {
|
2011-07-29 11:56:54 +00:00
|
|
|
if ($(this).find('td:first').children(':first-child').attr('id')) {
|
|
|
|
$(this).find('td:first').append('<span>' + $(this).find('td:eq(1)').text() + '</span>');
|
|
|
|
$(this).find('td:eq(1)').remove();
|
|
|
|
}
|
2011-06-28 13:25:07 +00:00
|
|
|
$(this).find('td').children(':first-child').addClass("parent_top");
|
|
|
|
if (!($(this).find('td').children(':first-child').attr('id'))) {
|
2011-07-27 11:26:17 +00:00
|
|
|
$(this).find('td:first').css('paddingLeft', '20px');
|
2011-06-16 05:34:04 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-06-07 13:14:19 +00:00
|
|
|
|
2011-06-29 11:46:52 +00:00
|
|
|
self.$element.find('tr[id ^= treerow_]').find('td').children(':last-child').click( function(e) {
|
2011-06-27 11:38:57 +00:00
|
|
|
row_id = $(this).parent().parent().attr('id');
|
2011-08-09 08:56:28 +00:00
|
|
|
var record_id = row_id.split('_')[1];
|
2011-06-30 05:28:02 +00:00
|
|
|
self.showrecord(record_id, self.model);
|
2011-06-29 11:46:52 +00:00
|
|
|
e.stopImmediatePropagation();
|
2011-06-16 05:34:04 +00:00
|
|
|
});
|
2011-06-13 05:51:02 +00:00
|
|
|
});
|
|
|
|
},
|
2011-06-08 13:37:06 +00:00
|
|
|
|
2011-06-27 11:38:57 +00:00
|
|
|
// Get details in listview
|
2011-06-29 11:46:52 +00:00
|
|
|
showrecord: function(id, model){
|
2011-06-22 10:14:41 +00:00
|
|
|
var self = this;
|
|
|
|
self.dataset.model = 'product.product';
|
2011-06-27 12:20:39 +00:00
|
|
|
self.dataset.domain = [['categ_id', 'child_of', parseInt(id, 10)]];
|
2011-06-28 13:25:07 +00:00
|
|
|
var modes = !!modes ? modes.split(",") : ["tree", "form"];
|
2011-06-22 10:14:41 +00:00
|
|
|
var views = [];
|
|
|
|
_.each(modes, function(mode) {
|
|
|
|
var view = [false, mode == "tree" ? "list" : mode];
|
|
|
|
if (self.fields.views && self.fields.views[mode]) {
|
|
|
|
view.push(self.fields.views[mode]);
|
|
|
|
}
|
|
|
|
views.push(view);
|
|
|
|
});
|
2011-07-05 06:55:33 +00:00
|
|
|
this.actionmanager.do_action({
|
2011-06-29 11:46:52 +00:00
|
|
|
"res_model" : self.dataset.model,
|
|
|
|
"domain" : self.dataset.domain,
|
2011-06-27 12:42:43 +00:00
|
|
|
"views" : views,
|
|
|
|
"type" : "ir.actions.act_window",
|
|
|
|
"auto_search" : true,
|
2011-07-05 06:55:33 +00:00
|
|
|
"view_mode" : "list",
|
|
|
|
"flags": {
|
|
|
|
search_view: true,
|
|
|
|
sidebar : true,
|
|
|
|
views_switcher : true,
|
|
|
|
action_buttons : true,
|
|
|
|
pager: true,
|
|
|
|
new_window : true
|
|
|
|
}
|
|
|
|
});
|
2011-06-22 10:14:41 +00:00
|
|
|
|
2011-06-29 11:46:52 +00:00
|
|
|
self.dataset.model = model;
|
2011-06-22 10:14:41 +00:00
|
|
|
},
|
|
|
|
|
2011-06-13 05:51:02 +00:00
|
|
|
// show & hide the contents
|
2011-08-09 10:29:51 +00:00
|
|
|
showcontent: function (record_id, show) {
|
|
|
|
this.$element.find('#parentimg_' + record_id)
|
|
|
|
.attr('src', show ? '/base/static/src/img/collapse.gif' : '/base/static/src/img/expand.gif');
|
2011-06-14 09:40:35 +00:00
|
|
|
|
2011-08-09 10:34:07 +00:00
|
|
|
_(this.records[record_id][this.children_field]).each(function (child_id) {
|
2011-08-09 10:29:51 +00:00
|
|
|
if (this.$element.find('img#parentimg_' + child_id).length) {
|
|
|
|
this.showcontent(child_id, false);
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-08-09 10:29:51 +00:00
|
|
|
|
|
|
|
this.$element.find('#treerow_' + child_id).toggle(show);
|
2011-08-09 10:34:07 +00:00
|
|
|
}, this);
|
2011-06-07 07:10:34 +00:00
|
|
|
},
|
2011-06-13 05:51:02 +00:00
|
|
|
|
2011-06-29 11:46:52 +00:00
|
|
|
do_show: function () {
|
|
|
|
this.$element.show();
|
|
|
|
this.view_manager.sidebar.do_refresh(true);
|
|
|
|
},
|
2011-06-30 06:42:07 +00:00
|
|
|
|
2011-06-29 11:46:52 +00:00
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
|
|
|
this.hidden = true;
|
2011-07-05 06:55:33 +00:00
|
|
|
}
|
2011-05-11 15:25:48 +00:00
|
|
|
});
|
2011-07-05 06:55:33 +00:00
|
|
|
};
|