vidhin mehta[Open Erp] has proposed merging 
lp:~openerp-dev/openerp-web/trunk-vieweditor-save-move-functionality-vme into 
lp:~openerp-dev/openerp-web/trunk-vieweditor-sma.

Requested reviews:
  OpenERP R&D Team (openerp-dev)

For more details, see:
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-vieweditor-save-move-functionality-vme/+merge/80323

Implement code for functionality of save and move tag of xml in view editor.
-- 
https://code.launchpad.net/~openerp-dev/openerp-web/trunk-vieweditor-save-move-functionality-vme/+merge/80323
Your team OpenERP R&D Team is requested to review the proposed merge of 
lp:~openerp-dev/openerp-web/trunk-vieweditor-save-move-functionality-vme into 
lp:~openerp-dev/openerp-web/trunk-vieweditor-sma.
=== modified file 'addons/web/static/src/js/view_editor.js'
--- addons/web/static/src/js/view_editor.js	2011-10-14 11:21:01 +0000
+++ addons/web/static/src/js/view_editor.js	2011-10-25 11:32:26 +0000
@@ -44,7 +44,7 @@
                 //to do
             },
             "Edit": function(){
-                self.xml_id = 0 ;
+                self.xml_id=0;
                 self.get_data();
             },
             "Close": function(){
@@ -67,18 +67,16 @@
         obj.att_list.push(tag);
         $(xml).each(function() {
             _.each(this.attributes, function(attrs){
-                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 + '"';
-                }
-                if (attrs.nodeName != "position") {
-                    obj.att_list.push( [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 + '"'; }
+            }
+            obj.att_list.push( [attrs.nodeName,attrs.nodeValue] );
             });
-            render_name+= ">";
+        render_name+= ">";
         });
         obj.name = render_name;
         return obj;
@@ -89,7 +87,7 @@
         var p_list = parent_list.slice(1);
         if (val.child_id.length != 0) {
             _.each(val.child_id, function(val, key) {
-                if (val.id==check_id) {
+                if (val.id == check_id) {
                     if (p_list.length!=0) {
                         self.save_object(val, p_list, child_obj_list);
                     } else {
@@ -134,18 +132,22 @@
     },
     get_data: function() {
         var self = this;
+
+        var view_arch_list = [];
         var view_id =((this.view_edit_dialog.$element.find("input[name='radiogroup']:checked").parent()).parent()).attr('data-id');
         var ve_dataset = new openerp.web.DataSet(this, 'ir.ui.view');
         ve_dataset.read_ids([parseInt(view_id)], ['arch'], function (arch) {
             one_object = self.parse_xml(arch[0].arch,view_id);
-            one_object.arch = arch[0].arch;
+            view_arch_list.push({"view_id" : view_id, "arch" : arch[0].arch});
             dataset = new openerp.web.DataSetSearch(self, 'ir.ui.view', null, null);
             dataset.read_slice([], {domain : [['inherit_id','=', parseInt(view_id)]]}, function (result) {
                 _.each(result, function(res) {
+                    view_arch_list.push({"view_id":res.id,"arch":res.arch});
                     self.inherit_view(one_object, res);
                 });
                 return self.edit_view({"main_object": one_object,
-                         "parent_child_id": self.parent_child_list(one_object, [])});
+                         "parent_child_id": self.parent_child_list(one_object, []),
+                         "arch": view_arch_list});
             });
         });
     },
@@ -178,7 +180,13 @@
                     check_list.push(_.without($.trim(part.replace(/[^a-zA-Z 0-9 _]+/g,'!')).split("!"),""));
                 });
             } else {
-                    check_list = [_.flatten(xpath_object[0].child_id[0].att_list)];
+                    var temp = [];
+                    _.each(xpath_object[0].child_id[0].att_list, function(list){
+                        if(!_.include(list, "position")){
+                           temp.push(list);
+                        }
+                    });
+                    check_list = [_.flatten(temp)];
             }
             self.full_path_search(check_list ,one_object ,xpath_object);
         });
@@ -216,7 +224,7 @@
                         var main_list = _.flatten(element.att_list);
                         return _.include(main_list, check[0]);
                     });
-                    if(list_1 != 0){
+                    if(list_1.length != 0){
                         (check_list.length == 1)? obj = list_1[0] : check_list.shift();
                     }
                     break;
@@ -288,12 +296,23 @@
             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 level = parseInt(side.attr('level'));
             var list_shift =[];
             var last_tr;
             var cur_tr = side;
             list_shift.push(side);
             var next_tr;
+            var ls = side;
+            var view_id;
+            var view_xml_id;
+            while(1){
+                ls = ls.prev();
+                if(($(ls).find('a').text()).search("view_id") != -1 && parseInt(ls.attr('level')) < level){
+                    view_id = parseInt(($(ls).find('a').text()).replace(/[^0-9]+/g,''));
+                    view_xml_id = (ls.attr('id')).split('-')[1];
+                    break;
+                }
+            }
             switch (this.id) {
                 case "side-add":
                     break;
@@ -304,7 +323,7 @@
                 case "side-up":
                     while (1) {
                         var prev_tr = cur_tr.prev();
-                        if(level >= prev_tr.attr('level') || prev_tr.length == 0) {
+                        if(level >= parseInt(prev_tr.attr('level')) || prev_tr.length == 0) {
                            last_tr = prev_tr;
                            break;
                         }
@@ -313,7 +332,7 @@
                     if (img) {
                         while (1) {
                             next_tr = side.next();
-                            if (next_tr.attr('level') <= level || next_tr.length == 0) {
+                            if ( parseInt(next_tr.attr('level')) <= level || next_tr.length == 0) {
                                 break;
                             } else {
                                 list_shift.push(next_tr);
@@ -321,17 +340,20 @@
                             }
                         }
                     }
-                    if (last_tr.length != 0 && last_tr.attr('level') == level) {
+                    if (last_tr.length != 0 
+                            && parseInt(last_tr.attr('level')) == level
+                                && ($(last_tr).find('a').text()).search("view_id") == -1) {
                         _.each(list_shift, function(rec) {
                              $(last_tr).before(rec);
                         });
+                        self.save_move_arch(one_object, view_id, view_xml_id, id_tr, level, "up");
                     }
                 break;
             case "side-down":
                 if (img) {
                     while (1) {
                         next_tr = cur_tr.next();
-                        if (next_tr.attr('level') <= level || next_tr.length == 0) {
+                        if ( parseInt(next_tr.attr('level')) <= level || next_tr.length == 0) {
                             last_tr = next_tr;
                             break;
                         } else {
@@ -343,7 +365,7 @@
                 else {
                     last_tr = cur_tr.next();
                 }
-                if (last_tr.length != 0 && last_tr.attr('level') == level) {
+                if (last_tr.length != 0 &&  parseInt(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) {
@@ -355,15 +377,95 @@
                             last_tr = next_tr;
                         }
                     }
+                    
                     list_shift.reverse();
-                    _.each(list_shift, function(rec) {
-                       $(last_tr).after(rec);
-                    });
+                    if(($(side.next()).find('a').text()).search("view_id") == -1){
+                        _.each(list_shift, function(rec) {
+                           $(last_tr).after(rec);
+                        });
+                        self.save_move_arch(one_object, view_id, view_xml_id, id_tr, level, "down");
+                    }
                 }
                 break;
             }
         });
     },
+    save_move_arch: function(one_object, view_id, view_xml_id, id_tr, level, move_direct){
+        var self = this;
+        var arch = _.detect(one_object['arch'],function(element){
+            return element.view_id == view_id;
+        });
+        var obj = self.get_view_object(view_xml_id, one_object['main_object'], []);
+        if(($(arch.arch).filter("data")).length != 0 && view_xml_id != 0){
+            var check_list = _.flatten(obj[0].child_id[0].att_list);
+            arch.arch = _.detect($(arch.arch).children(), function(xml_child){
+                var temp_obj = self.check_attr(xml_child, xml_child.tagName.toLowerCase());
+                var main_list = _.flatten(temp_obj.att_list);
+                check_list = _.uniq(check_list);
+                var insert = _.intersection(main_list,check_list);
+                if(insert.length == check_list.length ){return xml_child;}
+            });
+        }
+        return self.get_node(arch.arch, obj[0].child_id[0], parseInt(id_tr), [], parseInt(level),
+                        parseInt(view_id), arch, move_direct);
+    },
+
+    get_view_object: function(view_xml_id, one_object,result){
+        var self = this;
+        if(result.length==0){
+            var check = _.detect(one_object , function(obj){
+                return view_xml_id==obj.id;
+            });
+            if(check){result.push(check);};
+            _.each(one_object, function(obj){
+               self.get_view_object(view_xml_id, obj.child_id, result);
+            });
+        }
+        return result;
+    },
+
+    get_node: function(arch1, obj, id, child_list, level, view_id, arch, move_direct){
+        var self = this;
+        var children_list =  $(arch1).children();
+        var list_obj_xml = _.zip(children_list,obj.child_id);
+        if(id){
+            if(obj.id == id){
+                var id;
+                var parent = $(arch1).parents();
+                var index = _.indexOf(child_list,obj)
+                var re_insert_obj = child_list.splice(index,1);
+                if(move_direct == "down"){
+                    var next = $(arch1).next();
+                    $(next).after(arch1);
+                    child_list.splice(index+1, 0, re_insert_obj[0]);
+                }else{
+                    var prev = $(arch1).prev();
+                    $(prev).before(arch1);
+                    child_list.splice(index-1, 0, re_insert_obj[0]);
+                }
+                parent = parent[parent.length-1];
+                var convert_to_utf = "";
+                var xml_serilalizer = new XMLSerializer();
+                var stream = {
+                    write : function(string)
+                    {convert_to_utf = convert_to_utf + string + "";}
+                };
+                xml_serilalizer.serializeToStream(parent, stream, "UTF-8");
+                convert_to_utf = convert_to_utf.replace('xmlns="http://www.w3.org/1999/xhtml";', "");
+                convert_to_utf = '<?xml version="1.0" encoding="utf-8"?>' + convert_to_utf;
+                arch.arch = convert_to_utf;
+                dataset = new openerp.web.DataSet(this, 'ir.ui.view');
+                    dataset.write(parseInt(view_id),{"arch":convert_to_utf},function(r){
+                });
+            }
+            if(obj.level <= level){
+                _.each(list_obj_xml, function(child_node){
+                    self.get_node(child_node[0], child_node[1], id, obj.child_id, level, view_id, arch, move_direct);
+                });
+            }
+        }
+    },
+
     on_expand: function(expand_img){
         var level = parseInt($(expand_img).closest("tr[id^='viewedit-']").attr('level'));
         var cur_tr = $(expand_img).closest("tr[id^='viewedit-']");
@@ -387,5 +489,6 @@
             tr.show();
         });
     }
+
 });
-};
\ No newline at end of file
+};

_______________________________________________
Mailing list: https://launchpad.net/~openerp-dev-gtk
Post to     : [email protected]
Unsubscribe : https://launchpad.net/~openerp-dev-gtk
More help   : https://help.launchpad.net/ListHelp

Reply via email to