22d042c4f3e3ca04132a2082150049dcb85fe9fa
[odoo/odoo.git] / addons / crm / static / src / js / crm_case_section.js
1 openerp.crm = function(openerp) {
2     openerp.web_kanban.KanbanRecord.include({
3         renderElement: function () {
4             var rendering = this._super();
5
6             if (this.view.dataset.model === 'crm.case.section') {
7                 var self = this;
8                 $.when(rendering).done(function() {
9                     self.$(".oe_justgage").each(function () {
10                         var $el = $(this);
11                         var title = $el.html();
12                         var unique_id = _.uniqueId("JustGage");
13                         $el.empty().css("position", "relative").attr('id', unique_id);
14
15                         new JustGage({
16                             id: unique_id,
17                             node: this,
18                             title: title,
19                             value: +$el.data('value'),
20                             min: 0,
21                             max: +$el.data('max'),
22                             relativeGaugeSize: true,
23                             humanFriendly: true,
24                             titleFontColor: '#333333',
25                             valueFontColor: '#333333',
26                             labelFontColor: '#000',
27                             label: $el.data('label'),
28                             levelColors: [
29                                 "#ff0000",
30                                 "#f9c802",
31                                 "#a9d70b"
32                             ],
33                         });
34
35                         var flag_open = false;
36                         if ($el.data('action')) {
37                             $el.click(function (event) {
38                                 event.stopPropagation();
39                                 flag_open = false;
40                                 if (!self.view.is_action_enabled('edit')) {
41                                     return;
42                                 }
43                                 if (!$el.find(".oe_justgage_edit").size()) {
44                                     var $svg = $el.find('svg');
45                                     $div = $('<div class="oe_justgage_edit" style="text-align: center; z-index: 1; position: absolute; width: ' + $svg.outerWidth() + 'px; top: ' + ($svg.outerHeight()/2-5) + 'px;"/>');
46                                     $input = $('<input style="text-align: center; width: ' + ($svg.outerWidth()-40) + 'px; margin: auto;"/>').val($el.data('value'));
47                                     $div.append($input);
48                                     $el.prepend($div)
49                                     $input.focus()
50                                         .keydown(function (event) {
51                                             event.stopPropagation();
52                                             if (event.keyCode == 13 || event.keyCode == 9) {
53                                                 if ($input.val() != $el.data('value')) {
54                                                     self.view.dataset.call($el.data('action'), [self.id, $input.val()]).then(function () {
55                                                         self.do_reload();
56                                                     });
57                                                 } else {
58                                                     $div.remove();
59                                                 }
60                                             }
61                                         })
62                                         .click(function (event) {
63                                             event.stopPropagation();
64                                             flag_open = false;
65                                         })
66                                         .blur(function (event) {
67                                             if(!flag_open) {
68                                                 $el.find(".oe_justgage_edit").remove();
69                                             } else {
70                                                 flag_open = false;
71                                                 setTimeout(function () {$input.focus();}, 0);
72                                             }
73                                         });
74                                 }
75                             }).mousedown(function () {
76                                 flag_open = true;
77                             });
78                         }
79                     });
80                     setTimeout(function () {
81                         self.$(".oe_sparkline_bar").each(function () {
82                             var $el = $(this);
83                             $el.data("title", $el.html());
84                             $el.sparkline($el.data("value").split(','), {type: 'bar', barWidth: 5} );
85                             $el.tipsy({'delayIn': 0, 'html': true, 'title': function(){return $(this).data("title")}, 'gravity': 'n'});
86                         });
87                     }, 0);
88                 });
89             }
90         },
91         on_card_clicked: function() {
92             if (this.view.dataset.model === 'crm.case.section') {
93                 this.$('.oe_kanban_crm_salesteams_list a').first().click();
94             } else {
95                 this._super.apply(this, arguments);
96             }
97         },
98     });
99
100 };