6fed859449c853843f5a5dda54a6dc642e4ae26e
[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 var QWeb = openerp.web.qweb;
26 QWeb.add_template('/web_livechat/static/src/xml/web_livechat.xml');
27
28         
29 // tracking code from LiveChat
30 var license = '1035052',
31     params = '',
32     lang = 'en',
33     skill = '0';
34 __lc_load = function (p) { if (typeof __lc_loaded != 'function')
35   if (p) { var d = document, l = d.createElement('script'), s =
36     d.getElementsByTagName('script')[0], a = unescape('%26'),
37     h = ('https:' == d.location.protocol ? 'https://' : 'http://'); l.type = 'text/javascript'; l.async = true;
38     l.src = h + 'gis' + p +'.livechatinc.com/gis.cgi?serverType=control'+a+'licenseID='+license+a+'jsonp=__lc_load';
39     if (!(typeof p['server'] !== 'string' || typeof __lc_serv === 'string')) {
40       l.src = h + (__lc_serv = p['server']) + '/licence/'+license+'/script.cgi?lang='+lang+a+'groups='+skill;
41       l.src += (params == '') ? '' : a+'params='+encodeURIComponent(encodeURIComponent(params)); s.parentNode.insertBefore(l, s);
42     } else setTimeout(__lc_load, 1000); if(typeof __lc_serv != 'string'){ s.parentNode.insertBefore(l, s);}
43   } else __lc_load(Math.ceil(Math.random()*5)); }
44 __lc_load();
45
46
47
48 openerp.web_livechat.Livechat = openerp.web.Widget.extend({
49     template: 'Header-LiveChat',
50
51     start: function() {
52         this._super();
53         if (!this.session)
54             return;
55         var self = this;
56         var pwc = new openerp.web.Model(self.session, "publisher_warranty.contract");
57         pwc.get_func('get_default_livechat_text')().then(function(text) {
58             self.$element.html(text);
59             self.do_update();
60         });
61
62         openerp.webclient.header.do_update.add_last(this.do_update);
63     },
64
65     do_update: function() {
66         var self = this;
67         if (!this.session) {
68             self.$element.remove();
69             return;
70         }
71         
72         var lc_id = _.uniqueId('livechat_');
73         this.$element.attr('id', lc_id);
74
75         var pwc = new openerp.web.Model(self.session, "publisher_warranty.contract");
76         
77         pwc.get_func('is_livechat_enable')().then(function(res) {
78             console.log('res', res);
79             if(!res) {
80                 //return;
81             }
82
83             __lc_buttons.push({
84                 elementId: lc_id, //'livechat_status',
85                 language: 'en',
86                 skill: '0',
87                 type: 'text',
88                 labels: {
89                     online: '<img src="/web_livechat/static/src/img/available.png"/>Support',
90                     offline: '<img src="/web_livechat/static/src/img/away.png"/>Support',
91                 }
92             });
93         });
94     }
95 });
96
97 openerp.webclient.livechat = new openerp.web_livechat.Livechat(openerp.webclient);
98 openerp.webclient.livechat.prependTo('div.header_corner');
99
100 };