[MIG] web_timeline: Migration to 9.0

This commit is contained in:
Pedro M. Baeza
2016-08-02 00:59:05 +02:00
committed by CarlosRoca13
parent df6dc4abda
commit 9bdadac1ca
7 changed files with 303 additions and 355 deletions

View File

@@ -1,7 +1,7 @@
/*---------------------------------------------------------
* Odoo web_timeline
/* Odoo web_timeline
* Copyright 2015 ACSONE SA/NV
*---------------------------------------------------------*/
* Copyright 2016 Pedro M. Baeza <pedro.baeza@tecnativa.com>
* License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). */
_.str.toBoolElse = function (str, elseValues, trueValues, falseValues) {
var ret = _.str.toBool(str, trueValues, falseValues);
@@ -11,61 +11,55 @@ _.str.toBoolElse = function (str, elseValues, trueValues, falseValues) {
return ret;
};
openerp.web_timeline = function(instance) {
var _t = instance.web._t,
_lt = instance.web._lt,
QWeb = instance.web.qweb;
odoo.define('web_timeline.TimelineView', function (require) {
"use strict";
var core = require('web.core');
var form_common = require('web.form_common');
var Model = require('web.DataModel');
var time = require('web.time');
var View = require('web.View');
var widgets = require('web_calendar.widgets');
var _t = core._t;
var _lt = core._lt;
function isNullOrUndef(value) {
return _.isUndefined(value) || _.isNull(value);
}
instance.web.views.add('timeline', 'instance.web_timeline.TimelineView');
instance.web_timeline.TimelineView = instance.web.View.extend({
var TimelineView = View.extend({
template: "TimelineView",
display_name: _lt('Timeline'),
quick_create_instance: 'instance.web_timeline.QuickCreate',
icon: 'fa-clock-o',
quick_create_instance: widgets.QuickCreate,
init: function (parent, dataset, view_id, options) {
this._super(parent);
this.ready = $.Deferred();
this.permissions = {};
this.set_default_options(options);
this.dataset = dataset;
this.model = dataset.model;
this.fields_view = {};
this.view_id = view_id;
this.view_type = 'timeline';
this.color_map = {};
this.range_start = null;
this.range_stop = null;
this.selected_filters = [];
this.current_window = null;
return this._super.apply(this, arguments);
},
get_perm: function(name){
var self = this;
var promise = self.permissions[name];
if(!promise) {
var defer = $.Deferred();
new instance.web.Model(this.dataset.model)
.call("check_access_rights", [name, false])
.then(function (value) {
self.permissions[name] = value;
defer.resolve();
});
return defer;
if (self.permissions[name]) {
return $.when(self.permissions[name]);
} else {
return promise;
return new Model(this.dataset.model)
.call("check_access_rights", [name, false])
.then(function (value) {
self.permissions[name] = value;
return value;
});
}
},
set_default_options: function(options) {
this._super(options);
_.defaults(this.options, {
confirm_on_delete: true
});
},
// set_default_options: function(options) {
// this._super(options);
// _.defaults(this.options, {
// confirm_on_delete: true
// });
// },
parse_colors: function(){
if(this.fields_view.arch.attrs.colors) {
@@ -77,83 +71,72 @@ openerp.web_timeline = function(instance) {
}
},
view_loading: function (fv) {
/* xml view timeline options */
var attrs = fv.arch.attrs;
start: function () {
var self = this;
this.fields_view = fv;
var attrs = this.fields_view.arch.attrs;
var fv = this.fields_view;
this.parse_colors();
this.$timeline = this.$el.find(".oe_timeline_widget");
this.$el.find(".oe_timeline_button_today").click(self.on_today_clicked);
this.$el.find(".oe_timeline_button_scale_day").click(self.on_scale_day_clicked);
this.$el.find(".oe_timeline_button_scale_week").click(self.on_scale_week_clicked);
this.$el.find(".oe_timeline_button_scale_month").click(self.on_scale_month_clicked);
this.$el.find(".oe_timeline_button_scale_year").click(self.on_scale_year_clicked);
this.$(".oe_timeline_button_today").click(
this.proxy(this.on_today_clicked));
this.$(".oe_timeline_button_scale_day").click(
this.proxy(this.on_scale_day_clicked));
this.$(".oe_timeline_button_scale_week").click(
this.proxy(this.on_scale_week_clicked));
this.$(".oe_timeline_button_scale_month").click(
this.proxy(this.on_scale_month_clicked));
this.$(".oe_timeline_button_scale_year").click(
this.proxy(this.on_scale_year_clicked));
this.current_window = {
start: new Date(),
end : new Date().addHours(24),
}
start: new moment(),
end : new moment().add(24, 'hours')
};
this.$el.addClass(attrs['class']);
this.info_fields = [];
this.mode = attrs.mode;
if (!attrs.date_start) {
throw new Error(_t("Timeline view has not defined 'date_start' attribute."));
}
this.$el.addClass(attrs['class']);
this.name = fv.name || attrs.string;
this.view_id = fv.view_id;
this.start = py.eval(attrs.start || 'None', instance.web.pyeval.context());
this.mode = attrs.mode; // one of month, week or day
this.date_start = attrs.date_start; // Field name of starting
// date field
this.date_start = attrs.date_start;
this.date_stop = attrs.date_stop;
if (!isNullOrUndef(attrs.quick_create_instance)) {
self.quick_create_instance = 'instance.' + attrs.quick_create_instance;
}
// If this field is set ot true, we don't open the event in form
// If this field is set ot true, we don't open the event in form
// view, but in a popup with the view_id passed by this parameter
if (isNullOrUndef(attrs.event_open_popup) || !_.str.toBoolElse(attrs.event_open_popup, true)) {
this.open_popup_action = false;
} else {
this.open_popup_action = attrs.event_open_popup;
}
this.fields = fv.fields;
for (var fld = 0; fld < fv.arch.children.length; fld++) {
this.info_fields.push(fv.arch.children[fld].attrs.name);
}
var fields_get = new instance.web.Model(this.dataset.model)
.call('fields_get')
.then(function (fields) {
self.fields = fields;
});
var unlink_check = new instance.web.Model(this.dataset.model)
.call("check_access_rights", ["unlink", false])
.then(function (unlink_right) {
self.unlink_right = unlink_right;
});
var edit_check = new instance.web.Model(this.dataset.model)
.call("check_access_rights", ["write", false])
.then(function (write_right) {
self.write_right = write_right;
});
var init = function () {
self.init_timeline().then(function() {
$(window).trigger('resize');
self.trigger('timeline_view_loaded', fv);
self.ready.resolve();
var fields_get = new Model(this.dataset.model)
.call('fields_get')
.then(function (fields) {
self.fields = fields;
});
};
var test = $.when(self.fields_get, self.get_perm('unlink'), self.get_perm('write'), self.get_perm('create'));
return $.when(test).then(init);
this._super.apply(this, self);
return $.when(
self.fields_get,
self.get_perm('unlink'),
self.get_perm('write'),
self.get_perm('create')
).then(function() {
self.init_timeline();
$(window).trigger('resize');
self.trigger('timeline_view_loaded', fv);
});
},
init_timeline: function() {
@@ -161,10 +144,14 @@ openerp.web_timeline = function(instance) {
var options = {
groupOrder: self.group_order,
editable: {
add: self.permissions['create'], // add new items by double tapping
updateTime: self.permissions['write'], // drag items horizontally
updateGroup: self.permissions['write'], // drag items from one group to another
remove: self.permissions['unlink'], // delete an item by tapping the delete button top right
// add new items by double tapping
add: self.permissions['create'],
// drag items horizontally
updateTime: self.permissions['write'],
// drag items from one group to another
updateGroup: self.permissions['write'],
// delete an item by tapping the delete button top right
remove: self.permissions['unlink']
},
orientation: 'both',
selectable: true,
@@ -173,17 +160,13 @@ openerp.web_timeline = function(instance) {
onMove: self.on_move,
onUpdate: self.on_update,
onRemove: self.on_remove,
orientation: 'both',
start: self.start,
};
self.timeline = new vis.Timeline(self.$timeline.empty().get(0));
self.timeline.setOptions(options);
if(self.mode && self['on_scale_' + self.mode + '_clicked'])
{
if (self.mode && self['on_scale_' + self.mode + '_clicked']) {
self['on_scale_' + self.mode + '_clicked']();
}
self.timeline.on('click', self.on_click);
return $.when();
},
group_order: function(grp1, grp2) {
@@ -195,14 +178,14 @@ openerp.web_timeline = function(instance) {
return +1;
}
return grp1.content - grp2.content;
},
/**
* Transform OpenERP event object to timeline event object
*/
/* Transform Odoo event object to timeline event object */
event_data_transform: function(evt) {
var self = this;
var date_start = new moment();
var date_stop = new moment();
var date_delay = evt[this.date_delay] || 1.0,
all_day = this.all_day ? evt[this.all_day] : false,
@@ -211,19 +194,19 @@ openerp.web_timeline = function(instance) {
attendees = [];
if (!all_day) {
date_start = instance.web.auto_str_to_date(evt[this.date_start]);
date_stop = this.date_stop ? instance.web.auto_str_to_date(evt[this.date_stop]) : null;
date_start = time.auto_str_to_date(evt[this.date_start]);
date_stop = this.date_stop ? time.auto_str_to_date(evt[this.date_stop]) : null;
}
else {
date_start = instance.web.auto_str_to_date(evt[this.date_start].split(' ')[0],'start');
date_stop = this.date_stop ? instance.web.auto_str_to_date(evt[this.date_stop].split(' ')[0],'stop') : null;
date_start = time.auto_str_to_date(evt[this.date_start].split(' ')[0],'start');
date_stop = this.date_stop ? time.auto_str_to_date(evt[this.date_stop].split(' ')[0],'stop') : null;
}
if (!date_start){
date_start = new Date();
date_start = new moment();
}
if(!date_stop) {
date_stop = date_start.clone().addHours(date_delay);
date_stop = moment(date_start).add(date_delay, 'hours').toDate();
}
var group = evt[self.last_group_bys[0]];
if (group){
@@ -238,17 +221,15 @@ openerp.web_timeline = function(instance) {
var r = {
'start': date_start,
'end': date_stop,
'content': evt.__name,
'content': evt.__name != undefined ? evt.__name : evt.display_name,
'id': evt.id,
'group': group,
'evt': evt,
'style': 'background-color: ' + self.color + ';',
'style': 'background-color: ' + self.color + ';'
};
self.color = undefined;
return r;
},
do_search: function (domains, contexts, group_bys) {
var self = this;
@@ -279,7 +260,6 @@ openerp.web_timeline = function(instance) {
});
},
reload: function() {
var self = this;
if (this.last_domains !== undefined){
@@ -288,34 +268,34 @@ openerp.web_timeline = function(instance) {
}
},
on_data_loaded: function(tasks, group_bys) {
on_data_loaded: function(events, group_bys) {
var self = this;
var ids = _.pluck(tasks, "id");
var ids = _.pluck(events, "id");
return this.dataset.name_get(ids).then(function(names) {
var ntasks = _.map(tasks, function(task) {
return _.extend({__name: _.detect(names, function(name) { return name[0] == task.id; })[1]}, task);
var nevents = _.map(events, function(event) {
return _.extend({__name: _.detect(names, function(name) { return name[0] == event.id; })[1]}, event);
});
return self.on_data_loaded_2(ntasks, group_bys);
return self.on_data_loaded_2(nevents, group_bys);
});
},
on_data_loaded_2: function(tasks, group_bys) {
on_data_loaded_2: function(events, group_bys) {
var self = this;
var data = [];
var groups = [];
_.each(tasks, function(event) {
_.each(events, function(event) {
if (event[self.date_start]){
data.push(self.event_data_transform(event));
}
});
// get the groups
var split_groups = function(tasks, group_bys) {
// get the groups
var split_groups = function(events, group_bys) {
if (group_bys.length === 0)
return tasks;
return events;
var groups = [];
groups.push({id:-1, content: _t('-')})
_.each(tasks, function(task) {
var group_name = task[_.first(group_bys)];
_.each(events, function(event) {
var group_name = event[_.first(group_bys)];
if (group_name) {
var group = _.find(groups, function(group) { return _.isEqual(group.id, group_name[0]); });
if (group === undefined) {
@@ -326,10 +306,10 @@ openerp.web_timeline = function(instance) {
});
return groups;
}
var groups = split_groups(tasks, group_bys);
var groups = split_groups(events, group_bys);
this.timeline.setGroups(groups);
this.timeline.setItems(data);
this.timeline.setWindow(this.current_window);
this.timeline.fit();
},
do_show: function() {
@@ -343,55 +323,46 @@ openerp.web_timeline = function(instance) {
}
return this._super(action);
},
/**
* Handles a newly created record
*
* @param {id} id of the newly created record
*/
quick_created: function (id) {
/**
* Note: it's of the most utter importance NOT to use inplace
* modification on this.dataset.ids as reference to this data is
* spread out everywhere in the various widget. Some of these
* reference includes values that should trigger action upon
* modification.
*/
create_completed: function(id) {
var self = this;
this.dataset.ids = this.dataset.ids.concat([id]);
this.dataset.trigger("dataset_changed", id);
this.refresh_event(id);
this.dataset.read_ids([id], this.fields).done(function(records) {
var new_event = self.event_data_transform(records[0]);
var items = self.timeline.itemsData;
items.add(new_event);
self.timeline.setItems(items);
});
},
on_add: function(item, callback) {
var self = this,
pop = new instance.web.form.SelectCreatePopup(this),
context = this.get_popup_context(item);
pop.on("elements_selected", self, function(element_ids) {
self.reload().then(function() {
self.timeline.focus(element_ids);
});
});
pop.select_element(
self.dataset.model,
{
title: _t("Create"),
initial_view: "form",
},
null,
context
);
var self = this;
var context = this.dataset.get_context();
// Initialize default values for creation
var default_context = {};
default_context['default_'.concat(this.date_start)] = item.start;
default_context['default_'.concat(this.date_stop)] = moment(item.start).add(1, 'hours').toDate();
if (item.group > 0) {
default_context['default_'.concat(this.last_group_bys[0])] = item.group;
}
context.add(default_context);
// Show popup
var dialog = new form_common.FormViewDialog(this, {
res_model: this.dataset.model,
res_id: null,
context: context,
view_id: +this.open_popup_action
}).open();
dialog.on('create_completed', this, this.create_completed);
return false;
},
get_popup_context: function(item) {
var context = {};
context['default_'.concat(this.date_start)] = item.start;
context['default_'.concat(this.date_stop)] = item.start.clone()
.addHours(this.date_delay || 1);
if(item.group != -1)
{
context['default_'.concat(this.last_group_bys[0])] = item.group;
}
return context;
write_completed: function(id) {
this.dataset.trigger("dataset_changed", id);
this.current_window = this.timeline.getWindow();
this.reload();
this.timeline.setWindow(this.current_window);
},
on_update: function(item, callback) {
@@ -408,47 +379,14 @@ openerp.web_timeline = function(instance) {
}
}
else {
var id_cast = parseInt(id).toString() == id ? parseInt(id) : id;
var pop = new instance.web.form.FormOpenPopup(self);
pop.on('write_completed', self, self.reload);
pop.show_element(
self.dataset.model,
id_cast,
null,
{readonly: true, title: title}
);
var form_controller = pop.view_form;
form_controller.on("load_record", self, function() {
var footer = pop.$el.closest(".modal").find(".modal-footer");
footer.find('.oe_form_button_edit,.oe_form_button_save').remove();
footer.find(".oe_form_button_cancel").prev().remove();
footer.find('.oe_form_button_cancel').before("<span> or </span>");
button_edit = _.str.sprintf("<button class='oe_button oe_form_button_edit oe_bold oe_highlight'><span> %s </span></button>",_t("Edit"));
button_save = _.str.sprintf("<button class='oe_button oe_form_button_save oe_bold oe_highlight'><span> %s </span></button>",_t("Save"));
footer.prepend(button_edit + button_save);
footer.find('.oe_form_button_save').hide();
footer.find('.oe_form_button_edit').on('click', function() {
form_controller.to_edit_mode();
footer.find('.oe_form_button_edit,.oe_form_button_save').toggle();
});
footer.find('.oe_form_button_save').on('click', function() {
form_controller.save();
form_controller.to_view_mode();
footer.find('.oe_form_button_edit,.oe_form_button_save').toggle();
});
var chatter = pop.$el.closest(".modal").find(".oe_chatter");
if(chatter.length){
var chatter_toggler = $($.parseHTML(_.str.sprintf('<div class="oe_chatter_toggle fa fa-plus-circle"><span> %s </span><div class="oe_chatter_content"></div></div>', _t("Messages"))));
chatter.before(chatter_toggler)
var chatter_content = chatter_toggler.find(".oe_chatter_content");
chatter_content.prepend(chatter);
chatter_content.toggle();
chatter_toggler.click(function(){
chatter_content.toggle();
chatter_toggler.toggleClass('fa-plus-circle fa-minus-circle');
});
}
});
var dialog = new form_common.FormViewDialog(this, {
res_model: this.dataset.model,
res_id: parseInt(id).toString() == id ? parseInt(id) : id,
context: this.dataset.get_context(),
title: title,
view_id: +this.open_popup_action
}).open();
dialog.on('write_completed', this, this.write_completed);
}
return false;
},
@@ -463,14 +401,12 @@ openerp.web_timeline = function(instance) {
}
var data = {};
data[self.fields_view.arch.attrs.date_start] =
instance.web.auto_date_to_str(start, self.fields[self.fields_view.arch.attrs.date_start].type);
data[self.fields_view.arch.attrs.date_stop] =
instance.web.auto_date_to_str(end, self.fields[self.fields_view.arch.attrs.date_stop].type);
data[self.fields_view.arch.attrs.default_group_by] = group;
time.auto_date_to_str(start, self.fields[self.fields_view.arch.attrs.date_start].type);
data[self.fields_view.arch.attrs.date_stop] =
time.auto_date_to_str(end, self.fields[self.fields_view.arch.attrs.date_stop].type);
data[self.fields_view.arch.attrs.default_group_by] = group;
var id = item.evt.id;
this.dataset.write(id, data).then(function() {
self.reload();
});
this.dataset.write(id, data);
},
on_remove: function(item, callback) {
@@ -490,15 +426,13 @@ openerp.web_timeline = function(instance) {
on_click: function(e) {
// handle a click on a group header
if(e.what == 'group-label')
{
if (e.what == 'group-label') {
return this.on_group_click(e);
}
},
on_group_click: function(e) {
if(e.group == -1)
{
if (e.group == -1) {
return;
}
return this.do_action({
@@ -506,59 +440,46 @@ openerp.web_timeline = function(instance) {
res_model: this.fields[this.last_group_bys[0]].relation,
res_id: e.group,
target: 'new',
views: [[false, 'form']],
views: [[false, 'form']]
});
},
scale_current_window: function(factor){
if (this.timeline){
this.current_window = this.timeline.getWindow();
this.current_window.end = moment(this.current_window.start).add(factor, 'hours');
this.timeline.setWindow(this.current_window);
}
},
on_today_clicked: function(){
this.current_window = {
start: new Date(),
end : new Date().addHours(24),
}
if (this.timeline){
this.timeline.setWindow(this.current_window);
}
start: new moment(),
end : new moment().add(24, 'hours')
};
if (this.timeline) {
this.timeline.setWindow(this.current_window);
}
},
get_middel_date: function(start, end){
//Get 1 hour in milliseconds
var one_hour=1000*60*60;
// Convert both dates to milliseconds
var date1_ms = start.getTime();
var date2_ms = end.getTime();
on_scale_day_clicked: function() {
this.scale_current_window(24);
},
// Calculate the difference in milliseconds
var difference_ms = date2_ms - date1_ms;
// Convert back to days and return
nb_hours = Math.round(difference_ms/one_hour);
return start.clone().addHours(nb_hours/2)
},
on_scale_week_clicked: function() {
this.scale_current_window(24 * 7);
},
scale_current_window: function(factor){
if (this.timeline){
this.current_window = this.timeline.getWindow();
this.current_window.end = this.current_window.start.clone().addHours(factor);
this.timeline.setWindow(this.current_window);
}
},
on_scale_day_clicked: function(){
this.scale_current_window(24);
},
on_scale_week_clicked: function(){
this.scale_current_window(24 * 7);
},
on_scale_month_clicked: function(){
this.scale_current_window(24 * 30);
},
on_scale_year_clicked: function(){
this.scale_current_window(24 * 365);
},
on_scale_month_clicked: function() {
this.scale_current_window(24 * 30);
},
on_scale_year_clicked: function() {
this.scale_current_window(24 * 365);
}
});
};
core.view_registry.add('timeline', TimelineView);
return TimelineView;
});