mirror of https://github.com/OCA/web.git
Merge pull request #1085 from SimoRubi/patch-2
[FIX] web_widget_char_switchcase: keep caret positionpull/1091/head
commit
bf3e29cc58
|
@ -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.1",
|
"version": "10.0.1.0.2",
|
||||||
"author": "Agile Business Group, "
|
"author": "Agile Business Group, "
|
||||||
"Odoo Community Association (OCA)",
|
"Odoo Community Association (OCA)",
|
||||||
"license": "AGPL-3",
|
"license": "AGPL-3",
|
||||||
|
|
|
@ -1,29 +1,48 @@
|
||||||
odoo.define('web_widget_char_switchcase', function (require) {
|
odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var core = require('web.core');
|
|
||||||
var formats = require('web.formats');
|
|
||||||
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, {
|
events: _.extend({}, form_widgets.FieldChar.prototype.events, {
|
||||||
'keyup': '_onKeyUp',
|
'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);
|
||||||
this.options = _.defaults(this.options, {
|
this.options = _.defaults(this.options, {
|
||||||
transform: this.transformations[0]
|
transform: this.transformations[0],
|
||||||
});
|
});
|
||||||
this.current_transformation = this.options['transform'];
|
this.current_transformation = this.options.transform;
|
||||||
this.current_transformation_handler = this.get_transformation_handler()
|
this.current_transformation_handler =
|
||||||
if (!_.contains(this.transformations, this.current_transformation))
|
this.get_transformation_handler();
|
||||||
|
if (!_.contains(this.transformations,
|
||||||
|
this.current_transformation)) {
|
||||||
console.error(this.current_transformation + ' case unknown');
|
console.error(this.current_transformation + ' case unknown');
|
||||||
|
}
|
||||||
},
|
},
|
||||||
_onKeyUp: function (event) {
|
_onKeyUp: function () {
|
||||||
|
// Save caret position
|
||||||
|
var input = this.$input[0];
|
||||||
|
var start = input.selectionStart;
|
||||||
|
var end = input.selectionEnd;
|
||||||
|
|
||||||
|
// Transform the value
|
||||||
var old_val = this.$input.val();
|
var old_val = this.$input.val();
|
||||||
var new_val = this.current_transformation_handler(old_val);
|
var new_val = this.current_transformation_handler(old_val);
|
||||||
this.$input.val(new_val);
|
this.$input.val(new_val);
|
||||||
|
|
||||||
|
// Restore caret position
|
||||||
|
input.setSelectionRange(start, end);
|
||||||
},
|
},
|
||||||
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);
|
||||||
|
@ -36,82 +55,98 @@ odoo.define('web_widget_char_switchcase', function (require) {
|
||||||
},
|
},
|
||||||
get_transformation_handler: function () {
|
get_transformation_handler: function () {
|
||||||
switch (this.current_transformation) {
|
switch (this.current_transformation) {
|
||||||
case 'upper':
|
case 'upper':
|
||||||
// HELLO WORLD!
|
// Result: HELLO WORLD!
|
||||||
return function (val) {
|
return function (val) {
|
||||||
if (!this.check_val(val)) return val;
|
if (!this.check_val(val)) {
|
||||||
return val.toUpperCase();};
|
return val;
|
||||||
case 'lower':
|
}
|
||||||
// hello world!
|
return val.toUpperCase();
|
||||||
return function (val) {
|
};
|
||||||
if (!this.check_val(val)) return val;
|
case 'lower':
|
||||||
return val.toLowerCase();};
|
// Result: hello world!
|
||||||
case 'title':
|
return function (val) {
|
||||||
// Hello World!
|
if (!this.check_val(val)) {
|
||||||
return function (val) {
|
return val;
|
||||||
if (!this.check_val(val)) return val;
|
}
|
||||||
return val.replace(
|
return val.toLowerCase();
|
||||||
/\w\S*/g,
|
};
|
||||||
function(txt) {
|
case 'title':
|
||||||
return txt.charAt(0).toUpperCase()
|
// Result: Hello World!
|
||||||
+ txt.substr(1).toLowerCase();
|
return function (val) {
|
||||||
});
|
if (!this.check_val(val)) {
|
||||||
};
|
return val;
|
||||||
case 'sentence':
|
}
|
||||||
// Hello world!
|
return val.replace(
|
||||||
return function (val) {
|
/\w\S*/g,
|
||||||
if (!this.check_val(val)) return val;
|
function (txt) {
|
||||||
var first = true;
|
return txt.charAt(0).toUpperCase() +
|
||||||
return val.replace(
|
txt.substr(1).toLowerCase();
|
||||||
/\w\S*/g,
|
});
|
||||||
function(txt) {
|
};
|
||||||
if (first){
|
case 'sentence':
|
||||||
first = false;
|
// Result: Hello world!
|
||||||
return txt.charAt(0).toUpperCase()
|
return function (val) {
|
||||||
+ txt.substr(1).toLowerCase();
|
if (!this.check_val(val)) {
|
||||||
}
|
return val;
|
||||||
else
|
}
|
||||||
return txt.toLowerCase();
|
var first = true;
|
||||||
});
|
return val.replace(
|
||||||
};
|
/\w\S*/g,
|
||||||
case 'camel':
|
function (txt) {
|
||||||
// helloWorld!
|
if (first) {
|
||||||
return function (val) {
|
first = false;
|
||||||
if (!this.check_val(val)) return val;
|
return txt.charAt(0).toUpperCase() +
|
||||||
var first = true;
|
txt.substr(1).toLowerCase();
|
||||||
return val.replace(
|
}
|
||||||
/\w\S*/g,
|
return txt.toLowerCase();
|
||||||
function(txt) {
|
});
|
||||||
if (first){
|
};
|
||||||
first = false;
|
case 'camel':
|
||||||
return txt.toLowerCase();
|
// Result: helloWorld!
|
||||||
}
|
return function (val) {
|
||||||
else
|
if (!this.check_val(val)) {
|
||||||
return txt.charAt(0).toUpperCase()
|
return val;
|
||||||
+ txt.substr(1).toLowerCase();
|
}
|
||||||
}).replace(' ', '');
|
var first = true;
|
||||||
};
|
return val.replace(
|
||||||
case 'pascal':
|
/\w\S*/g,
|
||||||
// HelloWorld!
|
function (txt) {
|
||||||
return function (val) {
|
if (first) {
|
||||||
if (!this.check_val(val)) return val;
|
first = false;
|
||||||
return val.replace(
|
return txt.toLowerCase();
|
||||||
/\w\S*/g,
|
}
|
||||||
function(txt) {
|
return txt.charAt(0).toUpperCase() +
|
||||||
return txt.charAt(0).toUpperCase()
|
txt.substr(1).toLowerCase();
|
||||||
+ txt.substr(1).toLowerCase();
|
}).replace(' ', '');
|
||||||
}).replace(' ', '');
|
};
|
||||||
};
|
case 'pascal':
|
||||||
case 'snake':
|
// Result: HelloWorld!
|
||||||
// hello_world!
|
return function (val) {
|
||||||
return function (val) {
|
if (!this.check_val(val)) {
|
||||||
if (!this.check_val(val)) return val;
|
return val;
|
||||||
return val.toLowerCase().replace(' ', '_');
|
}
|
||||||
};
|
return val.replace(
|
||||||
case 'default':
|
/\w\S*/g,
|
||||||
default:
|
function (txt) {
|
||||||
return function (val) { return val; };
|
return txt.charAt(0).toUpperCase() +
|
||||||
|
txt.substr(1).toLowerCase();
|
||||||
|
}).replace(' ', '');
|
||||||
|
};
|
||||||
|
case 'snake':
|
||||||
|
// Result: hello_world!
|
||||||
|
return function (val) {
|
||||||
|
if (!this.check_val(val)) {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
return val.toLowerCase().replace(' ', '_');
|
||||||
|
};
|
||||||
|
case 'default':
|
||||||
|
default:
|
||||||
|
return function (val) {
|
||||||
|
return val;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,89 +1,108 @@
|
||||||
odoo.define_section('web_widget_char_switchcase', ['web.form_common', 'web.core', 'web.form_widgets'], function (test) {
|
odoo.define_section('web_widget_char_switchcase',
|
||||||
'use strict';
|
['web.form_common', 'web.core', 'web.form_widgets'], function (test) {
|
||||||
|
'use strict';
|
||||||
|
|
||||||
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 fieldWidget = new web_form_widgets.FieldChar(field_manager, node);
|
var fieldWidget =
|
||||||
fieldWidget.initialize_content();
|
new web_form_widgets.FieldChar(field_manager, node);
|
||||||
return fieldWidget;
|
fieldWidget.setElement($('<input/>'));
|
||||||
}
|
fieldWidget.initialize_content();
|
||||||
|
return fieldWidget;
|
||||||
|
}
|
||||||
|
|
||||||
test('Default does nothing', function(assert, form_common, core, web_form_widgets) {
|
test('Default does nothing', function (
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, {'attrs': {}});
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var fieldWidget = createField(
|
||||||
|
form_common, web_form_widgets, {'attrs': {}});
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
var orig_val = 'Hello World!';
|
||||||
fieldWidget.$input.val(orig_val);
|
fieldWidget.$input.val(orig_val);
|
||||||
fieldWidget.$input.trigger('keyup');
|
fieldWidget.$input.trigger('keyup');
|
||||||
assert.strictEqual(fieldWidget.$input.val(), orig_val);
|
assert.strictEqual(fieldWidget.$input.val(), orig_val);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('UPPER OPTION', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'upper'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), orig_val.toUpperCase());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('lower option', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'lower'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), orig_val.toLowerCase());
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Title Option', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'title'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), 'Hello World!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Sentence option', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'sentence'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), 'Hello world!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('camelOption', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'camel'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), 'helloWorld!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('PascalOption', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'pascal'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), 'HelloWorld!');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('snake_option', function (
|
||||||
|
assert, form_common, core, web_form_widgets) {
|
||||||
|
var node = {'attrs': {'options': "{'transform': 'snake'}"}};
|
||||||
|
var fieldWidget = createField(form_common, web_form_widgets, node);
|
||||||
|
|
||||||
|
var orig_val = 'Hello World!';
|
||||||
|
fieldWidget.$input.val(orig_val);
|
||||||
|
fieldWidget.$input.trigger('keyup');
|
||||||
|
assert.strictEqual(
|
||||||
|
fieldWidget.$input.val(), 'hello_world!');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('UPPER OPTION', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'upper'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), orig_val.toUpperCase());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('lower option', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'lower'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), orig_val.toLowerCase());
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Title Option', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'title'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), 'Hello World!');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Sentence option', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'sentence'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), 'Hello world!');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('camelOption', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'camel'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), 'helloWorld!');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('PascalOption', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'pascal'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), 'HelloWorld!');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('snake_option', function(assert, form_common, core, web_form_widgets) {
|
|
||||||
var node = {'attrs': {'options': "{'transform': 'snake'}"}};
|
|
||||||
var fieldWidget = createField(form_common, web_form_widgets, node);
|
|
||||||
|
|
||||||
var orig_val = 'Hello World!';
|
|
||||||
fieldWidget.$input.val(orig_val);
|
|
||||||
fieldWidget.$input.trigger('keyup');
|
|
||||||
assert.strictEqual(fieldWidget.$input.val(), 'hello_world!');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in New Issue