From a9a56bd0153f3a41250079357f8186b3b57e1b60 Mon Sep 17 00:00:00 2001 From: Simone Rubino Date: Fri, 12 Oct 2018 17:37:36 +0200 Subject: [PATCH] [ADD] web_widget_switchcase (#1064) --- web_widget_char_switchcase/README.rst | 97 ++++ web_widget_char_switchcase/__init__.py | 2 + web_widget_char_switchcase/__manifest__.py | 20 + .../readme/CONFIGURE.rst | 10 + .../readme/DESCRIPTION.rst | 1 + web_widget_char_switchcase/readme/USAGE.rst | 11 + .../static/description/index.html | 439 ++++++++++++++++++ .../src/js/web_widget_char_switchcase.js | 111 +++++ .../static/test/web_widget_char_switchcase.js | 90 ++++ web_widget_char_switchcase/tests/__init__.py | 4 + web_widget_char_switchcase/tests/test_js.py | 15 + .../views/web_widget_char_switchcase.xml | 15 + 12 files changed, 815 insertions(+) create mode 100644 web_widget_char_switchcase/README.rst create mode 100644 web_widget_char_switchcase/__init__.py create mode 100644 web_widget_char_switchcase/__manifest__.py create mode 100644 web_widget_char_switchcase/readme/CONFIGURE.rst create mode 100644 web_widget_char_switchcase/readme/DESCRIPTION.rst create mode 100644 web_widget_char_switchcase/readme/USAGE.rst create mode 100644 web_widget_char_switchcase/static/description/index.html create mode 100644 web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js create mode 100644 web_widget_char_switchcase/static/test/web_widget_char_switchcase.js create mode 100644 web_widget_char_switchcase/tests/__init__.py create mode 100644 web_widget_char_switchcase/tests/test_js.py create mode 100644 web_widget_char_switchcase/views/web_widget_char_switchcase.xml diff --git a/web_widget_char_switchcase/README.rst b/web_widget_char_switchcase/README.rst new file mode 100644 index 00000000..9ae27ddc --- /dev/null +++ b/web_widget_char_switchcase/README.rst @@ -0,0 +1,97 @@ +========================== +Web Char Switchcase Widget +========================== + +.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github + :target: https://github.com/OCA/web/tree/10.0/web_widget_char_switchcase + :alt: OCA/web +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/web-10-0/web-10-0-web_widget_char_switchcase + :alt: Translate me on Weblate +.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png + :target: https://runbot.odoo-community.org/runbot/162/10.0 + :alt: Try me on Runbot + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module improves the char field and enables it to transform its own text. + +**Table of contents** + +.. contents:: + :local: + +Configuration +============= + +In any char field (suppose it has value *Hello World!*), add the option `transform` having one of the following values: + +* `default` or no value produces *Hello World!* +* `upper` produces *HELLO WORLD!* +* `lower` produces *hello world!* +* `title` produces *Hello World!* +* `sentence` produces *Hello world!* +* `camel` produces *helloWorld!* +* `pascal` produces *HelloWorld!* +* `snake` produces *hello_world!* + +Usage +===== + +Switch the text of the char field `name` to uppercase: + +.. code-block:: xml + + + +Switch the text of the char field `name` to lowercase: + +.. code-block:: xml + + + +Bug Tracker +=========== + +Bugs are tracked on `GitHub Issues `_. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +`feedback `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +~~~~~~~ + +* Agile Business Group + +Maintainers +~~~~~~~~~~~ + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/web `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/web_widget_char_switchcase/__init__.py b/web_widget_char_switchcase/__init__.py new file mode 100644 index 00000000..4cdf2e8b --- /dev/null +++ b/web_widget_char_switchcase/__init__.py @@ -0,0 +1,2 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). diff --git a/web_widget_char_switchcase/__manifest__.py b/web_widget_char_switchcase/__manifest__.py new file mode 100644 index 00000000..b171eb2a --- /dev/null +++ b/web_widget_char_switchcase/__manifest__.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). +{ + "name": "Web Char Switchcase Widget", + "version": "10.0.1.0.0", + "author": "Agile Business Group, " + "Odoo Community Association (OCA)", + "license": "AGPL-3", + "category": "Web", + "website": "https://github.com/OCA/web/tree/" + "10.0/web_widget_char_switchcase", + 'installable': True, + "depends": [ + "web", + ], + "data": [ + "views/web_widget_char_switchcase.xml", + ] +} diff --git a/web_widget_char_switchcase/readme/CONFIGURE.rst b/web_widget_char_switchcase/readme/CONFIGURE.rst new file mode 100644 index 00000000..51dbe75f --- /dev/null +++ b/web_widget_char_switchcase/readme/CONFIGURE.rst @@ -0,0 +1,10 @@ +In any char field (suppose it has value *Hello World!*), add the option `transform` having one of the following values: + +* `default` or no value produces *Hello World!* +* `upper` produces *HELLO WORLD!* +* `lower` produces *hello world!* +* `title` produces *Hello World!* +* `sentence` produces *Hello world!* +* `camel` produces *helloWorld!* +* `pascal` produces *HelloWorld!* +* `snake` produces *hello_world!* diff --git a/web_widget_char_switchcase/readme/DESCRIPTION.rst b/web_widget_char_switchcase/readme/DESCRIPTION.rst new file mode 100644 index 00000000..c679c2fd --- /dev/null +++ b/web_widget_char_switchcase/readme/DESCRIPTION.rst @@ -0,0 +1 @@ +This module improves the char field and enables it to transform its own text. diff --git a/web_widget_char_switchcase/readme/USAGE.rst b/web_widget_char_switchcase/readme/USAGE.rst new file mode 100644 index 00000000..95fc5408 --- /dev/null +++ b/web_widget_char_switchcase/readme/USAGE.rst @@ -0,0 +1,11 @@ +Switch the text of the char field `name` to uppercase: + +.. code-block:: xml + + + +Switch the text of the char field `name` to lowercase: + +.. code-block:: xml + + diff --git a/web_widget_char_switchcase/static/description/index.html b/web_widget_char_switchcase/static/description/index.html new file mode 100644 index 00000000..935648a7 --- /dev/null +++ b/web_widget_char_switchcase/static/description/index.html @@ -0,0 +1,439 @@ + + + + + + +Web Char Switchcase Widget + + + +
+

Web Char Switchcase Widget

+ + +

Beta License: AGPL-3 OCA/web Translate me on Weblate Try me on Runbot

+

This module improves the char field and enables it to transform its own text.

+

Table of contents

+ +
+

Configuration

+

In any char field (suppose it has value Hello World!), add the option transform having one of the following values:

+
    +
  • default or no value produces Hello World!
  • +
  • upper produces HELLO WORLD!
  • +
  • lower produces hello world!
  • +
  • title produces Hello World!
  • +
  • sentence produces Hello world!
  • +
  • camel produces helloWorld!
  • +
  • pascal produces HelloWorld!
  • +
  • snake produces hello_world!
  • +
+
+
+

Usage

+

Switch the text of the char field name to uppercase:

+
+<field name="name" options="{'transform': 'upper'}" />
+
+

Switch the text of the char field name to lowercase:

+
+<field name="name" options="{'transform': 'lower'}" />
+
+
+
+

Bug Tracker

+

Bugs are tracked on GitHub Issues. +In case of trouble, please check there if your issue has already been reported. +If you spotted it first, help us smashing it by providing a detailed and welcomed +feedback.

+

Do not contact contributors directly about support or help with technical issues.

+
+
+

Credits

+
+

Authors

+
    +
  • Agile Business Group
  • +
+
+
+

Maintainers

+

This module is maintained by the OCA.

+Odoo Community Association +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/web project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js b/web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js new file mode 100644 index 00000000..b458d5c3 --- /dev/null +++ b/web_widget_char_switchcase/static/src/js/web_widget_char_switchcase.js @@ -0,0 +1,111 @@ +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({ + 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'); + }, + initialize_content: function() { + var res = this._super(); + var self = this; + if(this.$input) { + 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) { + return this._super(this.current_transformation_handler(val), def); + }, + format_value: function (val, def) { + return this._super(this.current_transformation_handler(val), def); + }, + get_transformation_handler: function () { + switch (this.current_transformation) { + case 'upper': + // HELLO WORLD! + return function (val) { return val.toUpperCase(); }; + case 'lower': + // hello world! + return function (val) { return val.toLowerCase(); }; + case 'title': + // Hello World! + return function (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) { + 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) { + 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) { + 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) { + return val.toLowerCase().replace(' ', '_'); + }; + case 'default': + default: + return function (val) { return val; }; + } + } + }); +}); diff --git a/web_widget_char_switchcase/static/test/web_widget_char_switchcase.js b/web_widget_char_switchcase/static/test/web_widget_char_switchcase.js new file mode 100644 index 00000000..03d3b4ee --- /dev/null +++ b/web_widget_char_switchcase/static/test/web_widget_char_switchcase.js @@ -0,0 +1,90 @@ +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 field = new web_form_widgets.FieldChar(field_manager, node); + field.$input = $(''); + field.initialize_content(); + return field; + } + + 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); + }); + + test('UPPER OPTION', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'upper'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), orig_val.toUpperCase()); + }); + + test('lower option', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'lower'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), orig_val.toLowerCase()); + }); + + test('Title Option', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'title'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), 'Hello World!'); + }); + + test('Sentence option', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'sentence'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), 'Hello world!'); + }); + + test('camelOption', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'camel'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), 'helloWorld!'); + }); + + test('PascalOption', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'pascal'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), 'HelloWorld!'); + }); + + test('snake_option', function(assert, form_common, core, web_form_widgets) { + var node = {'attrs': {'options': "{'transform': 'snake'}"}}; + this.field = createField(form_common, web_form_widgets, node); + + var orig_val = 'Hello World!'; + this.field.$input.val(orig_val); + this.field.$input.keyup(); + assert.strictEqual(this.field.$input.val(), 'hello_world!'); + }); +}); diff --git a/web_widget_char_switchcase/tests/__init__.py b/web_widget_char_switchcase/tests/__init__.py new file mode 100644 index 00000000..baae077a --- /dev/null +++ b/web_widget_char_switchcase/tests/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from . import test_js diff --git a/web_widget_char_switchcase/tests/test_js.py b/web_widget_char_switchcase/tests/test_js.py new file mode 100644 index 00000000..8019ed7a --- /dev/null +++ b/web_widget_char_switchcase/tests/test_js.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- +# Copyright 2018 Simone Rubino - Agile Business Group +# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). + +from odoo.tests import HttpCase + + +class TestJS(HttpCase): + + def test_js(self): + self.phantom_js( + "/web/tests?module=web_widget_char_switchcase", + "", + login="admin" + ) diff --git a/web_widget_char_switchcase/views/web_widget_char_switchcase.xml b/web_widget_char_switchcase/views/web_widget_char_switchcase.xml new file mode 100644 index 00000000..3d03a872 --- /dev/null +++ b/web_widget_char_switchcase/views/web_widget_char_switchcase.xml @@ -0,0 +1,15 @@ + + + +