[FIX] gantt addon so it loads correctly
[odoo/odoo.git] / addons / base_gantt / static / src / js / gantt.js
1 /*---------------------------------------------------------
2  * OpenERP base_gantt
3  *---------------------------------------------------------*/
4
5 openerp.base_gantt = function (openerp) {
6 QWeb.add_template('/base_gantt/static/src/xml/base_gantt.xml');
7 openerp.base.views.add('gantt', 'openerp.base_gantt.GanttView');
8 openerp.base_gantt.GanttView = openerp.base.Controller.extend({
9
10     init: function(view_manager, session, element_id, dataset, view_id) {
11         this._super(session, element_id);
12         this.view_manager = view_manager;
13         this.dataset = dataset;
14         this.model = dataset.model;
15         this.view_id = view_id;
16         this.fields_views = {};
17         this.widgets = {};
18         this.widgets_counter = 0;
19         this.fields = {};
20         this.datarecord = {};
21         this.calendar_fields = {};
22     },
23     do_show: function () {
24         // TODO: re-trigger search
25         this.$element.show();
26     },
27     do_hide: function () {
28         this.$element.hide();
29     },
30     start: function() {
31         this.rpc("/base_gantt/ganttview/load", {"model": this.model, "view_id": this.view_id}, this.on_loaded);
32     },
33     on_loaded: function(data) {
34         this.fields_view = data.fields_view;
35         var self = this;
36         this.name = this.fields_view.name || this.fields_view.arch.attrs.string;
37         this.view_id = this.fields_view.view_id;
38
39         this.date_start = this.fields_view.arch.attrs.date_start;
40         this.date_delay = this.fields_view.arch.attrs.date_delay;
41         this.date_stop = this.fields_view.arch.attrs.date_stop;
42         this.color_field = this.fields_view.arch.attrs.color;
43
44         this.day_length = this.fields_view.arch.attrs.day_length || 8;
45         this.colors = this.fields_view.arch.attrs.colors;
46         this.fields = this.fields_view.fields;
47
48         this.text = this.fields_view.arch.children[0].children[0].attrs.name;
49         this.parent = this.fields_view.arch.children[0].attrs.link;
50
51         this.calendar_fields['parent'] = {'name': this.parent};
52         this.calendar_fields['date_start'] = {'name': this.date_start};
53         this.calendar_fields['text'] = {'name': this.text};
54         if(this.date_delay)
55             this.calendar_fields['date_delay'] = {'name': this.date_delay};
56         if(this.date_stop)
57             this.calendar_fields['date_stop'] = {'name': this.date_stop};
58
59         this.calendar_fields['day_length'] = this.day_length;
60         this.rpc('/base_gantt/ganttview/get_events',
61                 {'model': this.model,
62                 'fields': this.fields,
63                 'color_field': this.color_field,
64                 'day_length': this.day_length,
65                 'calendar_fields': this.calendar_fields,
66                 'colors': this.colors,
67                 'info_fields': this.info_fields
68                 },
69                 function(res) {
70                     self.create_gantt();
71                     self.load_event(res);
72                 })
73         this.$element.html(QWeb.render("GanttView", {"view": this, "fields_view": this.fields_view}));
74
75     },
76     convert_date_format: function(date) {
77         date=date+"";
78         if(typeof (date)!="string"||date.length===0){
79             return null;
80         }
81         var iso=date.split("-");
82         if(iso.length===0){
83             return null;
84         }
85         var day = iso[2];
86         var iso_hours = day.split(' ');
87
88         if (iso_hours.length > 1) {
89             day = iso_hours[0];
90             var iso_date_hours = iso_hours[1].split(':')
91             var new_date = new Date(iso[0], iso[1] - 1, day);
92             new_date.setHours(iso_date_hours[0]);
93             new_date.setMinutes(iso_date_hours[1]);
94             new_date.setSeconds(iso_date_hours[2]);
95         }
96         else {
97             var new_date = new Date(iso[0], iso[1] - 1, day);
98         }
99         new_date.setFullYear(iso[0]);
100         new_date.setMonth(iso[1]-1);
101         new_date.setDate(day);
102         return new_date;
103     },
104     
105     create_gantt: function() {
106         ganttChartControl = new GanttChart();
107         ganttChartControl.setImagePath("/base_gantt/static/lib/dhtmlxGantt/codebase/imgs/");
108         ganttChartControl.setEditable(true);
109         ganttChartControl.showTreePanel(true);
110         ganttChartControl.showContextMenu(true);
111         ganttChartControl.showDescTask(true,'d,s-f');
112         ganttChartControl.showDescProject(true,'n,d');
113     },   
114     load_event: function(res) {
115         var self = this   
116         var result = res.result;
117         var sidebar = res.sidebar;
118         var project_id = new Array();
119         var project = new Array();
120         var j = -1;
121         var self = this;
122         for (i in result) {
123         
124             var parent_id =  result[i]['parent'][0];
125             var parent_name = result[i]['parent'][1];
126             
127             if (jQuery.inArray(parent_id, project_id) == -1){
128                 if (parent_id == undefined){
129                     parent_name = "";
130                 }
131                 j = j + 1;
132                 project[j] = new GanttProjectInfo(parent_id, parent_name, new Date(2011, 1, 1));
133                 project_id[j] = parent_id;
134             }
135             
136             var id = result[i]['id'];
137             var text = result[i]['text'];
138             var start_date = this.convert_date_format(result[i]['start_date']);
139             var duration = result[i]['duration'];
140             
141             var task = new GanttTaskInfo(id, text, start_date, duration, 100, "");
142             
143             k = project_id.indexOf(parent_id);
144             project[k].addTask(task);
145
146         }
147         for (i in project_id){
148             ganttChartControl.addProject(project[i]);
149         }   
150         ganttChartControl.create("GanttDiv");
151         ganttChartControl.attachEvent("onTaskEndResize", function(task) {self.on_task_end_resize(task);})
152         ganttChartControl.attachEvent("onTaskEndDrag", function(task) {self.on_task_end_drag(task);})
153         
154         //Create Sidebar
155         if (jQuery('#cal-sidebar-option').length == 0){
156             jQuery('#gantt-sidebar').append(
157                 jQuery('<table>',{'width':'100%','cellspacing': 0, 'cellpadding': 0, 'id':'cal-sidebar-option'})
158             )
159             for(s in sidebar) {
160                 jQuery('#cal-sidebar-option').append(
161                     jQuery('<tr>').append(
162                         jQuery('<td>').append(
163                             jQuery('<div>')
164                             .append(
165                                 jQuery('<input>',
166                                 {
167                                     'type': 'checkbox', 
168                                     'id':sidebar[s][0],
169                                     'value':sidebar[s][0]
170                                 }).bind('click',function(){
171                                     self.reload_gantt(self.color_field,self.model)
172                                 }),
173                                 sidebar[s][1]
174                             )
175                             .css('background-color',sidebar[s][sidebar[s].length-1])
176                         )
177                     )
178                 )
179             }
180         }
181     },
182     reload_gantt: function(color_field, model) {
183         var domain = [];
184         var self = this;
185         jQuery('input[type=checkbox]:checked','#cal-sidebar-option').each(function() {
186             domain.push(parseInt(jQuery(this).attr('id')))
187         });
188         this.rpc('/base_gantt/ganttview/reload_gantt',{
189             'domain':domain,
190             'color_field':color_field,
191             'model': model
192         },function(res) {
193             ganttChartControl.clearAll();
194             jQuery("#GanttDiv").children().remove();
195             self.load_event(res);
196         });
197     },
198     reverse_convert_date_format: function(date) {
199         return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
200     },
201     
202     on_task_end_resize : function(task) {
203         this.rpc('/base_gantt/ganttview/on_event_resize',
204                 {'id' : task.getId(),
205                 'end_date' : this.reverse_convert_date_format(task.getFinishDate()),
206                 'duration' : task.getDuration()
207                 },
208                 function(result) {
209                 })
210     },
211     on_task_end_drag : function(task) {
212         this.rpc('/base_gantt/ganttview/on_event_drag',
213                 {'id' : task.getId(),
214                 'start_date' : this.reverse_convert_date_format(task.getEST()),
215                 'end_date' : this.reverse_convert_date_format(task.getFinishDate()),
216                 'duration' : task.getDuration()
217                 },
218                 function(result) {
219                 })
220     }
221
222 });
223
224 // here you may tweak globals object, if any, and play with on_* or do_* callbacks on them
225
226 };
227
228 // vim:et fdc=0 fdl=0: