2011-04-07 11:18:18 +00:00
|
|
|
/*---------------------------------------------------------
|
2011-09-06 08:22:22 +00:00
|
|
|
* OpenERP diagram library
|
2011-04-07 11:18:18 +00:00
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2011-09-06 08:22:22 +00:00
|
|
|
openerp.web_diagram = function (openerp) {
|
2011-09-07 09:37:43 +00:00
|
|
|
var QWeb = openerp.web.qweb;
|
2011-09-05 12:28:15 +00:00
|
|
|
openerp.web.views.add('diagram', 'openerp.web.DiagramView');
|
2011-09-06 08:22:22 +00:00
|
|
|
openerp.web.DiagramView = openerp.web.View.extend({
|
2011-09-06 08:09:30 +00:00
|
|
|
searchable: false,
|
2011-09-15 09:49:44 +00:00
|
|
|
init: function(parent, dataset, view_id, options) {
|
|
|
|
this._super(parent);
|
2011-09-01 08:23:21 +00:00
|
|
|
this.set_default_options(options);
|
2011-08-19 05:51:54 +00:00
|
|
|
this.view_manager = parent;
|
2011-04-07 11:18:18 +00:00
|
|
|
this.dataset = dataset;
|
2011-08-19 05:51:54 +00:00
|
|
|
this.model = this.dataset.model;
|
2011-04-07 11:18:18 +00:00
|
|
|
this.view_id = view_id;
|
2011-09-01 08:23:21 +00:00
|
|
|
this.domain = this.dataset._domain || [];
|
|
|
|
this.context = {};
|
|
|
|
this.ids = this.dataset.ids;
|
|
|
|
},
|
|
|
|
start: function() {
|
2011-09-15 09:42:46 +00:00
|
|
|
this._super();
|
2011-09-06 08:22:22 +00:00
|
|
|
return this.rpc("/web_diagram/diagram/load", {"model": this.model, "view_id": this.view_id}, this.on_loaded);
|
2011-09-01 08:23:21 +00:00
|
|
|
},
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-01 08:23:21 +00:00
|
|
|
toTitleCase: function(str) {
|
|
|
|
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
|
|
|
|
},
|
|
|
|
|
|
|
|
on_loaded: function(result) {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
if(this.ids && this.ids.length) {
|
|
|
|
this.id = this.ids[self.dataset.index || 0];
|
|
|
|
}
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
this.fields_view = result.fields_view,
|
|
|
|
this.view_id = this.fields_view.view_id,
|
|
|
|
this.fields = this.fields_view.fields,
|
|
|
|
this.nodes = this.fields_view.arch.children[0],
|
2011-09-06 08:09:30 +00:00
|
|
|
this.connectors = this.fields_view.arch.children[1],
|
2011-09-02 09:36:11 +00:00
|
|
|
this.node = this.nodes.attrs.object,
|
|
|
|
this.connector = this.connectors.attrs.object;
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-01 08:23:21 +00:00
|
|
|
this.$element.html(QWeb.render("DiagramView", this));
|
|
|
|
|
|
|
|
this.$element.find('div.oe_diagram_pager button[data-pager-action]').click(function() {
|
2011-08-19 05:51:54 +00:00
|
|
|
var action = $(this).data('pager-action');
|
|
|
|
self.on_pager_action(action);
|
|
|
|
});
|
2011-09-01 08:23:21 +00:00
|
|
|
|
|
|
|
this.do_update_pager();
|
|
|
|
|
|
|
|
// New Node,Edge
|
2011-09-02 09:36:11 +00:00
|
|
|
this.$element.find('#new_node.oe_diagram_button_new').click(function(){self.add_edit_node(null, self.node);});
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-08-19 05:51:54 +00:00
|
|
|
if(this.id) {
|
2011-09-01 08:23:21 +00:00
|
|
|
self.get_diagram_info();
|
2011-08-19 05:51:54 +00:00
|
|
|
}
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-01 08:23:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
get_diagram_info: function() {
|
|
|
|
var self = this;
|
2011-09-02 09:36:11 +00:00
|
|
|
var params = {
|
2011-09-06 08:08:47 +00:00
|
|
|
'id': this.id,
|
|
|
|
'model': this.model,
|
|
|
|
'node': this.node,
|
|
|
|
'connector': this.connector,
|
|
|
|
'bgcolor': this.nodes.attrs.bgcolor,
|
|
|
|
'shape': this.nodes.attrs.shape,
|
|
|
|
'src_node': this.connectors.attrs.source,
|
|
|
|
'des_node': this.connectors.attrs.destination,
|
|
|
|
'visible_nodes': [],
|
|
|
|
'invisible_nodes': [],
|
|
|
|
'node_fields': [],
|
|
|
|
'connectors': [],
|
|
|
|
'connectors_fields': []
|
|
|
|
};
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
_.each(this.nodes.children, function(child) {
|
2011-09-06 08:09:30 +00:00
|
|
|
if(child.attrs.invisible == '1')
|
|
|
|
params['invisible_nodes'].push(child.attrs.name);
|
|
|
|
else {
|
|
|
|
params['visible_nodes'].push(child.attrs.name);
|
|
|
|
params['node_fields'].push(self.fields[child.attrs.name]['string']|| this.toTitleCase(child.attrs.name));
|
|
|
|
}
|
2011-09-02 09:36:11 +00:00
|
|
|
});
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
_.each(this.connectors.children, function(conn) {
|
2011-09-06 08:09:30 +00:00
|
|
|
params['connectors_fields'].push(self.fields[conn.attrs.name]['string']|| this.toTitleCase(conn.attrs.name));
|
|
|
|
params['connectors'].push(conn.attrs.name);
|
2011-09-02 09:36:11 +00:00
|
|
|
});
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-08-19 05:51:54 +00:00
|
|
|
this.rpc(
|
2011-09-06 08:22:22 +00:00
|
|
|
'/web_diagram/diagram/get_diagram_info',params,
|
2011-08-19 05:51:54 +00:00
|
|
|
function(result) {
|
2011-09-01 08:23:21 +00:00
|
|
|
self.draw_diagram(result);
|
2011-08-19 05:51:54 +00:00
|
|
|
}
|
|
|
|
);
|
2011-09-01 08:23:21 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
on_diagram_loaded: function(record) {
|
|
|
|
var id_record = record['id'];
|
|
|
|
if(id_record) {
|
|
|
|
this.id = id_record;
|
|
|
|
this.get_diagram_info();
|
2011-08-19 05:51:54 +00:00
|
|
|
}
|
|
|
|
},
|
2011-10-26 13:49:29 +00:00
|
|
|
select_node: function (node, element) {
|
|
|
|
if (!this.selected_node) {
|
|
|
|
this.selected_node = node;
|
|
|
|
element.attr('stroke', 'red');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Re-click selected node, deselect it
|
|
|
|
if (node.id === this.selected_node.id) {
|
|
|
|
this.selected_node = null;
|
|
|
|
element.attr('stroke', 'black');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.add_edit_node(null, this.connector, {
|
|
|
|
act_from: this.selected_node.id,
|
|
|
|
act_to: node.id
|
|
|
|
});
|
|
|
|
},
|
2011-08-19 05:51:54 +00:00
|
|
|
draw_diagram: function(result) {
|
2011-10-26 13:52:20 +00:00
|
|
|
this.selected_node = null;
|
2011-09-02 09:36:11 +00:00
|
|
|
var diagram = new Graph();
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-08-19 05:51:54 +00:00
|
|
|
this.active_model = result['id_model'];
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-08-19 05:51:54 +00:00
|
|
|
var res_nodes = result['nodes'];
|
|
|
|
var res_connectors = result['conn'];
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-08-19 05:51:54 +00:00
|
|
|
//Custom logic
|
|
|
|
var self = this;
|
2011-09-01 08:22:42 +00:00
|
|
|
var renderer = function(r, n) {
|
2011-10-26 12:57:42 +00:00
|
|
|
var shape = (n.node.shape === 'rectangle') ? 'rect' : 'ellipse';
|
|
|
|
|
2011-10-26 12:49:28 +00:00
|
|
|
var node = r[shape](n.node.x, n.node.y).attr({
|
2011-09-06 08:08:47 +00:00
|
|
|
"fill": n.node.color
|
|
|
|
});
|
2011-10-26 12:49:28 +00:00
|
|
|
|
2011-10-26 13:15:56 +00:00
|
|
|
var nodes = r.set(node, r.text(n.node.x, n.node.y, (n.label || n.id)))
|
|
|
|
.attr("cursor", "pointer")
|
|
|
|
.dblclick(function() {
|
|
|
|
self.add_edit_node(n.node.id, self.node);
|
2011-10-26 13:49:29 +00:00
|
|
|
})
|
|
|
|
.mousedown(function () { node.moved = false; })
|
|
|
|
.mousemove(function () { node.moved = true; })
|
|
|
|
.click(function () {
|
|
|
|
// Ignore click from move event
|
|
|
|
if (node.moved) { return; }
|
|
|
|
self.select_node(n.node, node);
|
2011-10-26 13:15:56 +00:00
|
|
|
});
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-10-26 12:57:42 +00:00
|
|
|
if (shape === 'rect') {
|
2011-09-06 08:09:30 +00:00
|
|
|
node.attr({width: "60", height: "44"});
|
|
|
|
node.next.attr({"text-anchor": "middle", x: n.node.x + 20, y: n.node.y + 20});
|
2011-10-26 12:57:42 +00:00
|
|
|
} else {
|
|
|
|
node.attr({rx: "40", ry: "20"});
|
2011-09-06 08:09:30 +00:00
|
|
|
}
|
2011-10-26 12:49:28 +00:00
|
|
|
|
|
|
|
return nodes;
|
2011-04-07 13:06:23 +00:00
|
|
|
};
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
_.each(res_nodes, function(res_node) {
|
2011-09-06 08:09:30 +00:00
|
|
|
diagram.addNode(res_node['name'],{node: res_node,render: renderer});
|
2011-09-02 09:36:11 +00:00
|
|
|
});
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
// Id for Path(Edges)
|
2011-08-26 06:32:38 +00:00
|
|
|
var edge_ids = [];
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
_.each(res_connectors, function(connector, index) {
|
2011-09-01 08:23:21 +00:00
|
|
|
edge_ids.push(index);
|
2011-09-02 09:36:11 +00:00
|
|
|
diagram.addEdge(connector['source'], connector['destination'], {directed : true, label: connector['signal']});
|
2011-08-26 06:32:38 +00:00
|
|
|
});
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-10-26 13:00:03 +00:00
|
|
|
self.$element.find('.diagram').empty();
|
2011-09-06 08:08:47 +00:00
|
|
|
|
2011-09-05 07:19:16 +00:00
|
|
|
var layouter = new Graph.Layout.Ordered(diagram);
|
2011-09-02 09:36:11 +00:00
|
|
|
var render_diagram = new Graph.Renderer.Raphael('dia-canvas', diagram, $('div#dia-canvas').width(), $('div#dia-canvas').height());
|
2011-09-06 08:09:30 +00:00
|
|
|
|
2011-09-02 09:36:11 +00:00
|
|
|
_.each(diagram.edges, function(edge, index) {
|
2011-09-06 08:09:30 +00:00
|
|
|
if(edge.connection) {
|
|
|
|
edge.connection.fg.attr({cursor: "pointer"}).dblclick(function() {
|
|
|
|
self.add_edit_node(edge_ids[index], self.connector);
|
|
|
|
});
|
|
|
|
}
|
2011-08-25 05:32:02 +00:00
|
|
|
});
|
2011-08-19 05:51:54 +00:00
|
|
|
},
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-10-26 13:49:29 +00:00
|
|
|
add_edit_node: function(id, model, defaults) {
|
|
|
|
defaults = defaults || {};
|
2011-09-01 08:23:21 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if(!model)
|
|
|
|
model = self.node;
|
|
|
|
if(id)
|
|
|
|
id = parseInt(id, 10);
|
2011-09-06 08:22:22 +00:00
|
|
|
var action_manager = new openerp.web.ActionManager(this);
|
|
|
|
var dialog = new openerp.web.Dialog(this, {
|
2011-08-23 09:44:09 +00:00
|
|
|
width: 800,
|
|
|
|
height: 600,
|
|
|
|
buttons : {
|
|
|
|
Cancel : function() {
|
|
|
|
$(this).dialog('destroy');
|
|
|
|
},
|
|
|
|
Save : function() {
|
2011-09-01 08:23:21 +00:00
|
|
|
var form_view = action_manager.inner_viewmanager.views.form.controller;
|
|
|
|
|
|
|
|
form_view.do_save(function() {
|
2011-09-02 09:36:11 +00:00
|
|
|
self.dataset.read_index(_.keys(self.fields_view.fields), self.on_diagram_loaded);
|
2011-09-01 08:23:21 +00:00
|
|
|
});
|
2011-08-23 09:44:09 +00:00
|
|
|
$(this).dialog('destroy');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).start().open();
|
2011-09-01 08:23:21 +00:00
|
|
|
action_manager.appendTo(dialog.$element);
|
|
|
|
action_manager.do_action({
|
2011-08-25 06:08:30 +00:00
|
|
|
res_model : model,
|
2011-08-24 11:12:26 +00:00
|
|
|
res_id: id,
|
2011-08-23 09:44:09 +00:00
|
|
|
views : [[false, 'form']],
|
|
|
|
type : 'ir.actions.act_window',
|
|
|
|
auto_search : false,
|
|
|
|
flags : {
|
2011-09-01 08:23:21 +00:00
|
|
|
search_view: false,
|
2011-08-23 09:44:09 +00:00
|
|
|
sidebar : false,
|
|
|
|
views_switcher : false,
|
2011-08-30 08:36:22 +00:00
|
|
|
action_buttons : false,
|
|
|
|
pager: false
|
2011-08-23 09:44:09 +00:00
|
|
|
}
|
|
|
|
});
|
2011-09-01 08:23:21 +00:00
|
|
|
|
|
|
|
var form_controller = action_manager.inner_viewmanager.views.form.controller;
|
|
|
|
|
|
|
|
var form_fields;
|
|
|
|
|
|
|
|
if(model == self.node) {
|
|
|
|
form_fields = ['wkf_id'];
|
|
|
|
} else {
|
|
|
|
form_fields = ['act_from', 'act_to'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if(model == self.node || id) {
|
2011-09-06 08:09:30 +00:00
|
|
|
$.each(form_fields, function(index, fld) {
|
2011-09-01 08:23:21 +00:00
|
|
|
form_controller.on_record_loaded.add_first(function() {
|
|
|
|
form_controller.fields[fld].modifiers.readonly = true;
|
|
|
|
form_controller.fields[fld].$input.attr('disabled', true);
|
2011-09-06 08:09:30 +00:00
|
|
|
form_controller.fields[fld].$drop_down.unbind();
|
|
|
|
form_controller.fields[fld].$menu_btn.unbind();
|
2011-09-01 08:23:21 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if(!id && (model == self.node)) {
|
|
|
|
$.each(form_fields, function(index, fld) {
|
|
|
|
form_controller.on_record_loaded.add_last(function() {
|
|
|
|
form_controller.fields[fld].set_value([self.id,self.active_model]);
|
|
|
|
form_controller.fields[fld].dirty = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2011-10-26 13:49:29 +00:00
|
|
|
if (!_.isEmpty(defaults)) {
|
|
|
|
form_controller.on_record_loaded.add_last(function () {
|
|
|
|
_(form_fields).each(function (field) {
|
|
|
|
if (!defaults[field]) { return; }
|
|
|
|
form_controller.fields[field].set_value(defaults[field]);
|
|
|
|
form_controller.fields[field].dirty = true;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2011-08-19 05:51:54 +00:00
|
|
|
},
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-08-19 12:16:51 +00:00
|
|
|
on_pager_action: function(action) {
|
2011-09-01 08:23:21 +00:00
|
|
|
switch (action) {
|
|
|
|
case 'first':
|
|
|
|
this.dataset.index = 0;
|
|
|
|
break;
|
|
|
|
case 'previous':
|
|
|
|
this.dataset.previous();
|
|
|
|
break;
|
|
|
|
case 'next':
|
|
|
|
this.dataset.next();
|
|
|
|
break;
|
|
|
|
case 'last':
|
|
|
|
this.dataset.index = this.dataset.ids.length - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
this.dataset.read_index(_.keys(this.fields_view.fields), this.on_diagram_loaded);
|
|
|
|
this.do_update_pager();
|
2011-08-19 12:16:51 +00:00
|
|
|
},
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-08-19 12:16:51 +00:00
|
|
|
do_update_pager: function(hide_index) {
|
|
|
|
var $pager = this.$element.find('div.oe_diagram_pager');
|
|
|
|
var index = hide_index ? '-' : this.dataset.index + 1;
|
2011-09-01 08:22:42 +00:00
|
|
|
if(!this.dataset.count) {
|
2011-09-01 08:23:21 +00:00
|
|
|
this.dataset.count = this.dataset.ids.length;
|
2011-09-01 08:22:42 +00:00
|
|
|
}
|
2011-08-19 12:16:51 +00:00
|
|
|
$pager.find('span.oe_pager_index').html(index);
|
|
|
|
$pager.find('span.oe_pager_count').html(this.dataset.count);
|
|
|
|
},
|
2011-09-01 08:23:21 +00:00
|
|
|
|
|
|
|
do_show: function () {
|
2011-04-07 11:18:18 +00:00
|
|
|
this.$element.show();
|
|
|
|
},
|
2011-09-01 08:23:21 +00:00
|
|
|
|
2011-04-07 11:18:18 +00:00
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:et fdc=0 fdl=0 foldnestmax=3 fdm=syntax:
|