openerp_mail_followers = function(session, mail) { var _t = session.web._t, _lt = session.web._lt; var mail_followers = session.mail_followers = {}; /** * ------------------------------------------------------------ * mail_followers Widget * ------------------------------------------------------------ * * This widget handles the display of a list of records as a vertical * list, with an image on the left. The widget itself is a floatting * right-sided box. * This widget is mainly used to display the followers of records * in OpenChatter. */ /* Add the widget to registry */ session.web.form.widgets.add('mail_followers', 'openerp.mail_followers.Followers'); mail_followers.Followers = session.web.form.AbstractField.extend({ template: 'mail.followers', init: function() { this._super.apply(this, arguments); this.options.image = this.node.attrs.image || 'image_small'; this.options.title = this.node.attrs.title || 'Followers'; this.options.context = this.node.attrs.context; this.options.comment = this.node.attrs.help || false; this.ds_model = new session.web.DataSetSearch(this, this.view.model); this.sub_model = new session.web.DataSetSearch(this,'mail.message.subtype'); this.ds_follow = new session.web.DataSetSearch(this, this.field.relation); this.follower_model = new session.web.DataSetSearch(this,'mail.followers'); }, start: function() { // use actual_mode property on view to know if the view is in create mode anymore this.view.on("change:actual_mode", this, this._check_visibility); this._check_visibility(); this.reinit(); this.bind_events(); }, _check_visibility: function() { this.$el.toggle(this.view.get("actual_mode") !== "create"); }, reinit: function() { this.message_is_follower == undefined; this.display_buttons(); }, bind_events: function() { var self = this; this.$('div.oe_mouse_subtypes') .on('mouseover', function () { $(this).removeClass('oe_mouseout').addClass('oe_mouseover'); self.display_subtypes(); }) .on('mouseleave', function () { $(this).removeClass('oe_mouseover').addClass('oe_mouseout'); self.display_subtypes(); }); this.$('button.oe_follower') .on('click', function () { if($(this).hasClass('oe_notfollow')) self.do_follow(); else self.do_unfollow(); }) .on('mouseover', function () { $(this).removeClass('oe_mouseout').addClass('oe_mouseover'); }) .on('mouseleave', function () { $(this).removeClass('oe_mouseover').addClass('oe_mouseout'); }); this.$el.on('click', 'ul.oe_subtypes input', function () { self.do_update_subscription(); }) this.$el.on('click', 'button.oe_invite', function(event) { action = { type: 'ir.actions.act_window', res_model: 'mail.wizard.invite', view_mode: 'form', view_type: 'form', views: [[false, 'form']], target: 'new', context: { 'default_res_model': self.view.dataset.model, 'default_res_id': self.view.datarecord.id }, } self.do_action(action, function() { self.read_value(); }); }); }, read_value: function() { var self = this; return this.ds_model.read_ids([this.view.datarecord.id], ['message_follower_ids']).pipe(function (results) { self.set_value(results[0].message_follower_ids); }); }, set_value: function(value_) { this.reinit(); return this.fetch_followers(value_ || this.get_value()); }, set_is_follower: function(value_) { for(var i in value_){ if(value_[i]['user_ids'][0]==this.session.uid) this.message_is_follower=true; this.display_buttons(); return true; } this.message_is_follower=false; this.display_buttons(); return false; }, fetch_followers: function (value_) { this.value = value_ || {}; this.message_is_follower = (this.getParent().fields.message_is_follower && this.getParent().fields.message_is_follower.get_value()); if(value_) return this.ds_follow.call('read', [this.value, ['name', 'user_ids']]).pipe(this.proxy('display_followers'), this.proxy('display_generic')); }, /* Display generic info about follower, for people not having access to res_partner */ display_generic: function (error, event) { event.preventDefault(); var node_user_list = this.$('ul.oe_mail_followers_display').empty(); // format content: Followers (You and 0 other) // Followers (3) var content = this.options.title; if (this.message_is_follower) { content += ' (You and ' + (this.value.length-1) + ' other)'; } else { content += ' (' + this.value.length + ')' } this.$('div.oe_mail_recthread_followers h4').html(content); this.display_buttons(); return $.when(); }, /** Display the followers, evaluate is_follower directly */ display_followers: function (records) { var self = this; var node_user_list = this.$('ul.oe_mail_followers_display').empty(); this.$('div.oe_mail_recthread_followers h4').html(this.options.title + (records.length>=5 ? ' (' + records.length + ')' : '') ); console.log(records); for(var i=0; i