summaryrefslogtreecommitdiff
path: root/addons/gamification/static/src/js/gamification.js
diff options
context:
space:
mode:
Diffstat (limited to 'addons/gamification/static/src/js/gamification.js')
-rw-r--r--addons/gamification/static/src/js/gamification.js148
1 files changed, 148 insertions, 0 deletions
diff --git a/addons/gamification/static/src/js/gamification.js b/addons/gamification/static/src/js/gamification.js
new file mode 100644
index 00000000000..2a8db5f369d
--- /dev/null
+++ b/addons/gamification/static/src/js/gamification.js
@@ -0,0 +1,148 @@
+openerp.gamification = function(instance) {
+ var QWeb = instance.web.qweb;
+
+ instance.gamification.Sidebar = instance.web.Widget.extend({
+ template: 'gamification.UserWallSidebar',
+ init: function (parent, action) {
+ var self = this;
+ this._super(parent, action);
+ this.deferred = $.Deferred();
+ this.goals_info = {};
+ this.challenge_suggestions = {};
+ $(document).off('keydown.klistener');
+ },
+ events: {
+ // update a challenge and related goals
+ 'click a.oe_update_challenge': function(event) {
+ var self = this;
+ var challenge_id = parseInt(event.currentTarget.id, 10);
+ var goals_updated = new instance.web.Model('gamification.challenge').call('quick_update', [challenge_id]);
+ $.when(goals_updated).done(function() {
+ self.get_goal_todo_info();
+ });
+ },
+ // action to modify a goal
+ 'click a.oe_goal_action': function(event) {
+ var self = this;
+ var goal_id = parseInt(event.currentTarget.id, 10);
+ 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() {
+ var action = self.do_action(goal_action.action);
+ $.when(action).done(function () {
+ new instance.web.Model('gamification.goal').call('update', [[goal_id]]).then(function(res) {
+ self.get_goal_todo_info();
+ });
+ });
+ });
+ },
+ // get more info about a challenge request
+ 'click a.oe_challenge_reply': function(event) {
+ var self = this;
+ 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;
+ });
+ $.when(challenge_action).done(function() {
+ self.do_action(challenge_action.action).done(function () {
+ self.get_goal_todo_info();
+ });
+ });
+ },
+ 'click .oe_goal h4': function(event) {
+ var self = this;
+ this.kkeys = [];
+ $(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');
+ }
+ }
+ });
+ }
+ },
+ start: function() {
+ var self = this;
+ this._super.apply(this, arguments);
+ self.get_goal_todo_info();
+ self.get_challenge_suggestions();
+ },
+ get_goal_todo_info: function() {
+ var self = this;
+ 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();
+ } else {
+ self.$el.find(".oe_gamification_challenge_list").empty();
+ _.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);
+ });
+ }
+ });
+ },
+ get_challenge_suggestions: function() {
+ var self = this;
+ 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();
+ } else {
+ var $item = $(QWeb.render("gamification.ChallengeSuggestion", {challenges: result}));
+ self.$el.find('.oe_gamification_suggestion').append($item);
+ }
+ });
+ },
+ render_money_fields: function(item) {
+ var self = this;
+ self.dfm = new instance.web.form.DefaultFieldManager(self);
+ // Generate a FieldMonetary for each .oe_goal_field_monetary
+ item.find(".oe_goal_field_monetary").each(function() {
+ var currency_id = parseInt( $(this).attr('data-id'), 10);
+ money_field = new instance.web.form.FieldMonetary(self.dfm, {
+ attrs: {
+ modifiers: '{"readonly": true}'
+ }
+ });
+ money_field.set('currency', currency_id);
+ money_field.get_currency_info();
+ money_field.set('value', parseInt($(this).text(), 10));
+ money_field.replace($(this));
+ });
+ },
+ render_user_avatars: function(item) {
+ var self = this;
+ item.find(".oe_user_avatar").each(function() {
+ 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);
+ });
+ }
+ });
+
+ instance.mail.Widget.include({
+ start: function() {
+ this._super();
+ var sidebar = new instance.gamification.Sidebar(this);
+ sidebar.appendTo($('.oe_mail_wall_aside'));
+ },
+ });
+
+ instance.web_kanban.KanbanRecord.include({
+ // open related goals when clicking on challenge kanban view
+ on_card_clicked: function() {
+ if (this.view.dataset.model === 'gamification.challenge') {
+ this.$('.oe_kanban_project_list a').first().click();
+ } else {
+ this._super.apply(this, arguments);
+ }
+ },
+ });
+
+};