Skip to content

Commit

Permalink
fixup! [ADD] web_widget_char_switchcase
Browse files Browse the repository at this point in the history
  • Loading branch information
SimoRubi committed Oct 10, 2018
1 parent 9b602af commit f5cfb58
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 43 deletions.
Expand Up @@ -6,13 +6,14 @@ odoo.define('web_widget_char_switchcase', function (require) {
var form_widgets = require('web.form_widgets');

form_widgets.FieldChar.include({
transformations: ['upper', 'lower', 'title', 'sentence', 'camel', 'pascal', 'snake'],
transformations: ['default', 'upper', 'lower', 'title', 'sentence', 'camel', 'pascal', 'snake'],
init: function (field_manager, node) {
this._super(field_manager, node);
this.options = _.defaults(this.options, {
transform: this.transformations[0]
});
this.current_transformation = this.options['transform'];
this.current_transformation_handler = this.get_transformation_handler()
if (!_.contains(this.transformations, this.current_transformation))
console.error(this.current_transformation + ' case unknown');
},
Expand All @@ -22,7 +23,9 @@ odoo.define('web_widget_char_switchcase', function (require) {
if(this.$input) {
this.$input.keyup(function(){
var old_val = self.$input.val();
var new_val = self.transform(old_val);
if (!old_val)
return;
var new_val = self.current_transformation_handler(old_val);
self.$input.val(new_val);
});
}
Expand All @@ -34,61 +37,74 @@ odoo.define('web_widget_char_switchcase', function (require) {
format_value: function (val, def) {
return this._super(this.transform(val), def);
},
transform: function (val) {
if (!val)
return val;
get_transformation_handler: function () {
switch (this.current_transformation) {
case 'upper':
// HELLO WORLD!
return val.toUpperCase();
return function (val) { return val.toUpperCase(); };
case 'lower':
// hello world!
return val.toLowerCase();
return function (val) { return val.toLowerCase(); };
case 'title':
// Hello World!
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
});
return function (val) {
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
});
};
case 'sentence':
// Hello world!
var first = true;
return val.replace(
/\w\S*/g,
function(txt) {
if (first){
first = false;
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}
else
return txt.toLowerCase();
});
return function (val) {
var first = true;
return val.replace(
/\w\S*/g,
function(txt) {
if (first){
first = false;
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
}
else
return txt.toLowerCase();
});
};
case 'camel':
// helloWorld!
var first = true;
return val.replace(
/\w\S*/g,
function(txt) {
if (first){
first = false;
return txt.toLowerCase();
}
else
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}).replace(' ', '');
return function (val) {
var first = true;
return val.replace(
/\w\S*/g,
function(txt) {
if (first){
first = false;
return txt.toLowerCase();
}
else
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
}).replace(' ', '');
};
case 'pascal':
// HelloWorld!
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();
}).replace(' ', '');
return function (val) {
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
}).replace(' ', '');
};
case 'snake':
// hello_world!
return val.toLowerCase().replace(' ', '_');
return function (val) {
return val.toLowerCase().replace(' ', '_');
};
case 'default':
default:
return val;
return function (val) { return val; };
}
}
});
Expand Down
Expand Up @@ -9,13 +9,13 @@ odoo.define_section('web_widget_char_switchcase', ['web.form_common', 'web.core'
return field;
}

test('Default is upper', 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 orig_val = 'Hello World!';
this.field.$input.val(orig_val);
this.field.$input.keyup();
assert.strictEqual(this.field.$input.val(), orig_val.toUpperCase());
assert.strictEqual(this.field.$input.val(), orig_val);
});

test('UPPER OPTION', function(assert, form_common, core, web_form_widgets) {
Expand Down

0 comments on commit f5cfb58

Please sign in to comment.