[IMP] share: better way to restrict the feature to the technical group.
[odoo/odoo.git] / addons / share / static / src / js / share.js
1
2 openerp.share = function(session) {
3     var _t = session.web._t;
4     var has_action_id = false;
5
6     function launch_wizard(self, view, user_type, invite) {
7         var action = view.getParent().action;
8         var Share = new session.web.DataSet(self, 'share.wizard', view.dataset.get_context());
9         var domain = new session.web.CompoundDomain(view.dataset.domain);
10         if (view.fields_view.type == 'form') {
11             domain = new session.web.CompoundDomain(domain, [['id', '=', view.datarecord.id]]);
12         }
13         if (view.fields_view.type == 'form') rec_name = view.datarecord.name;
14         else rec_name = '';
15         session.web.pyeval.eval_domains_and_contexts({
16             domains: [domain],
17             contexts: [Share.get_context()]
18         }).done(function (result) {
19             Share.create({
20                 name: action.name,
21                 record_name: rec_name,
22                 domain: result.domain,
23                 action_id: action.id,
24                 user_type: user_type || 'embedded',
25                 view_type: view.fields_view.type,
26                 invite: invite || false,
27             }).done(function(share_id) {
28                 var step1 = Share.call('go_step_1', [[share_id], Share.get_context()]).done(function(result) {
29                     var action = result;
30                     self.do_action(action);
31                 });
32             });
33         });
34     }
35
36     function has_share(yes, no) {
37         if (!session.session.share_flag) {
38             session.session.share_flag = $.Deferred(function() {
39                 var func = new session.web.Model("share.wizard").get_func("has_share");
40                 func(session.session.uid).then(function(res) {
41                     if(res) {
42                         session.session.share_flag.resolve();
43                     } else {
44                         session.session.share_flag.reject();
45                     }
46                 });
47             });
48         }
49         session.session.share_flag.done(yes).fail(no);
50     }
51
52     /* Extend the Sidebar to add Share and Embed links in the 'More' menu */
53     session.web.Sidebar = session.web.Sidebar.extend({
54
55         start: function() {
56             var self = this;
57             this._super(this);
58             has_share(function() {
59                 self.add_items('other', [
60                     {   label: _t('Share'),
61                         callback: self.on_click_share,
62                         classname: 'oe_share' },
63                     {   label: _t('Embed'),
64                         callback: self.on_click_share_link,
65                         classname: 'oe_share' },
66                 ]);
67             });
68         },
69
70         on_click_share: function(item) {
71             var view = this.getParent()
72             launch_wizard(this, view, 'emails', false);
73         },
74
75         on_click_share_link: function(item) {
76             var view = this.getParent()
77             launch_wizard(this, view, 'embedded', false);
78         },
79     });
80
81     /**
82      * Extends mail (Chatter widget)
83      * - show the 'invite' button' only we came on the form view through
84      *   an action. We do this because 'invite' is based on the share
85      *   mechanism, and it tries to share an action.
86      */
87     session.mail.RecordThread.include( {
88         start: function() {
89             start_res = this._super.apply(this, arguments);
90             if (has_action_id) {
91                 this.$el.find('button.oe_share_invite').show();
92             }
93             return start_res;
94         }
95     });
96
97     session.web.ViewManagerAction.include({
98         start: function() {
99             var self = this;
100             this.check_if_action_is_defined();
101             has_share(function() {
102                 self.$el.delegate('button.oe_share_invite', 'click', self.on_click_share_invite);
103             });
104             return this._super.apply(this, arguments);
105         },
106     
107         check_if_action_is_defined: function() {
108             if (this.action && this.action.id) {
109                 has_action_id = true;
110             }
111             else {
112                 has_action_id = false;
113             }
114         },
115
116         on_click_share_invite: function(e) {
117             e.preventDefault();
118             launch_wizard(this, this.views[this.active_view].controller, 'emails', true);
119         },
120     });
121 };
122