From 23caf567db0eb658910bdde7303353d4468960cf Mon Sep 17 00:00:00 2001 From: Nikolina Todorova Date: Wed, 27 May 2015 15:19:19 +0200 Subject: [PATCH] 7.0 add web text widget --- web_text_widget/__init__.py | 21 ++++++ web_text_widget/__openerp__.py | 62 +++++++++++++++++ .../static/src/css/text_limited.css | 8 +++ web_text_widget/static/src/js/text_limited.js | 68 +++++++++++++++++++ .../static/src/xml/text_limited.xml | 8 +++ 5 files changed, 167 insertions(+) create mode 100644 web_text_widget/__init__.py create mode 100644 web_text_widget/__openerp__.py create mode 100644 web_text_widget/static/src/css/text_limited.css create mode 100644 web_text_widget/static/src/js/text_limited.js create mode 100644 web_text_widget/static/src/xml/text_limited.xml diff --git a/web_text_widget/__init__.py b/web_text_widget/__init__.py new file mode 100644 index 00000000..78003df6 --- /dev/null +++ b/web_text_widget/__init__.py @@ -0,0 +1,21 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010-2015 OpenERP s.a. (). +# Copyright (C) 2015 initOS GmbH & Co. KG (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## diff --git a/web_text_widget/__openerp__.py b/web_text_widget/__openerp__.py new file mode 100644 index 00000000..5baf6e41 --- /dev/null +++ b/web_text_widget/__openerp__.py @@ -0,0 +1,62 @@ +# -*- coding: utf-8 -*- +############################################################################## +# +# OpenERP, Open Source Management Solution +# Copyright (C) 2010-2015 OpenERP s.a. (). +# Copyright (C) 2015 initOS GmbH & Co. KG (). +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . +# +############################################################################## +{ + 'name': 'Web text limited widget', + 'version': '0.1.0', + 'author': 'initOS GmbH & Co. KG', + 'category': '', + 'description': """ + +* Add new 'text_limited' widget for TextField, but data are limited to +10 lines or 500 characters (by default). +You can change default values by context varibles 'limit_lines' and +'limit_chars'. +If data contains more characters or lines, it will be cut. +Example of usage: + +""", + 'website': 'http://www.initos.com', + 'license': 'AGPL-3', + 'images': [], + 'depends': [ + 'web', + ], + 'data': [], + 'update_xml': [], + 'js': [ + 'static/src/js/text_limited.js', + ], + 'qweb': [ + 'static/src/xml/text_limited.xml', + ], + 'css': [ + 'static/src/css/text_limited.css', + ], + 'demo': [], + 'test': [], + 'active': False, + 'installable': True, + 'auto_install': True, +} diff --git a/web_text_widget/static/src/css/text_limited.css b/web_text_widget/static/src/css/text_limited.css new file mode 100644 index 00000000..c01c56bd --- /dev/null +++ b/web_text_widget/static/src/css/text_limited.css @@ -0,0 +1,8 @@ +div.oe_form_field.oe_form_field_text { + position: relative; +} + +span.length_limit { + position: absolute; + right: 0; +} diff --git a/web_text_widget/static/src/js/text_limited.js b/web_text_widget/static/src/js/text_limited.js new file mode 100644 index 00000000..d10305d9 --- /dev/null +++ b/web_text_widget/static/src/js/text_limited.js @@ -0,0 +1,68 @@ +openerp.web_text_widget = function (instance) +{ + +var QWeb = instance.web.qweb; +var _t = instance.web._t; + + +instance.web_text_widget.FieldTextLimited = instance.web.form.FieldText.extend( + instance.web.form.ReinitializeFieldMixin, { + template: 'FieldText', + LIMIT_LINES_CONTEXT_KEY: 'limit_lines', + LIMIT_LINES_DEFAULT: 10, + LIMIT_CHARS_CONTEXT_KEY: 'limit_chars', + LIMIT_CHARS_DEFAULT: 500, + + events: { + 'keyup': function (e) { + if (e.which === $.ui.keyCode.ENTER) { + e.stopPropagation(); + } + this.limit_value($(e.target)); + }, + 'change textarea': 'store_dom_value', + }, + + limit_value: function($textarea) + { + var ctx = this.build_context().eval(); + var limit_lines = ctx[this.LIMIT_LINES_CONTEXT_KEY]*1 + if (!limit_lines){ + limit_lines = this.LIMIT_LINES_DEFAULT; + console.log("No default values found for limit lines in '" + + this.name + "' field. Default value " + limit_lines + + " will be used."); + } + + var limit_chars = ctx[this.LIMIT_CHARS_CONTEXT_KEY]*1 + if (!limit_chars){ + limit_chars = this.LIMIT_CHARS_DEFAULT; + console.log("No default values found for limit chars in '" + + this.name + "' field. Default value " + limit_chars + + " will be used."); + } + + var value = $textarea.val(); + var lines = value.split("\n"); + if (lines.length > limit_lines){ + $textarea.val(lines.slice(0, limit_lines).join("\n")); + } + if (value.length > limit_chars){ + $textarea.val(value.slice(0, limit_chars)); + } + this.$el.find('span.length_limit').html(value.length + '/' + limit_chars); + }, + + initialize_content: function() { + return this._super(); + }, + + store_dom_value: function () { + this.limit_value(this.$textarea); + return this._super(); + }, +}); + +instance.web.form.widgets.add('text_limited', + 'instance.web_text_widget.FieldTextLimited'); +}; diff --git a/web_text_widget/static/src/xml/text_limited.xml b/web_text_widget/static/src/xml/text_limited.xml new file mode 100644 index 00000000..314a12f2 --- /dev/null +++ b/web_text_widget/static/src/xml/text_limited.xml @@ -0,0 +1,8 @@ + + + + + + + +