2011-09-14 05:37:46 +00:00
|
|
|
openerp.web.view_editor = function(openerp) {
|
2011-09-15 12:28:37 +00:00
|
|
|
var QWeb = openerp.web.qweb;
|
|
|
|
openerp.web.ViewEditor = openerp.web.Widget.extend({
|
2011-08-23 10:39:41 +00:00
|
|
|
init: function(parent, element_id, dataset, view, options) {
|
|
|
|
this._super(parent);
|
|
|
|
this.element_id = element_id
|
|
|
|
this.parent = parent
|
|
|
|
this.dataset = dataset;
|
|
|
|
this.model = dataset.model;
|
2011-09-13 13:28:29 +00:00
|
|
|
this.xml_id = 0;
|
2011-08-23 10:39:41 +00:00
|
|
|
},
|
2011-09-30 12:49:32 +00:00
|
|
|
|
2011-08-23 10:39:41 +00:00
|
|
|
start: function() {
|
2011-09-13 13:28:29 +00:00
|
|
|
this.View_editor();
|
|
|
|
},
|
2011-09-22 09:00:27 +00:00
|
|
|
|
2011-09-30 12:49:32 +00:00
|
|
|
View_editor : function(){
|
|
|
|
var self = this;
|
|
|
|
var action = {
|
|
|
|
name:'ViewEditor',
|
|
|
|
context:this.session.user_context,
|
|
|
|
domain: [["model", "=", this.dataset.model]],
|
|
|
|
res_model : 'ir.ui.view',
|
|
|
|
views : [[false, 'list']],
|
|
|
|
type: 'ir.actions.act_window',
|
|
|
|
target: "current",
|
|
|
|
limit : 80,
|
|
|
|
auto_search : true,
|
|
|
|
flags: {
|
|
|
|
sidebar: false,
|
|
|
|
views_switcher: false,
|
|
|
|
action_buttons:false,
|
|
|
|
search_view:false,
|
|
|
|
pager:false,
|
|
|
|
radio:true
|
|
|
|
},
|
|
|
|
};
|
|
|
|
var action_manager = new openerp.web.ActionManager(this);
|
|
|
|
this.dialog = new openerp.web.Dialog(this,{
|
|
|
|
modal: true,
|
|
|
|
title: 'ViewEditor',
|
|
|
|
width: 750,
|
|
|
|
height: 500,
|
|
|
|
buttons: {
|
|
|
|
"Create": function(){
|
|
|
|
//to do
|
|
|
|
},
|
|
|
|
"Edit": function(){
|
|
|
|
self.xml_id = 0 ;
|
|
|
|
self.get_data();
|
|
|
|
},
|
|
|
|
"Close": function(){
|
|
|
|
$(this).dialog('destroy');
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
this.dialog.start();
|
|
|
|
this.dialog.open();
|
|
|
|
action_manager.appendTo(this.dialog);
|
|
|
|
action_manager.do_action(action);
|
2011-09-13 13:28:29 +00:00
|
|
|
},
|
2011-09-16 13:24:11 +00:00
|
|
|
|
2011-10-05 11:21:27 +00:00
|
|
|
check_attr: function(xml ,tag,level) {
|
2011-09-13 13:28:29 +00:00
|
|
|
var obj = new Object();
|
|
|
|
obj.child_id = [];
|
|
|
|
obj.id = this.xml_id++;
|
2011-10-05 11:21:27 +00:00
|
|
|
obj.level = level+1;
|
2011-09-30 12:49:32 +00:00
|
|
|
var render_name = "<" + tag;
|
2011-09-13 13:28:29 +00:00
|
|
|
$(xml).each(function() {
|
2011-10-05 11:21:27 +00:00
|
|
|
_.each(this.attributes, function(attrs){
|
|
|
|
if (tag != 'button') {
|
|
|
|
if (attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index") {
|
2011-09-30 12:49:32 +00:00
|
|
|
render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
|
2011-10-05 11:21:27 +00:00
|
|
|
} else {
|
|
|
|
if (attrs.nodeName == "name") {
|
2011-09-30 12:49:32 +00:00
|
|
|
render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
render_name+= ">";
|
|
|
|
});
|
|
|
|
obj.name = render_name;
|
2011-09-13 13:28:29 +00:00
|
|
|
return obj;
|
|
|
|
},
|
2011-09-16 13:24:11 +00:00
|
|
|
|
2011-10-05 11:21:27 +00:00
|
|
|
save_object: function(val, parent_list, child_obj_list){
|
2011-09-13 13:28:29 +00:00
|
|
|
var self = this;
|
2011-09-15 12:28:37 +00:00
|
|
|
var check_id = parent_list[0];
|
|
|
|
var p_list = parent_list.slice(1);
|
2011-10-05 11:21:27 +00:00
|
|
|
if (val.child_id.length != 0) {
|
|
|
|
$.each(val.child_id, function(key, val) {
|
|
|
|
if (val.id == check_id) {
|
|
|
|
if (p_list.length!=0) {
|
|
|
|
self.save_object(val, p_list, child_obj_list);
|
|
|
|
} else {
|
2011-09-15 12:28:37 +00:00
|
|
|
val.child_id = child_obj_list;
|
2011-09-30 12:49:32 +00:00
|
|
|
return;
|
2011-09-15 12:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}else{
|
|
|
|
val.child_id = child_obj_list;
|
2011-09-13 13:28:29 +00:00
|
|
|
}
|
|
|
|
},
|
2011-09-16 13:24:11 +00:00
|
|
|
|
2011-10-05 11:21:27 +00:00
|
|
|
children_function: function(xml, root, parent_list, parent_id, main_object){
|
2011-09-13 13:28:29 +00:00
|
|
|
var self = this;
|
|
|
|
var child_obj_list = [];
|
|
|
|
var children_list = $(xml).filter(root).children();
|
2011-09-16 13:24:11 +00:00
|
|
|
var parents = $(children_list[0]).parents().get();
|
2011-09-30 12:49:32 +00:00
|
|
|
_.each(children_list, function(child_node){
|
2011-09-16 13:24:11 +00:00
|
|
|
var string = self.check_attr(child_node,child_node.tagName.toLowerCase(),parents.length);
|
2011-09-13 13:28:29 +00:00
|
|
|
child_obj_list.push(string);
|
|
|
|
});
|
2011-10-05 11:21:27 +00:00
|
|
|
if (children_list.length != 0) {
|
2011-09-15 12:28:37 +00:00
|
|
|
var parents = $(children_list[0]).parents().get();
|
2011-10-05 11:21:27 +00:00
|
|
|
if (parents.length <= parent_list.length) {
|
2011-09-30 12:49:32 +00:00
|
|
|
parent_list.splice(parents.length-1);
|
|
|
|
}
|
2011-09-15 12:28:37 +00:00
|
|
|
parent_list.push(parent_id);
|
2011-10-05 11:21:27 +00:00
|
|
|
$.each(main_object, function(key, val) {
|
|
|
|
self.save_object(val, parent_list.slice(1), child_obj_list);
|
2011-09-15 12:28:37 +00:00
|
|
|
});
|
2011-09-13 13:28:29 +00:00
|
|
|
}
|
2011-10-05 11:21:27 +00:00
|
|
|
for (var i=0;i<children_list.length;i++) {
|
2011-09-15 12:28:37 +00:00
|
|
|
self.children_function
|
2011-10-05 11:21:27 +00:00
|
|
|
(children_list[i], children_list[i].tagName.toLowerCase(),
|
|
|
|
parent_list, child_obj_list[i].id, main_object);
|
2011-09-13 13:28:29 +00:00
|
|
|
}
|
2011-10-05 11:21:27 +00:00
|
|
|
return main_object;
|
2011-09-13 13:28:29 +00:00
|
|
|
},
|
2011-09-30 12:49:32 +00:00
|
|
|
|
2011-10-05 11:21:27 +00:00
|
|
|
parse_xml: function(arch,view_id) {
|
2011-09-23 13:04:06 +00:00
|
|
|
var self = this;
|
|
|
|
var root = $(arch).filter(":first")[0];
|
|
|
|
var tag = root.tagName.toLowerCase();
|
2011-10-05 11:21:27 +00:00
|
|
|
var obj = new Object();
|
|
|
|
obj.child_id = [];
|
|
|
|
obj.id = this.xml_id++;
|
|
|
|
obj.level = 0;
|
|
|
|
obj.name = "<view view_id='"+view_id+"'>"
|
|
|
|
var root_object = self.check_attr(root,tag,0);
|
|
|
|
f_obj = self.children_function(arch, tag, [], this.xml_id-1, [root_object], [])
|
|
|
|
obj.child_id.push(f_obj[0]);
|
|
|
|
f_obj.pop();
|
|
|
|
f_obj.push(obj);
|
|
|
|
return f_obj;
|
2011-09-23 13:04:06 +00:00
|
|
|
},
|
2011-09-30 12:49:32 +00:00
|
|
|
|
2011-10-05 11:21:27 +00:00
|
|
|
get_data: function() {
|
2011-09-30 12:49:32 +00:00
|
|
|
var self = this;
|
|
|
|
var view_id =(($("input[name='radiogroup']:checked").parent()).parent()).attr('data-id');
|
|
|
|
var ve_dataset = new openerp.web.DataSet(this,'ir.ui.view');
|
2011-10-05 11:21:27 +00:00
|
|
|
ve_dataset.read_ids([parseInt(view_id)], ['arch'], function (arch) {
|
2011-10-03 09:49:31 +00:00
|
|
|
one_object = self.parse_xml(arch[0].arch,view_id);
|
2011-09-30 12:49:32 +00:00
|
|
|
one_object.arch = arch[0].arch;
|
2011-10-05 11:21:27 +00:00
|
|
|
dataset = new openerp.web.DataSetSearch(self, 'ir.ui.view', null, null);
|
2011-09-23 13:04:06 +00:00
|
|
|
dataset.read_slice([],{domain : [['inherit_id','=',parseInt(view_id)]]},function (result) {
|
2011-09-30 12:49:32 +00:00
|
|
|
_.each(result,function(res){
|
|
|
|
self.inherit_view(one_object,res);
|
|
|
|
});
|
2011-09-27 07:04:13 +00:00
|
|
|
return self.edit_view(one_object);
|
|
|
|
});
|
2011-09-30 12:49:32 +00:00
|
|
|
});
|
|
|
|
},
|
2011-10-05 11:21:27 +00:00
|
|
|
|
|
|
|
inherit_view: function(one_object, result){
|
2011-09-30 12:49:32 +00:00
|
|
|
var self = this;
|
|
|
|
var root = $(result.arch).filter('*');
|
2011-10-05 11:21:27 +00:00
|
|
|
var xml_list = [];
|
|
|
|
var xpath_object ;
|
|
|
|
if (root[0].tagName.toLowerCase() == "data") {
|
|
|
|
xml_list = $(root[0]).children();
|
|
|
|
} else {
|
|
|
|
xml_list.push(root[0]);
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
2011-10-05 11:21:27 +00:00
|
|
|
_.each(xml_list , function(xml){
|
|
|
|
var parent_id;
|
|
|
|
var check_list = [];
|
|
|
|
var position = $(xml).attr('position');
|
|
|
|
if (xml.tagName.toLowerCase() == "xpath") {
|
|
|
|
var part_expr = _.without($(xml).attr('expr').split("/"),"");;
|
|
|
|
xpath_object = self.parse_xml(xml,result.id);
|
|
|
|
if (part_expr[part_expr.length-1].search("@")!=-1) {
|
|
|
|
check_list = $.trim(part_expr[part_expr.length-1].replace(/[^a-zA-Z 0-9 _]+/g,' ')).split(" ");
|
|
|
|
|
|
|
|
}else{
|
|
|
|
//search full path...
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var xml_child = $(xml).children();
|
|
|
|
check_list.push(xml.tagName.toLowerCase());
|
|
|
|
if ($(root[0]).attr('name')){
|
|
|
|
check_list.push($(root[0]).attr('name'));
|
|
|
|
check_list.push("name");
|
|
|
|
}
|
|
|
|
xpath_object = self.parse_xml(xml,result.id);
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
2011-10-05 11:21:27 +00:00
|
|
|
$.each(function(){
|
|
|
|
});
|
|
|
|
self.search_object(val,check_list,[],position,xpath_object['main_object'],[]);
|
2011-09-30 12:49:32 +00:00
|
|
|
});
|
|
|
|
},
|
2011-10-05 11:21:27 +00:00
|
|
|
search_object: function(val ,list ,p_list ,position, xpath_object){
|
2011-09-30 12:49:32 +00:00
|
|
|
var self = this;
|
2011-10-03 09:49:31 +00:00
|
|
|
var main_list = $.trim(val.name.replace(/[^a-zA-Z 0-9 _]+/g,' ')).split(" ");
|
|
|
|
var insert = _.intersection(main_list,list);
|
|
|
|
var check = _.indexOf(p_list.child_id,xpath_object[0]);
|
|
|
|
if(check == -1){
|
|
|
|
if(insert.length == list.length){
|
2011-10-05 11:21:27 +00:00
|
|
|
var level = val.level;
|
|
|
|
$.each(xpath_object, function(key, val) {
|
|
|
|
self.increase_level(val, level)
|
2011-10-03 09:49:31 +00:00
|
|
|
});
|
|
|
|
var index = _.indexOf(p_list.child_id,val);
|
2011-10-05 11:21:27 +00:00
|
|
|
switch (position)
|
|
|
|
{
|
|
|
|
case "before":
|
|
|
|
if (index != 0) { index--; }
|
|
|
|
p_list.child_id.splice(index,0,xpath_object[0]);
|
|
|
|
break;
|
|
|
|
case "after":
|
|
|
|
index++;
|
|
|
|
p_list.child_id.splice(index,0,xpath_object[0]);
|
|
|
|
break;
|
|
|
|
case "inside":
|
|
|
|
val.child_id.push(xpath_object[0]);
|
|
|
|
break;
|
|
|
|
case "replace":
|
|
|
|
break;
|
2011-10-03 09:49:31 +00:00
|
|
|
}
|
2011-10-05 11:21:27 +00:00
|
|
|
|
2011-10-03 09:49:31 +00:00
|
|
|
}else{
|
2011-10-05 11:21:27 +00:00
|
|
|
if ( val.child_id.length != 0) { p_list = val; }
|
|
|
|
$.each(val.child_id, function(key, val) {
|
|
|
|
self.search_object(val, list, p_list, position, xpath_object);
|
2011-10-03 09:49:31 +00:00
|
|
|
});
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-03 09:49:31 +00:00
|
|
|
return return_list;
|
2011-09-30 12:49:32 +00:00
|
|
|
},
|
2011-10-05 11:21:27 +00:00
|
|
|
|
|
|
|
increase_level :function(val, level){
|
2011-09-30 12:49:32 +00:00
|
|
|
var self = this;
|
|
|
|
val.level = level;
|
2011-10-05 11:21:27 +00:00
|
|
|
$.each(val.child_id, function(key, val) {
|
2011-09-30 12:49:32 +00:00
|
|
|
self.increase_level(val,level+1);
|
|
|
|
});
|
2011-09-16 13:24:11 +00:00
|
|
|
},
|
2011-10-05 11:21:27 +00:00
|
|
|
|
2011-09-20 12:43:02 +00:00
|
|
|
edit_view : function(one_object){
|
2011-09-16 13:24:11 +00:00
|
|
|
var self = this;
|
2011-09-22 09:00:27 +00:00
|
|
|
this.dialog = new openerp.web.Dialog(this,{
|
|
|
|
modal: true,
|
|
|
|
title: 'Edit Xml',
|
|
|
|
width: 750,
|
|
|
|
height: 500,
|
|
|
|
buttons: {
|
2011-09-30 12:49:32 +00:00
|
|
|
"Inherited View": function(){
|
|
|
|
//todo
|
|
|
|
},
|
|
|
|
"Preview": function(){
|
|
|
|
//todo
|
|
|
|
},
|
|
|
|
"Close": function(){
|
|
|
|
$(this).dialog('destroy');
|
2011-09-20 12:43:02 +00:00
|
|
|
}
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
2011-09-22 09:00:27 +00:00
|
|
|
});
|
2011-09-30 12:49:32 +00:00
|
|
|
this.dialog.start().open();
|
|
|
|
this.dialog.$element.html(QWeb.render('view_editor', {
|
2011-10-05 11:21:27 +00:00
|
|
|
'data': one_object,
|
2011-09-30 12:49:32 +00:00
|
|
|
}));
|
2011-09-20 12:43:02 +00:00
|
|
|
|
2011-09-30 12:49:32 +00:00
|
|
|
$("tr[id^='viewedit-']").click(function() {
|
|
|
|
$("tr[id^='viewedit-']").removeClass('ui-selected');
|
|
|
|
$(this).addClass('ui-selected');
|
|
|
|
});
|
2011-09-22 09:00:27 +00:00
|
|
|
|
2011-09-30 12:49:32 +00:00
|
|
|
$("img[id^='parentimg-']").click(function() {
|
|
|
|
if ($(this).attr('src') == '/web/static/src/img/collapse.gif'){
|
|
|
|
$(this).attr('src', '/web/static/src/img/expand.gif');
|
|
|
|
self.on_expand(this);
|
|
|
|
}else{
|
|
|
|
$(this).attr('src', '/web/static/src/img/collapse.gif');
|
2011-10-03 09:49:31 +00:00
|
|
|
var id = this.id.split('-')[1];
|
|
|
|
self.on_collapse(this,one_object['parent_child_id'],one_object['main_object']);
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-22 09:45:13 +00:00
|
|
|
$("img[id^='side-']").click(function() {
|
2011-09-27 07:04:13 +00:00
|
|
|
var side = $(this).closest("tr[id^='viewedit-']")
|
2011-09-26 11:35:01 +00:00
|
|
|
var id_tr = (side.attr('id')).split('-')[1];
|
2011-09-30 12:49:32 +00:00
|
|
|
var img = side.find("img[id='parentimg-"+id_tr+"']").attr('src'); ;
|
|
|
|
var level = side.attr('level');
|
|
|
|
var list_shift =[];
|
|
|
|
var last_tr;
|
|
|
|
var cur_tr = side;
|
|
|
|
list_shift.push(side);
|
|
|
|
var next_tr;
|
2011-09-22 09:45:13 +00:00
|
|
|
switch (this.id)
|
|
|
|
{
|
2011-09-30 12:49:32 +00:00
|
|
|
case "side-add":
|
|
|
|
break;
|
|
|
|
case "side-remove":
|
|
|
|
break;
|
|
|
|
case "side-edit":
|
|
|
|
break;
|
|
|
|
case "side-up":
|
2011-09-26 11:35:01 +00:00
|
|
|
while(1){
|
2011-09-30 12:49:32 +00:00
|
|
|
var prev_tr = cur_tr.prev();
|
|
|
|
if(level >= prev_tr.attr('level') || prev_tr.length==0){
|
|
|
|
last_tr = prev_tr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur_tr = prev_tr;
|
|
|
|
}
|
|
|
|
if(img){
|
|
|
|
while(1){
|
|
|
|
next_tr = side.next();
|
2011-09-26 11:35:01 +00:00
|
|
|
if(next_tr.attr('level') <= level || next_tr.length==0){
|
|
|
|
break;
|
|
|
|
}else{
|
2011-09-30 12:49:32 +00:00
|
|
|
list_shift.push(next_tr);
|
|
|
|
side = next_tr;
|
2011-09-26 11:35:01 +00:00
|
|
|
}
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
2011-09-26 11:35:01 +00:00
|
|
|
}
|
2011-09-30 12:49:32 +00:00
|
|
|
if(last_tr.length!=0 && last_tr.attr('level') == level){
|
|
|
|
_.each(list_shift,function(rec){
|
|
|
|
$(last_tr).before(rec);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
break;
|
2011-09-27 05:05:03 +00:00
|
|
|
case "side-down":
|
2011-09-26 11:35:01 +00:00
|
|
|
if(img){
|
|
|
|
while(1){
|
2011-09-30 12:49:32 +00:00
|
|
|
next_tr = cur_tr.next();
|
|
|
|
if(next_tr.attr('level') <= level || next_tr.length==0){
|
|
|
|
last_tr = next_tr;
|
|
|
|
break;
|
|
|
|
}else{
|
2011-09-27 05:05:03 +00:00
|
|
|
list_shift.push(next_tr);
|
|
|
|
cur_tr = next_tr;
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
last_tr = cur_tr.next();
|
|
|
|
}
|
2011-09-27 05:05:03 +00:00
|
|
|
if(last_tr.length != 0 && last_tr.attr('level')==level){
|
|
|
|
var last_tr_id = (last_tr.attr('id')).split('-')[1];
|
|
|
|
img = last_tr.find("img[id='parentimg-"+last_tr_id+"']").attr('src');
|
|
|
|
if(img){
|
|
|
|
$("img[id='parentimg-"+last_tr_id+"']").attr('src', '/web/static/src/img/expand.gif');
|
|
|
|
while(1){
|
|
|
|
var next_tr = last_tr.next();
|
|
|
|
if (next_tr.attr('level') <= level || next_tr.length==0){break;}
|
2011-09-30 12:49:32 +00:00
|
|
|
next_tr.hide();
|
|
|
|
last_tr = next_tr;
|
2011-09-27 05:05:03 +00:00
|
|
|
}
|
2011-09-30 12:49:32 +00:00
|
|
|
}
|
2011-09-27 05:05:03 +00:00
|
|
|
list_shift.reverse();
|
|
|
|
_.each(list_shift,function(rec){
|
2011-09-30 12:49:32 +00:00
|
|
|
$(last_tr).after(rec);
|
|
|
|
});
|
2011-09-22 09:45:13 +00:00
|
|
|
}
|
2011-09-30 12:49:32 +00:00
|
|
|
break;
|
2011-09-22 09:45:13 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-22 09:00:27 +00:00
|
|
|
},
|
|
|
|
on_expand: function(self){
|
|
|
|
var level = $(self).closest("tr[id^='viewedit-']").attr('level');
|
|
|
|
var cur_tr = $(self).closest("tr[id^='viewedit-']");
|
|
|
|
while (1){
|
|
|
|
var nxt_tr = cur_tr.next();
|
|
|
|
if (nxt_tr.attr('level') > level){
|
|
|
|
cur_tr = nxt_tr;
|
|
|
|
nxt_tr.hide();
|
2011-09-26 11:35:01 +00:00
|
|
|
}else return nxt_tr;
|
2011-09-22 09:00:27 +00:00
|
|
|
}
|
|
|
|
},
|
2011-10-03 09:49:31 +00:00
|
|
|
on_collapse: function(self,parent_child_id,id,main_object){
|
2011-09-22 09:00:27 +00:00
|
|
|
var id = self.id.split('-')[1];
|
|
|
|
var datas = _.detect(parent_child_id,function(res){
|
|
|
|
return res.key == id;
|
|
|
|
});
|
|
|
|
_.each(datas.value,function(rec){
|
|
|
|
var tr = $("tr[id='viewedit-"+rec+"']");
|
|
|
|
tr.find("img[id='parentimg-"+rec+"']").attr('src','/web/static/src/img/expand.gif');
|
|
|
|
tr.show();
|
|
|
|
});
|
|
|
|
}
|
2011-08-23 10:39:41 +00:00
|
|
|
});
|
|
|
|
};
|