[FIX] replace integer with float in validation
[odoo/odoo.git] / addons / web_kanban_gauge / static / src / js / kanban_gauge.js
1 openerp.web_kanban_gauge = function (instance) {
2
3 /**
4  * Kanban widgets: GaugeWidget
5  *
6  */
7 var _t = instance.web._t,
8    _lt = instance.web._lt;
9 instance.web_kanban.GaugeWidget = instance.web_kanban.AbstractField.extend({
10     className: "oe_gauge",
11     start: function() {
12         var self = this;
13         var parent = this.getParent();
14         var max = 100;
15         if (this.options.max_field) {
16             max = this.getParent().record[this.options.max_field].raw_value;
17         }
18         var label = this.options.label || "";
19         if (this.options.label_field) {
20             label = this.getParent().record[this.options.label_field].raw_value;
21         }
22         var val = this.field.value;
23         var value = _.isArray(val) && val.length ? val[val.length-1]['value'] : val;
24         var title = this.$node.html() || this.field.string;
25         // var unique_id = _.uniqueId("JustGage");
26
27         this.$el.empty()
28             .attr('style', this.$node.attr('style') + ';position:relative; display:inline-block;');
29
30         this.gage = new JustGage({
31             parentNode: this.$el[0],
32             // id: unique_id,
33             value: value,
34             title: title,
35             min: 0,
36             max: max,
37             relativeGaugeSize: true,
38             humanFriendly: true,
39             titleFontColor: '#333333',
40             valueFontColor: '#333333',
41             labelFontColor: '#000',
42             label: label,
43             levelColors: [
44                 "#ff0000",
45                 "#f9c802",
46                 "#a9d70b"
47             ],
48         });
49         this.gage.refresh(value, max);
50
51         var flag_open = false;
52         if (this.options.action_change) {
53             var $svg = this.$el.find('svg');
54             var css = {
55                 'text-align': 'center',
56                 'position': 'absolute',
57                 'width': this.$el.outerWidth() + 'px',
58                 'top': (this.$el.outerHeight()/2-5) + 'px'
59             };
60
61             this.$el.click(function (event) {
62                 event.stopPropagation();
63                 flag_open = false;
64                 if (!parent.view.is_action_enabled('edit')) {
65                     return;
66                 }
67                 if (!self.$el.find(".oe_justgage_edit").size()) {
68                     $div = $('<div class="oe_justgage_edit" style="z-index:1"/>');
69                     $div.css(css);
70                     $input = $('<input/>').val(value);
71                     $input.css({
72                         'text-align': 'center',
73                         'margin': 'auto',
74                         'width': ($svg.outerWidth()-40) + 'px'
75                     });
76                     $div.append($input);
77                     self.$el.prepend($div)
78                     $input.focus()
79                         .keydown(function (event) {
80                             event.stopPropagation();
81                             if(isNaN($input.val())){
82                                 self.do_warn(_t("Wrong value entered!"), _t("Only Integer Value should be valid."));
83                                 $div.remove();
84                             } else {
85                                 if (event.keyCode == 13 || event.keyCode == 9) {
86                                     var val = self.parse_client($input.val());
87                                     if ($input.val() != value) {
88                                         parent.view.dataset.call(self.options.action_change, [parent.id, $input.val()]).then(function () {
89                                             parent.do_reload();
90                                         });
91                                     } else {
92                                         $div.remove();
93                                     }
94                                 }
95                             }
96                         })
97                         .click(function (event) {
98                             event.stopPropagation();
99                             flag_open = false;
100                         })
101                         .blur(function (event) {
102                             if(!flag_open) {
103                                 self.$el.find(".oe_justgage_edit").remove();
104                             } else {
105                                 flag_open = false;
106                                 setTimeout(function () {$input.focus();}, 0);
107                             }
108                         });
109                 }
110             }).mousedown(function () {
111                 flag_open = true;
112             });
113
114             if (!+value) {
115                 $svg.fadeTo(0, 0.3);
116                 $div = $('<div/>').text(_t("Click to change value"));
117                 $div.css(css);
118                 this.$el.append($div);
119             }
120         }
121     },
122
123     parse_client: function(value) {
124         return openerp.web.parse_value(value, { type:"float" });
125     },
126
127 });
128
129 instance.web_kanban.fields_registry.add("gauge", "instance.web_kanban.GaugeWidget");
130
131 }