2013-03-21 11:01:52 +00:00
|
|
|
openerp.gamification = function(instance) {
|
2013-03-22 15:18:03 +00:00
|
|
|
var QWeb = instance.web.qweb;
|
2013-03-21 11:01:52 +00:00
|
|
|
|
2013-03-22 15:18:03 +00:00
|
|
|
instance.gamification.Sidebar = instance.web.Widget.extend({
|
2013-12-20 14:45:21 +00:00
|
|
|
template: 'gamification.UserWallSidebar',
|
2013-03-21 11:01:52 +00:00
|
|
|
init: function (parent, action) {
|
|
|
|
var self = this;
|
|
|
|
this._super(parent, action);
|
|
|
|
this.deferred = $.Deferred();
|
2013-12-20 14:45:21 +00:00
|
|
|
// this.res_user = new instance.web.DataSetSearch(this, 'res.users');
|
2013-03-21 11:01:52 +00:00
|
|
|
this.goals_info = {};
|
2013-04-18 10:32:29 +00:00
|
|
|
this.challenge_suggestions = {};
|
2013-03-22 11:13:44 +00:00
|
|
|
},
|
|
|
|
events: {
|
2013-12-17 16:15:41 +00:00
|
|
|
// update a challenge and related goals
|
|
|
|
'click a.oe_update_challenge': function(event) {
|
2013-03-25 16:15:27 +00:00
|
|
|
var self = this;
|
2013-12-17 16:15:41 +00:00
|
|
|
var challenge_id = parseInt(event.currentTarget.id, 10);
|
|
|
|
var goals_updated = new instance.web.Model('gamification.challenge').call('quick_update', [challenge_id]);
|
2013-03-25 16:15:27 +00:00
|
|
|
$.when(goals_updated).done(function() {
|
|
|
|
self.get_goal_todo_info();
|
|
|
|
});
|
|
|
|
},
|
2013-04-23 13:03:51 +00:00
|
|
|
// action to modify a goal
|
2013-03-24 13:46:58 +00:00
|
|
|
'click a.oe_goal_action': function(event) {
|
|
|
|
var self = this;
|
2013-04-02 12:15:14 +00:00
|
|
|
var goal_id = parseInt(event.currentTarget.id, 10);
|
2013-03-24 18:05:57 +00:00
|
|
|
var goal_action = new instance.web.Model('gamification.goal').call('get_action', [goal_id]).then(function(res) {
|
|
|
|
goal_action['action'] = res;
|
|
|
|
});
|
|
|
|
$.when(goal_action).done(function() {
|
2013-05-27 09:37:38 +00:00
|
|
|
var action = self.do_action(goal_action.action);
|
2013-04-24 08:36:32 +00:00
|
|
|
$.when(action).done(function () {
|
|
|
|
new instance.web.Model('gamification.goal').call('update', [[goal_id]]).then(function(res) {
|
|
|
|
self.get_goal_todo_info();
|
|
|
|
});
|
2013-03-29 15:45:35 +00:00
|
|
|
});
|
2013-03-24 18:05:57 +00:00
|
|
|
});
|
2013-04-18 16:05:12 +00:00
|
|
|
},
|
2013-04-23 13:03:51 +00:00
|
|
|
// get more info about a challenge request
|
2013-04-18 16:05:12 +00:00
|
|
|
'click a.oe_challenge_reply': function(event) {
|
|
|
|
var self = this;
|
2013-12-17 16:15:41 +00:00
|
|
|
var challenge_id = parseInt(event.currentTarget.id, 10);
|
|
|
|
var challenge_action = new instance.web.Model('gamification.challenge').call('reply_challenge_wizard', [challenge_id]).then(function(res) {
|
|
|
|
challenge_action['action'] = res;
|
2013-04-18 16:05:12 +00:00
|
|
|
});
|
2013-12-17 16:15:41 +00:00
|
|
|
$.when(challenge_action).done(function() {
|
|
|
|
self.do_action(challenge_action.action).done(function () {
|
2013-04-18 16:05:12 +00:00
|
|
|
self.get_goal_todo_info();
|
|
|
|
});
|
|
|
|
});
|
2013-04-02 12:15:14 +00:00
|
|
|
}
|
2013-03-21 11:01:52 +00:00
|
|
|
},
|
2013-12-20 14:45:21 +00:00
|
|
|
start: function() {
|
2013-03-21 11:01:52 +00:00
|
|
|
var self = this;
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
self.get_goal_todo_info();
|
2013-04-18 10:32:29 +00:00
|
|
|
self.get_challenge_suggestions();
|
2013-03-21 11:01:52 +00:00
|
|
|
},
|
|
|
|
get_goal_todo_info: function() {
|
|
|
|
var self = this;
|
2013-12-20 14:45:21 +00:00
|
|
|
var challenges = new instance.web.Model('res.users').call('get_serialised_gamification_summary', []).then(function(result) {
|
|
|
|
if (result.length === 0) {
|
|
|
|
self.$el.find(".oe_gamification_challenge_list").hide();
|
2013-04-18 10:32:29 +00:00
|
|
|
} else {
|
2013-12-20 14:45:21 +00:00
|
|
|
_.each(result, function(item){
|
|
|
|
var $item = $(QWeb.render("gamification.ChallengeSummary", {challenge: item}));
|
|
|
|
self.render_money_fields($item);
|
|
|
|
self.render_user_avatars($item);
|
|
|
|
self.$el.find('.oe_gamification_challenge_list').append($item);
|
|
|
|
});
|
2013-04-18 10:32:29 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
get_challenge_suggestions: function() {
|
|
|
|
var self = this;
|
2013-12-20 14:45:21 +00:00
|
|
|
var challenge_suggestions = new instance.web.Model('res.users').call('get_challenge_suggestions', []).then(function(result) {
|
|
|
|
if (result.length === 0) {
|
|
|
|
self.$el.find(".oe_gamification_suggestion").hide();
|
2013-04-18 10:32:29 +00:00
|
|
|
} else {
|
2013-12-20 14:45:21 +00:00
|
|
|
var $item = $(QWeb.render("gamification.ChallengeSuggestion", {challenges: result}));
|
|
|
|
self.$el.find('.oe_gamification_suggestion').append($item);
|
2013-03-21 11:01:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2013-12-20 14:45:21 +00:00
|
|
|
render_money_fields: function(item) {
|
2013-03-26 15:45:53 +00:00
|
|
|
var self = this;
|
|
|
|
self.dfm = new instance.web.form.DefaultFieldManager(self);
|
2013-03-27 10:50:40 +00:00
|
|
|
// Generate a FieldMonetary for each .oe_goal_field_monetary
|
2013-12-20 14:45:21 +00:00
|
|
|
item.find(".oe_goal_field_monetary").each(function() {
|
|
|
|
var currency_id = parseInt( $(this).attr('data-id'), 10);
|
2013-03-26 15:45:53 +00:00
|
|
|
money_field = new instance.web.form.FieldMonetary(self.dfm, {
|
|
|
|
attrs: {
|
|
|
|
modifiers: '{"readonly": true}'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
money_field.set('currency', currency_id);
|
|
|
|
money_field.get_currency_info();
|
2013-04-02 12:15:14 +00:00
|
|
|
money_field.set('value', parseInt($(this).text(), 10));
|
2013-03-26 15:45:53 +00:00
|
|
|
money_field.replace($(this));
|
|
|
|
});
|
2013-03-29 11:52:15 +00:00
|
|
|
},
|
2013-12-20 14:45:21 +00:00
|
|
|
render_user_avatars: function(item) {
|
2013-04-18 16:05:12 +00:00
|
|
|
var self = this;
|
2013-12-20 14:45:21 +00:00
|
|
|
item.find(".oe_user_avatar").each(function() {
|
2013-04-18 16:05:12 +00:00
|
|
|
var user_id = parseInt( $(this).attr('data-id'), 10);
|
|
|
|
var url = instance.session.url('/web/binary/image', {model: 'res.users', field: 'image_small', id: user_id});
|
|
|
|
$(this).attr("src", url);
|
|
|
|
});
|
2013-03-26 15:45:53 +00:00
|
|
|
}
|
2013-03-21 11:01:52 +00:00
|
|
|
});
|
2013-03-22 15:18:03 +00:00
|
|
|
|
|
|
|
instance.mail.Widget.include({
|
|
|
|
start: function() {
|
|
|
|
this._super();
|
2013-12-23 15:54:04 +00:00
|
|
|
var sidebar = new instance.gamification.Sidebar(this);
|
2013-03-22 15:18:03 +00:00
|
|
|
sidebar.appendTo($('.oe_mail_wall_aside'));
|
|
|
|
}
|
|
|
|
});
|
2013-04-09 14:23:02 +00:00
|
|
|
|
2013-12-23 15:54:04 +00:00
|
|
|
instance.web_kanban.KanbanView.include({
|
|
|
|
start: function() {
|
|
|
|
this._super();
|
|
|
|
$(document).off('keydown.klistener');
|
|
|
|
},
|
|
|
|
events: {
|
|
|
|
'click': function(event) {
|
|
|
|
var self = this;
|
|
|
|
if (this.ViewManager.dataset.model === 'gamification.badge') {
|
|
|
|
this.kkeys = [];
|
|
|
|
$(document).off('keydown.klistener');
|
|
|
|
$(document).on('keydown.klistener', function(event) {
|
|
|
|
if ("37,38,39,40,65,66".indexOf(event.keyCode) < 0) {
|
|
|
|
$(document).off('keydown.klistener');
|
|
|
|
} else {
|
|
|
|
self.kkeys.push(event.keyCode);
|
|
|
|
if (self.kkeys.toString().indexOf("38,38,40,40,37,39,37,39,66,65") >= 0) {
|
|
|
|
new instance.web.Model('gamification.badge').call('check_progress', []);
|
|
|
|
$(document).off('keydown.klistener');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-04-09 14:23:02 +00:00
|
|
|
instance.web_kanban.KanbanRecord.include({
|
2013-04-18 10:32:29 +00:00
|
|
|
// open related goals when clicking on challenge kanban view
|
2013-04-09 14:23:02 +00:00
|
|
|
on_card_clicked: function() {
|
2013-12-17 16:15:41 +00:00
|
|
|
if (this.view.dataset.model === 'gamification.challenge') {
|
2013-04-09 14:23:02 +00:00
|
|
|
this.$('.oe_kanban_project_list a').first().click();
|
|
|
|
} else {
|
|
|
|
this._super.apply(this, arguments);
|
|
|
|
}
|
2013-12-23 15:54:04 +00:00
|
|
|
},
|
2013-04-09 14:23:02 +00:00
|
|
|
});
|
2013-04-29 11:43:05 +00:00
|
|
|
|
2013-03-21 11:01:52 +00:00
|
|
|
};
|