[IMP]created object key,value for parent,child.
[odoo/odoo.git] / addons / web / static / src / js / view_editor.js
1 openerp.web.view_editor = function(openerp) {
2 var QWeb = openerp.web.qweb;
3 openerp.web.ViewEditor =   openerp.web.Widget.extend({
4     init: function(parent, element_id, dataset, view, options) {
5         this._super(parent);
6         this.element_id = element_id
7         this.parent = parent
8         this.dataset = dataset;
9         this.model = dataset.model;
10         this.xml_id = 0;
11     },
12     start: function() {
13         this.View_editor();
14     },
15     View_editor : function(){
16         var self = this;
17         var action = {
18             name:'ViewEditor',
19             context:this.session.user_context,
20             domain: [["model", "=", this.dataset.model]],
21             res_model: 'ir.ui.view',
22             views : [[false, 'list']],
23             type: 'ir.actions.act_window',
24             target: "current",
25             limit : 80,
26             auto_search : true,
27             flags: {
28                 sidebar: false,
29                 views_switcher: false,
30                 action_buttons:false,
31                 search_view:false,
32                 pager:false,
33                 radio:true
34             },
35         };
36         var action_manager = new openerp.web.ActionManager(this);
37         this.dialog = new openerp.web.Dialog(this,{
38             modal: true,
39             title: 'ViewEditor',
40             width: 750,
41             height: 500,
42             buttons: {
43                 "Create": function(){
44                     
45                 },
46                 "Edit": function(){
47                     self.xml_id = 0 ;
48                     self.get_data();
49                     
50                 },
51                 "Close": function(){
52                  $(this).dialog('destroy');
53                 }
54             },
55
56         });
57        this.dialog.start(); 
58        this.dialog.open();
59        action_manager.appendTo(this.dialog);
60        action_manager.do_action(action);
61     },
62
63     check_attr:function(xml,tag,level){
64         var obj = new Object();
65         obj.child_id = [];
66         obj.id = this.xml_id++;
67         obj.level = level;
68         var att_list = [];
69         var name1 = "<" + tag;
70         $(xml).each(function() {
71             att_list = this.attributes;
72             att_list = _.select(att_list, function(attrs){
73                 if (tag != 'button'){
74                    if(attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index"){
75                         name1 += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';} 
76                 }else{
77                     if(attrs.nodeName == "name"){
78                         name1 += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';} 
79                 }
80                 });                
81                 name1+= ">";
82          });  
83         obj.name = name1;
84         return obj;
85     },
86
87     save_object : function(val,parent_list,child_obj_list){
88         var self = this;
89         var check_id = parent_list[0];
90         var p_list = parent_list.slice(1);
91         if(val.child_id.length != 0){
92              $.each(val.child_id, function(key,val) {
93                 if(val.id==check_id){
94                     if(p_list.length!=0){
95                         self.save_object(val,p_list,child_obj_list);
96                     }else{
97                         val.child_id = child_obj_list;
98                         return;
99                     }
100                 }
101             });
102         }else{
103             val.child_id = child_obj_list;
104         }
105     },
106
107     children_function : function(xml,root,parent_list,parent_id,main_object,parent_child_id){
108         var self = this;
109         var child_obj_list = [];
110         var parent_child_id = parent_child_id;
111         var parent_list = parent_list;
112         var main_object = main_object;
113         var children_list = $(xml).filter(root).children();
114         var parents = $(children_list[0]).parents().get();
115         _.each(children_list, function(child_node){
116             var string = self.check_attr(child_node,child_node.tagName.toLowerCase(),parents.length);
117             child_obj_list.push(string);
118         });
119         if(children_list.length != 0){
120             parent_child_id.push({key: parent_id, value: child_obj_list});
121             
122             var parents = $(children_list[0]).parents().get();
123             if(parents.length <= parent_list.length){
124                 parent_list.splice(parents.length-1);}
125             parent_list.push(parent_id);
126             $.each(main_object, function(key,val) {
127                 self.save_object(val,parent_list.slice(1),child_obj_list); 
128             });
129         }
130         for(var i=0;i<children_list.length;i++){
131             self.children_function
132       (children_list[i],children_list[i].tagName.toLowerCase(),parent_list,child_obj_list[i].id,main_object,parent_child_id);
133         }
134         return {"main_object":main_object,"parent_child_id":parent_child_id};
135     },
136     get_data : function(){
137         
138             var self = this;
139             var view_id =(($("input[name='radiogroup']:checked").parent()).parent()).attr('data-id');
140             var ve_dataset = new openerp.web.DataSet(this,'ir.ui.view');
141             ve_dataset.read_ids([parseInt(view_id)],['arch'],function (arch){
142                     var arch = arch[0].arch;
143                     var root = $(arch).filter(":first")[0];
144                     var tag = root.tagName.toLowerCase();
145                     var root_object = self.check_attr(root,tag,0);
146                     var one_object = self.children_function(arch,tag,[],0,[root_object],[]);
147                     return self.edit_view(one_object);
148                 });
149     },
150     edit_view : function(one_object){
151         var self = this;
152             this.dialog = new openerp.web.Dialog(this,{
153                 modal: true,
154                 title: 'Edit Xml',
155                 width: 750,
156                 height: 500,
157                 buttons: {
158                         "Inherited View": function(){
159                             
160                         },
161                         "Preview": function(){
162                             
163                         },
164                         "Close": function(){
165                             $(this).dialog('destroy');
166                         }
167                     }
168             });
169             this.dialog.start().open();
170             this.dialog.$element.html(QWeb.render('view_editor', {
171             'data': one_object['main_object'],
172             }));
173
174             $("tr[id^='viewedit-']").click(function() {
175                 $("tr[id^='viewedit-']").removeClass('ui-selected');
176                 $(this).addClass('ui-selected');
177             });
178
179             $("img[id^='parentimg-']").click(function() {
180                 if ($(this).attr('src') == '/web/static/src/img/collapse.gif'){
181                     $(this).attr('src', '/web/static/src/img/expand.gif');
182                 }else{
183                     $(this).attr('src', '/web/static/src/img/collapse.gif');
184                 }
185             });
186     }
187
188         
189 });
190 };