2011-04-07 12:13:31 +00:00
|
|
|
/*---------------------------------------------------------
|
|
|
|
* OpenERP base_gantt
|
|
|
|
*---------------------------------------------------------*/
|
|
|
|
|
2011-04-13 14:20:01 +00:00
|
|
|
openerp.base_gantt = function (openerp) {
|
|
|
|
QWeb.add_template('/base_gantt/static/src/xml/base_gantt.xml');
|
|
|
|
openerp.base.views.add('gantt', 'openerp.base_gantt.GanttView');
|
|
|
|
openerp.base_gantt.GanttView = openerp.base.Controller.extend({
|
2011-04-07 12:13:31 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
init: function(view_manager, session, element_id, dataset, view_id) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
this._super(session, element_id);
|
|
|
|
this.view_manager = view_manager;
|
|
|
|
this.dataset = dataset;
|
|
|
|
this.model = dataset.model;
|
|
|
|
this.view_id = view_id;
|
|
|
|
this.fields_views = {};
|
|
|
|
this.widgets = {};
|
|
|
|
this.widgets_counter = 0;
|
2011-04-15 09:32:53 +00:00
|
|
|
this.fields = this.dataset.fields ? this.dataset.fields: {};
|
|
|
|
this.ids = this.dataset.ids;
|
2011-04-28 11:57:03 +00:00
|
|
|
this.name = "";
|
|
|
|
this.date_start = "";
|
|
|
|
this.date_delay = "";
|
|
|
|
this.date_stop = "";
|
|
|
|
this.color_field = "";
|
|
|
|
this.day_lenth = 8;
|
|
|
|
this.colors = [];
|
|
|
|
this.color_values = [];
|
|
|
|
this.calendar_fields = {};
|
|
|
|
this.info_fields = [];
|
|
|
|
this.domain = this.dataset._domain ? this.dataset._domain: [];
|
|
|
|
this.context = {};
|
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
start: function() {
|
2011-04-28 11:57:03 +00:00
|
|
|
this.rpc("/base_gantt/ganttview/load",
|
|
|
|
{"model": this.model, "view_id": this.view_id}, this.on_loaded);
|
2011-04-07 12:13:31 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
on_loaded: function(data) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
var self = this;
|
2011-04-07 12:13:31 +00:00
|
|
|
this.fields_view = data.fields_view;
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
this.name = this.fields_view.name || this.fields_view.arch.attrs.string;
|
|
|
|
this.view_id = this.fields_view.view_id;
|
|
|
|
|
|
|
|
this.date_start = this.fields_view.arch.attrs.date_start;
|
|
|
|
this.date_delay = this.fields_view.arch.attrs.date_delay;
|
|
|
|
this.date_stop = this.fields_view.arch.attrs.date_stop;
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
this.color_field = this.fields_view.arch.attrs.color;
|
2011-04-07 12:13:31 +00:00
|
|
|
this.day_length = this.fields_view.arch.attrs.day_length || 8;
|
|
|
|
this.colors = this.fields_view.arch.attrs.colors;
|
|
|
|
|
|
|
|
this.text = this.fields_view.arch.children[0].children[0].attrs.name;
|
|
|
|
this.parent = this.fields_view.arch.children[0].attrs.link;
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
this.format = "%Y-%m-%d";
|
|
|
|
this.time = "00:00:00";
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
self.create_gantt();
|
2011-04-28 11:57:03 +00:00
|
|
|
self.get_events(self.ids);
|
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
this.$element.html(QWeb.render("GanttView", {"view": this, "fields_view": this.fields_view}));
|
|
|
|
|
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
create_gantt: function() {
|
|
|
|
|
2011-04-28 11:57:03 +00:00
|
|
|
ganttChartControl = new GanttChart(this.day_length);
|
|
|
|
ganttChartControl.setImagePath("/base_gantt/static/lib/dhtmlxGantt/codebase/imgs/");
|
|
|
|
ganttChartControl.setEditable(true);
|
|
|
|
ganttChartControl.showTreePanel(true);
|
|
|
|
ganttChartControl.showContextMenu(true);
|
|
|
|
ganttChartControl.showDescTask(true,'d,s-f');
|
|
|
|
ganttChartControl.showDescProject(true,'n,d');
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
get_events: function(event_ids) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
this.dataset.read_ids(event_ids, {}, function(result) {
|
|
|
|
self.load_event(result);
|
|
|
|
});
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
load_event: function(events) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
var self = this;
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
var result = events;
|
2011-04-28 11:57:03 +00:00
|
|
|
var project = {};
|
|
|
|
var project_smalldate = {};
|
|
|
|
var proj_id = [];
|
|
|
|
var proj_id_text = [];
|
|
|
|
|
|
|
|
//Smallest date of child is parent start date
|
|
|
|
for (i in result){
|
|
|
|
var res = result[i];
|
|
|
|
|
|
|
|
if (res[this.date_start] != false){
|
2011-04-15 09:32:53 +00:00
|
|
|
|
2011-04-28 11:57:03 +00:00
|
|
|
var parent_id = res[this.parent][0] || res[this.parent];
|
|
|
|
var parent_name = res[this.parent][1];
|
|
|
|
var start_date = this.convert_date_format(res[this.date_start]);
|
|
|
|
|
|
|
|
if (project_smalldate[parent_id] == undefined){
|
|
|
|
project_smalldate[parent_id] = start_date;
|
|
|
|
proj_id.push(parent_id);
|
|
|
|
|
|
|
|
if (parent_name != undefined){
|
|
|
|
proj_id_text.push(res[this.parent]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
if (start_date < project_smalldate[parent_id]){
|
|
|
|
project_smalldate[parent_id] = start_date;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//get parent text
|
|
|
|
if (parent_name == undefined){
|
|
|
|
|
|
|
|
var ajax = {
|
|
|
|
url: '/base/dataset/call',
|
|
|
|
async: false
|
|
|
|
};
|
|
|
|
|
|
|
|
this.rpc(ajax, {
|
|
|
|
model: this.dataset.model,
|
|
|
|
method: "name_get",
|
|
|
|
ids: proj_id,
|
|
|
|
args: ""
|
|
|
|
}, function(response) {
|
|
|
|
proj_id_text = response.result;
|
|
|
|
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//create parents
|
|
|
|
for (i in proj_id){
|
|
|
|
|
|
|
|
var id = proj_id_text[i][0];
|
|
|
|
var text = proj_id_text[i][1];
|
|
|
|
|
|
|
|
project[id] = new GanttProjectInfo(id, text, project_smalldate[id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
//create childs
|
2011-04-15 09:32:53 +00:00
|
|
|
for (i in result) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
var res = result[i];
|
|
|
|
if (res[this.date_start] != false){
|
|
|
|
|
|
|
|
var parent_id = res[this.parent][0] || res[this.parent];
|
|
|
|
var id = res['id'];
|
|
|
|
var text = res[this.text];
|
|
|
|
var start_date = this.convert_date_format(res[this.date_start]);
|
|
|
|
|
|
|
|
if (this.date_stop != undefined){
|
|
|
|
if (res[this.date_stop] != false){
|
|
|
|
var stop_date = this.convert_date_format(res[this.date_stop]);
|
|
|
|
var duration= self.hours_between(start_date, stop_date);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
var duration = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
var duration = res[this.date_delay];
|
|
|
|
}
|
|
|
|
if (duration == false)
|
|
|
|
duration = 0
|
|
|
|
task = new GanttTaskInfo(id, text, start_date, duration, 100, "");
|
|
|
|
project[parent_id].addTask(task);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Add parent
|
|
|
|
for (i in proj_id){
|
|
|
|
ganttChartControl.addProject(project[proj_id[i]]);
|
2011-04-15 09:32:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ganttChartControl.create("GanttDiv");
|
2011-04-28 11:57:03 +00:00
|
|
|
ganttChartControl.attachEvent("onTaskEndResize", function(task) {self.on_resize_drag(task, "resize");})
|
|
|
|
ganttChartControl.attachEvent("onTaskEndDrag", function(task) {self.on_resize_drag(task, "drag");})
|
2011-05-02 06:32:56 +00:00
|
|
|
ganttChartControl.attachEvent("onTaskDblClick", function(task){self.open_popup(task);});
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
hours_between: function(date1, date2) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-05-02 06:32:56 +00:00
|
|
|
var ONE_DAY = 1000 * 60 * 60 * 24;
|
|
|
|
var date1_ms = date1.getTime();
|
|
|
|
var date2_ms = date2.getTime();
|
|
|
|
var difference_ms = Math.abs(date1_ms - date2_ms);
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-05-02 06:32:56 +00:00
|
|
|
d = Math.round(difference_ms / ONE_DAY);
|
2011-04-28 11:57:03 +00:00
|
|
|
h = Math.round((difference_ms % ONE_DAY)/(1000 * 60 * 60));
|
|
|
|
return (d * this.day_length) + h;
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-05-02 06:32:56 +00:00
|
|
|
open_popup : function(task) {
|
|
|
|
var event_id = task.getId();
|
|
|
|
if (event_id) {
|
|
|
|
event_id = parseInt(event_id, 10);
|
|
|
|
var dataset_event_index = jQuery.inArray(event_id, this.ids);
|
|
|
|
} else {
|
|
|
|
var dataset_event_index = null;
|
|
|
|
}
|
|
|
|
this.dataset.index = dataset_event_index;
|
|
|
|
var element_id = _.uniqueId("act_window_dialog");
|
|
|
|
var dialog = jQuery('<div>',
|
|
|
|
{'id': element_id
|
|
|
|
}).dialog({
|
|
|
|
title: 'Gantt Chart',
|
|
|
|
modal: true,
|
|
|
|
minWidth: 800,
|
|
|
|
position: 'top'
|
|
|
|
});
|
|
|
|
var event_form = new openerp.base.FormView(this.view_manager, this.session, element_id, this.dataset, false);
|
|
|
|
event_form.start();
|
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
on_resize_drag : function(task, evt) {
|
|
|
|
|
|
|
|
var event_id = task.getId();
|
|
|
|
var data = {};
|
|
|
|
|
|
|
|
if (evt == "drag"){
|
|
|
|
data[this.date_start] = this.reverse_convert_date_format(task.getEST(), "09:00:00");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.date_stop != undefined){
|
|
|
|
|
|
|
|
tm = ((task.getDuration() % this.day_length) + 9) + "";
|
|
|
|
if (tm.length == 1)
|
|
|
|
tm = "0" + tm;
|
|
|
|
|
|
|
|
time = tm + ":00:00";
|
|
|
|
|
|
|
|
data[this.date_stop] = this.reverse_convert_date_format(task.getFinishDate(), time);
|
|
|
|
}else{
|
|
|
|
data[this.date_delay] = task.getDuration();
|
|
|
|
}
|
|
|
|
|
|
|
|
this.dataset.write(event_id, data, function(result) {});
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
do_show: function () {
|
2011-04-28 11:57:03 +00:00
|
|
|
this.$element.show();
|
2011-04-15 09:32:53 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
do_hide: function () {
|
|
|
|
this.$element.hide();
|
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
convert_date_format: function(date) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (date.length == 19){
|
|
|
|
self.format = "%Y-%m-%d %H:%M:%S";
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
date = date+"";
|
|
|
|
if(typeof (date) != "string" || date.length === 0){
|
2011-04-07 12:13:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var iso=date.split("-");
|
2011-04-15 09:32:53 +00:00
|
|
|
if(iso.length === 0){
|
2011-04-07 12:13:31 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var day = iso[2];
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
var iso_hours = day.split(' ');
|
|
|
|
|
|
|
|
if (iso_hours.length > 1) {
|
|
|
|
day = iso_hours[0];
|
2011-04-15 09:32:53 +00:00
|
|
|
var iso_date_hours = iso_hours[1].split(':');
|
2011-04-07 12:13:31 +00:00
|
|
|
var new_date = new Date(iso[0], iso[1] - 1, day);
|
|
|
|
new_date.setHours(iso_date_hours[0]);
|
|
|
|
new_date.setMinutes(iso_date_hours[1]);
|
|
|
|
new_date.setSeconds(iso_date_hours[2]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var new_date = new Date(iso[0], iso[1] - 1, day);
|
|
|
|
}
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
new_date.setFullYear(iso[0]);
|
|
|
|
new_date.setMonth(iso[1]-1);
|
|
|
|
new_date.setDate(day);
|
|
|
|
return new_date;
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
reverse_convert_date_format: function(date, time) {
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self.format == "%Y-%m-%d %H:%M:%S"){
|
|
|
|
return date.getFullYear()+'-'+(date.getMonth()+1)+'-'+(date.getDate())+' '+time;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
|
|
|
|
}
|
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
reload_gantt: function(domain) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
this.rpc('/base_gantt/ganttview/reload_gantt',{
|
2011-04-15 09:32:53 +00:00
|
|
|
'domain': domain,
|
|
|
|
'model': self.model
|
|
|
|
},function(event_ids) {
|
2011-04-07 12:13:31 +00:00
|
|
|
ganttChartControl.clearAll();
|
|
|
|
jQuery("#GanttDiv").children().remove();
|
2011-04-15 09:32:53 +00:00
|
|
|
self.get_events(event_ids);
|
2011-04-07 12:13:31 +00:00
|
|
|
});
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
},
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
do_search: function (domains, contexts, groupbys) {
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
var self = this;
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-15 09:32:53 +00:00
|
|
|
return this.rpc('/base/session/eval_domain_and_context', {
|
|
|
|
domains: domains,
|
|
|
|
contexts: contexts,
|
|
|
|
group_by_seq: groupbys
|
|
|
|
}, function (results) {
|
|
|
|
self.dataset.context = results.context;
|
|
|
|
self.dataset.domain = results.domain;
|
|
|
|
return self.reload_gantt(self.dataset.domain);
|
|
|
|
});
|
2011-04-13 14:20:01 +00:00
|
|
|
}
|
2011-04-28 11:57:03 +00:00
|
|
|
|
2011-04-07 12:13:31 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// here you may tweak globals object, if any, and play with on_* or do_* callbacks on them
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// vim:et fdc=0 fdl=0:
|