~openerp-dev/openerp-web/trunk-web_pdf_viewer-pga

« back to all changes in this revision

Viewing changes to addons/web_kanban_gauge/static/src/js/kanban_gauge.js

  • Committer: Parth Gajjar(Open ERP)
  • Date: 2013-09-16 11:25:36 UTC
  • mfrom: (3781.1.59 openerp-web)
  • Revision ID: pga@tinyerp.com-20130916112536-5b0zi1yf22wgsuij
merged with openerp-web

Show diffs side-by-side

added added

removed removed

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