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) {
6 this.element_id = element_id
8 this.dataset = dataset;
9 this.model = dataset.model;
17 View_editor : function(){
21 context:this.session.user_context,
22 domain: [["model", "=", this.dataset.model]],
23 res_model : 'ir.ui.view',
24 views : [[false, 'list']],
25 type: 'ir.actions.act_window',
31 views_switcher: false,
38 var action_manager = new openerp.web.ActionManager(this);
39 this.dialog = new openerp.web.Dialog(this,{
53 $(this).dialog('destroy');
59 action_manager.appendTo(this.dialog);
60 action_manager.do_action(action);
63 check_attr: function(xml ,tag, level) {
64 var obj = new Object();
66 obj.id = this.xml_id++;
68 var render_name = "<" + tag;
69 $(xml).each(function() {
70 _.each(this.attributes, function(attrs){
71 if (tag != 'button') {
72 if (attrs.nodeName == "string" || attrs.nodeName == "name" || attrs.nodeName == "index") {
73 render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
75 if (attrs.nodeName == "name") {
76 render_name += ' ' +attrs.nodeName+'='+'"'+attrs.nodeValue+'"';}
81 obj.name = render_name;
85 save_object: function(val, parent_list, child_obj_list) {
87 var check_id = parent_list[0];
88 var p_list = parent_list.slice(1);
89 if (val.child_id.length != 0) {
90 $.each(val.child_id, function(key, val) {
91 if (val.id == check_id) {
92 if (p_list.length != 0) {
93 self.save_object(val, p_list, child_obj_list);
95 val.child_id = child_obj_list;
101 val.child_id = child_obj_list;
105 children_function: function(xml, root, parent_list, parent_id, main_object){
107 var child_obj_list = [];
108 var children_list = $(xml).filter(root).children();
109 var parents = $(children_list[0]).parents().get();
110 _.each(children_list, function(child_node){
111 var string = self.check_attr(child_node,child_node.tagName.toLowerCase(),parents.length);
112 child_obj_list.push(string);
114 if (children_list.length != 0) {
115 var parents = $(children_list[0]).parents().get();
116 if (parents.length <= parent_list.length) {
117 parent_list.splice(parents.length-1);
119 parent_list.push(parent_id);
120 $.each(main_object, function(key, val) {
121 self.save_object(val, parent_list.slice(1), child_obj_list);
124 for (var i=0;i<children_list.length;i++) {
125 self.children_function
126 (children_list[i], children_list[i].tagName.toLowerCase(),
127 parent_list, child_obj_list[i].id, main_object);
132 parse_xml: function(arch,view_id) {
134 var root = $(arch).filter(":first")[0];
135 var tag = root.tagName.toLowerCase();
136 var obj = new Object();
138 obj.id = this.xml_id++;
140 obj.name = "<view view_id='"+view_id+"'>"
141 var root_object = self.check_attr(root,tag,0);
142 f_obj = self.children_function(arch, tag, [], this.xml_id-1, [root_object], [])
143 obj.child_id.push(f_obj[0]);
149 get_data: function() {
151 var view_id =(($("input[name='radiogroup']:checked").parent()).parent()).attr('data-id');
152 var ve_dataset = new openerp.web.DataSet(this,'ir.ui.view');
153 ve_dataset.read_ids([parseInt(view_id)], ['arch'], function (arch) {
154 one_object = self.parse_xml(arch[0].arch,view_id);
155 dataset = new openerp.web.DataSetSearch(self, 'ir.ui.view', null, null);
156 dataset.read_slice([],{domain : [['inherit_id','=',parseInt(view_id)]]},function (result) {
158 return self.edit_view({"main_object": one_object,
159 "parent_child_id": self.parent_child_list(one_object, [])});
163 parent_child_list: function(one_object, p_list) {
165 _.each(one_object , function(element){
166 if(element.child_id.length != 0){
167 p_list.push({"key":element.id,"value":_.pluck(element.child_id, 'id')});
168 self.parent_child_list(element.child_id,p_list);
173 inherit_view: function(one_object, result){
175 var root = $(result.arch).filter('*');
178 if (root[0].tagName.toLowerCase() == "data") {
179 xml_list = $(root[0]).children();
181 xml_list.push(root[0]);
183 _.each(xml_list , function(xml){
186 var position = $(xml).attr('position');
187 if (xml.tagName.toLowerCase() == "xpath") {
188 var part_expr = _.without($(xml).attr('expr').split("/"),"");;
189 xpath_object = self.parse_xml(xml,result.id);
190 if (part_expr[part_expr.length-1].search("@")!=-1) {
191 check_list = $.trim(part_expr[part_expr.length-1].replace(/[^a-zA-Z 0-9 _]+/g,' ')).split(" ");
194 //search full path...
197 var xml_child = $(xml).children();
198 check_list.push(xml.tagName.toLowerCase());
199 if ($(root[0]).attr('name')){
200 check_list.push($(root[0]).attr('name'));
201 check_list.push("name");
203 xpath_object = self.parse_xml(xml,result.id);
207 self.search_object(val,check_list,[],position,xpath_object['main_object'],[]);
210 search_object: function(val ,list ,p_list ,position, xpath_object){
212 var main_list = $.trim(val.name.replace(/[^a-zA-Z 0-9 _]+/g,' ')).split(" ");
213 var insert = _.intersection(main_list,list);
214 var check = _.indexOf(p_list.child_id,xpath_object[0]);
216 if(insert.length == list.length){
217 var level = val.level;
218 $.each(xpath_object, function(key, val) {
219 self.increase_level(val, level)
221 var index = _.indexOf(p_list.child_id,val);
225 if (index != 0) { index--; }
226 p_list.child_id.splice(index,0,xpath_object[0]);
230 p_list.child_id.splice(index,0,xpath_object[0]);
233 val.child_id.push(xpath_object[0]);
240 if ( val.child_id.length != 0) { p_list = val; }
241 $.each(val.child_id, function(key, val) {
242 self.search_object(val, list, p_list, position, xpath_object);
249 increase_level :function(val, level){
252 $.each(val.child_id, function(key, val) {
253 self.increase_level(val,level+1);
257 edit_view : function(one_object){
259 console.log("++++++++++++++++++++++",one_object);
260 this.dialog = new openerp.web.Dialog(this,{
266 "Inherited View": function(){
269 "Preview": function(){
273 $(this).dialog('destroy');
277 this.dialog.start().open();
278 this.dialog.$element.html(QWeb.render('view_editor', {
279 'data': one_object['main_object'],
282 $("tr[id^='viewedit-']").click(function() {
283 $("tr[id^='viewedit-']").removeClass('ui-selected');
284 $(this).addClass('ui-selected');
287 $("img[id^='parentimg-']").click(function() {
288 if ($(this).attr('src') == '/web/static/src/img/collapse.gif'){
289 $(this).attr('src', '/web/static/src/img/expand.gif');
290 self.on_expand(this);
292 $(this).attr('src', '/web/static/src/img/collapse.gif');
293 var id = this.id.split('-')[1];
294 self.on_collapse(this,one_object['parent_child_id'],one_object['main_object']);
297 $("img[id^='side-']").click(function() {
298 var side = $(this).closest("tr[id^='viewedit-']")
299 var id_tr = (side.attr('id')).split('-')[1];
300 var img = side.find("img[id='parentimg-"+id_tr+"']").attr('src'); ;
301 var level = side.attr('level');
305 list_shift.push(side);
317 var prev_tr = cur_tr.prev();
318 if(level >= prev_tr.attr('level') || prev_tr.length==0){
326 next_tr = side.next();
327 if(next_tr.attr('level') <= level || next_tr.length==0){
330 list_shift.push(next_tr);
335 if(last_tr.length!=0 && last_tr.attr('level') == level){
336 _.each(list_shift,function(rec){
337 $(last_tr).before(rec);
344 next_tr = cur_tr.next();
345 if(next_tr.attr('level') <= level || next_tr.length==0){
349 list_shift.push(next_tr);
355 last_tr = cur_tr.next();
357 if(last_tr.length != 0 && last_tr.attr('level')==level){
358 var last_tr_id = (last_tr.attr('id')).split('-')[1];
359 img = last_tr.find("img[id='parentimg-"+last_tr_id+"']").attr('src');
361 $("img[id='parentimg-"+last_tr_id+"']").attr('src', '/web/static/src/img/expand.gif');
363 var next_tr = last_tr.next();
364 if (next_tr.attr('level') <= level || next_tr.length==0){break;}
369 list_shift.reverse();
370 _.each(list_shift,function(rec){
371 $(last_tr).after(rec);
378 on_expand: function(self){
379 var level = $(self).closest("tr[id^='viewedit-']").attr('level');
380 var cur_tr = $(self).closest("tr[id^='viewedit-']");
382 var nxt_tr = cur_tr.next();
383 if (nxt_tr.attr('level') > level){
389 on_collapse: function(self,parent_child_id,id,main_object){
390 var id = self.id.split('-')[1];
391 var datas = _.detect(parent_child_id,function(res){
392 return res.key == id;
394 _.each(datas.value,function(rec){
395 var tr = $("tr[id='viewedit-"+rec+"']");
396 tr.find("img[id='parentimg-"+rec+"']").attr('src','/web/static/src/img/expand.gif');