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({
|
|
|
|
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;
|
|
|
|
|
|
|
|
this.columns = [];
|
|
|
|
|
|
|
|
this.options = _.extend({}, this.defaults, options || {});
|
|
|
|
this.flags = this.view_manager.action.flags;
|
|
|
|
},
|
2011-06-13 09:54: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-06-13 05:51:02 +00:00
|
|
|
|
2011-06-07 07:10:34 +00:00
|
|
|
on_loaded: function (data) {
|
|
|
|
var self = this;
|
|
|
|
this.dataset.read_slice([], 0, false, function (response) {
|
|
|
|
self.$element.html(QWeb.render('TreeView', {'field_data':response}));
|
|
|
|
id=self.dataset.ids[0];
|
2011-06-13 05:51:02 +00:00
|
|
|
self.$element.find('#parent_id').bind('change', function(){
|
|
|
|
self.getdata($('#parent_id').val(), 0);
|
2011-06-07 07:10:34 +00:00
|
|
|
});
|
2011-06-13 05:51:02 +00:00
|
|
|
self.getdata(id);
|
2011-06-07 07:10:34 +00:00
|
|
|
});
|
2011-05-11 15:25:48 +00:00
|
|
|
},
|
2011-06-08 13:37:06 +00:00
|
|
|
|
2011-06-13 05:51:02 +00:00
|
|
|
// get child data of selected value
|
|
|
|
getdata: function (id, flag) {
|
2011-06-13 09:54:43 +00:00
|
|
|
var self = this;
|
|
|
|
self.dataset.domain = [['parent_id', '=', parseInt(id, 10)]];
|
2011-06-13 05:51:02 +00:00
|
|
|
self.dataset.read_slice([], 0, false, function (response) {
|
2011-06-13 09:54:43 +00:00
|
|
|
if (($('tr #' + id).length) == 1){
|
|
|
|
$('tr #' + id).find('td #parentimg').attr('src','/base/static/src/img/collapse.gif');
|
2011-06-13 09:22:52 +00:00
|
|
|
|
2011-06-13 09:54:43 +00:00
|
|
|
$('tr #'+id).after(QWeb.render('TreeView_Secondry', {'child_data': response}));
|
|
|
|
for (i in response) {
|
|
|
|
if ($('tr #'+response[i].id)) {
|
|
|
|
indent=$('tr #' + id).css('textIndent');
|
2011-06-13 09:22:52 +00:00
|
|
|
ind=indent.split('px');
|
2011-06-13 09:54:43 +00:00
|
|
|
j=(parseInt(ind[0],10)) + 10;
|
2011-06-13 09:22:52 +00:00
|
|
|
$('tr #'+response[i].id).animate({textIndent: j});
|
|
|
|
}
|
|
|
|
}
|
2011-06-13 09:54:43 +00:00
|
|
|
} else {
|
|
|
|
if (flag == 0) {
|
2011-06-13 05:51:02 +00:00
|
|
|
self.$element.find('tr').remove();
|
2011-06-07 07:10:34 +00:00
|
|
|
}
|
2011-06-13 09:54:43 +00:00
|
|
|
self.$element.append(QWeb.render('TreeView_Secondry', {'child_data': response}));
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-06-07 13:14:19 +00:00
|
|
|
|
2011-06-13 09:54:43 +00:00
|
|
|
$('tr').click(function() {
|
|
|
|
divflag = 0;
|
|
|
|
if ($('#' + (this.id)).length == 1) {
|
|
|
|
for (i in response) {
|
|
|
|
if (this.id == response[i].id) {
|
|
|
|
if (response[i].child_id.length > 0) {
|
|
|
|
jQuery(response[i].child_id).each(function(e, chid) {
|
|
|
|
if (jQuery('tr #' + chid).length > 0){
|
|
|
|
if (jQuery('tr #' + chid).is(':hidden')) {
|
2011-06-13 05:51:02 +00:00
|
|
|
divflag = -1;
|
2011-06-13 09:54:43 +00:00
|
|
|
} else {
|
2011-06-13 05:51:02 +00:00
|
|
|
divflag++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-06-13 09:54:43 +00:00
|
|
|
if (divflag == 0) {
|
|
|
|
if ($('#' + (this.id)).find('td #parentimg').attr('src') == '/base/static/src/img/expand.gif') {
|
2011-06-13 05:51:02 +00:00
|
|
|
self.getdata(this.id);
|
|
|
|
}
|
2011-06-13 09:54:43 +00:00
|
|
|
} else if (divflag > 0) {
|
2011-06-13 05:51:02 +00:00
|
|
|
self.showcontent(this.id, 1, response[i].child_id);
|
2011-06-13 09:54:43 +00:00
|
|
|
} else if (divflag == -1) {
|
2011-06-13 05:51:02 +00:00
|
|
|
self.showcontent(this.id, 0, response[i].child_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-06-08 13:37:06 +00:00
|
|
|
}
|
2011-06-13 05:51:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
2011-06-08 13:37:06 +00:00
|
|
|
|
2011-06-13 05:51:02 +00:00
|
|
|
// show & hide the contents
|
|
|
|
showcontent: function (id, flag, childid) {
|
2011-06-13 09:54:43 +00:00
|
|
|
var self = this;
|
|
|
|
if (flag == 1) {
|
|
|
|
$('tr #' + id).find('td #parentimg').attr('src', '/base/static/src/img/expand.gif');
|
|
|
|
} else {
|
|
|
|
$('tr #' + id).find('td #parentimg').attr('src', '/base/static/src/img/collapse.gif');
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-06-08 13:37:06 +00:00
|
|
|
|
2011-06-13 09:54:43 +00:00
|
|
|
for (i in childid) {
|
|
|
|
if (flag == 1) {
|
|
|
|
self.dataset.domain = [['parent_id', '=', parseInt(childid[i], 10)]];
|
|
|
|
childimg = $('tr #' + childid[i]).find('td #parentimg').attr('src');
|
|
|
|
if (childimg == "/base/static/src/img/collapse.gif") {
|
|
|
|
$('tr #' + childid[i]).find('td #parentimg').attr('src', '/base/static/src/img/expand.gif');
|
2011-06-13 09:22:52 +00:00
|
|
|
}
|
2011-06-13 05:51:02 +00:00
|
|
|
self.dataset.read_slice([], 0, false, function (response) {
|
|
|
|
for (j in response){
|
2011-06-13 09:54:43 +00:00
|
|
|
if (jQuery('tr #' + response[j].id).length > 0) {
|
|
|
|
jQuery('tr #' + response[j].id).hide();
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2011-06-13 09:54:43 +00:00
|
|
|
jQuery ('tr #' + childid[i]).hide();
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-06-13 09:54:43 +00:00
|
|
|
else {
|
|
|
|
jQuery ('tr #' + childid[i]).show();
|
2011-06-13 05:51:02 +00:00
|
|
|
}
|
2011-06-07 07:10:34 +00:00
|
|
|
}
|
|
|
|
},
|
2011-06-13 05:51:02 +00:00
|
|
|
|
2011-06-07 07:10:34 +00:00
|
|
|
reload_view: function (grouped) {
|
|
|
|
var self = this;
|
|
|
|
this.dataset.offset = 0;
|
|
|
|
this.dataset.limit = false;
|
2011-06-13 05:51:02 +00:00
|
|
|
|
2011-06-07 07:10:34 +00:00
|
|
|
return this.rpc('/base/treeview/load', {
|
|
|
|
model: this.model,
|
|
|
|
view_id: this.view_id,
|
|
|
|
toolbar: !!this.flags.sidebar
|
|
|
|
}, function (field_view_get) {
|
|
|
|
self.on_loaded(field_view_get, grouped);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
do_search: function (domains, contexts, groupbys) {
|
|
|
|
var self = this;
|
|
|
|
return this.rpc('/base/session/eval_domain_and_context', {
|
|
|
|
domains: domains,
|
|
|
|
contexts: contexts,
|
|
|
|
group_by_seq: groupbys
|
|
|
|
}, function (results) {
|
|
|
|
self.dataset.context = results.context;
|
|
|
|
self.dataset.domain = results.domain;
|
|
|
|
self.reload_view(!!results.group_by).then(
|
|
|
|
$.proxy(self, 'reload_content'));
|
|
|
|
});
|
2011-05-11 15:25:48 +00:00
|
|
|
}
|
|
|
|
});
|
2011-06-07 07:10:34 +00:00
|
|
|
}
|