[MERGE] merge with parent branch
[odoo/odoo.git] / addons / mail / static / src / js / mail_followers.js
1 openerp_mail_followers = function(session, mail) {
2     var _t = session.web._t,
3        _lt = session.web._lt;
4
5     var mail_followers = session.mail_followers = {};
6
7     /** 
8      * ------------------------------------------------------------
9      * mail_followers Widget
10      * ------------------------------------------------------------
11      *
12      * This widget handles the display of a list of records as a vertical
13      * list, with an image on the left. The widget itself is a floatting
14      * right-sided box.
15      * This widget is mainly used to display the followers of records
16      * in OpenChatter.
17      */
18
19     /* Add the widget to registry */
20     session.web.form.widgets.add('mail_followers', 'openerp.mail_followers.Followers');
21
22     mail_followers.Followers = session.web.form.AbstractField.extend({
23         template: 'mail.followers',
24
25         init: function() {
26             this._super.apply(this, arguments);
27             this.options.image = this.node.attrs.image || 'image_small';
28             this.options.title = this.node.attrs.title || 'Followers';
29             this.ds_model = new session.web.DataSetSearch(this, this.view.model);
30             this.ds_follow = new session.web.DataSetSearch(this, this.field.relation);
31         },
32
33         start: function() {
34             var self = this;
35             // NB: all the widget should be modified to check the actual_mode property on view, not use
36             // any other method to know if the view is in create mode anymore
37             this.view.on("change:actual_mode", this, this._check_visibility);
38             this._check_visibility();
39             this.$el.find('button.oe_mail_button_follow').click(function () { self.do_follow(); })
40                 .mouseover(function () { $(this).html('Follow').removeClass('oe_mail_button_mouseout').addClass('oe_mail_button_mouseover'); })
41                 .mouseleave(function () { $(this).html('Not following').removeClass('oe_mail_button_mouseover').addClass('oe_mail_button_mouseout'); });
42             this.$el.find('button.oe_mail_button_unfollow').click(function () { self.do_unfollow(); })
43                 .mouseover(function () { $(this).html('Unfollow').removeClass('oe_mail_button_mouseout').addClass('oe_mail_button_mouseover'); })
44                 .mouseleave(function () { $(this).html('Following').removeClass('oe_mail_button_mouseover').addClass('oe_mail_button_mouseout'); });
45             this.reinit();
46         },
47
48         _check_visibility: function() {
49             this.$el.toggle(this.view.get("actual_mode") !== "create");
50         },
51
52         destroy: function () {
53             this._super.apply(this, arguments);
54         },
55
56         reinit: function() {
57             this.$el.find('button.oe_mail_button_follow').hide();
58             this.$el.find('button.oe_mail_button_unfollow').hide();
59         },
60
61         set_value: function(value_) {
62             this.reinit();
63             if (! this.view.datarecord.id ||
64                 session.web.BufferedDataSet.virtual_id_regex.test(this.view.datarecord.id)) {
65                 this.$el.find('div.oe_mail_recthread_aside').hide();
66                 return;
67             }
68             return this.fetch_followers(value_);
69         },
70
71         fetch_followers: function (value_) {
72             return this.ds_follow.call('read', [value_ || this.get_value(), ['name', 'user_ids']]).then(this.proxy('display_followers'));
73         },
74
75         /** Display the followers, evaluate is_follower directly */
76         display_followers: function (records) {
77             var self = this;
78             this.message_is_follower = _.indexOf(_.flatten(_.pluck(records, 'user_ids')), this.session.uid) != -1;
79             var node_user_list = this.$el.find('ul.oe_mail_followers_display').empty();
80             this.$el.find('div.oe_mail_recthread_followers h4').html(this.options.title + ' (' + records.length + ')');
81             _(records).each(function (record) {
82                 record.avatar_url = mail.ChatterUtils.get_image(self.session, 'res.partner', 'image_small', record.id);
83                 $(session.web.qweb.render('mail.followers.partner', {'record': record})).appendTo(node_user_list);
84             });
85             if (this.message_is_follower) {
86                 this.$el.find('button.oe_mail_button_follow').hide();
87                 this.$el.find('button.oe_mail_button_unfollow').show(); }
88             else {
89                 this.$el.find('button.oe_mail_button_follow').show();
90                 this.$el.find('button.oe_mail_button_unfollow').hide(); }
91         },
92
93         do_follow: function () {
94             var context = new session.web.CompoundContext(this.build_context(), {'read_back': true});
95             return this.ds_model.call('message_subscribe_users', [[this.view.datarecord.id], undefined, context]).pipe(this.proxy('set_value'));
96         },
97
98         do_unfollow: function () {
99             var context = new session.web.CompoundContext(this.build_context(), {'read_back': true});
100             return this.ds_model.call('message_unsubscribe_users', [[this.view.datarecord.id], undefined, context]).pipe(this.proxy('set_value'));
101         },
102     });
103 };