6d02a39cf1f158d59b5c3a73585eaa99f482269c
[odoo/odoo.git] / addons / share / static / src / js / share.js
1
2 openerp.share = function(instance) {
3 var QWeb = instance.web.qweb;
4 QWeb.add_template('/share/static/src/xml/share.xml');
5
6 function launch_wizard(self, view) {
7         var action = view.widget_parent.action;
8         var Share = new instance.web.DataSet(self, 'share.wizard', view.dataset.get_context());
9         var domain = view.dataset.domain;
10
11         if (view.fields_view.type == 'form') {
12             domain = new instance.web.CompoundDomain(domain, [['id', '=', view.datarecord.id]]);
13         }
14
15         Share.create({
16             name: action.name,
17             domain: domain,
18             action_id: action.id,
19         }, function(result) {
20             var share_id = result.result;
21             var step1 = Share.call('go_step_1', [[share_id],], function(result) {
22                 var action = result;
23                 self.do_action(action);
24             });
25         });
26 }
27
28 var _has_share = null;
29 function if_has_share(yes, no) {
30     if (!_has_share) {
31         _has_share = $.Deferred(function() {
32             var self = this;
33             var session = instance.webclient.session;
34             session.on_session_invalid.add_last(function() { _has_share = null; });
35             var func = new instance.web.Model(session, "share.wizard").get_func("has_share");
36             func(session.uid).pipe(function(res) {
37                 if(res) {
38                     self.resolve();
39                 } else {
40                     self.reject();
41                 }
42             });
43         });
44     }
45     _has_share.done(yes).fail(no);
46 }
47
48
49 instance.web.Sidebar = instance.web.Sidebar.extend({
50     add_default_sections: function() {
51         this._super();
52         var self = this;
53         if_has_share(function() {
54             self.add_items('other', [{
55                 label: 'Share',
56                 callback: self.on_sidebar_click_share,
57                 classname: 'oe-share',
58             }]);
59         });
60     },
61     on_sidebar_click_share: function(item) {
62         var view = this.widget_parent
63         launch_wizard(this, view);
64     },
65 });
66
67 instance.web.ViewManagerAction.include({
68     start: function() {
69         var self = this;
70         if_has_share(function() {
71             self.$element.find('a.oe-share').click(self.on_click_share);
72         }, function() {
73             self.$element.find('a.oe-share').remove();
74         });
75         return this._super.apply(this, arguments);
76     },
77     on_click_share: function(e) {
78         e.preventDefault();
79         launch_wizard(this, this.views[this.active_view].controller);
80     },
81 });
82
83 };