Browse Source

[FIX] web_widget_char_switchcase: JSLint

pull/1085/head
SimoRubi 6 years ago
parent
commit
e224a660e0
  1. 200
      web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js
  2. 196
      web_widget_char_switchcase/static/test/web_widget_char_switchcase.js

200
web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js

@ -1,37 +1,47 @@
odoo.define('web_widget_char_switchcase', function (require) {
"use strict";
var core = require('web.core');
var formats = require('web.formats');
var form_widgets = require('web.form_widgets');
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) {
this._super(field_manager, node);
this.options = _.defaults(this.options, {
transform: this.transformations[0]
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))
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');
}
},
_onKeyUp: function (event) {
// save caret position
_onKeyUp: function () {
// Save caret position
var input = this.$input[0];
var start = input.selectionStart;
var end = input.selectionEnd;
// transform the value
// Transform the value
var old_val = this.$input.val();
var new_val = this.current_transformation_handler(old_val);
this.$input.val(new_val);
// restore caret position
// Restore caret position
input.setSelectionRange(start, end);
},
parse_value: function (val, def) {
@ -45,82 +55,98 @@ odoo.define('web_widget_char_switchcase', function (require) {
},
get_transformation_handler: function () {
switch (this.current_transformation) {
case 'upper':
// HELLO WORLD!
return function (val) {
if (!this.check_val(val)) return val;
return val.toUpperCase();};
case 'lower':
// hello world!
return function (val) {
if (!this.check_val(val)) return val;
return val.toLowerCase();};
case 'title':
// Hello World!
return function (val) {
if (!this.check_val(val)) return val;
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
});
};
case 'sentence':
// Hello world!
return function (val) {
if (!this.check_val(val)) return 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!
return function (val) {
if (!this.check_val(val)) return 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 function (val) {
if (!this.check_val(val)) return val;
return val.replace(
/\w\S*/g,
function(txt) {
return txt.charAt(0).toUpperCase()
+ txt.substr(1).toLowerCase();
}).replace(' ', '');
};
case 'snake':
// hello_world!
return function (val) {
if (!this.check_val(val)) return val;
return val.toLowerCase().replace(' ', '_');
};
case 'default':
default:
return function (val) { return val; };
case 'upper':
// Result: HELLO WORLD!
return function (val) {
if (!this.check_val(val)) {
return val;
}
return val.toUpperCase();
};
case 'lower':
// Result: hello world!
return function (val) {
if (!this.check_val(val)) {
return val;
}
return val.toLowerCase();
};
case 'title':
// Result: Hello World!
return function (val) {
if (!this.check_val(val)) {
return val;
}
return val.replace(
/\w\S*/g,
function (txt) {
return txt.charAt(0).toUpperCase() +
txt.substr(1).toLowerCase();
});
};
case 'sentence':
// Result: Hello world!
return function (val) {
if (!this.check_val(val)) {
return 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();
}
return txt.toLowerCase();
});
};
case 'camel':
// Result: helloWorld!
return function (val) {
if (!this.check_val(val)) {
return val;
}
var first = true;
return val.replace(
/\w\S*/g,
function (txt) {
if (first) {
first = false;
return txt.toLowerCase();
}
return txt.charAt(0).toUpperCase() +
txt.substr(1).toLowerCase();
}).replace(' ', '');
};
case 'pascal':
// Result: HelloWorld!
return function (val) {
if (!this.check_val(val)) {
return val;
}
return val.replace(
/\w\S*/g,
function (txt) {
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;
};
}
}
},
});
});

196
web_widget_char_switchcase/static/test/web_widget_char_switchcase.js

@ -1,90 +1,108 @@
odoo.define_section('web_widget_char_switchcase', ['web.form_common', 'web.core', 'web.form_widgets'], function (test) {
'use strict';
function createField(form_common, web_form_widgets, node) {
var field_manager = new form_common.DefaultFieldManager(null, {});
var fieldWidget = new web_form_widgets.FieldChar(field_manager, node);
fieldWidget.setElement($('<input/>'));
fieldWidget.initialize_content();
return fieldWidget;
}
test('Default does nothing', function(assert, form_common, core, web_form_widgets) {
var fieldWidget = createField(form_common, web_form_widgets, {'attrs': {}});
var orig_val = 'Hello World!';
fieldWidget.$input.val(orig_val);
fieldWidget.$input.trigger('keyup');
assert.strictEqual(fieldWidget.$input.val(), orig_val);
odoo.define_section('web_widget_char_switchcase',
['web.form_common', 'web.core', 'web.form_widgets'], function (test) {
'use strict';
function createField (form_common, web_form_widgets, node) {
var field_manager = new form_common.DefaultFieldManager(null, {});
var fieldWidget =
new web_form_widgets.FieldChar(field_manager, node);
fieldWidget.setElement($('<input/>'));
fieldWidget.initialize_content();
return fieldWidget;
}
test('Default does nothing', function (
assert, form_common, core, web_form_widgets) {
var fieldWidget = createField(
form_common, web_form_widgets, {'attrs': {}});
var orig_val = 'Hello World!';
fieldWidget.$input.val(orig_val);
fieldWidget.$input.trigger('keyup');
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…
Cancel
Save