2013-09-06 10:05:28 +00:00
|
|
|
openerp.web_kanban_gauge = function (instance) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Kanban widgets: GaugeWidget
|
2014-02-11 10:04:45 +00:00
|
|
|
* options
|
|
|
|
* - max_value: maximum value of the gauge [default: 100]
|
|
|
|
* - max_field: get the max_value from the field that must be present in the
|
|
|
|
* view; takes over max_value
|
|
|
|
* - gauge_value_field: if set, the value displayed below the gauge is taken
|
|
|
|
from this field instead of the base field used for
|
|
|
|
the gauge. This allows to display a number different
|
|
|
|
from the gauge.
|
|
|
|
* - force_set: is value is 0, display a text 'Click to set' [default: True]
|
|
|
|
* - label: lable of the gauge, displayed below the gauge value
|
|
|
|
* - title: title of the gauge, displayed on top of the gauge
|
2014-02-11 12:22:40 +00:00
|
|
|
* - on_change: action to call when cliking and setting a value
|
2014-02-11 10:04:45 +00:00
|
|
|
* - on_click_label: optional label of the input displayed when clicking
|
2013-09-06 10:05:28 +00:00
|
|
|
*
|
|
|
|
*/
|
2013-09-16 13:08:25 +00:00
|
|
|
var _t = instance.web._t,
|
|
|
|
_lt = instance.web._lt;
|
2013-09-06 10:05:28 +00:00
|
|
|
|
|
|
|
instance.web_kanban.GaugeWidget = instance.web_kanban.AbstractField.extend({
|
|
|
|
className: "oe_gauge",
|
2013-12-27 16:49:11 +00:00
|
|
|
|
2013-09-06 10:05:28 +00:00
|
|
|
start: function() {
|
|
|
|
var self = this;
|
|
|
|
var parent = this.getParent();
|
2014-02-11 10:04:45 +00:00
|
|
|
// parameters
|
|
|
|
var max_value = this.options.max_value || 100;
|
2013-09-06 10:05:28 +00:00
|
|
|
if (this.options.max_field) {
|
2014-02-11 10:04:45 +00:00
|
|
|
max_value = this.getParent().record[this.options.max_field].raw_value;
|
2013-09-06 10:05:28 +00:00
|
|
|
}
|
|
|
|
var label = this.options.label || "";
|
|
|
|
if (this.options.label_field) {
|
|
|
|
label = this.getParent().record[this.options.label_field].raw_value;
|
|
|
|
}
|
2014-02-11 10:04:45 +00:00
|
|
|
var title = this.$node.html() || this.field.string;
|
|
|
|
// current gauge value
|
2013-09-06 10:05:28 +00:00
|
|
|
var val = this.field.value;
|
|
|
|
var value = _.isArray(val) && val.length ? val[val.length-1]['value'] : val;
|
2014-02-11 10:04:45 +00:00
|
|
|
// displayed value under gauge
|
|
|
|
var gauge_value = value;
|
|
|
|
if (this.options.gauge_value_field) {
|
|
|
|
gauge_value = this.getParent().record[this.options.gauge_value_field].raw_value;
|
2013-09-16 06:31:17 +00:00
|
|
|
}
|
2013-09-06 10:05:28 +00:00
|
|
|
// var unique_id = _.uniqueId("JustGage");
|
|
|
|
|
2014-02-11 10:04:45 +00:00
|
|
|
this.$el.empty().attr('style', this.$node.attr('style') + ';position:relative; display:inline-block;');
|
2013-09-06 10:05:28 +00:00
|
|
|
this.gage = new JustGage({
|
|
|
|
parentNode: this.$el[0],
|
|
|
|
// id: unique_id,
|
|
|
|
value: value,
|
|
|
|
title: title,
|
|
|
|
min: 0,
|
2014-02-11 10:04:45 +00:00
|
|
|
max: max_value,
|
2013-09-06 10:05:28 +00:00
|
|
|
relativeGaugeSize: true,
|
|
|
|
humanFriendly: true,
|
|
|
|
titleFontColor: '#333333',
|
|
|
|
valueFontColor: '#333333',
|
|
|
|
labelFontColor: '#000',
|
|
|
|
label: label,
|
2013-12-27 16:49:11 +00:00
|
|
|
levelColors: self.options.levelcolors || [
|
2013-09-06 10:05:28 +00:00
|
|
|
"#ff0000",
|
|
|
|
"#f9c802",
|
|
|
|
"#a9d70b"
|
|
|
|
],
|
|
|
|
});
|
2014-02-11 10:04:45 +00:00
|
|
|
this.gage.refresh(value, max_value);
|
2013-09-06 10:05:28 +00:00
|
|
|
|
|
|
|
var flag_open = false;
|
2014-02-11 12:22:40 +00:00
|
|
|
if (this.options.on_change) {
|
2013-09-06 10:05:28 +00:00
|
|
|
var $svg = this.$el.find('svg');
|
|
|
|
|
|
|
|
this.$el.click(function (event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
flag_open = false;
|
|
|
|
if (!parent.view.is_action_enabled('edit')) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-11 10:04:45 +00:00
|
|
|
// fade widget
|
|
|
|
$svg.fadeTo(0, 0.3);
|
|
|
|
|
|
|
|
// add input
|
2013-09-06 10:05:28 +00:00
|
|
|
if (!self.$el.find(".oe_justgage_edit").size()) {
|
|
|
|
$div = $('<div class="oe_justgage_edit" style="z-index:1"/>');
|
2014-02-11 10:04:45 +00:00
|
|
|
$div.css({
|
|
|
|
'text-align': 'center',
|
|
|
|
'position': 'absolute',
|
|
|
|
'width': self.$el.outerWidth() + 'px',
|
|
|
|
'top': (self.$el.outerHeight()/2-5) + 'px'
|
|
|
|
});
|
|
|
|
$input = $('<input/>').val(gauge_value);
|
2013-09-06 10:05:28 +00:00
|
|
|
$input.css({
|
|
|
|
'text-align': 'center',
|
|
|
|
'margin': 'auto',
|
|
|
|
'width': ($svg.outerWidth()-40) + 'px'
|
|
|
|
});
|
|
|
|
$div.append($input);
|
2014-02-11 10:04:45 +00:00
|
|
|
if (self.options.on_click_label) {
|
|
|
|
$post_input = $('<span>' + self.options.on_click_label + '</span>');
|
|
|
|
$div.append($post_input);
|
|
|
|
}
|
2013-09-06 10:05:28 +00:00
|
|
|
self.$el.prepend($div)
|
2014-02-11 10:04:45 +00:00
|
|
|
|
2013-09-06 10:05:28 +00:00
|
|
|
$input.focus()
|
|
|
|
.keydown(function (event) {
|
|
|
|
event.stopPropagation();
|
2013-09-16 13:08:25 +00:00
|
|
|
if(isNaN($input.val())){
|
|
|
|
self.do_warn(_t("Wrong value entered!"), _t("Only Integer Value should be valid."));
|
|
|
|
$div.remove();
|
2014-02-11 10:04:45 +00:00
|
|
|
$svg.fadeTo(0, 1);
|
2013-09-16 13:08:25 +00:00
|
|
|
} else {
|
|
|
|
if (event.keyCode == 13 || event.keyCode == 9) {
|
|
|
|
if ($input.val() != value) {
|
2014-02-11 10:04:45 +00:00
|
|
|
$svg.fadeTo(0, 1);
|
2014-02-11 12:22:40 +00:00
|
|
|
parent.view.dataset.call(self.options.on_change, [parent.id, $input.val()]).then(function () {
|
2013-09-16 13:08:25 +00:00
|
|
|
parent.do_reload();
|
|
|
|
});
|
|
|
|
} else {
|
2014-02-11 10:04:45 +00:00
|
|
|
$svg.fadeTo(0, 1);
|
2013-09-16 13:08:25 +00:00
|
|
|
$div.remove();
|
|
|
|
}
|
2013-09-06 10:05:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.click(function (event) {
|
|
|
|
event.stopPropagation();
|
|
|
|
flag_open = false;
|
|
|
|
})
|
|
|
|
.blur(function (event) {
|
|
|
|
if(!flag_open) {
|
|
|
|
self.$el.find(".oe_justgage_edit").remove();
|
2014-02-11 10:04:45 +00:00
|
|
|
$svg.fadeTo(0, 1);
|
2013-09-06 10:05:28 +00:00
|
|
|
} else {
|
2014-02-11 10:04:45 +00:00
|
|
|
$svg.fadeTo(0, 1);
|
2013-09-06 10:05:28 +00:00
|
|
|
flag_open = false;
|
|
|
|
setTimeout(function () {$input.focus();}, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}).mousedown(function () {
|
|
|
|
flag_open = true;
|
|
|
|
});
|
|
|
|
|
2014-02-11 10:04:45 +00:00
|
|
|
if (this.options.force_set && !+input_value) {
|
2013-09-06 10:05:28 +00:00
|
|
|
$svg.fadeTo(0, 0.3);
|
|
|
|
$div = $('<div/>').text(_t("Click to change value"));
|
|
|
|
$div.css(css);
|
|
|
|
this.$el.append($div);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
instance.web_kanban.fields_registry.add("gauge", "instance.web_kanban.GaugeWidget");
|
|
|
|
|
|
|
|
}
|