diff --git a/addons/sms/__manifest__.py b/addons/sms/__manifest__.py index 18330c1d793..c47ed128a8a 100644 --- a/addons/sms/__manifest__.py +++ b/addons/sms/__manifest__.py @@ -14,6 +14,10 @@ The service is provided by the In App Purchase Odoo platform. 'data': [ 'wizard/send_sms_views.xml', 'views/res_partner_views.xml', + 'views/templates.xml', + ], + 'qweb': [ + 'static/src/xml/sms_widget.xml', ], 'installable': True, 'auto_install': True, diff --git a/addons/sms/static/src/js/sms_widget.js b/addons/sms/static/src/js/sms_widget.js new file mode 100644 index 00000000000..a01ff3753cf --- /dev/null +++ b/addons/sms/static/src/js/sms_widget.js @@ -0,0 +1,120 @@ +odoo.define('sms.sms_widget', function (require) { +"use strict"; + +var basicFields = require('web.basic_fields'); +var core = require('web.core'); +var fieldRegistry = require('web.field_registry'); +var framework = require('web.framework'); + +var FieldText = basicFields.FieldText; +var QWeb = core.qweb; + +/** + * SmsWidget is a widget to display a textarea (the body) and a text representing + * the number of SMS and the number of characters. This text is computed every + * time the user changes the body. + */ +var SmsWidget = FieldText.extend({ + /** + * @constructor + */ + init: function () { + this._super.apply(this, arguments); + this.nbrChar = 0; + this.nbrSMS = 0; + this.encoding = "GSM7"; + this.tagName = 'div'; + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Compute the number of characters and sms + * @private + */ + _compute: function () { + var content = this._getValue(); + this.encoding = this._extractEncoding(content); + this.nbrChar = content.length; + this.nbrChar += (content.match(/\n/g) || []).length; + this.nbrSMS = this._countSMS(this.nbrChar, this.encoding); + this._renderSMS(); + }, + /** + * Count the number of SMS of the content + * @private + * @returns {integer} Number of SMS + */ + _countSMS: function () { + if (this.nbrChar === 0) { + return 0; + } + if (this.encoding === 'UNICODE') { + if (this.nbrChar <= 70) { + return 1; + } + return Math.ceil(this.nbrChar / 67); + } + if (this.nbrChar <= 160) { + return 1; + } + return Math.ceil(this.nbrChar / 153); + }, + /** + * @private + * @override + */ + _renderEdit: function () { + this.$el.empty(); + this._prepareInput($('