mirror of
https://github.com/OCA/web.git
synced 2025-02-22 13:21:25 +02:00
Improve code according to the comments
This commit is contained in:
@@ -21,20 +21,18 @@
|
||||
##############################################################################
|
||||
{
|
||||
'name': 'Web text limited widget',
|
||||
'version': '0.1.0',
|
||||
'version': '7.0.1.0.0',
|
||||
'author': 'initOS GmbH & Co. KG',
|
||||
'category': '',
|
||||
'category': 'web',
|
||||
'description': """
|
||||
|
||||
* Add new functionality for TextField, but data are limited to
|
||||
10 lines or 500 characters (by default).
|
||||
You can change default values by context varibles 'limit_lines' and
|
||||
'limit_chars'.
|
||||
* Add new functionality for TextField.
|
||||
You can change default values by context varibles 'maxlines' and
|
||||
'maxlength'.
|
||||
If data contains more characters or lines, it will be cut.
|
||||
Example of usage:
|
||||
<field name="some_text_field"
|
||||
widget="text_limited"
|
||||
context="{'limit_lines': 8, 'limit_chars': 400}"
|
||||
context="{'maxlines': 8, 'maxlength': 400}"
|
||||
/>
|
||||
""",
|
||||
'website': 'http://www.initos.com',
|
||||
|
||||
@@ -7,10 +7,8 @@ var _t = instance.web._t;
|
||||
|
||||
instance.web.form.FieldText.include({
|
||||
template: 'FieldText',
|
||||
LIMIT_LINES_CONTEXT_KEY: 'limit_lines',
|
||||
LIMIT_LINES_DEFAULT: 10,
|
||||
LIMIT_CHARS_CONTEXT_KEY: 'limit_chars',
|
||||
LIMIT_CHARS_DEFAULT: 500,
|
||||
LIMIT_LINES_CONTEXT_KEY: 'maxlines',
|
||||
LIMIT_CHARS_CONTEXT_KEY: 'maxlength',
|
||||
|
||||
events: {
|
||||
'keyup': function (e) {
|
||||
@@ -25,31 +23,45 @@ instance.web.form.FieldText.include({
|
||||
limit_value: function($textarea)
|
||||
{
|
||||
var ctx = this.build_context().eval();
|
||||
var limit_lines = ctx[this.LIMIT_LINES_CONTEXT_KEY]*1
|
||||
if (!limit_lines){
|
||||
limit_lines = this.LIMIT_LINES_DEFAULT;
|
||||
console.log("No default values found for limit lines in '"
|
||||
+ this.name + "' field. Default value " + limit_lines
|
||||
+ " will be used.");
|
||||
}
|
||||
|
||||
var limit_chars = ctx[this.LIMIT_CHARS_CONTEXT_KEY]*1
|
||||
if (!limit_chars){
|
||||
limit_chars = this.LIMIT_CHARS_DEFAULT;
|
||||
console.log("No default values found for limit chars in '"
|
||||
+ this.name + "' field. Default value " + limit_chars
|
||||
+ " will be used.");
|
||||
}
|
||||
var maxlines = ctx[this.LIMIT_LINES_CONTEXT_KEY]*1
|
||||
var maxlength = ctx[this.LIMIT_CHARS_CONTEXT_KEY]*1
|
||||
|
||||
var value = $textarea.val();
|
||||
var lines = value.split("\n");
|
||||
if (lines.length > limit_lines){
|
||||
$textarea.val(lines.slice(0, limit_lines).join("\n"));
|
||||
|
||||
if (maxlines && lines.length > maxlines){
|
||||
$textarea.val(lines.slice(0, maxlines).join("\n"));
|
||||
}
|
||||
if (value.length > limit_chars){
|
||||
$textarea.val(value.slice(0, limit_chars));
|
||||
if (maxlength && value.length > maxlength){
|
||||
$textarea.val(value.slice(0, maxlength));
|
||||
}
|
||||
this.$el.find('span.length_limit').html(value.length + '/' + limit_chars);
|
||||
this.$el.find('span.length_limit').html(value.length + '/' + maxlength);
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
instance.web.form.FieldChar.include({
|
||||
template: 'FieldChar',
|
||||
LIMIT_CHARS_CONTEXT_KEY: 'maxlength',
|
||||
|
||||
events: {
|
||||
'keyup': function (e) {
|
||||
this.limit_value($(e.target));
|
||||
},
|
||||
'change textarea': 'store_dom_value',
|
||||
},
|
||||
|
||||
limit_value: function($textarea)
|
||||
{
|
||||
var ctx = this.build_context().eval();
|
||||
var maxlength = ctx[this.LIMIT_CHARS_CONTEXT_KEY]*1
|
||||
|
||||
var value = $textarea.val();
|
||||
|
||||
if (maxlength && value.length > maxlength){
|
||||
$textarea.val(value.slice(0, maxlength));
|
||||
}
|
||||
this.$el.find('span.length_limit').html(value.length + '/' + maxlength);
|
||||
},
|
||||
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user