[IMP]include main xml tag in main_object for up down posisition of node.
[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 vidhin = xml;
69         var att_list = [];
70         var name1 = "<" + tag;
71         var xml_tag = "<" + tag;
72         $(xml).each(function() {
73             att_list = this.attributes;
74             att_list = _.select(att_list, function(attrs){
75                 xml_tag += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';
76                 if (tag != 'button'){
77                    if(attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index"){
78                         name1 += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
79                 }else{
80                     if(attrs.nodeName == "name"){
81                         name1 += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
82                 }
83                 });
84                 name1+= ">";
85                 xml_tag+= ">";
86          });
87         obj.main_xml = xml_tag;
88         obj.name = name1;
89         return obj;
90     },
91
92     save_object : function(val,parent_list,child_obj_list){
93         var self = this;
94         var check_id = parent_list[0];
95         var p_list = parent_list.slice(1);
96         if(val.child_id.length != 0){
97              $.each(val.child_id, function(key,val) {
98                 if(val.id==check_id){
99                     if(p_list.length!=0){
100                         self.save_object(val,p_list,child_obj_list);
101                     }else{
102                         val.child_id = child_obj_list;
103                         return;
104                     }
105                 }
106             });
107         }else{
108             val.child_id = child_obj_list;
109         }
110     },
111
112     children_function : function(xml,root,parent_list,parent_id,main_object,parent_child_id){
113         var self = this;
114         var child_obj_list = [];
115         var parent_child_id = parent_child_id;
116         var parent_list = parent_list;
117         var main_object = main_object;
118         var children_list = $(xml).filter(root).children();
119         var parents = $(children_list[0]).parents().get();
120         _.each(children_list, function(child_node){
121             var string = self.check_attr(child_node,child_node.tagName.toLowerCase(),parents.length);
122             child_obj_list.push(string);
123         });
124        
125         if(children_list.length != 0){
126             var child_ids = _.map(child_obj_list ,function(num){return num.id;});
127             parent_child_id.push({'key': parent_id, 'value': child_ids});
128             var parents = $(children_list[0]).parents().get();
129             if(parents.length <= parent_list.length){
130                 parent_list.splice(parents.length-1);}
131             parent_list.push(parent_id);
132             $.each(main_object, function(key,val) {
133                 self.save_object(val,parent_list.slice(1),child_obj_list);
134             });
135         }
136         for(var i=0;i<children_list.length;i++){
137             self.children_function
138       (children_list[i],children_list[i].tagName.toLowerCase(),parent_list,child_obj_list[i].id,main_object,parent_child_id);
139         }
140         return {"main_object":main_object,"parent_child_id":parent_child_id};
141     },
142     parse_xml :function(arch){
143         var self = this;
144         var root = $(arch).filter(":first")[0];
145         var tag = root.tagName.toLowerCase();
146         var root_object = self.check_attr(root,tag,0);
147         var one_object = self.children_function(arch,tag,[],0,[root_object],[]);
148         return self.edit_view(one_object);
149     },
150     get_data : function(){
151             var self = this;
152             var view_id =(($("input[name='radiogroup']:checked").parent()).parent()).attr('data-id');
153             dataset = new openerp.web.DataSetSearch(this,'ir.ui.view', null, null);
154             dataset.read_slice([],{domain : [['inherit_id','=',parseInt(view_id)]]},function (result) {
155                 _.each(result ,function(num){console.log(result);});
156             });
157             var ve_dataset = new openerp.web.DataSet(this,'ir.ui.view');
158             ve_dataset.read_ids([parseInt(view_id)],['arch'],function (arch){
159                 self.parse_xml(arch[0].arch);
160                 });
161     },
162     edit_view : function(one_object){
163         var self = this;
164         this.dialog = new openerp.web.Dialog(this,{
165             modal: true,
166             title: 'Edit Xml',
167             width: 750,
168             height: 500,
169             buttons: {
170                     "Inherited View": function(){
171
172                     },
173                     "Preview": function(){
174
175                     },
176                     "Close": function(){
177                         $(this).dialog('destroy');
178                     }
179                 }
180         });
181         this.dialog.start().open();
182         this.dialog.$element.html(QWeb.render('view_editor', {
183         'data': one_object['main_object'],
184         }));
185
186         $("tr[id^='viewedit-']").click(function() {
187             $("tr[id^='viewedit-']").removeClass('ui-selected');
188             $(this).addClass('ui-selected');
189         });
190
191         $("img[id^='parentimg-']").click(function() {
192             if ($(this).attr('src') == '/web/static/src/img/collapse.gif'){
193                 $(this).attr('src', '/web/static/src/img/expand.gif');
194                 self.on_expand(this);
195             }else{
196                 $(this).attr('src', '/web/static/src/img/collapse.gif');
197                 self.on_collapse(this,one_object['parent_child_id']);
198             }
199         });
200     $("img[id^='side-']").click(function() {
201         var side = $(this).closest("'tr[id^='viewedit-']'");
202         switch (this.id)
203         {
204         case "side-add":
205           break;
206         case "side-remove":
207           break;
208         case "side-edit":
209           break;
210         case "side-up":
211             if(side.prev().attr('level') == side.attr('level')){
212                 $(side.prev()).before(side);
213             }
214           break;
215         case "side-down":
216             if(side.next().attr('level') == side.attr('level')){
217                 $(side.next()).after(side);
218             }
219           break;
220         }
221     });
222     },
223     search_object:function(main_object){
224     
225     },
226     on_expand: function(self){
227         var level = $(self).closest("tr[id^='viewedit-']").attr('level');
228         var cur_tr = $(self).closest("tr[id^='viewedit-']");
229         while (1){
230             var nxt_tr = cur_tr.next();
231             if (nxt_tr.attr('level') > level){
232                 cur_tr = nxt_tr;
233                 nxt_tr.hide();
234             }else break;
235         }
236     },
237     on_collapse: function(self,parent_child_id){
238         var id = self.id.split('-')[1];
239         var datas = _.detect(parent_child_id,function(res){
240             return res.key == id;
241         });
242         _.each(datas.value,function(rec){
243             var tr = $("tr[id='viewedit-"+rec+"']");
244             tr.find("img[id='parentimg-"+rec+"']").attr('src','/web/static/src/img/expand.gif');
245             tr.show();
246         });
247     }
248 });
249 };