[FIX] web_livechat: updated web addons API
[odoo/odoo.git] / addons / web_livechat / static / src / js / web_livechat.js
1 /*############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2011 OpenERP SA (<http://openerp.com>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ############################################################################*/
20
21 var __lc_buttons = [];
22
23 openerp.web_livechat = function (openerp) {
24
25 openerp.web_livechat.Livechat = openerp.web.Widget.extend({
26     template: 'Header-LiveChat',
27
28     start: function() {
29         this._super();
30         if (!this.session)
31             return;
32         var self = this;
33         var pwc = new openerp.web.Model("publisher_warranty.contract");
34         pwc.get_func('get_default_livechat_text')().then(function(text) {
35             self.$element.html(text);
36             console.log('receiving text', text);
37             self.do_update();
38             pwc.get_func('is_livechat_enable')().then(function(res) {
39                 console.log('result', res);
40                 if(res) {
41                     self.$element.click(self.do_load_livechat);
42                 } else {
43                     self.do_action({type: 'ir.act.url', url: 'http://www.openerp.com/support-or-publisher-warranty-contract'});
44                 }
45             })
46         });
47
48         openerp.webclient.header.do_update.add_last(this.do_update);
49     },
50
51     do_update: function() {
52         var self = this;
53         if (!this.session) {
54             self.$element.remove();
55             return;
56         }
57      },
58
59     do_load_livechat: function(evt) {
60         evt.preventDefault();    
61         var self = this;
62
63         this.$element.unbind('click', this.do_load_livechat);
64
65         var lc_id = _.uniqueId('livechat_');
66         this.$element.attr('id', lc_id);
67
68         var pwc = new openerp.web.Model("publisher_warranty.contract");
69         
70         pwc.get_func('is_livechat_enable')().then(function(res) {
71             console.log('res', res);
72             if(!res) {
73                 //return;
74             }
75         // connect to LiveChat
76         __lc_load();
77
78             __lc_buttons.push({
79                 elementId: lc_id, //'livechat_status',
80                 language: 'en',
81                 skill: '0',
82                 type: 'text',
83                 labels: {
84                     online: '<img src="/web_livechat/static/src/img/available.png"/>Support',
85                     offline: '<img src="/web_livechat/static/src/img/away.png"/>Support',
86                 }
87             });
88         });
89     }
90 });
91
92 if (openerp.webclient) {
93     // tracking code from LiveChat
94     var license = '1035052',
95         params = '',
96         lang = 'en',
97         skill = '0';
98     __lc_load = function (p) { 
99       if (typeof __lc_loaded != 'function')
100         if (p) { 
101           var d = document,
102             l = d.createElement('script'),
103             s = d.getElementsByTagName('script')[0],
104             a = unescape('%26'),
105             h = ('https:' == d.location.protocol ? 'https://' : 'http://');
106           l.type = 'text/javascript';
107           l.async = true;
108           l.src = h + 'gis' + p +'.livechatinc.com/gis.cgi?serverType=control'+a+'licenseID='+license+a+'jsonp=__lc_load';
109           if (!(typeof p['server'] !== 'string' || typeof __lc_serv === 'string')) {
110             l.src = h + (__lc_serv = p['server']) + '/licence/'+license+'/script.cgi?lang='+lang+a+'groups='+skill;
111             l.src += (params == '') ? '' : a+'params='+encodeURIComponent(encodeURIComponent(params));
112             s.parentNode.insertBefore(l, s);
113           } else 
114             setTimeout(__lc_load, 1000);
115           if(typeof __lc_serv != 'string'){
116             s.parentNode.insertBefore(l, s);
117           }
118         } else __lc_load(Math.ceil(Math.random()*5));
119     }
120
121     // and add widget to webclient
122     openerp.webclient.livechat = new openerp.web_livechat.Livechat(openerp.webclient);
123     openerp.webclient.livechat.prependTo('div.header_corner');
124 }
125
126 };