From 59ef6514220f2e1d6a69024688abb0479e914dab Mon Sep 17 00:00:00 2001 From: "Yogesh (OpenERP)" Date: Tue, 4 Oct 2011 16:15:50 +0530 Subject: [PATCH] [IMP] code refactore in view editor. bzr revid: ysa@tinyerp.com-20111004104550-abxppmgpbbqedv1k --- addons/web/static/src/js/view_editor.js | 442 ++++++++++++------------ 1 file changed, 219 insertions(+), 223 deletions(-) diff --git a/addons/web/static/src/js/view_editor.js b/addons/web/static/src/js/view_editor.js index 2650f966c7e..4bed60d67cd 100644 --- a/addons/web/static/src/js/view_editor.js +++ b/addons/web/static/src/js/view_editor.js @@ -16,52 +16,52 @@ openerp.web.ViewEditor = openerp.web.Widget.extend({ }, 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 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'); + } }, - }; - 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); + }); + this.dialog.start(); + this.dialog.open(); + action_manager.appendTo(this.dialog); + action_manager.do_action(action); }, - check_attr:function(xml,tag,level){ + check_attr: function(xml, tag, level) { var obj = new Object(); obj.child_id = []; obj.id = this.xml_id++; @@ -71,45 +71,45 @@ openerp.web.ViewEditor = openerp.web.Widget.extend({ var xml_tag = "<" + tag; $(xml).each(function() { att_list = this.attributes; - att_list = _.select(att_list, function(attrs){ - xml_tag += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"'; - if (tag != 'button'){ - if(attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index"){ - render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';} - }else{ - if(attrs.nodeName == "name"){ - render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';} - } - }); - render_name+= ">"; - xml_tag+= ">"; + att_list = _.select(att_list, function(attrs) { + xml_tag += ' ' + attrs.nodeName + '=' + '"' + attrs.nodeValue + '"'; + if (tag != 'button') { + if (attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index") { + render_name += ' ' + attrs.nodeName + '=' + '"' + attrs.nodeValue + '"'; + } + } else { + if (attrs.nodeName == "name") render_name += ' ' + attrs.nodeName + '=' + '"' + attrs.nodeValue + '"'; + } + }); + render_name += ">"; + xml_tag += ">"; }); obj.main_xml = xml_tag; obj.name = render_name; return obj; }, - save_object : function(val,parent_list,child_obj_list){ + save_object : function(val, parent_list, child_obj_list) { var self = this; var check_id = parent_list[0]; var p_list = parent_list.slice(1); - 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{ + if (val.child_id.length != 0) { + _.each(val.child_id, function(val, key) { + if (val.id==check_id) { + if (p_list.length!=0) { + self.save_object(val, p_list, child_obj_list); + } else { val.child_id = child_obj_list; - return; + return false; } } }); - }else{ + } else { val.child_id = child_obj_list; } }, - children_function : function(xml,root,parent_list,parent_id,main_object,parent_child_id){ + children_function : function(xml, root, parent_list, parent_id, main_object, parent_child_id) { var self = this; var child_obj_list = []; var parent_child_id = parent_child_id; @@ -117,128 +117,125 @@ openerp.web.ViewEditor = openerp.web.Widget.extend({ var main_object = main_object; var children_list = $(xml).filter(root).children(); var parents = $(children_list[0]).parents().get(); - _.each(children_list, function(child_node){ + _.each(children_list, function (child_node) { var string = self.check_attr(child_node,child_node.tagName.toLowerCase(),parents.length); child_obj_list.push(string); }); - - if(children_list.length != 0){ - var child_ids = _.map(child_obj_list ,function(num){return num.id;}); + if (children_list.length != 0) { + var child_ids = _.map(child_obj_list, function (num) { return num.id; }); parent_child_id.push({'key': parent_id, 'value': child_ids}); var parents = $(children_list[0]).parents().get(); - if(parents.length <= parent_list.length){ - parent_list.splice(parents.length-1); + if (parents.length <= parent_list.length) { + parent_list.splice(parents.length - 1); } parent_list.push(parent_id); - $.each(main_object, function(key,val) { - self.save_object(val,parent_list.slice(1),child_obj_list); + _.each(main_object, function (val, key) { + self.save_object(val, parent_list.slice(1), child_obj_list); }); } - for(var i=0;i= prev_tr.attr('level') || prev_tr.length==0){ - last_tr = prev_tr; - break; + $("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'); + var id = this.id.split('-')[1]; + self.on_collapse(this,one_object['parent_child_id'], one_object['main_object']); + } + }); + $("img[id^='side-']").click(function() { + var side = $(this).closest("tr[id^='viewedit-']") + var id_tr = (side.attr('id')).split('-')[1]; + 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; + switch (this.id) { + case "side-add": + break; + case "side-remove": + break; + case "side-edit": + break; + case "side-up": + while (1) { + 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; } - cur_tr = prev_tr; - } - if(img){ - while(1){ - next_tr = side.next(); - if(next_tr.attr('level') <= level || next_tr.length==0){ + if (img) { + while (1) { + next_tr = side.next(); + if (next_tr.attr('level') <= level || next_tr.length == 0) { + break; + } else { + list_shift.push(next_tr); + side = next_tr; + } + } + } + if (last_tr.length != 0 && last_tr.attr('level') == level) { + _.each(list_shift, function(rec) { + $(last_tr).before(rec); + }); + } + break; + case "side-down": + if (img) { + while (1) { + next_tr = cur_tr.next(); + if (next_tr.attr('level') <= level || next_tr.length == 0) { + last_tr = next_tr; break; - }else{ + } else { list_shift.push(next_tr); - side = next_tr; - } - } + cur_tr = next_tr; + } + } } - if(last_tr.length!=0 && last_tr.attr('level') == level){ - _.each(list_shift,function(rec){ - $(last_tr).before(rec); + else { + last_tr = cur_tr.next(); + } + 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; + next_tr.hide(); + last_tr = next_tr; + } + } + list_shift.reverse(); + _.each(list_shift, function(rec) { + $(last_tr).after(rec); }); } - break; - case "side-down": - if(img){ - while(1){ - next_tr = cur_tr.next(); - if(next_tr.attr('level') <= level || next_tr.length==0){ - last_tr = next_tr; - break; - }else{ - list_shift.push(next_tr); - cur_tr = next_tr; - } - } + break; } - else{ - last_tr = cur_tr.next(); - } - 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;} - next_tr.hide(); - last_tr = next_tr; - } - } - list_shift.reverse(); - _.each(list_shift,function(rec){ - $(last_tr).after(rec); - }); - } - break; - } - }); + }); }, on_expand: function(self){ var level = $(self).closest("tr[id^='viewedit-']").attr('level'); var cur_tr = $(self).closest("tr[id^='viewedit-']"); - while (1){ + while (1) { var nxt_tr = cur_tr.next(); - if (nxt_tr.attr('level') > level){ + if (nxt_tr.attr('level') > level) { cur_tr = nxt_tr; nxt_tr.hide(); - }else return nxt_tr; + } else return nxt_tr; } }, - on_collapse: function(self,parent_child_id,id,main_object){ + on_collapse: function(self, parent_child_id, id, main_object) { var id = self.id.split('-')[1]; - var datas = _.detect(parent_child_id,function(res){ + var datas = _.detect(parent_child_id,function(res) { return res.key == id; }); - _.each(datas.value,function(rec){ + _.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();