mirror of https://github.com/OCA/web.git
[FIX] web_widget_char_switchcase: check input value (#1079)
parent
2d02e3ac19
commit
6e4f751059
|
@ -3,7 +3,7 @@
|
||||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||||
{
|
{
|
||||||
"name": "Web Char Switchcase Widget",
|
"name": "Web Char Switchcase Widget",
|
||||||
"version": "10.0.1.0.0",
|
"version": "10.0.1.0.1",
|
||||||
"author": "Agile Business Group, "
|
"author": "Agile Business Group, "
|
||||||
"Odoo Community Association (OCA)",
|
"Odoo Community Association (OCA)",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
|
|
|
@ -6,6 +6,9 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
var form_widgets = require('web.form_widgets');
|
var form_widgets = require('web.form_widgets');
|
||||||
|
|
||||||
form_widgets.FieldChar.include({
|
form_widgets.FieldChar.include({
|
||||||
|
events: _.extend({}, form_widgets.FieldChar.prototype.events, {
|
||||||
|
'keyup': '_onKeyUp',
|
||||||
|
}),
|
||||||
transformations: ['default', 'upper', 'lower', 'title', 'sentence', 'camel', 'pascal', 'snake'],
|
transformations: ['default', 'upper', 'lower', 'title', 'sentence', 'camel', 'pascal', 'snake'],
|
||||||
init: function (field_manager, node) {
|
init: function (field_manager, node) {
|
||||||
this._super(field_manager, node);
|
this._super(field_manager, node);
|
||||||
|
@ -17,19 +20,10 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
if (!_.contains(this.transformations, this.current_transformation))
|
if (!_.contains(this.transformations, this.current_transformation))
|
||||||
console.error(this.current_transformation + ' case unknown');
|
console.error(this.current_transformation + ' case unknown');
|
||||||
},
|
},
|
||||||
initialize_content: function() {
|
_onKeyUp: function (event) {
|
||||||
var res = this._super();
|
var old_val = this.$input.val();
|
||||||
var self = this;
|
var new_val = this.current_transformation_handler(old_val);
|
||||||
if(this.$input) {
|
this.$input.val(new_val);
|
||||||
this.$input.keyup(function(){
|
|
||||||
var old_val = self.$input.val();
|
|
||||||
if (!old_val)
|
|
||||||
return;
|
|
||||||
var new_val = self.current_transformation_handler(old_val);
|
|
||||||
self.$input.val(new_val);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return res;
|
|
||||||
},
|
},
|
||||||
parse_value: function (val, def) {
|
parse_value: function (val, def) {
|
||||||
return this._super(this.current_transformation_handler(val), def);
|
return this._super(this.current_transformation_handler(val), def);
|
||||||
|
@ -37,17 +31,25 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
format_value: function (val, def) {
|
format_value: function (val, def) {
|
||||||
return this._super(this.current_transformation_handler(val), def);
|
return this._super(this.current_transformation_handler(val), def);
|
||||||
},
|
},
|
||||||
|
check_val: function (val) {
|
||||||
|
return val && val.trim();
|
||||||
|
},
|
||||||
get_transformation_handler: function () {
|
get_transformation_handler: function () {
|
||||||
switch (this.current_transformation) {
|
switch (this.current_transformation) {
|
||||||
case 'upper':
|
case 'upper':
|
||||||
// HELLO WORLD!
|
// HELLO WORLD!
|
||||||
return function (val) { return val.toUpperCase(); };
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
|
return val.toUpperCase();};
|
||||||
case 'lower':
|
case 'lower':
|
||||||
// hello world!
|
// hello world!
|
||||||
return function (val) { return val.toLowerCase(); };
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
|
return val.toLowerCase();};
|
||||||
case 'title':
|
case 'title':
|
||||||
// Hello World!
|
// Hello World!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
return val.replace(
|
return val.replace(
|
||||||
/\w\S*/g,
|
/\w\S*/g,
|
||||||
function(txt) {
|
function(txt) {
|
||||||
|
@ -58,6 +60,7 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
case 'sentence':
|
case 'sentence':
|
||||||
// Hello world!
|
// Hello world!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
var first = true;
|
var first = true;
|
||||||
return val.replace(
|
return val.replace(
|
||||||
/\w\S*/g,
|
/\w\S*/g,
|
||||||
|
@ -74,6 +77,7 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
case 'camel':
|
case 'camel':
|
||||||
// helloWorld!
|
// helloWorld!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
var first = true;
|
var first = true;
|
||||||
return val.replace(
|
return val.replace(
|
||||||
/\w\S*/g,
|
/\w\S*/g,
|
||||||
|
@ -90,6 +94,7 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
case 'pascal':
|
case 'pascal':
|
||||||
// HelloWorld!
|
// HelloWorld!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
return val.replace(
|
return val.replace(
|
||||||
/\w\S*/g,
|
/\w\S*/g,
|
||||||
function(txt) {
|
function(txt) {
|
||||||
|
@ -100,6 +105,7 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
case 'snake':
|
case 'snake':
|
||||||
// hello_world!
|
// hello_world!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) return val;
|
||||||
return val.toLowerCase().replace(' ', '_');
|
return val.toLowerCase().replace(' ', '_');
|
||||||
};
|
};
|
||||||
case 'default':
|
case 'default':
|
||||||
|
|
|
@ -3,88 +3,87 @@ odoo.define_section('web_widget_char_switchcase', ['web.form_common', 'web.core'
|
||||||
|
|
||||||
function createField(form_common, web_form_widgets, node) {
|
function createField(form_common, web_form_widgets, node) {
|
||||||
var field_manager = new form_common.DefaultFieldManager(null, {});
|
var field_manager = new form_common.DefaultFieldManager(null, {});
|
||||||
var field = new web_form_widgets.FieldChar(field_manager, node);
|
var fieldWidget = new web_form_widgets.FieldChar(field_manager, node);
|
||||||
field.$input = $('<input/>');
|
fieldWidget.initialize_content();
|
||||||
field.initialize_content();
|
return fieldWidget;
|
||||||
return field;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test('Default does nothing', function(assert, form_common, core, web_form_widgets) {
|
test('Default does nothing', function(assert, form_common, core, web_form_widgets) {
|
||||||
this.field = createField(form_common, web_form_widgets, {'attrs': {}});
|
var fieldWidget = createField(form_common, web_form_widgets, {'attrs': {}});
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), orig_val);
|
assert.strictEqual(fieldWidget.$input.val(), orig_val);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('UPPER OPTION', function(assert, form_common, core, web_form_widgets) {
|
test('UPPER OPTION', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'upper'}"}};
|
var node = {'attrs': {'options': "{'transform': 'upper'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), orig_val.toUpperCase());
|
assert.strictEqual(fieldWidget.$input.val(), orig_val.toUpperCase());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('lower option', function(assert, form_common, core, web_form_widgets) {
|
test('lower option', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'lower'}"}};
|
var node = {'attrs': {'options': "{'transform': 'lower'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), orig_val.toLowerCase());
|
assert.strictEqual(fieldWidget.$input.val(), orig_val.toLowerCase());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Title Option', function(assert, form_common, core, web_form_widgets) {
|
test('Title Option', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'title'}"}};
|
var node = {'attrs': {'options': "{'transform': 'title'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), 'Hello World!');
|
assert.strictEqual(fieldWidget.$input.val(), 'Hello World!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Sentence option', function(assert, form_common, core, web_form_widgets) {
|
test('Sentence option', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'sentence'}"}};
|
var node = {'attrs': {'options': "{'transform': 'sentence'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), 'Hello world!');
|
assert.strictEqual(fieldWidget.$input.val(), 'Hello world!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('camelOption', function(assert, form_common, core, web_form_widgets) {
|
test('camelOption', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'camel'}"}};
|
var node = {'attrs': {'options': "{'transform': 'camel'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), 'helloWorld!');
|
assert.strictEqual(fieldWidget.$input.val(), 'helloWorld!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('PascalOption', function(assert, form_common, core, web_form_widgets) {
|
test('PascalOption', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'pascal'}"}};
|
var node = {'attrs': {'options': "{'transform': 'pascal'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), 'HelloWorld!');
|
assert.strictEqual(fieldWidget.$input.val(), 'HelloWorld!');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('snake_option', function(assert, form_common, core, web_form_widgets) {
|
test('snake_option', function(assert, form_common, core, web_form_widgets) {
|
||||||
var node = {'attrs': {'options': "{'transform': 'snake'}"}};
|
var node = {'attrs': {'options': "{'transform': 'snake'}"}};
|
||||||
this.field = createField(form_common, web_form_widgets, node);
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
this.field.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
this.field.$input.keyup();
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(this.field.$input.val(), 'hello_world!');
|
assert.strictEqual(fieldWidget.$input.val(), 'hello_world!');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue